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> 33fcaa681cSLen Brown #include <sys/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; 62c1c10cc7SPu Wen unsigned int hygon_genuine; 6334041551SLen Brown unsigned int max_level, max_extended_level; 64103a8feaSLen Brown unsigned int has_invariant_tsc; 65d7899447SLen Brown unsigned int do_nhm_platform_info; 66cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 67b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 68103a8feaSLen Brown double bclk; 69a2b7b749SLen Brown double base_hz; 7021ed5574SLen Brown unsigned int has_base_hz; 71a2b7b749SLen Brown double tsc_tweak = 1.0; 72c98d5d94SLen Brown unsigned int show_pkg_only; 73c98d5d94SLen Brown unsigned int show_core_only; 74c98d5d94SLen Brown char *output_buffer, *outp; 75889facbeSLen Brown unsigned int do_rapl; 76889facbeSLen Brown unsigned int do_dts; 77889facbeSLen Brown unsigned int do_ptm; 78fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 79be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 80be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 8127d47356SLen Brown unsigned int gfx_cur_mhz; 82b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 83889facbeSLen Brown unsigned int tcc_activation_temp; 84889facbeSLen Brown unsigned int tcc_activation_temp_override; 8540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 87889facbeSLen Brown double rapl_joule_counter_range; 883a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 89ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 903a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 913a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 928a5bdf41SLen Brown unsigned int crystal_hz; 938a5bdf41SLen Brown unsigned long long tsc_hz; 947ce7d5deSPrarit Bhargava int base_cpu; 9521ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 967f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 977f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 987f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 997f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1007f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1017f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 10233148d67SLen Brown unsigned int has_misc_feature_control; 1034c2122d4SLen Brown unsigned int first_counter_read = 1; 104c026c236SArtem Bityutskiy int ignore_stdin; 105889facbeSLen Brown 106889facbeSLen Brown #define RAPL_PKG (1 << 0) 107e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 108e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 109e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 110e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 111e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 112e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 113e6f9bb3cSLen Brown 114889facbeSLen Brown #define RAPL_DRAM (1 << 3) 115e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 116e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 117e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 118e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1190b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1200b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 121e6f9bb3cSLen Brown 1229148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 123e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1240b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 125e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 126e6f9bb3cSLen Brown 1270b2bb692SLen Brown #define RAPL_GFX (1 << 8) 128e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 129e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 130e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1319148494cSJacob Pan 1329148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1339148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1349392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1359392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1369392bd98SCalvin Walton * not per-package. */ 1379392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1389392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1399392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1409392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1419148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 142889facbeSLen Brown #define TJMAX_DEFAULT 100 143889facbeSLen Brown 1449392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1459392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1469392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1479392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1489392bd98SCalvin Walton 149889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 150103a8feaSLen Brown 151388e9c81SLen Brown /* 152388e9c81SLen Brown * buffer size used by sscanf() for added column names 153388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 154388e9c81SLen Brown */ 155388e9c81SLen Brown #define NAME_BYTES 20 156495c7654SLen Brown #define PATH_BYTES 128 157388e9c81SLen Brown 158103a8feaSLen Brown int backwards_count; 159103a8feaSLen Brown char *progname; 160103a8feaSLen Brown 1611ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1621ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1631ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1640748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1650748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1660e2d8f05SLen Brown #define BITMASK_SIZE 32 167c98d5d94SLen Brown 168c98d5d94SLen Brown struct thread_data { 169f4fdf2b4SLen Brown struct timeval tv_begin; 170f4fdf2b4SLen Brown struct timeval tv_end; 171d4794f25SYazen Ghannam struct timeval tv_delta; 172c98d5d94SLen Brown unsigned long long tsc; 173c98d5d94SLen Brown unsigned long long aperf; 174c98d5d94SLen Brown unsigned long long mperf; 175144b44b1SLen Brown unsigned long long c1; 1760de6c0dfSLen Brown unsigned long long irq_count; 1771ed51011SLen Brown unsigned int smi_count; 178c98d5d94SLen Brown unsigned int cpu_id; 1794c2122d4SLen Brown unsigned int apic_id; 1804c2122d4SLen Brown unsigned int x2apic_id; 181c98d5d94SLen Brown unsigned int flags; 182c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 183c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1840748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 185c98d5d94SLen Brown } *thread_even, *thread_odd; 186c98d5d94SLen Brown 187c98d5d94SLen Brown struct core_data { 188c98d5d94SLen Brown unsigned long long c3; 189c98d5d94SLen Brown unsigned long long c6; 190c98d5d94SLen Brown unsigned long long c7; 1910539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 192889facbeSLen Brown unsigned int core_temp_c; 1939392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 194c98d5d94SLen Brown unsigned int core_id; 195678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 196c98d5d94SLen Brown } *core_even, *core_odd; 197c98d5d94SLen Brown 198c98d5d94SLen Brown struct pkg_data { 199c98d5d94SLen Brown unsigned long long pc2; 200c98d5d94SLen Brown unsigned long long pc3; 201c98d5d94SLen Brown unsigned long long pc6; 202c98d5d94SLen Brown unsigned long long pc7; 203ca58710fSKristen Carlson Accardi unsigned long long pc8; 204ca58710fSKristen Carlson Accardi unsigned long long pc9; 205ca58710fSKristen Carlson Accardi unsigned long long pc10; 206be0e54c4SLen Brown unsigned long long cpu_lpi; 207be0e54c4SLen Brown unsigned long long sys_lpi; 2080b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2090b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2100b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2110b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2129185e988SLen Brown long long gfx_rc6_ms; 21327d47356SLen Brown unsigned int gfx_mhz; 214b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 215c98d5d94SLen Brown unsigned int package_id; 2167c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2177c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2187c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2197c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2207c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2217c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 222889facbeSLen Brown unsigned int pkg_temp_c; 223678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 224c98d5d94SLen Brown } *package_even, *package_odd; 225c98d5d94SLen Brown 226c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 227c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 228c98d5d94SLen Brown 22940f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 23040f5cfe7SPrarit Bhargava ((thread_base) + \ 23140f5cfe7SPrarit Bhargava ((pkg_no) * \ 23240f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 23340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 23440f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 23540f5cfe7SPrarit Bhargava (thread_no)) 23640f5cfe7SPrarit Bhargava 23740f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 23840f5cfe7SPrarit Bhargava ((core_base) + \ 23940f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 24040f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 24140f5cfe7SPrarit Bhargava (core_no)) 24240f5cfe7SPrarit Bhargava 24340f5cfe7SPrarit Bhargava 244c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 245c98d5d94SLen Brown 246388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 24741618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 248388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 249388e9c81SLen Brown 250388e9c81SLen Brown struct msr_counter { 251388e9c81SLen Brown unsigned int msr_num; 252388e9c81SLen Brown char name[NAME_BYTES]; 253495c7654SLen Brown char path[PATH_BYTES]; 254388e9c81SLen Brown unsigned int width; 255388e9c81SLen Brown enum counter_type type; 256388e9c81SLen Brown enum counter_format format; 257388e9c81SLen Brown struct msr_counter *next; 258812db3f7SLen Brown unsigned int flags; 259812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 260812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 26141618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 262388e9c81SLen Brown }; 263388e9c81SLen Brown 26487e15da9SChen Yu /* 26587e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 26687e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 26787e15da9SChen Yu * despite its register's bit width. 26887e15da9SChen Yu */ 26987e15da9SChen Yu enum { 27087e15da9SChen Yu IDX_PKG_ENERGY, 27187e15da9SChen Yu IDX_DRAM_ENERGY, 27287e15da9SChen Yu IDX_PP0_ENERGY, 27387e15da9SChen Yu IDX_PP1_ENERGY, 27487e15da9SChen Yu IDX_PKG_PERF, 27587e15da9SChen Yu IDX_DRAM_PERF, 27687e15da9SChen Yu IDX_COUNT, 27787e15da9SChen Yu }; 27887e15da9SChen Yu 27987e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 28087e15da9SChen Yu 28187e15da9SChen Yu struct msr_sum_array { 28287e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 28387e15da9SChen Yu struct { 28487e15da9SChen Yu /*The accumulated MSR value is updated by the timer*/ 28587e15da9SChen Yu unsigned long long sum; 28687e15da9SChen Yu /*The MSR footprint recorded in last timer*/ 28787e15da9SChen Yu unsigned long long last; 28887e15da9SChen Yu } entries[IDX_COUNT]; 28987e15da9SChen Yu }; 29087e15da9SChen Yu 29187e15da9SChen Yu /* The percpu MSR sum array.*/ 29287e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 29387e15da9SChen Yu 29487e15da9SChen Yu int idx_to_offset(int idx) 29587e15da9SChen Yu { 29687e15da9SChen Yu int offset; 29787e15da9SChen Yu 29887e15da9SChen Yu switch (idx) { 29987e15da9SChen Yu case IDX_PKG_ENERGY: 30087e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 30187e15da9SChen Yu break; 30287e15da9SChen Yu case IDX_DRAM_ENERGY: 30387e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 30487e15da9SChen Yu break; 30587e15da9SChen Yu case IDX_PP0_ENERGY: 30687e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 30787e15da9SChen Yu break; 30887e15da9SChen Yu case IDX_PP1_ENERGY: 30987e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 31087e15da9SChen Yu break; 31187e15da9SChen Yu case IDX_PKG_PERF: 31287e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 31387e15da9SChen Yu break; 31487e15da9SChen Yu case IDX_DRAM_PERF: 31587e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 31687e15da9SChen Yu break; 31787e15da9SChen Yu default: 31887e15da9SChen Yu offset = -1; 31987e15da9SChen Yu } 32087e15da9SChen Yu return offset; 32187e15da9SChen Yu } 32287e15da9SChen Yu 32387e15da9SChen Yu int offset_to_idx(int offset) 32487e15da9SChen Yu { 32587e15da9SChen Yu int idx; 32687e15da9SChen Yu 32787e15da9SChen Yu switch (offset) { 32887e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 32987e15da9SChen Yu idx = IDX_PKG_ENERGY; 33087e15da9SChen Yu break; 33187e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 33287e15da9SChen Yu idx = IDX_DRAM_ENERGY; 33387e15da9SChen Yu break; 33487e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 33587e15da9SChen Yu idx = IDX_PP0_ENERGY; 33687e15da9SChen Yu break; 33787e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 33887e15da9SChen Yu idx = IDX_PP1_ENERGY; 33987e15da9SChen Yu break; 34087e15da9SChen Yu case MSR_PKG_PERF_STATUS: 34187e15da9SChen Yu idx = IDX_PKG_PERF; 34287e15da9SChen Yu break; 34387e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 34487e15da9SChen Yu idx = IDX_DRAM_PERF; 34587e15da9SChen Yu break; 34687e15da9SChen Yu default: 34787e15da9SChen Yu idx = -1; 34887e15da9SChen Yu } 34987e15da9SChen Yu return idx; 35087e15da9SChen Yu } 35187e15da9SChen Yu 35287e15da9SChen Yu int idx_valid(int idx) 35387e15da9SChen Yu { 35487e15da9SChen Yu switch (idx) { 35587e15da9SChen Yu case IDX_PKG_ENERGY: 35687e15da9SChen Yu return do_rapl & RAPL_PKG; 35787e15da9SChen Yu case IDX_DRAM_ENERGY: 35887e15da9SChen Yu return do_rapl & RAPL_DRAM; 35987e15da9SChen Yu case IDX_PP0_ENERGY: 36087e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 36187e15da9SChen Yu case IDX_PP1_ENERGY: 36287e15da9SChen Yu return do_rapl & RAPL_GFX; 36387e15da9SChen Yu case IDX_PKG_PERF: 36487e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 36587e15da9SChen Yu case IDX_DRAM_PERF: 36687e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 36787e15da9SChen Yu default: 36887e15da9SChen Yu return 0; 36987e15da9SChen Yu } 37087e15da9SChen Yu } 371388e9c81SLen Brown struct sys_counters { 372678a3bd1SLen Brown unsigned int added_thread_counters; 373678a3bd1SLen Brown unsigned int added_core_counters; 374678a3bd1SLen Brown unsigned int added_package_counters; 375388e9c81SLen Brown struct msr_counter *tp; 376388e9c81SLen Brown struct msr_counter *cp; 377388e9c81SLen Brown struct msr_counter *pp; 378388e9c81SLen Brown } sys; 379388e9c81SLen Brown 380c98d5d94SLen Brown struct system_summary { 381c98d5d94SLen Brown struct thread_data threads; 382c98d5d94SLen Brown struct core_data cores; 383c98d5d94SLen Brown struct pkg_data packages; 384388e9c81SLen Brown } average; 385c98d5d94SLen Brown 3860e2d8f05SLen Brown struct cpu_topology { 3870e2d8f05SLen Brown int physical_package_id; 3886de68fe1SLen Brown int die_id; 3890e2d8f05SLen Brown int logical_cpu_id; 390ef605741SPrarit Bhargava int physical_node_id; 391ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 3920e2d8f05SLen Brown int physical_core_id; 3938cb48b32SPrarit Bhargava int thread_id; 3940e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 3950e2d8f05SLen Brown } *cpus; 396c98d5d94SLen Brown 397c98d5d94SLen Brown struct topo_params { 398c98d5d94SLen Brown int num_packages; 3996de68fe1SLen Brown int num_die; 400103a8feaSLen Brown int num_cpus; 401c98d5d94SLen Brown int num_cores; 402c98d5d94SLen Brown int max_cpu_num; 403ef605741SPrarit Bhargava int max_node_num; 40470a9c6e8SPrarit Bhargava int nodes_per_pkg; 40570a9c6e8SPrarit Bhargava int cores_per_node; 40670a9c6e8SPrarit Bhargava int threads_per_core; 407c98d5d94SLen Brown } topo; 408103a8feaSLen Brown 409c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 410103a8feaSLen Brown 411562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 412562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 413562a2d37SLen Brown 414c98d5d94SLen Brown void setup_all_buffers(void); 415103a8feaSLen Brown 4161f81c5efSLen Brown char *sys_lpi_file; 4171f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4181f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4191f81c5efSLen Brown 420c98d5d94SLen Brown int cpu_is_not_present(int cpu) 421d15cf7c1SLen Brown { 422c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 423c98d5d94SLen Brown } 424c98d5d94SLen Brown /* 425c98d5d94SLen Brown * run func(thread, core, package) in topology order 426c98d5d94SLen Brown * skip non-present cpus 427c98d5d94SLen Brown */ 428c98d5d94SLen Brown 429c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 430c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 431c98d5d94SLen Brown { 43240f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 433c98d5d94SLen Brown 434c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 435df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 43670a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 437c98d5d94SLen Brown for (thread_no = 0; thread_no < 43870a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 439c98d5d94SLen Brown struct thread_data *t; 440c98d5d94SLen Brown struct core_data *c; 441c98d5d94SLen Brown struct pkg_data *p; 442c98d5d94SLen Brown 44340f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 44440f5cfe7SPrarit Bhargava core_no, node_no, 44540f5cfe7SPrarit Bhargava pkg_no); 446c98d5d94SLen Brown 447c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 448c98d5d94SLen Brown continue; 449c98d5d94SLen Brown 45040f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 45140f5cfe7SPrarit Bhargava node_no, pkg_no); 452c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 453c98d5d94SLen Brown 454c98d5d94SLen Brown retval = func(t, c, p); 455c98d5d94SLen Brown if (retval) 456c98d5d94SLen Brown return retval; 457c98d5d94SLen Brown } 458c98d5d94SLen Brown } 459c98d5d94SLen Brown } 46040f5cfe7SPrarit Bhargava } 461d15cf7c1SLen Brown return 0; 462d15cf7c1SLen Brown } 463d15cf7c1SLen Brown 46488c3281fSLen Brown int cpu_migrate(int cpu) 46588c3281fSLen Brown { 466c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 467c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 468c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 46988c3281fSLen Brown return -1; 47088c3281fSLen Brown else 47188c3281fSLen Brown return 0; 47288c3281fSLen Brown } 47336229897SLen Brown int get_msr_fd(int cpu) 474103a8feaSLen Brown { 475103a8feaSLen Brown char pathname[32]; 476103a8feaSLen Brown int fd; 477103a8feaSLen Brown 47836229897SLen Brown fd = fd_percpu[cpu]; 47936229897SLen Brown 48036229897SLen Brown if (fd) 48136229897SLen Brown return fd; 48236229897SLen Brown 483103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 484103a8feaSLen Brown fd = open(pathname, O_RDONLY); 48515aaa346SLen Brown if (fd < 0) 48698481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 487103a8feaSLen Brown 48836229897SLen Brown fd_percpu[cpu] = fd; 48936229897SLen Brown 49036229897SLen Brown return fd; 49136229897SLen Brown } 49236229897SLen Brown 49336229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 49436229897SLen Brown { 49536229897SLen Brown ssize_t retval; 49636229897SLen Brown 49736229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 49815aaa346SLen Brown 49998481e79SLen Brown if (retval != sizeof *msr) 500cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 50115aaa346SLen Brown 50215aaa346SLen Brown return 0; 503103a8feaSLen Brown } 504103a8feaSLen Brown 505fc04cc67SLen Brown /* 506bdd5ae3aSLen Brown * This list matches the column headers, except 507bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 508bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 509bdd5ae3aSLen Brown * matching on them for --show and --hide. 510fc04cc67SLen Brown */ 511812db3f7SLen Brown struct msr_counter bic[] = { 5123f44a5c6SLen Brown { 0x0, "usec" }, 5133f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 514812db3f7SLen Brown { 0x0, "Package" }, 515bdd5ae3aSLen Brown { 0x0, "Node" }, 516812db3f7SLen Brown { 0x0, "Avg_MHz" }, 517bdd5ae3aSLen Brown { 0x0, "Busy%" }, 518812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 519812db3f7SLen Brown { 0x0, "TSC_MHz" }, 520812db3f7SLen Brown { 0x0, "IRQ" }, 521495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 522bdd5ae3aSLen Brown { 0x0, "sysfs" }, 523812db3f7SLen Brown { 0x0, "CPU%c1" }, 524812db3f7SLen Brown { 0x0, "CPU%c3" }, 525812db3f7SLen Brown { 0x0, "CPU%c6" }, 526812db3f7SLen Brown { 0x0, "CPU%c7" }, 527812db3f7SLen Brown { 0x0, "ThreadC" }, 528812db3f7SLen Brown { 0x0, "CoreTmp" }, 529812db3f7SLen Brown { 0x0, "CoreCnt" }, 530812db3f7SLen Brown { 0x0, "PkgTmp" }, 531812db3f7SLen Brown { 0x0, "GFX%rc6" }, 532812db3f7SLen Brown { 0x0, "GFXMHz" }, 533812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 534812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 535812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 536812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5370f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5380f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5394bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 540be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 541be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 542812db3f7SLen Brown { 0x0, "PkgWatt" }, 543812db3f7SLen Brown { 0x0, "CorWatt" }, 544812db3f7SLen Brown { 0x0, "GFXWatt" }, 545812db3f7SLen Brown { 0x0, "PkgCnt" }, 546812db3f7SLen Brown { 0x0, "RAMWatt" }, 547812db3f7SLen Brown { 0x0, "PKG_%" }, 548812db3f7SLen Brown { 0x0, "RAM_%" }, 549812db3f7SLen Brown { 0x0, "Pkg_J" }, 550812db3f7SLen Brown { 0x0, "Cor_J" }, 551812db3f7SLen Brown { 0x0, "GFX_J" }, 552812db3f7SLen Brown { 0x0, "RAM_J" }, 5530539ba11SLen Brown { 0x0, "Mod%c6" }, 554a99d8730SLen Brown { 0x0, "Totl%C0" }, 555a99d8730SLen Brown { 0x0, "Any%C0" }, 556a99d8730SLen Brown { 0x0, "GFX%C0" }, 557a99d8730SLen Brown { 0x0, "CPUGFX%" }, 558bdd5ae3aSLen Brown { 0x0, "Core" }, 559bdd5ae3aSLen Brown { 0x0, "CPU" }, 5604c2122d4SLen Brown { 0x0, "APIC" }, 5614c2122d4SLen Brown { 0x0, "X2APIC" }, 5626de68fe1SLen Brown { 0x0, "Die" }, 563b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 564812db3f7SLen Brown }; 565812db3f7SLen Brown 566812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 5673f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 5683f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 5693f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 570bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 571bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 572bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 573bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 574bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 575bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 576bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 577bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 578bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 579bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 580bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 581bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 582bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 583bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 584bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 585bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 586bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 587bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 588bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 589bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 590bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 591bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 592bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 593bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 594bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 595bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 596bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 597bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 598bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 599bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 600bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 601bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 602bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 603bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 604bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 605bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 606bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 607bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 608bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 609bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 610bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 611bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 612bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 613bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 614bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6154c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6164c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6176de68fe1SLen Brown #define BIC_Die (1ULL << 50) 618b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 619812db3f7SLen Brown 6204c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6213f44a5c6SLen Brown 6223f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6234c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 624812db3f7SLen Brown 625812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6261e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6273f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 628812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6290f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 630812db3f7SLen Brown 6313f44a5c6SLen Brown 632dd778a5eSLen Brown #define MAX_DEFERRED 16 633dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 634dd778a5eSLen Brown int deferred_skip_index; 635dd778a5eSLen Brown 636dd778a5eSLen Brown /* 637dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 638dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 639dd778a5eSLen Brown */ 640dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 641dd778a5eSLen Brown 642dd778a5eSLen Brown void help(void) 643dd778a5eSLen Brown { 644dd778a5eSLen Brown fprintf(outf, 645dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 646dd778a5eSLen Brown "\n" 647dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 648dd778a5eSLen Brown "when COMMAND completes.\n" 649dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 650dd778a5eSLen Brown "to print statistics, until interrupted.\n" 651cc481650SNathan Ciobanu " -a, --add add a counter\n" 652dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 653cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 654dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 6559ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 6569ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 6579ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 6589ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 6599ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 6609ce80578SNathan Ciobanu " hide the specified column(s)\n" 661cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 662cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 6639ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 664cc481650SNathan Ciobanu " -l, --list list column headers only\n" 665cc481650SNathan Ciobanu " -n, --num_iterations num\n" 666cc481650SNathan Ciobanu " number of the measurement iterations\n" 667cc481650SNathan Ciobanu " -o, --out file\n" 668cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 669cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 6709ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 6719ce80578SNathan Ciobanu " show only the specified column(s)\n" 6729ce80578SNathan Ciobanu " -S, --Summary\n" 6739ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 6749ce80578SNathan Ciobanu " -T, --TCC temperature\n" 6759ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 6769ce80578SNathan Ciobanu " degrees Celsius\n" 677cc481650SNathan Ciobanu " -h, --help print this help message\n" 678cc481650SNathan Ciobanu " -v, --version print version information\n" 679dd778a5eSLen Brown "\n" 680dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 681dd778a5eSLen Brown } 682dd778a5eSLen Brown 683812db3f7SLen Brown /* 684812db3f7SLen Brown * bic_lookup 685812db3f7SLen Brown * for all the strings in comma separate name_list, 686812db3f7SLen Brown * set the approprate bit in return value. 687812db3f7SLen Brown */ 688dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 689812db3f7SLen Brown { 690812db3f7SLen Brown int i; 691812db3f7SLen Brown unsigned long long retval = 0; 692812db3f7SLen Brown 693812db3f7SLen Brown while (name_list) { 694812db3f7SLen Brown char *comma; 695812db3f7SLen Brown 696812db3f7SLen Brown comma = strchr(name_list, ','); 697812db3f7SLen Brown 698812db3f7SLen Brown if (comma) 699812db3f7SLen Brown *comma = '\0'; 700812db3f7SLen Brown 7013f44a5c6SLen Brown if (!strcmp(name_list, "all")) 7023f44a5c6SLen Brown return ~0; 7033f44a5c6SLen Brown 704812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 705812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 706812db3f7SLen Brown retval |= (1ULL << i); 707812db3f7SLen Brown break; 708812db3f7SLen Brown } 709812db3f7SLen Brown } 710812db3f7SLen Brown if (i == MAX_BIC) { 711dd778a5eSLen Brown if (mode == SHOW_LIST) { 712812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 713812db3f7SLen Brown exit(-1); 714812db3f7SLen Brown } 715dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 716dd778a5eSLen Brown if (debug) 717dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 718dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 719dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 720dd778a5eSLen Brown MAX_DEFERRED, name_list); 721dd778a5eSLen Brown help(); 722dd778a5eSLen Brown exit(1); 723dd778a5eSLen Brown } 724dd778a5eSLen Brown } 725812db3f7SLen Brown 726812db3f7SLen Brown name_list = comma; 727812db3f7SLen Brown if (name_list) 728812db3f7SLen Brown name_list++; 729812db3f7SLen Brown 730812db3f7SLen Brown } 731812db3f7SLen Brown return retval; 732812db3f7SLen Brown } 733fc04cc67SLen Brown 734dd778a5eSLen Brown 735c8ade361SLen Brown void print_header(char *delim) 736103a8feaSLen Brown { 737388e9c81SLen Brown struct msr_counter *mp; 7386168c2e0SLen Brown int printed = 0; 739388e9c81SLen Brown 7403f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 7413f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 7423f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 7433f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 744812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7456168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 7466de68fe1SLen Brown if (DO_BIC(BIC_Die)) 7476de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 74801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 74901235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 750812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7516168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 752812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 7536168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 7544c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 7554c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 7564c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 7574c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 758812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 7596168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 760812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 7616168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 762812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 7636168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 764812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 7656168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 7661cc21f7bSLen Brown 7670de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 7680de6c0dfSLen Brown if (sums_need_wide_columns) 7696168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 7700de6c0dfSLen Brown else 7716168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 7720de6c0dfSLen Brown } 7730de6c0dfSLen Brown 774812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 7756168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 7761cc21f7bSLen Brown 777388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 778dd778a5eSLen Brown 779388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 780388e9c81SLen Brown if (mp->width == 64) 781dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 782388e9c81SLen Brown else 783dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 784388e9c81SLen Brown } else { 7850de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 786dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 7870de6c0dfSLen Brown else 788dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 789388e9c81SLen Brown } 790388e9c81SLen Brown } 791388e9c81SLen Brown 79241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 7936168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 794562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 7956168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 796812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 7976168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 798812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 7996168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 800678a3bd1SLen Brown 8010539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8026168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 803678a3bd1SLen Brown 804812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8056168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 806388e9c81SLen Brown 8079392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8089392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8099392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8109392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8119392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8129392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8139392bd98SCalvin Walton } 8149392bd98SCalvin Walton 815388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 816388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 817388e9c81SLen Brown if (mp->width == 64) 818c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 819388e9c81SLen Brown else 820c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 821388e9c81SLen Brown } else { 8220de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8230de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8240de6c0dfSLen Brown else 825c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 826388e9c81SLen Brown } 827388e9c81SLen Brown } 828388e9c81SLen Brown 829812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8306168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 831889facbeSLen Brown 832812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 8336168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 834fdf676e5SLen Brown 835812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8366168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 83727d47356SLen Brown 838b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 839b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 840b4b91569SRafael Antognolli 841a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 8426168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 843a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 8446168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 845a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 8466168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 847a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 8486168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 8490b2bb692SLen Brown 8500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 8516168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 8520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 8536168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 8540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 8556168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 8560f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 8576168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 8580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 8596168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 8600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 8616168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 8620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 8636168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 864be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 865be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 866be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 867be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 868103a8feaSLen Brown 8695c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 870812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 8716168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 8729392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 8736168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 874812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 8756168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 876812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 8776168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 878812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 8796168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 880812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 8816168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 882d7899447SLen Brown } else if (do_rapl && rapl_joules) { 883812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 8846168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 8859392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 8866168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 887812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 8886168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 889812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 8906168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 891812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 893812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 8955c56be9aSDirk Brandewie } 896388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 897388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 898388e9c81SLen Brown if (mp->width == 64) 899c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 900388e9c81SLen Brown else 901c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 902388e9c81SLen Brown } else { 9030de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9040de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9050de6c0dfSLen Brown else 906c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 907388e9c81SLen Brown } 908388e9c81SLen Brown } 909388e9c81SLen Brown 910c98d5d94SLen Brown outp += sprintf(outp, "\n"); 911103a8feaSLen Brown } 912103a8feaSLen Brown 913c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 914c98d5d94SLen Brown struct pkg_data *p) 915103a8feaSLen Brown { 916388e9c81SLen Brown int i; 917388e9c81SLen Brown struct msr_counter *mp; 918388e9c81SLen Brown 9193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 920c98d5d94SLen Brown 921c98d5d94SLen Brown if (t) { 9223b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 9233b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 9243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9286886fee4SLen Brown 929812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 9300de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 931812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 932218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 933388e9c81SLen Brown 934388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 935388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 936388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 937388e9c81SLen Brown } 938103a8feaSLen Brown } 939103a8feaSLen Brown 940c98d5d94SLen Brown if (c) { 9413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 9423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 9433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 9443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 9453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 9469392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 947388e9c81SLen Brown 948388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 949388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 950388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 951388e9c81SLen Brown } 9520539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 953c98d5d94SLen Brown } 954103a8feaSLen Brown 955c98d5d94SLen Brown if (p) { 9563b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 9570b2bb692SLen Brown 9580b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 9590b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 9600b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 9610b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 9620b2bb692SLen Brown 9633b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 9640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9653b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 9660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9673b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 9680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9693b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 9703b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 9713b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 9723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 973be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 974be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 9757c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 9767c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 9777c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 9787c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 9797c2ccc50SChen Yu outp += sprintf(outp, "Throttle PKG: %0llX\n", 9803b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 9817c2ccc50SChen Yu outp += sprintf(outp, "Throttle RAM: %0llX\n", 9823b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 9833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 984388e9c81SLen Brown 985388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 986388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 987388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 988388e9c81SLen Brown } 989c98d5d94SLen Brown } 9903b4d5c7fSAndy Shevchenko 9913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 9923b4d5c7fSAndy Shevchenko 993c98d5d94SLen Brown return 0; 994103a8feaSLen Brown } 995103a8feaSLen Brown 996e23da037SLen Brown /* 997e23da037SLen Brown * column formatting convention & formats 998e23da037SLen Brown */ 999c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 1000c98d5d94SLen Brown struct pkg_data *p) 1001103a8feaSLen Brown { 1002008d396eSLen Brown double interval_float, tsc; 1003fc04cc67SLen Brown char *fmt8; 1004388e9c81SLen Brown int i; 1005388e9c81SLen Brown struct msr_counter *mp; 10066168c2e0SLen Brown char *delim = "\t"; 10076168c2e0SLen Brown int printed = 0; 1008103a8feaSLen Brown 1009c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1010c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1011c98d5d94SLen Brown return 0; 1012c98d5d94SLen Brown 1013c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1014c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1015c98d5d94SLen Brown return 0; 1016c98d5d94SLen Brown 10171ef7d21aSLen Brown /*if not summary line and --cpu is used */ 10181ef7d21aSLen Brown if ((t != &average.threads) && 10191ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10201ef7d21aSLen Brown return 0; 10211ef7d21aSLen Brown 10223f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1023f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1024f4fdf2b4SLen Brown struct timeval tv; 1025f4fdf2b4SLen Brown 1026f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1027f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1028f4fdf2b4SLen Brown } 1029f4fdf2b4SLen Brown 10303f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10313f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10323f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 10333f44a5c6SLen Brown 1034d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0; 1035103a8feaSLen Brown 1036008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1037008d396eSLen Brown 1038c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1039c98d5d94SLen Brown if (t == &average.threads) { 1040812db3f7SLen Brown if (DO_BIC(BIC_Package)) 10416168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10426de68fe1SLen Brown if (DO_BIC(BIC_Die)) 10436de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 104401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 104501235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1046812db3f7SLen Brown if (DO_BIC(BIC_Core)) 10476168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1048812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10496168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10504c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10514c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10524c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10534c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1054103a8feaSLen Brown } else { 1055812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1056c98d5d94SLen Brown if (p) 10576168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1058c98d5d94SLen Brown else 10596168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1060c98d5d94SLen Brown } 10616de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 10626de68fe1SLen Brown if (c) 10636de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 10646de68fe1SLen Brown else 10656de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10666de68fe1SLen Brown } 106701235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 106801235041SPrarit Bhargava if (t) 106901235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 107001235041SPrarit Bhargava (printed++ ? delim : ""), 107101235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 107201235041SPrarit Bhargava else 107301235041SPrarit Bhargava outp += sprintf(outp, "%s-", 107401235041SPrarit Bhargava (printed++ ? delim : "")); 107501235041SPrarit Bhargava } 1076812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1077c98d5d94SLen Brown if (c) 10786168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1079c98d5d94SLen Brown else 10806168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1081c98d5d94SLen Brown } 1082812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10836168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 10844c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10854c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 10864c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10874c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1088103a8feaSLen Brown } 1089fc04cc67SLen Brown 1090812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 10916168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1092fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 1093fc04cc67SLen Brown 1094812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 10956168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 1096103a8feaSLen Brown 1097812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 109821ed5574SLen Brown if (has_base_hz) 10996168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 110021ed5574SLen Brown else 11016168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1102008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 110321ed5574SLen Brown } 1104103a8feaSLen Brown 1105812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11066168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 1107103a8feaSLen Brown 1108562a2d37SLen Brown /* IRQ */ 11090de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11100de6c0dfSLen Brown if (sums_need_wide_columns) 11116168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11120de6c0dfSLen Brown else 11136168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11140de6c0dfSLen Brown } 1115562a2d37SLen Brown 11161cc21f7bSLen Brown /* SMI */ 1117812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11186168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11191cc21f7bSLen Brown 1120678a3bd1SLen Brown /* Added counters */ 1121678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1122678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1123678a3bd1SLen Brown if (mp->width == 32) 11245f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 1125678a3bd1SLen Brown else 11266168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1127678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11280de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11296168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11300de6c0dfSLen Brown else 11316168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1132678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 113341618e63SLen Brown if (mp->type == COUNTER_USEC) 11346168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 113541618e63SLen Brown else 11366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 1137678a3bd1SLen Brown } 1138678a3bd1SLen Brown } 1139678a3bd1SLen Brown 114041618e63SLen Brown /* C1 */ 114141618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 11426168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 114341618e63SLen Brown 114441618e63SLen Brown 1145c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1146c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1147c98d5d94SLen Brown goto done; 1148c98d5d94SLen Brown 1149562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 11506168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1151812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 11526168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1153812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 11546168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1155c98d5d94SLen Brown 11560539ba11SLen Brown /* Mod%c6 */ 11570539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 11586168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 11590539ba11SLen Brown 1160812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 11616168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1162889facbeSLen Brown 1163388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1164388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1165388e9c81SLen Brown if (mp->width == 32) 11665f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1167388e9c81SLen Brown else 11686168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1169388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 11700de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11716168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 11720de6c0dfSLen Brown else 11736168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1174388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 11756168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1176388e9c81SLen Brown } 1177388e9c81SLen Brown } 1178388e9c81SLen Brown 11799392bd98SCalvin Walton fmt8 = "%s%.2f"; 11809392bd98SCalvin Walton 11819392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 11829392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 11839392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 11849392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 11859392bd98SCalvin Walton 1186c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1187c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1188c98d5d94SLen Brown goto done; 1189c98d5d94SLen Brown 11900b2bb692SLen Brown /* PkgTmp */ 1191812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 11926168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1193889facbeSLen Brown 1194fdf676e5SLen Brown /* GFXrc6 */ 1195812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1196ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 11976168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 11989185e988SLen Brown } else { 11996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12009185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12019185e988SLen Brown } 12029185e988SLen Brown } 1203fdf676e5SLen Brown 120427d47356SLen Brown /* GFXMHz */ 1205812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 12066168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 120727d47356SLen Brown 1208b4b91569SRafael Antognolli /* GFXACTMHz */ 1209b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1210b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1211b4b91569SRafael Antognolli 12120b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1213a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12146168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1215a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12166168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1217a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12186168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1219a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12206168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 12210b2bb692SLen Brown 12220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 12240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 12260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 12280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 12296168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 12300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 12316168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 12320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 12336168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 12340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 12356168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1236889facbeSLen Brown 1237be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1238be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1239be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1240be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1241be0e54c4SLen Brown 1242812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 12436168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 12449392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 12456168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1246812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 12476168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1248812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 12496168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1250812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 12516168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 12529392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 12536168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1254812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 12556168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1256812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 12576168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1258812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 12596168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1260812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 12616168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1262812db3f7SLen Brown 1263388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1264388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1265388e9c81SLen Brown if (mp->width == 32) 12665f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1267388e9c81SLen Brown else 12686168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1269388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12700de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12716168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 12720de6c0dfSLen Brown else 12736168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1274388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12756168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1276388e9c81SLen Brown } 1277388e9c81SLen Brown } 1278388e9c81SLen Brown 1279c98d5d94SLen Brown done: 128094d6ab4bSLen Brown if (*(outp - 1) != '\n') 1281c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1282c98d5d94SLen Brown 1283c98d5d94SLen Brown return 0; 1284103a8feaSLen Brown } 1285103a8feaSLen Brown 1286b7d8c148SLen Brown void flush_output_stdout(void) 1287103a8feaSLen Brown { 1288b7d8c148SLen Brown FILE *filep; 1289b7d8c148SLen Brown 1290b7d8c148SLen Brown if (outf == stderr) 1291b7d8c148SLen Brown filep = stdout; 1292b7d8c148SLen Brown else 1293b7d8c148SLen Brown filep = outf; 1294b7d8c148SLen Brown 1295b7d8c148SLen Brown fputs(output_buffer, filep); 1296b7d8c148SLen Brown fflush(filep); 1297b7d8c148SLen Brown 1298c98d5d94SLen Brown outp = output_buffer; 1299c98d5d94SLen Brown } 1300b7d8c148SLen Brown void flush_output_stderr(void) 1301c98d5d94SLen Brown { 1302b7d8c148SLen Brown fputs(output_buffer, outf); 1303b7d8c148SLen Brown fflush(outf); 1304c98d5d94SLen Brown outp = output_buffer; 1305c98d5d94SLen Brown } 1306c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1307c98d5d94SLen Brown { 1308e23da037SLen Brown static int printed; 1309103a8feaSLen Brown 1310e23da037SLen Brown if (!printed || !summary_only) 1311c8ade361SLen Brown print_header("\t"); 1312103a8feaSLen Brown 13139d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1314103a8feaSLen Brown 1315e23da037SLen Brown printed = 1; 1316e23da037SLen Brown 1317e23da037SLen Brown if (summary_only) 1318e23da037SLen Brown return; 1319e23da037SLen Brown 1320c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1321103a8feaSLen Brown } 1322103a8feaSLen Brown 1323889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 13247c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1325889facbeSLen Brown 1326ba3dec99SLen Brown int 1327c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1328103a8feaSLen Brown { 1329388e9c81SLen Brown int i; 1330388e9c81SLen Brown struct msr_counter *mp; 13310b2bb692SLen Brown 1332a99d8730SLen Brown 1333a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13340b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1335a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13360b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1337a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13380b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1339a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13400b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1341a99d8730SLen Brown 1342c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 13430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1344c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 13450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1346c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 13470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1348c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1349ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1350ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1351ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1352be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1353be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1354889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1355889facbeSLen Brown 13569185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 13579185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 13589185e988SLen Brown old->gfx_rc6_ms = -1; 13599185e988SLen Brown else 1360fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 13619185e988SLen Brown 136227d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1363b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 136427d47356SLen Brown 136587e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 136687e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 136787e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 136887e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 136987e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 137087e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1371ba3dec99SLen Brown 1372388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1373388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1374388e9c81SLen Brown old->counter[i] = new->counter[i]; 1375388e9c81SLen Brown else 1376388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1377388e9c81SLen Brown } 1378388e9c81SLen Brown 1379ba3dec99SLen Brown return 0; 1380103a8feaSLen Brown } 1381103a8feaSLen Brown 1382c98d5d94SLen Brown void 1383c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1384c98d5d94SLen Brown { 1385388e9c81SLen Brown int i; 1386388e9c81SLen Brown struct msr_counter *mp; 1387388e9c81SLen Brown 1388c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1389c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1390c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1391889facbeSLen Brown old->core_temp_c = new->core_temp_c; 13920539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1393388e9c81SLen Brown 13949392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 13959392bd98SCalvin Walton 1396388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1397388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1398388e9c81SLen Brown old->counter[i] = new->counter[i]; 1399388e9c81SLen Brown else 1400388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1401388e9c81SLen Brown } 1402103a8feaSLen Brown } 1403c98d5d94SLen Brown 14041e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 14051e9042b9SSrinivas Pandruvada { 14061e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 14071e9042b9SSrinivas Pandruvada return 0; 14081e9042b9SSrinivas Pandruvada 14091e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 14101e9042b9SSrinivas Pandruvada } 14111e9042b9SSrinivas Pandruvada 1412c3ae331dSLen Brown /* 1413c3ae331dSLen Brown * old = new - old 1414c3ae331dSLen Brown */ 1415ba3dec99SLen Brown int 1416c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1417c98d5d94SLen Brown struct core_data *core_delta) 1418c98d5d94SLen Brown { 1419388e9c81SLen Brown int i; 1420388e9c81SLen Brown struct msr_counter *mp; 1421388e9c81SLen Brown 14224c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 14234c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14244c2122d4SLen Brown new->apic_id = old->apic_id; 14254c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14264c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 14274c2122d4SLen Brown 14283f44a5c6SLen Brown /* 14293f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 14303f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 14313f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 14323f44a5c6SLen Brown */ 14333f44a5c6SLen Brown 1434d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 14353f44a5c6SLen Brown old->tv_begin = new->tv_begin; 14363f44a5c6SLen Brown old->tv_end = new->tv_end; 14373f44a5c6SLen Brown 1438c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1439c98d5d94SLen Brown 1440103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1441b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1442b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1443b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1444b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1445103a8feaSLen Brown 1446c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1447c98d5d94SLen Brown 14481e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 14491e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 1450c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1451c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1452c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1453c98d5d94SLen Brown } else { 1454ba3dec99SLen Brown return -1; 1455103a8feaSLen Brown } 1456a729617cSLen Brown } 1457103a8feaSLen Brown 1458c98d5d94SLen Brown 1459144b44b1SLen Brown if (use_c1_residency_msr) { 1460144b44b1SLen Brown /* 1461144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1462144b44b1SLen Brown * which should be more accurate than the derivation below. 1463144b44b1SLen Brown */ 1464144b44b1SLen Brown } else { 1465103a8feaSLen Brown /* 1466c3ae331dSLen Brown * As counter collection is not atomic, 1467c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1468103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1469103a8feaSLen Brown */ 147095149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1471c98d5d94SLen Brown old->c1 = 0; 1472c98d5d94SLen Brown else { 1473c98d5d94SLen Brown /* normal case, derive c1 */ 1474008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1475c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1476c98d5d94SLen Brown } 1477144b44b1SLen Brown } 1478c3ae331dSLen Brown 1479c98d5d94SLen Brown if (old->mperf == 0) { 1480b7d8c148SLen Brown if (debug > 1) 1481b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1482c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1483c98d5d94SLen Brown } 1484103a8feaSLen Brown 1485812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1486562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1487562a2d37SLen Brown 1488812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 14891ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1490ba3dec99SLen Brown 1491388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1492388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1493388e9c81SLen Brown old->counter[i] = new->counter[i]; 1494388e9c81SLen Brown else 1495388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1496388e9c81SLen Brown } 1497ba3dec99SLen Brown return 0; 1498103a8feaSLen Brown } 1499c98d5d94SLen Brown 1500c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1501c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1502c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1503c98d5d94SLen Brown { 1504ba3dec99SLen Brown int retval = 0; 1505ba3dec99SLen Brown 1506c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1507c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1508c98d5d94SLen Brown delta_core(c, c2); 1509c98d5d94SLen Brown 1510c98d5d94SLen Brown /* always calculate thread delta */ 1511ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1512ba3dec99SLen Brown if (retval) 1513ba3dec99SLen Brown return retval; 1514c98d5d94SLen Brown 1515c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1516c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1517ba3dec99SLen Brown retval = delta_package(p, p2); 1518c98d5d94SLen Brown 1519ba3dec99SLen Brown return retval; 1520103a8feaSLen Brown } 1521103a8feaSLen Brown 1522c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1523103a8feaSLen Brown { 1524388e9c81SLen Brown int i; 1525388e9c81SLen Brown struct msr_counter *mp; 1526388e9c81SLen Brown 15273f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 15283f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 15293f44a5c6SLen Brown t->tv_end.tv_sec = 0; 15303f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1531d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1532d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 15333f44a5c6SLen Brown 1534c98d5d94SLen Brown t->tsc = 0; 1535c98d5d94SLen Brown t->aperf = 0; 1536c98d5d94SLen Brown t->mperf = 0; 1537c98d5d94SLen Brown t->c1 = 0; 1538103a8feaSLen Brown 1539562a2d37SLen Brown t->irq_count = 0; 1540562a2d37SLen Brown t->smi_count = 0; 1541562a2d37SLen Brown 1542c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1543c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1544c98d5d94SLen Brown 1545c98d5d94SLen Brown c->c3 = 0; 1546c98d5d94SLen Brown c->c6 = 0; 1547c98d5d94SLen Brown c->c7 = 0; 15480539ba11SLen Brown c->mc6_us = 0; 1549889facbeSLen Brown c->core_temp_c = 0; 15509392bd98SCalvin Walton c->core_energy = 0; 1551c98d5d94SLen Brown 15520b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 15530b2bb692SLen Brown p->pkg_any_core_c0 = 0; 15540b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 15550b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 15560b2bb692SLen Brown 1557c98d5d94SLen Brown p->pc2 = 0; 15580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1559c98d5d94SLen Brown p->pc3 = 0; 15600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1561c98d5d94SLen Brown p->pc6 = 0; 15620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1563c98d5d94SLen Brown p->pc7 = 0; 1564ca58710fSKristen Carlson Accardi p->pc8 = 0; 1565ca58710fSKristen Carlson Accardi p->pc9 = 0; 1566ca58710fSKristen Carlson Accardi p->pc10 = 0; 1567be0e54c4SLen Brown p->cpu_lpi = 0; 1568be0e54c4SLen Brown p->sys_lpi = 0; 1569889facbeSLen Brown 1570889facbeSLen Brown p->energy_pkg = 0; 1571889facbeSLen Brown p->energy_dram = 0; 1572889facbeSLen Brown p->energy_cores = 0; 1573889facbeSLen Brown p->energy_gfx = 0; 1574889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1575889facbeSLen Brown p->rapl_dram_perf_status = 0; 1576889facbeSLen Brown p->pkg_temp_c = 0; 157727d47356SLen Brown 1578fdf676e5SLen Brown p->gfx_rc6_ms = 0; 157927d47356SLen Brown p->gfx_mhz = 0; 1580b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1581388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1582388e9c81SLen Brown t->counter[i] = 0; 1583388e9c81SLen Brown 1584388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1585388e9c81SLen Brown c->counter[i] = 0; 1586388e9c81SLen Brown 1587388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1588388e9c81SLen Brown p->counter[i] = 0; 1589103a8feaSLen Brown } 1590c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1591c98d5d94SLen Brown struct pkg_data *p) 1592103a8feaSLen Brown { 1593388e9c81SLen Brown int i; 1594388e9c81SLen Brown struct msr_counter *mp; 1595388e9c81SLen Brown 15964c2122d4SLen Brown /* copy un-changing apic_id's */ 15974c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15984c2122d4SLen Brown average.threads.apic_id = t->apic_id; 15994c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 16004c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 16014c2122d4SLen Brown 16023f44a5c6SLen Brown /* remember first tv_begin */ 16033f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 16043f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 16053f44a5c6SLen Brown 16063f44a5c6SLen Brown /* remember last tv_end */ 16073f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 16083f44a5c6SLen Brown 1609c98d5d94SLen Brown average.threads.tsc += t->tsc; 1610c98d5d94SLen Brown average.threads.aperf += t->aperf; 1611c98d5d94SLen Brown average.threads.mperf += t->mperf; 1612c98d5d94SLen Brown average.threads.c1 += t->c1; 161315aaa346SLen Brown 1614562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1615562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1616562a2d37SLen Brown 1617388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1618388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1619388e9c81SLen Brown continue; 1620388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1621388e9c81SLen Brown } 1622388e9c81SLen Brown 1623c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1624c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1625c98d5d94SLen Brown return 0; 1626c98d5d94SLen Brown 1627c98d5d94SLen Brown average.cores.c3 += c->c3; 1628c98d5d94SLen Brown average.cores.c6 += c->c6; 1629c98d5d94SLen Brown average.cores.c7 += c->c7; 16300539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1631c98d5d94SLen Brown 1632889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1633889facbeSLen Brown 16349392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 16359392bd98SCalvin Walton 1636388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1637388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1638388e9c81SLen Brown continue; 1639388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1640388e9c81SLen Brown } 1641388e9c81SLen Brown 1642c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1643c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1644c98d5d94SLen Brown return 0; 1645c98d5d94SLen Brown 1646a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 16470b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1648a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 16490b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1650a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 16510b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1652a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 16530b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 16540b2bb692SLen Brown 1655c98d5d94SLen Brown average.packages.pc2 += p->pc2; 16560f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1657c98d5d94SLen Brown average.packages.pc3 += p->pc3; 16580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1659c98d5d94SLen Brown average.packages.pc6 += p->pc6; 16600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1661c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1662ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1663ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1664ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1665c98d5d94SLen Brown 1666be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1667be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1668be0e54c4SLen Brown 1669889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1670889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1671889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1672889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1673889facbeSLen Brown 1674fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 167527d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1676b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 167727d47356SLen Brown 1678889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1679889facbeSLen Brown 1680889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1681889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1682388e9c81SLen Brown 1683388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1684388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1685388e9c81SLen Brown continue; 1686388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1687388e9c81SLen Brown } 1688c98d5d94SLen Brown return 0; 1689c98d5d94SLen Brown } 1690c98d5d94SLen Brown /* 1691c98d5d94SLen Brown * sum the counters for all cpus in the system 1692c98d5d94SLen Brown * compute the weighted average 1693c98d5d94SLen Brown */ 1694c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1695c98d5d94SLen Brown struct pkg_data *p) 1696c98d5d94SLen Brown { 1697388e9c81SLen Brown int i; 1698388e9c81SLen Brown struct msr_counter *mp; 1699388e9c81SLen Brown 1700c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1701c98d5d94SLen Brown 1702c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1703c98d5d94SLen Brown 1704d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1705d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1706d4794f25SYazen Ghannam 1707c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1708c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1709c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1710c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1711c98d5d94SLen Brown 17120de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 17130de6c0dfSLen Brown sums_need_wide_columns = 1; 17140de6c0dfSLen Brown 1715c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1716c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1717c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 17180539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1719c98d5d94SLen Brown 1720a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17210b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1722a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17230b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1724a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17250b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1726a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17270b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 17280b2bb692SLen Brown 1729c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 17300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1731c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 17320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1733c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 17340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1735c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1736ca58710fSKristen Carlson Accardi 1737ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1738ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1739ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1740388e9c81SLen Brown 1741388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1742388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1743388e9c81SLen Brown continue; 17440de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17450de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 17460de6c0dfSLen Brown sums_need_wide_columns = 1; 174741618e63SLen Brown continue; 17480de6c0dfSLen Brown } 1749388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1750388e9c81SLen Brown } 1751388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1752388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1753388e9c81SLen Brown continue; 17540de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17550de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 17560de6c0dfSLen Brown sums_need_wide_columns = 1; 17570de6c0dfSLen Brown } 1758388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1759388e9c81SLen Brown } 1760388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1761388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1762388e9c81SLen Brown continue; 17630de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17640de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 17650de6c0dfSLen Brown sums_need_wide_columns = 1; 17660de6c0dfSLen Brown } 1767388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1768388e9c81SLen Brown } 1769c98d5d94SLen Brown } 1770c98d5d94SLen Brown 1771c98d5d94SLen Brown static unsigned long long rdtsc(void) 1772c98d5d94SLen Brown { 1773c98d5d94SLen Brown unsigned int low, high; 1774c98d5d94SLen Brown 1775c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1776c98d5d94SLen Brown 1777c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1778c98d5d94SLen Brown } 1779c98d5d94SLen Brown 1780c98d5d94SLen Brown /* 1781495c7654SLen Brown * Open a file, and exit on failure 1782495c7654SLen Brown */ 1783495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1784495c7654SLen Brown { 1785495c7654SLen Brown FILE *filep = fopen(path, mode); 1786495c7654SLen Brown 1787495c7654SLen Brown if (!filep) 1788495c7654SLen Brown err(1, "%s: open failed", path); 1789495c7654SLen Brown return filep; 1790495c7654SLen Brown } 1791495c7654SLen Brown /* 1792495c7654SLen Brown * snapshot_sysfs_counter() 1793495c7654SLen Brown * 1794495c7654SLen Brown * return snapshot of given counter 1795495c7654SLen Brown */ 1796495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1797495c7654SLen Brown { 1798495c7654SLen Brown FILE *fp; 1799495c7654SLen Brown int retval; 1800495c7654SLen Brown unsigned long long counter; 1801495c7654SLen Brown 1802495c7654SLen Brown fp = fopen_or_die(path, "r"); 1803495c7654SLen Brown 1804495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1805495c7654SLen Brown if (retval != 1) 1806495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1807495c7654SLen Brown 1808495c7654SLen Brown fclose(fp); 1809495c7654SLen Brown 1810495c7654SLen Brown return counter; 1811495c7654SLen Brown } 1812495c7654SLen Brown 1813495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1814495c7654SLen Brown { 1815495c7654SLen Brown if (mp->msr_num != 0) { 1816495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1817495c7654SLen Brown return -1; 1818495c7654SLen Brown } else { 181946c27978SLen Brown char path[128 + PATH_BYTES]; 182041618e63SLen Brown 182141618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 182241618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 182341618e63SLen Brown cpu, mp->path); 182441618e63SLen Brown 182541618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 182641618e63SLen Brown } else { 1827495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1828495c7654SLen Brown } 182941618e63SLen Brown } 1830495c7654SLen Brown 1831495c7654SLen Brown return 0; 1832495c7654SLen Brown } 1833495c7654SLen Brown 18346d6501d9SBorislav Petkov int get_epb(int cpu) 18356d6501d9SBorislav Petkov { 18366d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 1837*7f1b11baSBorislav Petkov unsigned long long msr; 18386d6501d9SBorislav Petkov int ret, epb = -1; 18396d6501d9SBorislav Petkov FILE *fp; 18406d6501d9SBorislav Petkov 18416d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 18426d6501d9SBorislav Petkov 1843*7f1b11baSBorislav Petkov fp = fopen(path, "r"); 1844*7f1b11baSBorislav Petkov if (!fp) 1845*7f1b11baSBorislav Petkov goto msr_fallback; 18466d6501d9SBorislav Petkov 18476d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 18486d6501d9SBorislav Petkov if (ret != 1) 18496d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 18506d6501d9SBorislav Petkov 18516d6501d9SBorislav Petkov fclose(fp); 18526d6501d9SBorislav Petkov 18536d6501d9SBorislav Petkov return epb; 1854*7f1b11baSBorislav Petkov 1855*7f1b11baSBorislav Petkov msr_fallback: 1856*7f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 1857*7f1b11baSBorislav Petkov 1858*7f1b11baSBorislav Petkov return msr & 0xf; 18596d6501d9SBorislav Petkov } 18606d6501d9SBorislav Petkov 18614c2122d4SLen Brown void get_apic_id(struct thread_data *t) 18624c2122d4SLen Brown { 186334041551SLen Brown unsigned int eax, ebx, ecx, edx; 186434041551SLen Brown 186534041551SLen Brown if (DO_BIC(BIC_APIC)) { 186634041551SLen Brown eax = ebx = ecx = edx = 0; 186734041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 186834041551SLen Brown 186934041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 187034041551SLen Brown } 187134041551SLen Brown 187234041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 187334041551SLen Brown return; 187434041551SLen Brown 1875c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 187634041551SLen Brown unsigned int topology_extensions; 187734041551SLen Brown 187834041551SLen Brown if (max_extended_level < 0x8000001e) 187934041551SLen Brown return; 18804c2122d4SLen Brown 18814c2122d4SLen Brown eax = ebx = ecx = edx = 0; 188234041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 188334041551SLen Brown topology_extensions = ecx & (1 << 22); 188434041551SLen Brown 188534041551SLen Brown if (topology_extensions == 0) 188634041551SLen Brown return; 188734041551SLen Brown 188834041551SLen Brown eax = ebx = ecx = edx = 0; 188934041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 189034041551SLen Brown 189134041551SLen Brown t->x2apic_id = eax; 189234041551SLen Brown return; 189334041551SLen Brown } 18944c2122d4SLen Brown 18954c2122d4SLen Brown if (!genuine_intel) 18964c2122d4SLen Brown return; 18974c2122d4SLen Brown 18984c2122d4SLen Brown if (max_level < 0xb) 18994c2122d4SLen Brown return; 19004c2122d4SLen Brown 19014c2122d4SLen Brown ecx = 0; 19024c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 19034c2122d4SLen Brown t->x2apic_id = edx; 19044c2122d4SLen Brown 190534041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 190634041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 190734041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 19084c2122d4SLen Brown } 19094c2122d4SLen Brown 1910495c7654SLen Brown /* 1911c98d5d94SLen Brown * get_counters(...) 1912c98d5d94SLen Brown * migrate to cpu 1913c98d5d94SLen Brown * acquire and record local counters for that cpu 1914c98d5d94SLen Brown */ 1915c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1916c98d5d94SLen Brown { 1917c98d5d94SLen Brown int cpu = t->cpu_id; 1918889facbeSLen Brown unsigned long long msr; 19190102b067SLen Brown int aperf_mperf_retry_count = 0; 1920388e9c81SLen Brown struct msr_counter *mp; 1921388e9c81SLen Brown int i; 1922c98d5d94SLen Brown 1923e52966c0SLen Brown if (cpu_migrate(cpu)) { 19243d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 192515aaa346SLen Brown return -1; 1926e52966c0SLen Brown } 192715aaa346SLen Brown 1928d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1929d4794f25SYazen Ghannam 19304c2122d4SLen Brown if (first_counter_read) 19314c2122d4SLen Brown get_apic_id(t); 19320102b067SLen Brown retry: 1933c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 193415aaa346SLen Brown 19351e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 19361e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 19370102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 19380102b067SLen Brown 19390102b067SLen Brown /* 19400102b067SLen Brown * The TSC, APERF and MPERF must be read together for 19410102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 19420102b067SLen Brown * 19430102b067SLen Brown * Unfortunately, APERF and MPERF are read by 19440102b067SLen Brown * individual system call, so delays may occur 19450102b067SLen Brown * between them. If the time to read them 19460102b067SLen Brown * varies by a large amount, we re-read them. 19470102b067SLen Brown */ 19480102b067SLen Brown 19490102b067SLen Brown /* 19500102b067SLen Brown * This initial dummy APERF read has been seen to 19510102b067SLen Brown * reduce jitter in the subsequent reads. 19520102b067SLen Brown */ 19530102b067SLen Brown 19549c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1955c98d5d94SLen Brown return -3; 19560102b067SLen Brown 19570102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 19580102b067SLen Brown 19590102b067SLen Brown tsc_before = t->tsc; 19600102b067SLen Brown 19610102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 19620102b067SLen Brown return -3; 19630102b067SLen Brown 19640102b067SLen Brown tsc_between = rdtsc(); 19650102b067SLen Brown 19669c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1967c98d5d94SLen Brown return -4; 19680102b067SLen Brown 19690102b067SLen Brown tsc_after = rdtsc(); 19700102b067SLen Brown 19710102b067SLen Brown aperf_time = tsc_between - tsc_before; 19720102b067SLen Brown mperf_time = tsc_after - tsc_between; 19730102b067SLen Brown 19740102b067SLen Brown /* 19750102b067SLen Brown * If the system call latency to read APERF and MPERF 19760102b067SLen Brown * differ by more than 2x, then try again. 19770102b067SLen Brown */ 19780102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 19790102b067SLen Brown aperf_mperf_retry_count++; 19800102b067SLen Brown if (aperf_mperf_retry_count < 5) 19810102b067SLen Brown goto retry; 19820102b067SLen Brown else 19830102b067SLen Brown warnx("cpu%d jitter %lld %lld", 19840102b067SLen Brown cpu, aperf_time, mperf_time); 19850102b067SLen Brown } 19860102b067SLen Brown aperf_mperf_retry_count = 0; 19870102b067SLen Brown 1988b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1989b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 199088c3281fSLen Brown } 199188c3281fSLen Brown 1992812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1993562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1994812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 19951ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 19961ed51011SLen Brown return -5; 19971ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 19981ed51011SLen Brown } 19990539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2000144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2001144b44b1SLen Brown return -6; 2002144b44b1SLen Brown } 2003144b44b1SLen Brown 2004388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2005495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2006388e9c81SLen Brown return -10; 2007388e9c81SLen Brown } 2008388e9c81SLen Brown 2009c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2010c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2011f4fdf2b4SLen Brown goto done; 2012c98d5d94SLen Brown 20131e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2014c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2015c98d5d94SLen Brown return -6; 2016144b44b1SLen Brown } 2017144b44b1SLen Brown 20181e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2019c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2020c98d5d94SLen Brown return -7; 20211e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2022fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2023fb5d4327SDasaratharaman Chandramouli return -7; 2024103a8feaSLen Brown } 202515aaa346SLen Brown 20261e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) 2027c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2028c98d5d94SLen Brown return -8; 2029c98d5d94SLen Brown 20300539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 20310539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 20320539ba11SLen Brown return -8; 20330539ba11SLen Brown 2034812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2035889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2036889facbeSLen Brown return -9; 2037889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2038889facbeSLen Brown } 2039889facbeSLen Brown 20409392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 20419392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 20429392bd98SCalvin Walton return -14; 20439392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 20449392bd98SCalvin Walton } 20459392bd98SCalvin Walton 2046388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2047495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2048388e9c81SLen Brown return -10; 2049388e9c81SLen Brown } 2050889facbeSLen Brown 2051c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2052c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2053f4fdf2b4SLen Brown goto done; 205415aaa346SLen Brown 2055a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 20560b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 20570b2bb692SLen Brown return -10; 2058a99d8730SLen Brown } 2059a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 20600b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 20610b2bb692SLen Brown return -11; 2062a99d8730SLen Brown } 2063a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 20640b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 20650b2bb692SLen Brown return -12; 2066a99d8730SLen Brown } 2067a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 20680b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 20690b2bb692SLen Brown return -13; 20700b2bb692SLen Brown } 20710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2072c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2073c98d5d94SLen Brown return -9; 20740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 20750539ba11SLen Brown if (do_slm_cstates) { 20760539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 20770539ba11SLen Brown return -10; 20780539ba11SLen Brown } else { 2079c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2080c98d5d94SLen Brown return -10; 20810539ba11SLen Brown } 20820539ba11SLen Brown } 20830539ba11SLen Brown 20840f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2085c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2086c98d5d94SLen Brown return -11; 20870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2088c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2089c98d5d94SLen Brown return -12; 20900f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2091ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2092ca58710fSKristen Carlson Accardi return -13; 20930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2094ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2095ca58710fSKristen Carlson Accardi return -13; 20960f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2097ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2098ca58710fSKristen Carlson Accardi return -13; 20990f47c08dSLen Brown 2100be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2101be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2102be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2103be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2104be0e54c4SLen Brown 2105889facbeSLen Brown if (do_rapl & RAPL_PKG) { 21069972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2107889facbeSLen Brown return -13; 21089972d5d8SChen Yu p->energy_pkg = msr; 2109889facbeSLen Brown } 21109148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 21119972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2112889facbeSLen Brown return -14; 21139972d5d8SChen Yu p->energy_cores = msr; 2114889facbeSLen Brown } 2115889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 21169972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2117889facbeSLen Brown return -15; 21189972d5d8SChen Yu p->energy_dram = msr; 2119889facbeSLen Brown } 2120889facbeSLen Brown if (do_rapl & RAPL_GFX) { 21219972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2122889facbeSLen Brown return -16; 21239972d5d8SChen Yu p->energy_gfx = msr; 2124889facbeSLen Brown } 2125889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 21269972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2127889facbeSLen Brown return -16; 21289972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2129889facbeSLen Brown } 2130889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 21319972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2132889facbeSLen Brown return -16; 21339972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2134889facbeSLen Brown } 21353316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21369972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 21373316f99aSCalvin Walton return -13; 21389972d5d8SChen Yu p->energy_pkg = msr; 21393316f99aSCalvin Walton } 2140812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2141889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2142889facbeSLen Brown return -17; 2143889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2144889facbeSLen Brown } 2145fdf676e5SLen Brown 2146812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2147fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2148fdf676e5SLen Brown 2149812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 215027d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 215127d47356SLen Brown 2152b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2153b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2154b4b91569SRafael Antognolli 2155388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2156495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2157388e9c81SLen Brown return -10; 2158388e9c81SLen Brown } 2159f4fdf2b4SLen Brown done: 2160f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2161388e9c81SLen Brown 216215aaa346SLen Brown return 0; 2163103a8feaSLen Brown } 2164103a8feaSLen Brown 2165ee7e38e3SLen Brown /* 2166ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2167ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2168ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2169ee7e38e3SLen Brown */ 2170ee7e38e3SLen Brown 2171ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2172ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2173ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2174ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2175ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2176ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2177ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2178ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2179ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2180ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2181ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2182ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 21830b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 21840b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2185445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2186445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2187ee7e38e3SLen Brown 2188ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2189ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2190445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2191ee7e38e3SLen Brown 2192e9257f5fSLen 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}; 2193e9257f5fSLen 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}; 2194e9257f5fSLen 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}; 21950539ba11SLen 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}; 2196f2642888SLen 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}; 2197e9257f5fSLen 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}; 2198445640a5SLen 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}; 21992085e124SArtem 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}; 2200ee7e38e3SLen Brown 2201a2b7b749SLen Brown 2202a2b7b749SLen Brown static void 2203a2b7b749SLen Brown calculate_tsc_tweak() 2204a2b7b749SLen Brown { 2205a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2206a2b7b749SLen Brown } 2207a2b7b749SLen Brown 2208fcd17211SLen Brown static void 2209fcd17211SLen Brown dump_nhm_platform_info(void) 2210103a8feaSLen Brown { 2211103a8feaSLen Brown unsigned long long msr; 2212103a8feaSLen Brown unsigned int ratio; 2213103a8feaSLen Brown 2214ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2215103a8feaSLen Brown 2216b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 22176574a5d5SLen Brown 2218103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2219710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2220103a8feaSLen Brown ratio, bclk, ratio * bclk); 2221103a8feaSLen Brown 2222103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2223710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2224103a8feaSLen Brown ratio, bclk, ratio * bclk); 2225103a8feaSLen Brown 22267ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2227b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2228bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 222967920418SLen Brown 2230fcd17211SLen Brown return; 2231fcd17211SLen Brown } 2232fcd17211SLen Brown 2233fcd17211SLen Brown static void 2234fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2235fcd17211SLen Brown { 2236fcd17211SLen Brown unsigned long long msr; 2237fcd17211SLen Brown unsigned int ratio; 2238fcd17211SLen Brown 22397ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2240fcd17211SLen Brown 2241b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2242fcd17211SLen Brown 2243fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2244fcd17211SLen Brown if (ratio) 2245710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2246fcd17211SLen Brown ratio, bclk, ratio * bclk); 2247fcd17211SLen Brown 2248fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2249fcd17211SLen Brown if (ratio) 2250710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2251fcd17211SLen Brown ratio, bclk, ratio * bclk); 2252fcd17211SLen Brown return; 2253fcd17211SLen Brown } 2254fcd17211SLen Brown 2255fcd17211SLen Brown static void 2256fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2257fcd17211SLen Brown { 2258fcd17211SLen Brown unsigned long long msr; 2259fcd17211SLen Brown unsigned int ratio; 22606574a5d5SLen Brown 22617ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 22626574a5d5SLen Brown 2263b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 22646574a5d5SLen Brown 22656574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 22666574a5d5SLen Brown if (ratio) 2267710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 22686574a5d5SLen Brown ratio, bclk, ratio * bclk); 22696574a5d5SLen Brown 22706574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 22716574a5d5SLen Brown if (ratio) 2272710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 22736574a5d5SLen Brown ratio, bclk, ratio * bclk); 22746574a5d5SLen Brown 22756574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 22766574a5d5SLen Brown if (ratio) 2277710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 22786574a5d5SLen Brown ratio, bclk, ratio * bclk); 22796574a5d5SLen Brown 22806574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 22816574a5d5SLen Brown if (ratio) 2282710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 22836574a5d5SLen Brown ratio, bclk, ratio * bclk); 22846574a5d5SLen Brown 22856574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 22866574a5d5SLen Brown if (ratio) 2287710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 22886574a5d5SLen Brown ratio, bclk, ratio * bclk); 22896574a5d5SLen Brown 22906574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 22916574a5d5SLen Brown if (ratio) 2292710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 22936574a5d5SLen Brown ratio, bclk, ratio * bclk); 22946574a5d5SLen Brown 22956574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 22966574a5d5SLen Brown if (ratio) 2297710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 22986574a5d5SLen Brown ratio, bclk, ratio * bclk); 22996574a5d5SLen Brown 23006574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 23016574a5d5SLen Brown if (ratio) 2302710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 23036574a5d5SLen Brown ratio, bclk, ratio * bclk); 2304103a8feaSLen Brown return; 2305fcd17211SLen Brown } 230631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 230731e07522SLen Brown { 230831e07522SLen Brown 230931e07522SLen Brown if (!genuine_intel) 231031e07522SLen Brown return 0; 231131e07522SLen Brown 231231e07522SLen Brown switch (model) { 231331e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 231431e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 23155ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 231620de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 231731e07522SLen Brown return 1; 231831e07522SLen Brown } 231931e07522SLen Brown return 0; 232031e07522SLen Brown } 2321fcd17211SLen Brown 2322fcd17211SLen Brown static void 232331e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2324fcd17211SLen Brown { 232531e07522SLen Brown unsigned long long msr, core_counts; 232631e07522SLen Brown unsigned int ratio, group_size; 2327103a8feaSLen Brown 23287ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2329b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 23306574a5d5SLen Brown 233131e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 233231e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 233331e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 233431e07522SLen Brown } else { 233531e07522SLen Brown core_counts = 0x0807060504030201; 233631e07522SLen Brown } 233731e07522SLen Brown 23386574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 233931e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 23406574a5d5SLen Brown if (ratio) 234131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 234231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23436574a5d5SLen Brown 23446574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 234531e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 23466574a5d5SLen Brown if (ratio) 234731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 234831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23496574a5d5SLen Brown 23506574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 235131e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 23526574a5d5SLen Brown if (ratio) 235331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 235431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23556574a5d5SLen Brown 23566574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 235731e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 23586574a5d5SLen Brown if (ratio) 235931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 236031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23616574a5d5SLen Brown 2362103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 236331e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2364103a8feaSLen Brown if (ratio) 236531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 236631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2367103a8feaSLen Brown 2368103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 236931e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2370103a8feaSLen Brown if (ratio) 237131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 237231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2373103a8feaSLen Brown 2374103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 237531e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2376103a8feaSLen Brown if (ratio) 237731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 237831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2379103a8feaSLen Brown 2380103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 238131e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2382103a8feaSLen Brown if (ratio) 238331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 238431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2385fcd17211SLen Brown return; 2386fcd17211SLen Brown } 23873a9a941dSLen Brown 2388fcd17211SLen Brown static void 23890f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 23900f7887c4SLen Brown { 23910f7887c4SLen Brown unsigned long long msr; 23920f7887c4SLen Brown unsigned int ratio; 23930f7887c4SLen Brown 23940f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 23950f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 23960f7887c4SLen Brown 23970f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 23980f7887c4SLen Brown if (ratio) 23990f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 24000f7887c4SLen Brown ratio, bclk, ratio * bclk); 24010f7887c4SLen Brown 24020f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 24030f7887c4SLen Brown if (ratio) 24040f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 24050f7887c4SLen Brown ratio, bclk, ratio * bclk); 24060f7887c4SLen Brown 24070f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 24080f7887c4SLen Brown if (ratio) 24090f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 24100f7887c4SLen Brown ratio, bclk, ratio * bclk); 24110f7887c4SLen Brown 24120f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 24130f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 24140f7887c4SLen Brown 24150f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 24160f7887c4SLen Brown if (ratio) 24170f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 24180f7887c4SLen Brown ratio, bclk, ratio * bclk); 24190f7887c4SLen Brown 24200f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 24210f7887c4SLen Brown if (ratio) 24220f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 24230f7887c4SLen Brown ratio, bclk, ratio * bclk); 24240f7887c4SLen Brown 24250f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 24260f7887c4SLen Brown if (ratio) 24270f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 24280f7887c4SLen Brown ratio, bclk, ratio * bclk); 24290f7887c4SLen Brown 24300f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 24310f7887c4SLen Brown if (ratio) 24320f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 24330f7887c4SLen Brown ratio, bclk, ratio * bclk); 24340f7887c4SLen Brown } 24350f7887c4SLen Brown 24360f7887c4SLen Brown static void 2437fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2438fb5d4327SDasaratharaman Chandramouli { 2439cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2440cbf97abaSHubert Chrzaniuk 2441fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2442cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2443cbf97abaSHubert Chrzaniuk int i, b_nr; 2444cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2445cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2446fb5d4327SDasaratharaman Chandramouli 2447ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2448fb5d4327SDasaratharaman Chandramouli 2449b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2450bfae2052SLen Brown base_cpu, msr); 2451fb5d4327SDasaratharaman Chandramouli 2452fb5d4327SDasaratharaman Chandramouli /** 2453fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2454cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2455cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2456fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2457fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2458fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2459fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2460fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2461fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2462fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2463fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2464fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2465fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2466fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2467fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2468fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2469fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2470fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2471fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2472fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2473fb5d4327SDasaratharaman Chandramouli */ 2474fb5d4327SDasaratharaman Chandramouli 2475cbf97abaSHubert Chrzaniuk b_nr = 0; 2476cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2477cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2478cbf97abaSHubert Chrzaniuk 2479cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2480fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2481cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2482fb5d4327SDasaratharaman Chandramouli 2483cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2484cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2485cbf97abaSHubert Chrzaniuk b_nr++; 2486fb5d4327SDasaratharaman Chandramouli } 2487cbf97abaSHubert Chrzaniuk 2488cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2489cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2490b7d8c148SLen Brown fprintf(outf, 2491710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2492cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2493fb5d4327SDasaratharaman Chandramouli } 2494fb5d4327SDasaratharaman Chandramouli 2495fb5d4327SDasaratharaman Chandramouli static void 2496fcd17211SLen Brown dump_nhm_cst_cfg(void) 2497fcd17211SLen Brown { 2498fcd17211SLen Brown unsigned long long msr; 2499fcd17211SLen Brown 25001df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2501fcd17211SLen Brown 25021df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2503fcd17211SLen Brown 25043e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2505fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2506fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2507fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2508fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2509fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 25106c34f160SLen Brown (unsigned int)msr & 0xF, 2511fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2512ac980e13SArtem Bityutskiy 2513ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2514ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2515ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2516ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2517ac980e13SArtem Bityutskiy } 2518ac980e13SArtem Bityutskiy 2519ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2520ac980e13SArtem Bityutskiy 2521fcd17211SLen Brown return; 2522103a8feaSLen Brown } 2523103a8feaSLen Brown 25246fb3143bSLen Brown static void 25256fb3143bSLen Brown dump_config_tdp(void) 25266fb3143bSLen Brown { 25276fb3143bSLen Brown unsigned long long msr; 25286fb3143bSLen Brown 25296fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2530b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2531685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 25326fb3143bSLen Brown 25336fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2534b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 25356fb3143bSLen Brown if (msr) { 2536685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2537685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2538685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2539685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 25406fb3143bSLen Brown } 2541b7d8c148SLen Brown fprintf(outf, ")\n"); 25426fb3143bSLen Brown 25436fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2544b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 25456fb3143bSLen Brown if (msr) { 2546685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2547685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2548685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2549685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 25506fb3143bSLen Brown } 2551b7d8c148SLen Brown fprintf(outf, ")\n"); 25526fb3143bSLen Brown 25536fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2554b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 25556fb3143bSLen Brown if ((msr) & 0x3) 2556b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2557b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2558b7d8c148SLen Brown fprintf(outf, ")\n"); 25596fb3143bSLen Brown 25606fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2561b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2562685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2563b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2564b7d8c148SLen Brown fprintf(outf, ")\n"); 25656fb3143bSLen Brown } 25665a63426eSLen Brown 25675a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 25685a63426eSLen Brown 25695a63426eSLen Brown void print_irtl(void) 25705a63426eSLen Brown { 25715a63426eSLen Brown unsigned long long msr; 25725a63426eSLen Brown 25735a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 25745a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 25755a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25765a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25775a63426eSLen Brown 25785a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 25795a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 25805a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25815a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25825a63426eSLen Brown 25835a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 25845a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 25855a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25865a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25875a63426eSLen Brown 25885a63426eSLen Brown if (!do_irtl_hsw) 25895a63426eSLen Brown return; 25905a63426eSLen Brown 25915a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 25925a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 25935a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25945a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25955a63426eSLen Brown 25965a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 25975a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 25985a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25995a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26005a63426eSLen Brown 26015a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 26025a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 26035a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26045a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26055a63426eSLen Brown 26065a63426eSLen Brown } 260736229897SLen Brown void free_fd_percpu(void) 260836229897SLen Brown { 260936229897SLen Brown int i; 261036229897SLen Brown 261101a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 261236229897SLen Brown if (fd_percpu[i] != 0) 261336229897SLen Brown close(fd_percpu[i]); 261436229897SLen Brown } 261536229897SLen Brown 261636229897SLen Brown free(fd_percpu); 26176fb3143bSLen Brown } 26186fb3143bSLen Brown 2619c98d5d94SLen Brown void free_all_buffers(void) 2620103a8feaSLen Brown { 26210e2d8f05SLen Brown int i; 26220e2d8f05SLen Brown 2623c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2624c98d5d94SLen Brown cpu_present_set = NULL; 262536229897SLen Brown cpu_present_setsize = 0; 2626103a8feaSLen Brown 2627c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2628c98d5d94SLen Brown cpu_affinity_set = NULL; 2629c98d5d94SLen Brown cpu_affinity_setsize = 0; 2630103a8feaSLen Brown 2631c98d5d94SLen Brown free(thread_even); 2632c98d5d94SLen Brown free(core_even); 2633c98d5d94SLen Brown free(package_even); 2634103a8feaSLen Brown 2635c98d5d94SLen Brown thread_even = NULL; 2636c98d5d94SLen Brown core_even = NULL; 2637c98d5d94SLen Brown package_even = NULL; 2638103a8feaSLen Brown 2639c98d5d94SLen Brown free(thread_odd); 2640c98d5d94SLen Brown free(core_odd); 2641c98d5d94SLen Brown free(package_odd); 2642103a8feaSLen Brown 2643c98d5d94SLen Brown thread_odd = NULL; 2644c98d5d94SLen Brown core_odd = NULL; 2645c98d5d94SLen Brown package_odd = NULL; 2646103a8feaSLen Brown 2647c98d5d94SLen Brown free(output_buffer); 2648c98d5d94SLen Brown output_buffer = NULL; 2649c98d5d94SLen Brown outp = NULL; 265036229897SLen Brown 265136229897SLen Brown free_fd_percpu(); 2652562a2d37SLen Brown 2653562a2d37SLen Brown free(irq_column_2_cpu); 2654562a2d37SLen Brown free(irqs_per_cpu); 26550e2d8f05SLen Brown 26560e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 26570e2d8f05SLen Brown if (cpus[i].put_ids) 26580e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 26590e2d8f05SLen Brown } 26600e2d8f05SLen Brown free(cpus); 2661103a8feaSLen Brown } 2662103a8feaSLen Brown 266357a42a34SJosh Triplett 266457a42a34SJosh Triplett /* 266595aebc44SJosh Triplett * Parse a file containing a single int. 26666de68fe1SLen Brown * Return 0 if file can not be opened 26676de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 266895aebc44SJosh Triplett */ 266995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 267095aebc44SJosh Triplett { 267195aebc44SJosh Triplett va_list args; 267295aebc44SJosh Triplett char path[PATH_MAX]; 267395aebc44SJosh Triplett FILE *filep; 267495aebc44SJosh Triplett int value; 267595aebc44SJosh Triplett 267695aebc44SJosh Triplett va_start(args, fmt); 267795aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 267895aebc44SJosh Triplett va_end(args); 26796de68fe1SLen Brown filep = fopen(path, "r"); 26806de68fe1SLen Brown if (!filep) 26816de68fe1SLen Brown return 0; 2682b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2683b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 268495aebc44SJosh Triplett fclose(filep); 268595aebc44SJosh Triplett return value; 268695aebc44SJosh Triplett } 268795aebc44SJosh Triplett 268895aebc44SJosh Triplett /* 2689c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2690c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2691c98d5d94SLen Brown */ 2692c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2693c98d5d94SLen Brown { 269495aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2695103a8feaSLen Brown } 2696103a8feaSLen Brown 2697103a8feaSLen Brown int get_physical_package_id(int cpu) 2698103a8feaSLen Brown { 269995aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2700103a8feaSLen Brown } 2701103a8feaSLen Brown 27026de68fe1SLen Brown int get_die_id(int cpu) 27036de68fe1SLen Brown { 27046de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 27056de68fe1SLen Brown } 27066de68fe1SLen Brown 2707103a8feaSLen Brown int get_core_id(int cpu) 2708103a8feaSLen Brown { 270995aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2710103a8feaSLen Brown } 2711103a8feaSLen Brown 2712ef605741SPrarit Bhargava void set_node_data(void) 2713ef605741SPrarit Bhargava { 27142ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 27152ffbb224SPrarit Bhargava int cpu_count; 2716ef605741SPrarit Bhargava 27172ffbb224SPrarit Bhargava /* initialize logical_node_id */ 27182ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 27192ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2720ef605741SPrarit Bhargava 27212ffbb224SPrarit Bhargava cpu_count = 0; 27222ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 27232ffbb224SPrarit Bhargava lnode = 0; 27242ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 27252ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2726ef605741SPrarit Bhargava continue; 27272ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 27282ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 27292ffbb224SPrarit Bhargava continue; 27302ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2731ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 27322ffbb224SPrarit Bhargava cpu_count++; 27332ffbb224SPrarit Bhargava /* 27342ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 27352ffbb224SPrarit Bhargava * the logical_node_id 27362ffbb224SPrarit Bhargava */ 27372ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 27382ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 27392ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 27402ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 27412ffbb224SPrarit Bhargava cpu_count++; 2742ef605741SPrarit Bhargava } 27432ffbb224SPrarit Bhargava } 27442ffbb224SPrarit Bhargava lnode++; 27452ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 27462ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 27472ffbb224SPrarit Bhargava } 27482ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 27492ffbb224SPrarit Bhargava break; 27502ffbb224SPrarit Bhargava } 2751ef605741SPrarit Bhargava } 2752ef605741SPrarit Bhargava 2753ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2754c98d5d94SLen Brown { 2755c98d5d94SLen Brown char path[80]; 2756c98d5d94SLen Brown FILE *filep; 27570e2d8f05SLen Brown int i; 27580e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2759c98d5d94SLen Brown 27600e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 27610e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 27620e2d8f05SLen Brown cpu, i); 27630e2d8f05SLen Brown filep = fopen(path, "r"); 27640e2d8f05SLen Brown if (!filep) 27650e2d8f05SLen Brown continue; 27660e2d8f05SLen Brown fclose(filep); 27670e2d8f05SLen Brown return i; 27680e2d8f05SLen Brown } 27690e2d8f05SLen Brown return -1; 2770e275b388SDasaratharaman Chandramouli } 2771c98d5d94SLen Brown 27720e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 27730e2d8f05SLen Brown { 27740e2d8f05SLen Brown char path[80], character; 27750e2d8f05SLen Brown FILE *filep; 27760e2d8f05SLen Brown unsigned long map; 27778cb48b32SPrarit Bhargava int so, shift, sib_core; 27780e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 27790e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 27800e2d8f05SLen Brown size_t size; 27818cb48b32SPrarit Bhargava int thread_id = 0; 27820e2d8f05SLen Brown 27830e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 27848cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 27858cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 27860e2d8f05SLen Brown if (!thiscpu->put_ids) 27870e2d8f05SLen Brown return -1; 27880e2d8f05SLen Brown 27890e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 27900e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 27910e2d8f05SLen Brown 27920e2d8f05SLen Brown sprintf(path, 27930e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 27943d7772eaSLen Brown filep = fopen(path, "r"); 27953d7772eaSLen Brown 27963d7772eaSLen Brown if (!filep) { 27973d7772eaSLen Brown warnx("%s: open failed", path); 27983d7772eaSLen Brown return -1; 27993d7772eaSLen Brown } 28000e2d8f05SLen Brown do { 28010e2d8f05SLen Brown offset -= BITMASK_SIZE; 28028173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 28038173c336SBen Hutchings err(1, "%s: failed to parse file", path); 28040e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 28050e2d8f05SLen Brown if ((map >> shift) & 0x1) { 28068cb48b32SPrarit Bhargava so = shift + offset; 28078cb48b32SPrarit Bhargava sib_core = get_core_id(so); 28088cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 28098cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 28108cb48b32SPrarit Bhargava if ((so != cpu) && 28118cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 28128cb48b32SPrarit Bhargava cpus[so].thread_id = 28138cb48b32SPrarit Bhargava thread_id++; 28148cb48b32SPrarit Bhargava } 28150e2d8f05SLen Brown } 28160e2d8f05SLen Brown } 28170e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2818c98d5d94SLen Brown fclose(filep); 28190e2d8f05SLen Brown 28200e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2821c98d5d94SLen Brown } 2822c98d5d94SLen Brown 2823c98d5d94SLen Brown /* 2824c98d5d94SLen Brown * run func(thread, core, package) in topology order 2825c98d5d94SLen Brown * skip non-present cpus 2826103a8feaSLen Brown */ 2827103a8feaSLen Brown 2828c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2829c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2830c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2831c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2832c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2833c98d5d94SLen Brown struct pkg_data *pkg_base2) 2834c98d5d94SLen Brown { 283540f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2836c98d5d94SLen Brown 2837c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 283840f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 283940f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 284040f5cfe7SPrarit Bhargava ++core_no) { 2841c98d5d94SLen Brown for (thread_no = 0; thread_no < 284270a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2843c98d5d94SLen Brown struct thread_data *t, *t2; 2844c98d5d94SLen Brown struct core_data *c, *c2; 2845c98d5d94SLen Brown struct pkg_data *p, *p2; 2846c98d5d94SLen Brown 284740f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 284840f5cfe7SPrarit Bhargava core_no, node_no, 284940f5cfe7SPrarit Bhargava pkg_no); 2850c98d5d94SLen Brown 2851c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2852c98d5d94SLen Brown continue; 2853c98d5d94SLen Brown 285440f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 285540f5cfe7SPrarit Bhargava core_no, node_no, 285640f5cfe7SPrarit Bhargava pkg_no); 2857c98d5d94SLen Brown 285840f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 285940f5cfe7SPrarit Bhargava node_no, pkg_no); 286040f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 286140f5cfe7SPrarit Bhargava node_no, 286240f5cfe7SPrarit Bhargava pkg_no); 2863c98d5d94SLen Brown 2864c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2865c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2866c98d5d94SLen Brown 2867c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2868c98d5d94SLen Brown if (retval) 2869c98d5d94SLen Brown return retval; 2870c98d5d94SLen Brown } 2871c98d5d94SLen Brown } 2872c98d5d94SLen Brown } 287340f5cfe7SPrarit Bhargava } 2874c98d5d94SLen Brown return 0; 2875c98d5d94SLen Brown } 2876c98d5d94SLen Brown 2877c98d5d94SLen Brown /* 2878c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2879c98d5d94SLen Brown * return max_cpu number 2880c98d5d94SLen Brown */ 2881c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2882103a8feaSLen Brown { 2883103a8feaSLen Brown FILE *fp; 2884c98d5d94SLen Brown int cpu_num; 2885103a8feaSLen Brown int retval; 2886103a8feaSLen Brown 288757a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2888103a8feaSLen Brown 2889103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2890b2c95d90SJosh Triplett if (retval != 0) 2891b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2892103a8feaSLen Brown 2893c98d5d94SLen Brown while (1) { 2894c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2895103a8feaSLen Brown if (retval != 1) 2896103a8feaSLen Brown break; 2897103a8feaSLen Brown 2898c98d5d94SLen Brown retval = func(cpu_num); 2899c98d5d94SLen Brown if (retval) { 2900c98d5d94SLen Brown fclose(fp); 2901c98d5d94SLen Brown return(retval); 2902c98d5d94SLen Brown } 2903103a8feaSLen Brown } 2904103a8feaSLen Brown fclose(fp); 2905c98d5d94SLen Brown return 0; 2906103a8feaSLen Brown } 2907103a8feaSLen Brown 2908103a8feaSLen Brown void re_initialize(void) 2909103a8feaSLen Brown { 2910c98d5d94SLen Brown free_all_buffers(); 2911c98d5d94SLen Brown setup_all_buffers(); 29123d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2913103a8feaSLen Brown } 2914103a8feaSLen Brown 2915843c5791SPrarit Bhargava void set_max_cpu_num(void) 2916843c5791SPrarit Bhargava { 2917843c5791SPrarit Bhargava FILE *filep; 29188201a028SPrarit Bhargava int base_cpu; 2919843c5791SPrarit Bhargava unsigned long dummy; 29208201a028SPrarit Bhargava char pathname[64]; 2921843c5791SPrarit Bhargava 29228201a028SPrarit Bhargava base_cpu = sched_getcpu(); 29238201a028SPrarit Bhargava if (base_cpu < 0) 29248201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 29258201a028SPrarit Bhargava sprintf(pathname, 29268201a028SPrarit Bhargava "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", 29278201a028SPrarit Bhargava base_cpu); 29288201a028SPrarit Bhargava 29298201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 2930843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2931843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 29320e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2933843c5791SPrarit Bhargava fclose(filep); 2934843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2935843c5791SPrarit Bhargava } 2936c98d5d94SLen Brown 2937103a8feaSLen Brown /* 2938c98d5d94SLen Brown * count_cpus() 2939c98d5d94SLen Brown * remember the last one seen, it will be the max 2940103a8feaSLen Brown */ 2941c98d5d94SLen Brown int count_cpus(int cpu) 2942103a8feaSLen Brown { 2943843c5791SPrarit Bhargava topo.num_cpus++; 2944c98d5d94SLen Brown return 0; 2945103a8feaSLen Brown } 2946c98d5d94SLen Brown int mark_cpu_present(int cpu) 2947c98d5d94SLen Brown { 2948c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 294915aaa346SLen Brown return 0; 2950103a8feaSLen Brown } 2951103a8feaSLen Brown 29528cb48b32SPrarit Bhargava int init_thread_id(int cpu) 29538cb48b32SPrarit Bhargava { 29548cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 29558cb48b32SPrarit Bhargava return 0; 29568cb48b32SPrarit Bhargava } 29578cb48b32SPrarit Bhargava 2958562a2d37SLen Brown /* 2959562a2d37SLen Brown * snapshot_proc_interrupts() 2960562a2d37SLen Brown * 2961562a2d37SLen Brown * read and record summary of /proc/interrupts 2962562a2d37SLen Brown * 2963562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2964562a2d37SLen Brown */ 2965562a2d37SLen Brown int snapshot_proc_interrupts(void) 2966562a2d37SLen Brown { 2967562a2d37SLen Brown static FILE *fp; 2968562a2d37SLen Brown int column, retval; 2969562a2d37SLen Brown 2970562a2d37SLen Brown if (fp == NULL) 2971562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2972562a2d37SLen Brown else 2973562a2d37SLen Brown rewind(fp); 2974562a2d37SLen Brown 2975562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2976562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2977562a2d37SLen Brown int cpu_number; 2978562a2d37SLen Brown 2979562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2980562a2d37SLen Brown if (retval != 1) 2981562a2d37SLen Brown break; 2982562a2d37SLen Brown 2983562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2984562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2985562a2d37SLen Brown return 1; 2986562a2d37SLen Brown } 2987562a2d37SLen Brown 2988562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2989562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2990562a2d37SLen Brown } 2991562a2d37SLen Brown 2992562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2993562a2d37SLen Brown while (1) { 2994562a2d37SLen Brown int column; 2995562a2d37SLen Brown char buf[64]; 2996562a2d37SLen Brown 2997562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2998562a2d37SLen Brown if (retval != 1) 2999562a2d37SLen Brown break; 3000562a2d37SLen Brown 3001562a2d37SLen Brown /* read the count per cpu */ 3002562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3003562a2d37SLen Brown 3004562a2d37SLen Brown int cpu_number, irq_count; 3005562a2d37SLen Brown 3006562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3007562a2d37SLen Brown if (retval != 1) 3008562a2d37SLen Brown break; 3009562a2d37SLen Brown 3010562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3011562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3012562a2d37SLen Brown 3013562a2d37SLen Brown } 3014562a2d37SLen Brown 3015562a2d37SLen Brown while (getc(fp) != '\n') 3016562a2d37SLen Brown ; /* flush interrupt description */ 3017562a2d37SLen Brown 3018562a2d37SLen Brown } 3019562a2d37SLen Brown return 0; 3020562a2d37SLen Brown } 302127d47356SLen Brown /* 3022fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3023fdf676e5SLen Brown * 3024fdf676e5SLen Brown * record snapshot of 3025fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3026fdf676e5SLen Brown * 3027fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3028fdf676e5SLen Brown */ 3029fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3030fdf676e5SLen Brown { 3031fdf676e5SLen Brown FILE *fp; 3032fdf676e5SLen Brown int retval; 3033fdf676e5SLen Brown 3034fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3035fdf676e5SLen Brown 3036fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3037fdf676e5SLen Brown if (retval != 1) 3038fdf676e5SLen Brown err(1, "GFX rc6"); 3039fdf676e5SLen Brown 3040fdf676e5SLen Brown fclose(fp); 3041fdf676e5SLen Brown 3042fdf676e5SLen Brown return 0; 3043fdf676e5SLen Brown } 3044fdf676e5SLen Brown /* 304527d47356SLen Brown * snapshot_gfx_mhz() 304627d47356SLen Brown * 304727d47356SLen Brown * record snapshot of 304827d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 304927d47356SLen Brown * 305027d47356SLen Brown * return 1 if config change requires a restart, else return 0 305127d47356SLen Brown */ 305227d47356SLen Brown int snapshot_gfx_mhz(void) 305327d47356SLen Brown { 305427d47356SLen Brown static FILE *fp; 305527d47356SLen Brown int retval; 305627d47356SLen Brown 305727d47356SLen Brown if (fp == NULL) 305827d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 305922048c54SLen Brown else { 306027d47356SLen Brown rewind(fp); 306122048c54SLen Brown fflush(fp); 306222048c54SLen Brown } 306327d47356SLen Brown 306427d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 306527d47356SLen Brown if (retval != 1) 306627d47356SLen Brown err(1, "GFX MHz"); 306727d47356SLen Brown 306827d47356SLen Brown return 0; 306927d47356SLen Brown } 3070562a2d37SLen Brown 3071562a2d37SLen Brown /* 3072b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3073b4b91569SRafael Antognolli * 3074b4b91569SRafael Antognolli * record snapshot of 3075b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3076b4b91569SRafael Antognolli * 3077b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3078b4b91569SRafael Antognolli */ 3079b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3080b4b91569SRafael Antognolli { 3081b4b91569SRafael Antognolli static FILE *fp; 3082b4b91569SRafael Antognolli int retval; 3083b4b91569SRafael Antognolli 3084b4b91569SRafael Antognolli if (fp == NULL) 3085b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3086b4b91569SRafael Antognolli else { 3087b4b91569SRafael Antognolli rewind(fp); 3088b4b91569SRafael Antognolli fflush(fp); 3089b4b91569SRafael Antognolli } 3090b4b91569SRafael Antognolli 3091b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3092b4b91569SRafael Antognolli if (retval != 1) 3093b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3094b4b91569SRafael Antognolli 3095b4b91569SRafael Antognolli return 0; 3096b4b91569SRafael Antognolli } 3097b4b91569SRafael Antognolli 3098b4b91569SRafael Antognolli /* 3099be0e54c4SLen Brown * snapshot_cpu_lpi() 3100be0e54c4SLen Brown * 3101be0e54c4SLen Brown * record snapshot of 3102be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3103be0e54c4SLen Brown */ 3104be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3105be0e54c4SLen Brown { 3106be0e54c4SLen Brown FILE *fp; 3107be0e54c4SLen Brown int retval; 3108be0e54c4SLen Brown 3109be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3110be0e54c4SLen Brown 3111be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 31125ea7647bSPrarit Bhargava if (retval != 1) { 31135ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 31145ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3115605736c6SGustavo A. R. Silva fclose(fp); 31165ea7647bSPrarit Bhargava return -1; 31175ea7647bSPrarit Bhargava } 3118be0e54c4SLen Brown 3119be0e54c4SLen Brown fclose(fp); 3120be0e54c4SLen Brown 3121be0e54c4SLen Brown return 0; 3122be0e54c4SLen Brown } 3123be0e54c4SLen Brown /* 3124be0e54c4SLen Brown * snapshot_sys_lpi() 3125be0e54c4SLen Brown * 31261f81c5efSLen Brown * record snapshot of sys_lpi_file 3127be0e54c4SLen Brown */ 3128be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3129be0e54c4SLen Brown { 3130be0e54c4SLen Brown FILE *fp; 3131be0e54c4SLen Brown int retval; 3132be0e54c4SLen Brown 31331f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3134be0e54c4SLen Brown 3135be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 31365ea7647bSPrarit Bhargava if (retval != 1) { 31375ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 31385ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 313915423b95SColin Ian King fclose(fp); 31405ea7647bSPrarit Bhargava return -1; 31415ea7647bSPrarit Bhargava } 3142be0e54c4SLen Brown fclose(fp); 3143be0e54c4SLen Brown 3144be0e54c4SLen Brown return 0; 3145be0e54c4SLen Brown } 3146be0e54c4SLen Brown /* 3147562a2d37SLen Brown * snapshot /proc and /sys files 3148562a2d37SLen Brown * 3149562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3150562a2d37SLen Brown */ 3151562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3152562a2d37SLen Brown { 3153218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3154562a2d37SLen Brown if (snapshot_proc_interrupts()) 3155562a2d37SLen Brown return 1; 3156562a2d37SLen Brown 3157812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3158fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3159fdf676e5SLen Brown 3160812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 316127d47356SLen Brown snapshot_gfx_mhz(); 316227d47356SLen Brown 3163b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3164b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3165b4b91569SRafael Antognolli 3166be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3167be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3168be0e54c4SLen Brown 3169be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3170be0e54c4SLen Brown snapshot_sys_lpi_us(); 3171be0e54c4SLen Brown 3172562a2d37SLen Brown return 0; 3173562a2d37SLen Brown } 3174562a2d37SLen Brown 31758aa2ed0bSLen Brown int exit_requested; 31768aa2ed0bSLen Brown 31778aa2ed0bSLen Brown static void signal_handler (int signal) 31788aa2ed0bSLen Brown { 31798aa2ed0bSLen Brown switch (signal) { 31808aa2ed0bSLen Brown case SIGINT: 31818aa2ed0bSLen Brown exit_requested = 1; 31828aa2ed0bSLen Brown if (debug) 31838aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 31848aa2ed0bSLen Brown break; 318507211960SLen Brown case SIGUSR1: 318607211960SLen Brown if (debug > 1) 318707211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 318807211960SLen Brown break; 31898aa2ed0bSLen Brown } 31908aa2ed0bSLen Brown } 31918aa2ed0bSLen Brown 31928aa2ed0bSLen Brown void setup_signal_handler(void) 31938aa2ed0bSLen Brown { 31948aa2ed0bSLen Brown struct sigaction sa; 31958aa2ed0bSLen Brown 31968aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 31978aa2ed0bSLen Brown 31988aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 31998aa2ed0bSLen Brown 32008aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 32018aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 320207211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 320307211960SLen Brown err(1, "sigaction SIGUSR1"); 32048aa2ed0bSLen Brown } 3205b9ad8ee0SLen Brown 320647936f94SArtem Bityutskiy void do_sleep(void) 3207b9ad8ee0SLen Brown { 3208c026c236SArtem Bityutskiy struct timeval tout; 3209c026c236SArtem Bityutskiy struct timespec rest; 3210b9ad8ee0SLen Brown fd_set readfds; 3211b9ad8ee0SLen Brown int retval; 3212b9ad8ee0SLen Brown 3213b9ad8ee0SLen Brown FD_ZERO(&readfds); 3214b9ad8ee0SLen Brown FD_SET(0, &readfds); 3215b9ad8ee0SLen Brown 3216c026c236SArtem Bityutskiy if (ignore_stdin) { 321747936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 321847936f94SArtem Bityutskiy return; 321947936f94SArtem Bityutskiy } 3220b9ad8ee0SLen Brown 3221c026c236SArtem Bityutskiy tout = interval_tv; 3222c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3223b9ad8ee0SLen Brown 3224b9ad8ee0SLen Brown if (retval == 1) { 3225b9ad8ee0SLen Brown switch (getc(stdin)) { 3226b9ad8ee0SLen Brown case 'q': 3227b9ad8ee0SLen Brown exit_requested = 1; 3228b9ad8ee0SLen Brown break; 3229c026c236SArtem Bityutskiy case EOF: 3230c026c236SArtem Bityutskiy /* 3231c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3232c026c236SArtem Bityutskiy * won't be any further input. 3233c026c236SArtem Bityutskiy */ 3234c026c236SArtem Bityutskiy ignore_stdin = 1; 3235c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3236c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3237c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3238c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3239b9ad8ee0SLen Brown } 3240b9ad8ee0SLen Brown } 3241b9ad8ee0SLen Brown } 324247936f94SArtem Bityutskiy 324387e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 324487e15da9SChen Yu { 324587e15da9SChen Yu int ret, idx; 324687e15da9SChen Yu unsigned long long msr_cur, msr_last; 324787e15da9SChen Yu 324887e15da9SChen Yu if (!per_cpu_msr_sum) 324987e15da9SChen Yu return 1; 325087e15da9SChen Yu 325187e15da9SChen Yu idx = offset_to_idx(offset); 325287e15da9SChen Yu if (idx < 0) 325387e15da9SChen Yu return idx; 325487e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 325587e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 325687e15da9SChen Yu if (ret) 325787e15da9SChen Yu return ret; 325887e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 325987e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 326087e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 326187e15da9SChen Yu 326287e15da9SChen Yu return 0; 326387e15da9SChen Yu } 326487e15da9SChen Yu 326587e15da9SChen Yu timer_t timerid; 326687e15da9SChen Yu 326787e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 326887e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 326987e15da9SChen Yu { 327087e15da9SChen Yu int i, ret; 327187e15da9SChen Yu int cpu = t->cpu_id; 327287e15da9SChen Yu 327387e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 327487e15da9SChen Yu unsigned long long msr_cur, msr_last; 327587e15da9SChen Yu int offset; 327687e15da9SChen Yu 327787e15da9SChen Yu if (!idx_valid(i)) 327887e15da9SChen Yu continue; 327987e15da9SChen Yu offset = idx_to_offset(i); 328087e15da9SChen Yu if (offset < 0) 328187e15da9SChen Yu continue; 328287e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 328387e15da9SChen Yu if (ret) { 328487e15da9SChen Yu fprintf(outf, "Can not update msr(0x%x)\n", offset); 328587e15da9SChen Yu continue; 328687e15da9SChen Yu } 328787e15da9SChen Yu 328887e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 328987e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 329087e15da9SChen Yu 329187e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 329287e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 329387e15da9SChen Yu } 329487e15da9SChen Yu return 0; 329587e15da9SChen Yu } 329687e15da9SChen Yu 329787e15da9SChen Yu static void 329887e15da9SChen Yu msr_record_handler(union sigval v) 329987e15da9SChen Yu { 330087e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 330187e15da9SChen Yu } 330287e15da9SChen Yu 330387e15da9SChen Yu void msr_sum_record(void) 330487e15da9SChen Yu { 330587e15da9SChen Yu struct itimerspec its; 330687e15da9SChen Yu struct sigevent sev; 330787e15da9SChen Yu 330887e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 330987e15da9SChen Yu if (!per_cpu_msr_sum) { 331087e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 331187e15da9SChen Yu return; 331287e15da9SChen Yu } 331387e15da9SChen Yu /* 331487e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 331587e15da9SChen Yu */ 331687e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 331787e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 331887e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 331987e15da9SChen Yu 332087e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 332187e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 332287e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 332387e15da9SChen Yu goto release_msr; 332487e15da9SChen Yu } 332587e15da9SChen Yu 332687e15da9SChen Yu its.it_value.tv_sec = 0; 332787e15da9SChen Yu its.it_value.tv_nsec = 1; 332887e15da9SChen Yu /* 332987e15da9SChen Yu * A wraparound time has been calculated early. 333087e15da9SChen Yu * Some sources state that the peak power for a 333187e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 333287e15da9SChen Yu * use 2 * TDP for safety. 333387e15da9SChen Yu */ 333487e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 333587e15da9SChen Yu its.it_interval.tv_nsec = 0; 333687e15da9SChen Yu 333787e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 333887e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 333987e15da9SChen Yu goto release_timer; 334087e15da9SChen Yu } 334187e15da9SChen Yu return; 334287e15da9SChen Yu 334387e15da9SChen Yu release_timer: 334487e15da9SChen Yu timer_delete(timerid); 334587e15da9SChen Yu release_msr: 334687e15da9SChen Yu free(per_cpu_msr_sum); 334787e15da9SChen Yu } 33484c2122d4SLen Brown 3349103a8feaSLen Brown void turbostat_loop() 3350103a8feaSLen Brown { 3351c98d5d94SLen Brown int retval; 3352e52966c0SLen Brown int restarted = 0; 3353023fe0acSChen Yu int done_iters = 0; 3354c98d5d94SLen Brown 33558aa2ed0bSLen Brown setup_signal_handler(); 33568aa2ed0bSLen Brown 3357103a8feaSLen Brown restart: 3358e52966c0SLen Brown restarted++; 3359e52966c0SLen Brown 3360562a2d37SLen Brown snapshot_proc_sysfs_files(); 3361c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 33624c2122d4SLen Brown first_counter_read = 0; 3363d91bb17cSLen Brown if (retval < -1) { 3364d91bb17cSLen Brown exit(retval); 3365d91bb17cSLen Brown } else if (retval == -1) { 33663d7772eaSLen Brown if (restarted > 10) { 3367e52966c0SLen Brown exit(retval); 3368e52966c0SLen Brown } 3369c98d5d94SLen Brown re_initialize(); 3370c98d5d94SLen Brown goto restart; 3371c98d5d94SLen Brown } 3372e52966c0SLen Brown restarted = 0; 3373023fe0acSChen Yu done_iters = 0; 3374103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3375103a8feaSLen Brown 3376103a8feaSLen Brown while (1) { 3377c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3378103a8feaSLen Brown re_initialize(); 3379103a8feaSLen Brown goto restart; 3380103a8feaSLen Brown } 3381b9ad8ee0SLen Brown do_sleep(); 3382562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3383562a2d37SLen Brown goto restart; 3384c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3385d91bb17cSLen Brown if (retval < -1) { 3386d91bb17cSLen Brown exit(retval); 3387d91bb17cSLen Brown } else if (retval == -1) { 338815aaa346SLen Brown re_initialize(); 338915aaa346SLen Brown goto restart; 339015aaa346SLen Brown } 3391103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3392103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3393ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3394ba3dec99SLen Brown re_initialize(); 3395ba3dec99SLen Brown goto restart; 3396ba3dec99SLen Brown } 3397c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3398c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3399b7d8c148SLen Brown flush_output_stdout(); 34008aa2ed0bSLen Brown if (exit_requested) 34018aa2ed0bSLen Brown break; 3402023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3403023fe0acSChen Yu break; 3404b9ad8ee0SLen Brown do_sleep(); 3405562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3406562a2d37SLen Brown goto restart; 3407c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3408d91bb17cSLen Brown if (retval < -1) { 3409d91bb17cSLen Brown exit(retval); 3410d91bb17cSLen Brown } else if (retval == -1) { 3411103a8feaSLen Brown re_initialize(); 3412103a8feaSLen Brown goto restart; 3413103a8feaSLen Brown } 3414103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3415103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3416ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3417ba3dec99SLen Brown re_initialize(); 3418ba3dec99SLen Brown goto restart; 3419ba3dec99SLen Brown } 3420c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3421c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3422b7d8c148SLen Brown flush_output_stdout(); 34238aa2ed0bSLen Brown if (exit_requested) 34248aa2ed0bSLen Brown break; 3425023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3426023fe0acSChen Yu break; 3427103a8feaSLen Brown } 3428103a8feaSLen Brown } 3429103a8feaSLen Brown 3430103a8feaSLen Brown void check_dev_msr() 3431103a8feaSLen Brown { 3432103a8feaSLen Brown struct stat sb; 34337ce7d5deSPrarit Bhargava char pathname[32]; 3434103a8feaSLen Brown 34357ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 34367ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3437a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3438d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3439103a8feaSLen Brown } 3440103a8feaSLen Brown 3441fcaa681cSLen Brown /* 3442fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3443fcaa681cSLen Brown * return 0 on success 3444fcaa681cSLen Brown * return 1 on fail 3445fcaa681cSLen Brown */ 3446fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3447103a8feaSLen Brown { 3448fcaa681cSLen Brown cap_t caps; 3449fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3450fcaa681cSLen Brown 3451fcaa681cSLen Brown caps = cap_get_proc(); 3452fcaa681cSLen Brown if (caps == NULL) 3453fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3454fcaa681cSLen Brown 3455fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3456fcaa681cSLen Brown err(-6, "cap_get\n"); 3457fcaa681cSLen Brown 3458fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 3459fcaa681cSLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 3460fcaa681cSLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3461fcaa681cSLen Brown return 1; 3462fcaa681cSLen Brown } 3463fcaa681cSLen Brown 3464fcaa681cSLen Brown if (cap_free(caps) == -1) 3465fcaa681cSLen Brown err(-6, "cap_free\n"); 3466fcaa681cSLen Brown 3467fcaa681cSLen Brown return 0; 3468fcaa681cSLen Brown } 3469fcaa681cSLen Brown void check_permissions(void) 3470fcaa681cSLen Brown { 347198481e79SLen Brown int do_exit = 0; 34727ce7d5deSPrarit Bhargava char pathname[32]; 347398481e79SLen Brown 347498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3475fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 347698481e79SLen Brown 347798481e79SLen Brown /* test file permissions */ 34787ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 34797ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 348098481e79SLen Brown do_exit++; 348198481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 348298481e79SLen Brown } 348398481e79SLen Brown 348498481e79SLen Brown /* if all else fails, thell them to be root */ 348598481e79SLen Brown if (do_exit) 3486b2c95d90SJosh Triplett if (getuid() != 0) 3487d7899447SLen Brown warnx("... or simply run as root"); 348898481e79SLen Brown 348998481e79SLen Brown if (do_exit) 349098481e79SLen Brown exit(-6); 3491103a8feaSLen Brown } 3492103a8feaSLen Brown 3493d7899447SLen Brown /* 3494d7899447SLen Brown * NHM adds support for additional MSRs: 3495d7899447SLen Brown * 3496d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3497d7899447SLen Brown * 3498ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 34991df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3500d7899447SLen Brown * 3501cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3502cf4cbe53SLen Brown * 3503d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3504d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3505d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3506d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3507d7899447SLen Brown * 3508ee7e38e3SLen Brown * Side effect: 35091df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 351033148d67SLen Brown * sets has_misc_feature_control 3511d7899447SLen Brown */ 3512ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3513103a8feaSLen Brown { 3514ee7e38e3SLen Brown unsigned long long msr; 351521ed5574SLen Brown unsigned int base_ratio; 3516ee7e38e3SLen Brown int *pkg_cstate_limits; 3517ee7e38e3SLen Brown 3518103a8feaSLen Brown if (!genuine_intel) 3519103a8feaSLen Brown return 0; 3520103a8feaSLen Brown 3521103a8feaSLen Brown if (family != 6) 3522103a8feaSLen Brown return 0; 3523103a8feaSLen Brown 352421ed5574SLen Brown bclk = discover_bclk(family, model); 352521ed5574SLen Brown 3526103a8feaSLen Brown switch (model) { 3527869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3528869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3529ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3530ee7e38e3SLen Brown break; 3531869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3532869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3533869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3534869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3535ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 353633148d67SLen Brown has_misc_feature_control = 1; 3537ee7e38e3SLen Brown break; 3538c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 35395e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3540869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 354177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3542c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 35435e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3544869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3545af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3546af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3547ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 354833148d67SLen Brown has_misc_feature_control = 1; 3549ee7e38e3SLen Brown break; 3550d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3551d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 355233148d67SLen Brown has_misc_feature_control = 1; 3553d8ebb442SLen Brown break; 3554f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3555cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 35565ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3557ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3558ee7e38e3SLen Brown break; 3559869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3560ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3561cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3562ee7e38e3SLen Brown break; 3563869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3564ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3565ee7e38e3SLen Brown break; 3566869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3567f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 35685ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3569f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 357020de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3571445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3572e4085d54SLen Brown break; 3573103a8feaSLen Brown default: 3574103a8feaSLen Brown return 0; 3575103a8feaSLen Brown } 35761df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3577e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3578ee7e38e3SLen Brown 3579ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 358021ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 358121ed5574SLen Brown 358221ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 358321ed5574SLen Brown has_base_hz = 1; 3584ee7e38e3SLen Brown return 1; 3585103a8feaSLen Brown } 35860f7887c4SLen Brown /* 3587495c7654SLen Brown * SLV client has support for unique MSRs: 35880f7887c4SLen Brown * 35890f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 35900f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 35910f7887c4SLen Brown */ 35920f7887c4SLen Brown 35930f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 35940f7887c4SLen Brown { 35950f7887c4SLen Brown if (!genuine_intel) 35960f7887c4SLen Brown return 0; 35970f7887c4SLen Brown 35980f7887c4SLen Brown switch (model) { 3599f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3600f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3601f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 36020f7887c4SLen Brown return 1; 36030f7887c4SLen Brown } 36040f7887c4SLen Brown return 0; 36050f7887c4SLen Brown } 36067170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 36077170a374SLen Brown { 36087170a374SLen Brown 36097170a374SLen Brown if (!genuine_intel) 36107170a374SLen Brown return 0; 36117170a374SLen Brown 36127170a374SLen Brown switch (model) { 36135ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 36147170a374SLen Brown return 1; 36157170a374SLen Brown } 36167170a374SLen Brown return 0; 36177170a374SLen Brown } 3618ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3619ade0ebacSLen Brown { 3620ade0ebacSLen Brown 3621ade0ebacSLen Brown if (!genuine_intel) 3622ade0ebacSLen Brown return 0; 3623ade0ebacSLen Brown 3624ade0ebacSLen Brown switch (model) { 3625ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3626ade0ebacSLen Brown return 1; 3627ade0ebacSLen Brown } 3628ade0ebacSLen Brown return 0; 3629ade0ebacSLen Brown } 363034c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 363134c76197SLen Brown { 363234c76197SLen Brown 363334c76197SLen Brown if (!genuine_intel) 363434c76197SLen Brown return 0; 363534c76197SLen Brown 363634c76197SLen Brown switch (model) { 363734c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 363834c76197SLen Brown return 1; 363934c76197SLen Brown } 364034c76197SLen Brown return 0; 364134c76197SLen Brown } 3642f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3643f6708400SChen Yu { 3644f6708400SChen Yu if (!genuine_intel) 3645f6708400SChen Yu return 0; 3646f6708400SChen Yu 3647f6708400SChen Yu switch (model) { 3648f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3649f6708400SChen Yu return 1; 3650f6708400SChen Yu } 3651f6708400SChen Yu return 0; 3652f6708400SChen Yu } 365320de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 365420de0dabSAntti Laakso { 365520de0dabSAntti Laakso if (!genuine_intel) 365620de0dabSAntti Laakso return 0; 365720de0dabSAntti Laakso 365820de0dabSAntti Laakso switch (model) { 365920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 366020de0dabSAntti Laakso return 1; 366120de0dabSAntti Laakso } 366220de0dabSAntti Laakso return 0; 366320de0dabSAntti Laakso } 36640f7887c4SLen Brown 366531e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3666d7899447SLen Brown { 36670f7887c4SLen Brown if (has_slv_msrs(family, model)) 36680f7887c4SLen Brown return 0; 36690f7887c4SLen Brown 3670d7899447SLen Brown switch (model) { 3671d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3672869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3673869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3674d7899447SLen Brown return 0; 3675d7899447SLen Brown default: 3676d7899447SLen Brown return 1; 3677d7899447SLen Brown } 3678d7899447SLen Brown } 36790f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 36800f7887c4SLen Brown { 36810f7887c4SLen Brown if (has_slv_msrs(family, model)) 36820f7887c4SLen Brown return 1; 36830f7887c4SLen Brown 36840f7887c4SLen Brown return 0; 36850f7887c4SLen Brown } 36866574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 36876574a5d5SLen Brown { 36886574a5d5SLen Brown if (!genuine_intel) 36896574a5d5SLen Brown return 0; 36906574a5d5SLen Brown 36916574a5d5SLen Brown if (family != 6) 36926574a5d5SLen Brown return 0; 36936574a5d5SLen Brown 36946574a5d5SLen Brown switch (model) { 3695869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3696869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 36976574a5d5SLen Brown return 1; 36986574a5d5SLen Brown default: 36996574a5d5SLen Brown return 0; 37006574a5d5SLen Brown } 37016574a5d5SLen Brown } 3702fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3703fcd17211SLen Brown { 3704fcd17211SLen Brown if (!genuine_intel) 3705fcd17211SLen Brown return 0; 3706fcd17211SLen Brown 3707fcd17211SLen Brown if (family != 6) 3708fcd17211SLen Brown return 0; 3709fcd17211SLen Brown 3710fcd17211SLen Brown switch (model) { 3711869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3712fcd17211SLen Brown return 1; 3713fcd17211SLen Brown default: 3714fcd17211SLen Brown return 0; 3715fcd17211SLen Brown } 3716fcd17211SLen Brown } 3717fcd17211SLen Brown 3718fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3719fb5d4327SDasaratharaman Chandramouli { 3720fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3721fb5d4327SDasaratharaman Chandramouli return 0; 3722fb5d4327SDasaratharaman Chandramouli 3723fb5d4327SDasaratharaman Chandramouli if (family != 6) 3724fb5d4327SDasaratharaman Chandramouli return 0; 3725fb5d4327SDasaratharaman Chandramouli 3726fb5d4327SDasaratharaman Chandramouli switch (model) { 3727869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3728fb5d4327SDasaratharaman Chandramouli return 1; 3729fb5d4327SDasaratharaman Chandramouli default: 3730fb5d4327SDasaratharaman Chandramouli return 0; 3731fb5d4327SDasaratharaman Chandramouli } 3732fb5d4327SDasaratharaman Chandramouli } 373331e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 373431e07522SLen Brown { 373531e07522SLen Brown if (!genuine_intel) 373631e07522SLen Brown return 0; 373731e07522SLen Brown 373831e07522SLen Brown if (family != 6) 373931e07522SLen Brown return 0; 374031e07522SLen Brown 374131e07522SLen Brown switch (model) { 374231e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 374331e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 374431e07522SLen Brown return 1; 374531e07522SLen Brown default: 374631e07522SLen Brown return 0; 374731e07522SLen Brown } 374831e07522SLen Brown } 37496fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 37506fb3143bSLen Brown { 37516fb3143bSLen Brown if (!genuine_intel) 37526fb3143bSLen Brown return 0; 37536fb3143bSLen Brown 37546fb3143bSLen Brown if (family != 6) 37556fb3143bSLen Brown return 0; 37566fb3143bSLen Brown 37576fb3143bSLen Brown switch (model) { 3758869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3759c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3760869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 376177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 37625e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3763c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 37645e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3765869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3766af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3767af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3768869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 37696fb3143bSLen Brown 3770869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 37716fb3143bSLen Brown return 1; 37726fb3143bSLen Brown default: 37736fb3143bSLen Brown return 0; 37746fb3143bSLen Brown } 37756fb3143bSLen Brown } 37766fb3143bSLen Brown 3777fcd17211SLen Brown static void 3778fecb3bc8SDavid Arcari remove_underbar(char *s) 3779fecb3bc8SDavid Arcari { 3780fecb3bc8SDavid Arcari char *to = s; 3781fecb3bc8SDavid Arcari 3782fecb3bc8SDavid Arcari while (*s) { 3783fecb3bc8SDavid Arcari if (*s != '_') 3784fecb3bc8SDavid Arcari *to++ = *s; 3785fecb3bc8SDavid Arcari s++; 3786fecb3bc8SDavid Arcari } 3787fecb3bc8SDavid Arcari 3788fecb3bc8SDavid Arcari *to = 0; 3789fecb3bc8SDavid Arcari } 3790fecb3bc8SDavid Arcari 3791fecb3bc8SDavid Arcari static void 37921b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3793fcd17211SLen Brown { 3794fcd17211SLen Brown if (!do_nhm_platform_info) 3795fcd17211SLen Brown return; 3796fcd17211SLen Brown 3797fcd17211SLen Brown dump_nhm_platform_info(); 3798fcd17211SLen Brown 3799fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3800fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3801fcd17211SLen Brown 3802fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3803fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3804fcd17211SLen Brown 380531e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 380631e07522SLen Brown dump_turbo_ratio_limits(family, model); 3807fcd17211SLen Brown 38080f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 38090f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 38100f7887c4SLen Brown 3811fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3812fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3813fb5d4327SDasaratharaman Chandramouli 38146fb3143bSLen Brown if (has_config_tdp(family, model)) 38156fb3143bSLen Brown dump_config_tdp(); 38166fb3143bSLen Brown 3817fcd17211SLen Brown dump_nhm_cst_cfg(); 3818fcd17211SLen Brown } 3819fcd17211SLen Brown 3820abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3821abdcbdb2SLen Brown { 3822abdcbdb2SLen Brown FILE *input; 3823abdcbdb2SLen Brown char cpuidle_buf[64]; 3824abdcbdb2SLen Brown 3825abdcbdb2SLen Brown input = fopen(path, "r"); 3826abdcbdb2SLen Brown if (input == NULL) { 3827abdcbdb2SLen Brown if (debug) 3828abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3829abdcbdb2SLen Brown return; 3830abdcbdb2SLen Brown } 3831abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3832abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3833abdcbdb2SLen Brown fclose(input); 3834abdcbdb2SLen Brown 3835abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 3836abdcbdb2SLen Brown } 383741618e63SLen Brown static void 383841618e63SLen Brown dump_sysfs_cstate_config(void) 383941618e63SLen Brown { 384041618e63SLen Brown char path[64]; 384141618e63SLen Brown char name_buf[16]; 384241618e63SLen Brown char desc[64]; 384341618e63SLen Brown FILE *input; 384441618e63SLen Brown int state; 384541618e63SLen Brown char *sp; 384641618e63SLen Brown 3847abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 3848abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 3849abdcbdb2SLen Brown return; 3850abdcbdb2SLen Brown } 3851abdcbdb2SLen Brown 3852abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 3853abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 3854abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 3855abdcbdb2SLen Brown 385641618e63SLen Brown for (state = 0; state < 10; ++state) { 385741618e63SLen Brown 385841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 385941618e63SLen Brown base_cpu, state); 386041618e63SLen Brown input = fopen(path, "r"); 386141618e63SLen Brown if (input == NULL) 386241618e63SLen Brown continue; 38638173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 38648173c336SBen Hutchings err(1, "%s: failed to read file", path); 386541618e63SLen Brown 386641618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 386741618e63SLen Brown sp = strchr(name_buf, '-'); 386841618e63SLen Brown if (!sp) 386941618e63SLen Brown sp = strchrnul(name_buf, '\n'); 387041618e63SLen Brown *sp = '\0'; 387141618e63SLen Brown fclose(input); 387241618e63SLen Brown 3873fecb3bc8SDavid Arcari remove_underbar(name_buf); 3874fecb3bc8SDavid Arcari 387541618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 387641618e63SLen Brown base_cpu, state); 387741618e63SLen Brown input = fopen(path, "r"); 387841618e63SLen Brown if (input == NULL) 387941618e63SLen Brown continue; 38808173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 38818173c336SBen Hutchings err(1, "%s: failed to read file", path); 388241618e63SLen Brown 388341618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 388441618e63SLen Brown fclose(input); 388541618e63SLen Brown } 388641618e63SLen Brown } 38877293fccdSLen Brown static void 38887293fccdSLen Brown dump_sysfs_pstate_config(void) 38897293fccdSLen Brown { 38907293fccdSLen Brown char path[64]; 38917293fccdSLen Brown char driver_buf[64]; 38927293fccdSLen Brown char governor_buf[64]; 38937293fccdSLen Brown FILE *input; 38947293fccdSLen Brown int turbo; 38957293fccdSLen Brown 38967293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 38977293fccdSLen Brown base_cpu); 38987293fccdSLen Brown input = fopen(path, "r"); 38997293fccdSLen Brown if (input == NULL) { 39000a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 39017293fccdSLen Brown return; 39027293fccdSLen Brown } 39038173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 39048173c336SBen Hutchings err(1, "%s: failed to read file", path); 39057293fccdSLen Brown fclose(input); 39067293fccdSLen Brown 39077293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 39087293fccdSLen Brown base_cpu); 39097293fccdSLen Brown input = fopen(path, "r"); 39107293fccdSLen Brown if (input == NULL) { 39110a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 39127293fccdSLen Brown return; 39137293fccdSLen Brown } 39148173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 39158173c336SBen Hutchings err(1, "%s: failed to read file", path); 39167293fccdSLen Brown fclose(input); 39177293fccdSLen Brown 39187293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 39197293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 39207293fccdSLen Brown 39217293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 39227293fccdSLen Brown input = fopen(path, "r"); 39237293fccdSLen Brown if (input != NULL) { 39248173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 39258173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 39267293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 39277293fccdSLen Brown fclose(input); 39287293fccdSLen Brown } 39297293fccdSLen Brown 39307293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 39317293fccdSLen Brown input = fopen(path, "r"); 39327293fccdSLen Brown if (input != NULL) { 39338173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 39348173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 39357293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 39367293fccdSLen Brown fclose(input); 39377293fccdSLen Brown } 39387293fccdSLen Brown } 393941618e63SLen Brown 39406574a5d5SLen Brown 3941889facbeSLen Brown /* 3942889facbeSLen Brown * print_epb() 3943889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3944889facbeSLen Brown */ 3945889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3946889facbeSLen Brown { 3947889facbeSLen Brown char *epb_string; 39486d6501d9SBorislav Petkov int cpu, epb; 3949889facbeSLen Brown 3950889facbeSLen Brown if (!has_epb) 3951889facbeSLen Brown return 0; 3952889facbeSLen Brown 3953889facbeSLen Brown cpu = t->cpu_id; 3954889facbeSLen Brown 3955889facbeSLen Brown /* EPB is per-package */ 3956889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3957889facbeSLen Brown return 0; 3958889facbeSLen Brown 3959889facbeSLen Brown if (cpu_migrate(cpu)) { 39603d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 3961889facbeSLen Brown return -1; 3962889facbeSLen Brown } 3963889facbeSLen Brown 39646d6501d9SBorislav Petkov epb = get_epb(cpu); 39656d6501d9SBorislav Petkov if (epb < 0) 3966889facbeSLen Brown return 0; 3967889facbeSLen Brown 39686d6501d9SBorislav Petkov switch (epb) { 3969889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3970889facbeSLen Brown epb_string = "performance"; 3971889facbeSLen Brown break; 3972889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3973889facbeSLen Brown epb_string = "balanced"; 3974889facbeSLen Brown break; 3975889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3976889facbeSLen Brown epb_string = "powersave"; 3977889facbeSLen Brown break; 3978889facbeSLen Brown default: 3979889facbeSLen Brown epb_string = "custom"; 3980889facbeSLen Brown break; 3981889facbeSLen Brown } 39826d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 3983889facbeSLen Brown 3984889facbeSLen Brown return 0; 3985889facbeSLen Brown } 39867f5c258eSLen Brown /* 39877f5c258eSLen Brown * print_hwp() 39887f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 39897f5c258eSLen Brown */ 39907f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 39917f5c258eSLen Brown { 39927f5c258eSLen Brown unsigned long long msr; 39937f5c258eSLen Brown int cpu; 39947f5c258eSLen Brown 39957f5c258eSLen Brown if (!has_hwp) 39967f5c258eSLen Brown return 0; 39977f5c258eSLen Brown 39987f5c258eSLen Brown cpu = t->cpu_id; 39997f5c258eSLen Brown 40007f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 40017f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 40027f5c258eSLen Brown return 0; 40037f5c258eSLen Brown 40047f5c258eSLen Brown if (cpu_migrate(cpu)) { 40053d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 40067f5c258eSLen Brown return -1; 40077f5c258eSLen Brown } 40087f5c258eSLen Brown 40097f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 40107f5c258eSLen Brown return 0; 40117f5c258eSLen Brown 4012b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 40137f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 40147f5c258eSLen Brown 40157f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 40167f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 40177f5c258eSLen Brown return 0; 40187f5c258eSLen Brown 40197f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 40207f5c258eSLen Brown return 0; 40217f5c258eSLen Brown 4022b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 40236dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 40247f5c258eSLen Brown cpu, msr, 40257f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 40267f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 40277f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 40287f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 40297f5c258eSLen Brown 40307f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 40317f5c258eSLen Brown return 0; 40327f5c258eSLen Brown 4033b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 40346dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 40357f5c258eSLen Brown cpu, msr, 40367f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 40377f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 40387f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 40397f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 40407f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 40417f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 40427f5c258eSLen Brown 40437f5c258eSLen Brown if (has_hwp_pkg) { 40447f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 40457f5c258eSLen Brown return 0; 40467f5c258eSLen Brown 4047b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 40486dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 40497f5c258eSLen Brown cpu, msr, 40507f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 40517f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 40527f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 40537f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 40547f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 40557f5c258eSLen Brown } 40567f5c258eSLen Brown if (has_hwp_notify) { 40577f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 40587f5c258eSLen Brown return 0; 40597f5c258eSLen Brown 4060b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 40617f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 40627f5c258eSLen Brown cpu, msr, 40637f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 40647f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 40657f5c258eSLen Brown } 40667f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 40677f5c258eSLen Brown return 0; 40687f5c258eSLen Brown 4069b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 40707f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 40717f5c258eSLen Brown cpu, msr, 40727f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 40737f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 4074889facbeSLen Brown 4075889facbeSLen Brown return 0; 4076889facbeSLen Brown } 4077889facbeSLen Brown 40783a9a941dSLen Brown /* 40793a9a941dSLen Brown * print_perf_limit() 40803a9a941dSLen Brown */ 40813a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 40823a9a941dSLen Brown { 40833a9a941dSLen Brown unsigned long long msr; 40843a9a941dSLen Brown int cpu; 40853a9a941dSLen Brown 40863a9a941dSLen Brown cpu = t->cpu_id; 40873a9a941dSLen Brown 40883a9a941dSLen Brown /* per-package */ 40893a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 40903a9a941dSLen Brown return 0; 40913a9a941dSLen Brown 40923a9a941dSLen Brown if (cpu_migrate(cpu)) { 40933d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 40943a9a941dSLen Brown return -1; 40953a9a941dSLen Brown } 40963a9a941dSLen Brown 40973a9a941dSLen Brown if (do_core_perf_limit_reasons) { 40983a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4099b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4100b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4101e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 41023a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4103e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4104e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4105e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4106e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4107e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4108e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4109e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4110e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4111e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4112e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 4113e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 4114e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 4115b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4116e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 41173a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4118e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4119e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4120e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4121e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4122e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4123e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4124e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4125e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4126e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4127e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 4128e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 4129e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 41303a9a941dSLen Brown 41313a9a941dSLen Brown } 41323a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 41333a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4134b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4135b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 41363a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 41373a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 41383a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 41393a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 41403a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 41413a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 41423a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 41433a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4144b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 41453a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 41463a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 41473a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 41483a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 41493a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 41503a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 41513a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 41523a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 41533a9a941dSLen Brown } 41543a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 41553a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4156b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4157b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 41583a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 41593a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 41603a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 41613a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 41623a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 41633a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4164b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 41653a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 41663a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 41673a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 41683a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 41693a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 41703a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 41713a9a941dSLen Brown } 41723a9a941dSLen Brown return 0; 41733a9a941dSLen Brown } 41743a9a941dSLen Brown 4175889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4176889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4177889facbeSLen Brown 41789392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4179144b44b1SLen Brown { 4180144b44b1SLen Brown unsigned long long msr; 4181144b44b1SLen Brown 4182144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 41837ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4184144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4185144b44b1SLen Brown 4186144b44b1SLen Brown switch (model) { 4187f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 41885ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4189144b44b1SLen Brown return 30.0; 4190144b44b1SLen Brown default: 4191144b44b1SLen Brown return 135.0; 4192144b44b1SLen Brown } 4193144b44b1SLen Brown } 4194144b44b1SLen Brown 41959392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 41969392bd98SCalvin Walton { 419733eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 419833eb8225SKim Phillips return 280.0; 41999392bd98SCalvin Walton } 42009392bd98SCalvin Walton 420140ee8e3bSAndrey Semin /* 420240ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 420340ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 420440ee8e3bSAndrey Semin */ 420540ee8e3bSAndrey Semin static double 420640ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 420740ee8e3bSAndrey Semin { 420840ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 420940ee8e3bSAndrey Semin 421040ee8e3bSAndrey Semin switch (model) { 4211869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4212869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4213869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 421440ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 421540ee8e3bSAndrey Semin default: 421640ee8e3bSAndrey Semin return (rapl_energy_units); 421740ee8e3bSAndrey Semin } 421840ee8e3bSAndrey Semin } 421940ee8e3bSAndrey Semin 42209392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4221889facbeSLen Brown { 4222889facbeSLen Brown unsigned long long msr; 4223144b44b1SLen Brown unsigned int time_unit; 4224889facbeSLen Brown double tdp; 4225889facbeSLen Brown 4226889facbeSLen Brown if (family != 6) 4227889facbeSLen Brown return; 4228889facbeSLen Brown 4229889facbeSLen Brown switch (model) { 4230869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4231869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4232c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 423377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 42345e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4235c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 42365e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4237144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4238812db3f7SLen Brown if (rapl_joules) { 4239812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4240812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4241812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4242812db3f7SLen Brown } else { 4243812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4244812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4245812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4246812db3f7SLen Brown } 4247889facbeSLen Brown break; 4248869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4249f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4250e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4251812db3f7SLen Brown if (rapl_joules) 4252812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4253812db3f7SLen Brown else 4254812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4255e4085d54SLen Brown break; 4256f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4257f6708400SChen Yu 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; 4258f6708400SChen Yu if (rapl_joules) { 4259f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4260f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4261f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4262f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4263f6708400SChen Yu } else { 4264f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4265f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4266f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4267f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4268f6708400SChen Yu } 4269f6708400SChen Yu break; 427020de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 427120de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 427220de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 427320de0dabSAntti Laakso if (rapl_joules) 427420de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 427520de0dabSAntti Laakso else 427620de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 427720de0dabSAntti Laakso break; 4278af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4279af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 428081824921SLen 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; 4281812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4282812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4283812db3f7SLen Brown if (rapl_joules) { 4284812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4285812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4286812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 428781824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4288812db3f7SLen Brown } else { 4289812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4290812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4291812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 429281824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4293812db3f7SLen Brown } 42940b2bb692SLen Brown break; 4295869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4296869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4297869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4298869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 42990b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 4300812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4301812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4302812db3f7SLen Brown if (rapl_joules) { 4303812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4304812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4305812db3f7SLen Brown } else { 4306812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4307812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4308812db3f7SLen Brown } 4309e6f9bb3cSLen Brown break; 4310869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4311869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 43120b2bb692SLen 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; 4313812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4314812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4315812db3f7SLen Brown if (rapl_joules) { 4316812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4317812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4318812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4319812db3f7SLen Brown } else { 4320812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4321812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4322812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4323812db3f7SLen Brown } 4324144b44b1SLen Brown break; 4325f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 43265ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4327144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4328812db3f7SLen Brown if (rapl_joules) { 4329812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4330812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4331812db3f7SLen Brown } else { 4332812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4333812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4334812db3f7SLen Brown } 4335889facbeSLen Brown break; 43365ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 43370f644909SJacob 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; 4338812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4339812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4340812db3f7SLen Brown if (rapl_joules) { 4341812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4342812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4343812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4344812db3f7SLen Brown } else { 4345812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4346812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4347812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4348812db3f7SLen Brown } 43490f644909SJacob Pan break; 4350889facbeSLen Brown default: 4351889facbeSLen Brown return; 4352889facbeSLen Brown } 4353889facbeSLen Brown 4354889facbeSLen Brown /* units on package 0, verify later other packages match */ 43557ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4356889facbeSLen Brown return; 4357889facbeSLen Brown 4358889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4359f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4360144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4361144b44b1SLen Brown else 4362889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4363889facbeSLen Brown 436440ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 436540ee8e3bSAndrey Semin 4366144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4367144b44b1SLen Brown if (time_unit == 0) 4368144b44b1SLen Brown time_unit = 0xA; 4369889facbeSLen Brown 4370144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4371144b44b1SLen Brown 43729392bd98SCalvin Walton tdp = get_tdp_intel(model); 4373889facbeSLen Brown 4374889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 437596e47158SLen Brown if (!quiet) 4376b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 43779392bd98SCalvin Walton } 4378889facbeSLen Brown 43799392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 43809392bd98SCalvin Walton { 43819392bd98SCalvin Walton unsigned long long msr; 43829392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 43839392bd98SCalvin Walton unsigned int has_rapl = 0; 43849392bd98SCalvin Walton double tdp; 43859392bd98SCalvin Walton 43869392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 43879392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 438833eb8225SKim Phillips /* RAPL (Fam 17h+) */ 43899392bd98SCalvin Walton has_rapl = edx & (1 << 14); 43909392bd98SCalvin Walton } 43919392bd98SCalvin Walton 439233eb8225SKim Phillips if (!has_rapl || family < 0x17) 4393889facbeSLen Brown return; 43949392bd98SCalvin Walton 43959392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 43963316f99aSCalvin Walton if (rapl_joules) { 43973316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 43989392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 43993316f99aSCalvin Walton } else { 44003316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 44019392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 44023316f99aSCalvin Walton } 44039392bd98SCalvin Walton 44049392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 44059392bd98SCalvin Walton return; 44069392bd98SCalvin Walton 44079392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 44089392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 44099392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 44109392bd98SCalvin Walton 44119cfa8e04SPu Wen tdp = get_tdp_amd(family); 44129392bd98SCalvin Walton 44139392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 44149392bd98SCalvin Walton if (!quiet) 44159392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 44169392bd98SCalvin Walton } 44179392bd98SCalvin Walton 44189392bd98SCalvin Walton /* 44199392bd98SCalvin Walton * rapl_probe() 44209392bd98SCalvin Walton * 44219392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 44229392bd98SCalvin Walton */ 44239392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 44249392bd98SCalvin Walton { 44259392bd98SCalvin Walton if (genuine_intel) 44269392bd98SCalvin Walton rapl_probe_intel(family, model); 4427c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 44289392bd98SCalvin Walton rapl_probe_amd(family, model); 4429889facbeSLen Brown } 4430889facbeSLen Brown 44311b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 44323a9a941dSLen Brown { 44333a9a941dSLen Brown if (!genuine_intel) 44343a9a941dSLen Brown return; 44353a9a941dSLen Brown 44363a9a941dSLen Brown if (family != 6) 44373a9a941dSLen Brown return; 44383a9a941dSLen Brown 44393a9a941dSLen Brown switch (model) { 4440c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 444177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 44425e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 44433a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4444869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 44453a9a941dSLen Brown do_core_perf_limit_reasons = 1; 44463a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 44473a9a941dSLen Brown default: 44483a9a941dSLen Brown return; 44493a9a941dSLen Brown } 44503a9a941dSLen Brown } 44513a9a941dSLen Brown 4452ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4453ac980e13SArtem Bityutskiy { 4454ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 4455ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4456ac980e13SArtem Bityutskiy } 4457ac980e13SArtem Bityutskiy 4458889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4459889facbeSLen Brown { 4460889facbeSLen Brown unsigned long long msr; 4461f4896fa5SLen Brown unsigned int dts, dts2; 4462889facbeSLen Brown int cpu; 4463889facbeSLen Brown 4464889facbeSLen Brown if (!(do_dts || do_ptm)) 4465889facbeSLen Brown return 0; 4466889facbeSLen Brown 4467889facbeSLen Brown cpu = t->cpu_id; 4468889facbeSLen Brown 4469889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4470889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4471889facbeSLen Brown return 0; 4472889facbeSLen Brown 4473889facbeSLen Brown if (cpu_migrate(cpu)) { 44743d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4475889facbeSLen Brown return -1; 4476889facbeSLen Brown } 4477889facbeSLen Brown 4478889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4479889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4480889facbeSLen Brown return 0; 4481889facbeSLen Brown 4482889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4483b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4484889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4485889facbeSLen Brown 4486889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4487889facbeSLen Brown return 0; 4488889facbeSLen Brown 4489889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4490889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4491b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4492889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4493889facbeSLen Brown } 4494889facbeSLen Brown 4495889facbeSLen Brown 4496f4896fa5SLen Brown if (do_dts && debug) { 4497889facbeSLen Brown unsigned int resolution; 4498889facbeSLen Brown 4499889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4500889facbeSLen Brown return 0; 4501889facbeSLen Brown 4502889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4503889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4504b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4505889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4506889facbeSLen Brown 4507889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4508889facbeSLen Brown return 0; 4509889facbeSLen Brown 4510889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4511889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4512b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4513889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4514889facbeSLen Brown } 4515889facbeSLen Brown 4516889facbeSLen Brown return 0; 4517889facbeSLen Brown } 4518889facbeSLen Brown 4519889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4520889facbeSLen Brown { 4521b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4522889facbeSLen Brown cpu, label, 4523889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4524889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4525889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4526889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4527889facbeSLen Brown 4528889facbeSLen Brown return; 4529889facbeSLen Brown } 4530889facbeSLen Brown 4531889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4532889facbeSLen Brown { 4533889facbeSLen Brown unsigned long long msr; 45349392bd98SCalvin Walton const char *msr_name; 4535889facbeSLen Brown int cpu; 4536889facbeSLen Brown 4537889facbeSLen Brown if (!do_rapl) 4538889facbeSLen Brown return 0; 4539889facbeSLen Brown 4540889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4541889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4542889facbeSLen Brown return 0; 4543889facbeSLen Brown 4544889facbeSLen Brown cpu = t->cpu_id; 4545889facbeSLen Brown if (cpu_migrate(cpu)) { 45463d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4547889facbeSLen Brown return -1; 4548889facbeSLen Brown } 4549889facbeSLen Brown 45509392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 45519392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 45529392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 45539392bd98SCalvin Walton return -1; 45549392bd98SCalvin Walton } else { 45559392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4556889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4557889facbeSLen Brown return -1; 45589392bd98SCalvin Walton } 4559889facbeSLen Brown 45609392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4561144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 456296e47158SLen Brown 4563144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4564144b44b1SLen Brown 4565889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4566889facbeSLen Brown return -5; 4567889facbeSLen Brown 4568889facbeSLen Brown 4569b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4570889facbeSLen Brown cpu, msr, 4571889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4572889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4573889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4574889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4575889facbeSLen Brown 4576144b44b1SLen Brown } 4577144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4578144b44b1SLen Brown 4579889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4580889facbeSLen Brown return -9; 4581889facbeSLen Brown 4582b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4583889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4584889facbeSLen Brown 4585889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4586b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4587889facbeSLen Brown cpu, 4588889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4589889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4590889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4591889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4592889facbeSLen Brown } 4593889facbeSLen Brown 45940b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4595889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4596889facbeSLen Brown return -6; 4597889facbeSLen Brown 4598b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4599889facbeSLen Brown cpu, msr, 4600889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4601889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4602889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4603889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 46040b2bb692SLen Brown } 46050b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4606889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4607889facbeSLen Brown return -9; 4608b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4609889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4610889facbeSLen Brown 4611889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4612889facbeSLen Brown } 4613144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4614889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4615889facbeSLen Brown return -7; 4616889facbeSLen Brown 4617b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4618144b44b1SLen Brown } 46199148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4620889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4621889facbeSLen Brown return -9; 4622b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4623889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4624889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4625889facbeSLen Brown } 4626889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4627889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4628889facbeSLen Brown return -8; 4629889facbeSLen Brown 4630b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4631889facbeSLen Brown 4632889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4633889facbeSLen Brown return -9; 4634b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4635889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4636889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4637889facbeSLen Brown } 4638889facbeSLen Brown return 0; 4639889facbeSLen Brown } 4640889facbeSLen Brown 4641d7899447SLen Brown /* 4642d7899447SLen Brown * SNB adds support for additional MSRs: 4643d7899447SLen Brown * 4644d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4645d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4646d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4647d7899447SLen Brown */ 4648103a8feaSLen Brown 4649d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4650103a8feaSLen Brown { 4651103a8feaSLen Brown if (!genuine_intel) 4652103a8feaSLen Brown return 0; 4653103a8feaSLen Brown 4654103a8feaSLen Brown switch (model) { 4655869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4656869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4657869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4658869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4659c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4660869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 466177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 46625e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4663c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 46645e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4665869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4666af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4667af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4668869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4669869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4670f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 46715ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4672f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 467320de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4674103a8feaSLen Brown return 1; 4675103a8feaSLen Brown } 4676103a8feaSLen Brown return 0; 4677103a8feaSLen Brown } 4678103a8feaSLen Brown 4679d7899447SLen Brown /* 4680570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4681d7899447SLen Brown * 4682d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4683d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4684d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 46855a63426eSLen Brown * 46865a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 46875a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 46885a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 46895a63426eSLen Brown * 4690d7899447SLen Brown */ 4691570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4692ca58710fSKristen Carlson Accardi { 4693ca58710fSKristen Carlson Accardi if (!genuine_intel) 4694ca58710fSKristen Carlson Accardi return 0; 4695ca58710fSKristen Carlson Accardi 4696ca58710fSKristen Carlson Accardi switch (model) { 469777e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4698c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4699af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4700af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4701869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4702f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4703f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4704ca58710fSKristen Carlson Accardi return 1; 4705ca58710fSKristen Carlson Accardi } 4706ca58710fSKristen Carlson Accardi return 0; 4707ca58710fSKristen Carlson Accardi } 4708ca58710fSKristen Carlson Accardi 47090b2bb692SLen Brown /* 47100b2bb692SLen Brown * SKL adds support for additional MSRS: 47110b2bb692SLen Brown * 47120b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 47130b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 47140b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 47150b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 47160b2bb692SLen Brown */ 47170b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 47180b2bb692SLen Brown { 47190b2bb692SLen Brown if (!genuine_intel) 47200b2bb692SLen Brown return 0; 47210b2bb692SLen Brown 47220b2bb692SLen Brown switch (model) { 4723af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4724af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 47250b2bb692SLen Brown return 1; 47260b2bb692SLen Brown } 47270b2bb692SLen Brown return 0; 47280b2bb692SLen Brown } 47290b2bb692SLen Brown 4730144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4731144b44b1SLen Brown { 4732144b44b1SLen Brown if (!genuine_intel) 4733144b44b1SLen Brown return 0; 4734144b44b1SLen Brown switch (model) { 4735f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 47365ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4737144b44b1SLen Brown return 1; 4738144b44b1SLen Brown } 4739144b44b1SLen Brown return 0; 4740144b44b1SLen Brown } 4741144b44b1SLen Brown 4742fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4743fb5d4327SDasaratharaman Chandramouli { 4744fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4745fb5d4327SDasaratharaman Chandramouli return 0; 4746fb5d4327SDasaratharaman Chandramouli switch (model) { 4747869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4748fb5d4327SDasaratharaman Chandramouli return 1; 4749fb5d4327SDasaratharaman Chandramouli } 4750fb5d4327SDasaratharaman Chandramouli return 0; 4751fb5d4327SDasaratharaman Chandramouli } 4752fb5d4327SDasaratharaman Chandramouli 4753997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4754997e5395SSrinivas Pandruvada { 4755997e5395SSrinivas Pandruvada if (!genuine_intel) 4756997e5395SSrinivas Pandruvada return 0; 4757997e5395SSrinivas Pandruvada 4758997e5395SSrinivas Pandruvada switch (model) { 4759af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4760997e5395SSrinivas Pandruvada return 1; 4761997e5395SSrinivas Pandruvada } 4762997e5395SSrinivas Pandruvada 4763997e5395SSrinivas Pandruvada return 0; 4764997e5395SSrinivas Pandruvada } 4765997e5395SSrinivas Pandruvada 4766b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4767b2b34dfeSHubert Chrzaniuk { 4768b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4769b2b34dfeSHubert Chrzaniuk return 1024; 4770b2b34dfeSHubert Chrzaniuk return 1; 4771b2b34dfeSHubert Chrzaniuk } 4772b2b34dfeSHubert Chrzaniuk 4773144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4774144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4775144b44b1SLen Brown 4776144b44b1SLen Brown double slm_bclk(void) 4777144b44b1SLen Brown { 4778144b44b1SLen Brown unsigned long long msr = 3; 4779144b44b1SLen Brown unsigned int i; 4780144b44b1SLen Brown double freq; 4781144b44b1SLen Brown 47827ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4783b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4784144b44b1SLen Brown 4785144b44b1SLen Brown i = msr & 0xf; 4786144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4787b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 47880a91e551SColin Ian King i = 3; 4789144b44b1SLen Brown } 4790144b44b1SLen Brown freq = slm_freq_table[i]; 4791144b44b1SLen Brown 479296e47158SLen Brown if (!quiet) 4793b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4794144b44b1SLen Brown 4795144b44b1SLen Brown return freq; 4796144b44b1SLen Brown } 4797144b44b1SLen Brown 4798103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4799103a8feaSLen Brown { 4800121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4801103a8feaSLen Brown return 100.00; 4802144b44b1SLen Brown else if (is_slm(family, model)) 4803144b44b1SLen Brown return slm_bclk(); 4804103a8feaSLen Brown else 4805103a8feaSLen Brown return 133.33; 4806103a8feaSLen Brown } 4807103a8feaSLen Brown 4808889facbeSLen Brown /* 4809889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4810889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4811889facbeSLen Brown * This is usually equal to tjMax. 4812889facbeSLen Brown * 4813889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4814889facbeSLen Brown * but also allow cmdline over-ride with -T. 4815889facbeSLen Brown * 4816889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4817889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4818889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4819889facbeSLen Brown */ 48206ff7cb37SLen Brown int read_tcc_activation_temp() 4821889facbeSLen Brown { 4822889facbeSLen Brown unsigned long long msr; 48236ff7cb37SLen Brown unsigned int tcc, target_c, offset_c; 4824889facbeSLen Brown 48256ff7cb37SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 48266ff7cb37SLen Brown if (!do_nhm_platform_info) 48276ff7cb37SLen Brown return 0; 48286ff7cb37SLen Brown 48296ff7cb37SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 48306ff7cb37SLen Brown return 0; 48316ff7cb37SLen Brown 48326ff7cb37SLen Brown target_c = (msr >> 16) & 0xFF; 48336ff7cb37SLen Brown 48346ff7cb37SLen Brown offset_c = (msr >> 24) & 0xF; 48356ff7cb37SLen Brown 48366ff7cb37SLen Brown tcc = target_c - offset_c; 48376ff7cb37SLen Brown 48386ff7cb37SLen Brown if (!quiet) 48396ff7cb37SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 48406ff7cb37SLen Brown base_cpu, msr, tcc, target_c, offset_c); 48416ff7cb37SLen Brown 48426ff7cb37SLen Brown return tcc; 48436ff7cb37SLen Brown } 48446ff7cb37SLen Brown 48456ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 48466ff7cb37SLen Brown { 4847889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4848889facbeSLen Brown if (!(do_dts || do_ptm)) 4849889facbeSLen Brown return 0; 4850889facbeSLen Brown 4851889facbeSLen Brown /* this is a per-package concept */ 4852889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4853889facbeSLen Brown return 0; 4854889facbeSLen Brown 4855889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4856889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 48576ff7cb37SLen Brown fprintf(outf, "Using cmdline TCC Target (%d C)\n", tcc_activation_temp); 4858889facbeSLen Brown return 0; 4859889facbeSLen Brown } 4860889facbeSLen Brown 48616ff7cb37SLen Brown tcc_activation_temp = read_tcc_activation_temp(); 48626ff7cb37SLen Brown if (tcc_activation_temp) 4863889facbeSLen Brown return 0; 4864889facbeSLen Brown 4865889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 48666ff7cb37SLen Brown fprintf(outf, "Guessing tjMax %d C, Please use -T to specify\n", tcc_activation_temp); 4867889facbeSLen Brown 4868889facbeSLen Brown return 0; 4869889facbeSLen Brown } 487069807a63SLen Brown 4871aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4872aa8d8cc7SLen Brown { 4873aa8d8cc7SLen Brown unsigned long long msr; 4874aa8d8cc7SLen Brown 4875f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 4876aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4877aa8d8cc7SLen Brown base_cpu, msr, 4878f6505c88SSean Christopherson msr & FEAT_CTL_LOCKED ? "" : "UN-", 4879aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4880aa8d8cc7SLen Brown } 4881aa8d8cc7SLen Brown 488269807a63SLen Brown void decode_misc_enable_msr(void) 488369807a63SLen Brown { 488469807a63SLen Brown unsigned long long msr; 488569807a63SLen Brown 4886f26b1519SLen Brown if (!genuine_intel) 4887f26b1519SLen Brown return; 4888f26b1519SLen Brown 488969807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4890e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 489169807a63SLen Brown base_cpu, msr, 4892e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4893e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4894fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4895e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4896e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 489769807a63SLen Brown } 489869807a63SLen Brown 489933148d67SLen Brown void decode_misc_feature_control(void) 490033148d67SLen Brown { 490133148d67SLen Brown unsigned long long msr; 490233148d67SLen Brown 490333148d67SLen Brown if (!has_misc_feature_control) 490433148d67SLen Brown return; 490533148d67SLen Brown 490633148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 490733148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 490833148d67SLen Brown base_cpu, msr, 490933148d67SLen Brown msr & (0 << 0) ? "No-" : "", 491033148d67SLen Brown msr & (1 << 0) ? "No-" : "", 491133148d67SLen Brown msr & (2 << 0) ? "No-" : "", 491233148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 491333148d67SLen Brown } 4914f0057310SLen Brown /* 4915f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4916f0057310SLen Brown * 4917f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4918f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4919f0057310SLen Brown * bit[1] less so... 4920f0057310SLen Brown */ 4921f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4922f0057310SLen Brown { 4923f0057310SLen Brown unsigned long long msr; 4924f0057310SLen Brown 4925f0057310SLen Brown if (!do_nhm_platform_info) 4926f0057310SLen Brown return; 4927f0057310SLen Brown 4928cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4929cf4cbe53SLen Brown return; 4930cf4cbe53SLen Brown 4931f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4932ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4933f0057310SLen Brown base_cpu, msr, 4934f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4935ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4936ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4937f0057310SLen Brown } 493871616c8eSLen Brown /* 493971616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 494071616c8eSLen Brown * 494171616c8eSLen Brown * This MSRs are present on Silvermont processors, 494271616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 494371616c8eSLen Brown */ 494471616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 494571616c8eSLen Brown { 494671616c8eSLen Brown unsigned long long msr; 494771616c8eSLen Brown 494871616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 494971616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 495071616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 495171616c8eSLen Brown 495271616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 495371616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 495471616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 495571616c8eSLen Brown } 49567f5c258eSLen Brown 4957f5a4c76aSLen Brown /* 4958f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 4959f5a4c76aSLen Brown */ 4960f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 4961f5a4c76aSLen Brown { 4962f5a4c76aSLen Brown 4963f5a4c76aSLen Brown switch(model) { 4964f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 4965f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 4966f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 4967f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 4968f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 4969f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 4970f5a4c76aSLen Brown 4971f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 4972f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 4973f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 4974f5a4c76aSLen Brown 4975f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4976f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 4977f5a4c76aSLen Brown 4978f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 49795ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 4980f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 4981f5a4c76aSLen Brown 4982af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 4983c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 4984af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 4985c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 4986081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 4987081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 4988af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 4989937807d3SLen Brown 4990af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 4991d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 49924bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 49934bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 4994e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 4995e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 4996e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 4997af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 4998b62b3184SZhang Rui 4999d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5000d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5001d7814c30SChen Yu 500223274fafSChen Yu case INTEL_FAM6_ICELAKE_X: 5003e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 500423274fafSChen Yu return INTEL_FAM6_SKYLAKE_X; 5005f5a4c76aSLen Brown } 5006f5a4c76aSLen Brown return model; 5007f5a4c76aSLen Brown } 5008d76bb7a0SLen Brown 5009d76bb7a0SLen Brown void print_dev_latency(void) 5010d76bb7a0SLen Brown { 5011d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5012d76bb7a0SLen Brown int fd; 5013d76bb7a0SLen Brown int value; 5014d76bb7a0SLen Brown int retval; 5015d76bb7a0SLen Brown 5016d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5017d76bb7a0SLen Brown if (fd < 0) { 5018d76bb7a0SLen Brown warn("fopen %s\n", path); 5019d76bb7a0SLen Brown return; 5020d76bb7a0SLen Brown } 5021d76bb7a0SLen Brown 5022d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5023d76bb7a0SLen Brown if (retval != sizeof(int)) { 5024d76bb7a0SLen Brown warn("read %s\n", path); 5025d76bb7a0SLen Brown close(fd); 5026d76bb7a0SLen Brown return; 5027d76bb7a0SLen Brown } 5028d76bb7a0SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", 5029d76bb7a0SLen Brown value, value == 2000000000 ? "default" : "constrained"); 5030d76bb7a0SLen Brown 5031d76bb7a0SLen Brown close(fd); 5032d76bb7a0SLen Brown } 5033d76bb7a0SLen Brown 5034fcd17211SLen Brown void process_cpuid() 5035103a8feaSLen Brown { 503634041551SLen Brown unsigned int eax, ebx, ecx, edx; 503734041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5038b3a34e93SLen Brown unsigned int has_turbo; 5039103a8feaSLen Brown 5040103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5041103a8feaSLen Brown 50425aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5043103a8feaSLen Brown 504434041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5045103a8feaSLen Brown genuine_intel = 1; 504634041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 504734041551SLen Brown authentic_amd = 1; 5048c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5049c1c10cc7SPu Wen hygon_genuine = 1; 5050103a8feaSLen Brown 505196e47158SLen Brown if (!quiet) 5052b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 5053103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 5054103a8feaSLen Brown 50555aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5056103a8feaSLen Brown family = (fms >> 8) & 0xf; 5057103a8feaSLen Brown model = (fms >> 4) & 0xf; 5058103a8feaSLen Brown stepping = fms & 0xf; 50595aa3d1a2SCalvin Walton if (family == 0xf) 50605aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 50615aa3d1a2SCalvin Walton if (family >= 6) 5062103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 506334041551SLen Brown ecx_flags = ecx; 506434041551SLen Brown edx_flags = edx; 5065103a8feaSLen Brown 5066103a8feaSLen Brown /* 5067103a8feaSLen Brown * check max extended function levels of CPUID. 5068103a8feaSLen Brown * This is needed to check for invariant TSC. 5069103a8feaSLen Brown * This check is valid for both Intel and AMD. 5070103a8feaSLen Brown */ 5071103a8feaSLen Brown ebx = ecx = edx = 0; 50725aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5073103a8feaSLen Brown 507434041551SLen Brown if (!quiet) { 507534041551SLen Brown fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 507634041551SLen Brown max_level, max_extended_level, family, model, stepping, family, model, stepping); 507734041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 507834041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 507934041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 508034041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 508134041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 508234041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 508334041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 508434041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 508534041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 508634041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 508734041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 508834041551SLen Brown } 5089f5a4c76aSLen Brown if (genuine_intel) 5090f5a4c76aSLen Brown model = intel_model_duplicates(model); 509134041551SLen Brown 509234041551SLen Brown if (!(edx_flags & (1 << 5))) 509334041551SLen Brown errx(1, "CPUID: no MSR"); 509434041551SLen Brown 509561a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5096103a8feaSLen Brown 5097103a8feaSLen Brown /* 5098103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5099103a8feaSLen Brown * this check is valid for both Intel and AMD 5100103a8feaSLen Brown */ 51015aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 51028209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5103d7899447SLen Brown } 5104103a8feaSLen Brown 5105103a8feaSLen Brown /* 5106103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5107103a8feaSLen Brown * this check is valid for both Intel and AMD 5108103a8feaSLen Brown */ 5109103a8feaSLen Brown 51105aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 51118209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5112812db3f7SLen Brown if (has_aperf) { 5113812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5114812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5115812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5116812db3f7SLen Brown } 5117889facbeSLen Brown do_dts = eax & (1 << 0); 5118812db3f7SLen Brown if (do_dts) 5119812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5120b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5121889facbeSLen Brown do_ptm = eax & (1 << 6); 5122812db3f7SLen Brown if (do_ptm) 5123812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 51247f5c258eSLen Brown has_hwp = eax & (1 << 7); 51257f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 51267f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 51277f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 51287f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5129889facbeSLen Brown has_epb = ecx & (1 << 3); 5130889facbeSLen Brown 513196e47158SLen Brown if (!quiet) 5132b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 51337f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 51347f5c258eSLen Brown has_aperf ? "" : "No-", 5135b3a34e93SLen Brown has_turbo ? "" : "No-", 51367f5c258eSLen Brown do_dts ? "" : "No-", 51377f5c258eSLen Brown do_ptm ? "" : "No-", 51387f5c258eSLen Brown has_hwp ? "" : "No-", 51397f5c258eSLen Brown has_hwp_notify ? "" : "No-", 51407f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 51417f5c258eSLen Brown has_hwp_epp ? "" : "No-", 51427f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 51437f5c258eSLen Brown has_epb ? "" : "No-"); 5144103a8feaSLen Brown 514596e47158SLen Brown if (!quiet) 514669807a63SLen Brown decode_misc_enable_msr(); 514769807a63SLen Brown 514833148d67SLen Brown 514996e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5150aa8d8cc7SLen Brown int has_sgx; 5151aa8d8cc7SLen Brown 5152aa8d8cc7SLen Brown ecx = 0; 5153aa8d8cc7SLen Brown 5154aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5155aa8d8cc7SLen Brown 5156aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5157aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5158aa8d8cc7SLen Brown 5159aa8d8cc7SLen Brown if (has_sgx) 5160aa8d8cc7SLen Brown decode_feature_control_msr(); 5161aa8d8cc7SLen Brown } 5162aa8d8cc7SLen Brown 516361a87ba7SLen Brown if (max_level >= 0x15) { 51648a5bdf41SLen Brown unsigned int eax_crystal; 51658a5bdf41SLen Brown unsigned int ebx_tsc; 51668a5bdf41SLen Brown 51678a5bdf41SLen Brown /* 51688a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 51698a5bdf41SLen Brown */ 51708a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 51715aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 51728a5bdf41SLen Brown 51738a5bdf41SLen Brown if (ebx_tsc != 0) { 51748a5bdf41SLen Brown 517596e47158SLen Brown if (!quiet && (ebx != 0)) 5176b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 51778a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 51788a5bdf41SLen Brown 51798a5bdf41SLen Brown if (crystal_hz == 0) 51808a5bdf41SLen Brown switch(model) { 5181af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5182e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5183e8efbc80SLen Brown break; 51845ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5185ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5186ec53e594SLen Brown break; 5187869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5188f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5189e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 51908a5bdf41SLen Brown break; 51918a5bdf41SLen Brown default: 51928a5bdf41SLen Brown crystal_hz = 0; 51938a5bdf41SLen Brown } 51948a5bdf41SLen Brown 51958a5bdf41SLen Brown if (crystal_hz) { 51968a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 519796e47158SLen Brown if (!quiet) 5198b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 51998a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 52008a5bdf41SLen Brown } 52018a5bdf41SLen Brown } 52028a5bdf41SLen Brown } 520361a87ba7SLen Brown if (max_level >= 0x16) { 520461a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 520561a87ba7SLen Brown 520661a87ba7SLen Brown /* 520761a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 520861a87ba7SLen Brown */ 520961a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 521061a87ba7SLen Brown 52115aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 521296e47158SLen Brown if (!quiet) 5213b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 521461a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 521561a87ba7SLen Brown } 52168a5bdf41SLen Brown 5217b2b34dfeSHubert Chrzaniuk if (has_aperf) 5218b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5219b2b34dfeSHubert Chrzaniuk 5220812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5221812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5222812db3f7SLen Brown 5223812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5224812db3f7SLen Brown do_nhm_platform_info = 1; 5225812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5226812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5227812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5228812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5229812db3f7SLen Brown } 5230d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5231812db3f7SLen Brown 5232812db3f7SLen Brown if (do_snb_cstates) 5233812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5234812db3f7SLen Brown 52355a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 52360f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 52370f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 52380f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 52390f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 52400f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 52410f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 52420f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 52430f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 52440539ba11SLen Brown if (has_slv_msrs(family, model)) { 52450f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 52460f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 52470f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 52480f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 52490539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 52500539ba11SLen Brown use_c1_residency_msr = 1; 52510539ba11SLen Brown } 525220de0dabSAntti Laakso if (is_jvl(family, model)) { 525320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 525420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 525520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 525620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 525720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 525820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 525920de0dabSAntti Laakso } 52607170a374SLen Brown if (is_dnv(family, model)) { 52617170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 52627170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 52637170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 52647170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 52657170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 52667170a374SLen Brown use_c1_residency_msr = 1; 52677170a374SLen Brown } 526834c76197SLen Brown if (is_skx(family, model)) { 526934c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 527034c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 527134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 527234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 527334c76197SLen Brown } 5274ade0ebacSLen Brown if (is_bdx(family, model)) { 5275ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5276ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5277ade0ebacSLen Brown } 5278570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5279c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 52800f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5281c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 52820f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5283c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 52840f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 52850f47c08dSLen Brown } 5286570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5287a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5288a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5289a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5290a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5291a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5292a99d8730SLen Brown } 5293144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5294fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5295103a8feaSLen Brown 5296f6708400SChen Yu if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || 5297f6708400SChen Yu is_ehl(family, model)) 5298562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5299103a8feaSLen Brown 530096e47158SLen Brown if (!quiet) 5301f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5302f0057310SLen Brown 530396e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 530471616c8eSLen Brown decode_c6_demotion_policy_msr(); 530571616c8eSLen Brown 5306889facbeSLen Brown rapl_probe(family, model); 53073a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5308ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5309889facbeSLen Brown 531096e47158SLen Brown if (!quiet) 53111b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5312fcd17211SLen Brown 531341618e63SLen Brown if (!quiet) 5314d76bb7a0SLen Brown print_dev_latency(); 5315d76bb7a0SLen Brown if (!quiet) 531641618e63SLen Brown dump_sysfs_cstate_config(); 53177293fccdSLen Brown if (!quiet) 53187293fccdSLen Brown dump_sysfs_pstate_config(); 531941618e63SLen Brown 5320a2b7b749SLen Brown if (has_skl_msrs(family, model)) 5321a2b7b749SLen Brown calculate_tsc_tweak(); 5322a2b7b749SLen Brown 5323812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5324812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5325fdf676e5SLen Brown 5326812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5327812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 532827d47356SLen Brown 5329b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5330b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5331b4b91569SRafael Antognolli 5332be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5333be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5334be0e54c4SLen Brown else 5335be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5336be0e54c4SLen Brown 53371f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 53381f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5339be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 53401f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 53411f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 53421f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 53431f81c5efSLen Brown } else { 53441f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5345be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 53461f81c5efSLen Brown } 5347be0e54c4SLen Brown 534896e47158SLen Brown if (!quiet) 534933148d67SLen Brown decode_misc_feature_control(); 535033148d67SLen Brown 5351889facbeSLen Brown return; 5352103a8feaSLen Brown } 5353103a8feaSLen Brown 5354103a8feaSLen Brown /* 5355103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5356103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5357103a8feaSLen Brown */ 5358103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5359103a8feaSLen Brown { 5360103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5361103a8feaSLen Brown return 1; 5362103a8feaSLen Brown else 5363103a8feaSLen Brown return 0; 5364103a8feaSLen Brown } 5365103a8feaSLen Brown 5366103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5367103a8feaSLen Brown { 5368103a8feaSLen Brown return 0; 5369103a8feaSLen Brown } 5370103a8feaSLen Brown 5371c98d5d94SLen Brown void topology_probe() 5372c98d5d94SLen Brown { 5373c98d5d94SLen Brown int i; 5374c98d5d94SLen Brown int max_core_id = 0; 5375c98d5d94SLen Brown int max_package_id = 0; 53766de68fe1SLen Brown int max_die_id = 0; 5377c98d5d94SLen Brown int max_siblings = 0; 5378c98d5d94SLen Brown 5379c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5380843c5791SPrarit Bhargava set_max_cpu_num(); 5381c98d5d94SLen Brown topo.num_cpus = 0; 5382c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5383c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5384812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5385c98d5d94SLen Brown 5386d8af6f5fSLen Brown if (debug > 1) 5387b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5388c98d5d94SLen Brown 5389c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5390b2c95d90SJosh Triplett if (cpus == NULL) 5391b2c95d90SJosh Triplett err(1, "calloc cpus"); 5392c98d5d94SLen Brown 5393c98d5d94SLen Brown /* 5394c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5395c98d5d94SLen Brown */ 5396c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5397b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5398b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5399c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5400c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5401c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5402c98d5d94SLen Brown 5403c98d5d94SLen Brown /* 54041ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 54051ef7d21aSLen Brown */ 54061ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 54071ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 54081ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 54091ef7d21aSLen Brown err(1, "cpu%d not present", i); 54101ef7d21aSLen Brown } 54111ef7d21aSLen Brown 54121ef7d21aSLen Brown /* 5413c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5414c98d5d94SLen Brown */ 5415c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5416b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5417b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5418c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5419c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5420c98d5d94SLen Brown 54218cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5422c98d5d94SLen Brown 5423c98d5d94SLen Brown /* 5424c98d5d94SLen Brown * For online cpus 5425c98d5d94SLen Brown * find max_core_id, max_package_id 5426c98d5d94SLen Brown */ 5427c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5428c98d5d94SLen Brown int siblings; 5429c98d5d94SLen Brown 5430c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5431d8af6f5fSLen Brown if (debug > 1) 5432b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5433c98d5d94SLen Brown continue; 5434c98d5d94SLen Brown } 5435c98d5d94SLen Brown 54360e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 54370e2d8f05SLen Brown 54380e2d8f05SLen Brown /* get package information */ 5439c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5440c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5441c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5442c98d5d94SLen Brown 54436de68fe1SLen Brown /* get die information */ 54446de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 54456de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 54466de68fe1SLen Brown max_die_id = cpus[i].die_id; 54476de68fe1SLen Brown 54480e2d8f05SLen Brown /* get numa node information */ 5449ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5450ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5451ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 54520e2d8f05SLen Brown 54530e2d8f05SLen Brown /* get core information */ 54540e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 54550e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 54560e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 54570e2d8f05SLen Brown 54580e2d8f05SLen Brown /* get thread information */ 54590e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5460c98d5d94SLen Brown if (siblings > max_siblings) 5461c98d5d94SLen Brown max_siblings = siblings; 54624f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 54638cb48b32SPrarit Bhargava topo.num_cores++; 5464c98d5d94SLen Brown } 5465ef605741SPrarit Bhargava 546670a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5467d8af6f5fSLen Brown if (debug > 1) 5468b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 546970a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 547070a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5471812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5472c98d5d94SLen Brown 54736de68fe1SLen Brown topo.num_die = max_die_id + 1; 54746de68fe1SLen Brown if (debug > 1) 54756de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 54766de68fe1SLen Brown max_die_id, topo.num_die); 54776de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 54786de68fe1SLen Brown BIC_PRESENT(BIC_Die); 54796de68fe1SLen Brown 5480c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5481d8af6f5fSLen Brown if (debug > 1) 5482b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5483c98d5d94SLen Brown max_package_id, topo.num_packages); 54847da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5485812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5486c98d5d94SLen Brown 5487ef605741SPrarit Bhargava set_node_data(); 5488ef605741SPrarit Bhargava if (debug > 1) 548970a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 549001235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 549101235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5492ef605741SPrarit Bhargava 549370a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5494d8af6f5fSLen Brown if (debug > 1) 5495b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 54962ffbb224SPrarit Bhargava 54972ffbb224SPrarit Bhargava if (debug < 1) 54982ffbb224SPrarit Bhargava return; 54992ffbb224SPrarit Bhargava 55002ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 55010ec712e3SLen Brown if (cpu_is_not_present(i)) 55020ec712e3SLen Brown continue; 55032ffbb224SPrarit Bhargava fprintf(outf, 55046de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 55056de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 55062ffbb224SPrarit Bhargava cpus[i].physical_node_id, 55072ffbb224SPrarit Bhargava cpus[i].logical_node_id, 55082ffbb224SPrarit Bhargava cpus[i].physical_core_id, 55092ffbb224SPrarit Bhargava cpus[i].thread_id); 55102ffbb224SPrarit Bhargava } 55112ffbb224SPrarit Bhargava 5512c98d5d94SLen Brown } 5513c98d5d94SLen Brown 5514c98d5d94SLen Brown void 551540f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 551640f5cfe7SPrarit Bhargava struct pkg_data **p) 5517c98d5d94SLen Brown { 5518c98d5d94SLen Brown int i; 551940f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 552040f5cfe7SPrarit Bhargava topo.num_packages; 552140f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5522c98d5d94SLen Brown 552340f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5524c98d5d94SLen Brown if (*t == NULL) 5525c98d5d94SLen Brown goto error; 5526c98d5d94SLen Brown 552740f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5528c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5529c98d5d94SLen Brown 553040f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5531c98d5d94SLen Brown if (*c == NULL) 5532c98d5d94SLen Brown goto error; 5533c98d5d94SLen Brown 553440f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5535c98d5d94SLen Brown (*c)[i].core_id = -1; 5536c98d5d94SLen Brown 5537678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5538c98d5d94SLen Brown if (*p == NULL) 5539c98d5d94SLen Brown goto error; 5540c98d5d94SLen Brown 5541c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5542c98d5d94SLen Brown (*p)[i].package_id = i; 5543c98d5d94SLen Brown 5544c98d5d94SLen Brown return; 5545c98d5d94SLen Brown error: 5546b2c95d90SJosh Triplett err(1, "calloc counters"); 5547c98d5d94SLen Brown } 5548c98d5d94SLen Brown /* 5549c98d5d94SLen Brown * init_counter() 5550c98d5d94SLen Brown * 5551c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5552c98d5d94SLen Brown */ 5553c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 55548cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5555c98d5d94SLen Brown { 55568cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 555740f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 55588cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 55598cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5560c98d5d94SLen Brown struct thread_data *t; 5561c98d5d94SLen Brown struct core_data *c; 5562c98d5d94SLen Brown struct pkg_data *p; 5563c98d5d94SLen Brown 556442dd4520SNathan Ciobanu 556542dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 556642dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 556742dd4520SNathan Ciobanu */ 556842dd4520SNathan Ciobanu if (node_id < 0) 556942dd4520SNathan Ciobanu node_id = 0; 557042dd4520SNathan Ciobanu 557140f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 557240f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 55738cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5574c98d5d94SLen Brown 5575c98d5d94SLen Brown t->cpu_id = cpu_id; 55768cb48b32SPrarit Bhargava if (thread_id == 0) { 5577c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5578c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5579c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5580c98d5d94SLen Brown } 5581c98d5d94SLen Brown 55828cb48b32SPrarit Bhargava c->core_id = core_id; 55838cb48b32SPrarit Bhargava p->package_id = pkg_id; 5584c98d5d94SLen Brown } 5585c98d5d94SLen Brown 5586c98d5d94SLen Brown 5587c98d5d94SLen Brown int initialize_counters(int cpu_id) 5588c98d5d94SLen Brown { 55898cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 55908cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5591c98d5d94SLen Brown return 0; 5592c98d5d94SLen Brown } 5593c98d5d94SLen Brown 5594c98d5d94SLen Brown void allocate_output_buffer() 5595c98d5d94SLen Brown { 5596eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5597c98d5d94SLen Brown outp = output_buffer; 5598b2c95d90SJosh Triplett if (outp == NULL) 5599b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5600c98d5d94SLen Brown } 560136229897SLen Brown void allocate_fd_percpu(void) 560236229897SLen Brown { 560301a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 560436229897SLen Brown if (fd_percpu == NULL) 560536229897SLen Brown err(-1, "calloc fd_percpu"); 560636229897SLen Brown } 5607562a2d37SLen Brown void allocate_irq_buffers(void) 5608562a2d37SLen Brown { 5609562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5610562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5611562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5612c98d5d94SLen Brown 561301a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5614562a2d37SLen Brown if (irqs_per_cpu == NULL) 561501a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5616562a2d37SLen Brown } 5617c98d5d94SLen Brown void setup_all_buffers(void) 5618c98d5d94SLen Brown { 5619c98d5d94SLen Brown topology_probe(); 5620562a2d37SLen Brown allocate_irq_buffers(); 562136229897SLen Brown allocate_fd_percpu(); 5622c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5623c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5624c98d5d94SLen Brown allocate_output_buffer(); 5625c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5626c98d5d94SLen Brown } 56273b4d5c7fSAndy Shevchenko 56287ce7d5deSPrarit Bhargava void set_base_cpu(void) 56297ce7d5deSPrarit Bhargava { 56307ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 56317ce7d5deSPrarit Bhargava if (base_cpu < 0) 56327ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 56337ce7d5deSPrarit Bhargava 56347ce7d5deSPrarit Bhargava if (debug > 1) 5635b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 56367ce7d5deSPrarit Bhargava } 56377ce7d5deSPrarit Bhargava 5638103a8feaSLen Brown void turbostat_init() 5639103a8feaSLen Brown { 56407ce7d5deSPrarit Bhargava setup_all_buffers(); 56417ce7d5deSPrarit Bhargava set_base_cpu(); 5642103a8feaSLen Brown check_dev_msr(); 564398481e79SLen Brown check_permissions(); 5644fcd17211SLen Brown process_cpuid(); 5645103a8feaSLen Brown 5646103a8feaSLen Brown 564796e47158SLen Brown if (!quiet) 56487f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 56497f5c258eSLen Brown 565096e47158SLen Brown if (!quiet) 5651889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5652889facbeSLen Brown 565396e47158SLen Brown if (!quiet) 56543a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 56553a9a941dSLen Brown 565696e47158SLen Brown if (!quiet) 5657889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5658889facbeSLen Brown 5659889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5660889facbeSLen Brown 566196e47158SLen Brown if (!quiet) 5662889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 56635a63426eSLen Brown 566496e47158SLen Brown if (!quiet && do_irtl_snb) 56655a63426eSLen Brown print_irtl(); 5666103a8feaSLen Brown } 5667103a8feaSLen Brown 5668103a8feaSLen Brown int fork_it(char **argv) 5669103a8feaSLen Brown { 5670103a8feaSLen Brown pid_t child_pid; 5671d91bb17cSLen Brown int status; 5672d15cf7c1SLen Brown 5673218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5674d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 56754c2122d4SLen Brown first_counter_read = 0; 5676d91bb17cSLen Brown if (status) 5677d91bb17cSLen Brown exit(status); 5678d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5679d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5680103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5681103a8feaSLen Brown 5682103a8feaSLen Brown child_pid = fork(); 5683103a8feaSLen Brown if (!child_pid) { 5684103a8feaSLen Brown /* child */ 5685103a8feaSLen Brown execvp(argv[0], argv); 56860815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5687103a8feaSLen Brown } else { 5688103a8feaSLen Brown 5689103a8feaSLen Brown /* parent */ 5690b2c95d90SJosh Triplett if (child_pid == -1) 5691b2c95d90SJosh Triplett err(1, "fork"); 5692103a8feaSLen Brown 5693103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5694103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5695b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5696b2c95d90SJosh Triplett err(status, "waitpid"); 56972a954966SDavid Arcari 56982a954966SDavid Arcari if (WIFEXITED(status)) 56992a954966SDavid Arcari status = WEXITSTATUS(status); 5700103a8feaSLen Brown } 5701c98d5d94SLen Brown /* 5702c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5703c98d5d94SLen Brown * because re-starting is problematic when forking 5704c98d5d94SLen Brown */ 5705218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5706c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5707103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5708103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5709ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5710ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5711ba3dec99SLen Brown else { 5712c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5713c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5714ba3dec99SLen Brown } 5715103a8feaSLen Brown 5716b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5717b7d8c148SLen Brown 5718b7d8c148SLen Brown flush_output_stderr(); 5719103a8feaSLen Brown 5720d91bb17cSLen Brown return status; 5721103a8feaSLen Brown } 5722103a8feaSLen Brown 57233b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 57243b4d5c7fSAndy Shevchenko { 57253b4d5c7fSAndy Shevchenko int status; 57263b4d5c7fSAndy Shevchenko 5727218f0e8dSLen Brown snapshot_proc_sysfs_files(); 57283b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 57293b4d5c7fSAndy Shevchenko if (status) 57303b4d5c7fSAndy Shevchenko return status; 57313b4d5c7fSAndy Shevchenko 57323b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 57333b4d5c7fSAndy Shevchenko if (status) 57343b4d5c7fSAndy Shevchenko return status; 57353b4d5c7fSAndy Shevchenko 5736b7d8c148SLen Brown flush_output_stdout(); 57373b4d5c7fSAndy Shevchenko 57383b4d5c7fSAndy Shevchenko return status; 57393b4d5c7fSAndy Shevchenko } 57403b4d5c7fSAndy Shevchenko 5741d8af6f5fSLen Brown void print_version() { 57423e9fa998SLen Brown fprintf(outf, "turbostat version 20.09.30" 5743d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5744d8af6f5fSLen Brown } 5745d8af6f5fSLen Brown 5746495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5747495c7654SLen Brown unsigned int width, enum counter_scope scope, 574841618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5749388e9c81SLen Brown { 5750388e9c81SLen Brown struct msr_counter *msrp; 5751388e9c81SLen Brown 5752388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5753388e9c81SLen Brown if (msrp == NULL) { 5754388e9c81SLen Brown perror("calloc"); 5755388e9c81SLen Brown exit(1); 5756388e9c81SLen Brown } 5757388e9c81SLen Brown 5758388e9c81SLen Brown msrp->msr_num = msr_num; 5759d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5760495c7654SLen Brown if (path) 5761d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5762388e9c81SLen Brown msrp->width = width; 5763388e9c81SLen Brown msrp->type = type; 5764388e9c81SLen Brown msrp->format = format; 576541618e63SLen Brown msrp->flags = flags; 5766388e9c81SLen Brown 5767388e9c81SLen Brown switch (scope) { 5768388e9c81SLen Brown 5769388e9c81SLen Brown case SCOPE_CPU: 5770388e9c81SLen Brown msrp->next = sys.tp; 5771388e9c81SLen Brown sys.tp = msrp; 5772678a3bd1SLen Brown sys.added_thread_counters++; 57730748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5774678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5775678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5776678a3bd1SLen Brown exit(-1); 5777678a3bd1SLen Brown } 5778388e9c81SLen Brown break; 5779388e9c81SLen Brown 5780388e9c81SLen Brown case SCOPE_CORE: 5781388e9c81SLen Brown msrp->next = sys.cp; 5782388e9c81SLen Brown sys.cp = msrp; 5783678a3bd1SLen Brown sys.added_core_counters++; 5784678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5785678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5786678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5787678a3bd1SLen Brown exit(-1); 5788678a3bd1SLen Brown } 5789388e9c81SLen Brown break; 5790388e9c81SLen Brown 5791388e9c81SLen Brown case SCOPE_PACKAGE: 5792388e9c81SLen Brown msrp->next = sys.pp; 5793388e9c81SLen Brown sys.pp = msrp; 5794678a3bd1SLen Brown sys.added_package_counters++; 5795678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5796678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5797678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5798678a3bd1SLen Brown exit(-1); 5799678a3bd1SLen Brown } 5800388e9c81SLen Brown break; 5801388e9c81SLen Brown } 5802388e9c81SLen Brown 5803388e9c81SLen Brown return 0; 5804388e9c81SLen Brown } 5805388e9c81SLen Brown 5806388e9c81SLen Brown void parse_add_command(char *add_command) 5807388e9c81SLen Brown { 5808388e9c81SLen Brown int msr_num = 0; 5809495c7654SLen Brown char *path = NULL; 58100f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5811388e9c81SLen Brown int width = 64; 5812388e9c81SLen Brown int fail = 0; 5813388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5814388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5815388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5816388e9c81SLen Brown 5817388e9c81SLen Brown while (add_command) { 5818388e9c81SLen Brown 5819388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5820388e9c81SLen Brown goto next; 5821388e9c81SLen Brown 5822388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5823388e9c81SLen Brown goto next; 5824388e9c81SLen Brown 5825495c7654SLen Brown if (*add_command == '/') { 5826495c7654SLen Brown path = add_command; 5827495c7654SLen Brown goto next; 5828495c7654SLen Brown } 5829495c7654SLen Brown 5830388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5831388e9c81SLen Brown if ((width == 32) || (width == 64)) 5832388e9c81SLen Brown goto next; 5833388e9c81SLen Brown width = 64; 5834388e9c81SLen Brown } 5835388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5836388e9c81SLen Brown scope = SCOPE_CPU; 5837388e9c81SLen Brown goto next; 5838388e9c81SLen Brown } 5839388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5840388e9c81SLen Brown scope = SCOPE_CORE; 5841388e9c81SLen Brown goto next; 5842388e9c81SLen Brown } 5843388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5844388e9c81SLen Brown scope = SCOPE_PACKAGE; 5845388e9c81SLen Brown goto next; 5846388e9c81SLen Brown } 5847388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5848388e9c81SLen Brown type = COUNTER_CYCLES; 5849388e9c81SLen Brown goto next; 5850388e9c81SLen Brown } 5851388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5852388e9c81SLen Brown type = COUNTER_SECONDS; 5853388e9c81SLen Brown goto next; 5854388e9c81SLen Brown } 585541618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 585641618e63SLen Brown type = COUNTER_USEC; 585741618e63SLen Brown goto next; 585841618e63SLen Brown } 5859388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5860388e9c81SLen Brown format = FORMAT_RAW; 5861388e9c81SLen Brown goto next; 5862388e9c81SLen Brown } 5863388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5864388e9c81SLen Brown format = FORMAT_DELTA; 5865388e9c81SLen Brown goto next; 5866388e9c81SLen Brown } 5867388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5868388e9c81SLen Brown format = FORMAT_PERCENT; 5869388e9c81SLen Brown goto next; 5870388e9c81SLen Brown } 5871388e9c81SLen Brown 5872388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5873388e9c81SLen Brown char *eos; 5874388e9c81SLen Brown 5875388e9c81SLen Brown eos = strchr(name_buffer, ','); 5876388e9c81SLen Brown if (eos) 5877388e9c81SLen Brown *eos = '\0'; 5878388e9c81SLen Brown goto next; 5879388e9c81SLen Brown } 5880388e9c81SLen Brown 5881388e9c81SLen Brown next: 5882388e9c81SLen Brown add_command = strchr(add_command, ','); 5883495c7654SLen Brown if (add_command) { 5884495c7654SLen Brown *add_command = '\0'; 5885388e9c81SLen Brown add_command++; 5886495c7654SLen Brown } 5887388e9c81SLen Brown 5888388e9c81SLen Brown } 5889495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5890495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5891388e9c81SLen Brown fail++; 5892388e9c81SLen Brown } 5893388e9c81SLen Brown 5894388e9c81SLen Brown /* generate default column header */ 5895388e9c81SLen Brown if (*name_buffer == '\0') { 5896388e9c81SLen Brown if (width == 32) 58975f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5898388e9c81SLen Brown else 58995f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5900388e9c81SLen Brown } 5901388e9c81SLen Brown 590241618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5903388e9c81SLen Brown fail++; 5904388e9c81SLen Brown 5905388e9c81SLen Brown if (fail) { 5906388e9c81SLen Brown help(); 5907388e9c81SLen Brown exit(1); 5908388e9c81SLen Brown } 5909388e9c81SLen Brown } 591041618e63SLen Brown 5911dd778a5eSLen Brown int is_deferred_skip(char *name) 5912dd778a5eSLen Brown { 5913dd778a5eSLen Brown int i; 5914dd778a5eSLen Brown 5915dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5916dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5917dd778a5eSLen Brown return 1; 5918dd778a5eSLen Brown return 0; 5919dd778a5eSLen Brown } 5920dd778a5eSLen Brown 592141618e63SLen Brown void probe_sysfs(void) 592241618e63SLen Brown { 592341618e63SLen Brown char path[64]; 592441618e63SLen Brown char name_buf[16]; 592541618e63SLen Brown FILE *input; 592641618e63SLen Brown int state; 592741618e63SLen Brown char *sp; 592841618e63SLen Brown 592941618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 593041618e63SLen Brown return; 593141618e63SLen Brown 59320748eaf0SLen Brown for (state = 10; state >= 0; --state) { 593341618e63SLen Brown 593441618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 593541618e63SLen Brown base_cpu, state); 593641618e63SLen Brown input = fopen(path, "r"); 593741618e63SLen Brown if (input == NULL) 593841618e63SLen Brown continue; 59398173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 59408173c336SBen Hutchings err(1, "%s: failed to read file", path); 594141618e63SLen Brown 594241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 594341618e63SLen Brown sp = strchr(name_buf, '-'); 594441618e63SLen Brown if (!sp) 594541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 594641618e63SLen Brown *sp = '%'; 594741618e63SLen Brown *(sp + 1) = '\0'; 594841618e63SLen Brown 5949fecb3bc8SDavid Arcari remove_underbar(name_buf); 5950fecb3bc8SDavid Arcari 595141618e63SLen Brown fclose(input); 595241618e63SLen Brown 595341618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 595441618e63SLen Brown 5955dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5956dd778a5eSLen Brown continue; 5957dd778a5eSLen Brown 595841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 595941618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 596041618e63SLen Brown } 596141618e63SLen Brown 59620748eaf0SLen Brown for (state = 10; state >= 0; --state) { 596341618e63SLen Brown 596441618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 596541618e63SLen Brown base_cpu, state); 596641618e63SLen Brown input = fopen(path, "r"); 596741618e63SLen Brown if (input == NULL) 596841618e63SLen Brown continue; 59698173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 59708173c336SBen Hutchings err(1, "%s: failed to read file", path); 597141618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 597241618e63SLen Brown sp = strchr(name_buf, '-'); 597341618e63SLen Brown if (!sp) 597441618e63SLen Brown sp = strchrnul(name_buf, '\n'); 597541618e63SLen Brown *sp = '\0'; 597641618e63SLen Brown fclose(input); 597741618e63SLen Brown 5978fecb3bc8SDavid Arcari remove_underbar(name_buf); 5979fecb3bc8SDavid Arcari 598041618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 598141618e63SLen Brown 5982dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5983dd778a5eSLen Brown continue; 5984dd778a5eSLen Brown 598541618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 598641618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 598741618e63SLen Brown } 598841618e63SLen Brown 598941618e63SLen Brown } 599041618e63SLen Brown 59911ef7d21aSLen Brown 59921ef7d21aSLen Brown /* 59931ef7d21aSLen Brown * parse cpuset with following syntax 59941ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 59951ef7d21aSLen Brown */ 59961ef7d21aSLen Brown void parse_cpu_command(char *optarg) 59971ef7d21aSLen Brown { 59981ef7d21aSLen Brown unsigned int start, end; 59991ef7d21aSLen Brown char *next; 60001ef7d21aSLen Brown 60014e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 60024e4e1e7cSLen Brown if (cpu_subset) 60034e4e1e7cSLen Brown goto error; 60044e4e1e7cSLen Brown show_core_only++; 60054e4e1e7cSLen Brown return; 60064e4e1e7cSLen Brown } 60074e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 60084e4e1e7cSLen Brown if (cpu_subset) 60094e4e1e7cSLen Brown goto error; 60104e4e1e7cSLen Brown show_pkg_only++; 60114e4e1e7cSLen Brown return; 60124e4e1e7cSLen Brown } 60134e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 60144e4e1e7cSLen Brown goto error; 60154e4e1e7cSLen Brown 60161ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 60171ef7d21aSLen Brown if (cpu_subset == NULL) 60181ef7d21aSLen Brown err(3, "CPU_ALLOC"); 60191ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 60201ef7d21aSLen Brown 60211ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 60221ef7d21aSLen Brown 60231ef7d21aSLen Brown next = optarg; 60241ef7d21aSLen Brown 60251ef7d21aSLen Brown while (next && *next) { 60261ef7d21aSLen Brown 60271ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 60281ef7d21aSLen Brown goto error; 60291ef7d21aSLen Brown 60301ef7d21aSLen Brown start = strtoul(next, &next, 10); 60311ef7d21aSLen Brown 60321ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 60331ef7d21aSLen Brown goto error; 60341ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 60351ef7d21aSLen Brown 60361ef7d21aSLen Brown if (*next == '\0') 60371ef7d21aSLen Brown break; 60381ef7d21aSLen Brown 60391ef7d21aSLen Brown if (*next == ',') { 60401ef7d21aSLen Brown next += 1; 60411ef7d21aSLen Brown continue; 60421ef7d21aSLen Brown } 60431ef7d21aSLen Brown 60441ef7d21aSLen Brown if (*next == '-') { 60451ef7d21aSLen Brown next += 1; /* start range */ 60461ef7d21aSLen Brown } else if (*next == '.') { 60471ef7d21aSLen Brown next += 1; 60481ef7d21aSLen Brown if (*next == '.') 60491ef7d21aSLen Brown next += 1; /* start range */ 60501ef7d21aSLen Brown else 60511ef7d21aSLen Brown goto error; 60521ef7d21aSLen Brown } 60531ef7d21aSLen Brown 60541ef7d21aSLen Brown end = strtoul(next, &next, 10); 60551ef7d21aSLen Brown if (end <= start) 60561ef7d21aSLen Brown goto error; 60571ef7d21aSLen Brown 60581ef7d21aSLen Brown while (++start <= end) { 60591ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 60601ef7d21aSLen Brown goto error; 60611ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 60621ef7d21aSLen Brown } 60631ef7d21aSLen Brown 60641ef7d21aSLen Brown if (*next == ',') 60651ef7d21aSLen Brown next += 1; 60661ef7d21aSLen Brown else if (*next != '\0') 60671ef7d21aSLen Brown goto error; 60681ef7d21aSLen Brown } 60691ef7d21aSLen Brown 60701ef7d21aSLen Brown return; 60711ef7d21aSLen Brown 60721ef7d21aSLen Brown error: 60734e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 60744e4e1e7cSLen Brown help(); 60751ef7d21aSLen Brown exit(-1); 60761ef7d21aSLen Brown } 60771ef7d21aSLen Brown 6078812db3f7SLen Brown 6079103a8feaSLen Brown void cmdline(int argc, char **argv) 6080103a8feaSLen Brown { 6081103a8feaSLen Brown int opt; 6082d8af6f5fSLen Brown int option_index = 0; 6083d8af6f5fSLen Brown static struct option long_options[] = { 6084388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 60851ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 6086d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 608796e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 60883f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 6089d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 6090023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 6091d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 6092812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 6093d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 6094c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 6095b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 609696e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 6097812db3f7SLen Brown {"show", required_argument, 0, 's'}, 6098d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 6099d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 6100d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 6101d8af6f5fSLen Brown {0, 0, 0, 0 } 6102d8af6f5fSLen Brown }; 6103103a8feaSLen Brown 6104103a8feaSLen Brown progname = argv[0]; 6105103a8feaSLen Brown 6106023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 6107d8af6f5fSLen Brown long_options, &option_index)) != -1) { 6108103a8feaSLen Brown switch (opt) { 6109388e9c81SLen Brown case 'a': 6110388e9c81SLen Brown parse_add_command(optarg); 6111388e9c81SLen Brown break; 61121ef7d21aSLen Brown case 'c': 61131ef7d21aSLen Brown parse_cpu_command(optarg); 61141ef7d21aSLen Brown break; 6115d8af6f5fSLen Brown case 'D': 6116d8af6f5fSLen Brown dump_only++; 61178e180f3cSLen Brown break; 61183f44a5c6SLen Brown case 'e': 61193f44a5c6SLen Brown /* --enable specified counter */ 61204c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 61213f44a5c6SLen Brown break; 6122d8af6f5fSLen Brown case 'd': 6123d8af6f5fSLen Brown debug++; 61243f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 61252f32edf1SLen Brown break; 6126812db3f7SLen Brown case 'H': 61273f44a5c6SLen Brown /* 61283f44a5c6SLen Brown * --hide: do not show those specified 61293f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 61303f44a5c6SLen Brown */ 61313f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6132812db3f7SLen Brown break; 6133d8af6f5fSLen Brown case 'h': 6134d8af6f5fSLen Brown default: 6135d8af6f5fSLen Brown help(); 6136d8af6f5fSLen Brown exit(1); 6137d8af6f5fSLen Brown case 'i': 61382a0609c0SLen Brown { 61392a0609c0SLen Brown double interval = strtod(optarg, NULL); 61402a0609c0SLen Brown 61412a0609c0SLen Brown if (interval < 0.001) { 6142b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 61432a0609c0SLen Brown interval); 61442a0609c0SLen Brown exit(2); 61452a0609c0SLen Brown } 61462a0609c0SLen Brown 614747936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6148b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 614947936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 61502a0609c0SLen Brown } 6151889facbeSLen Brown break; 61525c56be9aSDirk Brandewie case 'J': 61535c56be9aSDirk Brandewie rapl_joules++; 61545c56be9aSDirk Brandewie break; 6155c8ade361SLen Brown case 'l': 61563f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6157c8ade361SLen Brown list_header_only++; 6158c8ade361SLen Brown quiet++; 6159c8ade361SLen Brown break; 6160b7d8c148SLen Brown case 'o': 6161b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6162b7d8c148SLen Brown break; 616396e47158SLen Brown case 'q': 616496e47158SLen Brown quiet = 1; 616596e47158SLen Brown break; 6166023fe0acSChen Yu case 'n': 6167023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6168023fe0acSChen Yu 6169023fe0acSChen Yu if (num_iterations <= 0) { 6170023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 6171023fe0acSChen Yu num_iterations); 6172023fe0acSChen Yu exit(2); 6173023fe0acSChen Yu } 6174023fe0acSChen Yu break; 6175812db3f7SLen Brown case 's': 61763f44a5c6SLen Brown /* 61773f44a5c6SLen Brown * --show: show only those specified 61783f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 61793f44a5c6SLen Brown * subsequent invocations can add to it. 61803f44a5c6SLen Brown */ 61813f44a5c6SLen Brown if (shown == 0) 61823f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 61833f44a5c6SLen Brown else 61843f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 61853f44a5c6SLen Brown shown = 1; 6186812db3f7SLen Brown break; 6187d8af6f5fSLen Brown case 'S': 6188d8af6f5fSLen Brown summary_only++; 6189d8af6f5fSLen Brown break; 6190d8af6f5fSLen Brown case 'T': 6191d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 6192d8af6f5fSLen Brown break; 6193d8af6f5fSLen Brown case 'v': 6194d8af6f5fSLen Brown print_version(); 6195d8af6f5fSLen Brown exit(0); 6196d8af6f5fSLen Brown break; 6197103a8feaSLen Brown } 6198103a8feaSLen Brown } 6199103a8feaSLen Brown } 6200103a8feaSLen Brown 6201103a8feaSLen Brown int main(int argc, char **argv) 6202103a8feaSLen Brown { 6203b7d8c148SLen Brown outf = stderr; 6204103a8feaSLen Brown cmdline(argc, argv); 6205103a8feaSLen Brown 620696e47158SLen Brown if (!quiet) 6207d8af6f5fSLen Brown print_version(); 6208103a8feaSLen Brown 620941618e63SLen Brown probe_sysfs(); 621041618e63SLen Brown 6211103a8feaSLen Brown turbostat_init(); 6212103a8feaSLen Brown 62133b4d5c7fSAndy Shevchenko /* dump counters and exit */ 62143b4d5c7fSAndy Shevchenko if (dump_only) 62153b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 62163b4d5c7fSAndy Shevchenko 6217c8ade361SLen Brown /* list header and exit */ 6218c8ade361SLen Brown if (list_header_only) { 6219c8ade361SLen Brown print_header(","); 6220c8ade361SLen Brown flush_output_stdout(); 6221c8ade361SLen Brown return 0; 6222c8ade361SLen Brown } 6223c8ade361SLen Brown 62249972d5d8SChen Yu msr_sum_record(); 6225103a8feaSLen Brown /* 6226103a8feaSLen Brown * if any params left, it must be a command to fork 6227103a8feaSLen Brown */ 6228103a8feaSLen Brown if (argc - optind) 6229103a8feaSLen Brown return fork_it(argv + optind); 6230103a8feaSLen Brown else 6231103a8feaSLen Brown turbostat_loop(); 6232103a8feaSLen Brown 6233103a8feaSLen Brown return 0; 6234103a8feaSLen Brown } 6235