1103a8feaSLen Brown /* 2103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 3103a8feaSLen Brown * on modern Intel turbo-capable processors. 4103a8feaSLen Brown * 5144b44b1SLen Brown * Copyright (c) 2013 Intel Corporation. 6103a8feaSLen Brown * Len Brown <len.brown@intel.com> 7103a8feaSLen Brown * 8103a8feaSLen Brown * This program is free software; you can redistribute it and/or modify it 9103a8feaSLen Brown * under the terms and conditions of the GNU General Public License, 10103a8feaSLen Brown * version 2, as published by the Free Software Foundation. 11103a8feaSLen Brown * 12103a8feaSLen Brown * This program is distributed in the hope it will be useful, but WITHOUT 13103a8feaSLen Brown * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14103a8feaSLen Brown * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15103a8feaSLen Brown * more details. 16103a8feaSLen Brown * 17103a8feaSLen Brown * You should have received a copy of the GNU General Public License along with 18103a8feaSLen Brown * this program; if not, write to the Free Software Foundation, Inc., 19103a8feaSLen Brown * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 20103a8feaSLen Brown */ 21103a8feaSLen Brown 2288c3281fSLen Brown #define _GNU_SOURCE 23b731f311SJosh Triplett #include MSRHEADER 24869ce69eSLen Brown #include INTEL_FAMILY_HEADER 2595aebc44SJosh Triplett #include <stdarg.h> 26103a8feaSLen Brown #include <stdio.h> 27b2c95d90SJosh Triplett #include <err.h> 28103a8feaSLen Brown #include <unistd.h> 29103a8feaSLen Brown #include <sys/types.h> 30103a8feaSLen Brown #include <sys/wait.h> 31103a8feaSLen Brown #include <sys/stat.h> 32103a8feaSLen Brown #include <sys/resource.h> 33103a8feaSLen Brown #include <fcntl.h> 34103a8feaSLen Brown #include <signal.h> 35103a8feaSLen Brown #include <sys/time.h> 36103a8feaSLen Brown #include <stdlib.h> 37d8af6f5fSLen Brown #include <getopt.h> 38103a8feaSLen Brown #include <dirent.h> 39103a8feaSLen Brown #include <string.h> 40103a8feaSLen Brown #include <ctype.h> 4188c3281fSLen Brown #include <sched.h> 422a0609c0SLen Brown #include <time.h> 432b92865eSJosh Triplett #include <cpuid.h> 4498481e79SLen Brown #include <linux/capability.h> 4598481e79SLen Brown #include <errno.h> 46103a8feaSLen Brown 47103a8feaSLen Brown char *proc_stat = "/proc/stat"; 48b7d8c148SLen Brown FILE *outf; 4936229897SLen Brown int *fd_percpu; 502a0609c0SLen Brown struct timespec interval_ts = {5, 0}; 51d8af6f5fSLen Brown unsigned int debug; 52d8af6f5fSLen Brown unsigned int rapl_joules; 53d8af6f5fSLen Brown unsigned int summary_only; 54d8af6f5fSLen Brown unsigned int dump_only; 55103a8feaSLen Brown unsigned int do_nhm_cstates; 56103a8feaSLen Brown unsigned int do_snb_cstates; 57fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 58ee7e38e3SLen Brown unsigned int do_pc2; 59ee7e38e3SLen Brown unsigned int do_pc3; 60ee7e38e3SLen Brown unsigned int do_pc6; 61ee7e38e3SLen Brown unsigned int do_pc7; 62ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10; 630b2bb692SLen Brown unsigned int do_skl_residency; 64144b44b1SLen Brown unsigned int do_slm_cstates; 65144b44b1SLen Brown unsigned int use_c1_residency_msr; 66103a8feaSLen Brown unsigned int has_aperf; 67889facbeSLen Brown unsigned int has_epb; 685a63426eSLen Brown unsigned int do_irtl_snb; 695a63426eSLen Brown unsigned int do_irtl_hsw; 70fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 71103a8feaSLen Brown unsigned int genuine_intel; 72103a8feaSLen Brown unsigned int has_invariant_tsc; 73d7899447SLen Brown unsigned int do_nhm_platform_info; 742f32edf1SLen Brown unsigned int extra_msr_offset32; 752f32edf1SLen Brown unsigned int extra_msr_offset64; 768e180f3cSLen Brown unsigned int extra_delta_offset32; 778e180f3cSLen Brown unsigned int extra_delta_offset64; 78b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 79562a2d37SLen Brown int do_irq = 1; 801ed51011SLen Brown int do_smi; 81103a8feaSLen Brown double bclk; 82a2b7b749SLen Brown double base_hz; 8321ed5574SLen Brown unsigned int has_base_hz; 84a2b7b749SLen Brown double tsc_tweak = 1.0; 85103a8feaSLen Brown unsigned int show_pkg; 86103a8feaSLen Brown unsigned int show_core; 87103a8feaSLen Brown unsigned int show_cpu; 88c98d5d94SLen Brown unsigned int show_pkg_only; 89c98d5d94SLen Brown unsigned int show_core_only; 90c98d5d94SLen Brown char *output_buffer, *outp; 91889facbeSLen Brown unsigned int do_rapl; 92889facbeSLen Brown unsigned int do_dts; 93889facbeSLen Brown unsigned int do_ptm; 94fdf676e5SLen Brown unsigned int do_gfx_rc6_ms; 95fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 9627d47356SLen Brown unsigned int do_gfx_mhz; 9727d47356SLen Brown unsigned int gfx_cur_mhz; 98889facbeSLen Brown unsigned int tcc_activation_temp; 99889facbeSLen Brown unsigned int tcc_activation_temp_override; 10040ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 10140ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 102889facbeSLen Brown double rapl_joule_counter_range; 1033a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 1043a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1053a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1068a5bdf41SLen Brown unsigned int crystal_hz; 1078a5bdf41SLen Brown unsigned long long tsc_hz; 1087ce7d5deSPrarit Bhargava int base_cpu; 10921ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1107f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1117f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1127f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1137f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1147f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1157f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 116889facbeSLen Brown 117889facbeSLen Brown #define RAPL_PKG (1 << 0) 118e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 119e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 120e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 121e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 122e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 123e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 124e6f9bb3cSLen Brown 125889facbeSLen Brown #define RAPL_DRAM (1 << 3) 126e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 127e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 128e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 129e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1300b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1310b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 132e6f9bb3cSLen Brown 1339148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 134e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1350b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 136e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 137e6f9bb3cSLen Brown 1380b2bb692SLen Brown #define RAPL_GFX (1 << 8) 139e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 140e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 141e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1429148494cSJacob Pan 1439148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1449148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1459148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 146889facbeSLen Brown #define TJMAX_DEFAULT 100 147889facbeSLen Brown 148889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 149103a8feaSLen Brown 150103a8feaSLen Brown int backwards_count; 151103a8feaSLen Brown char *progname; 152103a8feaSLen Brown 153c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 154c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 155c98d5d94SLen Brown 156c98d5d94SLen Brown struct thread_data { 157c98d5d94SLen Brown unsigned long long tsc; 158c98d5d94SLen Brown unsigned long long aperf; 159c98d5d94SLen Brown unsigned long long mperf; 160144b44b1SLen Brown unsigned long long c1; 1612f32edf1SLen Brown unsigned long long extra_msr64; 1628e180f3cSLen Brown unsigned long long extra_delta64; 1638e180f3cSLen Brown unsigned long long extra_msr32; 1648e180f3cSLen Brown unsigned long long extra_delta32; 165562a2d37SLen Brown unsigned int irq_count; 1661ed51011SLen Brown unsigned int smi_count; 167c98d5d94SLen Brown unsigned int cpu_id; 168c98d5d94SLen Brown unsigned int flags; 169c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 170c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 171c98d5d94SLen Brown } *thread_even, *thread_odd; 172c98d5d94SLen Brown 173c98d5d94SLen Brown struct core_data { 174c98d5d94SLen Brown unsigned long long c3; 175c98d5d94SLen Brown unsigned long long c6; 176c98d5d94SLen Brown unsigned long long c7; 177889facbeSLen Brown unsigned int core_temp_c; 178c98d5d94SLen Brown unsigned int core_id; 179c98d5d94SLen Brown } *core_even, *core_odd; 180c98d5d94SLen Brown 181c98d5d94SLen Brown struct pkg_data { 182c98d5d94SLen Brown unsigned long long pc2; 183c98d5d94SLen Brown unsigned long long pc3; 184c98d5d94SLen Brown unsigned long long pc6; 185c98d5d94SLen Brown unsigned long long pc7; 186ca58710fSKristen Carlson Accardi unsigned long long pc8; 187ca58710fSKristen Carlson Accardi unsigned long long pc9; 188ca58710fSKristen Carlson Accardi unsigned long long pc10; 1890b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1900b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1910b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1920b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 1939185e988SLen Brown long long gfx_rc6_ms; 19427d47356SLen Brown unsigned int gfx_mhz; 195c98d5d94SLen Brown unsigned int package_id; 196889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 197889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 198889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 199889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 200889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 201889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 202889facbeSLen Brown unsigned int pkg_temp_c; 203889facbeSLen Brown 204c98d5d94SLen Brown } *package_even, *package_odd; 205c98d5d94SLen Brown 206c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 207c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 208c98d5d94SLen Brown 209c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 210c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 211c98d5d94SLen Brown topo.num_threads_per_core + \ 212c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 213c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 214c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 215c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 216c98d5d94SLen Brown 217c98d5d94SLen Brown struct system_summary { 218c98d5d94SLen Brown struct thread_data threads; 219c98d5d94SLen Brown struct core_data cores; 220c98d5d94SLen Brown struct pkg_data packages; 221c98d5d94SLen Brown } sum, average; 222c98d5d94SLen Brown 223c98d5d94SLen Brown 224c98d5d94SLen Brown struct topo_params { 225c98d5d94SLen Brown int num_packages; 226103a8feaSLen Brown int num_cpus; 227c98d5d94SLen Brown int num_cores; 228c98d5d94SLen Brown int max_cpu_num; 229c98d5d94SLen Brown int num_cores_per_pkg; 230c98d5d94SLen Brown int num_threads_per_core; 231c98d5d94SLen Brown } topo; 232103a8feaSLen Brown 233c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 234103a8feaSLen Brown 235562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 236562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 237562a2d37SLen Brown 238c98d5d94SLen Brown void setup_all_buffers(void); 239103a8feaSLen Brown 240c98d5d94SLen Brown int cpu_is_not_present(int cpu) 241d15cf7c1SLen Brown { 242c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 243c98d5d94SLen Brown } 244c98d5d94SLen Brown /* 245c98d5d94SLen Brown * run func(thread, core, package) in topology order 246c98d5d94SLen Brown * skip non-present cpus 247c98d5d94SLen Brown */ 248c98d5d94SLen Brown 249c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 250c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 251c98d5d94SLen Brown { 252c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 253c98d5d94SLen Brown 254c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 255c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 256c98d5d94SLen Brown for (thread_no = 0; thread_no < 257c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 258c98d5d94SLen Brown struct thread_data *t; 259c98d5d94SLen Brown struct core_data *c; 260c98d5d94SLen Brown struct pkg_data *p; 261c98d5d94SLen Brown 262c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 263c98d5d94SLen Brown 264c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 265c98d5d94SLen Brown continue; 266c98d5d94SLen Brown 267c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 268c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 269c98d5d94SLen Brown 270c98d5d94SLen Brown retval = func(t, c, p); 271c98d5d94SLen Brown if (retval) 272c98d5d94SLen Brown return retval; 273c98d5d94SLen Brown } 274c98d5d94SLen Brown } 275c98d5d94SLen Brown } 276d15cf7c1SLen Brown return 0; 277d15cf7c1SLen Brown } 278d15cf7c1SLen Brown 27988c3281fSLen Brown int cpu_migrate(int cpu) 28088c3281fSLen Brown { 281c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 282c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 283c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 28488c3281fSLen Brown return -1; 28588c3281fSLen Brown else 28688c3281fSLen Brown return 0; 28788c3281fSLen Brown } 28836229897SLen Brown int get_msr_fd(int cpu) 289103a8feaSLen Brown { 290103a8feaSLen Brown char pathname[32]; 291103a8feaSLen Brown int fd; 292103a8feaSLen Brown 29336229897SLen Brown fd = fd_percpu[cpu]; 29436229897SLen Brown 29536229897SLen Brown if (fd) 29636229897SLen Brown return fd; 29736229897SLen Brown 298103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 299103a8feaSLen Brown fd = open(pathname, O_RDONLY); 30015aaa346SLen Brown if (fd < 0) 30198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 302103a8feaSLen Brown 30336229897SLen Brown fd_percpu[cpu] = fd; 30436229897SLen Brown 30536229897SLen Brown return fd; 30636229897SLen Brown } 30736229897SLen Brown 30836229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 30936229897SLen Brown { 31036229897SLen Brown ssize_t retval; 31136229897SLen Brown 31236229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 31315aaa346SLen Brown 31498481e79SLen Brown if (retval != sizeof *msr) 31536229897SLen Brown err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset); 31615aaa346SLen Brown 31715aaa346SLen Brown return 0; 318103a8feaSLen Brown } 319103a8feaSLen Brown 320fc04cc67SLen Brown /* 321fc04cc67SLen Brown * Example Format w/ field column widths: 322fc04cc67SLen Brown * 32327d47356SLen Brown * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt 324562a2d37SLen Brown * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 325fc04cc67SLen Brown */ 326fc04cc67SLen Brown 327a829eb4dSLen Brown void print_header(void) 328103a8feaSLen Brown { 329103a8feaSLen Brown if (show_pkg) 3303d109de2SLen Brown outp += sprintf(outp, "\tPackage"); 331103a8feaSLen Brown if (show_core) 3323d109de2SLen Brown outp += sprintf(outp, "\tCore"); 333103a8feaSLen Brown if (show_cpu) 3343d109de2SLen Brown outp += sprintf(outp, "\tCPU"); 335103a8feaSLen Brown if (has_aperf) 3363d109de2SLen Brown outp += sprintf(outp, "\tAvg_MHz"); 337d7899447SLen Brown if (has_aperf) 3383d109de2SLen Brown outp += sprintf(outp, "\tBusy%%"); 339fc04cc67SLen Brown if (has_aperf) 3403d109de2SLen Brown outp += sprintf(outp, "\tBzy_MHz"); 3413d109de2SLen Brown outp += sprintf(outp, "\tTSC_MHz"); 3421cc21f7bSLen Brown 3438e180f3cSLen Brown if (extra_delta_offset32) 3443d109de2SLen Brown outp += sprintf(outp, "\tcount 0x%03X", extra_delta_offset32); 3458e180f3cSLen Brown if (extra_delta_offset64) 3463d109de2SLen Brown outp += sprintf(outp, "\tCOUNT 0x%03X", extra_delta_offset64); 3472f32edf1SLen Brown if (extra_msr_offset32) 3483d109de2SLen Brown outp += sprintf(outp, "\tMSR 0x%03X", extra_msr_offset32); 3492f32edf1SLen Brown if (extra_msr_offset64) 3503d109de2SLen Brown outp += sprintf(outp, "\tMSR 0x%03X", extra_msr_offset64); 3511cc21f7bSLen Brown 3521cc21f7bSLen Brown if (!debug) 3531cc21f7bSLen Brown goto done; 3541cc21f7bSLen Brown 355562a2d37SLen Brown if (do_irq) 3563d109de2SLen Brown outp += sprintf(outp, "\tIRQ"); 3571cc21f7bSLen Brown if (do_smi) 3583d109de2SLen Brown outp += sprintf(outp, "\tSMI"); 3591cc21f7bSLen Brown 360103a8feaSLen Brown if (do_nhm_cstates) 3613d109de2SLen Brown outp += sprintf(outp, "\tCPU%%c1"); 362fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 3633d109de2SLen Brown outp += sprintf(outp, "\tCPU%%c3"); 364103a8feaSLen Brown if (do_nhm_cstates) 3653d109de2SLen Brown outp += sprintf(outp, "\tCPU%%c6"); 366103a8feaSLen Brown if (do_snb_cstates) 3673d109de2SLen Brown outp += sprintf(outp, "\tCPU%%c7"); 368889facbeSLen Brown 369889facbeSLen Brown if (do_dts) 3703d109de2SLen Brown outp += sprintf(outp, "\tCoreTmp"); 371889facbeSLen Brown if (do_ptm) 3723d109de2SLen Brown outp += sprintf(outp, "\tPkgTmp"); 373889facbeSLen Brown 374fdf676e5SLen Brown if (do_gfx_rc6_ms) 3753d109de2SLen Brown outp += sprintf(outp, "\tGFX%%rc6"); 376fdf676e5SLen Brown 37727d47356SLen Brown if (do_gfx_mhz) 3783d109de2SLen Brown outp += sprintf(outp, "\tGFXMHz"); 37927d47356SLen Brown 3800b2bb692SLen Brown if (do_skl_residency) { 3813d109de2SLen Brown outp += sprintf(outp, "\tTotl%%C0"); 3823d109de2SLen Brown outp += sprintf(outp, "\tAny%%C0"); 3833d109de2SLen Brown outp += sprintf(outp, "\tGFX%%C0"); 3843d109de2SLen Brown outp += sprintf(outp, "\tCPUGFX%%"); 3850b2bb692SLen Brown } 3860b2bb692SLen Brown 387ee7e38e3SLen Brown if (do_pc2) 3883d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc2"); 389ee7e38e3SLen Brown if (do_pc3) 3903d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc3"); 391ee7e38e3SLen Brown if (do_pc6) 3923d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc6"); 393ee7e38e3SLen Brown if (do_pc7) 3943d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc7"); 395ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 3963d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc8"); 3973d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc9"); 3983d109de2SLen Brown outp += sprintf(outp, "\tPk%%pc10"); 399ca58710fSKristen Carlson Accardi } 400103a8feaSLen Brown 4015c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 402889facbeSLen Brown if (do_rapl & RAPL_PKG) 4033d109de2SLen Brown outp += sprintf(outp, "\tPkgWatt"); 4049148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) 4053d109de2SLen Brown outp += sprintf(outp, "\tCorWatt"); 406889facbeSLen Brown if (do_rapl & RAPL_GFX) 4073d109de2SLen Brown outp += sprintf(outp, "\tGFXWatt"); 408889facbeSLen Brown if (do_rapl & RAPL_DRAM) 4093d109de2SLen Brown outp += sprintf(outp, "\tRAMWatt"); 410889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 4113d109de2SLen Brown outp += sprintf(outp, "\tPKG_%%"); 412889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 4133d109de2SLen Brown outp += sprintf(outp, "\tRAM_%%"); 414d7899447SLen Brown } else if (do_rapl && rapl_joules) { 4155c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 4163d109de2SLen Brown outp += sprintf(outp, "\tPkg_J"); 4179148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) 4183d109de2SLen Brown outp += sprintf(outp, "\tCor_J"); 4195c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 4203d109de2SLen Brown outp += sprintf(outp, "\tGFX_J"); 4215c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 4223d109de2SLen Brown outp += sprintf(outp, "\tRAM_J"); 4235c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 4243d109de2SLen Brown outp += sprintf(outp, "\tPKG_%%"); 4255c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 4263d109de2SLen Brown outp += sprintf(outp, "\tRAM_%%"); 4275c56be9aSDirk Brandewie } 4281cc21f7bSLen Brown done: 429c98d5d94SLen Brown outp += sprintf(outp, "\n"); 430103a8feaSLen Brown } 431103a8feaSLen Brown 432c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 433c98d5d94SLen Brown struct pkg_data *p) 434103a8feaSLen Brown { 4353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 436c98d5d94SLen Brown 437c98d5d94SLen Brown if (t) { 4383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 4393b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 4403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 4413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 4423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 4433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 4443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4458e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 4463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4478e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 4483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4492f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 4503b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4512f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 452562a2d37SLen Brown if (do_irq) 453562a2d37SLen Brown outp += sprintf(outp, "IRQ: %08X\n", t->irq_count); 4541ed51011SLen Brown if (do_smi) 4553b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 456103a8feaSLen Brown } 457103a8feaSLen Brown 458c98d5d94SLen Brown if (c) { 4593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 4603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 4613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 4623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 4633b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 464c98d5d94SLen Brown } 465103a8feaSLen Brown 466c98d5d94SLen Brown if (p) { 4673b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 4680b2bb692SLen Brown 4690b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 4700b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 4710b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 4720b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 4730b2bb692SLen Brown 4743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 475ee7e38e3SLen Brown if (do_pc3) 4763b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 477ee7e38e3SLen Brown if (do_pc6) 4783b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 479ee7e38e3SLen Brown if (do_pc7) 4803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 4813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 4823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 4833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 4843b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 4853b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 4863b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 4873b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 4883b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 4893b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 4903b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 4913b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 4923b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 493c98d5d94SLen Brown } 4943b4d5c7fSAndy Shevchenko 4953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 4963b4d5c7fSAndy Shevchenko 497c98d5d94SLen Brown return 0; 498103a8feaSLen Brown } 499103a8feaSLen Brown 500e23da037SLen Brown /* 501e23da037SLen Brown * column formatting convention & formats 502e23da037SLen Brown */ 503c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 504c98d5d94SLen Brown struct pkg_data *p) 505103a8feaSLen Brown { 506103a8feaSLen Brown double interval_float; 507fc04cc67SLen Brown char *fmt8; 508103a8feaSLen Brown 509c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 510c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 511c98d5d94SLen Brown return 0; 512c98d5d94SLen Brown 513c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 514c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 515c98d5d94SLen Brown return 0; 516c98d5d94SLen Brown 517103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 518103a8feaSLen Brown 519c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 520c98d5d94SLen Brown if (t == &average.threads) { 521103a8feaSLen Brown if (show_pkg) 5223d109de2SLen Brown outp += sprintf(outp, "\t-"); 523103a8feaSLen Brown if (show_core) 5243d109de2SLen Brown outp += sprintf(outp, "\t-"); 525103a8feaSLen Brown if (show_cpu) 5263d109de2SLen Brown outp += sprintf(outp, "\t-"); 527103a8feaSLen Brown } else { 528c98d5d94SLen Brown if (show_pkg) { 529c98d5d94SLen Brown if (p) 5303d109de2SLen Brown outp += sprintf(outp, "\t%d", p->package_id); 531c98d5d94SLen Brown else 5323d109de2SLen Brown outp += sprintf(outp, "\t-"); 533c98d5d94SLen Brown } 534c98d5d94SLen Brown if (show_core) { 535c98d5d94SLen Brown if (c) 5363d109de2SLen Brown outp += sprintf(outp, "\t%d", c->core_id); 537c98d5d94SLen Brown else 5383d109de2SLen Brown outp += sprintf(outp, "\t-"); 539c98d5d94SLen Brown } 540103a8feaSLen Brown if (show_cpu) 5413d109de2SLen Brown outp += sprintf(outp, "\t%d", t->cpu_id); 542103a8feaSLen Brown } 543fc04cc67SLen Brown 544d7899447SLen Brown /* Avg_MHz */ 545fc04cc67SLen Brown if (has_aperf) 5463d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 547fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 548fc04cc67SLen Brown 54975d2e44eSLen Brown /* Busy% */ 550ba3dec99SLen Brown if (has_aperf) 5513d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak); 552103a8feaSLen Brown 553d7899447SLen Brown /* Bzy_MHz */ 55421ed5574SLen Brown if (has_aperf) { 55521ed5574SLen Brown if (has_base_hz) 5563d109de2SLen Brown outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf); 55721ed5574SLen Brown else 5583d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 55921ed5574SLen Brown 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); 56021ed5574SLen Brown } 561103a8feaSLen Brown 562d7899447SLen Brown /* TSC_MHz */ 5633d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float); 564103a8feaSLen Brown 5658e180f3cSLen Brown /* delta */ 5668e180f3cSLen Brown if (extra_delta_offset32) 5673d109de2SLen Brown outp += sprintf(outp, "\t%11llu", t->extra_delta32); 5688e180f3cSLen Brown 5698e180f3cSLen Brown /* DELTA */ 5708e180f3cSLen Brown if (extra_delta_offset64) 5713d109de2SLen Brown outp += sprintf(outp, "\t%11llu", t->extra_delta64); 5722f32edf1SLen Brown /* msr */ 5732f32edf1SLen Brown if (extra_msr_offset32) 5743d109de2SLen Brown outp += sprintf(outp, "\t0x%08llx", t->extra_msr32); 5752f32edf1SLen Brown 576130ff304SLen Brown /* MSR */ 5772f32edf1SLen Brown if (extra_msr_offset64) 5783d109de2SLen Brown outp += sprintf(outp, "\t0x%016llx", t->extra_msr64); 579130ff304SLen Brown 5801cc21f7bSLen Brown if (!debug) 5811cc21f7bSLen Brown goto done; 5821cc21f7bSLen Brown 583562a2d37SLen Brown /* IRQ */ 584562a2d37SLen Brown if (do_irq) 5853d109de2SLen Brown outp += sprintf(outp, "\t%d", t->irq_count); 586562a2d37SLen Brown 5871cc21f7bSLen Brown /* SMI */ 5881cc21f7bSLen Brown if (do_smi) 5893d109de2SLen Brown outp += sprintf(outp, "\t%d", t->smi_count); 5901cc21f7bSLen Brown 591ba3dec99SLen Brown if (do_nhm_cstates) 5923d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc); 593c98d5d94SLen Brown 594c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 595c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 596c98d5d94SLen Brown goto done; 597c98d5d94SLen Brown 598fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 5993d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc); 600103a8feaSLen Brown if (do_nhm_cstates) 6013d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc); 602103a8feaSLen Brown if (do_snb_cstates) 6033d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc); 604c98d5d94SLen Brown 605889facbeSLen Brown if (do_dts) 6063d109de2SLen Brown outp += sprintf(outp, "\t%d", c->core_temp_c); 607889facbeSLen Brown 608c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 609c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 610c98d5d94SLen Brown goto done; 611c98d5d94SLen Brown 6120b2bb692SLen Brown /* PkgTmp */ 613889facbeSLen Brown if (do_ptm) 6143d109de2SLen Brown outp += sprintf(outp, "\t%d", p->pkg_temp_c); 615889facbeSLen Brown 616fdf676e5SLen Brown /* GFXrc6 */ 6179185e988SLen Brown if (do_gfx_rc6_ms) { 618ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 6193d109de2SLen Brown outp += sprintf(outp, "\t**.**"); 6209185e988SLen Brown } else { 6213d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 6229185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 6239185e988SLen Brown } 6249185e988SLen Brown } 625fdf676e5SLen Brown 62627d47356SLen Brown /* GFXMHz */ 62727d47356SLen Brown if (do_gfx_mhz) 6283d109de2SLen Brown outp += sprintf(outp, "\t%d", p->gfx_mhz); 62927d47356SLen Brown 6300b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 6310b2bb692SLen Brown if (do_skl_residency) { 6323d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc); 6333d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc); 6343d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc); 6353d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc); 6360b2bb692SLen Brown } 6370b2bb692SLen Brown 638ee7e38e3SLen Brown if (do_pc2) 6393d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc); 640ee7e38e3SLen Brown if (do_pc3) 6413d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc); 642ee7e38e3SLen Brown if (do_pc6) 6433d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc); 644ee7e38e3SLen Brown if (do_pc7) 6453d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc); 646ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 6473d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc); 6483d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc); 6493d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc); 650ca58710fSKristen Carlson Accardi } 651889facbeSLen Brown 652889facbeSLen Brown /* 653889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 654889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 655889facbeSLen Brown */ 656fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 6573d109de2SLen Brown fmt8 = "\t%.2f"; 658fc04cc67SLen Brown else 659fc04cc67SLen Brown fmt8 = "%6.0f**"; 660889facbeSLen Brown 6615c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 662889facbeSLen Brown if (do_rapl & RAPL_PKG) 663fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 6649148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) 665fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 666889facbeSLen Brown if (do_rapl & RAPL_GFX) 667fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 668889facbeSLen Brown if (do_rapl & RAPL_DRAM) 66940ee8e3bSAndrey Semin outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); 670889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 671fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 672889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 673fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 674d7899447SLen Brown } else if (do_rapl && rapl_joules) { 6755c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 676fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6775c56be9aSDirk Brandewie p->energy_pkg * rapl_energy_units); 6785c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 679fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6805c56be9aSDirk Brandewie p->energy_cores * rapl_energy_units); 6815c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 682fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6835c56be9aSDirk Brandewie p->energy_gfx * rapl_energy_units); 6845c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 685fc04cc67SLen Brown outp += sprintf(outp, fmt8, 68640ee8e3bSAndrey Semin p->energy_dram * rapl_dram_energy_units); 6875c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 688fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 6895c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 690fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 6915c56be9aSDirk Brandewie } 692c98d5d94SLen Brown done: 693c98d5d94SLen Brown outp += sprintf(outp, "\n"); 694c98d5d94SLen Brown 695c98d5d94SLen Brown return 0; 696103a8feaSLen Brown } 697103a8feaSLen Brown 698b7d8c148SLen Brown void flush_output_stdout(void) 699103a8feaSLen Brown { 700b7d8c148SLen Brown FILE *filep; 701b7d8c148SLen Brown 702b7d8c148SLen Brown if (outf == stderr) 703b7d8c148SLen Brown filep = stdout; 704b7d8c148SLen Brown else 705b7d8c148SLen Brown filep = outf; 706b7d8c148SLen Brown 707b7d8c148SLen Brown fputs(output_buffer, filep); 708b7d8c148SLen Brown fflush(filep); 709b7d8c148SLen Brown 710c98d5d94SLen Brown outp = output_buffer; 711c98d5d94SLen Brown } 712b7d8c148SLen Brown void flush_output_stderr(void) 713c98d5d94SLen Brown { 714b7d8c148SLen Brown fputs(output_buffer, outf); 715b7d8c148SLen Brown fflush(outf); 716c98d5d94SLen Brown outp = output_buffer; 717c98d5d94SLen Brown } 718c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 719c98d5d94SLen Brown { 720e23da037SLen Brown static int printed; 721103a8feaSLen Brown 722e23da037SLen Brown if (!printed || !summary_only) 723103a8feaSLen Brown print_header(); 724103a8feaSLen Brown 725c98d5d94SLen Brown if (topo.num_cpus > 1) 726c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 727c98d5d94SLen Brown &average.packages); 728103a8feaSLen Brown 729e23da037SLen Brown printed = 1; 730e23da037SLen Brown 731e23da037SLen Brown if (summary_only) 732e23da037SLen Brown return; 733e23da037SLen Brown 734c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 735103a8feaSLen Brown } 736103a8feaSLen Brown 737889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 738889facbeSLen Brown if (new > old) { \ 739889facbeSLen Brown old = new - old; \ 740889facbeSLen Brown } else { \ 741889facbeSLen Brown old = 0x100000000 + new - old; \ 742889facbeSLen Brown } 743889facbeSLen Brown 744ba3dec99SLen Brown int 745c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 746103a8feaSLen Brown { 7470b2bb692SLen Brown 7480b2bb692SLen Brown if (do_skl_residency) { 7490b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 7500b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 7510b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 7520b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 7530b2bb692SLen Brown } 754c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 755ee7e38e3SLen Brown if (do_pc3) 756c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 757ee7e38e3SLen Brown if (do_pc6) 758c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 759ee7e38e3SLen Brown if (do_pc7) 760c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 761ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 762ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 763ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 764889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 765889facbeSLen Brown 7669185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 7679185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 7689185e988SLen Brown old->gfx_rc6_ms = -1; 7699185e988SLen Brown else 770fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 7719185e988SLen Brown 77227d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 77327d47356SLen Brown 774889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 775889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 776889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 777889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 778889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 779889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 780ba3dec99SLen Brown 781ba3dec99SLen Brown return 0; 782103a8feaSLen Brown } 783103a8feaSLen Brown 784c98d5d94SLen Brown void 785c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 786c98d5d94SLen Brown { 787c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 788c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 789c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 790889facbeSLen Brown old->core_temp_c = new->core_temp_c; 791103a8feaSLen Brown } 792c98d5d94SLen Brown 793c3ae331dSLen Brown /* 794c3ae331dSLen Brown * old = new - old 795c3ae331dSLen Brown */ 796ba3dec99SLen Brown int 797c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 798c98d5d94SLen Brown struct core_data *core_delta) 799c98d5d94SLen Brown { 800c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 801c98d5d94SLen Brown 802103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 803b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 804b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 805b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 806b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 807103a8feaSLen Brown 808c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 809c98d5d94SLen Brown 810a729617cSLen Brown if (has_aperf) { 811c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 812c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 813c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 814c98d5d94SLen Brown } else { 815ba3dec99SLen Brown return -1; 816103a8feaSLen Brown } 817a729617cSLen Brown } 818103a8feaSLen Brown 819c98d5d94SLen Brown 820144b44b1SLen Brown if (use_c1_residency_msr) { 821144b44b1SLen Brown /* 822144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 823144b44b1SLen Brown * which should be more accurate than the derivation below. 824144b44b1SLen Brown */ 825144b44b1SLen Brown } else { 826103a8feaSLen Brown /* 827c3ae331dSLen Brown * As counter collection is not atomic, 828c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 829103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 830103a8feaSLen Brown */ 831c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 832c98d5d94SLen Brown old->c1 = 0; 833c98d5d94SLen Brown else { 834c98d5d94SLen Brown /* normal case, derive c1 */ 835c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 836c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 837c98d5d94SLen Brown } 838144b44b1SLen Brown } 839c3ae331dSLen Brown 840c98d5d94SLen Brown if (old->mperf == 0) { 841b7d8c148SLen Brown if (debug > 1) 842b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 843c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 844c98d5d94SLen Brown } 845103a8feaSLen Brown 8468e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 8478e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 8488e180f3cSLen Brown 8498e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 8508e180f3cSLen Brown 851103a8feaSLen Brown /* 8528e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 853103a8feaSLen Brown */ 8542f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 8552f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 8561ed51011SLen Brown 857562a2d37SLen Brown if (do_irq) 858562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 859562a2d37SLen Brown 8601ed51011SLen Brown if (do_smi) 8611ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 862ba3dec99SLen Brown 863ba3dec99SLen Brown return 0; 864103a8feaSLen Brown } 865c98d5d94SLen Brown 866c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 867c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 868c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 869c98d5d94SLen Brown { 870ba3dec99SLen Brown int retval = 0; 871ba3dec99SLen Brown 872c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 873c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 874c98d5d94SLen Brown delta_core(c, c2); 875c98d5d94SLen Brown 876c98d5d94SLen Brown /* always calculate thread delta */ 877ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 878ba3dec99SLen Brown if (retval) 879ba3dec99SLen Brown return retval; 880c98d5d94SLen Brown 881c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 882c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 883ba3dec99SLen Brown retval = delta_package(p, p2); 884c98d5d94SLen Brown 885ba3dec99SLen Brown return retval; 886103a8feaSLen Brown } 887103a8feaSLen Brown 888c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 889103a8feaSLen Brown { 890c98d5d94SLen Brown t->tsc = 0; 891c98d5d94SLen Brown t->aperf = 0; 892c98d5d94SLen Brown t->mperf = 0; 893c98d5d94SLen Brown t->c1 = 0; 894103a8feaSLen Brown 8958e180f3cSLen Brown t->extra_delta32 = 0; 8968e180f3cSLen Brown t->extra_delta64 = 0; 8978e180f3cSLen Brown 898562a2d37SLen Brown t->irq_count = 0; 899562a2d37SLen Brown t->smi_count = 0; 900562a2d37SLen Brown 901c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 902c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 903c98d5d94SLen Brown 904c98d5d94SLen Brown c->c3 = 0; 905c98d5d94SLen Brown c->c6 = 0; 906c98d5d94SLen Brown c->c7 = 0; 907889facbeSLen Brown c->core_temp_c = 0; 908c98d5d94SLen Brown 9090b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 9100b2bb692SLen Brown p->pkg_any_core_c0 = 0; 9110b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 9120b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 9130b2bb692SLen Brown 914c98d5d94SLen Brown p->pc2 = 0; 915ee7e38e3SLen Brown if (do_pc3) 916c98d5d94SLen Brown p->pc3 = 0; 917ee7e38e3SLen Brown if (do_pc6) 918c98d5d94SLen Brown p->pc6 = 0; 919ee7e38e3SLen Brown if (do_pc7) 920c98d5d94SLen Brown p->pc7 = 0; 921ca58710fSKristen Carlson Accardi p->pc8 = 0; 922ca58710fSKristen Carlson Accardi p->pc9 = 0; 923ca58710fSKristen Carlson Accardi p->pc10 = 0; 924889facbeSLen Brown 925889facbeSLen Brown p->energy_pkg = 0; 926889facbeSLen Brown p->energy_dram = 0; 927889facbeSLen Brown p->energy_cores = 0; 928889facbeSLen Brown p->energy_gfx = 0; 929889facbeSLen Brown p->rapl_pkg_perf_status = 0; 930889facbeSLen Brown p->rapl_dram_perf_status = 0; 931889facbeSLen Brown p->pkg_temp_c = 0; 93227d47356SLen Brown 933fdf676e5SLen Brown p->gfx_rc6_ms = 0; 93427d47356SLen Brown p->gfx_mhz = 0; 935103a8feaSLen Brown } 936c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 937c98d5d94SLen Brown struct pkg_data *p) 938103a8feaSLen Brown { 939c98d5d94SLen Brown average.threads.tsc += t->tsc; 940c98d5d94SLen Brown average.threads.aperf += t->aperf; 941c98d5d94SLen Brown average.threads.mperf += t->mperf; 942c98d5d94SLen Brown average.threads.c1 += t->c1; 94315aaa346SLen Brown 9448e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 9458e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 9468e180f3cSLen Brown 947562a2d37SLen Brown average.threads.irq_count += t->irq_count; 948562a2d37SLen Brown average.threads.smi_count += t->smi_count; 949562a2d37SLen Brown 950c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 951c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 952c98d5d94SLen Brown return 0; 953c98d5d94SLen Brown 954c98d5d94SLen Brown average.cores.c3 += c->c3; 955c98d5d94SLen Brown average.cores.c6 += c->c6; 956c98d5d94SLen Brown average.cores.c7 += c->c7; 957c98d5d94SLen Brown 958889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 959889facbeSLen Brown 960c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 961c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 962c98d5d94SLen Brown return 0; 963c98d5d94SLen Brown 9640b2bb692SLen Brown if (do_skl_residency) { 9650b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 9660b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 9670b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 9680b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 9690b2bb692SLen Brown } 9700b2bb692SLen Brown 971c98d5d94SLen Brown average.packages.pc2 += p->pc2; 972ee7e38e3SLen Brown if (do_pc3) 973c98d5d94SLen Brown average.packages.pc3 += p->pc3; 974ee7e38e3SLen Brown if (do_pc6) 975c98d5d94SLen Brown average.packages.pc6 += p->pc6; 976ee7e38e3SLen Brown if (do_pc7) 977c98d5d94SLen Brown average.packages.pc7 += p->pc7; 978ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 979ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 980ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 981c98d5d94SLen Brown 982889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 983889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 984889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 985889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 986889facbeSLen Brown 987fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 98827d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 98927d47356SLen Brown 990889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 991889facbeSLen Brown 992889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 993889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 994c98d5d94SLen Brown return 0; 995c98d5d94SLen Brown } 996c98d5d94SLen Brown /* 997c98d5d94SLen Brown * sum the counters for all cpus in the system 998c98d5d94SLen Brown * compute the weighted average 999c98d5d94SLen Brown */ 1000c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1001c98d5d94SLen Brown struct pkg_data *p) 1002c98d5d94SLen Brown { 1003c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1004c98d5d94SLen Brown 1005c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1006c98d5d94SLen Brown 1007c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1008c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1009c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1010c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1011c98d5d94SLen Brown 10128e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 10138e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 10148e180f3cSLen Brown 10158e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 10168e180f3cSLen Brown 1017c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1018c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1019c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 1020c98d5d94SLen Brown 10210b2bb692SLen Brown if (do_skl_residency) { 10220b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 10230b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 10240b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 10250b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 10260b2bb692SLen Brown } 10270b2bb692SLen Brown 1028c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 1029ee7e38e3SLen Brown if (do_pc3) 1030c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 1031ee7e38e3SLen Brown if (do_pc6) 1032c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 1033ee7e38e3SLen Brown if (do_pc7) 1034c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1035ca58710fSKristen Carlson Accardi 1036ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1037ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1038ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1039c98d5d94SLen Brown } 1040c98d5d94SLen Brown 1041c98d5d94SLen Brown static unsigned long long rdtsc(void) 1042c98d5d94SLen Brown { 1043c98d5d94SLen Brown unsigned int low, high; 1044c98d5d94SLen Brown 1045c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1046c98d5d94SLen Brown 1047c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1048c98d5d94SLen Brown } 1049c98d5d94SLen Brown 1050c98d5d94SLen Brown /* 1051c98d5d94SLen Brown * get_counters(...) 1052c98d5d94SLen Brown * migrate to cpu 1053c98d5d94SLen Brown * acquire and record local counters for that cpu 1054c98d5d94SLen Brown */ 1055c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1056c98d5d94SLen Brown { 1057c98d5d94SLen Brown int cpu = t->cpu_id; 1058889facbeSLen Brown unsigned long long msr; 10590102b067SLen Brown int aperf_mperf_retry_count = 0; 1060c98d5d94SLen Brown 1061e52966c0SLen Brown if (cpu_migrate(cpu)) { 1062b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 106315aaa346SLen Brown return -1; 1064e52966c0SLen Brown } 106515aaa346SLen Brown 10660102b067SLen Brown retry: 1067c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 106815aaa346SLen Brown 106915aaa346SLen Brown if (has_aperf) { 10700102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 10710102b067SLen Brown 10720102b067SLen Brown /* 10730102b067SLen Brown * The TSC, APERF and MPERF must be read together for 10740102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 10750102b067SLen Brown * 10760102b067SLen Brown * Unfortunately, APERF and MPERF are read by 10770102b067SLen Brown * individual system call, so delays may occur 10780102b067SLen Brown * between them. If the time to read them 10790102b067SLen Brown * varies by a large amount, we re-read them. 10800102b067SLen Brown */ 10810102b067SLen Brown 10820102b067SLen Brown /* 10830102b067SLen Brown * This initial dummy APERF read has been seen to 10840102b067SLen Brown * reduce jitter in the subsequent reads. 10850102b067SLen Brown */ 10860102b067SLen Brown 10879c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1088c98d5d94SLen Brown return -3; 10890102b067SLen Brown 10900102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 10910102b067SLen Brown 10920102b067SLen Brown tsc_before = t->tsc; 10930102b067SLen Brown 10940102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 10950102b067SLen Brown return -3; 10960102b067SLen Brown 10970102b067SLen Brown tsc_between = rdtsc(); 10980102b067SLen Brown 10999c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1100c98d5d94SLen Brown return -4; 11010102b067SLen Brown 11020102b067SLen Brown tsc_after = rdtsc(); 11030102b067SLen Brown 11040102b067SLen Brown aperf_time = tsc_between - tsc_before; 11050102b067SLen Brown mperf_time = tsc_after - tsc_between; 11060102b067SLen Brown 11070102b067SLen Brown /* 11080102b067SLen Brown * If the system call latency to read APERF and MPERF 11090102b067SLen Brown * differ by more than 2x, then try again. 11100102b067SLen Brown */ 11110102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 11120102b067SLen Brown aperf_mperf_retry_count++; 11130102b067SLen Brown if (aperf_mperf_retry_count < 5) 11140102b067SLen Brown goto retry; 11150102b067SLen Brown else 11160102b067SLen Brown warnx("cpu%d jitter %lld %lld", 11170102b067SLen Brown cpu, aperf_time, mperf_time); 11180102b067SLen Brown } 11190102b067SLen Brown aperf_mperf_retry_count = 0; 11200102b067SLen Brown 1121b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1122b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 112388c3281fSLen Brown } 112488c3281fSLen Brown 1125562a2d37SLen Brown if (do_irq) 1126562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 11271ed51011SLen Brown if (do_smi) { 11281ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 11291ed51011SLen Brown return -5; 11301ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 11311ed51011SLen Brown } 11328e180f3cSLen Brown if (extra_delta_offset32) { 1133889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 11342f32edf1SLen Brown return -5; 1135889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 11368e180f3cSLen Brown } 11378e180f3cSLen Brown 11388e180f3cSLen Brown if (extra_delta_offset64) 11398e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 11408e180f3cSLen Brown return -5; 11418e180f3cSLen Brown 11428e180f3cSLen Brown if (extra_msr_offset32) { 1143889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 11448e180f3cSLen Brown return -5; 1145889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 11468e180f3cSLen Brown } 11472f32edf1SLen Brown 11482f32edf1SLen Brown if (extra_msr_offset64) 11492f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 1150c98d5d94SLen Brown return -5; 1151c98d5d94SLen Brown 1152144b44b1SLen Brown if (use_c1_residency_msr) { 1153144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1154144b44b1SLen Brown return -6; 1155144b44b1SLen Brown } 1156144b44b1SLen Brown 1157c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1158c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1159c98d5d94SLen Brown return 0; 1160c98d5d94SLen Brown 1161fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) { 1162c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1163c98d5d94SLen Brown return -6; 1164144b44b1SLen Brown } 1165144b44b1SLen Brown 1166fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_knl_cstates) { 1167c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1168c98d5d94SLen Brown return -7; 1169fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1170fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1171fb5d4327SDasaratharaman Chandramouli return -7; 1172103a8feaSLen Brown } 117315aaa346SLen Brown 117415aaa346SLen Brown if (do_snb_cstates) 1175c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1176c98d5d94SLen Brown return -8; 1177c98d5d94SLen Brown 1178889facbeSLen Brown if (do_dts) { 1179889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1180889facbeSLen Brown return -9; 1181889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1182889facbeSLen Brown } 1183889facbeSLen Brown 1184889facbeSLen Brown 1185c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1186c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1187c98d5d94SLen Brown return 0; 118815aaa346SLen Brown 11890b2bb692SLen Brown if (do_skl_residency) { 11900b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 11910b2bb692SLen Brown return -10; 11920b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 11930b2bb692SLen Brown return -11; 11940b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 11950b2bb692SLen Brown return -12; 11960b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 11970b2bb692SLen Brown return -13; 11980b2bb692SLen Brown } 1199ee7e38e3SLen Brown if (do_pc3) 1200c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1201c98d5d94SLen Brown return -9; 1202ee7e38e3SLen Brown if (do_pc6) 1203c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1204c98d5d94SLen Brown return -10; 1205ee7e38e3SLen Brown if (do_pc2) 1206c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1207c98d5d94SLen Brown return -11; 1208ee7e38e3SLen Brown if (do_pc7) 1209c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1210c98d5d94SLen Brown return -12; 1211ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 1212ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1213ca58710fSKristen Carlson Accardi return -13; 1214ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1215ca58710fSKristen Carlson Accardi return -13; 1216ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1217ca58710fSKristen Carlson Accardi return -13; 1218ca58710fSKristen Carlson Accardi } 1219889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1220889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1221889facbeSLen Brown return -13; 1222889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1223889facbeSLen Brown } 12249148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1225889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1226889facbeSLen Brown return -14; 1227889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1228889facbeSLen Brown } 1229889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1230889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1231889facbeSLen Brown return -15; 1232889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1233889facbeSLen Brown } 1234889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1235889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1236889facbeSLen Brown return -16; 1237889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1238889facbeSLen Brown } 1239889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1240889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1241889facbeSLen Brown return -16; 1242889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1243889facbeSLen Brown } 1244889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1245889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1246889facbeSLen Brown return -16; 1247889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1248889facbeSLen Brown } 1249889facbeSLen Brown if (do_ptm) { 1250889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1251889facbeSLen Brown return -17; 1252889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1253889facbeSLen Brown } 1254fdf676e5SLen Brown 1255fdf676e5SLen Brown if (do_gfx_rc6_ms) 1256fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1257fdf676e5SLen Brown 125827d47356SLen Brown if (do_gfx_mhz) 125927d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 126027d47356SLen Brown 126115aaa346SLen Brown return 0; 1262103a8feaSLen Brown } 1263103a8feaSLen Brown 1264ee7e38e3SLen Brown /* 1265ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1266ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1267ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1268ee7e38e3SLen Brown */ 1269ee7e38e3SLen Brown 1270ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1271ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1272ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1273ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1274ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1275ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1276ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1277ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1278ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1279ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1280ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1281ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 12820b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 12830b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 12840b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1285ee7e38e3SLen Brown 1286ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1287ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 12880b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1289ee7e38e3SLen Brown 1290e9257f5fSLen 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}; 1291e9257f5fSLen 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}; 1292e9257f5fSLen 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}; 1293e9257f5fSLen 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, PCLRSV, PCLRSV}; 1294e9257f5fSLen Brown int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1295e9257f5fSLen 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}; 1296e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1297*d8ebb442SLen Brown int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1298ee7e38e3SLen Brown 1299a2b7b749SLen Brown 1300a2b7b749SLen Brown static void 1301a2b7b749SLen Brown calculate_tsc_tweak() 1302a2b7b749SLen Brown { 1303a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1304a2b7b749SLen Brown } 1305a2b7b749SLen Brown 1306fcd17211SLen Brown static void 1307fcd17211SLen Brown dump_nhm_platform_info(void) 1308103a8feaSLen Brown { 1309103a8feaSLen Brown unsigned long long msr; 1310103a8feaSLen Brown unsigned int ratio; 1311103a8feaSLen Brown 1312ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1313103a8feaSLen Brown 1314b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 13156574a5d5SLen Brown 1316103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1317b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n", 1318103a8feaSLen Brown ratio, bclk, ratio * bclk); 1319103a8feaSLen Brown 1320103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1321b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n", 1322103a8feaSLen Brown ratio, bclk, ratio * bclk); 1323103a8feaSLen Brown 13247ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1325b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1326bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 132767920418SLen Brown 1328fcd17211SLen Brown return; 1329fcd17211SLen Brown } 1330fcd17211SLen Brown 1331fcd17211SLen Brown static void 1332fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1333fcd17211SLen Brown { 1334fcd17211SLen Brown unsigned long long msr; 1335fcd17211SLen Brown unsigned int ratio; 1336fcd17211SLen Brown 13377ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1338fcd17211SLen Brown 1339b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1340fcd17211SLen Brown 1341fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1342fcd17211SLen Brown if (ratio) 1343b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n", 1344fcd17211SLen Brown ratio, bclk, ratio * bclk); 1345fcd17211SLen Brown 1346fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1347fcd17211SLen Brown if (ratio) 1348b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n", 1349fcd17211SLen Brown ratio, bclk, ratio * bclk); 1350fcd17211SLen Brown return; 1351fcd17211SLen Brown } 1352fcd17211SLen Brown 1353fcd17211SLen Brown static void 1354fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1355fcd17211SLen Brown { 1356fcd17211SLen Brown unsigned long long msr; 1357fcd17211SLen Brown unsigned int ratio; 13586574a5d5SLen Brown 13597ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 13606574a5d5SLen Brown 1361b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 13626574a5d5SLen Brown 13636574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 13646574a5d5SLen Brown if (ratio) 1365b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 13666574a5d5SLen Brown ratio, bclk, ratio * bclk); 13676574a5d5SLen Brown 13686574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 13696574a5d5SLen Brown if (ratio) 1370b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 13716574a5d5SLen Brown ratio, bclk, ratio * bclk); 13726574a5d5SLen Brown 13736574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 13746574a5d5SLen Brown if (ratio) 1375b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 13766574a5d5SLen Brown ratio, bclk, ratio * bclk); 13776574a5d5SLen Brown 13786574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 13796574a5d5SLen Brown if (ratio) 1380b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 13816574a5d5SLen Brown ratio, bclk, ratio * bclk); 13826574a5d5SLen Brown 13836574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 13846574a5d5SLen Brown if (ratio) 1385b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 13866574a5d5SLen Brown ratio, bclk, ratio * bclk); 13876574a5d5SLen Brown 13886574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 13896574a5d5SLen Brown if (ratio) 1390b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 13916574a5d5SLen Brown ratio, bclk, ratio * bclk); 13926574a5d5SLen Brown 13936574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 13946574a5d5SLen Brown if (ratio) 1395b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 13966574a5d5SLen Brown ratio, bclk, ratio * bclk); 13976574a5d5SLen Brown 13986574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 13996574a5d5SLen Brown if (ratio) 1400b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 14016574a5d5SLen Brown ratio, bclk, ratio * bclk); 1402103a8feaSLen Brown return; 1403fcd17211SLen Brown } 1404fcd17211SLen Brown 1405fcd17211SLen Brown static void 1406fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void) 1407fcd17211SLen Brown { 1408fcd17211SLen Brown unsigned long long msr; 1409fcd17211SLen Brown unsigned int ratio; 1410103a8feaSLen Brown 14117ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1412103a8feaSLen Brown 1413b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 14146574a5d5SLen Brown 14156574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 14166574a5d5SLen Brown if (ratio) 1417b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 14186574a5d5SLen Brown ratio, bclk, ratio * bclk); 14196574a5d5SLen Brown 14206574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 14216574a5d5SLen Brown if (ratio) 1422b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 14236574a5d5SLen Brown ratio, bclk, ratio * bclk); 14246574a5d5SLen Brown 14256574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 14266574a5d5SLen Brown if (ratio) 1427b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 14286574a5d5SLen Brown ratio, bclk, ratio * bclk); 14296574a5d5SLen Brown 14306574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 14316574a5d5SLen Brown if (ratio) 1432b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 14336574a5d5SLen Brown ratio, bclk, ratio * bclk); 14346574a5d5SLen Brown 1435103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1436103a8feaSLen Brown if (ratio) 1437b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1438103a8feaSLen Brown ratio, bclk, ratio * bclk); 1439103a8feaSLen Brown 1440103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1441103a8feaSLen Brown if (ratio) 1442b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1443103a8feaSLen Brown ratio, bclk, ratio * bclk); 1444103a8feaSLen Brown 1445103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1446103a8feaSLen Brown if (ratio) 1447b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1448103a8feaSLen Brown ratio, bclk, ratio * bclk); 1449103a8feaSLen Brown 1450103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1451103a8feaSLen Brown if (ratio) 1452b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1453103a8feaSLen Brown ratio, bclk, ratio * bclk); 1454fcd17211SLen Brown return; 1455fcd17211SLen Brown } 14563a9a941dSLen Brown 1457fcd17211SLen Brown static void 1458fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1459fb5d4327SDasaratharaman Chandramouli { 1460cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 1461cbf97abaSHubert Chrzaniuk 1462fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1463cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 1464cbf97abaSHubert Chrzaniuk int i, b_nr; 1465cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 1466cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 1467fb5d4327SDasaratharaman Chandramouli 1468ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1469fb5d4327SDasaratharaman Chandramouli 1470b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 1471bfae2052SLen Brown base_cpu, msr); 1472fb5d4327SDasaratharaman Chandramouli 1473fb5d4327SDasaratharaman Chandramouli /** 1474fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1475cbf97abaSHubert Chrzaniuk * [0] -- Reserved 1476cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 1477fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1478fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1479fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1480fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1481fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1482fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1483fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1484fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1485fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1486fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1487fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1488fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1489fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1490fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1491fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1492fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 1493fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 1494fb5d4327SDasaratharaman Chandramouli */ 1495fb5d4327SDasaratharaman Chandramouli 1496cbf97abaSHubert Chrzaniuk b_nr = 0; 1497cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 1498cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 1499cbf97abaSHubert Chrzaniuk 1500cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 1501fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 1502cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 1503fb5d4327SDasaratharaman Chandramouli 1504cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 1505cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 1506cbf97abaSHubert Chrzaniuk b_nr++; 1507fb5d4327SDasaratharaman Chandramouli } 1508cbf97abaSHubert Chrzaniuk 1509cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 1510cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 1511b7d8c148SLen Brown fprintf(outf, 1512fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1513cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 1514fb5d4327SDasaratharaman Chandramouli } 1515fb5d4327SDasaratharaman Chandramouli 1516fb5d4327SDasaratharaman Chandramouli static void 1517fcd17211SLen Brown dump_nhm_cst_cfg(void) 1518fcd17211SLen Brown { 1519fcd17211SLen Brown unsigned long long msr; 1520fcd17211SLen Brown 15217ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1522fcd17211SLen Brown 1523fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1524fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1525fcd17211SLen Brown 1526b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr); 1527fcd17211SLen Brown 1528b7d8c148SLen Brown fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1529fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1530fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1531fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1532fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1533fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 15346c34f160SLen Brown (unsigned int)msr & 0xF, 1535fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1536fcd17211SLen Brown return; 1537103a8feaSLen Brown } 1538103a8feaSLen Brown 15396fb3143bSLen Brown static void 15406fb3143bSLen Brown dump_config_tdp(void) 15416fb3143bSLen Brown { 15426fb3143bSLen Brown unsigned long long msr; 15436fb3143bSLen Brown 15446fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 1545b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 1546685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 15476fb3143bSLen Brown 15486fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 1549b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 15506fb3143bSLen Brown if (msr) { 1551685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 1552685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 1553685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 1554685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 15556fb3143bSLen Brown } 1556b7d8c148SLen Brown fprintf(outf, ")\n"); 15576fb3143bSLen Brown 15586fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 1559b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 15606fb3143bSLen Brown if (msr) { 1561685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 1562685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 1563685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 1564685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 15656fb3143bSLen Brown } 1566b7d8c148SLen Brown fprintf(outf, ")\n"); 15676fb3143bSLen Brown 15686fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 1569b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 15706fb3143bSLen Brown if ((msr) & 0x3) 1571b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 1572b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1573b7d8c148SLen Brown fprintf(outf, ")\n"); 15746fb3143bSLen Brown 15756fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 1576b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 1577685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 1578b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1579b7d8c148SLen Brown fprintf(outf, ")\n"); 15806fb3143bSLen Brown } 15815a63426eSLen Brown 15825a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 15835a63426eSLen Brown 15845a63426eSLen Brown void print_irtl(void) 15855a63426eSLen Brown { 15865a63426eSLen Brown unsigned long long msr; 15875a63426eSLen Brown 15885a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 15895a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 15905a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 15915a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 15925a63426eSLen Brown 15935a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 15945a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 15955a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 15965a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 15975a63426eSLen Brown 15985a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 15995a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 16005a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 16015a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 16025a63426eSLen Brown 16035a63426eSLen Brown if (!do_irtl_hsw) 16045a63426eSLen Brown return; 16055a63426eSLen Brown 16065a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 16075a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 16085a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 16095a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 16105a63426eSLen Brown 16115a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 16125a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 16135a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 16145a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 16155a63426eSLen Brown 16165a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 16175a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 16185a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 16195a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 16205a63426eSLen Brown 16215a63426eSLen Brown } 162236229897SLen Brown void free_fd_percpu(void) 162336229897SLen Brown { 162436229897SLen Brown int i; 162536229897SLen Brown 162601a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 162736229897SLen Brown if (fd_percpu[i] != 0) 162836229897SLen Brown close(fd_percpu[i]); 162936229897SLen Brown } 163036229897SLen Brown 163136229897SLen Brown free(fd_percpu); 16326fb3143bSLen Brown } 16336fb3143bSLen Brown 1634c98d5d94SLen Brown void free_all_buffers(void) 1635103a8feaSLen Brown { 1636c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1637c98d5d94SLen Brown cpu_present_set = NULL; 163836229897SLen Brown cpu_present_setsize = 0; 1639103a8feaSLen Brown 1640c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1641c98d5d94SLen Brown cpu_affinity_set = NULL; 1642c98d5d94SLen Brown cpu_affinity_setsize = 0; 1643103a8feaSLen Brown 1644c98d5d94SLen Brown free(thread_even); 1645c98d5d94SLen Brown free(core_even); 1646c98d5d94SLen Brown free(package_even); 1647103a8feaSLen Brown 1648c98d5d94SLen Brown thread_even = NULL; 1649c98d5d94SLen Brown core_even = NULL; 1650c98d5d94SLen Brown package_even = NULL; 1651103a8feaSLen Brown 1652c98d5d94SLen Brown free(thread_odd); 1653c98d5d94SLen Brown free(core_odd); 1654c98d5d94SLen Brown free(package_odd); 1655103a8feaSLen Brown 1656c98d5d94SLen Brown thread_odd = NULL; 1657c98d5d94SLen Brown core_odd = NULL; 1658c98d5d94SLen Brown package_odd = NULL; 1659103a8feaSLen Brown 1660c98d5d94SLen Brown free(output_buffer); 1661c98d5d94SLen Brown output_buffer = NULL; 1662c98d5d94SLen Brown outp = NULL; 166336229897SLen Brown 166436229897SLen Brown free_fd_percpu(); 1665562a2d37SLen Brown 1666562a2d37SLen Brown free(irq_column_2_cpu); 1667562a2d37SLen Brown free(irqs_per_cpu); 1668103a8feaSLen Brown } 1669103a8feaSLen Brown 1670103a8feaSLen Brown /* 167157a42a34SJosh Triplett * Open a file, and exit on failure 167257a42a34SJosh Triplett */ 167357a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 167457a42a34SJosh Triplett { 1675b7d8c148SLen Brown FILE *filep = fopen(path, mode); 1676b2c95d90SJosh Triplett if (!filep) 1677b2c95d90SJosh Triplett err(1, "%s: open failed", path); 167857a42a34SJosh Triplett return filep; 167957a42a34SJosh Triplett } 168057a42a34SJosh Triplett 168157a42a34SJosh Triplett /* 168295aebc44SJosh Triplett * Parse a file containing a single int. 168395aebc44SJosh Triplett */ 168495aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 168595aebc44SJosh Triplett { 168695aebc44SJosh Triplett va_list args; 168795aebc44SJosh Triplett char path[PATH_MAX]; 168895aebc44SJosh Triplett FILE *filep; 168995aebc44SJosh Triplett int value; 169095aebc44SJosh Triplett 169195aebc44SJosh Triplett va_start(args, fmt); 169295aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 169395aebc44SJosh Triplett va_end(args); 169457a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1695b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1696b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 169795aebc44SJosh Triplett fclose(filep); 169895aebc44SJosh Triplett return value; 169995aebc44SJosh Triplett } 170095aebc44SJosh Triplett 170195aebc44SJosh Triplett /* 1702e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 1703e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 1704e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 1705103a8feaSLen Brown */ 1706e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 1707103a8feaSLen Brown { 1708e275b388SDasaratharaman Chandramouli char path[64]; 1709e275b388SDasaratharaman Chandramouli FILE *filep; 1710e275b388SDasaratharaman Chandramouli int this_cpu; 1711e275b388SDasaratharaman Chandramouli char character; 1712e275b388SDasaratharaman Chandramouli int i; 1713e275b388SDasaratharaman Chandramouli 1714e275b388SDasaratharaman Chandramouli sprintf(path, 1715e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 1716e275b388SDasaratharaman Chandramouli cpu); 1717e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 1718e275b388SDasaratharaman Chandramouli if (filep == NULL) { 1719e275b388SDasaratharaman Chandramouli perror(path); 1720e275b388SDasaratharaman Chandramouli exit(1); 1721e275b388SDasaratharaman Chandramouli } 1722e275b388SDasaratharaman Chandramouli 1723e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 1724e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 1725e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 1726e275b388SDasaratharaman Chandramouli fclose(filep); 1727e275b388SDasaratharaman Chandramouli return i; 1728e275b388SDasaratharaman Chandramouli } 1729e275b388SDasaratharaman Chandramouli 1730e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 1731e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 1732e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 1733e275b388SDasaratharaman Chandramouli } 1734e275b388SDasaratharaman Chandramouli 1735e275b388SDasaratharaman Chandramouli fclose(filep); 1736e275b388SDasaratharaman Chandramouli return -1; 1737c98d5d94SLen Brown } 1738103a8feaSLen Brown 1739c98d5d94SLen Brown /* 1740c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1741c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1742c98d5d94SLen Brown */ 1743c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1744c98d5d94SLen Brown { 174595aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1746103a8feaSLen Brown } 1747103a8feaSLen Brown 1748103a8feaSLen Brown int get_physical_package_id(int cpu) 1749103a8feaSLen Brown { 175095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1751103a8feaSLen Brown } 1752103a8feaSLen Brown 1753103a8feaSLen Brown int get_core_id(int cpu) 1754103a8feaSLen Brown { 175595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1756103a8feaSLen Brown } 1757103a8feaSLen Brown 1758c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1759c98d5d94SLen Brown { 1760c98d5d94SLen Brown char path[80]; 1761c98d5d94SLen Brown FILE *filep; 1762e275b388SDasaratharaman Chandramouli int sib1; 1763e275b388SDasaratharaman Chandramouli int matches = 0; 1764c98d5d94SLen Brown char character; 1765e275b388SDasaratharaman Chandramouli char str[100]; 1766e275b388SDasaratharaman Chandramouli char *ch; 1767c98d5d94SLen Brown 1768c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 176957a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1770e275b388SDasaratharaman Chandramouli 1771103a8feaSLen Brown /* 1772c98d5d94SLen Brown * file format: 1773e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 1774e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 1775c98d5d94SLen Brown */ 1776e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 1777e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 1778e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 1779e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 1780e275b388SDasaratharaman Chandramouli while (ch != NULL) { 1781e275b388SDasaratharaman Chandramouli matches++; 1782e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 1783e275b388SDasaratharaman Chandramouli } 1784c98d5d94SLen Brown 1785c98d5d94SLen Brown fclose(filep); 1786e275b388SDasaratharaman Chandramouli return matches+1; 1787c98d5d94SLen Brown } 1788c98d5d94SLen Brown 1789c98d5d94SLen Brown /* 1790c98d5d94SLen Brown * run func(thread, core, package) in topology order 1791c98d5d94SLen Brown * skip non-present cpus 1792103a8feaSLen Brown */ 1793103a8feaSLen Brown 1794c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1795c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1796c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1797c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1798c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1799c98d5d94SLen Brown struct pkg_data *pkg_base2) 1800c98d5d94SLen Brown { 1801c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1802c98d5d94SLen Brown 1803c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1804c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1805c98d5d94SLen Brown for (thread_no = 0; thread_no < 1806c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1807c98d5d94SLen Brown struct thread_data *t, *t2; 1808c98d5d94SLen Brown struct core_data *c, *c2; 1809c98d5d94SLen Brown struct pkg_data *p, *p2; 1810c98d5d94SLen Brown 1811c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1812c98d5d94SLen Brown 1813c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1814c98d5d94SLen Brown continue; 1815c98d5d94SLen Brown 1816c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1817c98d5d94SLen Brown 1818c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1819c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1820c98d5d94SLen Brown 1821c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1822c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1823c98d5d94SLen Brown 1824c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1825c98d5d94SLen Brown if (retval) 1826c98d5d94SLen Brown return retval; 1827c98d5d94SLen Brown } 1828c98d5d94SLen Brown } 1829c98d5d94SLen Brown } 1830c98d5d94SLen Brown return 0; 1831c98d5d94SLen Brown } 1832c98d5d94SLen Brown 1833c98d5d94SLen Brown /* 1834c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1835c98d5d94SLen Brown * return max_cpu number 1836c98d5d94SLen Brown */ 1837c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1838103a8feaSLen Brown { 1839103a8feaSLen Brown FILE *fp; 1840c98d5d94SLen Brown int cpu_num; 1841103a8feaSLen Brown int retval; 1842103a8feaSLen Brown 184357a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1844103a8feaSLen Brown 1845103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1846b2c95d90SJosh Triplett if (retval != 0) 1847b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1848103a8feaSLen Brown 1849c98d5d94SLen Brown while (1) { 1850c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1851103a8feaSLen Brown if (retval != 1) 1852103a8feaSLen Brown break; 1853103a8feaSLen Brown 1854c98d5d94SLen Brown retval = func(cpu_num); 1855c98d5d94SLen Brown if (retval) { 1856c98d5d94SLen Brown fclose(fp); 1857c98d5d94SLen Brown return(retval); 1858c98d5d94SLen Brown } 1859103a8feaSLen Brown } 1860103a8feaSLen Brown fclose(fp); 1861c98d5d94SLen Brown return 0; 1862103a8feaSLen Brown } 1863103a8feaSLen Brown 1864103a8feaSLen Brown void re_initialize(void) 1865103a8feaSLen Brown { 1866c98d5d94SLen Brown free_all_buffers(); 1867c98d5d94SLen Brown setup_all_buffers(); 1868c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1869103a8feaSLen Brown } 1870103a8feaSLen Brown 1871c98d5d94SLen Brown 1872103a8feaSLen Brown /* 1873c98d5d94SLen Brown * count_cpus() 1874c98d5d94SLen Brown * remember the last one seen, it will be the max 1875103a8feaSLen Brown */ 1876c98d5d94SLen Brown int count_cpus(int cpu) 1877103a8feaSLen Brown { 1878c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1879c98d5d94SLen Brown topo.max_cpu_num = cpu; 1880103a8feaSLen Brown 1881c98d5d94SLen Brown topo.num_cpus += 1; 1882c98d5d94SLen Brown return 0; 1883103a8feaSLen Brown } 1884c98d5d94SLen Brown int mark_cpu_present(int cpu) 1885c98d5d94SLen Brown { 1886c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 188715aaa346SLen Brown return 0; 1888103a8feaSLen Brown } 1889103a8feaSLen Brown 1890562a2d37SLen Brown /* 1891562a2d37SLen Brown * snapshot_proc_interrupts() 1892562a2d37SLen Brown * 1893562a2d37SLen Brown * read and record summary of /proc/interrupts 1894562a2d37SLen Brown * 1895562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 1896562a2d37SLen Brown */ 1897562a2d37SLen Brown int snapshot_proc_interrupts(void) 1898562a2d37SLen Brown { 1899562a2d37SLen Brown static FILE *fp; 1900562a2d37SLen Brown int column, retval; 1901562a2d37SLen Brown 1902562a2d37SLen Brown if (fp == NULL) 1903562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 1904562a2d37SLen Brown else 1905562a2d37SLen Brown rewind(fp); 1906562a2d37SLen Brown 1907562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 1908562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 1909562a2d37SLen Brown int cpu_number; 1910562a2d37SLen Brown 1911562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 1912562a2d37SLen Brown if (retval != 1) 1913562a2d37SLen Brown break; 1914562a2d37SLen Brown 1915562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 1916562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 1917562a2d37SLen Brown return 1; 1918562a2d37SLen Brown } 1919562a2d37SLen Brown 1920562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 1921562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 1922562a2d37SLen Brown } 1923562a2d37SLen Brown 1924562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 1925562a2d37SLen Brown while (1) { 1926562a2d37SLen Brown int column; 1927562a2d37SLen Brown char buf[64]; 1928562a2d37SLen Brown 1929562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 1930562a2d37SLen Brown if (retval != 1) 1931562a2d37SLen Brown break; 1932562a2d37SLen Brown 1933562a2d37SLen Brown /* read the count per cpu */ 1934562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 1935562a2d37SLen Brown 1936562a2d37SLen Brown int cpu_number, irq_count; 1937562a2d37SLen Brown 1938562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 1939562a2d37SLen Brown if (retval != 1) 1940562a2d37SLen Brown break; 1941562a2d37SLen Brown 1942562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 1943562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 1944562a2d37SLen Brown 1945562a2d37SLen Brown } 1946562a2d37SLen Brown 1947562a2d37SLen Brown while (getc(fp) != '\n') 1948562a2d37SLen Brown ; /* flush interrupt description */ 1949562a2d37SLen Brown 1950562a2d37SLen Brown } 1951562a2d37SLen Brown return 0; 1952562a2d37SLen Brown } 195327d47356SLen Brown /* 1954fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 1955fdf676e5SLen Brown * 1956fdf676e5SLen Brown * record snapshot of 1957fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 1958fdf676e5SLen Brown * 1959fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 1960fdf676e5SLen Brown */ 1961fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 1962fdf676e5SLen Brown { 1963fdf676e5SLen Brown FILE *fp; 1964fdf676e5SLen Brown int retval; 1965fdf676e5SLen Brown 1966fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 1967fdf676e5SLen Brown 1968fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 1969fdf676e5SLen Brown if (retval != 1) 1970fdf676e5SLen Brown err(1, "GFX rc6"); 1971fdf676e5SLen Brown 1972fdf676e5SLen Brown fclose(fp); 1973fdf676e5SLen Brown 1974fdf676e5SLen Brown return 0; 1975fdf676e5SLen Brown } 1976fdf676e5SLen Brown /* 197727d47356SLen Brown * snapshot_gfx_mhz() 197827d47356SLen Brown * 197927d47356SLen Brown * record snapshot of 198027d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 198127d47356SLen Brown * 198227d47356SLen Brown * return 1 if config change requires a restart, else return 0 198327d47356SLen Brown */ 198427d47356SLen Brown int snapshot_gfx_mhz(void) 198527d47356SLen Brown { 198627d47356SLen Brown static FILE *fp; 198727d47356SLen Brown int retval; 198827d47356SLen Brown 198927d47356SLen Brown if (fp == NULL) 199027d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 199127d47356SLen Brown else 199227d47356SLen Brown rewind(fp); 199327d47356SLen Brown 199427d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 199527d47356SLen Brown if (retval != 1) 199627d47356SLen Brown err(1, "GFX MHz"); 199727d47356SLen Brown 199827d47356SLen Brown return 0; 199927d47356SLen Brown } 2000562a2d37SLen Brown 2001562a2d37SLen Brown /* 2002562a2d37SLen Brown * snapshot /proc and /sys files 2003562a2d37SLen Brown * 2004562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2005562a2d37SLen Brown */ 2006562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2007562a2d37SLen Brown { 2008562a2d37SLen Brown if (snapshot_proc_interrupts()) 2009562a2d37SLen Brown return 1; 2010562a2d37SLen Brown 2011fdf676e5SLen Brown if (do_gfx_rc6_ms) 2012fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2013fdf676e5SLen Brown 201427d47356SLen Brown if (do_gfx_mhz) 201527d47356SLen Brown snapshot_gfx_mhz(); 201627d47356SLen Brown 2017562a2d37SLen Brown return 0; 2018562a2d37SLen Brown } 2019562a2d37SLen Brown 2020103a8feaSLen Brown void turbostat_loop() 2021103a8feaSLen Brown { 2022c98d5d94SLen Brown int retval; 2023e52966c0SLen Brown int restarted = 0; 2024c98d5d94SLen Brown 2025103a8feaSLen Brown restart: 2026e52966c0SLen Brown restarted++; 2027e52966c0SLen Brown 2028562a2d37SLen Brown snapshot_proc_sysfs_files(); 2029c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2030d91bb17cSLen Brown if (retval < -1) { 2031d91bb17cSLen Brown exit(retval); 2032d91bb17cSLen Brown } else if (retval == -1) { 2033e52966c0SLen Brown if (restarted > 1) { 2034e52966c0SLen Brown exit(retval); 2035e52966c0SLen Brown } 2036c98d5d94SLen Brown re_initialize(); 2037c98d5d94SLen Brown goto restart; 2038c98d5d94SLen Brown } 2039e52966c0SLen Brown restarted = 0; 2040103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2041103a8feaSLen Brown 2042103a8feaSLen Brown while (1) { 2043c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2044103a8feaSLen Brown re_initialize(); 2045103a8feaSLen Brown goto restart; 2046103a8feaSLen Brown } 20472a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2048562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2049562a2d37SLen Brown goto restart; 2050c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2051d91bb17cSLen Brown if (retval < -1) { 2052d91bb17cSLen Brown exit(retval); 2053d91bb17cSLen Brown } else if (retval == -1) { 205415aaa346SLen Brown re_initialize(); 205515aaa346SLen Brown goto restart; 205615aaa346SLen Brown } 2057103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2058103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2059ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 2060ba3dec99SLen Brown re_initialize(); 2061ba3dec99SLen Brown goto restart; 2062ba3dec99SLen Brown } 2063c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2064c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2065b7d8c148SLen Brown flush_output_stdout(); 20662a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2067562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2068562a2d37SLen Brown goto restart; 2069c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2070d91bb17cSLen Brown if (retval < -1) { 2071d91bb17cSLen Brown exit(retval); 2072d91bb17cSLen Brown } else if (retval == -1) { 2073103a8feaSLen Brown re_initialize(); 2074103a8feaSLen Brown goto restart; 2075103a8feaSLen Brown } 2076103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2077103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 2078ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 2079ba3dec99SLen Brown re_initialize(); 2080ba3dec99SLen Brown goto restart; 2081ba3dec99SLen Brown } 2082c98d5d94SLen Brown compute_average(ODD_COUNTERS); 2083c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 2084b7d8c148SLen Brown flush_output_stdout(); 2085103a8feaSLen Brown } 2086103a8feaSLen Brown } 2087103a8feaSLen Brown 2088103a8feaSLen Brown void check_dev_msr() 2089103a8feaSLen Brown { 2090103a8feaSLen Brown struct stat sb; 20917ce7d5deSPrarit Bhargava char pathname[32]; 2092103a8feaSLen Brown 20937ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 20947ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 2095a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 2096d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 2097103a8feaSLen Brown } 2098103a8feaSLen Brown 209998481e79SLen Brown void check_permissions() 2100103a8feaSLen Brown { 210198481e79SLen Brown struct __user_cap_header_struct cap_header_data; 210298481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 210398481e79SLen Brown struct __user_cap_data_struct cap_data_data; 210498481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 210598481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 210698481e79SLen Brown int do_exit = 0; 21077ce7d5deSPrarit Bhargava char pathname[32]; 210898481e79SLen Brown 210998481e79SLen Brown /* check for CAP_SYS_RAWIO */ 211098481e79SLen Brown cap_header->pid = getpid(); 211198481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 211298481e79SLen Brown if (capget(cap_header, cap_data) < 0) 211398481e79SLen Brown err(-6, "capget(2) failed"); 211498481e79SLen Brown 211598481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 211698481e79SLen Brown do_exit++; 211798481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 211898481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 211998481e79SLen Brown } 212098481e79SLen Brown 212198481e79SLen Brown /* test file permissions */ 21227ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 21237ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 212498481e79SLen Brown do_exit++; 212598481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 212698481e79SLen Brown } 212798481e79SLen Brown 212898481e79SLen Brown /* if all else fails, thell them to be root */ 212998481e79SLen Brown if (do_exit) 2130b2c95d90SJosh Triplett if (getuid() != 0) 2131d7899447SLen Brown warnx("... or simply run as root"); 213298481e79SLen Brown 213398481e79SLen Brown if (do_exit) 213498481e79SLen Brown exit(-6); 2135103a8feaSLen Brown } 2136103a8feaSLen Brown 2137d7899447SLen Brown /* 2138d7899447SLen Brown * NHM adds support for additional MSRs: 2139d7899447SLen Brown * 2140d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 2141d7899447SLen Brown * 2142ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 2143d7899447SLen Brown * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 2144d7899447SLen Brown * 2145d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 2146d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 2147d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 2148d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 2149d7899447SLen Brown * 2150ee7e38e3SLen Brown * Side effect: 2151ee7e38e3SLen Brown * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL 2152d7899447SLen Brown */ 2153ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 2154103a8feaSLen Brown { 2155ee7e38e3SLen Brown unsigned long long msr; 215621ed5574SLen Brown unsigned int base_ratio; 2157ee7e38e3SLen Brown int *pkg_cstate_limits; 2158ee7e38e3SLen Brown 2159103a8feaSLen Brown if (!genuine_intel) 2160103a8feaSLen Brown return 0; 2161103a8feaSLen Brown 2162103a8feaSLen Brown if (family != 6) 2163103a8feaSLen Brown return 0; 2164103a8feaSLen Brown 216521ed5574SLen Brown bclk = discover_bclk(family, model); 216621ed5574SLen Brown 2167103a8feaSLen Brown switch (model) { 2168869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 2169869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 2170103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 2171869ce69eSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 2172869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 2173869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2174869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2175ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 2176ee7e38e3SLen Brown break; 2177869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 2178869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 2179869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2180869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2181ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 2182ee7e38e3SLen Brown break; 2183869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2184869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2185869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2186869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2187869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2188869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2189869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2190869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2191869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2192869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2193869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2194869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2195ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 2196ee7e38e3SLen Brown break; 2197*d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 2198*d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 2199*d8ebb442SLen Brown break; 2200869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 2201869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 2202ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 2203ee7e38e3SLen Brown break; 2204869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 2205ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 2206ee7e38e3SLen Brown break; 2207869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 2208005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2209ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 2210ee7e38e3SLen Brown break; 2211869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 2212869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 2213e4085d54SLen Brown pkg_cstate_limits = bxt_pkg_cstate_limits; 2214e4085d54SLen Brown break; 2215103a8feaSLen Brown default: 2216103a8feaSLen Brown return 0; 2217103a8feaSLen Brown } 22187ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 2219e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 2220ee7e38e3SLen Brown 2221ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 222221ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 222321ed5574SLen Brown 222421ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 222521ed5574SLen Brown has_base_hz = 1; 2226ee7e38e3SLen Brown return 1; 2227103a8feaSLen Brown } 2228d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) 2229d7899447SLen Brown { 2230d7899447SLen Brown switch (model) { 2231d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 2232869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2233869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2234869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 2235005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2236d7899447SLen Brown return 0; 2237d7899447SLen Brown default: 2238d7899447SLen Brown return 1; 2239d7899447SLen Brown } 2240d7899447SLen Brown } 22416574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 22426574a5d5SLen Brown { 22436574a5d5SLen Brown if (!genuine_intel) 22446574a5d5SLen Brown return 0; 22456574a5d5SLen Brown 22466574a5d5SLen Brown if (family != 6) 22476574a5d5SLen Brown return 0; 22486574a5d5SLen Brown 22496574a5d5SLen Brown switch (model) { 2250869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2251869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 22526574a5d5SLen Brown return 1; 22536574a5d5SLen Brown default: 22546574a5d5SLen Brown return 0; 22556574a5d5SLen Brown } 22566574a5d5SLen Brown } 2257fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 2258fcd17211SLen Brown { 2259fcd17211SLen Brown if (!genuine_intel) 2260fcd17211SLen Brown return 0; 2261fcd17211SLen Brown 2262fcd17211SLen Brown if (family != 6) 2263fcd17211SLen Brown return 0; 2264fcd17211SLen Brown 2265fcd17211SLen Brown switch (model) { 2266869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 2267fcd17211SLen Brown return 1; 2268fcd17211SLen Brown default: 2269fcd17211SLen Brown return 0; 2270fcd17211SLen Brown } 2271fcd17211SLen Brown } 2272fcd17211SLen Brown 2273fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 2274fb5d4327SDasaratharaman Chandramouli { 2275fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2276fb5d4327SDasaratharaman Chandramouli return 0; 2277fb5d4327SDasaratharaman Chandramouli 2278fb5d4327SDasaratharaman Chandramouli if (family != 6) 2279fb5d4327SDasaratharaman Chandramouli return 0; 2280fb5d4327SDasaratharaman Chandramouli 2281fb5d4327SDasaratharaman Chandramouli switch (model) { 2282869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2283005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2284fb5d4327SDasaratharaman Chandramouli return 1; 2285fb5d4327SDasaratharaman Chandramouli default: 2286fb5d4327SDasaratharaman Chandramouli return 0; 2287fb5d4327SDasaratharaman Chandramouli } 2288fb5d4327SDasaratharaman Chandramouli } 22896fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 22906fb3143bSLen Brown { 22916fb3143bSLen Brown if (!genuine_intel) 22926fb3143bSLen Brown return 0; 22936fb3143bSLen Brown 22946fb3143bSLen Brown if (family != 6) 22956fb3143bSLen Brown return 0; 22966fb3143bSLen Brown 22976fb3143bSLen Brown switch (model) { 2298869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2299869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2300869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2301869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2302869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2303869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2304869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2305869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2306869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2307869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2308869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2309869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2310869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2311869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 23126fb3143bSLen Brown 2313869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2314005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 23156fb3143bSLen Brown return 1; 23166fb3143bSLen Brown default: 23176fb3143bSLen Brown return 0; 23186fb3143bSLen Brown } 23196fb3143bSLen Brown } 23206fb3143bSLen Brown 2321fcd17211SLen Brown static void 23221b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 2323fcd17211SLen Brown { 2324fcd17211SLen Brown if (!do_nhm_platform_info) 2325fcd17211SLen Brown return; 2326fcd17211SLen Brown 2327fcd17211SLen Brown dump_nhm_platform_info(); 2328fcd17211SLen Brown 2329fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 2330fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 2331fcd17211SLen Brown 2332fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 2333fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 2334fcd17211SLen Brown 2335fcd17211SLen Brown if (has_nhm_turbo_ratio_limit(family, model)) 2336fcd17211SLen Brown dump_nhm_turbo_ratio_limits(); 2337fcd17211SLen Brown 2338fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 2339fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 2340fb5d4327SDasaratharaman Chandramouli 23416fb3143bSLen Brown if (has_config_tdp(family, model)) 23426fb3143bSLen Brown dump_config_tdp(); 23436fb3143bSLen Brown 2344fcd17211SLen Brown dump_nhm_cst_cfg(); 2345fcd17211SLen Brown } 2346fcd17211SLen Brown 23476574a5d5SLen Brown 2348889facbeSLen Brown /* 2349889facbeSLen Brown * print_epb() 2350889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 2351889facbeSLen Brown */ 2352889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2353889facbeSLen Brown { 2354889facbeSLen Brown unsigned long long msr; 2355889facbeSLen Brown char *epb_string; 2356889facbeSLen Brown int cpu; 2357889facbeSLen Brown 2358889facbeSLen Brown if (!has_epb) 2359889facbeSLen Brown return 0; 2360889facbeSLen Brown 2361889facbeSLen Brown cpu = t->cpu_id; 2362889facbeSLen Brown 2363889facbeSLen Brown /* EPB is per-package */ 2364889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2365889facbeSLen Brown return 0; 2366889facbeSLen Brown 2367889facbeSLen Brown if (cpu_migrate(cpu)) { 2368b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2369889facbeSLen Brown return -1; 2370889facbeSLen Brown } 2371889facbeSLen Brown 2372889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 2373889facbeSLen Brown return 0; 2374889facbeSLen Brown 2375e9be7dd6SLen Brown switch (msr & 0xF) { 2376889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 2377889facbeSLen Brown epb_string = "performance"; 2378889facbeSLen Brown break; 2379889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 2380889facbeSLen Brown epb_string = "balanced"; 2381889facbeSLen Brown break; 2382889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 2383889facbeSLen Brown epb_string = "powersave"; 2384889facbeSLen Brown break; 2385889facbeSLen Brown default: 2386889facbeSLen Brown epb_string = "custom"; 2387889facbeSLen Brown break; 2388889facbeSLen Brown } 2389b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 2390889facbeSLen Brown 2391889facbeSLen Brown return 0; 2392889facbeSLen Brown } 23937f5c258eSLen Brown /* 23947f5c258eSLen Brown * print_hwp() 23957f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 23967f5c258eSLen Brown */ 23977f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 23987f5c258eSLen Brown { 23997f5c258eSLen Brown unsigned long long msr; 24007f5c258eSLen Brown int cpu; 24017f5c258eSLen Brown 24027f5c258eSLen Brown if (!has_hwp) 24037f5c258eSLen Brown return 0; 24047f5c258eSLen Brown 24057f5c258eSLen Brown cpu = t->cpu_id; 24067f5c258eSLen Brown 24077f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 24087f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 24097f5c258eSLen Brown return 0; 24107f5c258eSLen Brown 24117f5c258eSLen Brown if (cpu_migrate(cpu)) { 2412b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 24137f5c258eSLen Brown return -1; 24147f5c258eSLen Brown } 24157f5c258eSLen Brown 24167f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 24177f5c258eSLen Brown return 0; 24187f5c258eSLen Brown 2419b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 24207f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 24217f5c258eSLen Brown 24227f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 24237f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 24247f5c258eSLen Brown return 0; 24257f5c258eSLen Brown 24267f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 24277f5c258eSLen Brown return 0; 24287f5c258eSLen Brown 2429b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 24307f5c258eSLen Brown "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n", 24317f5c258eSLen Brown cpu, msr, 24327f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 24337f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 24347f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 24357f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 24367f5c258eSLen Brown 24377f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 24387f5c258eSLen Brown return 0; 24397f5c258eSLen Brown 2440b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 24417f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n", 24427f5c258eSLen Brown cpu, msr, 24437f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 24447f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 24457f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 24467f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 24477f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 24487f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 24497f5c258eSLen Brown 24507f5c258eSLen Brown if (has_hwp_pkg) { 24517f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 24527f5c258eSLen Brown return 0; 24537f5c258eSLen Brown 2454b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 24557f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n", 24567f5c258eSLen Brown cpu, msr, 24577f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 24587f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 24597f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 24607f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 24617f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 24627f5c258eSLen Brown } 24637f5c258eSLen Brown if (has_hwp_notify) { 24647f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 24657f5c258eSLen Brown return 0; 24667f5c258eSLen Brown 2467b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 24687f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 24697f5c258eSLen Brown cpu, msr, 24707f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 24717f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 24727f5c258eSLen Brown } 24737f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 24747f5c258eSLen Brown return 0; 24757f5c258eSLen Brown 2476b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 24777f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 24787f5c258eSLen Brown cpu, msr, 24797f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 24807f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 2481889facbeSLen Brown 2482889facbeSLen Brown return 0; 2483889facbeSLen Brown } 2484889facbeSLen Brown 24853a9a941dSLen Brown /* 24863a9a941dSLen Brown * print_perf_limit() 24873a9a941dSLen Brown */ 24883a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 24893a9a941dSLen Brown { 24903a9a941dSLen Brown unsigned long long msr; 24913a9a941dSLen Brown int cpu; 24923a9a941dSLen Brown 24933a9a941dSLen Brown cpu = t->cpu_id; 24943a9a941dSLen Brown 24953a9a941dSLen Brown /* per-package */ 24963a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 24973a9a941dSLen Brown return 0; 24983a9a941dSLen Brown 24993a9a941dSLen Brown if (cpu_migrate(cpu)) { 2500b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 25013a9a941dSLen Brown return -1; 25023a9a941dSLen Brown } 25033a9a941dSLen Brown 25043a9a941dSLen Brown if (do_core_perf_limit_reasons) { 25053a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 2506b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2507b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 2508e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 25093a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 2510e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 2511e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 2512e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 2513e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 2514e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 2515e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 2516e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 2517e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 2518e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 2519e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 2520e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 2521e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 2522b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 2523e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 25243a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 2525e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 2526e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 2527e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 2528e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 2529e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 2530e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 2531e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 2532e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 2533e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 2534e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 2535e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 2536e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 25373a9a941dSLen Brown 25383a9a941dSLen Brown } 25393a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 25403a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 2541b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2542b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 25433a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 25443a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 25453a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 25463a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 25473a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 25483a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 25493a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 25503a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 2551b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 25523a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 25533a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 25543a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 25553a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 25563a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 25573a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 25583a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 25593a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 25603a9a941dSLen Brown } 25613a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 25623a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 2563b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2564b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 25653a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 25663a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 25673a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 25683a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 25693a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 25703a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 2571b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 25723a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 25733a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 25743a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 25753a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 25763a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 25773a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 25783a9a941dSLen Brown } 25793a9a941dSLen Brown return 0; 25803a9a941dSLen Brown } 25813a9a941dSLen Brown 2582889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 2583889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 2584889facbeSLen Brown 25851b69317dSColin Ian King double get_tdp(unsigned int model) 2586144b44b1SLen Brown { 2587144b44b1SLen Brown unsigned long long msr; 2588144b44b1SLen Brown 2589144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 25907ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 2591144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 2592144b44b1SLen Brown 2593144b44b1SLen Brown switch (model) { 2594869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 2595869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: 2596144b44b1SLen Brown return 30.0; 2597144b44b1SLen Brown default: 2598144b44b1SLen Brown return 135.0; 2599144b44b1SLen Brown } 2600144b44b1SLen Brown } 2601144b44b1SLen Brown 260240ee8e3bSAndrey Semin /* 260340ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 260440ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 260540ee8e3bSAndrey Semin */ 260640ee8e3bSAndrey Semin static double 260740ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 260840ee8e3bSAndrey Semin { 260940ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 261040ee8e3bSAndrey Semin 261140ee8e3bSAndrey Semin switch (model) { 2612869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2613869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2614869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2615869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 2616005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 261740ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 261840ee8e3bSAndrey Semin default: 261940ee8e3bSAndrey Semin return (rapl_energy_units); 262040ee8e3bSAndrey Semin } 262140ee8e3bSAndrey Semin } 262240ee8e3bSAndrey Semin 2623144b44b1SLen Brown 2624889facbeSLen Brown /* 2625889facbeSLen Brown * rapl_probe() 2626889facbeSLen Brown * 2627144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 2628889facbeSLen Brown */ 2629889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 2630889facbeSLen Brown { 2631889facbeSLen Brown unsigned long long msr; 2632144b44b1SLen Brown unsigned int time_unit; 2633889facbeSLen Brown double tdp; 2634889facbeSLen Brown 2635889facbeSLen Brown if (!genuine_intel) 2636889facbeSLen Brown return; 2637889facbeSLen Brown 2638889facbeSLen Brown if (family != 6) 2639889facbeSLen Brown return; 2640889facbeSLen Brown 2641889facbeSLen Brown switch (model) { 2642869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 2643869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 2644869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2645869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2646869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2647869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2648869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2649144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 2650889facbeSLen Brown break; 2651869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 2652e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 2653e4085d54SLen Brown break; 2654869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2655869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2656869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2657869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 26580b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 26590b2bb692SLen Brown break; 2660869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2661869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2662869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2663869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 2664869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 2665005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 26660b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 2667e6f9bb3cSLen Brown break; 2668869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 2669869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 26700b2bb692SLen 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; 2671144b44b1SLen Brown break; 2672869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 2673869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 2674144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 2675889facbeSLen Brown break; 2676869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 26770f644909SJacob 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; 26780f644909SJacob Pan break; 2679889facbeSLen Brown default: 2680889facbeSLen Brown return; 2681889facbeSLen Brown } 2682889facbeSLen Brown 2683889facbeSLen Brown /* units on package 0, verify later other packages match */ 26847ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 2685889facbeSLen Brown return; 2686889facbeSLen Brown 2687889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 2688869ce69eSLen Brown if (model == INTEL_FAM6_ATOM_SILVERMONT1) 2689144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 2690144b44b1SLen Brown else 2691889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 2692889facbeSLen Brown 269340ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 269440ee8e3bSAndrey Semin 2695144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 2696144b44b1SLen Brown if (time_unit == 0) 2697144b44b1SLen Brown time_unit = 0xA; 2698889facbeSLen Brown 2699144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 2700144b44b1SLen Brown 2701144b44b1SLen Brown tdp = get_tdp(model); 2702889facbeSLen Brown 2703889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 2704d8af6f5fSLen Brown if (debug) 2705b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 2706889facbeSLen Brown 2707889facbeSLen Brown return; 2708889facbeSLen Brown } 2709889facbeSLen Brown 27101b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 27113a9a941dSLen Brown { 27123a9a941dSLen Brown if (!genuine_intel) 27133a9a941dSLen Brown return; 27143a9a941dSLen Brown 27153a9a941dSLen Brown if (family != 6) 27163a9a941dSLen Brown return; 27173a9a941dSLen Brown 27183a9a941dSLen Brown switch (model) { 2719869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2720869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2721869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 27223a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 2723869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 27243a9a941dSLen Brown do_core_perf_limit_reasons = 1; 27253a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 27263a9a941dSLen Brown default: 27273a9a941dSLen Brown return; 27283a9a941dSLen Brown } 27293a9a941dSLen Brown } 27303a9a941dSLen Brown 2731889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2732889facbeSLen Brown { 2733889facbeSLen Brown unsigned long long msr; 2734889facbeSLen Brown unsigned int dts; 2735889facbeSLen Brown int cpu; 2736889facbeSLen Brown 2737889facbeSLen Brown if (!(do_dts || do_ptm)) 2738889facbeSLen Brown return 0; 2739889facbeSLen Brown 2740889facbeSLen Brown cpu = t->cpu_id; 2741889facbeSLen Brown 2742889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 2743889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2744889facbeSLen Brown return 0; 2745889facbeSLen Brown 2746889facbeSLen Brown if (cpu_migrate(cpu)) { 2747b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2748889facbeSLen Brown return -1; 2749889facbeSLen Brown } 2750889facbeSLen Brown 2751889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 2752889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2753889facbeSLen Brown return 0; 2754889facbeSLen Brown 2755889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2756b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 2757889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 2758889facbeSLen Brown 2759889facbeSLen Brown #ifdef THERM_DEBUG 2760889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 2761889facbeSLen Brown return 0; 2762889facbeSLen Brown 2763889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2764889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2765b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2766889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2767889facbeSLen Brown #endif 2768889facbeSLen Brown } 2769889facbeSLen Brown 2770889facbeSLen Brown 2771889facbeSLen Brown if (do_dts) { 2772889facbeSLen Brown unsigned int resolution; 2773889facbeSLen Brown 2774889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2775889facbeSLen Brown return 0; 2776889facbeSLen Brown 2777889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2778889facbeSLen Brown resolution = (msr >> 27) & 0xF; 2779b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 2780889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 2781889facbeSLen Brown 2782889facbeSLen Brown #ifdef THERM_DEBUG 2783889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 2784889facbeSLen Brown return 0; 2785889facbeSLen Brown 2786889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2787889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2788b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2789889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2790889facbeSLen Brown #endif 2791889facbeSLen Brown } 2792889facbeSLen Brown 2793889facbeSLen Brown return 0; 2794889facbeSLen Brown } 2795889facbeSLen Brown 2796889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 2797889facbeSLen Brown { 2798b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 2799889facbeSLen Brown cpu, label, 2800889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 2801889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 2802889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 2803889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 2804889facbeSLen Brown 2805889facbeSLen Brown return; 2806889facbeSLen Brown } 2807889facbeSLen Brown 2808889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2809889facbeSLen Brown { 2810889facbeSLen Brown unsigned long long msr; 2811889facbeSLen Brown int cpu; 2812889facbeSLen Brown 2813889facbeSLen Brown if (!do_rapl) 2814889facbeSLen Brown return 0; 2815889facbeSLen Brown 2816889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 2817889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2818889facbeSLen Brown return 0; 2819889facbeSLen Brown 2820889facbeSLen Brown cpu = t->cpu_id; 2821889facbeSLen Brown if (cpu_migrate(cpu)) { 2822b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2823889facbeSLen Brown return -1; 2824889facbeSLen Brown } 2825889facbeSLen Brown 2826889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 2827889facbeSLen Brown return -1; 2828889facbeSLen Brown 2829d8af6f5fSLen Brown if (debug) { 2830b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 2831889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 2832144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 2833889facbeSLen Brown } 2834144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 2835144b44b1SLen Brown 2836889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 2837889facbeSLen Brown return -5; 2838889facbeSLen Brown 2839889facbeSLen Brown 2840b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2841889facbeSLen Brown cpu, msr, 2842889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2843889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2844889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2845889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2846889facbeSLen Brown 2847144b44b1SLen Brown } 2848144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 2849144b44b1SLen Brown 2850889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 2851889facbeSLen Brown return -9; 2852889facbeSLen Brown 2853b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 2854889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 2855889facbeSLen Brown 2856889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 2857b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 2858889facbeSLen Brown cpu, 2859889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 2860889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 2861889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 2862889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 2863889facbeSLen Brown } 2864889facbeSLen Brown 28650b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 2866889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 2867889facbeSLen Brown return -6; 2868889facbeSLen Brown 2869b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2870889facbeSLen Brown cpu, msr, 2871889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2872889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2873889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2874889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 28750b2bb692SLen Brown } 28760b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 2877889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 2878889facbeSLen Brown return -9; 2879b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 2880889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2881889facbeSLen Brown 2882889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 2883889facbeSLen Brown } 2884144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 2885d8af6f5fSLen Brown if (debug) { 2886889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 2887889facbeSLen Brown return -7; 2888889facbeSLen Brown 2889b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 2890144b44b1SLen Brown } 2891144b44b1SLen Brown } 28929148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 2893d8af6f5fSLen Brown if (debug) { 2894889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 2895889facbeSLen Brown return -9; 2896b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 2897889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2898889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 2899889facbeSLen Brown } 2900889facbeSLen Brown } 2901889facbeSLen Brown if (do_rapl & RAPL_GFX) { 2902d8af6f5fSLen Brown if (debug) { 2903889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 2904889facbeSLen Brown return -8; 2905889facbeSLen Brown 2906b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 2907889facbeSLen Brown 2908889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 2909889facbeSLen Brown return -9; 2910b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 2911889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2912889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 2913889facbeSLen Brown } 2914889facbeSLen Brown } 2915889facbeSLen Brown return 0; 2916889facbeSLen Brown } 2917889facbeSLen Brown 2918d7899447SLen Brown /* 2919d7899447SLen Brown * SNB adds support for additional MSRs: 2920d7899447SLen Brown * 2921d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 2922d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 2923d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 2924d7899447SLen Brown */ 2925103a8feaSLen Brown 2926d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 2927103a8feaSLen Brown { 2928103a8feaSLen Brown if (!genuine_intel) 2929103a8feaSLen Brown return 0; 2930103a8feaSLen Brown 2931103a8feaSLen Brown switch (model) { 2932869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 2933869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 2934869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2935869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2936869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2937869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 2938869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2939869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2940869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2941869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2942869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2943869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2944869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2945869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2946869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2947869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2948869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 2949869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 29505bbac26eSXiaolong Wang case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 2951103a8feaSLen Brown return 1; 2952103a8feaSLen Brown } 2953103a8feaSLen Brown return 0; 2954103a8feaSLen Brown } 2955103a8feaSLen Brown 2956d7899447SLen Brown /* 2957d7899447SLen Brown * HSW adds support for additional MSRs: 2958d7899447SLen Brown * 2959d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 2960d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 2961d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 29625a63426eSLen Brown * 29635a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 29645a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 29655a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 29665a63426eSLen Brown * 2967d7899447SLen Brown */ 2968d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 2969ca58710fSKristen Carlson Accardi { 2970ca58710fSKristen Carlson Accardi if (!genuine_intel) 2971ca58710fSKristen Carlson Accardi return 0; 2972ca58710fSKristen Carlson Accardi 2973ca58710fSKristen Carlson Accardi switch (model) { 2974869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2975869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2976869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2977869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2978869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2979869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2980869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 2981ca58710fSKristen Carlson Accardi return 1; 2982ca58710fSKristen Carlson Accardi } 2983ca58710fSKristen Carlson Accardi return 0; 2984ca58710fSKristen Carlson Accardi } 2985ca58710fSKristen Carlson Accardi 29860b2bb692SLen Brown /* 29870b2bb692SLen Brown * SKL adds support for additional MSRS: 29880b2bb692SLen Brown * 29890b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 29900b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 29910b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 29920b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 29930b2bb692SLen Brown */ 29940b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 29950b2bb692SLen Brown { 29960b2bb692SLen Brown if (!genuine_intel) 29970b2bb692SLen Brown return 0; 29980b2bb692SLen Brown 29990b2bb692SLen Brown switch (model) { 3000869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3001869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3002869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3003869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 30040b2bb692SLen Brown return 1; 30050b2bb692SLen Brown } 30060b2bb692SLen Brown return 0; 30070b2bb692SLen Brown } 30080b2bb692SLen Brown 30090b2bb692SLen Brown 3010ca58710fSKristen Carlson Accardi 3011144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 3012144b44b1SLen Brown { 3013144b44b1SLen Brown if (!genuine_intel) 3014144b44b1SLen Brown return 0; 3015144b44b1SLen Brown switch (model) { 3016869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3017869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3018144b44b1SLen Brown return 1; 3019144b44b1SLen Brown } 3020144b44b1SLen Brown return 0; 3021144b44b1SLen Brown } 3022144b44b1SLen Brown 3023fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 3024fb5d4327SDasaratharaman Chandramouli { 3025fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3026fb5d4327SDasaratharaman Chandramouli return 0; 3027fb5d4327SDasaratharaman Chandramouli switch (model) { 3028869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3029005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3030fb5d4327SDasaratharaman Chandramouli return 1; 3031fb5d4327SDasaratharaman Chandramouli } 3032fb5d4327SDasaratharaman Chandramouli return 0; 3033fb5d4327SDasaratharaman Chandramouli } 3034fb5d4327SDasaratharaman Chandramouli 3035b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 3036b2b34dfeSHubert Chrzaniuk { 3037b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 3038b2b34dfeSHubert Chrzaniuk return 1024; 3039b2b34dfeSHubert Chrzaniuk return 1; 3040b2b34dfeSHubert Chrzaniuk } 3041b2b34dfeSHubert Chrzaniuk 3042144b44b1SLen Brown #define SLM_BCLK_FREQS 5 3043144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 3044144b44b1SLen Brown 3045144b44b1SLen Brown double slm_bclk(void) 3046144b44b1SLen Brown { 3047144b44b1SLen Brown unsigned long long msr = 3; 3048144b44b1SLen Brown unsigned int i; 3049144b44b1SLen Brown double freq; 3050144b44b1SLen Brown 30517ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 3052b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 3053144b44b1SLen Brown 3054144b44b1SLen Brown i = msr & 0xf; 3055144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 3056b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 30570a91e551SColin Ian King i = 3; 3058144b44b1SLen Brown } 3059144b44b1SLen Brown freq = slm_freq_table[i]; 3060144b44b1SLen Brown 3061b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 3062144b44b1SLen Brown 3063144b44b1SLen Brown return freq; 3064144b44b1SLen Brown } 3065144b44b1SLen Brown 3066103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 3067103a8feaSLen Brown { 3068121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 3069103a8feaSLen Brown return 100.00; 3070144b44b1SLen Brown else if (is_slm(family, model)) 3071144b44b1SLen Brown return slm_bclk(); 3072103a8feaSLen Brown else 3073103a8feaSLen Brown return 133.33; 3074103a8feaSLen Brown } 3075103a8feaSLen Brown 3076889facbeSLen Brown /* 3077889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 3078889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 3079889facbeSLen Brown * This is usually equal to tjMax. 3080889facbeSLen Brown * 3081889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 3082889facbeSLen Brown * but also allow cmdline over-ride with -T. 3083889facbeSLen Brown * 3084889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 3085889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 3086889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 3087889facbeSLen Brown */ 3088889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3089889facbeSLen Brown { 3090889facbeSLen Brown unsigned long long msr; 3091889facbeSLen Brown unsigned int target_c_local; 3092889facbeSLen Brown int cpu; 3093889facbeSLen Brown 3094889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 3095889facbeSLen Brown if (!(do_dts || do_ptm)) 3096889facbeSLen Brown return 0; 3097889facbeSLen Brown 3098889facbeSLen Brown /* this is a per-package concept */ 3099889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3100889facbeSLen Brown return 0; 3101889facbeSLen Brown 3102889facbeSLen Brown cpu = t->cpu_id; 3103889facbeSLen Brown if (cpu_migrate(cpu)) { 3104b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3105889facbeSLen Brown return -1; 3106889facbeSLen Brown } 3107889facbeSLen Brown 3108889facbeSLen Brown if (tcc_activation_temp_override != 0) { 3109889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 3110b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 3111889facbeSLen Brown cpu, tcc_activation_temp); 3112889facbeSLen Brown return 0; 3113889facbeSLen Brown } 3114889facbeSLen Brown 3115889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 3116d7899447SLen Brown if (!do_nhm_platform_info) 3117889facbeSLen Brown goto guess; 3118889facbeSLen Brown 31197ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 3120889facbeSLen Brown goto guess; 3121889facbeSLen Brown 31223482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 3123889facbeSLen Brown 3124d8af6f5fSLen Brown if (debug) 3125b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 3126889facbeSLen Brown cpu, msr, target_c_local); 3127889facbeSLen Brown 31283482124aSJean Delvare if (!target_c_local) 3129889facbeSLen Brown goto guess; 3130889facbeSLen Brown 3131889facbeSLen Brown tcc_activation_temp = target_c_local; 3132889facbeSLen Brown 3133889facbeSLen Brown return 0; 3134889facbeSLen Brown 3135889facbeSLen Brown guess: 3136889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 3137b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 3138889facbeSLen Brown cpu, tcc_activation_temp); 3139889facbeSLen Brown 3140889facbeSLen Brown return 0; 3141889facbeSLen Brown } 314269807a63SLen Brown 3143aa8d8cc7SLen Brown void decode_feature_control_msr(void) 3144aa8d8cc7SLen Brown { 3145aa8d8cc7SLen Brown unsigned long long msr; 3146aa8d8cc7SLen Brown 3147aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 3148aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 3149aa8d8cc7SLen Brown base_cpu, msr, 3150aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 3151aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 3152aa8d8cc7SLen Brown } 3153aa8d8cc7SLen Brown 315469807a63SLen Brown void decode_misc_enable_msr(void) 315569807a63SLen Brown { 315669807a63SLen Brown unsigned long long msr; 315769807a63SLen Brown 315869807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 3159b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n", 316069807a63SLen Brown base_cpu, msr, 316169807a63SLen Brown msr & (1 << 3) ? "TCC" : "", 316269807a63SLen Brown msr & (1 << 16) ? "EIST" : "", 316369807a63SLen Brown msr & (1 << 18) ? "MONITOR" : ""); 316469807a63SLen Brown } 316569807a63SLen Brown 3166f0057310SLen Brown /* 3167f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 3168f0057310SLen Brown * 3169f0057310SLen Brown * Decode the bits according to the Nehalem documentation 3170f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 3171f0057310SLen Brown * bit[1] less so... 3172f0057310SLen Brown */ 3173f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 3174f0057310SLen Brown { 3175f0057310SLen Brown unsigned long long msr; 3176f0057310SLen Brown 3177f0057310SLen Brown if (!do_nhm_platform_info) 3178f0057310SLen Brown return; 3179f0057310SLen Brown 3180f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 3181ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 3182f0057310SLen Brown base_cpu, msr, 3183f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 3184ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 3185ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 3186f0057310SLen Brown } 31877f5c258eSLen Brown 3188fcd17211SLen Brown void process_cpuid() 3189103a8feaSLen Brown { 319061a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 3191103a8feaSLen Brown unsigned int fms, family, model, stepping; 3192103a8feaSLen Brown 3193103a8feaSLen Brown eax = ebx = ecx = edx = 0; 3194103a8feaSLen Brown 31955aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 3196103a8feaSLen Brown 3197103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 3198103a8feaSLen Brown genuine_intel = 1; 3199103a8feaSLen Brown 3200d8af6f5fSLen Brown if (debug) 3201b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 3202103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 3203103a8feaSLen Brown 32045aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 3205103a8feaSLen Brown family = (fms >> 8) & 0xf; 3206103a8feaSLen Brown model = (fms >> 4) & 0xf; 3207103a8feaSLen Brown stepping = fms & 0xf; 3208103a8feaSLen Brown if (family == 6 || family == 0xf) 3209103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 3210103a8feaSLen Brown 321169807a63SLen Brown if (debug) { 3212b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 3213103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 3214aa8d8cc7SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n", 321569807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 321669807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 3217aa8d8cc7SLen Brown ecx & (1 << 6) ? "SMX" : "-", 321869807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 321969807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 322069807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 322169807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 322269807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 322369807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 322469807a63SLen Brown } 3225103a8feaSLen Brown 3226b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 3227b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 3228103a8feaSLen Brown 3229103a8feaSLen Brown /* 3230103a8feaSLen Brown * check max extended function levels of CPUID. 3231103a8feaSLen Brown * This is needed to check for invariant TSC. 3232103a8feaSLen Brown * This check is valid for both Intel and AMD. 3233103a8feaSLen Brown */ 3234103a8feaSLen Brown ebx = ecx = edx = 0; 32355aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 3236103a8feaSLen Brown 323761a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 3238103a8feaSLen Brown 3239103a8feaSLen Brown /* 3240103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 3241103a8feaSLen Brown * this check is valid for both Intel and AMD 3242103a8feaSLen Brown */ 32435aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 32448209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 3245d7899447SLen Brown } 3246103a8feaSLen Brown 3247103a8feaSLen Brown /* 3248103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 3249103a8feaSLen Brown * this check is valid for both Intel and AMD 3250103a8feaSLen Brown */ 3251103a8feaSLen Brown 32525aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 32538209e054SThomas Renninger has_aperf = ecx & (1 << 0); 3254889facbeSLen Brown do_dts = eax & (1 << 0); 3255889facbeSLen Brown do_ptm = eax & (1 << 6); 32567f5c258eSLen Brown has_hwp = eax & (1 << 7); 32577f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 32587f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 32597f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 32607f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 3261889facbeSLen Brown has_epb = ecx & (1 << 3); 3262889facbeSLen Brown 3263d8af6f5fSLen Brown if (debug) 3264b7d8c148SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, " 32657f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 32667f5c258eSLen Brown has_aperf ? "" : "No-", 32677f5c258eSLen Brown do_dts ? "" : "No-", 32687f5c258eSLen Brown do_ptm ? "" : "No-", 32697f5c258eSLen Brown has_hwp ? "" : "No-", 32707f5c258eSLen Brown has_hwp_notify ? "" : "No-", 32717f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 32727f5c258eSLen Brown has_hwp_epp ? "" : "No-", 32737f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 32747f5c258eSLen Brown has_epb ? "" : "No-"); 3275103a8feaSLen Brown 327669807a63SLen Brown if (debug) 327769807a63SLen Brown decode_misc_enable_msr(); 327869807a63SLen Brown 32798ae72255SLen Brown if (max_level >= 0x7 && debug) { 3280aa8d8cc7SLen Brown int has_sgx; 3281aa8d8cc7SLen Brown 3282aa8d8cc7SLen Brown ecx = 0; 3283aa8d8cc7SLen Brown 3284aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 3285aa8d8cc7SLen Brown 3286aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 3287aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 3288aa8d8cc7SLen Brown 3289aa8d8cc7SLen Brown if (has_sgx) 3290aa8d8cc7SLen Brown decode_feature_control_msr(); 3291aa8d8cc7SLen Brown } 3292aa8d8cc7SLen Brown 329361a87ba7SLen Brown if (max_level >= 0x15) { 32948a5bdf41SLen Brown unsigned int eax_crystal; 32958a5bdf41SLen Brown unsigned int ebx_tsc; 32968a5bdf41SLen Brown 32978a5bdf41SLen Brown /* 32988a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 32998a5bdf41SLen Brown */ 33008a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 33015aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 33028a5bdf41SLen Brown 33038a5bdf41SLen Brown if (ebx_tsc != 0) { 33048a5bdf41SLen Brown 33058a5bdf41SLen Brown if (debug && (ebx != 0)) 3306b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 33078a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 33088a5bdf41SLen Brown 33098a5bdf41SLen Brown if (crystal_hz == 0) 33108a5bdf41SLen Brown switch(model) { 3311869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3312869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3313869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3314869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3315e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 3316e8efbc80SLen Brown break; 3317869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3318ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 3319ec53e594SLen Brown break; 3320869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3321869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 3322e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 33238a5bdf41SLen Brown break; 33248a5bdf41SLen Brown default: 33258a5bdf41SLen Brown crystal_hz = 0; 33268a5bdf41SLen Brown } 33278a5bdf41SLen Brown 33288a5bdf41SLen Brown if (crystal_hz) { 33298a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 33308a5bdf41SLen Brown if (debug) 3331b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 33328a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 33338a5bdf41SLen Brown } 33348a5bdf41SLen Brown } 33358a5bdf41SLen Brown } 333661a87ba7SLen Brown if (max_level >= 0x16) { 333761a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 333861a87ba7SLen Brown 333961a87ba7SLen Brown /* 334061a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 334161a87ba7SLen Brown */ 334261a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 334361a87ba7SLen Brown 33445aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 334561a87ba7SLen Brown if (debug) 3346b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 334761a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 334861a87ba7SLen Brown } 33498a5bdf41SLen Brown 3350b2b34dfeSHubert Chrzaniuk if (has_aperf) 3351b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 3352b2b34dfeSHubert Chrzaniuk 3353ee7e38e3SLen Brown do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); 3354d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 33555a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 3356ee7e38e3SLen Brown do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); 3357ee7e38e3SLen Brown do_pc3 = (pkg_cstate_limit >= PCL__3); 3358ee7e38e3SLen Brown do_pc6 = (pkg_cstate_limit >= PCL__6); 3359ee7e38e3SLen Brown do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); 3360d7899447SLen Brown do_c8_c9_c10 = has_hsw_msrs(family, model); 33615a63426eSLen Brown do_irtl_hsw = has_hsw_msrs(family, model); 33620b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 3363144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 3364fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 3365103a8feaSLen Brown 3366f0057310SLen Brown if (debug) 3367f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 3368f0057310SLen Brown 3369889facbeSLen Brown rapl_probe(family, model); 33703a9a941dSLen Brown perf_limit_reasons_probe(family, model); 3371889facbeSLen Brown 3372fcd17211SLen Brown if (debug) 33731b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 3374fcd17211SLen Brown 3375a2b7b749SLen Brown if (has_skl_msrs(family, model)) 3376a2b7b749SLen Brown calculate_tsc_tweak(); 3377a2b7b749SLen Brown 3378fdf676e5SLen Brown do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK); 3379fdf676e5SLen Brown 338027d47356SLen Brown do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK); 338127d47356SLen Brown 3382889facbeSLen Brown return; 3383103a8feaSLen Brown } 3384103a8feaSLen Brown 3385d8af6f5fSLen Brown void help() 3386103a8feaSLen Brown { 3387b7d8c148SLen Brown fprintf(outf, 3388d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 3389d8af6f5fSLen Brown "\n" 3390d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 3391d8af6f5fSLen Brown "when COMMAND completes.\n" 3392d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 3393d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 3394d8af6f5fSLen Brown "--debug run in \"debug\" mode\n" 3395d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 3396d8af6f5fSLen Brown "--help print this help message\n" 3397d8af6f5fSLen Brown "--counter msr print 32-bit counter at address \"msr\"\n" 3398d8af6f5fSLen Brown "--Counter msr print 64-bit Counter at address \"msr\"\n" 3399b7d8c148SLen Brown "--out file create or truncate \"file\" for all output\n" 3400d8af6f5fSLen Brown "--msr msr print 32-bit value at address \"msr\"\n" 3401d8af6f5fSLen Brown "--MSR msr print 64-bit Value at address \"msr\"\n" 3402d8af6f5fSLen Brown "--version print version information\n" 3403d8af6f5fSLen Brown "\n" 3404d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 3405103a8feaSLen Brown } 3406103a8feaSLen Brown 3407103a8feaSLen Brown 3408103a8feaSLen Brown /* 3409103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 3410103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 3411103a8feaSLen Brown */ 3412103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 3413103a8feaSLen Brown { 3414103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 3415103a8feaSLen Brown return 1; 3416103a8feaSLen Brown else 3417103a8feaSLen Brown return 0; 3418103a8feaSLen Brown } 3419103a8feaSLen Brown 3420103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 3421103a8feaSLen Brown { 3422103a8feaSLen Brown return 0; 3423103a8feaSLen Brown } 3424103a8feaSLen Brown 3425c98d5d94SLen Brown void topology_probe() 3426c98d5d94SLen Brown { 3427c98d5d94SLen Brown int i; 3428c98d5d94SLen Brown int max_core_id = 0; 3429c98d5d94SLen Brown int max_package_id = 0; 3430c98d5d94SLen Brown int max_siblings = 0; 3431c98d5d94SLen Brown struct cpu_topology { 3432c98d5d94SLen Brown int core_id; 3433c98d5d94SLen Brown int physical_package_id; 3434c98d5d94SLen Brown } *cpus; 3435c98d5d94SLen Brown 3436c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 3437c98d5d94SLen Brown topo.num_cpus = 0; 3438c98d5d94SLen Brown topo.max_cpu_num = 0; 3439c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 3440c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 3441c98d5d94SLen Brown show_cpu = 1; 3442c98d5d94SLen Brown 3443d8af6f5fSLen Brown if (debug > 1) 3444b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 3445c98d5d94SLen Brown 3446c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 3447b2c95d90SJosh Triplett if (cpus == NULL) 3448b2c95d90SJosh Triplett err(1, "calloc cpus"); 3449c98d5d94SLen Brown 3450c98d5d94SLen Brown /* 3451c98d5d94SLen Brown * Allocate and initialize cpu_present_set 3452c98d5d94SLen Brown */ 3453c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3454b2c95d90SJosh Triplett if (cpu_present_set == NULL) 3455b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3456c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3457c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 3458c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 3459c98d5d94SLen Brown 3460c98d5d94SLen Brown /* 3461c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 3462c98d5d94SLen Brown */ 3463c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3464b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 3465b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3466c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3467c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 3468c98d5d94SLen Brown 3469c98d5d94SLen Brown 3470c98d5d94SLen Brown /* 3471c98d5d94SLen Brown * For online cpus 3472c98d5d94SLen Brown * find max_core_id, max_package_id 3473c98d5d94SLen Brown */ 3474c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 3475c98d5d94SLen Brown int siblings; 3476c98d5d94SLen Brown 3477c98d5d94SLen Brown if (cpu_is_not_present(i)) { 3478d8af6f5fSLen Brown if (debug > 1) 3479b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 3480c98d5d94SLen Brown continue; 3481c98d5d94SLen Brown } 3482c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 3483c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 3484c98d5d94SLen Brown max_core_id = cpus[i].core_id; 3485c98d5d94SLen Brown 3486c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 3487c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 3488c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 3489c98d5d94SLen Brown 3490c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 3491c98d5d94SLen Brown if (siblings > max_siblings) 3492c98d5d94SLen Brown max_siblings = siblings; 3493d8af6f5fSLen Brown if (debug > 1) 3494b7d8c148SLen Brown fprintf(outf, "cpu %d pkg %d core %d\n", 3495c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 3496c98d5d94SLen Brown } 3497c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 3498d8af6f5fSLen Brown if (debug > 1) 3499b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 3500c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 35011cc21f7bSLen Brown if (debug && !summary_only && topo.num_cores_per_pkg > 1) 3502c98d5d94SLen Brown show_core = 1; 3503c98d5d94SLen Brown 3504c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 3505d8af6f5fSLen Brown if (debug > 1) 3506b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 3507c98d5d94SLen Brown max_package_id, topo.num_packages); 35081cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 3509c98d5d94SLen Brown show_pkg = 1; 3510c98d5d94SLen Brown 3511c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 3512d8af6f5fSLen Brown if (debug > 1) 3513b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 3514c98d5d94SLen Brown 3515c98d5d94SLen Brown free(cpus); 3516c98d5d94SLen Brown } 3517c98d5d94SLen Brown 3518c98d5d94SLen Brown void 3519c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 3520c98d5d94SLen Brown { 3521c98d5d94SLen Brown int i; 3522c98d5d94SLen Brown 3523c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 3524c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 3525c98d5d94SLen Brown if (*t == NULL) 3526c98d5d94SLen Brown goto error; 3527c98d5d94SLen Brown 3528c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 3529c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 3530c98d5d94SLen Brown (*t)[i].cpu_id = -1; 3531c98d5d94SLen Brown 3532c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 3533c98d5d94SLen Brown sizeof(struct core_data)); 3534c98d5d94SLen Brown if (*c == NULL) 3535c98d5d94SLen Brown goto error; 3536c98d5d94SLen Brown 3537c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 3538c98d5d94SLen Brown (*c)[i].core_id = -1; 3539c98d5d94SLen Brown 3540c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 3541c98d5d94SLen Brown if (*p == NULL) 3542c98d5d94SLen Brown goto error; 3543c98d5d94SLen Brown 3544c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 3545c98d5d94SLen Brown (*p)[i].package_id = i; 3546c98d5d94SLen Brown 3547c98d5d94SLen Brown return; 3548c98d5d94SLen Brown error: 3549b2c95d90SJosh Triplett err(1, "calloc counters"); 3550c98d5d94SLen Brown } 3551c98d5d94SLen Brown /* 3552c98d5d94SLen Brown * init_counter() 3553c98d5d94SLen Brown * 3554c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 3555c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 3556c98d5d94SLen Brown * 3557c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 3558c98d5d94SLen Brown */ 3559c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 3560c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 3561c98d5d94SLen Brown int pkg_num, int cpu_id) 3562c98d5d94SLen Brown { 3563c98d5d94SLen Brown struct thread_data *t; 3564c98d5d94SLen Brown struct core_data *c; 3565c98d5d94SLen Brown struct pkg_data *p; 3566c98d5d94SLen Brown 3567c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 3568c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 3569c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 3570c98d5d94SLen Brown 3571c98d5d94SLen Brown t->cpu_id = cpu_id; 3572c98d5d94SLen Brown if (thread_num == 0) { 3573c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 3574c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 3575c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 3576c98d5d94SLen Brown } 3577c98d5d94SLen Brown 3578c98d5d94SLen Brown c->core_id = core_num; 3579c98d5d94SLen Brown p->package_id = pkg_num; 3580c98d5d94SLen Brown } 3581c98d5d94SLen Brown 3582c98d5d94SLen Brown 3583c98d5d94SLen Brown int initialize_counters(int cpu_id) 3584c98d5d94SLen Brown { 3585c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 3586c98d5d94SLen Brown 3587c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 3588c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 3589e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 3590e275b388SDasaratharaman Chandramouli if (!my_thread_id) 3591c98d5d94SLen Brown topo.num_cores++; 3592c98d5d94SLen Brown 3593c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3594c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3595c98d5d94SLen Brown return 0; 3596c98d5d94SLen Brown } 3597c98d5d94SLen Brown 3598c98d5d94SLen Brown void allocate_output_buffer() 3599c98d5d94SLen Brown { 36003b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 3601c98d5d94SLen Brown outp = output_buffer; 3602b2c95d90SJosh Triplett if (outp == NULL) 3603b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 3604c98d5d94SLen Brown } 360536229897SLen Brown void allocate_fd_percpu(void) 360636229897SLen Brown { 360701a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 360836229897SLen Brown if (fd_percpu == NULL) 360936229897SLen Brown err(-1, "calloc fd_percpu"); 361036229897SLen Brown } 3611562a2d37SLen Brown void allocate_irq_buffers(void) 3612562a2d37SLen Brown { 3613562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 3614562a2d37SLen Brown if (irq_column_2_cpu == NULL) 3615562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 3616c98d5d94SLen Brown 361701a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 3618562a2d37SLen Brown if (irqs_per_cpu == NULL) 361901a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 3620562a2d37SLen Brown } 3621c98d5d94SLen Brown void setup_all_buffers(void) 3622c98d5d94SLen Brown { 3623c98d5d94SLen Brown topology_probe(); 3624562a2d37SLen Brown allocate_irq_buffers(); 362536229897SLen Brown allocate_fd_percpu(); 3626c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 3627c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 3628c98d5d94SLen Brown allocate_output_buffer(); 3629c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 3630c98d5d94SLen Brown } 36313b4d5c7fSAndy Shevchenko 36327ce7d5deSPrarit Bhargava void set_base_cpu(void) 36337ce7d5deSPrarit Bhargava { 36347ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 36357ce7d5deSPrarit Bhargava if (base_cpu < 0) 36367ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 36377ce7d5deSPrarit Bhargava 36387ce7d5deSPrarit Bhargava if (debug > 1) 3639b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 36407ce7d5deSPrarit Bhargava } 36417ce7d5deSPrarit Bhargava 3642103a8feaSLen Brown void turbostat_init() 3643103a8feaSLen Brown { 36447ce7d5deSPrarit Bhargava setup_all_buffers(); 36457ce7d5deSPrarit Bhargava set_base_cpu(); 3646103a8feaSLen Brown check_dev_msr(); 364798481e79SLen Brown check_permissions(); 3648fcd17211SLen Brown process_cpuid(); 3649103a8feaSLen Brown 3650103a8feaSLen Brown 3651d8af6f5fSLen Brown if (debug) 36527f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 36537f5c258eSLen Brown 36547f5c258eSLen Brown if (debug) 3655889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 3656889facbeSLen Brown 3657d8af6f5fSLen Brown if (debug) 36583a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 36593a9a941dSLen Brown 3660d8af6f5fSLen Brown if (debug) 3661889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 3662889facbeSLen Brown 3663889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 3664889facbeSLen Brown 3665d8af6f5fSLen Brown if (debug) 3666889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 36675a63426eSLen Brown 36685a63426eSLen Brown if (debug && do_irtl_snb) 36695a63426eSLen Brown print_irtl(); 3670103a8feaSLen Brown } 3671103a8feaSLen Brown 3672103a8feaSLen Brown int fork_it(char **argv) 3673103a8feaSLen Brown { 3674103a8feaSLen Brown pid_t child_pid; 3675d91bb17cSLen Brown int status; 3676d15cf7c1SLen Brown 3677d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 3678d91bb17cSLen Brown if (status) 3679d91bb17cSLen Brown exit(status); 3680d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 3681d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 3682103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3683103a8feaSLen Brown 3684103a8feaSLen Brown child_pid = fork(); 3685103a8feaSLen Brown if (!child_pid) { 3686103a8feaSLen Brown /* child */ 3687103a8feaSLen Brown execvp(argv[0], argv); 3688103a8feaSLen Brown } else { 3689103a8feaSLen Brown 3690103a8feaSLen Brown /* parent */ 3691b2c95d90SJosh Triplett if (child_pid == -1) 3692b2c95d90SJosh Triplett err(1, "fork"); 3693103a8feaSLen Brown 3694103a8feaSLen Brown signal(SIGINT, SIG_IGN); 3695103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 3696b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 3697b2c95d90SJosh Triplett err(status, "waitpid"); 3698103a8feaSLen Brown } 3699c98d5d94SLen Brown /* 3700c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 3701c98d5d94SLen Brown * because re-starting is problematic when forking 3702c98d5d94SLen Brown */ 3703c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 3704103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3705103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3706ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 3707ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 3708ba3dec99SLen Brown else { 3709c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3710c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3711ba3dec99SLen Brown } 3712103a8feaSLen Brown 3713b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 3714b7d8c148SLen Brown 3715b7d8c148SLen Brown flush_output_stderr(); 3716103a8feaSLen Brown 3717d91bb17cSLen Brown return status; 3718103a8feaSLen Brown } 3719103a8feaSLen Brown 37203b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 37213b4d5c7fSAndy Shevchenko { 37223b4d5c7fSAndy Shevchenko int status; 37233b4d5c7fSAndy Shevchenko 37243b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 37253b4d5c7fSAndy Shevchenko if (status) 37263b4d5c7fSAndy Shevchenko return status; 37273b4d5c7fSAndy Shevchenko 37283b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 37293b4d5c7fSAndy Shevchenko if (status) 37303b4d5c7fSAndy Shevchenko return status; 37313b4d5c7fSAndy Shevchenko 3732b7d8c148SLen Brown flush_output_stdout(); 37333b4d5c7fSAndy Shevchenko 37343b4d5c7fSAndy Shevchenko return status; 37353b4d5c7fSAndy Shevchenko } 37363b4d5c7fSAndy Shevchenko 3737d8af6f5fSLen Brown void print_version() { 37383d109de2SLen Brown fprintf(outf, "turbostat version 4.14 22 Apr 2016" 3739d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 3740d8af6f5fSLen Brown } 3741d8af6f5fSLen Brown 3742103a8feaSLen Brown void cmdline(int argc, char **argv) 3743103a8feaSLen Brown { 3744103a8feaSLen Brown int opt; 3745d8af6f5fSLen Brown int option_index = 0; 3746d8af6f5fSLen Brown static struct option long_options[] = { 3747d8af6f5fSLen Brown {"Counter", required_argument, 0, 'C'}, 3748d8af6f5fSLen Brown {"counter", required_argument, 0, 'c'}, 3749d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 3750d8af6f5fSLen Brown {"debug", no_argument, 0, 'd'}, 3751d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 3752d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 3753d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 3754d8af6f5fSLen Brown {"MSR", required_argument, 0, 'M'}, 3755d8af6f5fSLen Brown {"msr", required_argument, 0, 'm'}, 3756b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 3757d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 3758d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 3759d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 3760d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 3761d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 3762d8af6f5fSLen Brown {0, 0, 0, 0 } 3763d8af6f5fSLen Brown }; 3764103a8feaSLen Brown 3765103a8feaSLen Brown progname = argv[0]; 3766103a8feaSLen Brown 3767b7d8c148SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v", 3768d8af6f5fSLen Brown long_options, &option_index)) != -1) { 3769103a8feaSLen Brown switch (opt) { 3770d8af6f5fSLen Brown case 'C': 3771d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 3772103a8feaSLen Brown break; 3773f9240813SLen Brown case 'c': 37748e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 37758e180f3cSLen Brown break; 3776d8af6f5fSLen Brown case 'D': 3777d8af6f5fSLen Brown dump_only++; 37788e180f3cSLen Brown break; 3779d8af6f5fSLen Brown case 'd': 3780d8af6f5fSLen Brown debug++; 37812f32edf1SLen Brown break; 3782d8af6f5fSLen Brown case 'h': 3783d8af6f5fSLen Brown default: 3784d8af6f5fSLen Brown help(); 3785d8af6f5fSLen Brown exit(1); 3786d8af6f5fSLen Brown case 'i': 37872a0609c0SLen Brown { 37882a0609c0SLen Brown double interval = strtod(optarg, NULL); 37892a0609c0SLen Brown 37902a0609c0SLen Brown if (interval < 0.001) { 3791b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 37922a0609c0SLen Brown interval); 37932a0609c0SLen Brown exit(2); 37942a0609c0SLen Brown } 37952a0609c0SLen Brown 37962a0609c0SLen Brown interval_ts.tv_sec = interval; 37972a0609c0SLen Brown interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 37982a0609c0SLen Brown } 3799889facbeSLen Brown break; 38005c56be9aSDirk Brandewie case 'J': 38015c56be9aSDirk Brandewie rapl_joules++; 38025c56be9aSDirk Brandewie break; 3803d8af6f5fSLen Brown case 'M': 3804d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 3805d8af6f5fSLen Brown break; 3806d8af6f5fSLen Brown case 'm': 3807d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 3808d8af6f5fSLen Brown break; 3809b7d8c148SLen Brown case 'o': 3810b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 3811b7d8c148SLen Brown break; 3812d8af6f5fSLen Brown case 'P': 3813d8af6f5fSLen Brown show_pkg_only++; 3814d8af6f5fSLen Brown break; 3815d8af6f5fSLen Brown case 'p': 3816d8af6f5fSLen Brown show_core_only++; 3817d8af6f5fSLen Brown break; 3818d8af6f5fSLen Brown case 'S': 3819d8af6f5fSLen Brown summary_only++; 3820d8af6f5fSLen Brown break; 3821d8af6f5fSLen Brown case 'T': 3822d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 3823d8af6f5fSLen Brown break; 3824d8af6f5fSLen Brown case 'v': 3825d8af6f5fSLen Brown print_version(); 3826d8af6f5fSLen Brown exit(0); 3827d8af6f5fSLen Brown break; 3828103a8feaSLen Brown } 3829103a8feaSLen Brown } 3830103a8feaSLen Brown } 3831103a8feaSLen Brown 3832103a8feaSLen Brown int main(int argc, char **argv) 3833103a8feaSLen Brown { 3834b7d8c148SLen Brown outf = stderr; 3835b7d8c148SLen Brown 3836103a8feaSLen Brown cmdline(argc, argv); 3837103a8feaSLen Brown 3838d8af6f5fSLen Brown if (debug) 3839d8af6f5fSLen Brown print_version(); 3840103a8feaSLen Brown 3841103a8feaSLen Brown turbostat_init(); 3842103a8feaSLen Brown 38433b4d5c7fSAndy Shevchenko /* dump counters and exit */ 38443b4d5c7fSAndy Shevchenko if (dump_only) 38453b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 38463b4d5c7fSAndy Shevchenko 3847103a8feaSLen Brown /* 3848103a8feaSLen Brown * if any params left, it must be a command to fork 3849103a8feaSLen Brown */ 3850103a8feaSLen Brown if (argc - optind) 3851103a8feaSLen Brown return fork_it(argv + optind); 3852103a8feaSLen Brown else 3853103a8feaSLen Brown turbostat_loop(); 3854103a8feaSLen Brown 3855103a8feaSLen Brown return 0; 3856103a8feaSLen Brown } 3857