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 2495aebc44SJosh Triplett #include <stdarg.h> 25103a8feaSLen Brown #include <stdio.h> 26b2c95d90SJosh Triplett #include <err.h> 27103a8feaSLen Brown #include <unistd.h> 28103a8feaSLen Brown #include <sys/types.h> 29103a8feaSLen Brown #include <sys/wait.h> 30103a8feaSLen Brown #include <sys/stat.h> 31103a8feaSLen Brown #include <sys/resource.h> 32103a8feaSLen Brown #include <fcntl.h> 33103a8feaSLen Brown #include <signal.h> 34103a8feaSLen Brown #include <sys/time.h> 35103a8feaSLen Brown #include <stdlib.h> 36d8af6f5fSLen Brown #include <getopt.h> 37103a8feaSLen Brown #include <dirent.h> 38103a8feaSLen Brown #include <string.h> 39103a8feaSLen Brown #include <ctype.h> 4088c3281fSLen Brown #include <sched.h> 412a0609c0SLen Brown #include <time.h> 422b92865eSJosh Triplett #include <cpuid.h> 4398481e79SLen Brown #include <linux/capability.h> 4498481e79SLen Brown #include <errno.h> 45103a8feaSLen Brown 46103a8feaSLen Brown char *proc_stat = "/proc/stat"; 47b7d8c148SLen Brown FILE *outf; 4836229897SLen Brown int *fd_percpu; 492a0609c0SLen Brown struct timespec interval_ts = {5, 0}; 50d8af6f5fSLen Brown unsigned int debug; 51d8af6f5fSLen Brown unsigned int rapl_joules; 52d8af6f5fSLen Brown unsigned int summary_only; 53d8af6f5fSLen Brown unsigned int dump_only; 54103a8feaSLen Brown unsigned int skip_c0; 55103a8feaSLen Brown unsigned int skip_c1; 56103a8feaSLen Brown unsigned int do_nhm_cstates; 57103a8feaSLen Brown unsigned int do_snb_cstates; 58fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 59ee7e38e3SLen Brown unsigned int do_pc2; 60ee7e38e3SLen Brown unsigned int do_pc3; 61ee7e38e3SLen Brown unsigned int do_pc6; 62ee7e38e3SLen Brown unsigned int do_pc7; 63ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10; 640b2bb692SLen Brown unsigned int do_skl_residency; 65144b44b1SLen Brown unsigned int do_slm_cstates; 66144b44b1SLen Brown unsigned int use_c1_residency_msr; 67103a8feaSLen Brown unsigned int has_aperf; 68889facbeSLen Brown unsigned int has_epb; 69fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 70103a8feaSLen Brown unsigned int genuine_intel; 71103a8feaSLen Brown unsigned int has_invariant_tsc; 72d7899447SLen Brown unsigned int do_nhm_platform_info; 732f32edf1SLen Brown unsigned int extra_msr_offset32; 742f32edf1SLen Brown unsigned int extra_msr_offset64; 758e180f3cSLen Brown unsigned int extra_delta_offset32; 768e180f3cSLen Brown unsigned int extra_delta_offset64; 77b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 78562a2d37SLen Brown int do_irq = 1; 791ed51011SLen Brown int do_smi; 80103a8feaSLen Brown double bclk; 81a2b7b749SLen Brown double base_hz; 8221ed5574SLen Brown unsigned int has_base_hz; 83a2b7b749SLen Brown double tsc_tweak = 1.0; 84103a8feaSLen Brown unsigned int show_pkg; 85103a8feaSLen Brown unsigned int show_core; 86103a8feaSLen Brown unsigned int show_cpu; 87c98d5d94SLen Brown unsigned int show_pkg_only; 88c98d5d94SLen Brown unsigned int show_core_only; 89c98d5d94SLen Brown char *output_buffer, *outp; 90889facbeSLen Brown unsigned int do_rapl; 91889facbeSLen Brown unsigned int do_dts; 92889facbeSLen Brown unsigned int do_ptm; 93fdf676e5SLen Brown unsigned int do_gfx_rc6_ms; 94fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 9527d47356SLen Brown unsigned int do_gfx_mhz; 9627d47356SLen Brown unsigned int gfx_cur_mhz; 97889facbeSLen Brown unsigned int tcc_activation_temp; 98889facbeSLen Brown unsigned int tcc_activation_temp_override; 9940ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 10040ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 101889facbeSLen Brown double rapl_joule_counter_range; 1023a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 1033a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1043a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1058a5bdf41SLen Brown unsigned int crystal_hz; 1068a5bdf41SLen Brown unsigned long long tsc_hz; 1077ce7d5deSPrarit Bhargava int base_cpu; 10821ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1097f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1107f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1117f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1127f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1137f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1147f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 115889facbeSLen Brown 116889facbeSLen Brown #define RAPL_PKG (1 << 0) 117e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 118e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 119e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 120e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 121e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 122e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 123e6f9bb3cSLen Brown 124889facbeSLen Brown #define RAPL_DRAM (1 << 3) 125e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 126e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 127e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 128e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1290b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1300b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 131e6f9bb3cSLen Brown 1320b2bb692SLen Brown #define RAPL_CORES (1 << 6) 133e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 134e6f9bb3cSLen Brown /* 0x639 MSR_PP0_ENERGY_STATUS */ 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 */ 142889facbeSLen Brown #define TJMAX_DEFAULT 100 143889facbeSLen Brown 144889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 145103a8feaSLen Brown 146103a8feaSLen Brown int aperf_mperf_unstable; 147103a8feaSLen Brown int backwards_count; 148103a8feaSLen Brown char *progname; 149103a8feaSLen Brown 150c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 151c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 152c98d5d94SLen Brown 153c98d5d94SLen Brown struct thread_data { 154c98d5d94SLen Brown unsigned long long tsc; 155c98d5d94SLen Brown unsigned long long aperf; 156c98d5d94SLen Brown unsigned long long mperf; 157144b44b1SLen Brown unsigned long long c1; 1582f32edf1SLen Brown unsigned long long extra_msr64; 1598e180f3cSLen Brown unsigned long long extra_delta64; 1608e180f3cSLen Brown unsigned long long extra_msr32; 1618e180f3cSLen Brown unsigned long long extra_delta32; 162562a2d37SLen Brown unsigned int irq_count; 1631ed51011SLen Brown unsigned int smi_count; 164c98d5d94SLen Brown unsigned int cpu_id; 165c98d5d94SLen Brown unsigned int flags; 166c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 167c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 168c98d5d94SLen Brown } *thread_even, *thread_odd; 169c98d5d94SLen Brown 170c98d5d94SLen Brown struct core_data { 171c98d5d94SLen Brown unsigned long long c3; 172c98d5d94SLen Brown unsigned long long c6; 173c98d5d94SLen Brown unsigned long long c7; 174889facbeSLen Brown unsigned int core_temp_c; 175c98d5d94SLen Brown unsigned int core_id; 176c98d5d94SLen Brown } *core_even, *core_odd; 177c98d5d94SLen Brown 178c98d5d94SLen Brown struct pkg_data { 179c98d5d94SLen Brown unsigned long long pc2; 180c98d5d94SLen Brown unsigned long long pc3; 181c98d5d94SLen Brown unsigned long long pc6; 182c98d5d94SLen Brown unsigned long long pc7; 183ca58710fSKristen Carlson Accardi unsigned long long pc8; 184ca58710fSKristen Carlson Accardi unsigned long long pc9; 185ca58710fSKristen Carlson Accardi unsigned long long pc10; 1860b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1870b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1880b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1890b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 190fdf676e5SLen Brown unsigned long long gfx_rc6_ms; 19127d47356SLen Brown unsigned int gfx_mhz; 192c98d5d94SLen Brown unsigned int package_id; 193889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 194889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 195889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 196889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 197889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 198889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 199889facbeSLen Brown unsigned int pkg_temp_c; 200889facbeSLen Brown 201c98d5d94SLen Brown } *package_even, *package_odd; 202c98d5d94SLen Brown 203c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 204c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 205c98d5d94SLen Brown 206c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 207c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 208c98d5d94SLen Brown topo.num_threads_per_core + \ 209c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 210c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 211c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 212c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 213c98d5d94SLen Brown 214c98d5d94SLen Brown struct system_summary { 215c98d5d94SLen Brown struct thread_data threads; 216c98d5d94SLen Brown struct core_data cores; 217c98d5d94SLen Brown struct pkg_data packages; 218c98d5d94SLen Brown } sum, average; 219c98d5d94SLen Brown 220c98d5d94SLen Brown 221c98d5d94SLen Brown struct topo_params { 222c98d5d94SLen Brown int num_packages; 223103a8feaSLen Brown int num_cpus; 224c98d5d94SLen Brown int num_cores; 225c98d5d94SLen Brown int max_cpu_num; 226c98d5d94SLen Brown int num_cores_per_pkg; 227c98d5d94SLen Brown int num_threads_per_core; 228c98d5d94SLen Brown } topo; 229103a8feaSLen Brown 230c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 231103a8feaSLen Brown 232562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 233562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 234562a2d37SLen Brown 235c98d5d94SLen Brown void setup_all_buffers(void); 236103a8feaSLen Brown 237c98d5d94SLen Brown int cpu_is_not_present(int cpu) 238d15cf7c1SLen Brown { 239c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 240c98d5d94SLen Brown } 241c98d5d94SLen Brown /* 242c98d5d94SLen Brown * run func(thread, core, package) in topology order 243c98d5d94SLen Brown * skip non-present cpus 244c98d5d94SLen Brown */ 245c98d5d94SLen Brown 246c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 247c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 248c98d5d94SLen Brown { 249c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 250c98d5d94SLen Brown 251c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 252c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 253c98d5d94SLen Brown for (thread_no = 0; thread_no < 254c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 255c98d5d94SLen Brown struct thread_data *t; 256c98d5d94SLen Brown struct core_data *c; 257c98d5d94SLen Brown struct pkg_data *p; 258c98d5d94SLen Brown 259c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 260c98d5d94SLen Brown 261c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 262c98d5d94SLen Brown continue; 263c98d5d94SLen Brown 264c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 265c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 266c98d5d94SLen Brown 267c98d5d94SLen Brown retval = func(t, c, p); 268c98d5d94SLen Brown if (retval) 269c98d5d94SLen Brown return retval; 270c98d5d94SLen Brown } 271c98d5d94SLen Brown } 272c98d5d94SLen Brown } 273d15cf7c1SLen Brown return 0; 274d15cf7c1SLen Brown } 275d15cf7c1SLen Brown 27688c3281fSLen Brown int cpu_migrate(int cpu) 27788c3281fSLen Brown { 278c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 279c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 280c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 28188c3281fSLen Brown return -1; 28288c3281fSLen Brown else 28388c3281fSLen Brown return 0; 28488c3281fSLen Brown } 28536229897SLen Brown int get_msr_fd(int cpu) 286103a8feaSLen Brown { 287103a8feaSLen Brown char pathname[32]; 288103a8feaSLen Brown int fd; 289103a8feaSLen Brown 29036229897SLen Brown fd = fd_percpu[cpu]; 29136229897SLen Brown 29236229897SLen Brown if (fd) 29336229897SLen Brown return fd; 29436229897SLen Brown 295103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 296103a8feaSLen Brown fd = open(pathname, O_RDONLY); 29715aaa346SLen Brown if (fd < 0) 29898481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 299103a8feaSLen Brown 30036229897SLen Brown fd_percpu[cpu] = fd; 30136229897SLen Brown 30236229897SLen Brown return fd; 30336229897SLen Brown } 30436229897SLen Brown 30536229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 30636229897SLen Brown { 30736229897SLen Brown ssize_t retval; 30836229897SLen Brown 30936229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 31015aaa346SLen Brown 31198481e79SLen Brown if (retval != sizeof *msr) 31236229897SLen Brown err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset); 31315aaa346SLen Brown 31415aaa346SLen Brown return 0; 315103a8feaSLen Brown } 316103a8feaSLen Brown 317fc04cc67SLen Brown /* 318fc04cc67SLen Brown * Example Format w/ field column widths: 319fc04cc67SLen Brown * 32027d47356SLen 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 321562a2d37SLen Brown * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 322fc04cc67SLen Brown */ 323fc04cc67SLen Brown 324a829eb4dSLen Brown void print_header(void) 325103a8feaSLen Brown { 326103a8feaSLen Brown if (show_pkg) 327fc04cc67SLen Brown outp += sprintf(outp, " Package"); 328103a8feaSLen Brown if (show_core) 329fc04cc67SLen Brown outp += sprintf(outp, " Core"); 330103a8feaSLen Brown if (show_cpu) 331c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 332103a8feaSLen Brown if (has_aperf) 333fc04cc67SLen Brown outp += sprintf(outp, " Avg_MHz"); 334d7899447SLen Brown if (has_aperf) 33575d2e44eSLen Brown outp += sprintf(outp, " Busy%%"); 336fc04cc67SLen Brown if (has_aperf) 337fc04cc67SLen Brown outp += sprintf(outp, " Bzy_MHz"); 338fc04cc67SLen Brown outp += sprintf(outp, " TSC_MHz"); 3391cc21f7bSLen Brown 3408e180f3cSLen Brown if (extra_delta_offset32) 341f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 3428e180f3cSLen Brown if (extra_delta_offset64) 343f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 3442f32edf1SLen Brown if (extra_msr_offset32) 3458e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 3462f32edf1SLen Brown if (extra_msr_offset64) 3478e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 3481cc21f7bSLen Brown 3491cc21f7bSLen Brown if (!debug) 3501cc21f7bSLen Brown goto done; 3511cc21f7bSLen Brown 352562a2d37SLen Brown if (do_irq) 353562a2d37SLen Brown outp += sprintf(outp, " IRQ"); 3541cc21f7bSLen Brown if (do_smi) 3551cc21f7bSLen Brown outp += sprintf(outp, " SMI"); 3561cc21f7bSLen Brown 357103a8feaSLen Brown if (do_nhm_cstates) 358fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c1"); 359fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 360fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c3"); 361103a8feaSLen Brown if (do_nhm_cstates) 362fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c6"); 363103a8feaSLen Brown if (do_snb_cstates) 364fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c7"); 365889facbeSLen Brown 366889facbeSLen Brown if (do_dts) 367fc04cc67SLen Brown outp += sprintf(outp, " CoreTmp"); 368889facbeSLen Brown if (do_ptm) 369fc04cc67SLen Brown outp += sprintf(outp, " PkgTmp"); 370889facbeSLen Brown 371fdf676e5SLen Brown if (do_gfx_rc6_ms) 372fdf676e5SLen Brown outp += sprintf(outp, " GFX%%rc6"); 373fdf676e5SLen Brown 37427d47356SLen Brown if (do_gfx_mhz) 37527d47356SLen Brown outp += sprintf(outp, " GFXMHz"); 37627d47356SLen Brown 3770b2bb692SLen Brown if (do_skl_residency) { 3780b2bb692SLen Brown outp += sprintf(outp, " Totl%%C0"); 3790b2bb692SLen Brown outp += sprintf(outp, " Any%%C0"); 3800b2bb692SLen Brown outp += sprintf(outp, " GFX%%C0"); 3810b2bb692SLen Brown outp += sprintf(outp, " CPUGFX%%"); 3820b2bb692SLen Brown } 3830b2bb692SLen Brown 384ee7e38e3SLen Brown if (do_pc2) 385fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc2"); 386ee7e38e3SLen Brown if (do_pc3) 387fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc3"); 388ee7e38e3SLen Brown if (do_pc6) 389fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc6"); 390ee7e38e3SLen Brown if (do_pc7) 391fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc7"); 392ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 393fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc8"); 394fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc9"); 395fc04cc67SLen Brown outp += sprintf(outp, " Pk%%pc10"); 396ca58710fSKristen Carlson Accardi } 397103a8feaSLen Brown 3985c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 399889facbeSLen Brown if (do_rapl & RAPL_PKG) 400fc04cc67SLen Brown outp += sprintf(outp, " PkgWatt"); 401889facbeSLen Brown if (do_rapl & RAPL_CORES) 402fc04cc67SLen Brown outp += sprintf(outp, " CorWatt"); 403889facbeSLen Brown if (do_rapl & RAPL_GFX) 404fc04cc67SLen Brown outp += sprintf(outp, " GFXWatt"); 405889facbeSLen Brown if (do_rapl & RAPL_DRAM) 406fc04cc67SLen Brown outp += sprintf(outp, " RAMWatt"); 407889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 408889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 409889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 410889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 411d7899447SLen Brown } else if (do_rapl && rapl_joules) { 4125c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 4135c56be9aSDirk Brandewie outp += sprintf(outp, " Pkg_J"); 4145c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 4155c56be9aSDirk Brandewie outp += sprintf(outp, " Cor_J"); 4165c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 4175c56be9aSDirk Brandewie outp += sprintf(outp, " GFX_J"); 4185c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 419bd6906edSLen Brown outp += sprintf(outp, " RAM_J"); 4205c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 4215c56be9aSDirk Brandewie outp += sprintf(outp, " PKG_%%"); 4225c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 4235c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_%%"); 4245c56be9aSDirk Brandewie outp += sprintf(outp, " time"); 425889facbeSLen Brown 4265c56be9aSDirk Brandewie } 4271cc21f7bSLen Brown done: 428c98d5d94SLen Brown outp += sprintf(outp, "\n"); 429103a8feaSLen Brown } 430103a8feaSLen Brown 431c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 432c98d5d94SLen Brown struct pkg_data *p) 433103a8feaSLen Brown { 4343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 435c98d5d94SLen Brown 436c98d5d94SLen Brown if (t) { 4373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 4383b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 4393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 4403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 4413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 4423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 4433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4448e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 4453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4468e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 4473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4482f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 4493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4502f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 451562a2d37SLen Brown if (do_irq) 452562a2d37SLen Brown outp += sprintf(outp, "IRQ: %08X\n", t->irq_count); 4531ed51011SLen Brown if (do_smi) 4543b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 455103a8feaSLen Brown } 456103a8feaSLen Brown 457c98d5d94SLen Brown if (c) { 4583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 4593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 4603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 4613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 4623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 463c98d5d94SLen Brown } 464103a8feaSLen Brown 465c98d5d94SLen Brown if (p) { 4663b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 4670b2bb692SLen Brown 4680b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 4690b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 4700b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 4710b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 4720b2bb692SLen Brown 4733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 474ee7e38e3SLen Brown if (do_pc3) 4753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 476ee7e38e3SLen Brown if (do_pc6) 4773b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 478ee7e38e3SLen Brown if (do_pc7) 4793b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 4803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 4813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 4823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 4833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 4843b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 4853b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 4863b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 4873b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 4883b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 4893b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 4903b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 4913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 492c98d5d94SLen Brown } 4933b4d5c7fSAndy Shevchenko 4943b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 4953b4d5c7fSAndy Shevchenko 496c98d5d94SLen Brown return 0; 497103a8feaSLen Brown } 498103a8feaSLen Brown 499e23da037SLen Brown /* 500e23da037SLen Brown * column formatting convention & formats 501e23da037SLen Brown */ 502c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 503c98d5d94SLen Brown struct pkg_data *p) 504103a8feaSLen Brown { 505103a8feaSLen Brown double interval_float; 506fc04cc67SLen Brown char *fmt8; 507103a8feaSLen Brown 508c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 509c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 510c98d5d94SLen Brown return 0; 511c98d5d94SLen Brown 512c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 513c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 514c98d5d94SLen Brown return 0; 515c98d5d94SLen Brown 516103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 517103a8feaSLen Brown 518c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 519c98d5d94SLen Brown if (t == &average.threads) { 520103a8feaSLen Brown if (show_pkg) 521fc04cc67SLen Brown outp += sprintf(outp, " -"); 522103a8feaSLen Brown if (show_core) 523fc04cc67SLen Brown outp += sprintf(outp, " -"); 524103a8feaSLen Brown if (show_cpu) 525fc04cc67SLen Brown outp += sprintf(outp, " -"); 526103a8feaSLen Brown } else { 527c98d5d94SLen Brown if (show_pkg) { 528c98d5d94SLen Brown if (p) 529fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->package_id); 530c98d5d94SLen Brown else 531fc04cc67SLen Brown outp += sprintf(outp, " -"); 532c98d5d94SLen Brown } 533c98d5d94SLen Brown if (show_core) { 534c98d5d94SLen Brown if (c) 535fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_id); 536c98d5d94SLen Brown else 537fc04cc67SLen Brown outp += sprintf(outp, " -"); 538c98d5d94SLen Brown } 539103a8feaSLen Brown if (show_cpu) 540fc04cc67SLen Brown outp += sprintf(outp, "%8d", t->cpu_id); 541103a8feaSLen Brown } 542fc04cc67SLen Brown 543d7899447SLen Brown /* Avg_MHz */ 544fc04cc67SLen Brown if (has_aperf) 545fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 546fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 547fc04cc67SLen Brown 54875d2e44eSLen Brown /* Busy% */ 549d7899447SLen Brown if (has_aperf) { 550103a8feaSLen Brown if (!skip_c0) 551a2b7b749SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak); 552103a8feaSLen Brown else 553fc04cc67SLen Brown outp += sprintf(outp, "********"); 554103a8feaSLen Brown } 555103a8feaSLen Brown 556d7899447SLen Brown /* Bzy_MHz */ 55721ed5574SLen Brown if (has_aperf) { 55821ed5574SLen Brown if (has_base_hz) 55921ed5574SLen Brown outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf); 56021ed5574SLen Brown else 561fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 56221ed5574SLen Brown 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); 56321ed5574SLen Brown } 564103a8feaSLen Brown 565d7899447SLen Brown /* TSC_MHz */ 566fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float); 567103a8feaSLen Brown 5688e180f3cSLen Brown /* delta */ 5698e180f3cSLen Brown if (extra_delta_offset32) 5708e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 5718e180f3cSLen Brown 5728e180f3cSLen Brown /* DELTA */ 5738e180f3cSLen Brown if (extra_delta_offset64) 5748e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 5752f32edf1SLen Brown /* msr */ 5762f32edf1SLen Brown if (extra_msr_offset32) 5778e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 5782f32edf1SLen Brown 579130ff304SLen Brown /* MSR */ 5802f32edf1SLen Brown if (extra_msr_offset64) 5812f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 582130ff304SLen Brown 5831cc21f7bSLen Brown if (!debug) 5841cc21f7bSLen Brown goto done; 5851cc21f7bSLen Brown 586562a2d37SLen Brown /* IRQ */ 587562a2d37SLen Brown if (do_irq) 588562a2d37SLen Brown outp += sprintf(outp, "%8d", t->irq_count); 589562a2d37SLen Brown 5901cc21f7bSLen Brown /* SMI */ 5911cc21f7bSLen Brown if (do_smi) 5921cc21f7bSLen Brown outp += sprintf(outp, "%8d", t->smi_count); 5931cc21f7bSLen Brown 594103a8feaSLen Brown if (do_nhm_cstates) { 595103a8feaSLen Brown if (!skip_c1) 596fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc); 597103a8feaSLen Brown else 598fc04cc67SLen Brown outp += sprintf(outp, "********"); 599103a8feaSLen Brown } 600c98d5d94SLen Brown 601c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 602c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 603c98d5d94SLen Brown goto done; 604c98d5d94SLen Brown 605fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 606fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc); 607103a8feaSLen Brown if (do_nhm_cstates) 608fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc); 609103a8feaSLen Brown if (do_snb_cstates) 610fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc); 611c98d5d94SLen Brown 612889facbeSLen Brown if (do_dts) 613fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_temp_c); 614889facbeSLen Brown 615c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 616c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 617c98d5d94SLen Brown goto done; 618c98d5d94SLen Brown 6190b2bb692SLen Brown /* PkgTmp */ 620889facbeSLen Brown if (do_ptm) 621fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->pkg_temp_c); 622889facbeSLen Brown 623fdf676e5SLen Brown /* GFXrc6 */ 624fdf676e5SLen Brown if (do_gfx_rc6_ms) 625fdf676e5SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->gfx_rc6_ms / 1000.0 / interval_float); 626fdf676e5SLen Brown 62727d47356SLen Brown /* GFXMHz */ 62827d47356SLen Brown if (do_gfx_mhz) 62927d47356SLen Brown outp += sprintf(outp, "%8d", p->gfx_mhz); 63027d47356SLen Brown 6310b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 6320b2bb692SLen Brown if (do_skl_residency) { 6330b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc); 6340b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc); 6350b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc); 6360b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc); 6370b2bb692SLen Brown } 6380b2bb692SLen Brown 639ee7e38e3SLen Brown if (do_pc2) 640fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc); 641ee7e38e3SLen Brown if (do_pc3) 642fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc); 643ee7e38e3SLen Brown if (do_pc6) 644fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc); 645ee7e38e3SLen Brown if (do_pc7) 646fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc); 647ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 648fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc); 649fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc); 650fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc); 651ca58710fSKristen Carlson Accardi } 652889facbeSLen Brown 653889facbeSLen Brown /* 654889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 655889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 656889facbeSLen Brown */ 657fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 658fc04cc67SLen Brown fmt8 = "%8.2f"; 659fc04cc67SLen Brown else 660fc04cc67SLen Brown fmt8 = " %6.0f**"; 661889facbeSLen Brown 6625c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 663889facbeSLen Brown if (do_rapl & RAPL_PKG) 664fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 665889facbeSLen Brown if (do_rapl & RAPL_CORES) 666fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 667889facbeSLen Brown if (do_rapl & RAPL_GFX) 668fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 669889facbeSLen Brown if (do_rapl & RAPL_DRAM) 67040ee8e3bSAndrey Semin outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); 671889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 672fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 673889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 674fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 675d7899447SLen Brown } else if (do_rapl && rapl_joules) { 6765c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 677fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6785c56be9aSDirk Brandewie p->energy_pkg * rapl_energy_units); 6795c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 680fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6815c56be9aSDirk Brandewie p->energy_cores * rapl_energy_units); 6825c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 683fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6845c56be9aSDirk Brandewie p->energy_gfx * rapl_energy_units); 6855c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 686fc04cc67SLen Brown outp += sprintf(outp, fmt8, 68740ee8e3bSAndrey Semin p->energy_dram * rapl_dram_energy_units); 6885c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 689fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 6905c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 691fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 692889facbeSLen Brown 693d7899447SLen Brown outp += sprintf(outp, fmt8, interval_float); 6945c56be9aSDirk Brandewie } 695c98d5d94SLen Brown done: 696c98d5d94SLen Brown outp += sprintf(outp, "\n"); 697c98d5d94SLen Brown 698c98d5d94SLen Brown return 0; 699103a8feaSLen Brown } 700103a8feaSLen Brown 701b7d8c148SLen Brown void flush_output_stdout(void) 702103a8feaSLen Brown { 703b7d8c148SLen Brown FILE *filep; 704b7d8c148SLen Brown 705b7d8c148SLen Brown if (outf == stderr) 706b7d8c148SLen Brown filep = stdout; 707b7d8c148SLen Brown else 708b7d8c148SLen Brown filep = outf; 709b7d8c148SLen Brown 710b7d8c148SLen Brown fputs(output_buffer, filep); 711b7d8c148SLen Brown fflush(filep); 712b7d8c148SLen Brown 713c98d5d94SLen Brown outp = output_buffer; 714c98d5d94SLen Brown } 715b7d8c148SLen Brown void flush_output_stderr(void) 716c98d5d94SLen Brown { 717b7d8c148SLen Brown fputs(output_buffer, outf); 718b7d8c148SLen Brown fflush(outf); 719c98d5d94SLen Brown outp = output_buffer; 720c98d5d94SLen Brown } 721c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 722c98d5d94SLen Brown { 723e23da037SLen Brown static int printed; 724103a8feaSLen Brown 725e23da037SLen Brown if (!printed || !summary_only) 726103a8feaSLen Brown print_header(); 727103a8feaSLen Brown 728c98d5d94SLen Brown if (topo.num_cpus > 1) 729c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 730c98d5d94SLen Brown &average.packages); 731103a8feaSLen Brown 732e23da037SLen Brown printed = 1; 733e23da037SLen Brown 734e23da037SLen Brown if (summary_only) 735e23da037SLen Brown return; 736e23da037SLen Brown 737c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 738103a8feaSLen Brown } 739103a8feaSLen Brown 740889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 741889facbeSLen Brown if (new > old) { \ 742889facbeSLen Brown old = new - old; \ 743889facbeSLen Brown } else { \ 744889facbeSLen Brown old = 0x100000000 + new - old; \ 745889facbeSLen Brown } 746889facbeSLen Brown 747c98d5d94SLen Brown void 748c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 749103a8feaSLen Brown { 7500b2bb692SLen Brown 7510b2bb692SLen Brown if (do_skl_residency) { 7520b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 7530b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 7540b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 7550b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 7560b2bb692SLen Brown } 757c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 758ee7e38e3SLen Brown if (do_pc3) 759c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 760ee7e38e3SLen Brown if (do_pc6) 761c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 762ee7e38e3SLen Brown if (do_pc7) 763c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 764ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 765ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 766ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 767889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 768889facbeSLen Brown 769fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 77027d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 77127d47356SLen Brown 772889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 773889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 774889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 775889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 776889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 777889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 778103a8feaSLen Brown } 779103a8feaSLen Brown 780c98d5d94SLen Brown void 781c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 782c98d5d94SLen Brown { 783c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 784c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 785c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 786889facbeSLen Brown old->core_temp_c = new->core_temp_c; 787103a8feaSLen Brown } 788c98d5d94SLen Brown 789c3ae331dSLen Brown /* 790c3ae331dSLen Brown * old = new - old 791c3ae331dSLen Brown */ 792c98d5d94SLen Brown void 793c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 794c98d5d94SLen Brown struct core_data *core_delta) 795c98d5d94SLen Brown { 796c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 797c98d5d94SLen Brown 798103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 799b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 800b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 801b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 802b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 803103a8feaSLen Brown 804c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 805c98d5d94SLen Brown 806a729617cSLen Brown if (has_aperf) { 807c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 808c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 809c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 810c98d5d94SLen Brown } else { 811c98d5d94SLen Brown 812103a8feaSLen Brown if (!aperf_mperf_unstable) { 813b7d8c148SLen Brown fprintf(outf, "%s: APERF or MPERF went backwards *\n", progname); 814b7d8c148SLen Brown fprintf(outf, "* Frequency results do not cover entire interval *\n"); 815b7d8c148SLen Brown fprintf(outf, "* fix this by running Linux-2.6.30 or later *\n"); 816103a8feaSLen Brown 817103a8feaSLen Brown aperf_mperf_unstable = 1; 818103a8feaSLen Brown } 819103a8feaSLen Brown /* 820103a8feaSLen Brown * mperf delta is likely a huge "positive" number 821103a8feaSLen Brown * can not use it for calculating c0 time 822103a8feaSLen Brown */ 823103a8feaSLen Brown skip_c0 = 1; 824103a8feaSLen Brown skip_c1 = 1; 825103a8feaSLen Brown } 826a729617cSLen Brown } 827103a8feaSLen Brown 828c98d5d94SLen Brown 829144b44b1SLen Brown if (use_c1_residency_msr) { 830144b44b1SLen Brown /* 831144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 832144b44b1SLen Brown * which should be more accurate than the derivation below. 833144b44b1SLen Brown */ 834144b44b1SLen Brown } else { 835103a8feaSLen Brown /* 836c3ae331dSLen Brown * As counter collection is not atomic, 837c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 838103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 839103a8feaSLen Brown */ 840c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 841c98d5d94SLen Brown old->c1 = 0; 842c98d5d94SLen Brown else { 843c98d5d94SLen Brown /* normal case, derive c1 */ 844c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 845c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 846c98d5d94SLen Brown } 847144b44b1SLen Brown } 848c3ae331dSLen Brown 849c98d5d94SLen Brown if (old->mperf == 0) { 850b7d8c148SLen Brown if (debug > 1) 851b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 852c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 853c98d5d94SLen Brown } 854103a8feaSLen Brown 8558e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 8568e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 8578e180f3cSLen Brown 8588e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 8598e180f3cSLen Brown 860103a8feaSLen Brown /* 8618e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 862103a8feaSLen Brown */ 8632f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 8642f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 8651ed51011SLen Brown 866562a2d37SLen Brown if (do_irq) 867562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 868562a2d37SLen Brown 8691ed51011SLen Brown if (do_smi) 8701ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 871103a8feaSLen Brown } 872c98d5d94SLen Brown 873c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 874c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 875c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 876c98d5d94SLen Brown { 877c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 878c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 879c98d5d94SLen Brown delta_core(c, c2); 880c98d5d94SLen Brown 881c98d5d94SLen Brown /* always calculate thread delta */ 882c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 883c98d5d94SLen Brown 884c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 885c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 886c98d5d94SLen Brown delta_package(p, p2); 887c98d5d94SLen Brown 888103a8feaSLen Brown return 0; 889103a8feaSLen Brown } 890103a8feaSLen Brown 891c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 892103a8feaSLen Brown { 893c98d5d94SLen Brown t->tsc = 0; 894c98d5d94SLen Brown t->aperf = 0; 895c98d5d94SLen Brown t->mperf = 0; 896c98d5d94SLen Brown t->c1 = 0; 897103a8feaSLen Brown 8988e180f3cSLen Brown t->extra_delta32 = 0; 8998e180f3cSLen Brown t->extra_delta64 = 0; 9008e180f3cSLen Brown 901562a2d37SLen Brown t->irq_count = 0; 902562a2d37SLen Brown t->smi_count = 0; 903562a2d37SLen Brown 904c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 905c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 906c98d5d94SLen Brown 907c98d5d94SLen Brown c->c3 = 0; 908c98d5d94SLen Brown c->c6 = 0; 909c98d5d94SLen Brown c->c7 = 0; 910889facbeSLen Brown c->core_temp_c = 0; 911c98d5d94SLen Brown 9120b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 9130b2bb692SLen Brown p->pkg_any_core_c0 = 0; 9140b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 9150b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 9160b2bb692SLen Brown 917c98d5d94SLen Brown p->pc2 = 0; 918ee7e38e3SLen Brown if (do_pc3) 919c98d5d94SLen Brown p->pc3 = 0; 920ee7e38e3SLen Brown if (do_pc6) 921c98d5d94SLen Brown p->pc6 = 0; 922ee7e38e3SLen Brown if (do_pc7) 923c98d5d94SLen Brown p->pc7 = 0; 924ca58710fSKristen Carlson Accardi p->pc8 = 0; 925ca58710fSKristen Carlson Accardi p->pc9 = 0; 926ca58710fSKristen Carlson Accardi p->pc10 = 0; 927889facbeSLen Brown 928889facbeSLen Brown p->energy_pkg = 0; 929889facbeSLen Brown p->energy_dram = 0; 930889facbeSLen Brown p->energy_cores = 0; 931889facbeSLen Brown p->energy_gfx = 0; 932889facbeSLen Brown p->rapl_pkg_perf_status = 0; 933889facbeSLen Brown p->rapl_dram_perf_status = 0; 934889facbeSLen Brown p->pkg_temp_c = 0; 93527d47356SLen Brown 936fdf676e5SLen Brown p->gfx_rc6_ms = 0; 93727d47356SLen Brown p->gfx_mhz = 0; 938103a8feaSLen Brown } 939c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 940c98d5d94SLen Brown struct pkg_data *p) 941103a8feaSLen Brown { 942c98d5d94SLen Brown average.threads.tsc += t->tsc; 943c98d5d94SLen Brown average.threads.aperf += t->aperf; 944c98d5d94SLen Brown average.threads.mperf += t->mperf; 945c98d5d94SLen Brown average.threads.c1 += t->c1; 94615aaa346SLen Brown 9478e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 9488e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 9498e180f3cSLen Brown 950562a2d37SLen Brown average.threads.irq_count += t->irq_count; 951562a2d37SLen Brown average.threads.smi_count += t->smi_count; 952562a2d37SLen Brown 953c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 954c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 955c98d5d94SLen Brown return 0; 956c98d5d94SLen Brown 957c98d5d94SLen Brown average.cores.c3 += c->c3; 958c98d5d94SLen Brown average.cores.c6 += c->c6; 959c98d5d94SLen Brown average.cores.c7 += c->c7; 960c98d5d94SLen Brown 961889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 962889facbeSLen Brown 963c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 964c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 965c98d5d94SLen Brown return 0; 966c98d5d94SLen Brown 9670b2bb692SLen Brown if (do_skl_residency) { 9680b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 9690b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 9700b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 9710b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 9720b2bb692SLen Brown } 9730b2bb692SLen Brown 974c98d5d94SLen Brown average.packages.pc2 += p->pc2; 975ee7e38e3SLen Brown if (do_pc3) 976c98d5d94SLen Brown average.packages.pc3 += p->pc3; 977ee7e38e3SLen Brown if (do_pc6) 978c98d5d94SLen Brown average.packages.pc6 += p->pc6; 979ee7e38e3SLen Brown if (do_pc7) 980c98d5d94SLen Brown average.packages.pc7 += p->pc7; 981ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 982ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 983ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 984c98d5d94SLen Brown 985889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 986889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 987889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 988889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 989889facbeSLen Brown 990fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 99127d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 99227d47356SLen Brown 993889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 994889facbeSLen Brown 995889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 996889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 997c98d5d94SLen Brown return 0; 998c98d5d94SLen Brown } 999c98d5d94SLen Brown /* 1000c98d5d94SLen Brown * sum the counters for all cpus in the system 1001c98d5d94SLen Brown * compute the weighted average 1002c98d5d94SLen Brown */ 1003c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1004c98d5d94SLen Brown struct pkg_data *p) 1005c98d5d94SLen Brown { 1006c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1007c98d5d94SLen Brown 1008c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1009c98d5d94SLen Brown 1010c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1011c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1012c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1013c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1014c98d5d94SLen Brown 10158e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 10168e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 10178e180f3cSLen Brown 10188e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 10198e180f3cSLen Brown 1020c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1021c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1022c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 1023c98d5d94SLen Brown 10240b2bb692SLen Brown if (do_skl_residency) { 10250b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 10260b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 10270b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 10280b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 10290b2bb692SLen Brown } 10300b2bb692SLen Brown 1031c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 1032ee7e38e3SLen Brown if (do_pc3) 1033c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 1034ee7e38e3SLen Brown if (do_pc6) 1035c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 1036ee7e38e3SLen Brown if (do_pc7) 1037c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1038ca58710fSKristen Carlson Accardi 1039ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1040ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1041ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1042c98d5d94SLen Brown } 1043c98d5d94SLen Brown 1044c98d5d94SLen Brown static unsigned long long rdtsc(void) 1045c98d5d94SLen Brown { 1046c98d5d94SLen Brown unsigned int low, high; 1047c98d5d94SLen Brown 1048c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1049c98d5d94SLen Brown 1050c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1051c98d5d94SLen Brown } 1052c98d5d94SLen Brown 1053c98d5d94SLen Brown /* 1054c98d5d94SLen Brown * get_counters(...) 1055c98d5d94SLen Brown * migrate to cpu 1056c98d5d94SLen Brown * acquire and record local counters for that cpu 1057c98d5d94SLen Brown */ 1058c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1059c98d5d94SLen Brown { 1060c98d5d94SLen Brown int cpu = t->cpu_id; 1061889facbeSLen Brown unsigned long long msr; 1062*0102b067SLen Brown int aperf_mperf_retry_count = 0; 1063c98d5d94SLen Brown 1064e52966c0SLen Brown if (cpu_migrate(cpu)) { 1065b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 106615aaa346SLen Brown return -1; 1067e52966c0SLen Brown } 106815aaa346SLen Brown 1069*0102b067SLen Brown retry: 1070c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 107115aaa346SLen Brown 107215aaa346SLen Brown if (has_aperf) { 1073*0102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 1074*0102b067SLen Brown 1075*0102b067SLen Brown /* 1076*0102b067SLen Brown * The TSC, APERF and MPERF must be read together for 1077*0102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 1078*0102b067SLen Brown * 1079*0102b067SLen Brown * Unfortunately, APERF and MPERF are read by 1080*0102b067SLen Brown * individual system call, so delays may occur 1081*0102b067SLen Brown * between them. If the time to read them 1082*0102b067SLen Brown * varies by a large amount, we re-read them. 1083*0102b067SLen Brown */ 1084*0102b067SLen Brown 1085*0102b067SLen Brown /* 1086*0102b067SLen Brown * This initial dummy APERF read has been seen to 1087*0102b067SLen Brown * reduce jitter in the subsequent reads. 1088*0102b067SLen Brown */ 1089*0102b067SLen Brown 10909c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1091c98d5d94SLen Brown return -3; 1092*0102b067SLen Brown 1093*0102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 1094*0102b067SLen Brown 1095*0102b067SLen Brown tsc_before = t->tsc; 1096*0102b067SLen Brown 1097*0102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1098*0102b067SLen Brown return -3; 1099*0102b067SLen Brown 1100*0102b067SLen Brown tsc_between = rdtsc(); 1101*0102b067SLen Brown 11029c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1103c98d5d94SLen Brown return -4; 1104*0102b067SLen Brown 1105*0102b067SLen Brown tsc_after = rdtsc(); 1106*0102b067SLen Brown 1107*0102b067SLen Brown aperf_time = tsc_between - tsc_before; 1108*0102b067SLen Brown mperf_time = tsc_after - tsc_between; 1109*0102b067SLen Brown 1110*0102b067SLen Brown /* 1111*0102b067SLen Brown * If the system call latency to read APERF and MPERF 1112*0102b067SLen Brown * differ by more than 2x, then try again. 1113*0102b067SLen Brown */ 1114*0102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 1115*0102b067SLen Brown aperf_mperf_retry_count++; 1116*0102b067SLen Brown if (aperf_mperf_retry_count < 5) 1117*0102b067SLen Brown goto retry; 1118*0102b067SLen Brown else 1119*0102b067SLen Brown warnx("cpu%d jitter %lld %lld", 1120*0102b067SLen Brown cpu, aperf_time, mperf_time); 1121*0102b067SLen Brown } 1122*0102b067SLen Brown aperf_mperf_retry_count = 0; 1123*0102b067SLen Brown 1124b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1125b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 112688c3281fSLen Brown } 112788c3281fSLen Brown 1128562a2d37SLen Brown if (do_irq) 1129562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 11301ed51011SLen Brown if (do_smi) { 11311ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 11321ed51011SLen Brown return -5; 11331ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 11341ed51011SLen Brown } 11358e180f3cSLen Brown if (extra_delta_offset32) { 1136889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 11372f32edf1SLen Brown return -5; 1138889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 11398e180f3cSLen Brown } 11408e180f3cSLen Brown 11418e180f3cSLen Brown if (extra_delta_offset64) 11428e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 11438e180f3cSLen Brown return -5; 11448e180f3cSLen Brown 11458e180f3cSLen Brown if (extra_msr_offset32) { 1146889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 11478e180f3cSLen Brown return -5; 1148889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 11498e180f3cSLen Brown } 11502f32edf1SLen Brown 11512f32edf1SLen Brown if (extra_msr_offset64) 11522f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 1153c98d5d94SLen Brown return -5; 1154c98d5d94SLen Brown 1155144b44b1SLen Brown if (use_c1_residency_msr) { 1156144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1157144b44b1SLen Brown return -6; 1158144b44b1SLen Brown } 1159144b44b1SLen Brown 1160c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1161c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1162c98d5d94SLen Brown return 0; 1163c98d5d94SLen Brown 1164fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) { 1165c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1166c98d5d94SLen Brown return -6; 1167144b44b1SLen Brown } 1168144b44b1SLen Brown 1169fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_knl_cstates) { 1170c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1171c98d5d94SLen Brown return -7; 1172fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1173fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1174fb5d4327SDasaratharaman Chandramouli return -7; 1175103a8feaSLen Brown } 117615aaa346SLen Brown 117715aaa346SLen Brown if (do_snb_cstates) 1178c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1179c98d5d94SLen Brown return -8; 1180c98d5d94SLen Brown 1181889facbeSLen Brown if (do_dts) { 1182889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1183889facbeSLen Brown return -9; 1184889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1185889facbeSLen Brown } 1186889facbeSLen Brown 1187889facbeSLen Brown 1188c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1189c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1190c98d5d94SLen Brown return 0; 119115aaa346SLen Brown 11920b2bb692SLen Brown if (do_skl_residency) { 11930b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 11940b2bb692SLen Brown return -10; 11950b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 11960b2bb692SLen Brown return -11; 11970b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 11980b2bb692SLen Brown return -12; 11990b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 12000b2bb692SLen Brown return -13; 12010b2bb692SLen Brown } 1202ee7e38e3SLen Brown if (do_pc3) 1203c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1204c98d5d94SLen Brown return -9; 1205ee7e38e3SLen Brown if (do_pc6) 1206c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1207c98d5d94SLen Brown return -10; 1208ee7e38e3SLen Brown if (do_pc2) 1209c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1210c98d5d94SLen Brown return -11; 1211ee7e38e3SLen Brown if (do_pc7) 1212c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1213c98d5d94SLen Brown return -12; 1214ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 1215ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1216ca58710fSKristen Carlson Accardi return -13; 1217ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1218ca58710fSKristen Carlson Accardi return -13; 1219ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1220ca58710fSKristen Carlson Accardi return -13; 1221ca58710fSKristen Carlson Accardi } 1222889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1223889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1224889facbeSLen Brown return -13; 1225889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1226889facbeSLen Brown } 1227889facbeSLen Brown if (do_rapl & RAPL_CORES) { 1228889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1229889facbeSLen Brown return -14; 1230889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1231889facbeSLen Brown } 1232889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1233889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1234889facbeSLen Brown return -15; 1235889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1236889facbeSLen Brown } 1237889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1238889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1239889facbeSLen Brown return -16; 1240889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1241889facbeSLen Brown } 1242889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1243889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1244889facbeSLen Brown return -16; 1245889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1246889facbeSLen Brown } 1247889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1248889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1249889facbeSLen Brown return -16; 1250889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1251889facbeSLen Brown } 1252889facbeSLen Brown if (do_ptm) { 1253889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1254889facbeSLen Brown return -17; 1255889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1256889facbeSLen Brown } 1257fdf676e5SLen Brown 1258fdf676e5SLen Brown if (do_gfx_rc6_ms) 1259fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1260fdf676e5SLen Brown 126127d47356SLen Brown if (do_gfx_mhz) 126227d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 126327d47356SLen Brown 126415aaa346SLen Brown return 0; 1265103a8feaSLen Brown } 1266103a8feaSLen Brown 1267ee7e38e3SLen Brown /* 1268ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1269ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1270ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1271ee7e38e3SLen Brown */ 1272ee7e38e3SLen Brown 1273ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1274ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1275ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1276ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1277ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1278ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1279ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1280ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1281ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1282ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1283ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1284ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 12850b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 12860b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 12870b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1288ee7e38e3SLen Brown 1289ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1290ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 12910b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1292ee7e38e3SLen Brown 1293e9257f5fSLen 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}; 1294e9257f5fSLen 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}; 1295e9257f5fSLen 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}; 1296e9257f5fSLen 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}; 1297e9257f5fSLen 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}; 1298e9257f5fSLen 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}; 1299ee7e38e3SLen Brown 1300a2b7b749SLen Brown 1301a2b7b749SLen Brown static void 1302a2b7b749SLen Brown calculate_tsc_tweak() 1303a2b7b749SLen Brown { 1304a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1305a2b7b749SLen Brown } 1306a2b7b749SLen Brown 1307fcd17211SLen Brown static void 1308fcd17211SLen Brown dump_nhm_platform_info(void) 1309103a8feaSLen Brown { 1310103a8feaSLen Brown unsigned long long msr; 1311103a8feaSLen Brown unsigned int ratio; 1312103a8feaSLen Brown 1313ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1314103a8feaSLen Brown 1315b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 13166574a5d5SLen Brown 1317103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1318b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n", 1319103a8feaSLen Brown ratio, bclk, ratio * bclk); 1320103a8feaSLen Brown 1321103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1322b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n", 1323103a8feaSLen Brown ratio, bclk, ratio * bclk); 1324103a8feaSLen Brown 13257ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1326b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1327bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 132867920418SLen Brown 1329fcd17211SLen Brown return; 1330fcd17211SLen Brown } 1331fcd17211SLen Brown 1332fcd17211SLen Brown static void 1333fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1334fcd17211SLen Brown { 1335fcd17211SLen Brown unsigned long long msr; 1336fcd17211SLen Brown unsigned int ratio; 1337fcd17211SLen Brown 13387ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1339fcd17211SLen Brown 1340b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1341fcd17211SLen Brown 1342fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1343fcd17211SLen Brown if (ratio) 1344b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n", 1345fcd17211SLen Brown ratio, bclk, ratio * bclk); 1346fcd17211SLen Brown 1347fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1348fcd17211SLen Brown if (ratio) 1349b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n", 1350fcd17211SLen Brown ratio, bclk, ratio * bclk); 1351fcd17211SLen Brown return; 1352fcd17211SLen Brown } 1353fcd17211SLen Brown 1354fcd17211SLen Brown static void 1355fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1356fcd17211SLen Brown { 1357fcd17211SLen Brown unsigned long long msr; 1358fcd17211SLen Brown unsigned int ratio; 13596574a5d5SLen Brown 13607ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 13616574a5d5SLen Brown 1362b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 13636574a5d5SLen Brown 13646574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 13656574a5d5SLen Brown if (ratio) 1366b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 13676574a5d5SLen Brown ratio, bclk, ratio * bclk); 13686574a5d5SLen Brown 13696574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 13706574a5d5SLen Brown if (ratio) 1371b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 13726574a5d5SLen Brown ratio, bclk, ratio * bclk); 13736574a5d5SLen Brown 13746574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 13756574a5d5SLen Brown if (ratio) 1376b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 13776574a5d5SLen Brown ratio, bclk, ratio * bclk); 13786574a5d5SLen Brown 13796574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 13806574a5d5SLen Brown if (ratio) 1381b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 13826574a5d5SLen Brown ratio, bclk, ratio * bclk); 13836574a5d5SLen Brown 13846574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 13856574a5d5SLen Brown if (ratio) 1386b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 13876574a5d5SLen Brown ratio, bclk, ratio * bclk); 13886574a5d5SLen Brown 13896574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 13906574a5d5SLen Brown if (ratio) 1391b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 13926574a5d5SLen Brown ratio, bclk, ratio * bclk); 13936574a5d5SLen Brown 13946574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 13956574a5d5SLen Brown if (ratio) 1396b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 13976574a5d5SLen Brown ratio, bclk, ratio * bclk); 13986574a5d5SLen Brown 13996574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 14006574a5d5SLen Brown if (ratio) 1401b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 14026574a5d5SLen Brown ratio, bclk, ratio * bclk); 1403103a8feaSLen Brown return; 1404fcd17211SLen Brown } 1405fcd17211SLen Brown 1406fcd17211SLen Brown static void 1407fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void) 1408fcd17211SLen Brown { 1409fcd17211SLen Brown unsigned long long msr; 1410fcd17211SLen Brown unsigned int ratio; 1411103a8feaSLen Brown 14127ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1413103a8feaSLen Brown 1414b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 14156574a5d5SLen Brown 14166574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 14176574a5d5SLen Brown if (ratio) 1418b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 14196574a5d5SLen Brown ratio, bclk, ratio * bclk); 14206574a5d5SLen Brown 14216574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 14226574a5d5SLen Brown if (ratio) 1423b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 14246574a5d5SLen Brown ratio, bclk, ratio * bclk); 14256574a5d5SLen Brown 14266574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 14276574a5d5SLen Brown if (ratio) 1428b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 14296574a5d5SLen Brown ratio, bclk, ratio * bclk); 14306574a5d5SLen Brown 14316574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 14326574a5d5SLen Brown if (ratio) 1433b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 14346574a5d5SLen Brown ratio, bclk, ratio * bclk); 14356574a5d5SLen Brown 1436103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1437103a8feaSLen Brown if (ratio) 1438b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1439103a8feaSLen Brown ratio, bclk, ratio * bclk); 1440103a8feaSLen Brown 1441103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1442103a8feaSLen Brown if (ratio) 1443b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1444103a8feaSLen Brown ratio, bclk, ratio * bclk); 1445103a8feaSLen Brown 1446103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1447103a8feaSLen Brown if (ratio) 1448b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1449103a8feaSLen Brown ratio, bclk, ratio * bclk); 1450103a8feaSLen Brown 1451103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1452103a8feaSLen Brown if (ratio) 1453b7d8c148SLen Brown fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1454103a8feaSLen Brown ratio, bclk, ratio * bclk); 1455fcd17211SLen Brown return; 1456fcd17211SLen Brown } 14573a9a941dSLen Brown 1458fcd17211SLen Brown static void 1459fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1460fb5d4327SDasaratharaman Chandramouli { 1461cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 1462cbf97abaSHubert Chrzaniuk 1463fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1464cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 1465cbf97abaSHubert Chrzaniuk int i, b_nr; 1466cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 1467cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 1468fb5d4327SDasaratharaman Chandramouli 14697ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1470fb5d4327SDasaratharaman Chandramouli 1471b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 1472bfae2052SLen Brown base_cpu, msr); 1473fb5d4327SDasaratharaman Chandramouli 1474fb5d4327SDasaratharaman Chandramouli /** 1475fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1476cbf97abaSHubert Chrzaniuk * [0] -- Reserved 1477cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 1478fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1479fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1480fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1481fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1482fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1483fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1484fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1485fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1486fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1487fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1488fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1489fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1490fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1491fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1492fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1493fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 1494fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 1495fb5d4327SDasaratharaman Chandramouli */ 1496fb5d4327SDasaratharaman Chandramouli 1497cbf97abaSHubert Chrzaniuk b_nr = 0; 1498cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 1499cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 1500cbf97abaSHubert Chrzaniuk 1501cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 1502fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 1503cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 1504fb5d4327SDasaratharaman Chandramouli 1505cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 1506cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 1507cbf97abaSHubert Chrzaniuk b_nr++; 1508cbf97abaSHubert Chrzaniuk } 1509cbf97abaSHubert Chrzaniuk 1510cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 1511cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 1512b7d8c148SLen Brown fprintf(outf, 1513fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1514cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 1515fb5d4327SDasaratharaman Chandramouli } 1516fb5d4327SDasaratharaman Chandramouli 1517fb5d4327SDasaratharaman Chandramouli static void 1518fcd17211SLen Brown dump_nhm_cst_cfg(void) 1519fcd17211SLen Brown { 1520fcd17211SLen Brown unsigned long long msr; 1521fcd17211SLen Brown 15227ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1523fcd17211SLen Brown 1524fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1525fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1526fcd17211SLen Brown 1527b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr); 1528fcd17211SLen Brown 1529b7d8c148SLen Brown fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1530fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1531fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1532fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1533fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1534fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 1535fcd17211SLen Brown (unsigned int)msr & 7, 1536fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1537fcd17211SLen Brown return; 1538103a8feaSLen Brown } 1539103a8feaSLen Brown 15406fb3143bSLen Brown static void 15416fb3143bSLen Brown dump_config_tdp(void) 15426fb3143bSLen Brown { 15436fb3143bSLen Brown unsigned long long msr; 15446fb3143bSLen Brown 15456fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 1546b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 1547b7d8c148SLen Brown fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF); 15486fb3143bSLen Brown 15496fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 1550b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 15516fb3143bSLen Brown if (msr) { 1552b7d8c148SLen Brown fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF); 1553b7d8c148SLen Brown fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF); 1554b7d8c148SLen Brown fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); 1555b7d8c148SLen Brown fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF); 15566fb3143bSLen Brown } 1557b7d8c148SLen Brown fprintf(outf, ")\n"); 15586fb3143bSLen Brown 15596fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 1560b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 15616fb3143bSLen Brown if (msr) { 1562b7d8c148SLen Brown fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF); 1563b7d8c148SLen Brown fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF); 1564b7d8c148SLen Brown fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); 1565b7d8c148SLen Brown fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF); 15666fb3143bSLen Brown } 1567b7d8c148SLen Brown fprintf(outf, ")\n"); 15686fb3143bSLen Brown 15696fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 1570b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 15716fb3143bSLen Brown if ((msr) & 0x3) 1572b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 1573b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1574b7d8c148SLen Brown fprintf(outf, ")\n"); 15756fb3143bSLen Brown 15766fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 1577b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 1578b7d8c148SLen Brown fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F); 1579b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1580b7d8c148SLen Brown fprintf(outf, ")\n"); 15816fb3143bSLen Brown } 158236229897SLen Brown void free_fd_percpu(void) 158336229897SLen Brown { 158436229897SLen Brown int i; 158536229897SLen Brown 158636229897SLen Brown for (i = 0; i < topo.max_cpu_num; ++i) { 158736229897SLen Brown if (fd_percpu[i] != 0) 158836229897SLen Brown close(fd_percpu[i]); 158936229897SLen Brown } 159036229897SLen Brown 159136229897SLen Brown free(fd_percpu); 159236229897SLen Brown } 15936fb3143bSLen Brown 1594c98d5d94SLen Brown void free_all_buffers(void) 1595103a8feaSLen Brown { 1596c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1597c98d5d94SLen Brown cpu_present_set = NULL; 159836229897SLen Brown cpu_present_setsize = 0; 1599103a8feaSLen Brown 1600c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1601c98d5d94SLen Brown cpu_affinity_set = NULL; 1602c98d5d94SLen Brown cpu_affinity_setsize = 0; 1603103a8feaSLen Brown 1604c98d5d94SLen Brown free(thread_even); 1605c98d5d94SLen Brown free(core_even); 1606c98d5d94SLen Brown free(package_even); 1607103a8feaSLen Brown 1608c98d5d94SLen Brown thread_even = NULL; 1609c98d5d94SLen Brown core_even = NULL; 1610c98d5d94SLen Brown package_even = NULL; 1611103a8feaSLen Brown 1612c98d5d94SLen Brown free(thread_odd); 1613c98d5d94SLen Brown free(core_odd); 1614c98d5d94SLen Brown free(package_odd); 1615103a8feaSLen Brown 1616c98d5d94SLen Brown thread_odd = NULL; 1617c98d5d94SLen Brown core_odd = NULL; 1618c98d5d94SLen Brown package_odd = NULL; 1619103a8feaSLen Brown 1620c98d5d94SLen Brown free(output_buffer); 1621c98d5d94SLen Brown output_buffer = NULL; 1622c98d5d94SLen Brown outp = NULL; 162336229897SLen Brown 162436229897SLen Brown free_fd_percpu(); 1625562a2d37SLen Brown 1626562a2d37SLen Brown free(irq_column_2_cpu); 1627562a2d37SLen Brown free(irqs_per_cpu); 1628103a8feaSLen Brown } 1629103a8feaSLen Brown 1630103a8feaSLen Brown /* 163157a42a34SJosh Triplett * Open a file, and exit on failure 163257a42a34SJosh Triplett */ 163357a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 163457a42a34SJosh Triplett { 1635b7d8c148SLen Brown FILE *filep = fopen(path, mode); 1636b2c95d90SJosh Triplett if (!filep) 1637b2c95d90SJosh Triplett err(1, "%s: open failed", path); 163857a42a34SJosh Triplett return filep; 163957a42a34SJosh Triplett } 164057a42a34SJosh Triplett 164157a42a34SJosh Triplett /* 164295aebc44SJosh Triplett * Parse a file containing a single int. 164395aebc44SJosh Triplett */ 164495aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 164595aebc44SJosh Triplett { 164695aebc44SJosh Triplett va_list args; 164795aebc44SJosh Triplett char path[PATH_MAX]; 164895aebc44SJosh Triplett FILE *filep; 164995aebc44SJosh Triplett int value; 165095aebc44SJosh Triplett 165195aebc44SJosh Triplett va_start(args, fmt); 165295aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 165395aebc44SJosh Triplett va_end(args); 165457a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1655b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1656b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 165795aebc44SJosh Triplett fclose(filep); 165895aebc44SJosh Triplett return value; 165995aebc44SJosh Triplett } 166095aebc44SJosh Triplett 166195aebc44SJosh Triplett /* 1662e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 1663e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 1664e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 1665103a8feaSLen Brown */ 1666e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 1667103a8feaSLen Brown { 1668e275b388SDasaratharaman Chandramouli char path[64]; 1669e275b388SDasaratharaman Chandramouli FILE *filep; 1670e275b388SDasaratharaman Chandramouli int this_cpu; 1671e275b388SDasaratharaman Chandramouli char character; 1672e275b388SDasaratharaman Chandramouli int i; 1673e275b388SDasaratharaman Chandramouli 1674e275b388SDasaratharaman Chandramouli sprintf(path, 1675e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 1676e275b388SDasaratharaman Chandramouli cpu); 1677e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 1678e275b388SDasaratharaman Chandramouli if (filep == NULL) { 1679e275b388SDasaratharaman Chandramouli perror(path); 1680e275b388SDasaratharaman Chandramouli exit(1); 1681e275b388SDasaratharaman Chandramouli } 1682e275b388SDasaratharaman Chandramouli 1683e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 1684e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 1685e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 1686e275b388SDasaratharaman Chandramouli fclose(filep); 1687e275b388SDasaratharaman Chandramouli return i; 1688e275b388SDasaratharaman Chandramouli } 1689e275b388SDasaratharaman Chandramouli 1690e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 1691e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 1692e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 1693e275b388SDasaratharaman Chandramouli } 1694e275b388SDasaratharaman Chandramouli 1695e275b388SDasaratharaman Chandramouli fclose(filep); 1696e275b388SDasaratharaman Chandramouli return -1; 1697c98d5d94SLen Brown } 1698103a8feaSLen Brown 1699c98d5d94SLen Brown /* 1700c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1701c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1702c98d5d94SLen Brown */ 1703c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1704c98d5d94SLen Brown { 170595aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1706103a8feaSLen Brown } 1707103a8feaSLen Brown 1708103a8feaSLen Brown int get_physical_package_id(int cpu) 1709103a8feaSLen Brown { 171095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1711103a8feaSLen Brown } 1712103a8feaSLen Brown 1713103a8feaSLen Brown int get_core_id(int cpu) 1714103a8feaSLen Brown { 171595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1716103a8feaSLen Brown } 1717103a8feaSLen Brown 1718c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1719c98d5d94SLen Brown { 1720c98d5d94SLen Brown char path[80]; 1721c98d5d94SLen Brown FILE *filep; 1722e275b388SDasaratharaman Chandramouli int sib1; 1723e275b388SDasaratharaman Chandramouli int matches = 0; 1724c98d5d94SLen Brown char character; 1725e275b388SDasaratharaman Chandramouli char str[100]; 1726e275b388SDasaratharaman Chandramouli char *ch; 1727c98d5d94SLen Brown 1728c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 172957a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1730e275b388SDasaratharaman Chandramouli 1731103a8feaSLen Brown /* 1732c98d5d94SLen Brown * file format: 1733e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 1734e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 1735c98d5d94SLen Brown */ 1736e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 1737e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 1738e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 1739e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 1740e275b388SDasaratharaman Chandramouli while (ch != NULL) { 1741e275b388SDasaratharaman Chandramouli matches++; 1742e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 1743e275b388SDasaratharaman Chandramouli } 1744c98d5d94SLen Brown 1745c98d5d94SLen Brown fclose(filep); 1746e275b388SDasaratharaman Chandramouli return matches+1; 1747c98d5d94SLen Brown } 1748c98d5d94SLen Brown 1749c98d5d94SLen Brown /* 1750c98d5d94SLen Brown * run func(thread, core, package) in topology order 1751c98d5d94SLen Brown * skip non-present cpus 1752103a8feaSLen Brown */ 1753103a8feaSLen Brown 1754c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1755c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1756c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1757c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1758c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1759c98d5d94SLen Brown struct pkg_data *pkg_base2) 1760c98d5d94SLen Brown { 1761c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1762c98d5d94SLen Brown 1763c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1764c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1765c98d5d94SLen Brown for (thread_no = 0; thread_no < 1766c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1767c98d5d94SLen Brown struct thread_data *t, *t2; 1768c98d5d94SLen Brown struct core_data *c, *c2; 1769c98d5d94SLen Brown struct pkg_data *p, *p2; 1770c98d5d94SLen Brown 1771c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1772c98d5d94SLen Brown 1773c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1774c98d5d94SLen Brown continue; 1775c98d5d94SLen Brown 1776c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1777c98d5d94SLen Brown 1778c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1779c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1780c98d5d94SLen Brown 1781c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1782c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1783c98d5d94SLen Brown 1784c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1785c98d5d94SLen Brown if (retval) 1786c98d5d94SLen Brown return retval; 1787c98d5d94SLen Brown } 1788c98d5d94SLen Brown } 1789c98d5d94SLen Brown } 1790c98d5d94SLen Brown return 0; 1791c98d5d94SLen Brown } 1792c98d5d94SLen Brown 1793c98d5d94SLen Brown /* 1794c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1795c98d5d94SLen Brown * return max_cpu number 1796c98d5d94SLen Brown */ 1797c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1798103a8feaSLen Brown { 1799103a8feaSLen Brown FILE *fp; 1800c98d5d94SLen Brown int cpu_num; 1801103a8feaSLen Brown int retval; 1802103a8feaSLen Brown 180357a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1804103a8feaSLen Brown 1805103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1806b2c95d90SJosh Triplett if (retval != 0) 1807b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1808103a8feaSLen Brown 1809c98d5d94SLen Brown while (1) { 1810c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1811103a8feaSLen Brown if (retval != 1) 1812103a8feaSLen Brown break; 1813103a8feaSLen Brown 1814c98d5d94SLen Brown retval = func(cpu_num); 1815c98d5d94SLen Brown if (retval) { 1816c98d5d94SLen Brown fclose(fp); 1817c98d5d94SLen Brown return(retval); 1818c98d5d94SLen Brown } 1819103a8feaSLen Brown } 1820103a8feaSLen Brown fclose(fp); 1821c98d5d94SLen Brown return 0; 1822103a8feaSLen Brown } 1823103a8feaSLen Brown 1824103a8feaSLen Brown void re_initialize(void) 1825103a8feaSLen Brown { 1826c98d5d94SLen Brown free_all_buffers(); 1827c98d5d94SLen Brown setup_all_buffers(); 1828c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1829103a8feaSLen Brown } 1830103a8feaSLen Brown 1831c98d5d94SLen Brown 1832103a8feaSLen Brown /* 1833c98d5d94SLen Brown * count_cpus() 1834c98d5d94SLen Brown * remember the last one seen, it will be the max 1835103a8feaSLen Brown */ 1836c98d5d94SLen Brown int count_cpus(int cpu) 1837103a8feaSLen Brown { 1838c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1839c98d5d94SLen Brown topo.max_cpu_num = cpu; 1840103a8feaSLen Brown 1841c98d5d94SLen Brown topo.num_cpus += 1; 1842c98d5d94SLen Brown return 0; 1843103a8feaSLen Brown } 1844c98d5d94SLen Brown int mark_cpu_present(int cpu) 1845c98d5d94SLen Brown { 1846c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 184715aaa346SLen Brown return 0; 1848103a8feaSLen Brown } 1849103a8feaSLen Brown 1850562a2d37SLen Brown /* 1851562a2d37SLen Brown * snapshot_proc_interrupts() 1852562a2d37SLen Brown * 1853562a2d37SLen Brown * read and record summary of /proc/interrupts 1854562a2d37SLen Brown * 1855562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 1856562a2d37SLen Brown */ 1857562a2d37SLen Brown int snapshot_proc_interrupts(void) 1858562a2d37SLen Brown { 1859562a2d37SLen Brown static FILE *fp; 1860562a2d37SLen Brown int column, retval; 1861562a2d37SLen Brown 1862562a2d37SLen Brown if (fp == NULL) 1863562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 1864562a2d37SLen Brown else 1865562a2d37SLen Brown rewind(fp); 1866562a2d37SLen Brown 1867562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 1868562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 1869562a2d37SLen Brown int cpu_number; 1870562a2d37SLen Brown 1871562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 1872562a2d37SLen Brown if (retval != 1) 1873562a2d37SLen Brown break; 1874562a2d37SLen Brown 1875562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 1876562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 1877562a2d37SLen Brown return 1; 1878562a2d37SLen Brown } 1879562a2d37SLen Brown 1880562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 1881562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 1882562a2d37SLen Brown } 1883562a2d37SLen Brown 1884562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 1885562a2d37SLen Brown while (1) { 1886562a2d37SLen Brown int column; 1887562a2d37SLen Brown char buf[64]; 1888562a2d37SLen Brown 1889562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 1890562a2d37SLen Brown if (retval != 1) 1891562a2d37SLen Brown break; 1892562a2d37SLen Brown 1893562a2d37SLen Brown /* read the count per cpu */ 1894562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 1895562a2d37SLen Brown 1896562a2d37SLen Brown int cpu_number, irq_count; 1897562a2d37SLen Brown 1898562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 1899562a2d37SLen Brown if (retval != 1) 1900562a2d37SLen Brown break; 1901562a2d37SLen Brown 1902562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 1903562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 1904562a2d37SLen Brown 1905562a2d37SLen Brown } 1906562a2d37SLen Brown 1907562a2d37SLen Brown while (getc(fp) != '\n') 1908562a2d37SLen Brown ; /* flush interrupt description */ 1909562a2d37SLen Brown 1910562a2d37SLen Brown } 1911562a2d37SLen Brown return 0; 1912562a2d37SLen Brown } 191327d47356SLen Brown /* 1914fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 1915fdf676e5SLen Brown * 1916fdf676e5SLen Brown * record snapshot of 1917fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 1918fdf676e5SLen Brown * 1919fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 1920fdf676e5SLen Brown */ 1921fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 1922fdf676e5SLen Brown { 1923fdf676e5SLen Brown FILE *fp; 1924fdf676e5SLen Brown int retval; 1925fdf676e5SLen Brown 1926fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 1927fdf676e5SLen Brown 1928fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 1929fdf676e5SLen Brown if (retval != 1) 1930fdf676e5SLen Brown err(1, "GFX rc6"); 1931fdf676e5SLen Brown 1932fdf676e5SLen Brown fclose(fp); 1933fdf676e5SLen Brown 1934fdf676e5SLen Brown return 0; 1935fdf676e5SLen Brown } 1936fdf676e5SLen Brown /* 193727d47356SLen Brown * snapshot_gfx_mhz() 193827d47356SLen Brown * 193927d47356SLen Brown * record snapshot of 194027d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 194127d47356SLen Brown * 194227d47356SLen Brown * return 1 if config change requires a restart, else return 0 194327d47356SLen Brown */ 194427d47356SLen Brown int snapshot_gfx_mhz(void) 194527d47356SLen Brown { 194627d47356SLen Brown static FILE *fp; 194727d47356SLen Brown int retval; 194827d47356SLen Brown 194927d47356SLen Brown if (fp == NULL) 195027d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 195127d47356SLen Brown else 195227d47356SLen Brown rewind(fp); 195327d47356SLen Brown 195427d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 195527d47356SLen Brown if (retval != 1) 195627d47356SLen Brown err(1, "GFX MHz"); 195727d47356SLen Brown 195827d47356SLen Brown return 0; 195927d47356SLen Brown } 1960562a2d37SLen Brown 1961562a2d37SLen Brown /* 1962562a2d37SLen Brown * snapshot /proc and /sys files 1963562a2d37SLen Brown * 1964562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 1965562a2d37SLen Brown */ 1966562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 1967562a2d37SLen Brown { 1968562a2d37SLen Brown if (snapshot_proc_interrupts()) 1969562a2d37SLen Brown return 1; 1970562a2d37SLen Brown 1971fdf676e5SLen Brown if (do_gfx_rc6_ms) 1972fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 1973fdf676e5SLen Brown 197427d47356SLen Brown if (do_gfx_mhz) 197527d47356SLen Brown snapshot_gfx_mhz(); 197627d47356SLen Brown 1977562a2d37SLen Brown return 0; 1978562a2d37SLen Brown } 1979562a2d37SLen Brown 1980103a8feaSLen Brown void turbostat_loop() 1981103a8feaSLen Brown { 1982c98d5d94SLen Brown int retval; 1983e52966c0SLen Brown int restarted = 0; 1984c98d5d94SLen Brown 1985103a8feaSLen Brown restart: 1986e52966c0SLen Brown restarted++; 1987e52966c0SLen Brown 1988562a2d37SLen Brown snapshot_proc_sysfs_files(); 1989c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1990d91bb17cSLen Brown if (retval < -1) { 1991d91bb17cSLen Brown exit(retval); 1992d91bb17cSLen Brown } else if (retval == -1) { 1993e52966c0SLen Brown if (restarted > 1) { 1994e52966c0SLen Brown exit(retval); 1995e52966c0SLen Brown } 1996c98d5d94SLen Brown re_initialize(); 1997c98d5d94SLen Brown goto restart; 1998c98d5d94SLen Brown } 1999e52966c0SLen Brown restarted = 0; 2000103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2001103a8feaSLen Brown 2002103a8feaSLen Brown while (1) { 2003c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2004103a8feaSLen Brown re_initialize(); 2005103a8feaSLen Brown goto restart; 2006103a8feaSLen Brown } 20072a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2008562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2009562a2d37SLen Brown goto restart; 2010c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2011d91bb17cSLen Brown if (retval < -1) { 2012d91bb17cSLen Brown exit(retval); 2013d91bb17cSLen Brown } else if (retval == -1) { 201415aaa346SLen Brown re_initialize(); 201515aaa346SLen Brown goto restart; 201615aaa346SLen Brown } 2017103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2018103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2019c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2020c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2021c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2022b7d8c148SLen Brown flush_output_stdout(); 20232a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2024562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2025562a2d37SLen Brown goto restart; 2026c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2027d91bb17cSLen Brown if (retval < -1) { 2028d91bb17cSLen Brown exit(retval); 2029d91bb17cSLen Brown } else if (retval == -1) { 2030103a8feaSLen Brown re_initialize(); 2031103a8feaSLen Brown goto restart; 2032103a8feaSLen Brown } 2033103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2034103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 2035c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 2036c98d5d94SLen Brown compute_average(ODD_COUNTERS); 2037c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 2038b7d8c148SLen Brown flush_output_stdout(); 2039103a8feaSLen Brown } 2040103a8feaSLen Brown } 2041103a8feaSLen Brown 2042103a8feaSLen Brown void check_dev_msr() 2043103a8feaSLen Brown { 2044103a8feaSLen Brown struct stat sb; 20457ce7d5deSPrarit Bhargava char pathname[32]; 2046103a8feaSLen Brown 20477ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 20487ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 2049a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 2050d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 2051103a8feaSLen Brown } 2052103a8feaSLen Brown 205398481e79SLen Brown void check_permissions() 2054103a8feaSLen Brown { 205598481e79SLen Brown struct __user_cap_header_struct cap_header_data; 205698481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 205798481e79SLen Brown struct __user_cap_data_struct cap_data_data; 205898481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 205998481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 206098481e79SLen Brown int do_exit = 0; 20617ce7d5deSPrarit Bhargava char pathname[32]; 206298481e79SLen Brown 206398481e79SLen Brown /* check for CAP_SYS_RAWIO */ 206498481e79SLen Brown cap_header->pid = getpid(); 206598481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 206698481e79SLen Brown if (capget(cap_header, cap_data) < 0) 206798481e79SLen Brown err(-6, "capget(2) failed"); 206898481e79SLen Brown 206998481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 207098481e79SLen Brown do_exit++; 207198481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 207298481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 207398481e79SLen Brown } 207498481e79SLen Brown 207598481e79SLen Brown /* test file permissions */ 20767ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 20777ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 207898481e79SLen Brown do_exit++; 207998481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 208098481e79SLen Brown } 208198481e79SLen Brown 208298481e79SLen Brown /* if all else fails, thell them to be root */ 208398481e79SLen Brown if (do_exit) 2084b2c95d90SJosh Triplett if (getuid() != 0) 2085d7899447SLen Brown warnx("... or simply run as root"); 208698481e79SLen Brown 208798481e79SLen Brown if (do_exit) 208898481e79SLen Brown exit(-6); 2089103a8feaSLen Brown } 2090103a8feaSLen Brown 2091d7899447SLen Brown /* 2092d7899447SLen Brown * NHM adds support for additional MSRs: 2093d7899447SLen Brown * 2094d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 2095d7899447SLen Brown * 2096ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 2097d7899447SLen Brown * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 2098d7899447SLen Brown * 2099d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 2100d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 2101d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 2102d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 2103d7899447SLen Brown * 2104ee7e38e3SLen Brown * Side effect: 2105ee7e38e3SLen Brown * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL 2106d7899447SLen Brown */ 2107ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 2108103a8feaSLen Brown { 2109ee7e38e3SLen Brown unsigned long long msr; 211021ed5574SLen Brown unsigned int base_ratio; 2111ee7e38e3SLen Brown int *pkg_cstate_limits; 2112ee7e38e3SLen Brown 2113103a8feaSLen Brown if (!genuine_intel) 2114103a8feaSLen Brown return 0; 2115103a8feaSLen Brown 2116103a8feaSLen Brown if (family != 6) 2117103a8feaSLen Brown return 0; 2118103a8feaSLen Brown 211921ed5574SLen Brown bclk = discover_bclk(family, model); 212021ed5574SLen Brown 2121103a8feaSLen Brown switch (model) { 2122103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 2123103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 2124103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 2125103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 2126103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 2127ee7e38e3SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 2128ee7e38e3SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 2129ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 2130ee7e38e3SLen Brown break; 2131103a8feaSLen Brown case 0x2A: /* SNB */ 2132103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 2133553575f1SLen Brown case 0x3A: /* IVB */ 21341300651bSLen Brown case 0x3E: /* IVB Xeon */ 2135ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 2136ee7e38e3SLen Brown break; 213770b43400SLen Brown case 0x3C: /* HSW */ 2138e6f9bb3cSLen Brown case 0x3F: /* HSX */ 213970b43400SLen Brown case 0x45: /* HSW */ 2140149c2319SLen Brown case 0x46: /* HSW */ 21414e8e863fSLen Brown case 0x3D: /* BDW */ 214248a0631cSLen Brown case 0x47: /* BDW */ 21434e8e863fSLen Brown case 0x4F: /* BDX */ 21444e8e863fSLen Brown case 0x56: /* BDX-DE */ 21450b2bb692SLen Brown case 0x4E: /* SKL */ 21460b2bb692SLen Brown case 0x5E: /* SKL */ 2147ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 2148ee7e38e3SLen Brown break; 2149ee7e38e3SLen Brown case 0x37: /* BYT */ 2150ee7e38e3SLen Brown case 0x4D: /* AVN */ 2151ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 2152ee7e38e3SLen Brown break; 2153ee7e38e3SLen Brown case 0x4C: /* AMT */ 2154ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 2155ee7e38e3SLen Brown break; 2156ee7e38e3SLen Brown case 0x57: /* PHI */ 2157ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 2158ee7e38e3SLen Brown break; 2159103a8feaSLen Brown default: 2160103a8feaSLen Brown return 0; 2161103a8feaSLen Brown } 21627ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 2163e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 2164ee7e38e3SLen Brown 2165ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 216621ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 216721ed5574SLen Brown 216821ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 216921ed5574SLen Brown has_base_hz = 1; 2170ee7e38e3SLen Brown return 1; 2171103a8feaSLen Brown } 2172d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) 2173d7899447SLen Brown { 2174d7899447SLen Brown switch (model) { 2175d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 2176d7899447SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 2177d7899447SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 2178cbf97abaSHubert Chrzaniuk case 0x57: /* PHI - Knights Landing (different MSR definition) */ 2179d7899447SLen Brown return 0; 2180d7899447SLen Brown default: 2181d7899447SLen Brown return 1; 2182d7899447SLen Brown } 2183d7899447SLen Brown } 21846574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 21856574a5d5SLen Brown { 21866574a5d5SLen Brown if (!genuine_intel) 21876574a5d5SLen Brown return 0; 21886574a5d5SLen Brown 21896574a5d5SLen Brown if (family != 6) 21906574a5d5SLen Brown return 0; 21916574a5d5SLen Brown 21926574a5d5SLen Brown switch (model) { 21936574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 2194fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 21956574a5d5SLen Brown return 1; 21966574a5d5SLen Brown default: 21976574a5d5SLen Brown return 0; 21986574a5d5SLen Brown } 21996574a5d5SLen Brown } 2200fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 2201fcd17211SLen Brown { 2202fcd17211SLen Brown if (!genuine_intel) 2203fcd17211SLen Brown return 0; 2204fcd17211SLen Brown 2205fcd17211SLen Brown if (family != 6) 2206fcd17211SLen Brown return 0; 2207fcd17211SLen Brown 2208fcd17211SLen Brown switch (model) { 2209fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 2210fcd17211SLen Brown return 1; 2211fcd17211SLen Brown default: 2212fcd17211SLen Brown return 0; 2213fcd17211SLen Brown } 2214fcd17211SLen Brown } 2215fcd17211SLen Brown 2216fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 2217fb5d4327SDasaratharaman Chandramouli { 2218fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2219fb5d4327SDasaratharaman Chandramouli return 0; 2220fb5d4327SDasaratharaman Chandramouli 2221fb5d4327SDasaratharaman Chandramouli if (family != 6) 2222fb5d4327SDasaratharaman Chandramouli return 0; 2223fb5d4327SDasaratharaman Chandramouli 2224fb5d4327SDasaratharaman Chandramouli switch (model) { 2225fb5d4327SDasaratharaman Chandramouli case 0x57: /* Knights Landing */ 2226fb5d4327SDasaratharaman Chandramouli return 1; 2227fb5d4327SDasaratharaman Chandramouli default: 2228fb5d4327SDasaratharaman Chandramouli return 0; 2229fb5d4327SDasaratharaman Chandramouli } 2230fb5d4327SDasaratharaman Chandramouli } 22316fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 22326fb3143bSLen Brown { 22336fb3143bSLen Brown if (!genuine_intel) 22346fb3143bSLen Brown return 0; 22356fb3143bSLen Brown 22366fb3143bSLen Brown if (family != 6) 22376fb3143bSLen Brown return 0; 22386fb3143bSLen Brown 22396fb3143bSLen Brown switch (model) { 22406fb3143bSLen Brown case 0x3A: /* IVB */ 22416fb3143bSLen Brown case 0x3C: /* HSW */ 22426fb3143bSLen Brown case 0x3F: /* HSX */ 22436fb3143bSLen Brown case 0x45: /* HSW */ 22446fb3143bSLen Brown case 0x46: /* HSW */ 22456fb3143bSLen Brown case 0x3D: /* BDW */ 22466fb3143bSLen Brown case 0x47: /* BDW */ 22476fb3143bSLen Brown case 0x4F: /* BDX */ 22486fb3143bSLen Brown case 0x56: /* BDX-DE */ 22496fb3143bSLen Brown case 0x4E: /* SKL */ 22506fb3143bSLen Brown case 0x5E: /* SKL */ 22516fb3143bSLen Brown 22526fb3143bSLen Brown case 0x57: /* Knights Landing */ 22536fb3143bSLen Brown return 1; 22546fb3143bSLen Brown default: 22556fb3143bSLen Brown return 0; 22566fb3143bSLen Brown } 22576fb3143bSLen Brown } 22586fb3143bSLen Brown 2259fcd17211SLen Brown static void 226058cc30a4SLen Brown dump_cstate_pstate_config_info(int family, int model) 2261fcd17211SLen Brown { 2262fcd17211SLen Brown if (!do_nhm_platform_info) 2263fcd17211SLen Brown return; 2264fcd17211SLen Brown 2265fcd17211SLen Brown dump_nhm_platform_info(); 2266fcd17211SLen Brown 2267fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 2268fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 2269fcd17211SLen Brown 2270fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 2271fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 2272fcd17211SLen Brown 2273fcd17211SLen Brown if (has_nhm_turbo_ratio_limit(family, model)) 2274fcd17211SLen Brown dump_nhm_turbo_ratio_limits(); 2275fcd17211SLen Brown 2276fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 2277fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 2278fb5d4327SDasaratharaman Chandramouli 22796fb3143bSLen Brown if (has_config_tdp(family, model)) 22806fb3143bSLen Brown dump_config_tdp(); 22816fb3143bSLen Brown 2282fcd17211SLen Brown dump_nhm_cst_cfg(); 2283fcd17211SLen Brown } 2284fcd17211SLen Brown 22856574a5d5SLen Brown 2286889facbeSLen Brown /* 2287889facbeSLen Brown * print_epb() 2288889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 2289889facbeSLen Brown */ 2290889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2291889facbeSLen Brown { 2292889facbeSLen Brown unsigned long long msr; 2293889facbeSLen Brown char *epb_string; 2294889facbeSLen Brown int cpu; 2295889facbeSLen Brown 2296889facbeSLen Brown if (!has_epb) 2297889facbeSLen Brown return 0; 2298889facbeSLen Brown 2299889facbeSLen Brown cpu = t->cpu_id; 2300889facbeSLen Brown 2301889facbeSLen Brown /* EPB is per-package */ 2302889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2303889facbeSLen Brown return 0; 2304889facbeSLen Brown 2305889facbeSLen Brown if (cpu_migrate(cpu)) { 2306b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2307889facbeSLen Brown return -1; 2308889facbeSLen Brown } 2309889facbeSLen Brown 2310889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 2311889facbeSLen Brown return 0; 2312889facbeSLen Brown 2313e9be7dd6SLen Brown switch (msr & 0xF) { 2314889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 2315889facbeSLen Brown epb_string = "performance"; 2316889facbeSLen Brown break; 2317889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 2318889facbeSLen Brown epb_string = "balanced"; 2319889facbeSLen Brown break; 2320889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 2321889facbeSLen Brown epb_string = "powersave"; 2322889facbeSLen Brown break; 2323889facbeSLen Brown default: 2324889facbeSLen Brown epb_string = "custom"; 2325889facbeSLen Brown break; 2326889facbeSLen Brown } 2327b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 2328889facbeSLen Brown 2329889facbeSLen Brown return 0; 2330889facbeSLen Brown } 23317f5c258eSLen Brown /* 23327f5c258eSLen Brown * print_hwp() 23337f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 23347f5c258eSLen Brown */ 23357f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 23367f5c258eSLen Brown { 23377f5c258eSLen Brown unsigned long long msr; 23387f5c258eSLen Brown int cpu; 23397f5c258eSLen Brown 23407f5c258eSLen Brown if (!has_hwp) 23417f5c258eSLen Brown return 0; 23427f5c258eSLen Brown 23437f5c258eSLen Brown cpu = t->cpu_id; 23447f5c258eSLen Brown 23457f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 23467f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 23477f5c258eSLen Brown return 0; 23487f5c258eSLen Brown 23497f5c258eSLen Brown if (cpu_migrate(cpu)) { 2350b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 23517f5c258eSLen Brown return -1; 23527f5c258eSLen Brown } 23537f5c258eSLen Brown 23547f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 23557f5c258eSLen Brown return 0; 23567f5c258eSLen Brown 2357b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 23587f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 23597f5c258eSLen Brown 23607f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 23617f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 23627f5c258eSLen Brown return 0; 23637f5c258eSLen Brown 23647f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 23657f5c258eSLen Brown return 0; 23667f5c258eSLen Brown 2367b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 23687f5c258eSLen Brown "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n", 23697f5c258eSLen Brown cpu, msr, 23707f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 23717f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 23727f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 23737f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 23747f5c258eSLen Brown 23757f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 23767f5c258eSLen Brown return 0; 23777f5c258eSLen Brown 2378b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 23797f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n", 23807f5c258eSLen Brown cpu, msr, 23817f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 23827f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 23837f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 23847f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 23857f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 23867f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 23877f5c258eSLen Brown 23887f5c258eSLen Brown if (has_hwp_pkg) { 23897f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 23907f5c258eSLen Brown return 0; 23917f5c258eSLen Brown 2392b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 23937f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n", 23947f5c258eSLen Brown cpu, msr, 23957f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 23967f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 23977f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 23987f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 23997f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 24007f5c258eSLen Brown } 24017f5c258eSLen Brown if (has_hwp_notify) { 24027f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 24037f5c258eSLen Brown return 0; 24047f5c258eSLen Brown 2405b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 24067f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 24077f5c258eSLen Brown cpu, msr, 24087f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 24097f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 24107f5c258eSLen Brown } 24117f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 24127f5c258eSLen Brown return 0; 24137f5c258eSLen Brown 2414b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 24157f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 24167f5c258eSLen Brown cpu, msr, 24177f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 24187f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 24197f5c258eSLen Brown 24207f5c258eSLen Brown return 0; 24217f5c258eSLen Brown } 2422889facbeSLen Brown 24233a9a941dSLen Brown /* 24243a9a941dSLen Brown * print_perf_limit() 24253a9a941dSLen Brown */ 24263a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 24273a9a941dSLen Brown { 24283a9a941dSLen Brown unsigned long long msr; 24293a9a941dSLen Brown int cpu; 24303a9a941dSLen Brown 24313a9a941dSLen Brown cpu = t->cpu_id; 24323a9a941dSLen Brown 24333a9a941dSLen Brown /* per-package */ 24343a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 24353a9a941dSLen Brown return 0; 24363a9a941dSLen Brown 24373a9a941dSLen Brown if (cpu_migrate(cpu)) { 2438b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 24393a9a941dSLen Brown return -1; 24403a9a941dSLen Brown } 24413a9a941dSLen Brown 24423a9a941dSLen Brown if (do_core_perf_limit_reasons) { 24433a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 2444b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2445b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 2446e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 24473a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 2448e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 2449e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 2450e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 2451e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 2452e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 2453e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 2454e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 2455e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 2456e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 2457e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 2458e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 2459e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 2460b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 2461e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 24623a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 2463e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 2464e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 2465e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 2466e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 2467e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 2468e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 2469e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 2470e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 2471e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 2472e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 2473e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 2474e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 24753a9a941dSLen Brown 24763a9a941dSLen Brown } 24773a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 24783a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 2479b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2480b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 24813a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 24823a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 24833a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 24843a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 24853a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 24863a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 24873a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 24883a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 2489b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 24903a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 24913a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 24923a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 24933a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 24943a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 24953a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 24963a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 24973a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 24983a9a941dSLen Brown } 24993a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 25003a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 2501b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 2502b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 25033a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 25043a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 25053a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 25063a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 25073a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 25083a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 2509b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 25103a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 25113a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 25123a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 25133a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 25143a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 25153a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 25163a9a941dSLen Brown } 25173a9a941dSLen Brown return 0; 25183a9a941dSLen Brown } 25193a9a941dSLen Brown 2520889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 2521889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 2522889facbeSLen Brown 252358cc30a4SLen Brown double get_tdp(int model) 2524144b44b1SLen Brown { 2525144b44b1SLen Brown unsigned long long msr; 2526144b44b1SLen Brown 2527144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 25287ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 2529144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 2530144b44b1SLen Brown 2531144b44b1SLen Brown switch (model) { 2532144b44b1SLen Brown case 0x37: 2533144b44b1SLen Brown case 0x4D: 2534144b44b1SLen Brown return 30.0; 2535144b44b1SLen Brown default: 2536144b44b1SLen Brown return 135.0; 2537144b44b1SLen Brown } 2538144b44b1SLen Brown } 2539144b44b1SLen Brown 254040ee8e3bSAndrey Semin /* 254140ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 254240ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 254340ee8e3bSAndrey Semin */ 254440ee8e3bSAndrey Semin static double 254540ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 254640ee8e3bSAndrey Semin { 254740ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 254840ee8e3bSAndrey Semin 254940ee8e3bSAndrey Semin switch (model) { 255040ee8e3bSAndrey Semin case 0x3F: /* HSX */ 255140ee8e3bSAndrey Semin case 0x4F: /* BDX */ 255240ee8e3bSAndrey Semin case 0x56: /* BDX-DE */ 2553fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 255440ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 255540ee8e3bSAndrey Semin default: 255640ee8e3bSAndrey Semin return (rapl_energy_units); 255740ee8e3bSAndrey Semin } 255840ee8e3bSAndrey Semin } 255940ee8e3bSAndrey Semin 2560144b44b1SLen Brown 2561889facbeSLen Brown /* 2562889facbeSLen Brown * rapl_probe() 2563889facbeSLen Brown * 2564144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 2565889facbeSLen Brown */ 2566889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 2567889facbeSLen Brown { 2568889facbeSLen Brown unsigned long long msr; 2569144b44b1SLen Brown unsigned int time_unit; 2570889facbeSLen Brown double tdp; 2571889facbeSLen Brown 2572889facbeSLen Brown if (!genuine_intel) 2573889facbeSLen Brown return; 2574889facbeSLen Brown 2575889facbeSLen Brown if (family != 6) 2576889facbeSLen Brown return; 2577889facbeSLen Brown 2578889facbeSLen Brown switch (model) { 2579889facbeSLen Brown case 0x2A: 2580889facbeSLen Brown case 0x3A: 258170b43400SLen Brown case 0x3C: /* HSW */ 258270b43400SLen Brown case 0x45: /* HSW */ 2583149c2319SLen Brown case 0x46: /* HSW */ 25844e8e863fSLen Brown case 0x3D: /* BDW */ 258548a0631cSLen Brown case 0x47: /* BDW */ 2586144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 2587889facbeSLen Brown break; 25880b2bb692SLen Brown case 0x4E: /* SKL */ 25890b2bb692SLen Brown case 0x5E: /* SKL */ 25900b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 25910b2bb692SLen Brown break; 2592e6f9bb3cSLen Brown case 0x3F: /* HSX */ 25934e8e863fSLen Brown case 0x4F: /* BDX */ 25944e8e863fSLen Brown case 0x56: /* BDX-DE */ 2595fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 25960b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 2597e6f9bb3cSLen Brown break; 2598889facbeSLen Brown case 0x2D: 2599889facbeSLen Brown case 0x3E: 26000b2bb692SLen 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; 2601144b44b1SLen Brown break; 2602144b44b1SLen Brown case 0x37: /* BYT */ 2603144b44b1SLen Brown case 0x4D: /* AVN */ 2604144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES ; 2605889facbeSLen Brown break; 2606889facbeSLen Brown default: 2607889facbeSLen Brown return; 2608889facbeSLen Brown } 2609889facbeSLen Brown 2610889facbeSLen Brown /* units on package 0, verify later other packages match */ 26117ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 2612889facbeSLen Brown return; 2613889facbeSLen Brown 2614889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 2615144b44b1SLen Brown if (model == 0x37) 2616144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 2617144b44b1SLen Brown else 2618889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 2619889facbeSLen Brown 262040ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 262140ee8e3bSAndrey Semin 2622144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 2623144b44b1SLen Brown if (time_unit == 0) 2624144b44b1SLen Brown time_unit = 0xA; 2625889facbeSLen Brown 2626144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 2627144b44b1SLen Brown 2628144b44b1SLen Brown tdp = get_tdp(model); 2629889facbeSLen Brown 2630889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 2631d8af6f5fSLen Brown if (debug) 2632b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 2633889facbeSLen Brown 2634889facbeSLen Brown return; 2635889facbeSLen Brown } 2636889facbeSLen Brown 263758cc30a4SLen Brown void perf_limit_reasons_probe(int family, int model) 26383a9a941dSLen Brown { 26393a9a941dSLen Brown if (!genuine_intel) 26403a9a941dSLen Brown return; 26413a9a941dSLen Brown 26423a9a941dSLen Brown if (family != 6) 26433a9a941dSLen Brown return; 26443a9a941dSLen Brown 26453a9a941dSLen Brown switch (model) { 26463a9a941dSLen Brown case 0x3C: /* HSW */ 26473a9a941dSLen Brown case 0x45: /* HSW */ 26483a9a941dSLen Brown case 0x46: /* HSW */ 26493a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 26503a9a941dSLen Brown case 0x3F: /* HSX */ 26513a9a941dSLen Brown do_core_perf_limit_reasons = 1; 26523a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 26533a9a941dSLen Brown default: 26543a9a941dSLen Brown return; 26553a9a941dSLen Brown } 26563a9a941dSLen Brown } 26573a9a941dSLen Brown 2658889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2659889facbeSLen Brown { 2660889facbeSLen Brown unsigned long long msr; 2661889facbeSLen Brown unsigned int dts; 2662889facbeSLen Brown int cpu; 2663889facbeSLen Brown 2664889facbeSLen Brown if (!(do_dts || do_ptm)) 2665889facbeSLen Brown return 0; 2666889facbeSLen Brown 2667889facbeSLen Brown cpu = t->cpu_id; 2668889facbeSLen Brown 2669889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 2670889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2671889facbeSLen Brown return 0; 2672889facbeSLen Brown 2673889facbeSLen Brown if (cpu_migrate(cpu)) { 2674b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2675889facbeSLen Brown return -1; 2676889facbeSLen Brown } 2677889facbeSLen Brown 2678889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 2679889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2680889facbeSLen Brown return 0; 2681889facbeSLen Brown 2682889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2683b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 2684889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 2685889facbeSLen Brown 2686889facbeSLen Brown #ifdef THERM_DEBUG 2687889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 2688889facbeSLen Brown return 0; 2689889facbeSLen Brown 2690889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2691889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2692b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2693889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2694889facbeSLen Brown #endif 2695889facbeSLen Brown } 2696889facbeSLen Brown 2697889facbeSLen Brown 2698889facbeSLen Brown if (do_dts) { 2699889facbeSLen Brown unsigned int resolution; 2700889facbeSLen Brown 2701889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2702889facbeSLen Brown return 0; 2703889facbeSLen Brown 2704889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2705889facbeSLen Brown resolution = (msr >> 27) & 0xF; 2706b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 2707889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 2708889facbeSLen Brown 2709889facbeSLen Brown #ifdef THERM_DEBUG 2710889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 2711889facbeSLen Brown return 0; 2712889facbeSLen Brown 2713889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2714889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2715b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2716889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2717889facbeSLen Brown #endif 2718889facbeSLen Brown } 2719889facbeSLen Brown 2720889facbeSLen Brown return 0; 2721889facbeSLen Brown } 2722889facbeSLen Brown 2723889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 2724889facbeSLen Brown { 2725b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 2726889facbeSLen Brown cpu, label, 2727889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 2728889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 2729889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 2730889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 2731889facbeSLen Brown 2732889facbeSLen Brown return; 2733889facbeSLen Brown } 2734889facbeSLen Brown 2735889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2736889facbeSLen Brown { 2737889facbeSLen Brown unsigned long long msr; 2738889facbeSLen Brown int cpu; 2739889facbeSLen Brown 2740889facbeSLen Brown if (!do_rapl) 2741889facbeSLen Brown return 0; 2742889facbeSLen Brown 2743889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 2744889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2745889facbeSLen Brown return 0; 2746889facbeSLen Brown 2747889facbeSLen Brown cpu = t->cpu_id; 2748889facbeSLen Brown if (cpu_migrate(cpu)) { 2749b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2750889facbeSLen Brown return -1; 2751889facbeSLen Brown } 2752889facbeSLen Brown 2753889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 2754889facbeSLen Brown return -1; 2755889facbeSLen Brown 2756d8af6f5fSLen Brown if (debug) { 2757b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 2758889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 2759144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 2760889facbeSLen Brown } 2761144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 2762144b44b1SLen Brown 2763889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 2764889facbeSLen Brown return -5; 2765889facbeSLen Brown 2766889facbeSLen Brown 2767b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2768889facbeSLen Brown cpu, msr, 2769889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2770889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2771889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2772889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2773889facbeSLen Brown 2774144b44b1SLen Brown } 2775144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 2776144b44b1SLen Brown 2777889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 2778889facbeSLen Brown return -9; 2779889facbeSLen Brown 2780b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 2781889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 2782889facbeSLen Brown 2783889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 2784b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 2785889facbeSLen Brown cpu, 2786889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 2787889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 2788889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 2789889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 2790889facbeSLen Brown } 2791889facbeSLen Brown 27920b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 2793889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 2794889facbeSLen Brown return -6; 2795889facbeSLen Brown 2796b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2797889facbeSLen Brown cpu, msr, 2798889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2799889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2800889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2801889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 28020b2bb692SLen Brown } 28030b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 2804889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 2805889facbeSLen Brown return -9; 2806b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 2807889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2808889facbeSLen Brown 2809889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 2810889facbeSLen Brown } 2811144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 2812d8af6f5fSLen Brown if (debug) { 2813889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 2814889facbeSLen Brown return -7; 2815889facbeSLen Brown 2816b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 2817144b44b1SLen Brown } 2818144b44b1SLen Brown } 2819144b44b1SLen Brown if (do_rapl & RAPL_CORES) { 2820d8af6f5fSLen Brown if (debug) { 2821889facbeSLen Brown 2822889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 2823889facbeSLen Brown return -9; 2824b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 2825889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2826889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 2827889facbeSLen Brown } 2828889facbeSLen Brown } 2829889facbeSLen Brown if (do_rapl & RAPL_GFX) { 2830d8af6f5fSLen Brown if (debug) { 2831889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 2832889facbeSLen Brown return -8; 2833889facbeSLen Brown 2834b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 2835889facbeSLen Brown 2836889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 2837889facbeSLen Brown return -9; 2838b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 2839889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2840889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 2841889facbeSLen Brown } 2842889facbeSLen Brown } 2843889facbeSLen Brown return 0; 2844889facbeSLen Brown } 2845889facbeSLen Brown 2846d7899447SLen Brown /* 2847d7899447SLen Brown * SNB adds support for additional MSRs: 2848d7899447SLen Brown * 2849d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 2850d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 2851d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 2852d7899447SLen Brown */ 2853103a8feaSLen Brown 2854d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 2855103a8feaSLen Brown { 2856103a8feaSLen Brown if (!genuine_intel) 2857103a8feaSLen Brown return 0; 2858103a8feaSLen Brown 2859103a8feaSLen Brown switch (model) { 2860103a8feaSLen Brown case 0x2A: 2861103a8feaSLen Brown case 0x2D: 2862650a37f3SLen Brown case 0x3A: /* IVB */ 28631300651bSLen Brown case 0x3E: /* IVB Xeon */ 286470b43400SLen Brown case 0x3C: /* HSW */ 286570b43400SLen Brown case 0x3F: /* HSW */ 286670b43400SLen Brown case 0x45: /* HSW */ 2867149c2319SLen Brown case 0x46: /* HSW */ 28684e8e863fSLen Brown case 0x3D: /* BDW */ 286948a0631cSLen Brown case 0x47: /* BDW */ 28704e8e863fSLen Brown case 0x4F: /* BDX */ 28714e8e863fSLen Brown case 0x56: /* BDX-DE */ 28720b2bb692SLen Brown case 0x4E: /* SKL */ 28730b2bb692SLen Brown case 0x5E: /* SKL */ 2874103a8feaSLen Brown return 1; 2875103a8feaSLen Brown } 2876103a8feaSLen Brown return 0; 2877103a8feaSLen Brown } 2878103a8feaSLen Brown 2879d7899447SLen Brown /* 2880d7899447SLen Brown * HSW adds support for additional MSRs: 2881d7899447SLen Brown * 2882d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 2883d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 2884d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 2885d7899447SLen Brown */ 2886d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 2887ca58710fSKristen Carlson Accardi { 2888ca58710fSKristen Carlson Accardi if (!genuine_intel) 2889ca58710fSKristen Carlson Accardi return 0; 2890ca58710fSKristen Carlson Accardi 2891ca58710fSKristen Carlson Accardi switch (model) { 28924e8e863fSLen Brown case 0x45: /* HSW */ 28934e8e863fSLen Brown case 0x3D: /* BDW */ 28940b2bb692SLen Brown case 0x4E: /* SKL */ 28950b2bb692SLen Brown case 0x5E: /* SKL */ 2896ca58710fSKristen Carlson Accardi return 1; 2897ca58710fSKristen Carlson Accardi } 2898ca58710fSKristen Carlson Accardi return 0; 2899ca58710fSKristen Carlson Accardi } 2900ca58710fSKristen Carlson Accardi 29010b2bb692SLen Brown /* 29020b2bb692SLen Brown * SKL adds support for additional MSRS: 29030b2bb692SLen Brown * 29040b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 29050b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 29060b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 29070b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 29080b2bb692SLen Brown */ 29090b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 29100b2bb692SLen Brown { 29110b2bb692SLen Brown if (!genuine_intel) 29120b2bb692SLen Brown return 0; 29130b2bb692SLen Brown 29140b2bb692SLen Brown switch (model) { 29150b2bb692SLen Brown case 0x4E: /* SKL */ 29160b2bb692SLen Brown case 0x5E: /* SKL */ 29170b2bb692SLen Brown return 1; 29180b2bb692SLen Brown } 29190b2bb692SLen Brown return 0; 29200b2bb692SLen Brown } 29210b2bb692SLen Brown 29220b2bb692SLen Brown 2923ca58710fSKristen Carlson Accardi 2924144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 2925144b44b1SLen Brown { 2926144b44b1SLen Brown if (!genuine_intel) 2927144b44b1SLen Brown return 0; 2928144b44b1SLen Brown switch (model) { 2929144b44b1SLen Brown case 0x37: /* BYT */ 2930144b44b1SLen Brown case 0x4D: /* AVN */ 2931144b44b1SLen Brown return 1; 2932144b44b1SLen Brown } 2933144b44b1SLen Brown return 0; 2934144b44b1SLen Brown } 2935144b44b1SLen Brown 2936fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 2937fb5d4327SDasaratharaman Chandramouli { 2938fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2939fb5d4327SDasaratharaman Chandramouli return 0; 2940fb5d4327SDasaratharaman Chandramouli switch (model) { 2941fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 2942fb5d4327SDasaratharaman Chandramouli return 1; 2943fb5d4327SDasaratharaman Chandramouli } 2944fb5d4327SDasaratharaman Chandramouli return 0; 2945fb5d4327SDasaratharaman Chandramouli } 2946fb5d4327SDasaratharaman Chandramouli 2947b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 2948b2b34dfeSHubert Chrzaniuk { 2949b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 2950b2b34dfeSHubert Chrzaniuk return 1024; 2951b2b34dfeSHubert Chrzaniuk return 1; 2952b2b34dfeSHubert Chrzaniuk } 2953b2b34dfeSHubert Chrzaniuk 2954144b44b1SLen Brown #define SLM_BCLK_FREQS 5 2955144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 2956144b44b1SLen Brown 2957144b44b1SLen Brown double slm_bclk(void) 2958144b44b1SLen Brown { 2959144b44b1SLen Brown unsigned long long msr = 3; 2960144b44b1SLen Brown unsigned int i; 2961144b44b1SLen Brown double freq; 2962144b44b1SLen Brown 29637ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 2964b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 2965144b44b1SLen Brown 2966144b44b1SLen Brown i = msr & 0xf; 2967144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 2968b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 2969144b44b1SLen Brown msr = 3; 2970144b44b1SLen Brown } 2971144b44b1SLen Brown freq = slm_freq_table[i]; 2972144b44b1SLen Brown 2973b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 2974144b44b1SLen Brown 2975144b44b1SLen Brown return freq; 2976144b44b1SLen Brown } 2977144b44b1SLen Brown 2978103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 2979103a8feaSLen Brown { 2980121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 2981103a8feaSLen Brown return 100.00; 2982144b44b1SLen Brown else if (is_slm(family, model)) 2983144b44b1SLen Brown return slm_bclk(); 2984103a8feaSLen Brown else 2985103a8feaSLen Brown return 133.33; 2986103a8feaSLen Brown } 2987103a8feaSLen Brown 2988889facbeSLen Brown /* 2989889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 2990889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 2991889facbeSLen Brown * This is usually equal to tjMax. 2992889facbeSLen Brown * 2993889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 2994889facbeSLen Brown * but also allow cmdline over-ride with -T. 2995889facbeSLen Brown * 2996889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 2997889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 2998889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 2999889facbeSLen Brown */ 3000889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3001889facbeSLen Brown { 3002889facbeSLen Brown unsigned long long msr; 3003889facbeSLen Brown unsigned int target_c_local; 3004889facbeSLen Brown int cpu; 3005889facbeSLen Brown 3006889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 3007889facbeSLen Brown if (!(do_dts || do_ptm)) 3008889facbeSLen Brown return 0; 3009889facbeSLen Brown 3010889facbeSLen Brown /* this is a per-package concept */ 3011889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3012889facbeSLen Brown return 0; 3013889facbeSLen Brown 3014889facbeSLen Brown cpu = t->cpu_id; 3015889facbeSLen Brown if (cpu_migrate(cpu)) { 3016b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3017889facbeSLen Brown return -1; 3018889facbeSLen Brown } 3019889facbeSLen Brown 3020889facbeSLen Brown if (tcc_activation_temp_override != 0) { 3021889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 3022b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 3023889facbeSLen Brown cpu, tcc_activation_temp); 3024889facbeSLen Brown return 0; 3025889facbeSLen Brown } 3026889facbeSLen Brown 3027889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 3028d7899447SLen Brown if (!do_nhm_platform_info) 3029889facbeSLen Brown goto guess; 3030889facbeSLen Brown 30317ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 3032889facbeSLen Brown goto guess; 3033889facbeSLen Brown 30343482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 3035889facbeSLen Brown 3036d8af6f5fSLen Brown if (debug) 3037b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 3038889facbeSLen Brown cpu, msr, target_c_local); 3039889facbeSLen Brown 30403482124aSJean Delvare if (!target_c_local) 3041889facbeSLen Brown goto guess; 3042889facbeSLen Brown 3043889facbeSLen Brown tcc_activation_temp = target_c_local; 3044889facbeSLen Brown 3045889facbeSLen Brown return 0; 3046889facbeSLen Brown 3047889facbeSLen Brown guess: 3048889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 3049b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 3050889facbeSLen Brown cpu, tcc_activation_temp); 3051889facbeSLen Brown 3052889facbeSLen Brown return 0; 3053889facbeSLen Brown } 305469807a63SLen Brown 305569807a63SLen Brown void decode_misc_enable_msr(void) 305669807a63SLen Brown { 305769807a63SLen Brown unsigned long long msr; 305869807a63SLen Brown 305969807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 3060b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n", 306169807a63SLen Brown base_cpu, msr, 306269807a63SLen Brown msr & (1 << 3) ? "TCC" : "", 306369807a63SLen Brown msr & (1 << 16) ? "EIST" : "", 306469807a63SLen Brown msr & (1 << 18) ? "MONITOR" : ""); 306569807a63SLen Brown } 306669807a63SLen Brown 3067f0057310SLen Brown /* 3068f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 3069f0057310SLen Brown * 3070f0057310SLen Brown * Decode the bits according to the Nehalem documentation 3071f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 3072f0057310SLen Brown * bit[1] less so... 3073f0057310SLen Brown */ 3074f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 3075f0057310SLen Brown { 3076f0057310SLen Brown unsigned long long msr; 3077f0057310SLen Brown 3078f0057310SLen Brown if (!do_nhm_platform_info) 3079f0057310SLen Brown return; 3080f0057310SLen Brown 3081f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 3082b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n", 3083f0057310SLen Brown base_cpu, msr, 3084f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 3085f0057310SLen Brown msr & (1 << 1) ? "EN" : "DIS"); 3086f0057310SLen Brown } 30877f5c258eSLen Brown 3088fcd17211SLen Brown void process_cpuid() 3089103a8feaSLen Brown { 309061a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 3091103a8feaSLen Brown unsigned int fms, family, model, stepping; 3092103a8feaSLen Brown 3093103a8feaSLen Brown eax = ebx = ecx = edx = 0; 3094103a8feaSLen Brown 30952b92865eSJosh Triplett __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 3096103a8feaSLen Brown 3097103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 3098103a8feaSLen Brown genuine_intel = 1; 3099103a8feaSLen Brown 3100d8af6f5fSLen Brown if (debug) 3101b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 3102103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 3103103a8feaSLen Brown 31042b92865eSJosh Triplett __get_cpuid(1, &fms, &ebx, &ecx, &edx); 3105103a8feaSLen Brown family = (fms >> 8) & 0xf; 3106103a8feaSLen Brown model = (fms >> 4) & 0xf; 3107103a8feaSLen Brown stepping = fms & 0xf; 3108103a8feaSLen Brown if (family == 6 || family == 0xf) 3109103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 3110103a8feaSLen Brown 311169807a63SLen Brown if (debug) { 3112b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 3113103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 3114b7d8c148SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s\n", 311569807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 311669807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 311769807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 311869807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 311969807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 312069807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 312169807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 312269807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 312369807a63SLen Brown } 3124103a8feaSLen Brown 3125b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 3126b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 3127103a8feaSLen Brown 3128103a8feaSLen Brown /* 3129103a8feaSLen Brown * check max extended function levels of CPUID. 3130103a8feaSLen Brown * This is needed to check for invariant TSC. 3131103a8feaSLen Brown * This check is valid for both Intel and AMD. 3132103a8feaSLen Brown */ 3133103a8feaSLen Brown ebx = ecx = edx = 0; 313461a87ba7SLen Brown __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx); 3135103a8feaSLen Brown 313661a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 3137103a8feaSLen Brown 3138103a8feaSLen Brown /* 3139103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 3140103a8feaSLen Brown * this check is valid for both Intel and AMD 3141103a8feaSLen Brown */ 31422b92865eSJosh Triplett __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 31438209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 3144d7899447SLen Brown } 3145103a8feaSLen Brown 3146103a8feaSLen Brown /* 3147103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 3148103a8feaSLen Brown * this check is valid for both Intel and AMD 3149103a8feaSLen Brown */ 3150103a8feaSLen Brown 31512b92865eSJosh Triplett __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 31528209e054SThomas Renninger has_aperf = ecx & (1 << 0); 3153889facbeSLen Brown do_dts = eax & (1 << 0); 3154889facbeSLen Brown do_ptm = eax & (1 << 6); 31557f5c258eSLen Brown has_hwp = eax & (1 << 7); 31567f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 31577f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 31587f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 31597f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 3160889facbeSLen Brown has_epb = ecx & (1 << 3); 3161889facbeSLen Brown 3162d8af6f5fSLen Brown if (debug) 3163b7d8c148SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, " 31647f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 31657f5c258eSLen Brown has_aperf ? "" : "No-", 31667f5c258eSLen Brown do_dts ? "" : "No-", 31677f5c258eSLen Brown do_ptm ? "" : "No-", 31687f5c258eSLen Brown has_hwp ? "" : "No-", 31697f5c258eSLen Brown has_hwp_notify ? "" : "No-", 31707f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 31717f5c258eSLen Brown has_hwp_epp ? "" : "No-", 31727f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 31737f5c258eSLen Brown has_epb ? "" : "No-"); 3174103a8feaSLen Brown 317569807a63SLen Brown if (debug) 317669807a63SLen Brown decode_misc_enable_msr(); 317769807a63SLen Brown 317861a87ba7SLen Brown if (max_level >= 0x15) { 31798a5bdf41SLen Brown unsigned int eax_crystal; 31808a5bdf41SLen Brown unsigned int ebx_tsc; 31818a5bdf41SLen Brown 31828a5bdf41SLen Brown /* 31838a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 31848a5bdf41SLen Brown */ 31858a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 31868a5bdf41SLen Brown __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx); 31878a5bdf41SLen Brown 31888a5bdf41SLen Brown if (ebx_tsc != 0) { 31898a5bdf41SLen Brown 31908a5bdf41SLen Brown if (debug && (ebx != 0)) 3191b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 31928a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 31938a5bdf41SLen Brown 31948a5bdf41SLen Brown if (crystal_hz == 0) 31958a5bdf41SLen Brown switch(model) { 31968a5bdf41SLen Brown case 0x4E: /* SKL */ 31978a5bdf41SLen Brown case 0x5E: /* SKL */ 31988a5bdf41SLen Brown crystal_hz = 24000000; /* 24 MHz */ 31998a5bdf41SLen Brown break; 32008a5bdf41SLen Brown default: 32018a5bdf41SLen Brown crystal_hz = 0; 32028a5bdf41SLen Brown } 32038a5bdf41SLen Brown 32048a5bdf41SLen Brown if (crystal_hz) { 32058a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 32068a5bdf41SLen Brown if (debug) 3207b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 32088a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 32098a5bdf41SLen Brown } 32108a5bdf41SLen Brown } 32118a5bdf41SLen Brown } 321261a87ba7SLen Brown if (max_level >= 0x16) { 321361a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 321461a87ba7SLen Brown 321561a87ba7SLen Brown /* 321661a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 321761a87ba7SLen Brown */ 321861a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 321961a87ba7SLen Brown 322061a87ba7SLen Brown __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx); 322161a87ba7SLen Brown if (debug) 3222b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 322361a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 322461a87ba7SLen Brown } 32258a5bdf41SLen Brown 3226b2b34dfeSHubert Chrzaniuk if (has_aperf) 3227b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 3228b2b34dfeSHubert Chrzaniuk 3229ee7e38e3SLen Brown do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); 3230d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 3231ee7e38e3SLen Brown do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); 3232ee7e38e3SLen Brown do_pc3 = (pkg_cstate_limit >= PCL__3); 3233ee7e38e3SLen Brown do_pc6 = (pkg_cstate_limit >= PCL__6); 3234ee7e38e3SLen Brown do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); 3235d7899447SLen Brown do_c8_c9_c10 = has_hsw_msrs(family, model); 32360b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 3237144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 3238fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 3239103a8feaSLen Brown 3240f0057310SLen Brown if (debug) 3241f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 3242f0057310SLen Brown 3243889facbeSLen Brown rapl_probe(family, model); 32443a9a941dSLen Brown perf_limit_reasons_probe(family, model); 3245889facbeSLen Brown 3246fcd17211SLen Brown if (debug) 324758cc30a4SLen Brown dump_cstate_pstate_config_info(family, model); 3248fcd17211SLen Brown 3249a2b7b749SLen Brown if (has_skl_msrs(family, model)) 3250a2b7b749SLen Brown calculate_tsc_tweak(); 3251a2b7b749SLen Brown 3252fdf676e5SLen Brown do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK); 3253fdf676e5SLen Brown 325427d47356SLen Brown do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK); 325527d47356SLen Brown 3256889facbeSLen Brown return; 3257103a8feaSLen Brown } 3258103a8feaSLen Brown 3259d8af6f5fSLen Brown void help() 3260103a8feaSLen Brown { 3261b7d8c148SLen Brown fprintf(outf, 3262d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 3263d8af6f5fSLen Brown "\n" 3264d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 3265d8af6f5fSLen Brown "when COMMAND completes.\n" 3266d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 3267d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 3268d8af6f5fSLen Brown "--debug run in \"debug\" mode\n" 3269d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 3270d8af6f5fSLen Brown "--help print this help message\n" 3271d8af6f5fSLen Brown "--counter msr print 32-bit counter at address \"msr\"\n" 3272d8af6f5fSLen Brown "--Counter msr print 64-bit Counter at address \"msr\"\n" 3273b7d8c148SLen Brown "--out file create or truncate \"file\" for all output\n" 3274d8af6f5fSLen Brown "--msr msr print 32-bit value at address \"msr\"\n" 3275d8af6f5fSLen Brown "--MSR msr print 64-bit Value at address \"msr\"\n" 3276d8af6f5fSLen Brown "--version print version information\n" 3277d8af6f5fSLen Brown "\n" 3278d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 3279103a8feaSLen Brown } 3280103a8feaSLen Brown 3281103a8feaSLen Brown 3282103a8feaSLen Brown /* 3283103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 3284103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 3285103a8feaSLen Brown */ 3286103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 3287103a8feaSLen Brown { 3288103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 3289103a8feaSLen Brown return 1; 3290103a8feaSLen Brown else 3291103a8feaSLen Brown return 0; 3292103a8feaSLen Brown } 3293103a8feaSLen Brown 3294103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 3295103a8feaSLen Brown { 3296103a8feaSLen Brown return 0; 3297103a8feaSLen Brown } 3298103a8feaSLen Brown 3299c98d5d94SLen Brown void topology_probe() 3300c98d5d94SLen Brown { 3301c98d5d94SLen Brown int i; 3302c98d5d94SLen Brown int max_core_id = 0; 3303c98d5d94SLen Brown int max_package_id = 0; 3304c98d5d94SLen Brown int max_siblings = 0; 3305c98d5d94SLen Brown struct cpu_topology { 3306c98d5d94SLen Brown int core_id; 3307c98d5d94SLen Brown int physical_package_id; 3308c98d5d94SLen Brown } *cpus; 3309c98d5d94SLen Brown 3310c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 3311c98d5d94SLen Brown topo.num_cpus = 0; 3312c98d5d94SLen Brown topo.max_cpu_num = 0; 3313c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 3314c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 3315c98d5d94SLen Brown show_cpu = 1; 3316c98d5d94SLen Brown 3317d8af6f5fSLen Brown if (debug > 1) 3318b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 3319c98d5d94SLen Brown 3320c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 3321b2c95d90SJosh Triplett if (cpus == NULL) 3322b2c95d90SJosh Triplett err(1, "calloc cpus"); 3323c98d5d94SLen Brown 3324c98d5d94SLen Brown /* 3325c98d5d94SLen Brown * Allocate and initialize cpu_present_set 3326c98d5d94SLen Brown */ 3327c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3328b2c95d90SJosh Triplett if (cpu_present_set == NULL) 3329b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3330c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3331c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 3332c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 3333c98d5d94SLen Brown 3334c98d5d94SLen Brown /* 3335c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 3336c98d5d94SLen Brown */ 3337c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3338b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 3339b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3340c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3341c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 3342c98d5d94SLen Brown 3343c98d5d94SLen Brown 3344c98d5d94SLen Brown /* 3345c98d5d94SLen Brown * For online cpus 3346c98d5d94SLen Brown * find max_core_id, max_package_id 3347c98d5d94SLen Brown */ 3348c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 3349c98d5d94SLen Brown int siblings; 3350c98d5d94SLen Brown 3351c98d5d94SLen Brown if (cpu_is_not_present(i)) { 3352d8af6f5fSLen Brown if (debug > 1) 3353b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 3354c98d5d94SLen Brown continue; 3355c98d5d94SLen Brown } 3356c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 3357c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 3358c98d5d94SLen Brown max_core_id = cpus[i].core_id; 3359c98d5d94SLen Brown 3360c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 3361c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 3362c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 3363c98d5d94SLen Brown 3364c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 3365c98d5d94SLen Brown if (siblings > max_siblings) 3366c98d5d94SLen Brown max_siblings = siblings; 3367d8af6f5fSLen Brown if (debug > 1) 3368b7d8c148SLen Brown fprintf(outf, "cpu %d pkg %d core %d\n", 3369c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 3370c98d5d94SLen Brown } 3371c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 3372d8af6f5fSLen Brown if (debug > 1) 3373b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 3374c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 33751cc21f7bSLen Brown if (debug && !summary_only && topo.num_cores_per_pkg > 1) 3376c98d5d94SLen Brown show_core = 1; 3377c98d5d94SLen Brown 3378c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 3379d8af6f5fSLen Brown if (debug > 1) 3380b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 3381c98d5d94SLen Brown max_package_id, topo.num_packages); 33821cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 3383c98d5d94SLen Brown show_pkg = 1; 3384c98d5d94SLen Brown 3385c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 3386d8af6f5fSLen Brown if (debug > 1) 3387b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 3388c98d5d94SLen Brown 3389c98d5d94SLen Brown free(cpus); 3390c98d5d94SLen Brown } 3391c98d5d94SLen Brown 3392c98d5d94SLen Brown void 3393c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 3394c98d5d94SLen Brown { 3395c98d5d94SLen Brown int i; 3396c98d5d94SLen Brown 3397c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 3398c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 3399c98d5d94SLen Brown if (*t == NULL) 3400c98d5d94SLen Brown goto error; 3401c98d5d94SLen Brown 3402c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 3403c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 3404c98d5d94SLen Brown (*t)[i].cpu_id = -1; 3405c98d5d94SLen Brown 3406c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 3407c98d5d94SLen Brown sizeof(struct core_data)); 3408c98d5d94SLen Brown if (*c == NULL) 3409c98d5d94SLen Brown goto error; 3410c98d5d94SLen Brown 3411c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 3412c98d5d94SLen Brown (*c)[i].core_id = -1; 3413c98d5d94SLen Brown 3414c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 3415c98d5d94SLen Brown if (*p == NULL) 3416c98d5d94SLen Brown goto error; 3417c98d5d94SLen Brown 3418c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 3419c98d5d94SLen Brown (*p)[i].package_id = i; 3420c98d5d94SLen Brown 3421c98d5d94SLen Brown return; 3422c98d5d94SLen Brown error: 3423b2c95d90SJosh Triplett err(1, "calloc counters"); 3424c98d5d94SLen Brown } 3425c98d5d94SLen Brown /* 3426c98d5d94SLen Brown * init_counter() 3427c98d5d94SLen Brown * 3428c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 3429c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 3430c98d5d94SLen Brown * 3431c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 3432c98d5d94SLen Brown */ 3433c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 3434c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 3435c98d5d94SLen Brown int pkg_num, int cpu_id) 3436c98d5d94SLen Brown { 3437c98d5d94SLen Brown struct thread_data *t; 3438c98d5d94SLen Brown struct core_data *c; 3439c98d5d94SLen Brown struct pkg_data *p; 3440c98d5d94SLen Brown 3441c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 3442c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 3443c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 3444c98d5d94SLen Brown 3445c98d5d94SLen Brown t->cpu_id = cpu_id; 3446c98d5d94SLen Brown if (thread_num == 0) { 3447c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 3448c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 3449c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 3450c98d5d94SLen Brown } 3451c98d5d94SLen Brown 3452c98d5d94SLen Brown c->core_id = core_num; 3453c98d5d94SLen Brown p->package_id = pkg_num; 3454c98d5d94SLen Brown } 3455c98d5d94SLen Brown 3456c98d5d94SLen Brown 3457c98d5d94SLen Brown int initialize_counters(int cpu_id) 3458c98d5d94SLen Brown { 3459c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 3460c98d5d94SLen Brown 3461c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 3462c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 3463e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 3464e275b388SDasaratharaman Chandramouli if (!my_thread_id) 3465c98d5d94SLen Brown topo.num_cores++; 3466c98d5d94SLen Brown 3467c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3468c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3469c98d5d94SLen Brown return 0; 3470c98d5d94SLen Brown } 3471c98d5d94SLen Brown 3472c98d5d94SLen Brown void allocate_output_buffer() 3473c98d5d94SLen Brown { 34743b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 3475c98d5d94SLen Brown outp = output_buffer; 3476b2c95d90SJosh Triplett if (outp == NULL) 3477b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 3478c98d5d94SLen Brown } 347936229897SLen Brown void allocate_fd_percpu(void) 348036229897SLen Brown { 348136229897SLen Brown fd_percpu = calloc(topo.max_cpu_num, sizeof(int)); 348236229897SLen Brown if (fd_percpu == NULL) 348336229897SLen Brown err(-1, "calloc fd_percpu"); 348436229897SLen Brown } 3485562a2d37SLen Brown void allocate_irq_buffers(void) 3486562a2d37SLen Brown { 3487562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 3488562a2d37SLen Brown if (irq_column_2_cpu == NULL) 3489562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 3490562a2d37SLen Brown 3491562a2d37SLen Brown irqs_per_cpu = calloc(topo.max_cpu_num, sizeof(int)); 3492562a2d37SLen Brown if (irqs_per_cpu == NULL) 3493562a2d37SLen Brown err(-1, "calloc %d", topo.max_cpu_num); 3494562a2d37SLen Brown } 3495c98d5d94SLen Brown void setup_all_buffers(void) 3496c98d5d94SLen Brown { 3497c98d5d94SLen Brown topology_probe(); 3498562a2d37SLen Brown allocate_irq_buffers(); 349936229897SLen Brown allocate_fd_percpu(); 3500c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 3501c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 3502c98d5d94SLen Brown allocate_output_buffer(); 3503c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 3504c98d5d94SLen Brown } 35053b4d5c7fSAndy Shevchenko 35067ce7d5deSPrarit Bhargava void set_base_cpu(void) 35077ce7d5deSPrarit Bhargava { 35087ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 35097ce7d5deSPrarit Bhargava if (base_cpu < 0) 35107ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 35117ce7d5deSPrarit Bhargava 35127ce7d5deSPrarit Bhargava if (debug > 1) 3513b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 35147ce7d5deSPrarit Bhargava } 35157ce7d5deSPrarit Bhargava 3516103a8feaSLen Brown void turbostat_init() 3517103a8feaSLen Brown { 35187ce7d5deSPrarit Bhargava setup_all_buffers(); 35197ce7d5deSPrarit Bhargava set_base_cpu(); 3520103a8feaSLen Brown check_dev_msr(); 352198481e79SLen Brown check_permissions(); 3522fcd17211SLen Brown process_cpuid(); 3523103a8feaSLen Brown 3524103a8feaSLen Brown 3525d8af6f5fSLen Brown if (debug) 35267f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 35277f5c258eSLen Brown 35287f5c258eSLen Brown if (debug) 3529889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 3530889facbeSLen Brown 3531d8af6f5fSLen Brown if (debug) 35323a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 35333a9a941dSLen Brown 3534d8af6f5fSLen Brown if (debug) 3535889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 3536889facbeSLen Brown 3537889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 3538889facbeSLen Brown 3539d8af6f5fSLen Brown if (debug) 3540889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 3541103a8feaSLen Brown } 3542103a8feaSLen Brown 3543103a8feaSLen Brown int fork_it(char **argv) 3544103a8feaSLen Brown { 3545103a8feaSLen Brown pid_t child_pid; 3546d91bb17cSLen Brown int status; 3547d15cf7c1SLen Brown 3548d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 3549d91bb17cSLen Brown if (status) 3550d91bb17cSLen Brown exit(status); 3551d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 3552d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 3553103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3554103a8feaSLen Brown 3555103a8feaSLen Brown child_pid = fork(); 3556103a8feaSLen Brown if (!child_pid) { 3557103a8feaSLen Brown /* child */ 3558103a8feaSLen Brown execvp(argv[0], argv); 3559103a8feaSLen Brown } else { 3560103a8feaSLen Brown 3561103a8feaSLen Brown /* parent */ 3562b2c95d90SJosh Triplett if (child_pid == -1) 3563b2c95d90SJosh Triplett err(1, "fork"); 3564103a8feaSLen Brown 3565103a8feaSLen Brown signal(SIGINT, SIG_IGN); 3566103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 3567b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 3568b2c95d90SJosh Triplett err(status, "waitpid"); 3569103a8feaSLen Brown } 3570c98d5d94SLen Brown /* 3571c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 3572c98d5d94SLen Brown * because re-starting is problematic when forking 3573c98d5d94SLen Brown */ 3574c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 3575103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3576103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3577c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 3578c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3579c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3580103a8feaSLen Brown 3581b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 3582b7d8c148SLen Brown 3583b7d8c148SLen Brown flush_output_stderr(); 3584103a8feaSLen Brown 3585d91bb17cSLen Brown return status; 3586103a8feaSLen Brown } 3587103a8feaSLen Brown 35883b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 35893b4d5c7fSAndy Shevchenko { 35903b4d5c7fSAndy Shevchenko int status; 35913b4d5c7fSAndy Shevchenko 35923b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 35933b4d5c7fSAndy Shevchenko if (status) 35943b4d5c7fSAndy Shevchenko return status; 35953b4d5c7fSAndy Shevchenko 35963b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 35973b4d5c7fSAndy Shevchenko if (status) 35983b4d5c7fSAndy Shevchenko return status; 35993b4d5c7fSAndy Shevchenko 3600b7d8c148SLen Brown flush_output_stdout(); 36013b4d5c7fSAndy Shevchenko 36023b4d5c7fSAndy Shevchenko return status; 36033b4d5c7fSAndy Shevchenko } 36043b4d5c7fSAndy Shevchenko 3605d8af6f5fSLen Brown void print_version() { 3606*0102b067SLen Brown fprintf(outf, "turbostat version 4.11 27 Feb 2016" 3607d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 3608d8af6f5fSLen Brown } 3609d8af6f5fSLen Brown 3610103a8feaSLen Brown void cmdline(int argc, char **argv) 3611103a8feaSLen Brown { 3612103a8feaSLen Brown int opt; 3613d8af6f5fSLen Brown int option_index = 0; 3614d8af6f5fSLen Brown static struct option long_options[] = { 3615d8af6f5fSLen Brown {"Counter", required_argument, 0, 'C'}, 3616d8af6f5fSLen Brown {"counter", required_argument, 0, 'c'}, 3617d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 3618d8af6f5fSLen Brown {"debug", no_argument, 0, 'd'}, 3619d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 3620d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 3621d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 3622d8af6f5fSLen Brown {"MSR", required_argument, 0, 'M'}, 3623d8af6f5fSLen Brown {"msr", required_argument, 0, 'm'}, 3624b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 3625d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 3626d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 3627d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 3628d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 3629d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 3630d8af6f5fSLen Brown {0, 0, 0, 0 } 3631d8af6f5fSLen Brown }; 3632103a8feaSLen Brown 3633103a8feaSLen Brown progname = argv[0]; 3634103a8feaSLen Brown 3635b7d8c148SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v", 3636d8af6f5fSLen Brown long_options, &option_index)) != -1) { 3637103a8feaSLen Brown switch (opt) { 3638d8af6f5fSLen Brown case 'C': 3639d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 3640103a8feaSLen Brown break; 3641f9240813SLen Brown case 'c': 36428e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 36438e180f3cSLen Brown break; 3644d8af6f5fSLen Brown case 'D': 3645d8af6f5fSLen Brown dump_only++; 36468e180f3cSLen Brown break; 3647d8af6f5fSLen Brown case 'd': 3648d8af6f5fSLen Brown debug++; 36492f32edf1SLen Brown break; 3650d8af6f5fSLen Brown case 'h': 3651d8af6f5fSLen Brown default: 3652d8af6f5fSLen Brown help(); 3653d8af6f5fSLen Brown exit(1); 3654d8af6f5fSLen Brown case 'i': 36552a0609c0SLen Brown { 36562a0609c0SLen Brown double interval = strtod(optarg, NULL); 36572a0609c0SLen Brown 36582a0609c0SLen Brown if (interval < 0.001) { 3659b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 36602a0609c0SLen Brown interval); 36612a0609c0SLen Brown exit(2); 36622a0609c0SLen Brown } 36632a0609c0SLen Brown 36642a0609c0SLen Brown interval_ts.tv_sec = interval; 36652a0609c0SLen Brown interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 36662a0609c0SLen Brown } 3667889facbeSLen Brown break; 36685c56be9aSDirk Brandewie case 'J': 36695c56be9aSDirk Brandewie rapl_joules++; 36705c56be9aSDirk Brandewie break; 3671d8af6f5fSLen Brown case 'M': 3672d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 3673d8af6f5fSLen Brown break; 3674d8af6f5fSLen Brown case 'm': 3675d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 3676d8af6f5fSLen Brown break; 3677b7d8c148SLen Brown case 'o': 3678b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 3679b7d8c148SLen Brown break; 3680d8af6f5fSLen Brown case 'P': 3681d8af6f5fSLen Brown show_pkg_only++; 3682d8af6f5fSLen Brown break; 3683d8af6f5fSLen Brown case 'p': 3684d8af6f5fSLen Brown show_core_only++; 3685d8af6f5fSLen Brown break; 3686d8af6f5fSLen Brown case 'S': 3687d8af6f5fSLen Brown summary_only++; 3688d8af6f5fSLen Brown break; 3689d8af6f5fSLen Brown case 'T': 3690d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 3691d8af6f5fSLen Brown break; 3692d8af6f5fSLen Brown case 'v': 3693d8af6f5fSLen Brown print_version(); 3694d8af6f5fSLen Brown exit(0); 3695d8af6f5fSLen Brown break; 3696103a8feaSLen Brown } 3697103a8feaSLen Brown } 3698103a8feaSLen Brown } 3699103a8feaSLen Brown 3700103a8feaSLen Brown int main(int argc, char **argv) 3701103a8feaSLen Brown { 3702b7d8c148SLen Brown outf = stderr; 3703b7d8c148SLen Brown 3704103a8feaSLen Brown cmdline(argc, argv); 3705103a8feaSLen Brown 3706d8af6f5fSLen Brown if (debug) 3707d8af6f5fSLen Brown print_version(); 3708103a8feaSLen Brown 3709103a8feaSLen Brown turbostat_init(); 3710103a8feaSLen Brown 37113b4d5c7fSAndy Shevchenko /* dump counters and exit */ 37123b4d5c7fSAndy Shevchenko if (dump_only) 37133b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 37143b4d5c7fSAndy Shevchenko 3715103a8feaSLen Brown /* 3716103a8feaSLen Brown * if any params left, it must be a command to fork 3717103a8feaSLen Brown */ 3718103a8feaSLen Brown if (argc - optind) 3719103a8feaSLen Brown return fork_it(argv + optind); 3720103a8feaSLen Brown else 3721103a8feaSLen Brown turbostat_loop(); 3722103a8feaSLen Brown 3723103a8feaSLen Brown return 0; 3724103a8feaSLen Brown } 3725