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 18344c2122d4SLen Brown void get_apic_id(struct thread_data *t) 18354c2122d4SLen Brown { 183634041551SLen Brown unsigned int eax, ebx, ecx, edx; 183734041551SLen Brown 183834041551SLen Brown if (DO_BIC(BIC_APIC)) { 183934041551SLen Brown eax = ebx = ecx = edx = 0; 184034041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 184134041551SLen Brown 184234041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 184334041551SLen Brown } 184434041551SLen Brown 184534041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 184634041551SLen Brown return; 184734041551SLen Brown 1848c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 184934041551SLen Brown unsigned int topology_extensions; 185034041551SLen Brown 185134041551SLen Brown if (max_extended_level < 0x8000001e) 185234041551SLen Brown return; 18534c2122d4SLen Brown 18544c2122d4SLen Brown eax = ebx = ecx = edx = 0; 185534041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 185634041551SLen Brown topology_extensions = ecx & (1 << 22); 185734041551SLen Brown 185834041551SLen Brown if (topology_extensions == 0) 185934041551SLen Brown return; 186034041551SLen Brown 186134041551SLen Brown eax = ebx = ecx = edx = 0; 186234041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 186334041551SLen Brown 186434041551SLen Brown t->x2apic_id = eax; 186534041551SLen Brown return; 186634041551SLen Brown } 18674c2122d4SLen Brown 18684c2122d4SLen Brown if (!genuine_intel) 18694c2122d4SLen Brown return; 18704c2122d4SLen Brown 18714c2122d4SLen Brown if (max_level < 0xb) 18724c2122d4SLen Brown return; 18734c2122d4SLen Brown 18744c2122d4SLen Brown ecx = 0; 18754c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 18764c2122d4SLen Brown t->x2apic_id = edx; 18774c2122d4SLen Brown 187834041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 187934041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 188034041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 18814c2122d4SLen Brown } 18824c2122d4SLen Brown 1883495c7654SLen Brown /* 1884c98d5d94SLen Brown * get_counters(...) 1885c98d5d94SLen Brown * migrate to cpu 1886c98d5d94SLen Brown * acquire and record local counters for that cpu 1887c98d5d94SLen Brown */ 1888c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1889c98d5d94SLen Brown { 1890c98d5d94SLen Brown int cpu = t->cpu_id; 1891889facbeSLen Brown unsigned long long msr; 18920102b067SLen Brown int aperf_mperf_retry_count = 0; 1893388e9c81SLen Brown struct msr_counter *mp; 1894388e9c81SLen Brown int i; 1895c98d5d94SLen Brown 1896e52966c0SLen Brown if (cpu_migrate(cpu)) { 1897b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 189815aaa346SLen Brown return -1; 1899e52966c0SLen Brown } 190015aaa346SLen Brown 1901d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1902d4794f25SYazen Ghannam 19034c2122d4SLen Brown if (first_counter_read) 19044c2122d4SLen Brown get_apic_id(t); 19050102b067SLen Brown retry: 1906c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 190715aaa346SLen Brown 19081e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 19091e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 19100102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 19110102b067SLen Brown 19120102b067SLen Brown /* 19130102b067SLen Brown * The TSC, APERF and MPERF must be read together for 19140102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 19150102b067SLen Brown * 19160102b067SLen Brown * Unfortunately, APERF and MPERF are read by 19170102b067SLen Brown * individual system call, so delays may occur 19180102b067SLen Brown * between them. If the time to read them 19190102b067SLen Brown * varies by a large amount, we re-read them. 19200102b067SLen Brown */ 19210102b067SLen Brown 19220102b067SLen Brown /* 19230102b067SLen Brown * This initial dummy APERF read has been seen to 19240102b067SLen Brown * reduce jitter in the subsequent reads. 19250102b067SLen Brown */ 19260102b067SLen Brown 19279c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1928c98d5d94SLen Brown return -3; 19290102b067SLen Brown 19300102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 19310102b067SLen Brown 19320102b067SLen Brown tsc_before = t->tsc; 19330102b067SLen Brown 19340102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 19350102b067SLen Brown return -3; 19360102b067SLen Brown 19370102b067SLen Brown tsc_between = rdtsc(); 19380102b067SLen Brown 19399c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1940c98d5d94SLen Brown return -4; 19410102b067SLen Brown 19420102b067SLen Brown tsc_after = rdtsc(); 19430102b067SLen Brown 19440102b067SLen Brown aperf_time = tsc_between - tsc_before; 19450102b067SLen Brown mperf_time = tsc_after - tsc_between; 19460102b067SLen Brown 19470102b067SLen Brown /* 19480102b067SLen Brown * If the system call latency to read APERF and MPERF 19490102b067SLen Brown * differ by more than 2x, then try again. 19500102b067SLen Brown */ 19510102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 19520102b067SLen Brown aperf_mperf_retry_count++; 19530102b067SLen Brown if (aperf_mperf_retry_count < 5) 19540102b067SLen Brown goto retry; 19550102b067SLen Brown else 19560102b067SLen Brown warnx("cpu%d jitter %lld %lld", 19570102b067SLen Brown cpu, aperf_time, mperf_time); 19580102b067SLen Brown } 19590102b067SLen Brown aperf_mperf_retry_count = 0; 19600102b067SLen Brown 1961b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1962b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 196388c3281fSLen Brown } 196488c3281fSLen Brown 1965812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1966562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1967812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 19681ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 19691ed51011SLen Brown return -5; 19701ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 19711ed51011SLen Brown } 19720539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1973144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1974144b44b1SLen Brown return -6; 1975144b44b1SLen Brown } 1976144b44b1SLen Brown 1977388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1978495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1979388e9c81SLen Brown return -10; 1980388e9c81SLen Brown } 1981388e9c81SLen Brown 1982c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1983c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1984f4fdf2b4SLen Brown goto done; 1985c98d5d94SLen Brown 19861e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 1987c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1988c98d5d94SLen Brown return -6; 1989144b44b1SLen Brown } 1990144b44b1SLen Brown 19911e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 1992c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1993c98d5d94SLen Brown return -7; 19941e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 1995fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1996fb5d4327SDasaratharaman Chandramouli return -7; 1997103a8feaSLen Brown } 199815aaa346SLen Brown 19991e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) 2000c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2001c98d5d94SLen Brown return -8; 2002c98d5d94SLen Brown 20030539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 20040539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 20050539ba11SLen Brown return -8; 20060539ba11SLen Brown 2007812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2008889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2009889facbeSLen Brown return -9; 2010889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2011889facbeSLen Brown } 2012889facbeSLen Brown 20139392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 20149392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 20159392bd98SCalvin Walton return -14; 20169392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 20179392bd98SCalvin Walton } 20189392bd98SCalvin Walton 2019388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2020495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2021388e9c81SLen Brown return -10; 2022388e9c81SLen Brown } 2023889facbeSLen Brown 2024c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2025c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2026f4fdf2b4SLen Brown goto done; 202715aaa346SLen Brown 2028a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 20290b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 20300b2bb692SLen Brown return -10; 2031a99d8730SLen Brown } 2032a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 20330b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 20340b2bb692SLen Brown return -11; 2035a99d8730SLen Brown } 2036a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 20370b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 20380b2bb692SLen Brown return -12; 2039a99d8730SLen Brown } 2040a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 20410b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 20420b2bb692SLen Brown return -13; 20430b2bb692SLen Brown } 20440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2045c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2046c98d5d94SLen Brown return -9; 20470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 20480539ba11SLen Brown if (do_slm_cstates) { 20490539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 20500539ba11SLen Brown return -10; 20510539ba11SLen Brown } else { 2052c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2053c98d5d94SLen Brown return -10; 20540539ba11SLen Brown } 20550539ba11SLen Brown } 20560539ba11SLen Brown 20570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2058c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2059c98d5d94SLen Brown return -11; 20600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2061c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2062c98d5d94SLen Brown return -12; 20630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2064ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2065ca58710fSKristen Carlson Accardi return -13; 20660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2067ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2068ca58710fSKristen Carlson Accardi return -13; 20690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2070ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2071ca58710fSKristen Carlson Accardi return -13; 20720f47c08dSLen Brown 2073be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2074be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2075be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2076be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2077be0e54c4SLen Brown 2078889facbeSLen Brown if (do_rapl & RAPL_PKG) { 20799972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2080889facbeSLen Brown return -13; 20819972d5d8SChen Yu p->energy_pkg = msr; 2082889facbeSLen Brown } 20839148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 20849972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2085889facbeSLen Brown return -14; 20869972d5d8SChen Yu p->energy_cores = msr; 2087889facbeSLen Brown } 2088889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 20899972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2090889facbeSLen Brown return -15; 20919972d5d8SChen Yu p->energy_dram = msr; 2092889facbeSLen Brown } 2093889facbeSLen Brown if (do_rapl & RAPL_GFX) { 20949972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2095889facbeSLen Brown return -16; 20969972d5d8SChen Yu p->energy_gfx = msr; 2097889facbeSLen Brown } 2098889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 20999972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2100889facbeSLen Brown return -16; 21019972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2102889facbeSLen Brown } 2103889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 21049972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2105889facbeSLen Brown return -16; 21069972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2107889facbeSLen Brown } 21083316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21099972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 21103316f99aSCalvin Walton return -13; 21119972d5d8SChen Yu p->energy_pkg = msr; 21123316f99aSCalvin Walton } 2113812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2114889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2115889facbeSLen Brown return -17; 2116889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2117889facbeSLen Brown } 2118fdf676e5SLen Brown 2119812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2120fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2121fdf676e5SLen Brown 2122812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 212327d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 212427d47356SLen Brown 2125b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2126b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2127b4b91569SRafael Antognolli 2128388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2129495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2130388e9c81SLen Brown return -10; 2131388e9c81SLen Brown } 2132f4fdf2b4SLen Brown done: 2133f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2134388e9c81SLen Brown 213515aaa346SLen Brown return 0; 2136103a8feaSLen Brown } 2137103a8feaSLen Brown 2138ee7e38e3SLen Brown /* 2139ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2140ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2141ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2142ee7e38e3SLen Brown */ 2143ee7e38e3SLen Brown 2144ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2145ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2146ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2147ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2148ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2149ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2150ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2151ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2152ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2153ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2154ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2155ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 21560b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 21570b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2158445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2159445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2160ee7e38e3SLen Brown 2161ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2162ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2163445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2164ee7e38e3SLen Brown 2165e9257f5fSLen 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}; 2166e9257f5fSLen 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}; 2167e9257f5fSLen 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}; 21680539ba11SLen 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}; 2169f2642888SLen 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}; 2170e9257f5fSLen 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}; 2171445640a5SLen 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}; 21722085e124SArtem 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}; 2173ee7e38e3SLen Brown 2174a2b7b749SLen Brown 2175a2b7b749SLen Brown static void 2176a2b7b749SLen Brown calculate_tsc_tweak() 2177a2b7b749SLen Brown { 2178a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2179a2b7b749SLen Brown } 2180a2b7b749SLen Brown 2181fcd17211SLen Brown static void 2182fcd17211SLen Brown dump_nhm_platform_info(void) 2183103a8feaSLen Brown { 2184103a8feaSLen Brown unsigned long long msr; 2185103a8feaSLen Brown unsigned int ratio; 2186103a8feaSLen Brown 2187ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2188103a8feaSLen Brown 2189b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 21906574a5d5SLen Brown 2191103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2192710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2193103a8feaSLen Brown ratio, bclk, ratio * bclk); 2194103a8feaSLen Brown 2195103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2196710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2197103a8feaSLen Brown ratio, bclk, ratio * bclk); 2198103a8feaSLen Brown 21997ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2200b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2201bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 220267920418SLen Brown 2203fcd17211SLen Brown return; 2204fcd17211SLen Brown } 2205fcd17211SLen Brown 2206fcd17211SLen Brown static void 2207fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2208fcd17211SLen Brown { 2209fcd17211SLen Brown unsigned long long msr; 2210fcd17211SLen Brown unsigned int ratio; 2211fcd17211SLen Brown 22127ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2213fcd17211SLen Brown 2214b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2215fcd17211SLen Brown 2216fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2217fcd17211SLen Brown if (ratio) 2218710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2219fcd17211SLen Brown ratio, bclk, ratio * bclk); 2220fcd17211SLen Brown 2221fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2222fcd17211SLen Brown if (ratio) 2223710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2224fcd17211SLen Brown ratio, bclk, ratio * bclk); 2225fcd17211SLen Brown return; 2226fcd17211SLen Brown } 2227fcd17211SLen Brown 2228fcd17211SLen Brown static void 2229fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2230fcd17211SLen Brown { 2231fcd17211SLen Brown unsigned long long msr; 2232fcd17211SLen Brown unsigned int ratio; 22336574a5d5SLen Brown 22347ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 22356574a5d5SLen Brown 2236b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 22376574a5d5SLen Brown 22386574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 22396574a5d5SLen Brown if (ratio) 2240710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 22416574a5d5SLen Brown ratio, bclk, ratio * bclk); 22426574a5d5SLen Brown 22436574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 22446574a5d5SLen Brown if (ratio) 2245710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 22466574a5d5SLen Brown ratio, bclk, ratio * bclk); 22476574a5d5SLen Brown 22486574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 22496574a5d5SLen Brown if (ratio) 2250710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 22516574a5d5SLen Brown ratio, bclk, ratio * bclk); 22526574a5d5SLen Brown 22536574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 22546574a5d5SLen Brown if (ratio) 2255710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 22566574a5d5SLen Brown ratio, bclk, ratio * bclk); 22576574a5d5SLen Brown 22586574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 22596574a5d5SLen Brown if (ratio) 2260710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 22616574a5d5SLen Brown ratio, bclk, ratio * bclk); 22626574a5d5SLen Brown 22636574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 22646574a5d5SLen Brown if (ratio) 2265710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 22666574a5d5SLen Brown ratio, bclk, ratio * bclk); 22676574a5d5SLen Brown 22686574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 22696574a5d5SLen Brown if (ratio) 2270710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 22716574a5d5SLen Brown ratio, bclk, ratio * bclk); 22726574a5d5SLen Brown 22736574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 22746574a5d5SLen Brown if (ratio) 2275710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 22766574a5d5SLen Brown ratio, bclk, ratio * bclk); 2277103a8feaSLen Brown return; 2278fcd17211SLen Brown } 227931e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 228031e07522SLen Brown { 228131e07522SLen Brown 228231e07522SLen Brown if (!genuine_intel) 228331e07522SLen Brown return 0; 228431e07522SLen Brown 228531e07522SLen Brown switch (model) { 228631e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 228731e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 22885ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 228920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 229031e07522SLen Brown return 1; 229131e07522SLen Brown } 229231e07522SLen Brown return 0; 229331e07522SLen Brown } 2294fcd17211SLen Brown 2295fcd17211SLen Brown static void 229631e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2297fcd17211SLen Brown { 229831e07522SLen Brown unsigned long long msr, core_counts; 229931e07522SLen Brown unsigned int ratio, group_size; 2300103a8feaSLen Brown 23017ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2302b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 23036574a5d5SLen Brown 230431e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 230531e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 230631e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 230731e07522SLen Brown } else { 230831e07522SLen Brown core_counts = 0x0807060504030201; 230931e07522SLen Brown } 231031e07522SLen Brown 23116574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 231231e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 23136574a5d5SLen Brown if (ratio) 231431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 231531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23166574a5d5SLen Brown 23176574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 231831e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 23196574a5d5SLen Brown if (ratio) 232031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 232131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23226574a5d5SLen Brown 23236574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 232431e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 23256574a5d5SLen Brown if (ratio) 232631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 232731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23286574a5d5SLen Brown 23296574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 233031e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 23316574a5d5SLen Brown if (ratio) 233231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 233331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23346574a5d5SLen Brown 2335103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 233631e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2337103a8feaSLen Brown if (ratio) 233831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 233931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2340103a8feaSLen Brown 2341103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 234231e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2343103a8feaSLen Brown if (ratio) 234431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 234531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2346103a8feaSLen Brown 2347103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 234831e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2349103a8feaSLen Brown if (ratio) 235031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 235131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2352103a8feaSLen Brown 2353103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 235431e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2355103a8feaSLen Brown if (ratio) 235631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 235731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2358fcd17211SLen Brown return; 2359fcd17211SLen Brown } 23603a9a941dSLen Brown 2361fcd17211SLen Brown static void 23620f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 23630f7887c4SLen Brown { 23640f7887c4SLen Brown unsigned long long msr; 23650f7887c4SLen Brown unsigned int ratio; 23660f7887c4SLen Brown 23670f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 23680f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 23690f7887c4SLen Brown 23700f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 23710f7887c4SLen Brown if (ratio) 23720f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 23730f7887c4SLen Brown ratio, bclk, ratio * bclk); 23740f7887c4SLen Brown 23750f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 23760f7887c4SLen Brown if (ratio) 23770f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 23780f7887c4SLen Brown ratio, bclk, ratio * bclk); 23790f7887c4SLen Brown 23800f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 23810f7887c4SLen Brown if (ratio) 23820f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 23830f7887c4SLen Brown ratio, bclk, ratio * bclk); 23840f7887c4SLen Brown 23850f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 23860f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 23870f7887c4SLen Brown 23880f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 23890f7887c4SLen Brown if (ratio) 23900f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 23910f7887c4SLen Brown ratio, bclk, ratio * bclk); 23920f7887c4SLen Brown 23930f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 23940f7887c4SLen Brown if (ratio) 23950f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 23960f7887c4SLen Brown ratio, bclk, ratio * bclk); 23970f7887c4SLen Brown 23980f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 23990f7887c4SLen Brown if (ratio) 24000f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 24010f7887c4SLen Brown ratio, bclk, ratio * bclk); 24020f7887c4SLen Brown 24030f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 24040f7887c4SLen Brown if (ratio) 24050f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 24060f7887c4SLen Brown ratio, bclk, ratio * bclk); 24070f7887c4SLen Brown } 24080f7887c4SLen Brown 24090f7887c4SLen Brown static void 2410fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2411fb5d4327SDasaratharaman Chandramouli { 2412cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2413cbf97abaSHubert Chrzaniuk 2414fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2415cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2416cbf97abaSHubert Chrzaniuk int i, b_nr; 2417cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2418cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2419fb5d4327SDasaratharaman Chandramouli 2420ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2421fb5d4327SDasaratharaman Chandramouli 2422b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2423bfae2052SLen Brown base_cpu, msr); 2424fb5d4327SDasaratharaman Chandramouli 2425fb5d4327SDasaratharaman Chandramouli /** 2426fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2427cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2428cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2429fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2430fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2431fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2432fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2433fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2434fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2435fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2436fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2437fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2438fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2439fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2440fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2441fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2442fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2443fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2444fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2445fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2446fb5d4327SDasaratharaman Chandramouli */ 2447fb5d4327SDasaratharaman Chandramouli 2448cbf97abaSHubert Chrzaniuk b_nr = 0; 2449cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2450cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2451cbf97abaSHubert Chrzaniuk 2452cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2453fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2454cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2455fb5d4327SDasaratharaman Chandramouli 2456cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2457cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2458cbf97abaSHubert Chrzaniuk b_nr++; 2459fb5d4327SDasaratharaman Chandramouli } 2460cbf97abaSHubert Chrzaniuk 2461cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2462cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2463b7d8c148SLen Brown fprintf(outf, 2464710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2465cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2466fb5d4327SDasaratharaman Chandramouli } 2467fb5d4327SDasaratharaman Chandramouli 2468fb5d4327SDasaratharaman Chandramouli static void 2469fcd17211SLen Brown dump_nhm_cst_cfg(void) 2470fcd17211SLen Brown { 2471fcd17211SLen Brown unsigned long long msr; 2472fcd17211SLen Brown 24731df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2474fcd17211SLen Brown 24751df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2476fcd17211SLen Brown 24773e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2478fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2479fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2480fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2481fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2482fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 24836c34f160SLen Brown (unsigned int)msr & 0xF, 2484fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2485ac980e13SArtem Bityutskiy 2486ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2487ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2488ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2489ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2490ac980e13SArtem Bityutskiy } 2491ac980e13SArtem Bityutskiy 2492ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2493ac980e13SArtem Bityutskiy 2494fcd17211SLen Brown return; 2495103a8feaSLen Brown } 2496103a8feaSLen Brown 24976fb3143bSLen Brown static void 24986fb3143bSLen Brown dump_config_tdp(void) 24996fb3143bSLen Brown { 25006fb3143bSLen Brown unsigned long long msr; 25016fb3143bSLen Brown 25026fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2503b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2504685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 25056fb3143bSLen Brown 25066fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2507b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 25086fb3143bSLen Brown if (msr) { 2509685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2510685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2511685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2512685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 25136fb3143bSLen Brown } 2514b7d8c148SLen Brown fprintf(outf, ")\n"); 25156fb3143bSLen Brown 25166fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2517b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 25186fb3143bSLen Brown if (msr) { 2519685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2520685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2521685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2522685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 25236fb3143bSLen Brown } 2524b7d8c148SLen Brown fprintf(outf, ")\n"); 25256fb3143bSLen Brown 25266fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2527b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 25286fb3143bSLen Brown if ((msr) & 0x3) 2529b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2530b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2531b7d8c148SLen Brown fprintf(outf, ")\n"); 25326fb3143bSLen Brown 25336fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2534b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2535685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2536b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2537b7d8c148SLen Brown fprintf(outf, ")\n"); 25386fb3143bSLen Brown } 25395a63426eSLen Brown 25405a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 25415a63426eSLen Brown 25425a63426eSLen Brown void print_irtl(void) 25435a63426eSLen Brown { 25445a63426eSLen Brown unsigned long long msr; 25455a63426eSLen Brown 25465a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 25475a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 25485a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25495a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25505a63426eSLen Brown 25515a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 25525a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 25535a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25545a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25555a63426eSLen Brown 25565a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 25575a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 25585a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25595a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25605a63426eSLen Brown 25615a63426eSLen Brown if (!do_irtl_hsw) 25625a63426eSLen Brown return; 25635a63426eSLen Brown 25645a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 25655a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 25665a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25675a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25685a63426eSLen Brown 25695a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 25705a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 25715a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25725a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25735a63426eSLen Brown 25745a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 25755a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 25765a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25775a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25785a63426eSLen Brown 25795a63426eSLen Brown } 258036229897SLen Brown void free_fd_percpu(void) 258136229897SLen Brown { 258236229897SLen Brown int i; 258336229897SLen Brown 258401a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 258536229897SLen Brown if (fd_percpu[i] != 0) 258636229897SLen Brown close(fd_percpu[i]); 258736229897SLen Brown } 258836229897SLen Brown 258936229897SLen Brown free(fd_percpu); 25906fb3143bSLen Brown } 25916fb3143bSLen Brown 2592c98d5d94SLen Brown void free_all_buffers(void) 2593103a8feaSLen Brown { 25940e2d8f05SLen Brown int i; 25950e2d8f05SLen Brown 2596c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2597c98d5d94SLen Brown cpu_present_set = NULL; 259836229897SLen Brown cpu_present_setsize = 0; 2599103a8feaSLen Brown 2600c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2601c98d5d94SLen Brown cpu_affinity_set = NULL; 2602c98d5d94SLen Brown cpu_affinity_setsize = 0; 2603103a8feaSLen Brown 2604c98d5d94SLen Brown free(thread_even); 2605c98d5d94SLen Brown free(core_even); 2606c98d5d94SLen Brown free(package_even); 2607103a8feaSLen Brown 2608c98d5d94SLen Brown thread_even = NULL; 2609c98d5d94SLen Brown core_even = NULL; 2610c98d5d94SLen Brown package_even = NULL; 2611103a8feaSLen Brown 2612c98d5d94SLen Brown free(thread_odd); 2613c98d5d94SLen Brown free(core_odd); 2614c98d5d94SLen Brown free(package_odd); 2615103a8feaSLen Brown 2616c98d5d94SLen Brown thread_odd = NULL; 2617c98d5d94SLen Brown core_odd = NULL; 2618c98d5d94SLen Brown package_odd = NULL; 2619103a8feaSLen Brown 2620c98d5d94SLen Brown free(output_buffer); 2621c98d5d94SLen Brown output_buffer = NULL; 2622c98d5d94SLen Brown outp = NULL; 262336229897SLen Brown 262436229897SLen Brown free_fd_percpu(); 2625562a2d37SLen Brown 2626562a2d37SLen Brown free(irq_column_2_cpu); 2627562a2d37SLen Brown free(irqs_per_cpu); 26280e2d8f05SLen Brown 26290e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 26300e2d8f05SLen Brown if (cpus[i].put_ids) 26310e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 26320e2d8f05SLen Brown } 26330e2d8f05SLen Brown free(cpus); 2634103a8feaSLen Brown } 2635103a8feaSLen Brown 263657a42a34SJosh Triplett 263757a42a34SJosh Triplett /* 263895aebc44SJosh Triplett * Parse a file containing a single int. 26396de68fe1SLen Brown * Return 0 if file can not be opened 26406de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 264195aebc44SJosh Triplett */ 264295aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 264395aebc44SJosh Triplett { 264495aebc44SJosh Triplett va_list args; 264595aebc44SJosh Triplett char path[PATH_MAX]; 264695aebc44SJosh Triplett FILE *filep; 264795aebc44SJosh Triplett int value; 264895aebc44SJosh Triplett 264995aebc44SJosh Triplett va_start(args, fmt); 265095aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 265195aebc44SJosh Triplett va_end(args); 26526de68fe1SLen Brown filep = fopen(path, "r"); 26536de68fe1SLen Brown if (!filep) 26546de68fe1SLen Brown return 0; 2655b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2656b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 265795aebc44SJosh Triplett fclose(filep); 265895aebc44SJosh Triplett return value; 265995aebc44SJosh Triplett } 266095aebc44SJosh Triplett 266195aebc44SJosh Triplett /* 2662c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2663c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2664c98d5d94SLen Brown */ 2665c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2666c98d5d94SLen Brown { 266795aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2668103a8feaSLen Brown } 2669103a8feaSLen Brown 2670103a8feaSLen Brown int get_physical_package_id(int cpu) 2671103a8feaSLen Brown { 267295aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2673103a8feaSLen Brown } 2674103a8feaSLen Brown 26756de68fe1SLen Brown int get_die_id(int cpu) 26766de68fe1SLen Brown { 26776de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 26786de68fe1SLen Brown } 26796de68fe1SLen Brown 2680103a8feaSLen Brown int get_core_id(int cpu) 2681103a8feaSLen Brown { 268295aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2683103a8feaSLen Brown } 2684103a8feaSLen Brown 2685ef605741SPrarit Bhargava void set_node_data(void) 2686ef605741SPrarit Bhargava { 26872ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 26882ffbb224SPrarit Bhargava int cpu_count; 2689ef605741SPrarit Bhargava 26902ffbb224SPrarit Bhargava /* initialize logical_node_id */ 26912ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 26922ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2693ef605741SPrarit Bhargava 26942ffbb224SPrarit Bhargava cpu_count = 0; 26952ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 26962ffbb224SPrarit Bhargava lnode = 0; 26972ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 26982ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2699ef605741SPrarit Bhargava continue; 27002ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 27012ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 27022ffbb224SPrarit Bhargava continue; 27032ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2704ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 27052ffbb224SPrarit Bhargava cpu_count++; 27062ffbb224SPrarit Bhargava /* 27072ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 27082ffbb224SPrarit Bhargava * the logical_node_id 27092ffbb224SPrarit Bhargava */ 27102ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 27112ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 27122ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 27132ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 27142ffbb224SPrarit Bhargava cpu_count++; 2715ef605741SPrarit Bhargava } 27162ffbb224SPrarit Bhargava } 27172ffbb224SPrarit Bhargava lnode++; 27182ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 27192ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 27202ffbb224SPrarit Bhargava } 27212ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 27222ffbb224SPrarit Bhargava break; 27232ffbb224SPrarit Bhargava } 2724ef605741SPrarit Bhargava } 2725ef605741SPrarit Bhargava 2726ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2727c98d5d94SLen Brown { 2728c98d5d94SLen Brown char path[80]; 2729c98d5d94SLen Brown FILE *filep; 27300e2d8f05SLen Brown int i; 27310e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2732c98d5d94SLen Brown 27330e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 27340e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 27350e2d8f05SLen Brown cpu, i); 27360e2d8f05SLen Brown filep = fopen(path, "r"); 27370e2d8f05SLen Brown if (!filep) 27380e2d8f05SLen Brown continue; 27390e2d8f05SLen Brown fclose(filep); 27400e2d8f05SLen Brown return i; 27410e2d8f05SLen Brown } 27420e2d8f05SLen Brown return -1; 2743e275b388SDasaratharaman Chandramouli } 2744c98d5d94SLen Brown 27450e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 27460e2d8f05SLen Brown { 27470e2d8f05SLen Brown char path[80], character; 27480e2d8f05SLen Brown FILE *filep; 27490e2d8f05SLen Brown unsigned long map; 27508cb48b32SPrarit Bhargava int so, shift, sib_core; 27510e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 27520e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 27530e2d8f05SLen Brown size_t size; 27548cb48b32SPrarit Bhargava int thread_id = 0; 27550e2d8f05SLen Brown 27560e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 27578cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 27588cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 27590e2d8f05SLen Brown if (!thiscpu->put_ids) 27600e2d8f05SLen Brown return -1; 27610e2d8f05SLen Brown 27620e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 27630e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 27640e2d8f05SLen Brown 27650e2d8f05SLen Brown sprintf(path, 27660e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 27670e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 27680e2d8f05SLen Brown do { 27690e2d8f05SLen Brown offset -= BITMASK_SIZE; 27708173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 27718173c336SBen Hutchings err(1, "%s: failed to parse file", path); 27720e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 27730e2d8f05SLen Brown if ((map >> shift) & 0x1) { 27748cb48b32SPrarit Bhargava so = shift + offset; 27758cb48b32SPrarit Bhargava sib_core = get_core_id(so); 27768cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 27778cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 27788cb48b32SPrarit Bhargava if ((so != cpu) && 27798cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 27808cb48b32SPrarit Bhargava cpus[so].thread_id = 27818cb48b32SPrarit Bhargava thread_id++; 27828cb48b32SPrarit Bhargava } 27830e2d8f05SLen Brown } 27840e2d8f05SLen Brown } 27850e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2786c98d5d94SLen Brown fclose(filep); 27870e2d8f05SLen Brown 27880e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2789c98d5d94SLen Brown } 2790c98d5d94SLen Brown 2791c98d5d94SLen Brown /* 2792c98d5d94SLen Brown * run func(thread, core, package) in topology order 2793c98d5d94SLen Brown * skip non-present cpus 2794103a8feaSLen Brown */ 2795103a8feaSLen Brown 2796c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2797c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2798c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2799c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2800c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2801c98d5d94SLen Brown struct pkg_data *pkg_base2) 2802c98d5d94SLen Brown { 280340f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2804c98d5d94SLen Brown 2805c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 280640f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 280740f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 280840f5cfe7SPrarit Bhargava ++core_no) { 2809c98d5d94SLen Brown for (thread_no = 0; thread_no < 281070a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2811c98d5d94SLen Brown struct thread_data *t, *t2; 2812c98d5d94SLen Brown struct core_data *c, *c2; 2813c98d5d94SLen Brown struct pkg_data *p, *p2; 2814c98d5d94SLen Brown 281540f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 281640f5cfe7SPrarit Bhargava core_no, node_no, 281740f5cfe7SPrarit Bhargava pkg_no); 2818c98d5d94SLen Brown 2819c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2820c98d5d94SLen Brown continue; 2821c98d5d94SLen Brown 282240f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 282340f5cfe7SPrarit Bhargava core_no, node_no, 282440f5cfe7SPrarit Bhargava pkg_no); 2825c98d5d94SLen Brown 282640f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 282740f5cfe7SPrarit Bhargava node_no, pkg_no); 282840f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 282940f5cfe7SPrarit Bhargava node_no, 283040f5cfe7SPrarit Bhargava pkg_no); 2831c98d5d94SLen Brown 2832c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2833c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2834c98d5d94SLen Brown 2835c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2836c98d5d94SLen Brown if (retval) 2837c98d5d94SLen Brown return retval; 2838c98d5d94SLen Brown } 2839c98d5d94SLen Brown } 2840c98d5d94SLen Brown } 284140f5cfe7SPrarit Bhargava } 2842c98d5d94SLen Brown return 0; 2843c98d5d94SLen Brown } 2844c98d5d94SLen Brown 2845c98d5d94SLen Brown /* 2846c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2847c98d5d94SLen Brown * return max_cpu number 2848c98d5d94SLen Brown */ 2849c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2850103a8feaSLen Brown { 2851103a8feaSLen Brown FILE *fp; 2852c98d5d94SLen Brown int cpu_num; 2853103a8feaSLen Brown int retval; 2854103a8feaSLen Brown 285557a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2856103a8feaSLen Brown 2857103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2858b2c95d90SJosh Triplett if (retval != 0) 2859b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2860103a8feaSLen Brown 2861c98d5d94SLen Brown while (1) { 2862c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2863103a8feaSLen Brown if (retval != 1) 2864103a8feaSLen Brown break; 2865103a8feaSLen Brown 2866c98d5d94SLen Brown retval = func(cpu_num); 2867c98d5d94SLen Brown if (retval) { 2868c98d5d94SLen Brown fclose(fp); 2869c98d5d94SLen Brown return(retval); 2870c98d5d94SLen Brown } 2871103a8feaSLen Brown } 2872103a8feaSLen Brown fclose(fp); 2873c98d5d94SLen Brown return 0; 2874103a8feaSLen Brown } 2875103a8feaSLen Brown 2876103a8feaSLen Brown void re_initialize(void) 2877103a8feaSLen Brown { 2878c98d5d94SLen Brown free_all_buffers(); 2879c98d5d94SLen Brown setup_all_buffers(); 2880c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2881103a8feaSLen Brown } 2882103a8feaSLen Brown 2883843c5791SPrarit Bhargava void set_max_cpu_num(void) 2884843c5791SPrarit Bhargava { 2885843c5791SPrarit Bhargava FILE *filep; 28868201a028SPrarit Bhargava int base_cpu; 2887843c5791SPrarit Bhargava unsigned long dummy; 28888201a028SPrarit Bhargava char pathname[64]; 2889843c5791SPrarit Bhargava 28908201a028SPrarit Bhargava base_cpu = sched_getcpu(); 28918201a028SPrarit Bhargava if (base_cpu < 0) 28928201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 28938201a028SPrarit Bhargava sprintf(pathname, 28948201a028SPrarit Bhargava "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", 28958201a028SPrarit Bhargava base_cpu); 28968201a028SPrarit Bhargava 28978201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 2898843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2899843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 29000e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2901843c5791SPrarit Bhargava fclose(filep); 2902843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2903843c5791SPrarit Bhargava } 2904c98d5d94SLen Brown 2905103a8feaSLen Brown /* 2906c98d5d94SLen Brown * count_cpus() 2907c98d5d94SLen Brown * remember the last one seen, it will be the max 2908103a8feaSLen Brown */ 2909c98d5d94SLen Brown int count_cpus(int cpu) 2910103a8feaSLen Brown { 2911843c5791SPrarit Bhargava topo.num_cpus++; 2912c98d5d94SLen Brown return 0; 2913103a8feaSLen Brown } 2914c98d5d94SLen Brown int mark_cpu_present(int cpu) 2915c98d5d94SLen Brown { 2916c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 291715aaa346SLen Brown return 0; 2918103a8feaSLen Brown } 2919103a8feaSLen Brown 29208cb48b32SPrarit Bhargava int init_thread_id(int cpu) 29218cb48b32SPrarit Bhargava { 29228cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 29238cb48b32SPrarit Bhargava return 0; 29248cb48b32SPrarit Bhargava } 29258cb48b32SPrarit Bhargava 2926562a2d37SLen Brown /* 2927562a2d37SLen Brown * snapshot_proc_interrupts() 2928562a2d37SLen Brown * 2929562a2d37SLen Brown * read and record summary of /proc/interrupts 2930562a2d37SLen Brown * 2931562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2932562a2d37SLen Brown */ 2933562a2d37SLen Brown int snapshot_proc_interrupts(void) 2934562a2d37SLen Brown { 2935562a2d37SLen Brown static FILE *fp; 2936562a2d37SLen Brown int column, retval; 2937562a2d37SLen Brown 2938562a2d37SLen Brown if (fp == NULL) 2939562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2940562a2d37SLen Brown else 2941562a2d37SLen Brown rewind(fp); 2942562a2d37SLen Brown 2943562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2944562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2945562a2d37SLen Brown int cpu_number; 2946562a2d37SLen Brown 2947562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2948562a2d37SLen Brown if (retval != 1) 2949562a2d37SLen Brown break; 2950562a2d37SLen Brown 2951562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2952562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2953562a2d37SLen Brown return 1; 2954562a2d37SLen Brown } 2955562a2d37SLen Brown 2956562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2957562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2958562a2d37SLen Brown } 2959562a2d37SLen Brown 2960562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2961562a2d37SLen Brown while (1) { 2962562a2d37SLen Brown int column; 2963562a2d37SLen Brown char buf[64]; 2964562a2d37SLen Brown 2965562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2966562a2d37SLen Brown if (retval != 1) 2967562a2d37SLen Brown break; 2968562a2d37SLen Brown 2969562a2d37SLen Brown /* read the count per cpu */ 2970562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2971562a2d37SLen Brown 2972562a2d37SLen Brown int cpu_number, irq_count; 2973562a2d37SLen Brown 2974562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2975562a2d37SLen Brown if (retval != 1) 2976562a2d37SLen Brown break; 2977562a2d37SLen Brown 2978562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2979562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2980562a2d37SLen Brown 2981562a2d37SLen Brown } 2982562a2d37SLen Brown 2983562a2d37SLen Brown while (getc(fp) != '\n') 2984562a2d37SLen Brown ; /* flush interrupt description */ 2985562a2d37SLen Brown 2986562a2d37SLen Brown } 2987562a2d37SLen Brown return 0; 2988562a2d37SLen Brown } 298927d47356SLen Brown /* 2990fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2991fdf676e5SLen Brown * 2992fdf676e5SLen Brown * record snapshot of 2993fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2994fdf676e5SLen Brown * 2995fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2996fdf676e5SLen Brown */ 2997fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2998fdf676e5SLen Brown { 2999fdf676e5SLen Brown FILE *fp; 3000fdf676e5SLen Brown int retval; 3001fdf676e5SLen Brown 3002fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3003fdf676e5SLen Brown 3004fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3005fdf676e5SLen Brown if (retval != 1) 3006fdf676e5SLen Brown err(1, "GFX rc6"); 3007fdf676e5SLen Brown 3008fdf676e5SLen Brown fclose(fp); 3009fdf676e5SLen Brown 3010fdf676e5SLen Brown return 0; 3011fdf676e5SLen Brown } 3012fdf676e5SLen Brown /* 301327d47356SLen Brown * snapshot_gfx_mhz() 301427d47356SLen Brown * 301527d47356SLen Brown * record snapshot of 301627d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 301727d47356SLen Brown * 301827d47356SLen Brown * return 1 if config change requires a restart, else return 0 301927d47356SLen Brown */ 302027d47356SLen Brown int snapshot_gfx_mhz(void) 302127d47356SLen Brown { 302227d47356SLen Brown static FILE *fp; 302327d47356SLen Brown int retval; 302427d47356SLen Brown 302527d47356SLen Brown if (fp == NULL) 302627d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 302722048c54SLen Brown else { 302827d47356SLen Brown rewind(fp); 302922048c54SLen Brown fflush(fp); 303022048c54SLen Brown } 303127d47356SLen Brown 303227d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 303327d47356SLen Brown if (retval != 1) 303427d47356SLen Brown err(1, "GFX MHz"); 303527d47356SLen Brown 303627d47356SLen Brown return 0; 303727d47356SLen Brown } 3038562a2d37SLen Brown 3039562a2d37SLen Brown /* 3040b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3041b4b91569SRafael Antognolli * 3042b4b91569SRafael Antognolli * record snapshot of 3043b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3044b4b91569SRafael Antognolli * 3045b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3046b4b91569SRafael Antognolli */ 3047b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3048b4b91569SRafael Antognolli { 3049b4b91569SRafael Antognolli static FILE *fp; 3050b4b91569SRafael Antognolli int retval; 3051b4b91569SRafael Antognolli 3052b4b91569SRafael Antognolli if (fp == NULL) 3053b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3054b4b91569SRafael Antognolli else { 3055b4b91569SRafael Antognolli rewind(fp); 3056b4b91569SRafael Antognolli fflush(fp); 3057b4b91569SRafael Antognolli } 3058b4b91569SRafael Antognolli 3059b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3060b4b91569SRafael Antognolli if (retval != 1) 3061b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3062b4b91569SRafael Antognolli 3063b4b91569SRafael Antognolli return 0; 3064b4b91569SRafael Antognolli } 3065b4b91569SRafael Antognolli 3066b4b91569SRafael Antognolli /* 3067be0e54c4SLen Brown * snapshot_cpu_lpi() 3068be0e54c4SLen Brown * 3069be0e54c4SLen Brown * record snapshot of 3070be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3071be0e54c4SLen Brown */ 3072be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3073be0e54c4SLen Brown { 3074be0e54c4SLen Brown FILE *fp; 3075be0e54c4SLen Brown int retval; 3076be0e54c4SLen Brown 3077be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3078be0e54c4SLen Brown 3079be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 30805ea7647bSPrarit Bhargava if (retval != 1) { 30815ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 30825ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3083605736c6SGustavo A. R. Silva fclose(fp); 30845ea7647bSPrarit Bhargava return -1; 30855ea7647bSPrarit Bhargava } 3086be0e54c4SLen Brown 3087be0e54c4SLen Brown fclose(fp); 3088be0e54c4SLen Brown 3089be0e54c4SLen Brown return 0; 3090be0e54c4SLen Brown } 3091be0e54c4SLen Brown /* 3092be0e54c4SLen Brown * snapshot_sys_lpi() 3093be0e54c4SLen Brown * 30941f81c5efSLen Brown * record snapshot of sys_lpi_file 3095be0e54c4SLen Brown */ 3096be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3097be0e54c4SLen Brown { 3098be0e54c4SLen Brown FILE *fp; 3099be0e54c4SLen Brown int retval; 3100be0e54c4SLen Brown 31011f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3102be0e54c4SLen Brown 3103be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 31045ea7647bSPrarit Bhargava if (retval != 1) { 31055ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 31065ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 310715423b95SColin Ian King fclose(fp); 31085ea7647bSPrarit Bhargava return -1; 31095ea7647bSPrarit Bhargava } 3110be0e54c4SLen Brown fclose(fp); 3111be0e54c4SLen Brown 3112be0e54c4SLen Brown return 0; 3113be0e54c4SLen Brown } 3114be0e54c4SLen Brown /* 3115562a2d37SLen Brown * snapshot /proc and /sys files 3116562a2d37SLen Brown * 3117562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3118562a2d37SLen Brown */ 3119562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3120562a2d37SLen Brown { 3121218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3122562a2d37SLen Brown if (snapshot_proc_interrupts()) 3123562a2d37SLen Brown return 1; 3124562a2d37SLen Brown 3125812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3126fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3127fdf676e5SLen Brown 3128812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 312927d47356SLen Brown snapshot_gfx_mhz(); 313027d47356SLen Brown 3131b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3132b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3133b4b91569SRafael Antognolli 3134be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3135be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3136be0e54c4SLen Brown 3137be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3138be0e54c4SLen Brown snapshot_sys_lpi_us(); 3139be0e54c4SLen Brown 3140562a2d37SLen Brown return 0; 3141562a2d37SLen Brown } 3142562a2d37SLen Brown 31438aa2ed0bSLen Brown int exit_requested; 31448aa2ed0bSLen Brown 31458aa2ed0bSLen Brown static void signal_handler (int signal) 31468aa2ed0bSLen Brown { 31478aa2ed0bSLen Brown switch (signal) { 31488aa2ed0bSLen Brown case SIGINT: 31498aa2ed0bSLen Brown exit_requested = 1; 31508aa2ed0bSLen Brown if (debug) 31518aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 31528aa2ed0bSLen Brown break; 315307211960SLen Brown case SIGUSR1: 315407211960SLen Brown if (debug > 1) 315507211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 315607211960SLen Brown break; 31578aa2ed0bSLen Brown } 31588aa2ed0bSLen Brown } 31598aa2ed0bSLen Brown 31608aa2ed0bSLen Brown void setup_signal_handler(void) 31618aa2ed0bSLen Brown { 31628aa2ed0bSLen Brown struct sigaction sa; 31638aa2ed0bSLen Brown 31648aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 31658aa2ed0bSLen Brown 31668aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 31678aa2ed0bSLen Brown 31688aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 31698aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 317007211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 317107211960SLen Brown err(1, "sigaction SIGUSR1"); 31728aa2ed0bSLen Brown } 3173b9ad8ee0SLen Brown 317447936f94SArtem Bityutskiy void do_sleep(void) 3175b9ad8ee0SLen Brown { 3176c026c236SArtem Bityutskiy struct timeval tout; 3177c026c236SArtem Bityutskiy struct timespec rest; 3178b9ad8ee0SLen Brown fd_set readfds; 3179b9ad8ee0SLen Brown int retval; 3180b9ad8ee0SLen Brown 3181b9ad8ee0SLen Brown FD_ZERO(&readfds); 3182b9ad8ee0SLen Brown FD_SET(0, &readfds); 3183b9ad8ee0SLen Brown 3184c026c236SArtem Bityutskiy if (ignore_stdin) { 318547936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 318647936f94SArtem Bityutskiy return; 318747936f94SArtem Bityutskiy } 3188b9ad8ee0SLen Brown 3189c026c236SArtem Bityutskiy tout = interval_tv; 3190c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3191b9ad8ee0SLen Brown 3192b9ad8ee0SLen Brown if (retval == 1) { 3193b9ad8ee0SLen Brown switch (getc(stdin)) { 3194b9ad8ee0SLen Brown case 'q': 3195b9ad8ee0SLen Brown exit_requested = 1; 3196b9ad8ee0SLen Brown break; 3197c026c236SArtem Bityutskiy case EOF: 3198c026c236SArtem Bityutskiy /* 3199c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3200c026c236SArtem Bityutskiy * won't be any further input. 3201c026c236SArtem Bityutskiy */ 3202c026c236SArtem Bityutskiy ignore_stdin = 1; 3203c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3204c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3205c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3206c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3207b9ad8ee0SLen Brown } 3208b9ad8ee0SLen Brown } 3209b9ad8ee0SLen Brown } 321047936f94SArtem Bityutskiy 321187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 321287e15da9SChen Yu { 321387e15da9SChen Yu int ret, idx; 321487e15da9SChen Yu unsigned long long msr_cur, msr_last; 321587e15da9SChen Yu 321687e15da9SChen Yu if (!per_cpu_msr_sum) 321787e15da9SChen Yu return 1; 321887e15da9SChen Yu 321987e15da9SChen Yu idx = offset_to_idx(offset); 322087e15da9SChen Yu if (idx < 0) 322187e15da9SChen Yu return idx; 322287e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 322387e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 322487e15da9SChen Yu if (ret) 322587e15da9SChen Yu return ret; 322687e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 322787e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 322887e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 322987e15da9SChen Yu 323087e15da9SChen Yu return 0; 323187e15da9SChen Yu } 323287e15da9SChen Yu 323387e15da9SChen Yu timer_t timerid; 323487e15da9SChen Yu 323587e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 323687e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 323787e15da9SChen Yu { 323887e15da9SChen Yu int i, ret; 323987e15da9SChen Yu int cpu = t->cpu_id; 324087e15da9SChen Yu 324187e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 324287e15da9SChen Yu unsigned long long msr_cur, msr_last; 324387e15da9SChen Yu int offset; 324487e15da9SChen Yu 324587e15da9SChen Yu if (!idx_valid(i)) 324687e15da9SChen Yu continue; 324787e15da9SChen Yu offset = idx_to_offset(i); 324887e15da9SChen Yu if (offset < 0) 324987e15da9SChen Yu continue; 325087e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 325187e15da9SChen Yu if (ret) { 325287e15da9SChen Yu fprintf(outf, "Can not update msr(0x%x)\n", offset); 325387e15da9SChen Yu continue; 325487e15da9SChen Yu } 325587e15da9SChen Yu 325687e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 325787e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 325887e15da9SChen Yu 325987e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 326087e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 326187e15da9SChen Yu } 326287e15da9SChen Yu return 0; 326387e15da9SChen Yu } 326487e15da9SChen Yu 326587e15da9SChen Yu static void 326687e15da9SChen Yu msr_record_handler(union sigval v) 326787e15da9SChen Yu { 326887e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 326987e15da9SChen Yu } 327087e15da9SChen Yu 327187e15da9SChen Yu void msr_sum_record(void) 327287e15da9SChen Yu { 327387e15da9SChen Yu struct itimerspec its; 327487e15da9SChen Yu struct sigevent sev; 327587e15da9SChen Yu 327687e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 327787e15da9SChen Yu if (!per_cpu_msr_sum) { 327887e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 327987e15da9SChen Yu return; 328087e15da9SChen Yu } 328187e15da9SChen Yu /* 328287e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 328387e15da9SChen Yu */ 328487e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 328587e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 328687e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 328787e15da9SChen Yu 328887e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 328987e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 329087e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 329187e15da9SChen Yu goto release_msr; 329287e15da9SChen Yu } 329387e15da9SChen Yu 329487e15da9SChen Yu its.it_value.tv_sec = 0; 329587e15da9SChen Yu its.it_value.tv_nsec = 1; 329687e15da9SChen Yu /* 329787e15da9SChen Yu * A wraparound time has been calculated early. 329887e15da9SChen Yu * Some sources state that the peak power for a 329987e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 330087e15da9SChen Yu * use 2 * TDP for safety. 330187e15da9SChen Yu */ 330287e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 330387e15da9SChen Yu its.it_interval.tv_nsec = 0; 330487e15da9SChen Yu 330587e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 330687e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 330787e15da9SChen Yu goto release_timer; 330887e15da9SChen Yu } 330987e15da9SChen Yu return; 331087e15da9SChen Yu 331187e15da9SChen Yu release_timer: 331287e15da9SChen Yu timer_delete(timerid); 331387e15da9SChen Yu release_msr: 331487e15da9SChen Yu free(per_cpu_msr_sum); 331587e15da9SChen Yu } 33164c2122d4SLen Brown 3317103a8feaSLen Brown void turbostat_loop() 3318103a8feaSLen Brown { 3319c98d5d94SLen Brown int retval; 3320e52966c0SLen Brown int restarted = 0; 3321023fe0acSChen Yu int done_iters = 0; 3322c98d5d94SLen Brown 33238aa2ed0bSLen Brown setup_signal_handler(); 33248aa2ed0bSLen Brown 3325103a8feaSLen Brown restart: 3326e52966c0SLen Brown restarted++; 3327e52966c0SLen Brown 3328562a2d37SLen Brown snapshot_proc_sysfs_files(); 3329c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 33304c2122d4SLen Brown first_counter_read = 0; 3331d91bb17cSLen Brown if (retval < -1) { 3332d91bb17cSLen Brown exit(retval); 3333d91bb17cSLen Brown } else if (retval == -1) { 3334e52966c0SLen Brown if (restarted > 1) { 3335e52966c0SLen Brown exit(retval); 3336e52966c0SLen Brown } 3337c98d5d94SLen Brown re_initialize(); 3338c98d5d94SLen Brown goto restart; 3339c98d5d94SLen Brown } 3340e52966c0SLen Brown restarted = 0; 3341023fe0acSChen Yu done_iters = 0; 3342103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3343103a8feaSLen Brown 3344103a8feaSLen Brown while (1) { 3345c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3346103a8feaSLen Brown re_initialize(); 3347103a8feaSLen Brown goto restart; 3348103a8feaSLen Brown } 3349b9ad8ee0SLen Brown do_sleep(); 3350562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3351562a2d37SLen Brown goto restart; 3352c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3353d91bb17cSLen Brown if (retval < -1) { 3354d91bb17cSLen Brown exit(retval); 3355d91bb17cSLen Brown } else if (retval == -1) { 335615aaa346SLen Brown re_initialize(); 335715aaa346SLen Brown goto restart; 335815aaa346SLen Brown } 3359103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3360103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3361ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3362ba3dec99SLen Brown re_initialize(); 3363ba3dec99SLen Brown goto restart; 3364ba3dec99SLen Brown } 3365c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3366c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3367b7d8c148SLen Brown flush_output_stdout(); 33688aa2ed0bSLen Brown if (exit_requested) 33698aa2ed0bSLen Brown break; 3370023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3371023fe0acSChen Yu break; 3372b9ad8ee0SLen Brown do_sleep(); 3373562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3374562a2d37SLen Brown goto restart; 3375c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3376d91bb17cSLen Brown if (retval < -1) { 3377d91bb17cSLen Brown exit(retval); 3378d91bb17cSLen Brown } else if (retval == -1) { 3379103a8feaSLen Brown re_initialize(); 3380103a8feaSLen Brown goto restart; 3381103a8feaSLen Brown } 3382103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3383103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3384ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3385ba3dec99SLen Brown re_initialize(); 3386ba3dec99SLen Brown goto restart; 3387ba3dec99SLen Brown } 3388c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3389c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3390b7d8c148SLen Brown flush_output_stdout(); 33918aa2ed0bSLen Brown if (exit_requested) 33928aa2ed0bSLen Brown break; 3393023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3394023fe0acSChen Yu break; 3395103a8feaSLen Brown } 3396103a8feaSLen Brown } 3397103a8feaSLen Brown 3398103a8feaSLen Brown void check_dev_msr() 3399103a8feaSLen Brown { 3400103a8feaSLen Brown struct stat sb; 34017ce7d5deSPrarit Bhargava char pathname[32]; 3402103a8feaSLen Brown 34037ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 34047ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3405a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3406d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3407103a8feaSLen Brown } 3408103a8feaSLen Brown 3409fcaa681cSLen Brown /* 3410fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3411fcaa681cSLen Brown * return 0 on success 3412fcaa681cSLen Brown * return 1 on fail 3413fcaa681cSLen Brown */ 3414fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3415103a8feaSLen Brown { 3416fcaa681cSLen Brown cap_t caps; 3417fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3418fcaa681cSLen Brown 3419fcaa681cSLen Brown caps = cap_get_proc(); 3420fcaa681cSLen Brown if (caps == NULL) 3421fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3422fcaa681cSLen Brown 3423fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3424fcaa681cSLen Brown err(-6, "cap_get\n"); 3425fcaa681cSLen Brown 3426fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 3427fcaa681cSLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 3428fcaa681cSLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3429fcaa681cSLen Brown return 1; 3430fcaa681cSLen Brown } 3431fcaa681cSLen Brown 3432fcaa681cSLen Brown if (cap_free(caps) == -1) 3433fcaa681cSLen Brown err(-6, "cap_free\n"); 3434fcaa681cSLen Brown 3435fcaa681cSLen Brown return 0; 3436fcaa681cSLen Brown } 3437fcaa681cSLen Brown void check_permissions(void) 3438fcaa681cSLen Brown { 343998481e79SLen Brown int do_exit = 0; 34407ce7d5deSPrarit Bhargava char pathname[32]; 344198481e79SLen Brown 344298481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3443fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 344498481e79SLen Brown 344598481e79SLen Brown /* test file permissions */ 34467ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 34477ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 344898481e79SLen Brown do_exit++; 344998481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 345098481e79SLen Brown } 345198481e79SLen Brown 345298481e79SLen Brown /* if all else fails, thell them to be root */ 345398481e79SLen Brown if (do_exit) 3454b2c95d90SJosh Triplett if (getuid() != 0) 3455d7899447SLen Brown warnx("... or simply run as root"); 345698481e79SLen Brown 345798481e79SLen Brown if (do_exit) 345898481e79SLen Brown exit(-6); 3459103a8feaSLen Brown } 3460103a8feaSLen Brown 3461d7899447SLen Brown /* 3462d7899447SLen Brown * NHM adds support for additional MSRs: 3463d7899447SLen Brown * 3464d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3465d7899447SLen Brown * 3466ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 34671df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3468d7899447SLen Brown * 3469cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3470cf4cbe53SLen Brown * 3471d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3472d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3473d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3474d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3475d7899447SLen Brown * 3476ee7e38e3SLen Brown * Side effect: 34771df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 347833148d67SLen Brown * sets has_misc_feature_control 3479d7899447SLen Brown */ 3480ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3481103a8feaSLen Brown { 3482ee7e38e3SLen Brown unsigned long long msr; 348321ed5574SLen Brown unsigned int base_ratio; 3484ee7e38e3SLen Brown int *pkg_cstate_limits; 3485ee7e38e3SLen Brown 3486103a8feaSLen Brown if (!genuine_intel) 3487103a8feaSLen Brown return 0; 3488103a8feaSLen Brown 3489103a8feaSLen Brown if (family != 6) 3490103a8feaSLen Brown return 0; 3491103a8feaSLen Brown 349221ed5574SLen Brown bclk = discover_bclk(family, model); 349321ed5574SLen Brown 3494103a8feaSLen Brown switch (model) { 3495869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3496869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3497ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3498ee7e38e3SLen Brown break; 3499869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3500869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3501869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3502869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3503ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 350433148d67SLen Brown has_misc_feature_control = 1; 3505ee7e38e3SLen Brown break; 3506c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 35075e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3508869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 350977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3510c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 35115e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3512869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3513af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3514af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3515ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 351633148d67SLen Brown has_misc_feature_control = 1; 3517ee7e38e3SLen Brown break; 3518d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3519d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 352033148d67SLen Brown has_misc_feature_control = 1; 3521d8ebb442SLen Brown break; 3522f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3523cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 35245ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3525ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3526ee7e38e3SLen Brown break; 3527869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3528ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3529cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3530ee7e38e3SLen Brown break; 3531869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3532ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3533ee7e38e3SLen Brown break; 3534869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3535f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 35365ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3537f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 353820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3539445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3540e4085d54SLen Brown break; 3541103a8feaSLen Brown default: 3542103a8feaSLen Brown return 0; 3543103a8feaSLen Brown } 35441df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3545e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3546ee7e38e3SLen Brown 3547ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 354821ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 354921ed5574SLen Brown 355021ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 355121ed5574SLen Brown has_base_hz = 1; 3552ee7e38e3SLen Brown return 1; 3553103a8feaSLen Brown } 35540f7887c4SLen Brown /* 3555495c7654SLen Brown * SLV client has support for unique MSRs: 35560f7887c4SLen Brown * 35570f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 35580f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 35590f7887c4SLen Brown */ 35600f7887c4SLen Brown 35610f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 35620f7887c4SLen Brown { 35630f7887c4SLen Brown if (!genuine_intel) 35640f7887c4SLen Brown return 0; 35650f7887c4SLen Brown 35660f7887c4SLen Brown switch (model) { 3567f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3568f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3569f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 35700f7887c4SLen Brown return 1; 35710f7887c4SLen Brown } 35720f7887c4SLen Brown return 0; 35730f7887c4SLen Brown } 35747170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 35757170a374SLen Brown { 35767170a374SLen Brown 35777170a374SLen Brown if (!genuine_intel) 35787170a374SLen Brown return 0; 35797170a374SLen Brown 35807170a374SLen Brown switch (model) { 35815ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 35827170a374SLen Brown return 1; 35837170a374SLen Brown } 35847170a374SLen Brown return 0; 35857170a374SLen Brown } 3586ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3587ade0ebacSLen Brown { 3588ade0ebacSLen Brown 3589ade0ebacSLen Brown if (!genuine_intel) 3590ade0ebacSLen Brown return 0; 3591ade0ebacSLen Brown 3592ade0ebacSLen Brown switch (model) { 3593ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3594ade0ebacSLen Brown return 1; 3595ade0ebacSLen Brown } 3596ade0ebacSLen Brown return 0; 3597ade0ebacSLen Brown } 359834c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 359934c76197SLen Brown { 360034c76197SLen Brown 360134c76197SLen Brown if (!genuine_intel) 360234c76197SLen Brown return 0; 360334c76197SLen Brown 360434c76197SLen Brown switch (model) { 360534c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 360634c76197SLen Brown return 1; 360734c76197SLen Brown } 360834c76197SLen Brown return 0; 360934c76197SLen Brown } 3610f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3611f6708400SChen Yu { 3612f6708400SChen Yu if (!genuine_intel) 3613f6708400SChen Yu return 0; 3614f6708400SChen Yu 3615f6708400SChen Yu switch (model) { 3616f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3617f6708400SChen Yu return 1; 3618f6708400SChen Yu } 3619f6708400SChen Yu return 0; 3620f6708400SChen Yu } 362120de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 362220de0dabSAntti Laakso { 362320de0dabSAntti Laakso if (!genuine_intel) 362420de0dabSAntti Laakso return 0; 362520de0dabSAntti Laakso 362620de0dabSAntti Laakso switch (model) { 362720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 362820de0dabSAntti Laakso return 1; 362920de0dabSAntti Laakso } 363020de0dabSAntti Laakso return 0; 363120de0dabSAntti Laakso } 36320f7887c4SLen Brown 363331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3634d7899447SLen Brown { 36350f7887c4SLen Brown if (has_slv_msrs(family, model)) 36360f7887c4SLen Brown return 0; 36370f7887c4SLen Brown 3638d7899447SLen Brown switch (model) { 3639d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3640869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3641869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3642d7899447SLen Brown return 0; 3643d7899447SLen Brown default: 3644d7899447SLen Brown return 1; 3645d7899447SLen Brown } 3646d7899447SLen Brown } 36470f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 36480f7887c4SLen Brown { 36490f7887c4SLen Brown if (has_slv_msrs(family, model)) 36500f7887c4SLen Brown return 1; 36510f7887c4SLen Brown 36520f7887c4SLen Brown return 0; 36530f7887c4SLen Brown } 36546574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 36556574a5d5SLen Brown { 36566574a5d5SLen Brown if (!genuine_intel) 36576574a5d5SLen Brown return 0; 36586574a5d5SLen Brown 36596574a5d5SLen Brown if (family != 6) 36606574a5d5SLen Brown return 0; 36616574a5d5SLen Brown 36626574a5d5SLen Brown switch (model) { 3663869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3664869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 36656574a5d5SLen Brown return 1; 36666574a5d5SLen Brown default: 36676574a5d5SLen Brown return 0; 36686574a5d5SLen Brown } 36696574a5d5SLen Brown } 3670fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3671fcd17211SLen Brown { 3672fcd17211SLen Brown if (!genuine_intel) 3673fcd17211SLen Brown return 0; 3674fcd17211SLen Brown 3675fcd17211SLen Brown if (family != 6) 3676fcd17211SLen Brown return 0; 3677fcd17211SLen Brown 3678fcd17211SLen Brown switch (model) { 3679869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3680fcd17211SLen Brown return 1; 3681fcd17211SLen Brown default: 3682fcd17211SLen Brown return 0; 3683fcd17211SLen Brown } 3684fcd17211SLen Brown } 3685fcd17211SLen Brown 3686fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3687fb5d4327SDasaratharaman Chandramouli { 3688fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3689fb5d4327SDasaratharaman Chandramouli return 0; 3690fb5d4327SDasaratharaman Chandramouli 3691fb5d4327SDasaratharaman Chandramouli if (family != 6) 3692fb5d4327SDasaratharaman Chandramouli return 0; 3693fb5d4327SDasaratharaman Chandramouli 3694fb5d4327SDasaratharaman Chandramouli switch (model) { 3695869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3696fb5d4327SDasaratharaman Chandramouli return 1; 3697fb5d4327SDasaratharaman Chandramouli default: 3698fb5d4327SDasaratharaman Chandramouli return 0; 3699fb5d4327SDasaratharaman Chandramouli } 3700fb5d4327SDasaratharaman Chandramouli } 370131e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 370231e07522SLen Brown { 370331e07522SLen Brown if (!genuine_intel) 370431e07522SLen Brown return 0; 370531e07522SLen Brown 370631e07522SLen Brown if (family != 6) 370731e07522SLen Brown return 0; 370831e07522SLen Brown 370931e07522SLen Brown switch (model) { 371031e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 371131e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 371231e07522SLen Brown return 1; 371331e07522SLen Brown default: 371431e07522SLen Brown return 0; 371531e07522SLen Brown } 371631e07522SLen Brown } 37176fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 37186fb3143bSLen Brown { 37196fb3143bSLen Brown if (!genuine_intel) 37206fb3143bSLen Brown return 0; 37216fb3143bSLen Brown 37226fb3143bSLen Brown if (family != 6) 37236fb3143bSLen Brown return 0; 37246fb3143bSLen Brown 37256fb3143bSLen Brown switch (model) { 3726869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3727c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3728869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 372977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 37305e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3731c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 37325e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3733869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3734af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3735af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3736869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 37376fb3143bSLen Brown 3738869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 37396fb3143bSLen Brown return 1; 37406fb3143bSLen Brown default: 37416fb3143bSLen Brown return 0; 37426fb3143bSLen Brown } 37436fb3143bSLen Brown } 37446fb3143bSLen Brown 3745fcd17211SLen Brown static void 3746fecb3bc8SDavid Arcari remove_underbar(char *s) 3747fecb3bc8SDavid Arcari { 3748fecb3bc8SDavid Arcari char *to = s; 3749fecb3bc8SDavid Arcari 3750fecb3bc8SDavid Arcari while (*s) { 3751fecb3bc8SDavid Arcari if (*s != '_') 3752fecb3bc8SDavid Arcari *to++ = *s; 3753fecb3bc8SDavid Arcari s++; 3754fecb3bc8SDavid Arcari } 3755fecb3bc8SDavid Arcari 3756fecb3bc8SDavid Arcari *to = 0; 3757fecb3bc8SDavid Arcari } 3758fecb3bc8SDavid Arcari 3759fecb3bc8SDavid Arcari static void 37601b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3761fcd17211SLen Brown { 3762fcd17211SLen Brown if (!do_nhm_platform_info) 3763fcd17211SLen Brown return; 3764fcd17211SLen Brown 3765fcd17211SLen Brown dump_nhm_platform_info(); 3766fcd17211SLen Brown 3767fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3768fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3769fcd17211SLen Brown 3770fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3771fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3772fcd17211SLen Brown 377331e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 377431e07522SLen Brown dump_turbo_ratio_limits(family, model); 3775fcd17211SLen Brown 37760f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 37770f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 37780f7887c4SLen Brown 3779fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3780fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3781fb5d4327SDasaratharaman Chandramouli 37826fb3143bSLen Brown if (has_config_tdp(family, model)) 37836fb3143bSLen Brown dump_config_tdp(); 37846fb3143bSLen Brown 3785fcd17211SLen Brown dump_nhm_cst_cfg(); 3786fcd17211SLen Brown } 3787fcd17211SLen Brown 3788abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3789abdcbdb2SLen Brown { 3790abdcbdb2SLen Brown FILE *input; 3791abdcbdb2SLen Brown char cpuidle_buf[64]; 3792abdcbdb2SLen Brown 3793abdcbdb2SLen Brown input = fopen(path, "r"); 3794abdcbdb2SLen Brown if (input == NULL) { 3795abdcbdb2SLen Brown if (debug) 3796abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3797abdcbdb2SLen Brown return; 3798abdcbdb2SLen Brown } 3799abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3800abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3801abdcbdb2SLen Brown fclose(input); 3802abdcbdb2SLen Brown 3803abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 3804abdcbdb2SLen Brown } 380541618e63SLen Brown static void 380641618e63SLen Brown dump_sysfs_cstate_config(void) 380741618e63SLen Brown { 380841618e63SLen Brown char path[64]; 380941618e63SLen Brown char name_buf[16]; 381041618e63SLen Brown char desc[64]; 381141618e63SLen Brown FILE *input; 381241618e63SLen Brown int state; 381341618e63SLen Brown char *sp; 381441618e63SLen Brown 3815abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 3816abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 3817abdcbdb2SLen Brown return; 3818abdcbdb2SLen Brown } 3819abdcbdb2SLen Brown 3820abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 3821abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 3822abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 3823abdcbdb2SLen Brown 382441618e63SLen Brown for (state = 0; state < 10; ++state) { 382541618e63SLen Brown 382641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 382741618e63SLen Brown base_cpu, state); 382841618e63SLen Brown input = fopen(path, "r"); 382941618e63SLen Brown if (input == NULL) 383041618e63SLen Brown continue; 38318173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 38328173c336SBen Hutchings err(1, "%s: failed to read file", path); 383341618e63SLen Brown 383441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 383541618e63SLen Brown sp = strchr(name_buf, '-'); 383641618e63SLen Brown if (!sp) 383741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 383841618e63SLen Brown *sp = '\0'; 383941618e63SLen Brown fclose(input); 384041618e63SLen Brown 3841fecb3bc8SDavid Arcari remove_underbar(name_buf); 3842fecb3bc8SDavid Arcari 384341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 384441618e63SLen Brown base_cpu, state); 384541618e63SLen Brown input = fopen(path, "r"); 384641618e63SLen Brown if (input == NULL) 384741618e63SLen Brown continue; 38488173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 38498173c336SBen Hutchings err(1, "%s: failed to read file", path); 385041618e63SLen Brown 385141618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 385241618e63SLen Brown fclose(input); 385341618e63SLen Brown } 385441618e63SLen Brown } 38557293fccdSLen Brown static void 38567293fccdSLen Brown dump_sysfs_pstate_config(void) 38577293fccdSLen Brown { 38587293fccdSLen Brown char path[64]; 38597293fccdSLen Brown char driver_buf[64]; 38607293fccdSLen Brown char governor_buf[64]; 38617293fccdSLen Brown FILE *input; 38627293fccdSLen Brown int turbo; 38637293fccdSLen Brown 38647293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 38657293fccdSLen Brown base_cpu); 38667293fccdSLen Brown input = fopen(path, "r"); 38677293fccdSLen Brown if (input == NULL) { 38680a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 38697293fccdSLen Brown return; 38707293fccdSLen Brown } 38718173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 38728173c336SBen Hutchings err(1, "%s: failed to read file", path); 38737293fccdSLen Brown fclose(input); 38747293fccdSLen Brown 38757293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 38767293fccdSLen Brown base_cpu); 38777293fccdSLen Brown input = fopen(path, "r"); 38787293fccdSLen Brown if (input == NULL) { 38790a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 38807293fccdSLen Brown return; 38817293fccdSLen Brown } 38828173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 38838173c336SBen Hutchings err(1, "%s: failed to read file", path); 38847293fccdSLen Brown fclose(input); 38857293fccdSLen Brown 38867293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 38877293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 38887293fccdSLen Brown 38897293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 38907293fccdSLen Brown input = fopen(path, "r"); 38917293fccdSLen Brown if (input != NULL) { 38928173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 38938173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 38947293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 38957293fccdSLen Brown fclose(input); 38967293fccdSLen Brown } 38977293fccdSLen Brown 38987293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 38997293fccdSLen Brown input = fopen(path, "r"); 39007293fccdSLen Brown if (input != NULL) { 39018173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 39028173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 39037293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 39047293fccdSLen Brown fclose(input); 39057293fccdSLen Brown } 39067293fccdSLen Brown } 390741618e63SLen Brown 39086574a5d5SLen Brown 3909889facbeSLen Brown /* 3910889facbeSLen Brown * print_epb() 3911889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3912889facbeSLen Brown */ 3913889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3914889facbeSLen Brown { 3915889facbeSLen Brown unsigned long long msr; 3916889facbeSLen Brown char *epb_string; 3917889facbeSLen Brown int cpu; 3918889facbeSLen Brown 3919889facbeSLen Brown if (!has_epb) 3920889facbeSLen Brown return 0; 3921889facbeSLen Brown 3922889facbeSLen Brown cpu = t->cpu_id; 3923889facbeSLen Brown 3924889facbeSLen Brown /* EPB is per-package */ 3925889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3926889facbeSLen Brown return 0; 3927889facbeSLen Brown 3928889facbeSLen Brown if (cpu_migrate(cpu)) { 3929b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3930889facbeSLen Brown return -1; 3931889facbeSLen Brown } 3932889facbeSLen Brown 3933889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3934889facbeSLen Brown return 0; 3935889facbeSLen Brown 3936e9be7dd6SLen Brown switch (msr & 0xF) { 3937889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3938889facbeSLen Brown epb_string = "performance"; 3939889facbeSLen Brown break; 3940889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3941889facbeSLen Brown epb_string = "balanced"; 3942889facbeSLen Brown break; 3943889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3944889facbeSLen Brown epb_string = "powersave"; 3945889facbeSLen Brown break; 3946889facbeSLen Brown default: 3947889facbeSLen Brown epb_string = "custom"; 3948889facbeSLen Brown break; 3949889facbeSLen Brown } 3950b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3951889facbeSLen Brown 3952889facbeSLen Brown return 0; 3953889facbeSLen Brown } 39547f5c258eSLen Brown /* 39557f5c258eSLen Brown * print_hwp() 39567f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 39577f5c258eSLen Brown */ 39587f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 39597f5c258eSLen Brown { 39607f5c258eSLen Brown unsigned long long msr; 39617f5c258eSLen Brown int cpu; 39627f5c258eSLen Brown 39637f5c258eSLen Brown if (!has_hwp) 39647f5c258eSLen Brown return 0; 39657f5c258eSLen Brown 39667f5c258eSLen Brown cpu = t->cpu_id; 39677f5c258eSLen Brown 39687f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 39697f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 39707f5c258eSLen Brown return 0; 39717f5c258eSLen Brown 39727f5c258eSLen Brown if (cpu_migrate(cpu)) { 3973b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 39747f5c258eSLen Brown return -1; 39757f5c258eSLen Brown } 39767f5c258eSLen Brown 39777f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 39787f5c258eSLen Brown return 0; 39797f5c258eSLen Brown 3980b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 39817f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 39827f5c258eSLen Brown 39837f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 39847f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 39857f5c258eSLen Brown return 0; 39867f5c258eSLen Brown 39877f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 39887f5c258eSLen Brown return 0; 39897f5c258eSLen Brown 3990b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 39916dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 39927f5c258eSLen Brown cpu, msr, 39937f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 39947f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 39957f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 39967f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 39977f5c258eSLen Brown 39987f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 39997f5c258eSLen Brown return 0; 40007f5c258eSLen Brown 4001b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 40026dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 40037f5c258eSLen Brown cpu, msr, 40047f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 40057f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 40067f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 40077f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 40087f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 40097f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 40107f5c258eSLen Brown 40117f5c258eSLen Brown if (has_hwp_pkg) { 40127f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 40137f5c258eSLen Brown return 0; 40147f5c258eSLen Brown 4015b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 40166dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 40177f5c258eSLen Brown cpu, msr, 40187f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 40197f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 40207f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 40217f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 40227f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 40237f5c258eSLen Brown } 40247f5c258eSLen Brown if (has_hwp_notify) { 40257f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 40267f5c258eSLen Brown return 0; 40277f5c258eSLen Brown 4028b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 40297f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 40307f5c258eSLen Brown cpu, msr, 40317f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 40327f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 40337f5c258eSLen Brown } 40347f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 40357f5c258eSLen Brown return 0; 40367f5c258eSLen Brown 4037b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 40387f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 40397f5c258eSLen Brown cpu, msr, 40407f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 40417f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 4042889facbeSLen Brown 4043889facbeSLen Brown return 0; 4044889facbeSLen Brown } 4045889facbeSLen Brown 40463a9a941dSLen Brown /* 40473a9a941dSLen Brown * print_perf_limit() 40483a9a941dSLen Brown */ 40493a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 40503a9a941dSLen Brown { 40513a9a941dSLen Brown unsigned long long msr; 40523a9a941dSLen Brown int cpu; 40533a9a941dSLen Brown 40543a9a941dSLen Brown cpu = t->cpu_id; 40553a9a941dSLen Brown 40563a9a941dSLen Brown /* per-package */ 40573a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 40583a9a941dSLen Brown return 0; 40593a9a941dSLen Brown 40603a9a941dSLen Brown if (cpu_migrate(cpu)) { 4061b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 40623a9a941dSLen Brown return -1; 40633a9a941dSLen Brown } 40643a9a941dSLen Brown 40653a9a941dSLen Brown if (do_core_perf_limit_reasons) { 40663a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4067b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4068b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4069e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 40703a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4071e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4072e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4073e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4074e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4075e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4076e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4077e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4078e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4079e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4080e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 4081e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 4082e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 4083b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4084e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 40853a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4086e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4087e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4088e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4089e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4090e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4091e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4092e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4093e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4094e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4095e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 4096e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 4097e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 40983a9a941dSLen Brown 40993a9a941dSLen Brown } 41003a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 41013a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4102b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4103b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 41043a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 41053a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 41063a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 41073a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 41083a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 41093a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 41103a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 41113a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4112b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 41133a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 41143a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 41153a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 41163a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 41173a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 41183a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 41193a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 41203a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 41213a9a941dSLen Brown } 41223a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 41233a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4124b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4125b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 41263a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 41273a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 41283a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 41293a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 41303a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 41313a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4132b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 41333a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 41343a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 41353a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 41363a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 41373a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 41383a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 41393a9a941dSLen Brown } 41403a9a941dSLen Brown return 0; 41413a9a941dSLen Brown } 41423a9a941dSLen Brown 4143889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4144889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4145889facbeSLen Brown 41469392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4147144b44b1SLen Brown { 4148144b44b1SLen Brown unsigned long long msr; 4149144b44b1SLen Brown 4150144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 41517ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4152144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4153144b44b1SLen Brown 4154144b44b1SLen Brown switch (model) { 4155f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 41565ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4157144b44b1SLen Brown return 30.0; 4158144b44b1SLen Brown default: 4159144b44b1SLen Brown return 135.0; 4160144b44b1SLen Brown } 4161144b44b1SLen Brown } 4162144b44b1SLen Brown 41639392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 41649392bd98SCalvin Walton { 4165*33eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 4166*33eb8225SKim Phillips return 280.0; 41679392bd98SCalvin Walton } 41689392bd98SCalvin Walton 416940ee8e3bSAndrey Semin /* 417040ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 417140ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 417240ee8e3bSAndrey Semin */ 417340ee8e3bSAndrey Semin static double 417440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 417540ee8e3bSAndrey Semin { 417640ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 417740ee8e3bSAndrey Semin 417840ee8e3bSAndrey Semin switch (model) { 4179869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4180869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4181869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 418240ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 418340ee8e3bSAndrey Semin default: 418440ee8e3bSAndrey Semin return (rapl_energy_units); 418540ee8e3bSAndrey Semin } 418640ee8e3bSAndrey Semin } 418740ee8e3bSAndrey Semin 41889392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4189889facbeSLen Brown { 4190889facbeSLen Brown unsigned long long msr; 4191144b44b1SLen Brown unsigned int time_unit; 4192889facbeSLen Brown double tdp; 4193889facbeSLen Brown 4194889facbeSLen Brown if (family != 6) 4195889facbeSLen Brown return; 4196889facbeSLen Brown 4197889facbeSLen Brown switch (model) { 4198869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4199869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4200c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 420177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 42025e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4203c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 42045e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4205144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4206812db3f7SLen Brown if (rapl_joules) { 4207812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4208812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4209812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4210812db3f7SLen Brown } else { 4211812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4212812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4213812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4214812db3f7SLen Brown } 4215889facbeSLen Brown break; 4216869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4217f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4218e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4219812db3f7SLen Brown if (rapl_joules) 4220812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4221812db3f7SLen Brown else 4222812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4223e4085d54SLen Brown break; 4224f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4225f6708400SChen 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; 4226f6708400SChen Yu if (rapl_joules) { 4227f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4228f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4229f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4230f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4231f6708400SChen Yu } else { 4232f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4233f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4234f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4235f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4236f6708400SChen Yu } 4237f6708400SChen Yu break; 423820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 423920de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 424020de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 424120de0dabSAntti Laakso if (rapl_joules) 424220de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 424320de0dabSAntti Laakso else 424420de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 424520de0dabSAntti Laakso break; 4246af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4247af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 424881824921SLen 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; 4249812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4250812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4251812db3f7SLen Brown if (rapl_joules) { 4252812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4253812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4254812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 425581824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4256812db3f7SLen Brown } else { 4257812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4258812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4259812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 426081824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4261812db3f7SLen Brown } 42620b2bb692SLen Brown break; 4263869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4264869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4265869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4266869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 42670b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 4268812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4269812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4270812db3f7SLen Brown if (rapl_joules) { 4271812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4272812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4273812db3f7SLen Brown } else { 4274812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4275812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4276812db3f7SLen Brown } 4277e6f9bb3cSLen Brown break; 4278869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4279869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 42800b2bb692SLen 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; 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); 4287812db3f7SLen Brown } else { 4288812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4289812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4290812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4291812db3f7SLen Brown } 4292144b44b1SLen Brown break; 4293f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 42945ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4295144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4296812db3f7SLen Brown if (rapl_joules) { 4297812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4298812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4299812db3f7SLen Brown } else { 4300812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4301812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4302812db3f7SLen Brown } 4303889facbeSLen Brown break; 43045ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 43050f644909SJacob 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; 4306812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4307812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4308812db3f7SLen Brown if (rapl_joules) { 4309812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4310812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4311812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4312812db3f7SLen Brown } else { 4313812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4314812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4315812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4316812db3f7SLen Brown } 43170f644909SJacob Pan break; 4318889facbeSLen Brown default: 4319889facbeSLen Brown return; 4320889facbeSLen Brown } 4321889facbeSLen Brown 4322889facbeSLen Brown /* units on package 0, verify later other packages match */ 43237ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4324889facbeSLen Brown return; 4325889facbeSLen Brown 4326889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4327f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4328144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4329144b44b1SLen Brown else 4330889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4331889facbeSLen Brown 433240ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 433340ee8e3bSAndrey Semin 4334144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4335144b44b1SLen Brown if (time_unit == 0) 4336144b44b1SLen Brown time_unit = 0xA; 4337889facbeSLen Brown 4338144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4339144b44b1SLen Brown 43409392bd98SCalvin Walton tdp = get_tdp_intel(model); 4341889facbeSLen Brown 4342889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 434396e47158SLen Brown if (!quiet) 4344b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 43459392bd98SCalvin Walton } 4346889facbeSLen Brown 43479392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 43489392bd98SCalvin Walton { 43499392bd98SCalvin Walton unsigned long long msr; 43509392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 43519392bd98SCalvin Walton unsigned int has_rapl = 0; 43529392bd98SCalvin Walton double tdp; 43539392bd98SCalvin Walton 43549392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 43559392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 4356*33eb8225SKim Phillips /* RAPL (Fam 17h+) */ 43579392bd98SCalvin Walton has_rapl = edx & (1 << 14); 43589392bd98SCalvin Walton } 43599392bd98SCalvin Walton 4360*33eb8225SKim Phillips if (!has_rapl || family < 0x17) 4361889facbeSLen Brown return; 43629392bd98SCalvin Walton 43639392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 43643316f99aSCalvin Walton if (rapl_joules) { 43653316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 43669392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 43673316f99aSCalvin Walton } else { 43683316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 43699392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 43703316f99aSCalvin Walton } 43719392bd98SCalvin Walton 43729392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 43739392bd98SCalvin Walton return; 43749392bd98SCalvin Walton 43759392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 43769392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 43779392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 43789392bd98SCalvin Walton 43799cfa8e04SPu Wen tdp = get_tdp_amd(family); 43809392bd98SCalvin Walton 43819392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 43829392bd98SCalvin Walton if (!quiet) 43839392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 43849392bd98SCalvin Walton } 43859392bd98SCalvin Walton 43869392bd98SCalvin Walton /* 43879392bd98SCalvin Walton * rapl_probe() 43889392bd98SCalvin Walton * 43899392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 43909392bd98SCalvin Walton */ 43919392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 43929392bd98SCalvin Walton { 43939392bd98SCalvin Walton if (genuine_intel) 43949392bd98SCalvin Walton rapl_probe_intel(family, model); 4395c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 43969392bd98SCalvin Walton rapl_probe_amd(family, model); 4397889facbeSLen Brown } 4398889facbeSLen Brown 43991b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 44003a9a941dSLen Brown { 44013a9a941dSLen Brown if (!genuine_intel) 44023a9a941dSLen Brown return; 44033a9a941dSLen Brown 44043a9a941dSLen Brown if (family != 6) 44053a9a941dSLen Brown return; 44063a9a941dSLen Brown 44073a9a941dSLen Brown switch (model) { 4408c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 440977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 44105e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 44113a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4412869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 44133a9a941dSLen Brown do_core_perf_limit_reasons = 1; 44143a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 44153a9a941dSLen Brown default: 44163a9a941dSLen Brown return; 44173a9a941dSLen Brown } 44183a9a941dSLen Brown } 44193a9a941dSLen Brown 4420ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4421ac980e13SArtem Bityutskiy { 4422ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 4423ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4424ac980e13SArtem Bityutskiy } 4425ac980e13SArtem Bityutskiy 4426889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4427889facbeSLen Brown { 4428889facbeSLen Brown unsigned long long msr; 4429f4896fa5SLen Brown unsigned int dts, dts2; 4430889facbeSLen Brown int cpu; 4431889facbeSLen Brown 4432889facbeSLen Brown if (!(do_dts || do_ptm)) 4433889facbeSLen Brown return 0; 4434889facbeSLen Brown 4435889facbeSLen Brown cpu = t->cpu_id; 4436889facbeSLen Brown 4437889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4438889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4439889facbeSLen Brown return 0; 4440889facbeSLen Brown 4441889facbeSLen Brown if (cpu_migrate(cpu)) { 4442b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4443889facbeSLen Brown return -1; 4444889facbeSLen Brown } 4445889facbeSLen Brown 4446889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4447889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4448889facbeSLen Brown return 0; 4449889facbeSLen Brown 4450889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4451b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4452889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4453889facbeSLen Brown 4454889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4455889facbeSLen Brown return 0; 4456889facbeSLen Brown 4457889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4458889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4459b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4460889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4461889facbeSLen Brown } 4462889facbeSLen Brown 4463889facbeSLen Brown 4464f4896fa5SLen Brown if (do_dts && debug) { 4465889facbeSLen Brown unsigned int resolution; 4466889facbeSLen Brown 4467889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4468889facbeSLen Brown return 0; 4469889facbeSLen Brown 4470889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4471889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4472b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4473889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4474889facbeSLen Brown 4475889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4476889facbeSLen Brown return 0; 4477889facbeSLen Brown 4478889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4479889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4480b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4481889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4482889facbeSLen Brown } 4483889facbeSLen Brown 4484889facbeSLen Brown return 0; 4485889facbeSLen Brown } 4486889facbeSLen Brown 4487889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4488889facbeSLen Brown { 4489b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4490889facbeSLen Brown cpu, label, 4491889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4492889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4493889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4494889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4495889facbeSLen Brown 4496889facbeSLen Brown return; 4497889facbeSLen Brown } 4498889facbeSLen Brown 4499889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4500889facbeSLen Brown { 4501889facbeSLen Brown unsigned long long msr; 45029392bd98SCalvin Walton const char *msr_name; 4503889facbeSLen Brown int cpu; 4504889facbeSLen Brown 4505889facbeSLen Brown if (!do_rapl) 4506889facbeSLen Brown return 0; 4507889facbeSLen Brown 4508889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4509889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4510889facbeSLen Brown return 0; 4511889facbeSLen Brown 4512889facbeSLen Brown cpu = t->cpu_id; 4513889facbeSLen Brown if (cpu_migrate(cpu)) { 4514b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4515889facbeSLen Brown return -1; 4516889facbeSLen Brown } 4517889facbeSLen Brown 45189392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 45199392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 45209392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 45219392bd98SCalvin Walton return -1; 45229392bd98SCalvin Walton } else { 45239392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4524889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4525889facbeSLen Brown return -1; 45269392bd98SCalvin Walton } 4527889facbeSLen Brown 45289392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4529144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 453096e47158SLen Brown 4531144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4532144b44b1SLen Brown 4533889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4534889facbeSLen Brown return -5; 4535889facbeSLen Brown 4536889facbeSLen Brown 4537b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4538889facbeSLen Brown cpu, msr, 4539889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4540889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4541889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4542889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4543889facbeSLen Brown 4544144b44b1SLen Brown } 4545144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4546144b44b1SLen Brown 4547889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4548889facbeSLen Brown return -9; 4549889facbeSLen Brown 4550b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4551889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4552889facbeSLen Brown 4553889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4554b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4555889facbeSLen Brown cpu, 4556889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4557889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4558889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4559889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4560889facbeSLen Brown } 4561889facbeSLen Brown 45620b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4563889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4564889facbeSLen Brown return -6; 4565889facbeSLen Brown 4566b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4567889facbeSLen Brown cpu, msr, 4568889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4569889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4570889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4571889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 45720b2bb692SLen Brown } 45730b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4574889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4575889facbeSLen Brown return -9; 4576b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4577889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4578889facbeSLen Brown 4579889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4580889facbeSLen Brown } 4581144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4582889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4583889facbeSLen Brown return -7; 4584889facbeSLen Brown 4585b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4586144b44b1SLen Brown } 45879148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4588889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4589889facbeSLen Brown return -9; 4590b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4591889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4592889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4593889facbeSLen Brown } 4594889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4595889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4596889facbeSLen Brown return -8; 4597889facbeSLen Brown 4598b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4599889facbeSLen Brown 4600889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4601889facbeSLen Brown return -9; 4602b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4603889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4604889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4605889facbeSLen Brown } 4606889facbeSLen Brown return 0; 4607889facbeSLen Brown } 4608889facbeSLen Brown 4609d7899447SLen Brown /* 4610d7899447SLen Brown * SNB adds support for additional MSRs: 4611d7899447SLen Brown * 4612d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4613d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4614d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4615d7899447SLen Brown */ 4616103a8feaSLen Brown 4617d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4618103a8feaSLen Brown { 4619103a8feaSLen Brown if (!genuine_intel) 4620103a8feaSLen Brown return 0; 4621103a8feaSLen Brown 4622103a8feaSLen Brown switch (model) { 4623869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4624869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4625869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4626869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4627c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4628869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 462977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 46305e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4631c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 46325e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4633869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4634af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4635af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4636869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4637869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4638f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 46395ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4640f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 464120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4642103a8feaSLen Brown return 1; 4643103a8feaSLen Brown } 4644103a8feaSLen Brown return 0; 4645103a8feaSLen Brown } 4646103a8feaSLen Brown 4647d7899447SLen Brown /* 4648570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4649d7899447SLen Brown * 4650d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4651d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4652d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 46535a63426eSLen Brown * 46545a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 46555a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 46565a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 46575a63426eSLen Brown * 4658d7899447SLen Brown */ 4659570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4660ca58710fSKristen Carlson Accardi { 4661ca58710fSKristen Carlson Accardi if (!genuine_intel) 4662ca58710fSKristen Carlson Accardi return 0; 4663ca58710fSKristen Carlson Accardi 4664ca58710fSKristen Carlson Accardi switch (model) { 466577e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4666c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4667af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4668af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4669869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4670f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4671f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4672ca58710fSKristen Carlson Accardi return 1; 4673ca58710fSKristen Carlson Accardi } 4674ca58710fSKristen Carlson Accardi return 0; 4675ca58710fSKristen Carlson Accardi } 4676ca58710fSKristen Carlson Accardi 46770b2bb692SLen Brown /* 46780b2bb692SLen Brown * SKL adds support for additional MSRS: 46790b2bb692SLen Brown * 46800b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 46810b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 46820b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 46830b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 46840b2bb692SLen Brown */ 46850b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 46860b2bb692SLen Brown { 46870b2bb692SLen Brown if (!genuine_intel) 46880b2bb692SLen Brown return 0; 46890b2bb692SLen Brown 46900b2bb692SLen Brown switch (model) { 4691af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4692af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 46930b2bb692SLen Brown return 1; 46940b2bb692SLen Brown } 46950b2bb692SLen Brown return 0; 46960b2bb692SLen Brown } 46970b2bb692SLen Brown 4698144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4699144b44b1SLen Brown { 4700144b44b1SLen Brown if (!genuine_intel) 4701144b44b1SLen Brown return 0; 4702144b44b1SLen Brown switch (model) { 4703f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 47045ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4705144b44b1SLen Brown return 1; 4706144b44b1SLen Brown } 4707144b44b1SLen Brown return 0; 4708144b44b1SLen Brown } 4709144b44b1SLen Brown 4710fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4711fb5d4327SDasaratharaman Chandramouli { 4712fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4713fb5d4327SDasaratharaman Chandramouli return 0; 4714fb5d4327SDasaratharaman Chandramouli switch (model) { 4715869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4716fb5d4327SDasaratharaman Chandramouli return 1; 4717fb5d4327SDasaratharaman Chandramouli } 4718fb5d4327SDasaratharaman Chandramouli return 0; 4719fb5d4327SDasaratharaman Chandramouli } 4720fb5d4327SDasaratharaman Chandramouli 4721997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4722997e5395SSrinivas Pandruvada { 4723997e5395SSrinivas Pandruvada if (!genuine_intel) 4724997e5395SSrinivas Pandruvada return 0; 4725997e5395SSrinivas Pandruvada 4726997e5395SSrinivas Pandruvada switch (model) { 4727af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4728997e5395SSrinivas Pandruvada return 1; 4729997e5395SSrinivas Pandruvada } 4730997e5395SSrinivas Pandruvada 4731997e5395SSrinivas Pandruvada return 0; 4732997e5395SSrinivas Pandruvada } 4733997e5395SSrinivas Pandruvada 4734b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4735b2b34dfeSHubert Chrzaniuk { 4736b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4737b2b34dfeSHubert Chrzaniuk return 1024; 4738b2b34dfeSHubert Chrzaniuk return 1; 4739b2b34dfeSHubert Chrzaniuk } 4740b2b34dfeSHubert Chrzaniuk 4741144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4742144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4743144b44b1SLen Brown 4744144b44b1SLen Brown double slm_bclk(void) 4745144b44b1SLen Brown { 4746144b44b1SLen Brown unsigned long long msr = 3; 4747144b44b1SLen Brown unsigned int i; 4748144b44b1SLen Brown double freq; 4749144b44b1SLen Brown 47507ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4751b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4752144b44b1SLen Brown 4753144b44b1SLen Brown i = msr & 0xf; 4754144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4755b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 47560a91e551SColin Ian King i = 3; 4757144b44b1SLen Brown } 4758144b44b1SLen Brown freq = slm_freq_table[i]; 4759144b44b1SLen Brown 476096e47158SLen Brown if (!quiet) 4761b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4762144b44b1SLen Brown 4763144b44b1SLen Brown return freq; 4764144b44b1SLen Brown } 4765144b44b1SLen Brown 4766103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4767103a8feaSLen Brown { 4768121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4769103a8feaSLen Brown return 100.00; 4770144b44b1SLen Brown else if (is_slm(family, model)) 4771144b44b1SLen Brown return slm_bclk(); 4772103a8feaSLen Brown else 4773103a8feaSLen Brown return 133.33; 4774103a8feaSLen Brown } 4775103a8feaSLen Brown 4776889facbeSLen Brown /* 4777889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4778889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4779889facbeSLen Brown * This is usually equal to tjMax. 4780889facbeSLen Brown * 4781889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4782889facbeSLen Brown * but also allow cmdline over-ride with -T. 4783889facbeSLen Brown * 4784889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4785889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4786889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4787889facbeSLen Brown */ 4788889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4789889facbeSLen Brown { 4790889facbeSLen Brown unsigned long long msr; 4791889facbeSLen Brown unsigned int target_c_local; 4792889facbeSLen Brown int cpu; 4793889facbeSLen Brown 4794889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4795889facbeSLen Brown if (!(do_dts || do_ptm)) 4796889facbeSLen Brown return 0; 4797889facbeSLen Brown 4798889facbeSLen Brown /* this is a per-package concept */ 4799889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4800889facbeSLen Brown return 0; 4801889facbeSLen Brown 4802889facbeSLen Brown cpu = t->cpu_id; 4803889facbeSLen Brown if (cpu_migrate(cpu)) { 4804b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4805889facbeSLen Brown return -1; 4806889facbeSLen Brown } 4807889facbeSLen Brown 4808889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4809889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4810b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4811889facbeSLen Brown cpu, tcc_activation_temp); 4812889facbeSLen Brown return 0; 4813889facbeSLen Brown } 4814889facbeSLen Brown 4815889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4816d7899447SLen Brown if (!do_nhm_platform_info) 4817889facbeSLen Brown goto guess; 4818889facbeSLen Brown 48197ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4820889facbeSLen Brown goto guess; 4821889facbeSLen Brown 48223482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4823889facbeSLen Brown 482496e47158SLen Brown if (!quiet) 4825b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4826889facbeSLen Brown cpu, msr, target_c_local); 4827889facbeSLen Brown 48283482124aSJean Delvare if (!target_c_local) 4829889facbeSLen Brown goto guess; 4830889facbeSLen Brown 4831889facbeSLen Brown tcc_activation_temp = target_c_local; 4832889facbeSLen Brown 4833889facbeSLen Brown return 0; 4834889facbeSLen Brown 4835889facbeSLen Brown guess: 4836889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4837b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4838889facbeSLen Brown cpu, tcc_activation_temp); 4839889facbeSLen Brown 4840889facbeSLen Brown return 0; 4841889facbeSLen Brown } 484269807a63SLen Brown 4843aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4844aa8d8cc7SLen Brown { 4845aa8d8cc7SLen Brown unsigned long long msr; 4846aa8d8cc7SLen Brown 4847f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 4848aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4849aa8d8cc7SLen Brown base_cpu, msr, 4850f6505c88SSean Christopherson msr & FEAT_CTL_LOCKED ? "" : "UN-", 4851aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4852aa8d8cc7SLen Brown } 4853aa8d8cc7SLen Brown 485469807a63SLen Brown void decode_misc_enable_msr(void) 485569807a63SLen Brown { 485669807a63SLen Brown unsigned long long msr; 485769807a63SLen Brown 4858f26b1519SLen Brown if (!genuine_intel) 4859f26b1519SLen Brown return; 4860f26b1519SLen Brown 486169807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4862e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 486369807a63SLen Brown base_cpu, msr, 4864e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4865e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4866fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4867e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4868e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 486969807a63SLen Brown } 487069807a63SLen Brown 487133148d67SLen Brown void decode_misc_feature_control(void) 487233148d67SLen Brown { 487333148d67SLen Brown unsigned long long msr; 487433148d67SLen Brown 487533148d67SLen Brown if (!has_misc_feature_control) 487633148d67SLen Brown return; 487733148d67SLen Brown 487833148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 487933148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 488033148d67SLen Brown base_cpu, msr, 488133148d67SLen Brown msr & (0 << 0) ? "No-" : "", 488233148d67SLen Brown msr & (1 << 0) ? "No-" : "", 488333148d67SLen Brown msr & (2 << 0) ? "No-" : "", 488433148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 488533148d67SLen Brown } 4886f0057310SLen Brown /* 4887f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4888f0057310SLen Brown * 4889f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4890f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4891f0057310SLen Brown * bit[1] less so... 4892f0057310SLen Brown */ 4893f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4894f0057310SLen Brown { 4895f0057310SLen Brown unsigned long long msr; 4896f0057310SLen Brown 4897f0057310SLen Brown if (!do_nhm_platform_info) 4898f0057310SLen Brown return; 4899f0057310SLen Brown 4900cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4901cf4cbe53SLen Brown return; 4902cf4cbe53SLen Brown 4903f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4904ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4905f0057310SLen Brown base_cpu, msr, 4906f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4907ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4908ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4909f0057310SLen Brown } 491071616c8eSLen Brown /* 491171616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 491271616c8eSLen Brown * 491371616c8eSLen Brown * This MSRs are present on Silvermont processors, 491471616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 491571616c8eSLen Brown */ 491671616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 491771616c8eSLen Brown { 491871616c8eSLen Brown unsigned long long msr; 491971616c8eSLen Brown 492071616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 492171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 492271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 492371616c8eSLen Brown 492471616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 492571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 492671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 492771616c8eSLen Brown } 49287f5c258eSLen Brown 4929f5a4c76aSLen Brown /* 4930f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 4931f5a4c76aSLen Brown */ 4932f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 4933f5a4c76aSLen Brown { 4934f5a4c76aSLen Brown 4935f5a4c76aSLen Brown switch(model) { 4936f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 4937f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 4938f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 4939f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 4940f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 4941f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 4942f5a4c76aSLen Brown 4943f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 4944f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 4945f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 4946f5a4c76aSLen Brown 4947f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4948f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 4949f5a4c76aSLen Brown 4950f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 49515ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 4952f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 4953f5a4c76aSLen Brown 4954af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 4955c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 4956af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 4957c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 4958081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 4959081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 4960af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 4961937807d3SLen Brown 4962af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 4963d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 49644bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 49654bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 4966e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 4967e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 4968e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 4969af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 4970b62b3184SZhang Rui 4971d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 4972d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 4973d7814c30SChen Yu 497423274fafSChen Yu case INTEL_FAM6_ICELAKE_X: 4975e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 497623274fafSChen Yu return INTEL_FAM6_SKYLAKE_X; 4977f5a4c76aSLen Brown } 4978f5a4c76aSLen Brown return model; 4979f5a4c76aSLen Brown } 4980d76bb7a0SLen Brown 4981d76bb7a0SLen Brown void print_dev_latency(void) 4982d76bb7a0SLen Brown { 4983d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 4984d76bb7a0SLen Brown int fd; 4985d76bb7a0SLen Brown int value; 4986d76bb7a0SLen Brown int retval; 4987d76bb7a0SLen Brown 4988d76bb7a0SLen Brown fd = open(path, O_RDONLY); 4989d76bb7a0SLen Brown if (fd < 0) { 4990d76bb7a0SLen Brown warn("fopen %s\n", path); 4991d76bb7a0SLen Brown return; 4992d76bb7a0SLen Brown } 4993d76bb7a0SLen Brown 4994d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 4995d76bb7a0SLen Brown if (retval != sizeof(int)) { 4996d76bb7a0SLen Brown warn("read %s\n", path); 4997d76bb7a0SLen Brown close(fd); 4998d76bb7a0SLen Brown return; 4999d76bb7a0SLen Brown } 5000d76bb7a0SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", 5001d76bb7a0SLen Brown value, value == 2000000000 ? "default" : "constrained"); 5002d76bb7a0SLen Brown 5003d76bb7a0SLen Brown close(fd); 5004d76bb7a0SLen Brown } 5005d76bb7a0SLen Brown 5006fcd17211SLen Brown void process_cpuid() 5007103a8feaSLen Brown { 500834041551SLen Brown unsigned int eax, ebx, ecx, edx; 500934041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5010b3a34e93SLen Brown unsigned int has_turbo; 5011103a8feaSLen Brown 5012103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5013103a8feaSLen Brown 50145aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5015103a8feaSLen Brown 501634041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5017103a8feaSLen Brown genuine_intel = 1; 501834041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 501934041551SLen Brown authentic_amd = 1; 5020c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5021c1c10cc7SPu Wen hygon_genuine = 1; 5022103a8feaSLen Brown 502396e47158SLen Brown if (!quiet) 5024b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 5025103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 5026103a8feaSLen Brown 50275aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5028103a8feaSLen Brown family = (fms >> 8) & 0xf; 5029103a8feaSLen Brown model = (fms >> 4) & 0xf; 5030103a8feaSLen Brown stepping = fms & 0xf; 50315aa3d1a2SCalvin Walton if (family == 0xf) 50325aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 50335aa3d1a2SCalvin Walton if (family >= 6) 5034103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 503534041551SLen Brown ecx_flags = ecx; 503634041551SLen Brown edx_flags = edx; 5037103a8feaSLen Brown 5038103a8feaSLen Brown /* 5039103a8feaSLen Brown * check max extended function levels of CPUID. 5040103a8feaSLen Brown * This is needed to check for invariant TSC. 5041103a8feaSLen Brown * This check is valid for both Intel and AMD. 5042103a8feaSLen Brown */ 5043103a8feaSLen Brown ebx = ecx = edx = 0; 50445aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5045103a8feaSLen Brown 504634041551SLen Brown if (!quiet) { 504734041551SLen Brown fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 504834041551SLen Brown max_level, max_extended_level, family, model, stepping, family, model, stepping); 504934041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 505034041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 505134041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 505234041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 505334041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 505434041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 505534041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 505634041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 505734041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 505834041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 505934041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 506034041551SLen Brown } 5061f5a4c76aSLen Brown if (genuine_intel) 5062f5a4c76aSLen Brown model = intel_model_duplicates(model); 506334041551SLen Brown 506434041551SLen Brown if (!(edx_flags & (1 << 5))) 506534041551SLen Brown errx(1, "CPUID: no MSR"); 506634041551SLen Brown 506761a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5068103a8feaSLen Brown 5069103a8feaSLen Brown /* 5070103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5071103a8feaSLen Brown * this check is valid for both Intel and AMD 5072103a8feaSLen Brown */ 50735aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 50748209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5075d7899447SLen Brown } 5076103a8feaSLen Brown 5077103a8feaSLen Brown /* 5078103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5079103a8feaSLen Brown * this check is valid for both Intel and AMD 5080103a8feaSLen Brown */ 5081103a8feaSLen Brown 50825aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 50838209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5084812db3f7SLen Brown if (has_aperf) { 5085812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5086812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5087812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5088812db3f7SLen Brown } 5089889facbeSLen Brown do_dts = eax & (1 << 0); 5090812db3f7SLen Brown if (do_dts) 5091812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5092b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5093889facbeSLen Brown do_ptm = eax & (1 << 6); 5094812db3f7SLen Brown if (do_ptm) 5095812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 50967f5c258eSLen Brown has_hwp = eax & (1 << 7); 50977f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 50987f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 50997f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 51007f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5101889facbeSLen Brown has_epb = ecx & (1 << 3); 5102889facbeSLen Brown 510396e47158SLen Brown if (!quiet) 5104b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 51057f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 51067f5c258eSLen Brown has_aperf ? "" : "No-", 5107b3a34e93SLen Brown has_turbo ? "" : "No-", 51087f5c258eSLen Brown do_dts ? "" : "No-", 51097f5c258eSLen Brown do_ptm ? "" : "No-", 51107f5c258eSLen Brown has_hwp ? "" : "No-", 51117f5c258eSLen Brown has_hwp_notify ? "" : "No-", 51127f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 51137f5c258eSLen Brown has_hwp_epp ? "" : "No-", 51147f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 51157f5c258eSLen Brown has_epb ? "" : "No-"); 5116103a8feaSLen Brown 511796e47158SLen Brown if (!quiet) 511869807a63SLen Brown decode_misc_enable_msr(); 511969807a63SLen Brown 512033148d67SLen Brown 512196e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5122aa8d8cc7SLen Brown int has_sgx; 5123aa8d8cc7SLen Brown 5124aa8d8cc7SLen Brown ecx = 0; 5125aa8d8cc7SLen Brown 5126aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5127aa8d8cc7SLen Brown 5128aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5129aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5130aa8d8cc7SLen Brown 5131aa8d8cc7SLen Brown if (has_sgx) 5132aa8d8cc7SLen Brown decode_feature_control_msr(); 5133aa8d8cc7SLen Brown } 5134aa8d8cc7SLen Brown 513561a87ba7SLen Brown if (max_level >= 0x15) { 51368a5bdf41SLen Brown unsigned int eax_crystal; 51378a5bdf41SLen Brown unsigned int ebx_tsc; 51388a5bdf41SLen Brown 51398a5bdf41SLen Brown /* 51408a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 51418a5bdf41SLen Brown */ 51428a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 51435aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 51448a5bdf41SLen Brown 51458a5bdf41SLen Brown if (ebx_tsc != 0) { 51468a5bdf41SLen Brown 514796e47158SLen Brown if (!quiet && (ebx != 0)) 5148b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 51498a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 51508a5bdf41SLen Brown 51518a5bdf41SLen Brown if (crystal_hz == 0) 51528a5bdf41SLen Brown switch(model) { 5153af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5154e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5155e8efbc80SLen Brown break; 51565ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5157ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5158ec53e594SLen Brown break; 5159869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5160f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5161e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 51628a5bdf41SLen Brown break; 51638a5bdf41SLen Brown default: 51648a5bdf41SLen Brown crystal_hz = 0; 51658a5bdf41SLen Brown } 51668a5bdf41SLen Brown 51678a5bdf41SLen Brown if (crystal_hz) { 51688a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 516996e47158SLen Brown if (!quiet) 5170b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 51718a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 51728a5bdf41SLen Brown } 51738a5bdf41SLen Brown } 51748a5bdf41SLen Brown } 517561a87ba7SLen Brown if (max_level >= 0x16) { 517661a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 517761a87ba7SLen Brown 517861a87ba7SLen Brown /* 517961a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 518061a87ba7SLen Brown */ 518161a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 518261a87ba7SLen Brown 51835aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 518496e47158SLen Brown if (!quiet) 5185b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 518661a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 518761a87ba7SLen Brown } 51888a5bdf41SLen Brown 5189b2b34dfeSHubert Chrzaniuk if (has_aperf) 5190b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5191b2b34dfeSHubert Chrzaniuk 5192812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5193812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5194812db3f7SLen Brown 5195812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5196812db3f7SLen Brown do_nhm_platform_info = 1; 5197812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5198812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5199812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5200812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5201812db3f7SLen Brown } 5202d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5203812db3f7SLen Brown 5204812db3f7SLen Brown if (do_snb_cstates) 5205812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5206812db3f7SLen Brown 52075a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 52080f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 52090f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 52100f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 52110f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 52120f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 52130f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 52140f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 52150f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 52160539ba11SLen Brown if (has_slv_msrs(family, model)) { 52170f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 52180f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 52190f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 52200f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 52210539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 52220539ba11SLen Brown use_c1_residency_msr = 1; 52230539ba11SLen Brown } 522420de0dabSAntti Laakso if (is_jvl(family, model)) { 522520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 522620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 522720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 522820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 522920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 523020de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 523120de0dabSAntti Laakso } 52327170a374SLen Brown if (is_dnv(family, model)) { 52337170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 52347170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 52357170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 52367170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 52377170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 52387170a374SLen Brown use_c1_residency_msr = 1; 52397170a374SLen Brown } 524034c76197SLen Brown if (is_skx(family, model)) { 524134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 524234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 524334c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 524434c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 524534c76197SLen Brown } 5246ade0ebacSLen Brown if (is_bdx(family, model)) { 5247ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5248ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5249ade0ebacSLen Brown } 5250570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5251c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 52520f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5253c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 52540f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5255c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 52560f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 52570f47c08dSLen Brown } 5258570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5259a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5260a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5261a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5262a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5263a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5264a99d8730SLen Brown } 5265144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5266fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5267103a8feaSLen Brown 5268f6708400SChen Yu if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || 5269f6708400SChen Yu is_ehl(family, model)) 5270562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5271103a8feaSLen Brown 527296e47158SLen Brown if (!quiet) 5273f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5274f0057310SLen Brown 527596e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 527671616c8eSLen Brown decode_c6_demotion_policy_msr(); 527771616c8eSLen Brown 5278889facbeSLen Brown rapl_probe(family, model); 52793a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5280ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5281889facbeSLen Brown 528296e47158SLen Brown if (!quiet) 52831b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5284fcd17211SLen Brown 528541618e63SLen Brown if (!quiet) 5286d76bb7a0SLen Brown print_dev_latency(); 5287d76bb7a0SLen Brown if (!quiet) 528841618e63SLen Brown dump_sysfs_cstate_config(); 52897293fccdSLen Brown if (!quiet) 52907293fccdSLen Brown dump_sysfs_pstate_config(); 529141618e63SLen Brown 5292a2b7b749SLen Brown if (has_skl_msrs(family, model)) 5293a2b7b749SLen Brown calculate_tsc_tweak(); 5294a2b7b749SLen Brown 5295812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5296812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5297fdf676e5SLen Brown 5298812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5299812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 530027d47356SLen Brown 5301b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5302b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5303b4b91569SRafael Antognolli 5304be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5305be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5306be0e54c4SLen Brown else 5307be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5308be0e54c4SLen Brown 53091f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 53101f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5311be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 53121f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 53131f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 53141f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 53151f81c5efSLen Brown } else { 53161f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5317be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 53181f81c5efSLen Brown } 5319be0e54c4SLen Brown 532096e47158SLen Brown if (!quiet) 532133148d67SLen Brown decode_misc_feature_control(); 532233148d67SLen Brown 5323889facbeSLen Brown return; 5324103a8feaSLen Brown } 5325103a8feaSLen Brown 5326103a8feaSLen Brown /* 5327103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5328103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5329103a8feaSLen Brown */ 5330103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5331103a8feaSLen Brown { 5332103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5333103a8feaSLen Brown return 1; 5334103a8feaSLen Brown else 5335103a8feaSLen Brown return 0; 5336103a8feaSLen Brown } 5337103a8feaSLen Brown 5338103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5339103a8feaSLen Brown { 5340103a8feaSLen Brown return 0; 5341103a8feaSLen Brown } 5342103a8feaSLen Brown 5343c98d5d94SLen Brown void topology_probe() 5344c98d5d94SLen Brown { 5345c98d5d94SLen Brown int i; 5346c98d5d94SLen Brown int max_core_id = 0; 5347c98d5d94SLen Brown int max_package_id = 0; 53486de68fe1SLen Brown int max_die_id = 0; 5349c98d5d94SLen Brown int max_siblings = 0; 5350c98d5d94SLen Brown 5351c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5352843c5791SPrarit Bhargava set_max_cpu_num(); 5353c98d5d94SLen Brown topo.num_cpus = 0; 5354c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5355c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5356812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5357c98d5d94SLen Brown 5358d8af6f5fSLen Brown if (debug > 1) 5359b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5360c98d5d94SLen Brown 5361c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5362b2c95d90SJosh Triplett if (cpus == NULL) 5363b2c95d90SJosh Triplett err(1, "calloc cpus"); 5364c98d5d94SLen Brown 5365c98d5d94SLen Brown /* 5366c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5367c98d5d94SLen Brown */ 5368c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5369b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5370b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5371c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5372c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5373c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5374c98d5d94SLen Brown 5375c98d5d94SLen Brown /* 53761ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 53771ef7d21aSLen Brown */ 53781ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 53791ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 53801ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 53811ef7d21aSLen Brown err(1, "cpu%d not present", i); 53821ef7d21aSLen Brown } 53831ef7d21aSLen Brown 53841ef7d21aSLen Brown /* 5385c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5386c98d5d94SLen Brown */ 5387c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5388b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5389b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5390c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5391c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5392c98d5d94SLen Brown 53938cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5394c98d5d94SLen Brown 5395c98d5d94SLen Brown /* 5396c98d5d94SLen Brown * For online cpus 5397c98d5d94SLen Brown * find max_core_id, max_package_id 5398c98d5d94SLen Brown */ 5399c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5400c98d5d94SLen Brown int siblings; 5401c98d5d94SLen Brown 5402c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5403d8af6f5fSLen Brown if (debug > 1) 5404b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5405c98d5d94SLen Brown continue; 5406c98d5d94SLen Brown } 5407c98d5d94SLen Brown 54080e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 54090e2d8f05SLen Brown 54100e2d8f05SLen Brown /* get package information */ 5411c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5412c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5413c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5414c98d5d94SLen Brown 54156de68fe1SLen Brown /* get die information */ 54166de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 54176de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 54186de68fe1SLen Brown max_die_id = cpus[i].die_id; 54196de68fe1SLen Brown 54200e2d8f05SLen Brown /* get numa node information */ 5421ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5422ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5423ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 54240e2d8f05SLen Brown 54250e2d8f05SLen Brown /* get core information */ 54260e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 54270e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 54280e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 54290e2d8f05SLen Brown 54300e2d8f05SLen Brown /* get thread information */ 54310e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5432c98d5d94SLen Brown if (siblings > max_siblings) 5433c98d5d94SLen Brown max_siblings = siblings; 54344f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 54358cb48b32SPrarit Bhargava topo.num_cores++; 5436c98d5d94SLen Brown } 5437ef605741SPrarit Bhargava 543870a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5439d8af6f5fSLen Brown if (debug > 1) 5440b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 544170a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 544270a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5443812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5444c98d5d94SLen Brown 54456de68fe1SLen Brown topo.num_die = max_die_id + 1; 54466de68fe1SLen Brown if (debug > 1) 54476de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 54486de68fe1SLen Brown max_die_id, topo.num_die); 54496de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 54506de68fe1SLen Brown BIC_PRESENT(BIC_Die); 54516de68fe1SLen Brown 5452c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5453d8af6f5fSLen Brown if (debug > 1) 5454b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5455c98d5d94SLen Brown max_package_id, topo.num_packages); 54567da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5457812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5458c98d5d94SLen Brown 5459ef605741SPrarit Bhargava set_node_data(); 5460ef605741SPrarit Bhargava if (debug > 1) 546170a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 546201235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 546301235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5464ef605741SPrarit Bhargava 546570a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5466d8af6f5fSLen Brown if (debug > 1) 5467b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 54682ffbb224SPrarit Bhargava 54692ffbb224SPrarit Bhargava if (debug < 1) 54702ffbb224SPrarit Bhargava return; 54712ffbb224SPrarit Bhargava 54722ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 54730ec712e3SLen Brown if (cpu_is_not_present(i)) 54740ec712e3SLen Brown continue; 54752ffbb224SPrarit Bhargava fprintf(outf, 54766de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 54776de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 54782ffbb224SPrarit Bhargava cpus[i].physical_node_id, 54792ffbb224SPrarit Bhargava cpus[i].logical_node_id, 54802ffbb224SPrarit Bhargava cpus[i].physical_core_id, 54812ffbb224SPrarit Bhargava cpus[i].thread_id); 54822ffbb224SPrarit Bhargava } 54832ffbb224SPrarit Bhargava 5484c98d5d94SLen Brown } 5485c98d5d94SLen Brown 5486c98d5d94SLen Brown void 548740f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 548840f5cfe7SPrarit Bhargava struct pkg_data **p) 5489c98d5d94SLen Brown { 5490c98d5d94SLen Brown int i; 549140f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 549240f5cfe7SPrarit Bhargava topo.num_packages; 549340f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5494c98d5d94SLen Brown 549540f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5496c98d5d94SLen Brown if (*t == NULL) 5497c98d5d94SLen Brown goto error; 5498c98d5d94SLen Brown 549940f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5500c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5501c98d5d94SLen Brown 550240f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5503c98d5d94SLen Brown if (*c == NULL) 5504c98d5d94SLen Brown goto error; 5505c98d5d94SLen Brown 550640f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5507c98d5d94SLen Brown (*c)[i].core_id = -1; 5508c98d5d94SLen Brown 5509678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5510c98d5d94SLen Brown if (*p == NULL) 5511c98d5d94SLen Brown goto error; 5512c98d5d94SLen Brown 5513c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5514c98d5d94SLen Brown (*p)[i].package_id = i; 5515c98d5d94SLen Brown 5516c98d5d94SLen Brown return; 5517c98d5d94SLen Brown error: 5518b2c95d90SJosh Triplett err(1, "calloc counters"); 5519c98d5d94SLen Brown } 5520c98d5d94SLen Brown /* 5521c98d5d94SLen Brown * init_counter() 5522c98d5d94SLen Brown * 5523c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5524c98d5d94SLen Brown */ 5525c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 55268cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5527c98d5d94SLen Brown { 55288cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 552940f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 55308cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 55318cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5532c98d5d94SLen Brown struct thread_data *t; 5533c98d5d94SLen Brown struct core_data *c; 5534c98d5d94SLen Brown struct pkg_data *p; 5535c98d5d94SLen Brown 553642dd4520SNathan Ciobanu 553742dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 553842dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 553942dd4520SNathan Ciobanu */ 554042dd4520SNathan Ciobanu if (node_id < 0) 554142dd4520SNathan Ciobanu node_id = 0; 554242dd4520SNathan Ciobanu 554340f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 554440f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 55458cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5546c98d5d94SLen Brown 5547c98d5d94SLen Brown t->cpu_id = cpu_id; 55488cb48b32SPrarit Bhargava if (thread_id == 0) { 5549c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5550c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5551c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5552c98d5d94SLen Brown } 5553c98d5d94SLen Brown 55548cb48b32SPrarit Bhargava c->core_id = core_id; 55558cb48b32SPrarit Bhargava p->package_id = pkg_id; 5556c98d5d94SLen Brown } 5557c98d5d94SLen Brown 5558c98d5d94SLen Brown 5559c98d5d94SLen Brown int initialize_counters(int cpu_id) 5560c98d5d94SLen Brown { 55618cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 55628cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5563c98d5d94SLen Brown return 0; 5564c98d5d94SLen Brown } 5565c98d5d94SLen Brown 5566c98d5d94SLen Brown void allocate_output_buffer() 5567c98d5d94SLen Brown { 5568eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5569c98d5d94SLen Brown outp = output_buffer; 5570b2c95d90SJosh Triplett if (outp == NULL) 5571b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5572c98d5d94SLen Brown } 557336229897SLen Brown void allocate_fd_percpu(void) 557436229897SLen Brown { 557501a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 557636229897SLen Brown if (fd_percpu == NULL) 557736229897SLen Brown err(-1, "calloc fd_percpu"); 557836229897SLen Brown } 5579562a2d37SLen Brown void allocate_irq_buffers(void) 5580562a2d37SLen Brown { 5581562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5582562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5583562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5584c98d5d94SLen Brown 558501a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5586562a2d37SLen Brown if (irqs_per_cpu == NULL) 558701a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5588562a2d37SLen Brown } 5589c98d5d94SLen Brown void setup_all_buffers(void) 5590c98d5d94SLen Brown { 5591c98d5d94SLen Brown topology_probe(); 5592562a2d37SLen Brown allocate_irq_buffers(); 559336229897SLen Brown allocate_fd_percpu(); 5594c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5595c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5596c98d5d94SLen Brown allocate_output_buffer(); 5597c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5598c98d5d94SLen Brown } 55993b4d5c7fSAndy Shevchenko 56007ce7d5deSPrarit Bhargava void set_base_cpu(void) 56017ce7d5deSPrarit Bhargava { 56027ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 56037ce7d5deSPrarit Bhargava if (base_cpu < 0) 56047ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 56057ce7d5deSPrarit Bhargava 56067ce7d5deSPrarit Bhargava if (debug > 1) 5607b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 56087ce7d5deSPrarit Bhargava } 56097ce7d5deSPrarit Bhargava 5610103a8feaSLen Brown void turbostat_init() 5611103a8feaSLen Brown { 56127ce7d5deSPrarit Bhargava setup_all_buffers(); 56137ce7d5deSPrarit Bhargava set_base_cpu(); 5614103a8feaSLen Brown check_dev_msr(); 561598481e79SLen Brown check_permissions(); 5616fcd17211SLen Brown process_cpuid(); 5617103a8feaSLen Brown 5618103a8feaSLen Brown 561996e47158SLen Brown if (!quiet) 56207f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 56217f5c258eSLen Brown 562296e47158SLen Brown if (!quiet) 5623889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5624889facbeSLen Brown 562596e47158SLen Brown if (!quiet) 56263a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 56273a9a941dSLen Brown 562896e47158SLen Brown if (!quiet) 5629889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5630889facbeSLen Brown 5631889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5632889facbeSLen Brown 563396e47158SLen Brown if (!quiet) 5634889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 56355a63426eSLen Brown 563696e47158SLen Brown if (!quiet && do_irtl_snb) 56375a63426eSLen Brown print_irtl(); 5638103a8feaSLen Brown } 5639103a8feaSLen Brown 5640103a8feaSLen Brown int fork_it(char **argv) 5641103a8feaSLen Brown { 5642103a8feaSLen Brown pid_t child_pid; 5643d91bb17cSLen Brown int status; 5644d15cf7c1SLen Brown 5645218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5646d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 56474c2122d4SLen Brown first_counter_read = 0; 5648d91bb17cSLen Brown if (status) 5649d91bb17cSLen Brown exit(status); 5650d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5651d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5652103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5653103a8feaSLen Brown 5654103a8feaSLen Brown child_pid = fork(); 5655103a8feaSLen Brown if (!child_pid) { 5656103a8feaSLen Brown /* child */ 5657103a8feaSLen Brown execvp(argv[0], argv); 56580815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5659103a8feaSLen Brown } else { 5660103a8feaSLen Brown 5661103a8feaSLen Brown /* parent */ 5662b2c95d90SJosh Triplett if (child_pid == -1) 5663b2c95d90SJosh Triplett err(1, "fork"); 5664103a8feaSLen Brown 5665103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5666103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5667b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5668b2c95d90SJosh Triplett err(status, "waitpid"); 56692a954966SDavid Arcari 56702a954966SDavid Arcari if (WIFEXITED(status)) 56712a954966SDavid Arcari status = WEXITSTATUS(status); 5672103a8feaSLen Brown } 5673c98d5d94SLen Brown /* 5674c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5675c98d5d94SLen Brown * because re-starting is problematic when forking 5676c98d5d94SLen Brown */ 5677218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5678c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5679103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5680103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5681ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5682ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5683ba3dec99SLen Brown else { 5684c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5685c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5686ba3dec99SLen Brown } 5687103a8feaSLen Brown 5688b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5689b7d8c148SLen Brown 5690b7d8c148SLen Brown flush_output_stderr(); 5691103a8feaSLen Brown 5692d91bb17cSLen Brown return status; 5693103a8feaSLen Brown } 5694103a8feaSLen Brown 56953b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 56963b4d5c7fSAndy Shevchenko { 56973b4d5c7fSAndy Shevchenko int status; 56983b4d5c7fSAndy Shevchenko 5699218f0e8dSLen Brown snapshot_proc_sysfs_files(); 57003b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 57013b4d5c7fSAndy Shevchenko if (status) 57023b4d5c7fSAndy Shevchenko return status; 57033b4d5c7fSAndy Shevchenko 57043b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 57053b4d5c7fSAndy Shevchenko if (status) 57063b4d5c7fSAndy Shevchenko return status; 57073b4d5c7fSAndy Shevchenko 5708b7d8c148SLen Brown flush_output_stdout(); 57093b4d5c7fSAndy Shevchenko 57103b4d5c7fSAndy Shevchenko return status; 57113b4d5c7fSAndy Shevchenko } 57123b4d5c7fSAndy Shevchenko 5713d8af6f5fSLen Brown void print_version() { 5714b95fffb9SLen Brown fprintf(outf, "turbostat version 20.03.20" 5715d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5716d8af6f5fSLen Brown } 5717d8af6f5fSLen Brown 5718495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5719495c7654SLen Brown unsigned int width, enum counter_scope scope, 572041618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5721388e9c81SLen Brown { 5722388e9c81SLen Brown struct msr_counter *msrp; 5723388e9c81SLen Brown 5724388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5725388e9c81SLen Brown if (msrp == NULL) { 5726388e9c81SLen Brown perror("calloc"); 5727388e9c81SLen Brown exit(1); 5728388e9c81SLen Brown } 5729388e9c81SLen Brown 5730388e9c81SLen Brown msrp->msr_num = msr_num; 5731d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5732495c7654SLen Brown if (path) 5733d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5734388e9c81SLen Brown msrp->width = width; 5735388e9c81SLen Brown msrp->type = type; 5736388e9c81SLen Brown msrp->format = format; 573741618e63SLen Brown msrp->flags = flags; 5738388e9c81SLen Brown 5739388e9c81SLen Brown switch (scope) { 5740388e9c81SLen Brown 5741388e9c81SLen Brown case SCOPE_CPU: 5742388e9c81SLen Brown msrp->next = sys.tp; 5743388e9c81SLen Brown sys.tp = msrp; 5744678a3bd1SLen Brown sys.added_thread_counters++; 57450748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5746678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5747678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5748678a3bd1SLen Brown exit(-1); 5749678a3bd1SLen Brown } 5750388e9c81SLen Brown break; 5751388e9c81SLen Brown 5752388e9c81SLen Brown case SCOPE_CORE: 5753388e9c81SLen Brown msrp->next = sys.cp; 5754388e9c81SLen Brown sys.cp = msrp; 5755678a3bd1SLen Brown sys.added_core_counters++; 5756678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5757678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5758678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5759678a3bd1SLen Brown exit(-1); 5760678a3bd1SLen Brown } 5761388e9c81SLen Brown break; 5762388e9c81SLen Brown 5763388e9c81SLen Brown case SCOPE_PACKAGE: 5764388e9c81SLen Brown msrp->next = sys.pp; 5765388e9c81SLen Brown sys.pp = msrp; 5766678a3bd1SLen Brown sys.added_package_counters++; 5767678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5768678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5769678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5770678a3bd1SLen Brown exit(-1); 5771678a3bd1SLen Brown } 5772388e9c81SLen Brown break; 5773388e9c81SLen Brown } 5774388e9c81SLen Brown 5775388e9c81SLen Brown return 0; 5776388e9c81SLen Brown } 5777388e9c81SLen Brown 5778388e9c81SLen Brown void parse_add_command(char *add_command) 5779388e9c81SLen Brown { 5780388e9c81SLen Brown int msr_num = 0; 5781495c7654SLen Brown char *path = NULL; 57820f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5783388e9c81SLen Brown int width = 64; 5784388e9c81SLen Brown int fail = 0; 5785388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5786388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5787388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5788388e9c81SLen Brown 5789388e9c81SLen Brown while (add_command) { 5790388e9c81SLen Brown 5791388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5792388e9c81SLen Brown goto next; 5793388e9c81SLen Brown 5794388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5795388e9c81SLen Brown goto next; 5796388e9c81SLen Brown 5797495c7654SLen Brown if (*add_command == '/') { 5798495c7654SLen Brown path = add_command; 5799495c7654SLen Brown goto next; 5800495c7654SLen Brown } 5801495c7654SLen Brown 5802388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5803388e9c81SLen Brown if ((width == 32) || (width == 64)) 5804388e9c81SLen Brown goto next; 5805388e9c81SLen Brown width = 64; 5806388e9c81SLen Brown } 5807388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5808388e9c81SLen Brown scope = SCOPE_CPU; 5809388e9c81SLen Brown goto next; 5810388e9c81SLen Brown } 5811388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5812388e9c81SLen Brown scope = SCOPE_CORE; 5813388e9c81SLen Brown goto next; 5814388e9c81SLen Brown } 5815388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5816388e9c81SLen Brown scope = SCOPE_PACKAGE; 5817388e9c81SLen Brown goto next; 5818388e9c81SLen Brown } 5819388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5820388e9c81SLen Brown type = COUNTER_CYCLES; 5821388e9c81SLen Brown goto next; 5822388e9c81SLen Brown } 5823388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5824388e9c81SLen Brown type = COUNTER_SECONDS; 5825388e9c81SLen Brown goto next; 5826388e9c81SLen Brown } 582741618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 582841618e63SLen Brown type = COUNTER_USEC; 582941618e63SLen Brown goto next; 583041618e63SLen Brown } 5831388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5832388e9c81SLen Brown format = FORMAT_RAW; 5833388e9c81SLen Brown goto next; 5834388e9c81SLen Brown } 5835388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5836388e9c81SLen Brown format = FORMAT_DELTA; 5837388e9c81SLen Brown goto next; 5838388e9c81SLen Brown } 5839388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5840388e9c81SLen Brown format = FORMAT_PERCENT; 5841388e9c81SLen Brown goto next; 5842388e9c81SLen Brown } 5843388e9c81SLen Brown 5844388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5845388e9c81SLen Brown char *eos; 5846388e9c81SLen Brown 5847388e9c81SLen Brown eos = strchr(name_buffer, ','); 5848388e9c81SLen Brown if (eos) 5849388e9c81SLen Brown *eos = '\0'; 5850388e9c81SLen Brown goto next; 5851388e9c81SLen Brown } 5852388e9c81SLen Brown 5853388e9c81SLen Brown next: 5854388e9c81SLen Brown add_command = strchr(add_command, ','); 5855495c7654SLen Brown if (add_command) { 5856495c7654SLen Brown *add_command = '\0'; 5857388e9c81SLen Brown add_command++; 5858495c7654SLen Brown } 5859388e9c81SLen Brown 5860388e9c81SLen Brown } 5861495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5862495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5863388e9c81SLen Brown fail++; 5864388e9c81SLen Brown } 5865388e9c81SLen Brown 5866388e9c81SLen Brown /* generate default column header */ 5867388e9c81SLen Brown if (*name_buffer == '\0') { 5868388e9c81SLen Brown if (width == 32) 58695f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5870388e9c81SLen Brown else 58715f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5872388e9c81SLen Brown } 5873388e9c81SLen Brown 587441618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5875388e9c81SLen Brown fail++; 5876388e9c81SLen Brown 5877388e9c81SLen Brown if (fail) { 5878388e9c81SLen Brown help(); 5879388e9c81SLen Brown exit(1); 5880388e9c81SLen Brown } 5881388e9c81SLen Brown } 588241618e63SLen Brown 5883dd778a5eSLen Brown int is_deferred_skip(char *name) 5884dd778a5eSLen Brown { 5885dd778a5eSLen Brown int i; 5886dd778a5eSLen Brown 5887dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5888dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5889dd778a5eSLen Brown return 1; 5890dd778a5eSLen Brown return 0; 5891dd778a5eSLen Brown } 5892dd778a5eSLen Brown 589341618e63SLen Brown void probe_sysfs(void) 589441618e63SLen Brown { 589541618e63SLen Brown char path[64]; 589641618e63SLen Brown char name_buf[16]; 589741618e63SLen Brown FILE *input; 589841618e63SLen Brown int state; 589941618e63SLen Brown char *sp; 590041618e63SLen Brown 590141618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 590241618e63SLen Brown return; 590341618e63SLen Brown 59040748eaf0SLen Brown for (state = 10; state >= 0; --state) { 590541618e63SLen Brown 590641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 590741618e63SLen Brown base_cpu, state); 590841618e63SLen Brown input = fopen(path, "r"); 590941618e63SLen Brown if (input == NULL) 591041618e63SLen Brown continue; 59118173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 59128173c336SBen Hutchings err(1, "%s: failed to read file", path); 591341618e63SLen Brown 591441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 591541618e63SLen Brown sp = strchr(name_buf, '-'); 591641618e63SLen Brown if (!sp) 591741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 591841618e63SLen Brown *sp = '%'; 591941618e63SLen Brown *(sp + 1) = '\0'; 592041618e63SLen Brown 5921fecb3bc8SDavid Arcari remove_underbar(name_buf); 5922fecb3bc8SDavid Arcari 592341618e63SLen Brown fclose(input); 592441618e63SLen Brown 592541618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 592641618e63SLen Brown 5927dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5928dd778a5eSLen Brown continue; 5929dd778a5eSLen Brown 593041618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 593141618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 593241618e63SLen Brown } 593341618e63SLen Brown 59340748eaf0SLen Brown for (state = 10; state >= 0; --state) { 593541618e63SLen Brown 593641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 593741618e63SLen Brown base_cpu, state); 593841618e63SLen Brown input = fopen(path, "r"); 593941618e63SLen Brown if (input == NULL) 594041618e63SLen Brown continue; 59418173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 59428173c336SBen Hutchings err(1, "%s: failed to read file", path); 594341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 594441618e63SLen Brown sp = strchr(name_buf, '-'); 594541618e63SLen Brown if (!sp) 594641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 594741618e63SLen Brown *sp = '\0'; 594841618e63SLen Brown fclose(input); 594941618e63SLen Brown 5950fecb3bc8SDavid Arcari remove_underbar(name_buf); 5951fecb3bc8SDavid Arcari 595241618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 595341618e63SLen Brown 5954dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5955dd778a5eSLen Brown continue; 5956dd778a5eSLen Brown 595741618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 595841618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 595941618e63SLen Brown } 596041618e63SLen Brown 596141618e63SLen Brown } 596241618e63SLen Brown 59631ef7d21aSLen Brown 59641ef7d21aSLen Brown /* 59651ef7d21aSLen Brown * parse cpuset with following syntax 59661ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 59671ef7d21aSLen Brown */ 59681ef7d21aSLen Brown void parse_cpu_command(char *optarg) 59691ef7d21aSLen Brown { 59701ef7d21aSLen Brown unsigned int start, end; 59711ef7d21aSLen Brown char *next; 59721ef7d21aSLen Brown 59734e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 59744e4e1e7cSLen Brown if (cpu_subset) 59754e4e1e7cSLen Brown goto error; 59764e4e1e7cSLen Brown show_core_only++; 59774e4e1e7cSLen Brown return; 59784e4e1e7cSLen Brown } 59794e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 59804e4e1e7cSLen Brown if (cpu_subset) 59814e4e1e7cSLen Brown goto error; 59824e4e1e7cSLen Brown show_pkg_only++; 59834e4e1e7cSLen Brown return; 59844e4e1e7cSLen Brown } 59854e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 59864e4e1e7cSLen Brown goto error; 59874e4e1e7cSLen Brown 59881ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 59891ef7d21aSLen Brown if (cpu_subset == NULL) 59901ef7d21aSLen Brown err(3, "CPU_ALLOC"); 59911ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 59921ef7d21aSLen Brown 59931ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 59941ef7d21aSLen Brown 59951ef7d21aSLen Brown next = optarg; 59961ef7d21aSLen Brown 59971ef7d21aSLen Brown while (next && *next) { 59981ef7d21aSLen Brown 59991ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 60001ef7d21aSLen Brown goto error; 60011ef7d21aSLen Brown 60021ef7d21aSLen Brown start = strtoul(next, &next, 10); 60031ef7d21aSLen Brown 60041ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 60051ef7d21aSLen Brown goto error; 60061ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 60071ef7d21aSLen Brown 60081ef7d21aSLen Brown if (*next == '\0') 60091ef7d21aSLen Brown break; 60101ef7d21aSLen Brown 60111ef7d21aSLen Brown if (*next == ',') { 60121ef7d21aSLen Brown next += 1; 60131ef7d21aSLen Brown continue; 60141ef7d21aSLen Brown } 60151ef7d21aSLen Brown 60161ef7d21aSLen Brown if (*next == '-') { 60171ef7d21aSLen Brown next += 1; /* start range */ 60181ef7d21aSLen Brown } else if (*next == '.') { 60191ef7d21aSLen Brown next += 1; 60201ef7d21aSLen Brown if (*next == '.') 60211ef7d21aSLen Brown next += 1; /* start range */ 60221ef7d21aSLen Brown else 60231ef7d21aSLen Brown goto error; 60241ef7d21aSLen Brown } 60251ef7d21aSLen Brown 60261ef7d21aSLen Brown end = strtoul(next, &next, 10); 60271ef7d21aSLen Brown if (end <= start) 60281ef7d21aSLen Brown goto error; 60291ef7d21aSLen Brown 60301ef7d21aSLen Brown while (++start <= end) { 60311ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 60321ef7d21aSLen Brown goto error; 60331ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 60341ef7d21aSLen Brown } 60351ef7d21aSLen Brown 60361ef7d21aSLen Brown if (*next == ',') 60371ef7d21aSLen Brown next += 1; 60381ef7d21aSLen Brown else if (*next != '\0') 60391ef7d21aSLen Brown goto error; 60401ef7d21aSLen Brown } 60411ef7d21aSLen Brown 60421ef7d21aSLen Brown return; 60431ef7d21aSLen Brown 60441ef7d21aSLen Brown error: 60454e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 60464e4e1e7cSLen Brown help(); 60471ef7d21aSLen Brown exit(-1); 60481ef7d21aSLen Brown } 60491ef7d21aSLen Brown 6050812db3f7SLen Brown 6051103a8feaSLen Brown void cmdline(int argc, char **argv) 6052103a8feaSLen Brown { 6053103a8feaSLen Brown int opt; 6054d8af6f5fSLen Brown int option_index = 0; 6055d8af6f5fSLen Brown static struct option long_options[] = { 6056388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 60571ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 6058d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 605996e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 60603f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 6061d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 6062023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 6063d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 6064812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 6065d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 6066c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 6067b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 606896e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 6069812db3f7SLen Brown {"show", required_argument, 0, 's'}, 6070d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 6071d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 6072d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 6073d8af6f5fSLen Brown {0, 0, 0, 0 } 6074d8af6f5fSLen Brown }; 6075103a8feaSLen Brown 6076103a8feaSLen Brown progname = argv[0]; 6077103a8feaSLen Brown 6078023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 6079d8af6f5fSLen Brown long_options, &option_index)) != -1) { 6080103a8feaSLen Brown switch (opt) { 6081388e9c81SLen Brown case 'a': 6082388e9c81SLen Brown parse_add_command(optarg); 6083388e9c81SLen Brown break; 60841ef7d21aSLen Brown case 'c': 60851ef7d21aSLen Brown parse_cpu_command(optarg); 60861ef7d21aSLen Brown break; 6087d8af6f5fSLen Brown case 'D': 6088d8af6f5fSLen Brown dump_only++; 60898e180f3cSLen Brown break; 60903f44a5c6SLen Brown case 'e': 60913f44a5c6SLen Brown /* --enable specified counter */ 60924c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 60933f44a5c6SLen Brown break; 6094d8af6f5fSLen Brown case 'd': 6095d8af6f5fSLen Brown debug++; 60963f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 60972f32edf1SLen Brown break; 6098812db3f7SLen Brown case 'H': 60993f44a5c6SLen Brown /* 61003f44a5c6SLen Brown * --hide: do not show those specified 61013f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 61023f44a5c6SLen Brown */ 61033f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6104812db3f7SLen Brown break; 6105d8af6f5fSLen Brown case 'h': 6106d8af6f5fSLen Brown default: 6107d8af6f5fSLen Brown help(); 6108d8af6f5fSLen Brown exit(1); 6109d8af6f5fSLen Brown case 'i': 61102a0609c0SLen Brown { 61112a0609c0SLen Brown double interval = strtod(optarg, NULL); 61122a0609c0SLen Brown 61132a0609c0SLen Brown if (interval < 0.001) { 6114b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 61152a0609c0SLen Brown interval); 61162a0609c0SLen Brown exit(2); 61172a0609c0SLen Brown } 61182a0609c0SLen Brown 611947936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6120b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 612147936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 61222a0609c0SLen Brown } 6123889facbeSLen Brown break; 61245c56be9aSDirk Brandewie case 'J': 61255c56be9aSDirk Brandewie rapl_joules++; 61265c56be9aSDirk Brandewie break; 6127c8ade361SLen Brown case 'l': 61283f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6129c8ade361SLen Brown list_header_only++; 6130c8ade361SLen Brown quiet++; 6131c8ade361SLen Brown break; 6132b7d8c148SLen Brown case 'o': 6133b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6134b7d8c148SLen Brown break; 613596e47158SLen Brown case 'q': 613696e47158SLen Brown quiet = 1; 613796e47158SLen Brown break; 6138023fe0acSChen Yu case 'n': 6139023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6140023fe0acSChen Yu 6141023fe0acSChen Yu if (num_iterations <= 0) { 6142023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 6143023fe0acSChen Yu num_iterations); 6144023fe0acSChen Yu exit(2); 6145023fe0acSChen Yu } 6146023fe0acSChen Yu break; 6147812db3f7SLen Brown case 's': 61483f44a5c6SLen Brown /* 61493f44a5c6SLen Brown * --show: show only those specified 61503f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 61513f44a5c6SLen Brown * subsequent invocations can add to it. 61523f44a5c6SLen Brown */ 61533f44a5c6SLen Brown if (shown == 0) 61543f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 61553f44a5c6SLen Brown else 61563f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 61573f44a5c6SLen Brown shown = 1; 6158812db3f7SLen Brown break; 6159d8af6f5fSLen Brown case 'S': 6160d8af6f5fSLen Brown summary_only++; 6161d8af6f5fSLen Brown break; 6162d8af6f5fSLen Brown case 'T': 6163d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 6164d8af6f5fSLen Brown break; 6165d8af6f5fSLen Brown case 'v': 6166d8af6f5fSLen Brown print_version(); 6167d8af6f5fSLen Brown exit(0); 6168d8af6f5fSLen Brown break; 6169103a8feaSLen Brown } 6170103a8feaSLen Brown } 6171103a8feaSLen Brown } 6172103a8feaSLen Brown 6173103a8feaSLen Brown int main(int argc, char **argv) 6174103a8feaSLen Brown { 6175b7d8c148SLen Brown outf = stderr; 6176103a8feaSLen Brown cmdline(argc, argv); 6177103a8feaSLen Brown 617896e47158SLen Brown if (!quiet) 6179d8af6f5fSLen Brown print_version(); 6180103a8feaSLen Brown 618141618e63SLen Brown probe_sysfs(); 618241618e63SLen Brown 6183103a8feaSLen Brown turbostat_init(); 6184103a8feaSLen Brown 61853b4d5c7fSAndy Shevchenko /* dump counters and exit */ 61863b4d5c7fSAndy Shevchenko if (dump_only) 61873b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 61883b4d5c7fSAndy Shevchenko 6189c8ade361SLen Brown /* list header and exit */ 6190c8ade361SLen Brown if (list_header_only) { 6191c8ade361SLen Brown print_header(","); 6192c8ade361SLen Brown flush_output_stdout(); 6193c8ade361SLen Brown return 0; 6194c8ade361SLen Brown } 6195c8ade361SLen Brown 61969972d5d8SChen Yu msr_sum_record(); 6197103a8feaSLen Brown /* 6198103a8feaSLen Brown * if any params left, it must be a command to fork 6199103a8feaSLen Brown */ 6200103a8feaSLen Brown if (argc - optind) 6201103a8feaSLen Brown return fork_it(argv + optind); 6202103a8feaSLen Brown else 6203103a8feaSLen Brown turbostat_loop(); 6204103a8feaSLen Brown 6205103a8feaSLen Brown return 0; 6206103a8feaSLen Brown } 6207