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> 412b92865eSJosh Triplett #include <cpuid.h> 4298481e79SLen Brown #include <linux/capability.h> 4398481e79SLen Brown #include <errno.h> 44103a8feaSLen Brown 45103a8feaSLen Brown char *proc_stat = "/proc/stat"; 46d8af6f5fSLen Brown unsigned int interval_sec = 5; 47d8af6f5fSLen Brown unsigned int debug; 48d8af6f5fSLen Brown unsigned int rapl_joules; 49d8af6f5fSLen Brown unsigned int summary_only; 50d8af6f5fSLen Brown unsigned int dump_only; 51103a8feaSLen Brown unsigned int skip_c0; 52103a8feaSLen Brown unsigned int skip_c1; 53103a8feaSLen Brown unsigned int do_nhm_cstates; 54103a8feaSLen Brown unsigned int do_snb_cstates; 55fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 56ee7e38e3SLen Brown unsigned int do_pc2; 57ee7e38e3SLen Brown unsigned int do_pc3; 58ee7e38e3SLen Brown unsigned int do_pc6; 59ee7e38e3SLen Brown unsigned int do_pc7; 60ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10; 610b2bb692SLen Brown unsigned int do_skl_residency; 62144b44b1SLen Brown unsigned int do_slm_cstates; 63144b44b1SLen Brown unsigned int use_c1_residency_msr; 64103a8feaSLen Brown unsigned int has_aperf; 65889facbeSLen Brown unsigned int has_epb; 66fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 67103a8feaSLen Brown unsigned int genuine_intel; 68103a8feaSLen Brown unsigned int has_invariant_tsc; 69d7899447SLen Brown unsigned int do_nhm_platform_info; 702f32edf1SLen Brown unsigned int extra_msr_offset32; 712f32edf1SLen Brown unsigned int extra_msr_offset64; 728e180f3cSLen Brown unsigned int extra_delta_offset32; 738e180f3cSLen Brown unsigned int extra_delta_offset64; 74b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 751ed51011SLen Brown int do_smi; 76103a8feaSLen Brown double bclk; 77a2b7b749SLen Brown double base_hz; 7821ed5574SLen Brown unsigned int has_base_hz; 79a2b7b749SLen Brown double tsc_tweak = 1.0; 80103a8feaSLen Brown unsigned int show_pkg; 81103a8feaSLen Brown unsigned int show_core; 82103a8feaSLen Brown unsigned int show_cpu; 83c98d5d94SLen Brown unsigned int show_pkg_only; 84c98d5d94SLen Brown unsigned int show_core_only; 85c98d5d94SLen Brown char *output_buffer, *outp; 86889facbeSLen Brown unsigned int do_rapl; 87889facbeSLen Brown unsigned int do_dts; 88889facbeSLen Brown unsigned int do_ptm; 89889facbeSLen Brown unsigned int tcc_activation_temp; 90889facbeSLen Brown unsigned int tcc_activation_temp_override; 9140ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9240ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 93889facbeSLen Brown double rapl_joule_counter_range; 943a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 953a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 963a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 978a5bdf41SLen Brown unsigned int crystal_hz; 988a5bdf41SLen Brown unsigned long long tsc_hz; 997ce7d5deSPrarit Bhargava int base_cpu; 10021ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 101*7f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 102*7f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 103*7f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 104*7f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 105*7f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 106*7f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 107889facbeSLen Brown 108889facbeSLen Brown #define RAPL_PKG (1 << 0) 109e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 110e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 111e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 112e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 113e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 114e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 115e6f9bb3cSLen Brown 116889facbeSLen Brown #define RAPL_DRAM (1 << 3) 117e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 118e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 119e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 120e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1210b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1220b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 123e6f9bb3cSLen Brown 1240b2bb692SLen Brown #define RAPL_CORES (1 << 6) 125e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 126e6f9bb3cSLen Brown /* 0x639 MSR_PP0_ENERGY_STATUS */ 1270b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 128e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 129e6f9bb3cSLen Brown 1300b2bb692SLen Brown #define RAPL_GFX (1 << 8) 131e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 132e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 133e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 134889facbeSLen Brown #define TJMAX_DEFAULT 100 135889facbeSLen Brown 136889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 137103a8feaSLen Brown 138103a8feaSLen Brown int aperf_mperf_unstable; 139103a8feaSLen Brown int backwards_count; 140103a8feaSLen Brown char *progname; 141103a8feaSLen Brown 142c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 143c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 144c98d5d94SLen Brown 145c98d5d94SLen Brown struct thread_data { 146c98d5d94SLen Brown unsigned long long tsc; 147c98d5d94SLen Brown unsigned long long aperf; 148c98d5d94SLen Brown unsigned long long mperf; 149144b44b1SLen Brown unsigned long long c1; 1502f32edf1SLen Brown unsigned long long extra_msr64; 1518e180f3cSLen Brown unsigned long long extra_delta64; 1528e180f3cSLen Brown unsigned long long extra_msr32; 1538e180f3cSLen Brown unsigned long long extra_delta32; 1541ed51011SLen Brown unsigned int smi_count; 155c98d5d94SLen Brown unsigned int cpu_id; 156c98d5d94SLen Brown unsigned int flags; 157c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 158c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 159c98d5d94SLen Brown } *thread_even, *thread_odd; 160c98d5d94SLen Brown 161c98d5d94SLen Brown struct core_data { 162c98d5d94SLen Brown unsigned long long c3; 163c98d5d94SLen Brown unsigned long long c6; 164c98d5d94SLen Brown unsigned long long c7; 165889facbeSLen Brown unsigned int core_temp_c; 166c98d5d94SLen Brown unsigned int core_id; 167c98d5d94SLen Brown } *core_even, *core_odd; 168c98d5d94SLen Brown 169c98d5d94SLen Brown struct pkg_data { 170c98d5d94SLen Brown unsigned long long pc2; 171c98d5d94SLen Brown unsigned long long pc3; 172c98d5d94SLen Brown unsigned long long pc6; 173c98d5d94SLen Brown unsigned long long pc7; 174ca58710fSKristen Carlson Accardi unsigned long long pc8; 175ca58710fSKristen Carlson Accardi unsigned long long pc9; 176ca58710fSKristen Carlson Accardi unsigned long long pc10; 1770b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1780b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1790b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1800b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 181c98d5d94SLen Brown unsigned int package_id; 182889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 183889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 184889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 185889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 186889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 187889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 188889facbeSLen Brown unsigned int pkg_temp_c; 189889facbeSLen Brown 190c98d5d94SLen Brown } *package_even, *package_odd; 191c98d5d94SLen Brown 192c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 193c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 194c98d5d94SLen Brown 195c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 196c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 197c98d5d94SLen Brown topo.num_threads_per_core + \ 198c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 199c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 200c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 201c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 202c98d5d94SLen Brown 203c98d5d94SLen Brown struct system_summary { 204c98d5d94SLen Brown struct thread_data threads; 205c98d5d94SLen Brown struct core_data cores; 206c98d5d94SLen Brown struct pkg_data packages; 207c98d5d94SLen Brown } sum, average; 208c98d5d94SLen Brown 209c98d5d94SLen Brown 210c98d5d94SLen Brown struct topo_params { 211c98d5d94SLen Brown int num_packages; 212103a8feaSLen Brown int num_cpus; 213c98d5d94SLen Brown int num_cores; 214c98d5d94SLen Brown int max_cpu_num; 215c98d5d94SLen Brown int num_cores_per_pkg; 216c98d5d94SLen Brown int num_threads_per_core; 217c98d5d94SLen Brown } topo; 218103a8feaSLen Brown 219c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 220103a8feaSLen Brown 221c98d5d94SLen Brown void setup_all_buffers(void); 222103a8feaSLen Brown 223c98d5d94SLen Brown int cpu_is_not_present(int cpu) 224d15cf7c1SLen Brown { 225c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 226c98d5d94SLen Brown } 227c98d5d94SLen Brown /* 228c98d5d94SLen Brown * run func(thread, core, package) in topology order 229c98d5d94SLen Brown * skip non-present cpus 230c98d5d94SLen Brown */ 231c98d5d94SLen Brown 232c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 233c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 234c98d5d94SLen Brown { 235c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 236c98d5d94SLen Brown 237c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 238c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 239c98d5d94SLen Brown for (thread_no = 0; thread_no < 240c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 241c98d5d94SLen Brown struct thread_data *t; 242c98d5d94SLen Brown struct core_data *c; 243c98d5d94SLen Brown struct pkg_data *p; 244c98d5d94SLen Brown 245c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 246c98d5d94SLen Brown 247c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 248c98d5d94SLen Brown continue; 249c98d5d94SLen Brown 250c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 251c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 252c98d5d94SLen Brown 253c98d5d94SLen Brown retval = func(t, c, p); 254c98d5d94SLen Brown if (retval) 255c98d5d94SLen Brown return retval; 256c98d5d94SLen Brown } 257c98d5d94SLen Brown } 258c98d5d94SLen Brown } 259d15cf7c1SLen Brown return 0; 260d15cf7c1SLen Brown } 261d15cf7c1SLen Brown 26288c3281fSLen Brown int cpu_migrate(int cpu) 26388c3281fSLen Brown { 264c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 265c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 266c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 26788c3281fSLen Brown return -1; 26888c3281fSLen Brown else 26988c3281fSLen Brown return 0; 27088c3281fSLen Brown } 27188c3281fSLen Brown 27215aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 273103a8feaSLen Brown { 274103a8feaSLen Brown ssize_t retval; 275103a8feaSLen Brown char pathname[32]; 276103a8feaSLen Brown int fd; 277103a8feaSLen Brown 278103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 279103a8feaSLen Brown fd = open(pathname, O_RDONLY); 28015aaa346SLen Brown if (fd < 0) 28198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 282103a8feaSLen Brown 28315aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 284103a8feaSLen Brown close(fd); 28515aaa346SLen Brown 28698481e79SLen Brown if (retval != sizeof *msr) 28798481e79SLen Brown err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset); 28815aaa346SLen Brown 28915aaa346SLen Brown return 0; 290103a8feaSLen Brown } 291103a8feaSLen Brown 292fc04cc67SLen Brown /* 293fc04cc67SLen Brown * Example Format w/ field column widths: 294fc04cc67SLen Brown * 295fc04cc67SLen Brown * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt 296e7c95ff3SLen Brown * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 297fc04cc67SLen Brown */ 298fc04cc67SLen Brown 299a829eb4dSLen Brown void print_header(void) 300103a8feaSLen Brown { 301103a8feaSLen Brown if (show_pkg) 302fc04cc67SLen Brown outp += sprintf(outp, " Package"); 303103a8feaSLen Brown if (show_core) 304fc04cc67SLen Brown outp += sprintf(outp, " Core"); 305103a8feaSLen Brown if (show_cpu) 306c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 307103a8feaSLen Brown if (has_aperf) 308fc04cc67SLen Brown outp += sprintf(outp, " Avg_MHz"); 309d7899447SLen Brown if (has_aperf) 310fc04cc67SLen Brown outp += sprintf(outp, " %%Busy"); 311fc04cc67SLen Brown if (has_aperf) 312fc04cc67SLen Brown outp += sprintf(outp, " Bzy_MHz"); 313fc04cc67SLen Brown outp += sprintf(outp, " TSC_MHz"); 3141cc21f7bSLen Brown 3158e180f3cSLen Brown if (extra_delta_offset32) 316f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 3178e180f3cSLen Brown if (extra_delta_offset64) 318f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 3192f32edf1SLen Brown if (extra_msr_offset32) 3208e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 3212f32edf1SLen Brown if (extra_msr_offset64) 3228e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 3231cc21f7bSLen Brown 3241cc21f7bSLen Brown if (!debug) 3251cc21f7bSLen Brown goto done; 3261cc21f7bSLen Brown 3271cc21f7bSLen Brown if (do_smi) 3281cc21f7bSLen Brown outp += sprintf(outp, " SMI"); 3291cc21f7bSLen Brown 330103a8feaSLen Brown if (do_nhm_cstates) 331fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c1"); 332fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 333fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c3"); 334103a8feaSLen Brown if (do_nhm_cstates) 335fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c6"); 336103a8feaSLen Brown if (do_snb_cstates) 337fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c7"); 338889facbeSLen Brown 339889facbeSLen Brown if (do_dts) 340fc04cc67SLen Brown outp += sprintf(outp, " CoreTmp"); 341889facbeSLen Brown if (do_ptm) 342fc04cc67SLen Brown outp += sprintf(outp, " PkgTmp"); 343889facbeSLen Brown 3440b2bb692SLen Brown if (do_skl_residency) { 3450b2bb692SLen Brown outp += sprintf(outp, " Totl%%C0"); 3460b2bb692SLen Brown outp += sprintf(outp, " Any%%C0"); 3470b2bb692SLen Brown outp += sprintf(outp, " GFX%%C0"); 3480b2bb692SLen Brown outp += sprintf(outp, " CPUGFX%%"); 3490b2bb692SLen Brown } 3500b2bb692SLen Brown 351ee7e38e3SLen Brown if (do_pc2) 352fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc2"); 353ee7e38e3SLen Brown if (do_pc3) 354fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc3"); 355ee7e38e3SLen Brown if (do_pc6) 356fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc6"); 357ee7e38e3SLen Brown if (do_pc7) 358fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc7"); 359ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 360fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc8"); 361fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc9"); 362fc04cc67SLen Brown outp += sprintf(outp, " Pk%%pc10"); 363ca58710fSKristen Carlson Accardi } 364103a8feaSLen Brown 3655c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 366889facbeSLen Brown if (do_rapl & RAPL_PKG) 367fc04cc67SLen Brown outp += sprintf(outp, " PkgWatt"); 368889facbeSLen Brown if (do_rapl & RAPL_CORES) 369fc04cc67SLen Brown outp += sprintf(outp, " CorWatt"); 370889facbeSLen Brown if (do_rapl & RAPL_GFX) 371fc04cc67SLen Brown outp += sprintf(outp, " GFXWatt"); 372889facbeSLen Brown if (do_rapl & RAPL_DRAM) 373fc04cc67SLen Brown outp += sprintf(outp, " RAMWatt"); 374889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 375889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 376889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 377889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 378d7899447SLen Brown } else if (do_rapl && rapl_joules) { 3795c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 3805c56be9aSDirk Brandewie outp += sprintf(outp, " Pkg_J"); 3815c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 3825c56be9aSDirk Brandewie outp += sprintf(outp, " Cor_J"); 3835c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 3845c56be9aSDirk Brandewie outp += sprintf(outp, " GFX_J"); 3855c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 386bd6906edSLen Brown outp += sprintf(outp, " RAM_J"); 3875c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 3885c56be9aSDirk Brandewie outp += sprintf(outp, " PKG_%%"); 3895c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 3905c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_%%"); 3915c56be9aSDirk Brandewie outp += sprintf(outp, " time"); 392889facbeSLen Brown 3935c56be9aSDirk Brandewie } 3941cc21f7bSLen Brown done: 395c98d5d94SLen Brown outp += sprintf(outp, "\n"); 396103a8feaSLen Brown } 397103a8feaSLen Brown 398c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 399c98d5d94SLen Brown struct pkg_data *p) 400103a8feaSLen Brown { 4013b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 402c98d5d94SLen Brown 403c98d5d94SLen Brown if (t) { 4043b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 4053b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 4063b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 4073b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 4083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 4093b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 4103b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4118e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 4123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4138e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 4143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4152f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 4163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4172f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 4181ed51011SLen Brown if (do_smi) 4193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 420103a8feaSLen Brown } 421103a8feaSLen Brown 422c98d5d94SLen Brown if (c) { 4233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 4243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 4253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 4263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 4273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 428c98d5d94SLen Brown } 429103a8feaSLen Brown 430c98d5d94SLen Brown if (p) { 4313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 4320b2bb692SLen Brown 4330b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 4340b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 4350b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 4360b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 4370b2bb692SLen Brown 4383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 439ee7e38e3SLen Brown if (do_pc3) 4403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 441ee7e38e3SLen Brown if (do_pc6) 4423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 443ee7e38e3SLen Brown if (do_pc7) 4443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 4453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 4463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 4473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 4483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 4493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 4503b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 4513b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 4523b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 4533b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 4543b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 4553b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 4563b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 457c98d5d94SLen Brown } 4583b4d5c7fSAndy Shevchenko 4593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 4603b4d5c7fSAndy Shevchenko 461c98d5d94SLen Brown return 0; 462103a8feaSLen Brown } 463103a8feaSLen Brown 464e23da037SLen Brown /* 465e23da037SLen Brown * column formatting convention & formats 466e23da037SLen Brown */ 467c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 468c98d5d94SLen Brown struct pkg_data *p) 469103a8feaSLen Brown { 470103a8feaSLen Brown double interval_float; 471fc04cc67SLen Brown char *fmt8; 472103a8feaSLen Brown 473c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 474c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 475c98d5d94SLen Brown return 0; 476c98d5d94SLen Brown 477c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 478c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 479c98d5d94SLen Brown return 0; 480c98d5d94SLen Brown 481103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 482103a8feaSLen Brown 483c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 484c98d5d94SLen Brown if (t == &average.threads) { 485103a8feaSLen Brown if (show_pkg) 486fc04cc67SLen Brown outp += sprintf(outp, " -"); 487103a8feaSLen Brown if (show_core) 488fc04cc67SLen Brown outp += sprintf(outp, " -"); 489103a8feaSLen Brown if (show_cpu) 490fc04cc67SLen Brown outp += sprintf(outp, " -"); 491103a8feaSLen Brown } else { 492c98d5d94SLen Brown if (show_pkg) { 493c98d5d94SLen Brown if (p) 494fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->package_id); 495c98d5d94SLen Brown else 496fc04cc67SLen Brown outp += sprintf(outp, " -"); 497c98d5d94SLen Brown } 498c98d5d94SLen Brown if (show_core) { 499c98d5d94SLen Brown if (c) 500fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_id); 501c98d5d94SLen Brown else 502fc04cc67SLen Brown outp += sprintf(outp, " -"); 503c98d5d94SLen Brown } 504103a8feaSLen Brown if (show_cpu) 505fc04cc67SLen Brown outp += sprintf(outp, "%8d", t->cpu_id); 506103a8feaSLen Brown } 507fc04cc67SLen Brown 508d7899447SLen Brown /* Avg_MHz */ 509fc04cc67SLen Brown if (has_aperf) 510fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 511fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 512fc04cc67SLen Brown 513d7899447SLen Brown /* %Busy */ 514d7899447SLen Brown if (has_aperf) { 515103a8feaSLen Brown if (!skip_c0) 516a2b7b749SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak); 517103a8feaSLen Brown else 518fc04cc67SLen Brown outp += sprintf(outp, "********"); 519103a8feaSLen Brown } 520103a8feaSLen Brown 521d7899447SLen Brown /* Bzy_MHz */ 52221ed5574SLen Brown if (has_aperf) { 52321ed5574SLen Brown if (has_base_hz) 52421ed5574SLen Brown outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf); 52521ed5574SLen Brown else 526fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 52721ed5574SLen Brown 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); 52821ed5574SLen Brown } 529103a8feaSLen Brown 530d7899447SLen Brown /* TSC_MHz */ 531fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float); 532103a8feaSLen Brown 5338e180f3cSLen Brown /* delta */ 5348e180f3cSLen Brown if (extra_delta_offset32) 5358e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 5368e180f3cSLen Brown 5378e180f3cSLen Brown /* DELTA */ 5388e180f3cSLen Brown if (extra_delta_offset64) 5398e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 5402f32edf1SLen Brown /* msr */ 5412f32edf1SLen Brown if (extra_msr_offset32) 5428e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 5432f32edf1SLen Brown 544130ff304SLen Brown /* MSR */ 5452f32edf1SLen Brown if (extra_msr_offset64) 5462f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 547130ff304SLen Brown 5481cc21f7bSLen Brown if (!debug) 5491cc21f7bSLen Brown goto done; 5501cc21f7bSLen Brown 5511cc21f7bSLen Brown /* SMI */ 5521cc21f7bSLen Brown if (do_smi) 5531cc21f7bSLen Brown outp += sprintf(outp, "%8d", t->smi_count); 5541cc21f7bSLen Brown 555103a8feaSLen Brown if (do_nhm_cstates) { 556103a8feaSLen Brown if (!skip_c1) 557fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc); 558103a8feaSLen Brown else 559fc04cc67SLen Brown outp += sprintf(outp, "********"); 560103a8feaSLen Brown } 561c98d5d94SLen Brown 562c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 563c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 564c98d5d94SLen Brown goto done; 565c98d5d94SLen Brown 566fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 567fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc); 568103a8feaSLen Brown if (do_nhm_cstates) 569fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc); 570103a8feaSLen Brown if (do_snb_cstates) 571fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc); 572c98d5d94SLen Brown 573889facbeSLen Brown if (do_dts) 574fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_temp_c); 575889facbeSLen Brown 576c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 577c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 578c98d5d94SLen Brown goto done; 579c98d5d94SLen Brown 5800b2bb692SLen Brown /* PkgTmp */ 581889facbeSLen Brown if (do_ptm) 582fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->pkg_temp_c); 583889facbeSLen Brown 5840b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 5850b2bb692SLen Brown if (do_skl_residency) { 5860b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc); 5870b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc); 5880b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc); 5890b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc); 5900b2bb692SLen Brown } 5910b2bb692SLen Brown 592ee7e38e3SLen Brown if (do_pc2) 593fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc); 594ee7e38e3SLen Brown if (do_pc3) 595fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc); 596ee7e38e3SLen Brown if (do_pc6) 597fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc); 598ee7e38e3SLen Brown if (do_pc7) 599fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc); 600ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 601fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc); 602fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc); 603fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc); 604ca58710fSKristen Carlson Accardi } 605889facbeSLen Brown 606889facbeSLen Brown /* 607889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 608889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 609889facbeSLen Brown */ 610fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 611fc04cc67SLen Brown fmt8 = "%8.2f"; 612fc04cc67SLen Brown else 613fc04cc67SLen Brown fmt8 = " %6.0f**"; 614889facbeSLen Brown 6155c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 616889facbeSLen Brown if (do_rapl & RAPL_PKG) 617fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 618889facbeSLen Brown if (do_rapl & RAPL_CORES) 619fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 620889facbeSLen Brown if (do_rapl & RAPL_GFX) 621fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 622889facbeSLen Brown if (do_rapl & RAPL_DRAM) 62340ee8e3bSAndrey Semin outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); 624889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 625fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 626889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 627fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 628d7899447SLen Brown } else if (do_rapl && rapl_joules) { 6295c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 630fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6315c56be9aSDirk Brandewie p->energy_pkg * rapl_energy_units); 6325c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 633fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6345c56be9aSDirk Brandewie p->energy_cores * rapl_energy_units); 6355c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 636fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6375c56be9aSDirk Brandewie p->energy_gfx * rapl_energy_units); 6385c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 639fc04cc67SLen Brown outp += sprintf(outp, fmt8, 64040ee8e3bSAndrey Semin p->energy_dram * rapl_dram_energy_units); 6415c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 642fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 6435c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 644fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 645889facbeSLen Brown 646d7899447SLen Brown outp += sprintf(outp, fmt8, interval_float); 6475c56be9aSDirk Brandewie } 648c98d5d94SLen Brown done: 649c98d5d94SLen Brown outp += sprintf(outp, "\n"); 650c98d5d94SLen Brown 651c98d5d94SLen Brown return 0; 652103a8feaSLen Brown } 653103a8feaSLen Brown 654c98d5d94SLen Brown void flush_stdout() 655103a8feaSLen Brown { 656c98d5d94SLen Brown fputs(output_buffer, stdout); 657ddac0d68SLen Brown fflush(stdout); 658c98d5d94SLen Brown outp = output_buffer; 659c98d5d94SLen Brown } 660c98d5d94SLen Brown void flush_stderr() 661c98d5d94SLen Brown { 662c98d5d94SLen Brown fputs(output_buffer, stderr); 663c98d5d94SLen Brown outp = output_buffer; 664c98d5d94SLen Brown } 665c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 666c98d5d94SLen Brown { 667e23da037SLen Brown static int printed; 668103a8feaSLen Brown 669e23da037SLen Brown if (!printed || !summary_only) 670103a8feaSLen Brown print_header(); 671103a8feaSLen Brown 672c98d5d94SLen Brown if (topo.num_cpus > 1) 673c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 674c98d5d94SLen Brown &average.packages); 675103a8feaSLen Brown 676e23da037SLen Brown printed = 1; 677e23da037SLen Brown 678e23da037SLen Brown if (summary_only) 679e23da037SLen Brown return; 680e23da037SLen Brown 681c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 682103a8feaSLen Brown } 683103a8feaSLen Brown 684889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 685889facbeSLen Brown if (new > old) { \ 686889facbeSLen Brown old = new - old; \ 687889facbeSLen Brown } else { \ 688889facbeSLen Brown old = 0x100000000 + new - old; \ 689889facbeSLen Brown } 690889facbeSLen Brown 691c98d5d94SLen Brown void 692c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 693103a8feaSLen Brown { 6940b2bb692SLen Brown 6950b2bb692SLen Brown if (do_skl_residency) { 6960b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 6970b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 6980b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 6990b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 7000b2bb692SLen Brown } 701c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 702ee7e38e3SLen Brown if (do_pc3) 703c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 704ee7e38e3SLen Brown if (do_pc6) 705c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 706ee7e38e3SLen Brown if (do_pc7) 707c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 708ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 709ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 710ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 711889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 712889facbeSLen Brown 713889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 714889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 715889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 716889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 717889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 718889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 719103a8feaSLen Brown } 720103a8feaSLen Brown 721c98d5d94SLen Brown void 722c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 723c98d5d94SLen Brown { 724c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 725c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 726c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 727889facbeSLen Brown old->core_temp_c = new->core_temp_c; 728103a8feaSLen Brown } 729c98d5d94SLen Brown 730c3ae331dSLen Brown /* 731c3ae331dSLen Brown * old = new - old 732c3ae331dSLen Brown */ 733c98d5d94SLen Brown void 734c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 735c98d5d94SLen Brown struct core_data *core_delta) 736c98d5d94SLen Brown { 737c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 738c98d5d94SLen Brown 739103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 740b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 741b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 742b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 743b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 744103a8feaSLen Brown 745c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 746c98d5d94SLen Brown 747a729617cSLen Brown if (has_aperf) { 748c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 749c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 750c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 751c98d5d94SLen Brown } else { 752c98d5d94SLen Brown 753103a8feaSLen Brown if (!aperf_mperf_unstable) { 754103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 755103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 756103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 757103a8feaSLen Brown 758103a8feaSLen Brown aperf_mperf_unstable = 1; 759103a8feaSLen Brown } 760103a8feaSLen Brown /* 761103a8feaSLen Brown * mperf delta is likely a huge "positive" number 762103a8feaSLen Brown * can not use it for calculating c0 time 763103a8feaSLen Brown */ 764103a8feaSLen Brown skip_c0 = 1; 765103a8feaSLen Brown skip_c1 = 1; 766103a8feaSLen Brown } 767a729617cSLen Brown } 768103a8feaSLen Brown 769c98d5d94SLen Brown 770144b44b1SLen Brown if (use_c1_residency_msr) { 771144b44b1SLen Brown /* 772144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 773144b44b1SLen Brown * which should be more accurate than the derivation below. 774144b44b1SLen Brown */ 775144b44b1SLen Brown } else { 776103a8feaSLen Brown /* 777c3ae331dSLen Brown * As counter collection is not atomic, 778c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 779103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 780103a8feaSLen Brown */ 781c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 782c98d5d94SLen Brown old->c1 = 0; 783c98d5d94SLen Brown else { 784c98d5d94SLen Brown /* normal case, derive c1 */ 785c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 786c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 787c98d5d94SLen Brown } 788144b44b1SLen Brown } 789c3ae331dSLen Brown 790c98d5d94SLen Brown if (old->mperf == 0) { 791d8af6f5fSLen Brown if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 792c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 793c98d5d94SLen Brown } 794103a8feaSLen Brown 7958e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 7968e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 7978e180f3cSLen Brown 7988e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 7998e180f3cSLen Brown 800103a8feaSLen Brown /* 8018e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 802103a8feaSLen Brown */ 8032f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 8042f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 8051ed51011SLen Brown 8061ed51011SLen Brown if (do_smi) 8071ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 808103a8feaSLen Brown } 809c98d5d94SLen Brown 810c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 811c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 812c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 813c98d5d94SLen Brown { 814c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 815c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 816c98d5d94SLen Brown delta_core(c, c2); 817c98d5d94SLen Brown 818c98d5d94SLen Brown /* always calculate thread delta */ 819c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 820c98d5d94SLen Brown 821c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 822c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 823c98d5d94SLen Brown delta_package(p, p2); 824c98d5d94SLen Brown 825103a8feaSLen Brown return 0; 826103a8feaSLen Brown } 827103a8feaSLen Brown 828c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 829103a8feaSLen Brown { 830c98d5d94SLen Brown t->tsc = 0; 831c98d5d94SLen Brown t->aperf = 0; 832c98d5d94SLen Brown t->mperf = 0; 833c98d5d94SLen Brown t->c1 = 0; 834103a8feaSLen Brown 8351ed51011SLen Brown t->smi_count = 0; 8368e180f3cSLen Brown t->extra_delta32 = 0; 8378e180f3cSLen Brown t->extra_delta64 = 0; 8388e180f3cSLen Brown 839c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 840c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 841c98d5d94SLen Brown 842c98d5d94SLen Brown c->c3 = 0; 843c98d5d94SLen Brown c->c6 = 0; 844c98d5d94SLen Brown c->c7 = 0; 845889facbeSLen Brown c->core_temp_c = 0; 846c98d5d94SLen Brown 8470b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 8480b2bb692SLen Brown p->pkg_any_core_c0 = 0; 8490b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 8500b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 8510b2bb692SLen Brown 852c98d5d94SLen Brown p->pc2 = 0; 853ee7e38e3SLen Brown if (do_pc3) 854c98d5d94SLen Brown p->pc3 = 0; 855ee7e38e3SLen Brown if (do_pc6) 856c98d5d94SLen Brown p->pc6 = 0; 857ee7e38e3SLen Brown if (do_pc7) 858c98d5d94SLen Brown p->pc7 = 0; 859ca58710fSKristen Carlson Accardi p->pc8 = 0; 860ca58710fSKristen Carlson Accardi p->pc9 = 0; 861ca58710fSKristen Carlson Accardi p->pc10 = 0; 862889facbeSLen Brown 863889facbeSLen Brown p->energy_pkg = 0; 864889facbeSLen Brown p->energy_dram = 0; 865889facbeSLen Brown p->energy_cores = 0; 866889facbeSLen Brown p->energy_gfx = 0; 867889facbeSLen Brown p->rapl_pkg_perf_status = 0; 868889facbeSLen Brown p->rapl_dram_perf_status = 0; 869889facbeSLen Brown p->pkg_temp_c = 0; 870103a8feaSLen Brown } 871c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 872c98d5d94SLen Brown struct pkg_data *p) 873103a8feaSLen Brown { 874c98d5d94SLen Brown average.threads.tsc += t->tsc; 875c98d5d94SLen Brown average.threads.aperf += t->aperf; 876c98d5d94SLen Brown average.threads.mperf += t->mperf; 877c98d5d94SLen Brown average.threads.c1 += t->c1; 87815aaa346SLen Brown 8798e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 8808e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 8818e180f3cSLen Brown 882c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 883c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 884c98d5d94SLen Brown return 0; 885c98d5d94SLen Brown 886c98d5d94SLen Brown average.cores.c3 += c->c3; 887c98d5d94SLen Brown average.cores.c6 += c->c6; 888c98d5d94SLen Brown average.cores.c7 += c->c7; 889c98d5d94SLen Brown 890889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 891889facbeSLen Brown 892c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 893c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 894c98d5d94SLen Brown return 0; 895c98d5d94SLen Brown 8960b2bb692SLen Brown if (do_skl_residency) { 8970b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 8980b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 8990b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 9000b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 9010b2bb692SLen Brown } 9020b2bb692SLen Brown 903c98d5d94SLen Brown average.packages.pc2 += p->pc2; 904ee7e38e3SLen Brown if (do_pc3) 905c98d5d94SLen Brown average.packages.pc3 += p->pc3; 906ee7e38e3SLen Brown if (do_pc6) 907c98d5d94SLen Brown average.packages.pc6 += p->pc6; 908ee7e38e3SLen Brown if (do_pc7) 909c98d5d94SLen Brown average.packages.pc7 += p->pc7; 910ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 911ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 912ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 913c98d5d94SLen Brown 914889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 915889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 916889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 917889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 918889facbeSLen Brown 919889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 920889facbeSLen Brown 921889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 922889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 923c98d5d94SLen Brown return 0; 924c98d5d94SLen Brown } 925c98d5d94SLen Brown /* 926c98d5d94SLen Brown * sum the counters for all cpus in the system 927c98d5d94SLen Brown * compute the weighted average 928c98d5d94SLen Brown */ 929c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 930c98d5d94SLen Brown struct pkg_data *p) 931c98d5d94SLen Brown { 932c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 933c98d5d94SLen Brown 934c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 935c98d5d94SLen Brown 936c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 937c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 938c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 939c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 940c98d5d94SLen Brown 9418e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 9428e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 9438e180f3cSLen Brown 9448e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 9458e180f3cSLen Brown 946c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 947c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 948c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 949c98d5d94SLen Brown 9500b2bb692SLen Brown if (do_skl_residency) { 9510b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 9520b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 9530b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 9540b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 9550b2bb692SLen Brown } 9560b2bb692SLen Brown 957c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 958ee7e38e3SLen Brown if (do_pc3) 959c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 960ee7e38e3SLen Brown if (do_pc6) 961c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 962ee7e38e3SLen Brown if (do_pc7) 963c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 964ca58710fSKristen Carlson Accardi 965ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 966ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 967ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 968c98d5d94SLen Brown } 969c98d5d94SLen Brown 970c98d5d94SLen Brown static unsigned long long rdtsc(void) 971c98d5d94SLen Brown { 972c98d5d94SLen Brown unsigned int low, high; 973c98d5d94SLen Brown 974c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 975c98d5d94SLen Brown 976c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 977c98d5d94SLen Brown } 978c98d5d94SLen Brown 979c98d5d94SLen Brown 980c98d5d94SLen Brown /* 981c98d5d94SLen Brown * get_counters(...) 982c98d5d94SLen Brown * migrate to cpu 983c98d5d94SLen Brown * acquire and record local counters for that cpu 984c98d5d94SLen Brown */ 985c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 986c98d5d94SLen Brown { 987c98d5d94SLen Brown int cpu = t->cpu_id; 988889facbeSLen Brown unsigned long long msr; 989c98d5d94SLen Brown 990e52966c0SLen Brown if (cpu_migrate(cpu)) { 991e52966c0SLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 99215aaa346SLen Brown return -1; 993e52966c0SLen Brown } 99415aaa346SLen Brown 995c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 99615aaa346SLen Brown 99715aaa346SLen Brown if (has_aperf) { 9989c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 999c98d5d94SLen Brown return -3; 10009c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1001c98d5d94SLen Brown return -4; 1002b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1003b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 100488c3281fSLen Brown } 100588c3281fSLen Brown 10061ed51011SLen Brown if (do_smi) { 10071ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 10081ed51011SLen Brown return -5; 10091ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 10101ed51011SLen Brown } 10118e180f3cSLen Brown if (extra_delta_offset32) { 1012889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 10132f32edf1SLen Brown return -5; 1014889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 10158e180f3cSLen Brown } 10168e180f3cSLen Brown 10178e180f3cSLen Brown if (extra_delta_offset64) 10188e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 10198e180f3cSLen Brown return -5; 10208e180f3cSLen Brown 10218e180f3cSLen Brown if (extra_msr_offset32) { 1022889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 10238e180f3cSLen Brown return -5; 1024889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 10258e180f3cSLen Brown } 10262f32edf1SLen Brown 10272f32edf1SLen Brown if (extra_msr_offset64) 10282f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 1029c98d5d94SLen Brown return -5; 1030c98d5d94SLen Brown 1031144b44b1SLen Brown if (use_c1_residency_msr) { 1032144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1033144b44b1SLen Brown return -6; 1034144b44b1SLen Brown } 1035144b44b1SLen Brown 1036c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1037c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1038c98d5d94SLen Brown return 0; 1039c98d5d94SLen Brown 1040fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) { 1041c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1042c98d5d94SLen Brown return -6; 1043144b44b1SLen Brown } 1044144b44b1SLen Brown 1045fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_knl_cstates) { 1046c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1047c98d5d94SLen Brown return -7; 1048fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1049fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1050fb5d4327SDasaratharaman Chandramouli return -7; 1051103a8feaSLen Brown } 105215aaa346SLen Brown 105315aaa346SLen Brown if (do_snb_cstates) 1054c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1055c98d5d94SLen Brown return -8; 1056c98d5d94SLen Brown 1057889facbeSLen Brown if (do_dts) { 1058889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1059889facbeSLen Brown return -9; 1060889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1061889facbeSLen Brown } 1062889facbeSLen Brown 1063889facbeSLen Brown 1064c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1065c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1066c98d5d94SLen Brown return 0; 106715aaa346SLen Brown 10680b2bb692SLen Brown if (do_skl_residency) { 10690b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 10700b2bb692SLen Brown return -10; 10710b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 10720b2bb692SLen Brown return -11; 10730b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 10740b2bb692SLen Brown return -12; 10750b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 10760b2bb692SLen Brown return -13; 10770b2bb692SLen Brown } 1078ee7e38e3SLen Brown if (do_pc3) 1079c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1080c98d5d94SLen Brown return -9; 1081ee7e38e3SLen Brown if (do_pc6) 1082c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1083c98d5d94SLen Brown return -10; 1084ee7e38e3SLen Brown if (do_pc2) 1085c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1086c98d5d94SLen Brown return -11; 1087ee7e38e3SLen Brown if (do_pc7) 1088c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1089c98d5d94SLen Brown return -12; 1090ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 1091ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1092ca58710fSKristen Carlson Accardi return -13; 1093ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1094ca58710fSKristen Carlson Accardi return -13; 1095ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1096ca58710fSKristen Carlson Accardi return -13; 1097ca58710fSKristen Carlson Accardi } 1098889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1099889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1100889facbeSLen Brown return -13; 1101889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1102889facbeSLen Brown } 1103889facbeSLen Brown if (do_rapl & RAPL_CORES) { 1104889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1105889facbeSLen Brown return -14; 1106889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1107889facbeSLen Brown } 1108889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1109889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1110889facbeSLen Brown return -15; 1111889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1112889facbeSLen Brown } 1113889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1114889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1115889facbeSLen Brown return -16; 1116889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1117889facbeSLen Brown } 1118889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1119889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1120889facbeSLen Brown return -16; 1121889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1122889facbeSLen Brown } 1123889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1124889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1125889facbeSLen Brown return -16; 1126889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1127889facbeSLen Brown } 1128889facbeSLen Brown if (do_ptm) { 1129889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1130889facbeSLen Brown return -17; 1131889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1132889facbeSLen Brown } 113315aaa346SLen Brown return 0; 1134103a8feaSLen Brown } 1135103a8feaSLen Brown 1136ee7e38e3SLen Brown /* 1137ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1138ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1139ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1140ee7e38e3SLen Brown */ 1141ee7e38e3SLen Brown 1142ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1143ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1144ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1145ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1146ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1147ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1148ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1149ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1150ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1151ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1152ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1153ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 11540b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 11550b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 11560b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1157ee7e38e3SLen Brown 1158ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1159ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 11600b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1161ee7e38e3SLen Brown 1162e9257f5fSLen 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}; 1163e9257f5fSLen 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}; 1164e9257f5fSLen 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}; 1165e9257f5fSLen 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}; 1166e9257f5fSLen 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}; 1167e9257f5fSLen 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}; 1168ee7e38e3SLen Brown 1169a2b7b749SLen Brown 1170a2b7b749SLen Brown static void 1171a2b7b749SLen Brown calculate_tsc_tweak() 1172a2b7b749SLen Brown { 1173a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1174a2b7b749SLen Brown } 1175a2b7b749SLen Brown 1176fcd17211SLen Brown static void 1177fcd17211SLen Brown dump_nhm_platform_info(void) 1178103a8feaSLen Brown { 1179103a8feaSLen Brown unsigned long long msr; 1180103a8feaSLen Brown unsigned int ratio; 1181103a8feaSLen Brown 1182ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1183103a8feaSLen Brown 1184ec0adc53SLen Brown fprintf(stderr, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 11856574a5d5SLen Brown 1186103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 11878f61f359SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n", 1188103a8feaSLen Brown ratio, bclk, ratio * bclk); 1189103a8feaSLen Brown 1190103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 11918f61f359SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n", 1192103a8feaSLen Brown ratio, bclk, ratio * bclk); 1193103a8feaSLen Brown 11947ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1195bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1196bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 119767920418SLen Brown 1198fcd17211SLen Brown return; 1199fcd17211SLen Brown } 1200fcd17211SLen Brown 1201fcd17211SLen Brown static void 1202fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1203fcd17211SLen Brown { 1204fcd17211SLen Brown unsigned long long msr; 1205fcd17211SLen Brown unsigned int ratio; 1206fcd17211SLen Brown 12077ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1208fcd17211SLen Brown 1209bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1210fcd17211SLen Brown 1211fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1212fcd17211SLen Brown if (ratio) 1213fcd17211SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n", 1214fcd17211SLen Brown ratio, bclk, ratio * bclk); 1215fcd17211SLen Brown 1216fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1217fcd17211SLen Brown if (ratio) 1218fcd17211SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n", 1219fcd17211SLen Brown ratio, bclk, ratio * bclk); 1220fcd17211SLen Brown return; 1221fcd17211SLen Brown } 1222fcd17211SLen Brown 1223fcd17211SLen Brown static void 1224fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1225fcd17211SLen Brown { 1226fcd17211SLen Brown unsigned long long msr; 1227fcd17211SLen Brown unsigned int ratio; 12286574a5d5SLen Brown 12297ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 12306574a5d5SLen Brown 1231bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 12326574a5d5SLen Brown 12336574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 12346574a5d5SLen Brown if (ratio) 12356574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 12366574a5d5SLen Brown ratio, bclk, ratio * bclk); 12376574a5d5SLen Brown 12386574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 12396574a5d5SLen Brown if (ratio) 12406574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 12416574a5d5SLen Brown ratio, bclk, ratio * bclk); 12426574a5d5SLen Brown 12436574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 12446574a5d5SLen Brown if (ratio) 12456574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 12466574a5d5SLen Brown ratio, bclk, ratio * bclk); 12476574a5d5SLen Brown 12486574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 12496574a5d5SLen Brown if (ratio) 12506574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 12516574a5d5SLen Brown ratio, bclk, ratio * bclk); 12526574a5d5SLen Brown 12536574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 12546574a5d5SLen Brown if (ratio) 12556574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 12566574a5d5SLen Brown ratio, bclk, ratio * bclk); 12576574a5d5SLen Brown 12586574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 12596574a5d5SLen Brown if (ratio) 12606574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 12616574a5d5SLen Brown ratio, bclk, ratio * bclk); 12626574a5d5SLen Brown 12636574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 12646574a5d5SLen Brown if (ratio) 12656574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 12666574a5d5SLen Brown ratio, bclk, ratio * bclk); 12676574a5d5SLen Brown 12686574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 12696574a5d5SLen Brown if (ratio) 12706574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 12716574a5d5SLen Brown ratio, bclk, ratio * bclk); 1272103a8feaSLen Brown return; 1273fcd17211SLen Brown } 1274fcd17211SLen Brown 1275fcd17211SLen Brown static void 1276fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void) 1277fcd17211SLen Brown { 1278fcd17211SLen Brown unsigned long long msr; 1279fcd17211SLen Brown unsigned int ratio; 1280103a8feaSLen Brown 12817ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1282103a8feaSLen Brown 1283bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 12846574a5d5SLen Brown 12856574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 12866574a5d5SLen Brown if (ratio) 12876574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 12886574a5d5SLen Brown ratio, bclk, ratio * bclk); 12896574a5d5SLen Brown 12906574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 12916574a5d5SLen Brown if (ratio) 12926574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 12936574a5d5SLen Brown ratio, bclk, ratio * bclk); 12946574a5d5SLen Brown 12956574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 12966574a5d5SLen Brown if (ratio) 12976574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 12986574a5d5SLen Brown ratio, bclk, ratio * bclk); 12996574a5d5SLen Brown 13006574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 13016574a5d5SLen Brown if (ratio) 13026574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 13036574a5d5SLen Brown ratio, bclk, ratio * bclk); 13046574a5d5SLen Brown 1305103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1306103a8feaSLen Brown if (ratio) 1307103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1308103a8feaSLen Brown ratio, bclk, ratio * bclk); 1309103a8feaSLen Brown 1310103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1311103a8feaSLen Brown if (ratio) 1312103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1313103a8feaSLen Brown ratio, bclk, ratio * bclk); 1314103a8feaSLen Brown 1315103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1316103a8feaSLen Brown if (ratio) 1317103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1318103a8feaSLen Brown ratio, bclk, ratio * bclk); 1319103a8feaSLen Brown 1320103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1321103a8feaSLen Brown if (ratio) 1322103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1323103a8feaSLen Brown ratio, bclk, ratio * bclk); 1324fcd17211SLen Brown return; 1325fcd17211SLen Brown } 13263a9a941dSLen Brown 1327fcd17211SLen Brown static void 1328fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1329fb5d4327SDasaratharaman Chandramouli { 1330fb5d4327SDasaratharaman Chandramouli int cores; 1331fb5d4327SDasaratharaman Chandramouli unsigned int ratio; 1332fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1333fb5d4327SDasaratharaman Chandramouli int delta_cores; 1334fb5d4327SDasaratharaman Chandramouli int delta_ratio; 1335fb5d4327SDasaratharaman Chandramouli int i; 1336fb5d4327SDasaratharaman Chandramouli 13377ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1338fb5d4327SDasaratharaman Chandramouli 1339bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", 1340bfae2052SLen Brown base_cpu, msr); 1341fb5d4327SDasaratharaman Chandramouli 1342fb5d4327SDasaratharaman Chandramouli /** 1343fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1344fb5d4327SDasaratharaman Chandramouli * [7:0] -- Base value of number of active cores of bucket 1. 1345fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1346fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1347fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1348fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1349fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1350fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1351fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1352fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1353fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1354fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1355fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1356fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1357fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1358fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1359fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1360fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 1361fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 1362fb5d4327SDasaratharaman Chandramouli */ 1363fb5d4327SDasaratharaman Chandramouli cores = msr & 0xFF; 1364fb5d4327SDasaratharaman Chandramouli ratio = (msr >> 8) && 0xFF; 1365fb5d4327SDasaratharaman Chandramouli if (ratio > 0) 1366fb5d4327SDasaratharaman Chandramouli fprintf(stderr, 1367fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1368fb5d4327SDasaratharaman Chandramouli ratio, bclk, ratio * bclk, cores); 1369fb5d4327SDasaratharaman Chandramouli 1370fb5d4327SDasaratharaman Chandramouli for (i = 16; i < 64; i = i + 8) { 1371fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 1372fb5d4327SDasaratharaman Chandramouli delta_ratio = (msr >> (i + 5)) && 0x7; 1373fb5d4327SDasaratharaman Chandramouli if (!delta_cores || !delta_ratio) 1374fb5d4327SDasaratharaman Chandramouli return; 1375fb5d4327SDasaratharaman Chandramouli cores = cores + delta_cores; 1376fb5d4327SDasaratharaman Chandramouli ratio = ratio - delta_ratio; 1377fb5d4327SDasaratharaman Chandramouli 1378fb5d4327SDasaratharaman Chandramouli /** -ve ratios will make successive ratio calculations 1379fb5d4327SDasaratharaman Chandramouli * negative. Hence return instead of carrying on. 1380fb5d4327SDasaratharaman Chandramouli */ 1381fb5d4327SDasaratharaman Chandramouli if (ratio > 0) 1382fb5d4327SDasaratharaman Chandramouli fprintf(stderr, 1383fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1384fb5d4327SDasaratharaman Chandramouli ratio, bclk, ratio * bclk, cores); 1385fb5d4327SDasaratharaman Chandramouli } 1386fb5d4327SDasaratharaman Chandramouli } 1387fb5d4327SDasaratharaman Chandramouli 1388fb5d4327SDasaratharaman Chandramouli static void 1389fcd17211SLen Brown dump_nhm_cst_cfg(void) 1390fcd17211SLen Brown { 1391fcd17211SLen Brown unsigned long long msr; 1392fcd17211SLen Brown 13937ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1394fcd17211SLen Brown 1395fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1396fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1397fcd17211SLen Brown 1398bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr); 1399fcd17211SLen Brown 1400fcd17211SLen Brown fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1401fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1402fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1403fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1404fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1405fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 1406fcd17211SLen Brown (unsigned int)msr & 7, 1407fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1408fcd17211SLen Brown return; 1409103a8feaSLen Brown } 1410103a8feaSLen Brown 14116fb3143bSLen Brown static void 14126fb3143bSLen Brown dump_config_tdp(void) 14136fb3143bSLen Brown { 14146fb3143bSLen Brown unsigned long long msr; 14156fb3143bSLen Brown 14166fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 14176fb3143bSLen Brown fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 14186fb3143bSLen Brown fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF); 14196fb3143bSLen Brown 14206fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 14216fb3143bSLen Brown fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 14226fb3143bSLen Brown if (msr) { 14236fb3143bSLen Brown fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF); 14246fb3143bSLen Brown fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF); 14256fb3143bSLen Brown fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); 14266fb3143bSLen Brown fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF); 14276fb3143bSLen Brown } 14286fb3143bSLen Brown fprintf(stderr, ")\n"); 14296fb3143bSLen Brown 14306fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 14316fb3143bSLen Brown fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 14326fb3143bSLen Brown if (msr) { 14336fb3143bSLen Brown fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF); 14346fb3143bSLen Brown fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF); 14356fb3143bSLen Brown fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); 14366fb3143bSLen Brown fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF); 14376fb3143bSLen Brown } 14386fb3143bSLen Brown fprintf(stderr, ")\n"); 14396fb3143bSLen Brown 14406fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 14416fb3143bSLen Brown fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 14426fb3143bSLen Brown if ((msr) & 0x3) 14436fb3143bSLen Brown fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 14446fb3143bSLen Brown fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1); 14456fb3143bSLen Brown fprintf(stderr, ")\n"); 14466fb3143bSLen Brown 14476fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 14486fb3143bSLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 1449759d2a93SLen Brown fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F); 14506fb3143bSLen Brown fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1); 14516fb3143bSLen Brown fprintf(stderr, ")\n"); 14526fb3143bSLen Brown } 14536fb3143bSLen Brown 1454c98d5d94SLen Brown void free_all_buffers(void) 1455103a8feaSLen Brown { 1456c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1457c98d5d94SLen Brown cpu_present_set = NULL; 1458c98d5d94SLen Brown cpu_present_set = 0; 1459103a8feaSLen Brown 1460c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1461c98d5d94SLen Brown cpu_affinity_set = NULL; 1462c98d5d94SLen Brown cpu_affinity_setsize = 0; 1463103a8feaSLen Brown 1464c98d5d94SLen Brown free(thread_even); 1465c98d5d94SLen Brown free(core_even); 1466c98d5d94SLen Brown free(package_even); 1467103a8feaSLen Brown 1468c98d5d94SLen Brown thread_even = NULL; 1469c98d5d94SLen Brown core_even = NULL; 1470c98d5d94SLen Brown package_even = NULL; 1471103a8feaSLen Brown 1472c98d5d94SLen Brown free(thread_odd); 1473c98d5d94SLen Brown free(core_odd); 1474c98d5d94SLen Brown free(package_odd); 1475103a8feaSLen Brown 1476c98d5d94SLen Brown thread_odd = NULL; 1477c98d5d94SLen Brown core_odd = NULL; 1478c98d5d94SLen Brown package_odd = NULL; 1479103a8feaSLen Brown 1480c98d5d94SLen Brown free(output_buffer); 1481c98d5d94SLen Brown output_buffer = NULL; 1482c98d5d94SLen Brown outp = NULL; 1483103a8feaSLen Brown } 1484103a8feaSLen Brown 1485103a8feaSLen Brown /* 148657a42a34SJosh Triplett * Open a file, and exit on failure 148757a42a34SJosh Triplett */ 148857a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 148957a42a34SJosh Triplett { 149057a42a34SJosh Triplett FILE *filep = fopen(path, "r"); 1491b2c95d90SJosh Triplett if (!filep) 1492b2c95d90SJosh Triplett err(1, "%s: open failed", path); 149357a42a34SJosh Triplett return filep; 149457a42a34SJosh Triplett } 149557a42a34SJosh Triplett 149657a42a34SJosh Triplett /* 149795aebc44SJosh Triplett * Parse a file containing a single int. 149895aebc44SJosh Triplett */ 149995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 150095aebc44SJosh Triplett { 150195aebc44SJosh Triplett va_list args; 150295aebc44SJosh Triplett char path[PATH_MAX]; 150395aebc44SJosh Triplett FILE *filep; 150495aebc44SJosh Triplett int value; 150595aebc44SJosh Triplett 150695aebc44SJosh Triplett va_start(args, fmt); 150795aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 150895aebc44SJosh Triplett va_end(args); 150957a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1510b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1511b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 151295aebc44SJosh Triplett fclose(filep); 151395aebc44SJosh Triplett return value; 151495aebc44SJosh Triplett } 151595aebc44SJosh Triplett 151695aebc44SJosh Triplett /* 1517e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 1518e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 1519e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 1520103a8feaSLen Brown */ 1521e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 1522103a8feaSLen Brown { 1523e275b388SDasaratharaman Chandramouli char path[64]; 1524e275b388SDasaratharaman Chandramouli FILE *filep; 1525e275b388SDasaratharaman Chandramouli int this_cpu; 1526e275b388SDasaratharaman Chandramouli char character; 1527e275b388SDasaratharaman Chandramouli int i; 1528e275b388SDasaratharaman Chandramouli 1529e275b388SDasaratharaman Chandramouli sprintf(path, 1530e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 1531e275b388SDasaratharaman Chandramouli cpu); 1532e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 1533e275b388SDasaratharaman Chandramouli if (filep == NULL) { 1534e275b388SDasaratharaman Chandramouli perror(path); 1535e275b388SDasaratharaman Chandramouli exit(1); 1536e275b388SDasaratharaman Chandramouli } 1537e275b388SDasaratharaman Chandramouli 1538e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 1539e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 1540e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 1541e275b388SDasaratharaman Chandramouli fclose(filep); 1542e275b388SDasaratharaman Chandramouli return i; 1543e275b388SDasaratharaman Chandramouli } 1544e275b388SDasaratharaman Chandramouli 1545e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 1546e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 1547e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 1548e275b388SDasaratharaman Chandramouli } 1549e275b388SDasaratharaman Chandramouli 1550e275b388SDasaratharaman Chandramouli fclose(filep); 1551e275b388SDasaratharaman Chandramouli return -1; 1552c98d5d94SLen Brown } 1553103a8feaSLen Brown 1554c98d5d94SLen Brown /* 1555c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1556c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1557c98d5d94SLen Brown */ 1558c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1559c98d5d94SLen Brown { 156095aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1561103a8feaSLen Brown } 1562103a8feaSLen Brown 1563103a8feaSLen Brown int get_physical_package_id(int cpu) 1564103a8feaSLen Brown { 156595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1566103a8feaSLen Brown } 1567103a8feaSLen Brown 1568103a8feaSLen Brown int get_core_id(int cpu) 1569103a8feaSLen Brown { 157095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1571103a8feaSLen Brown } 1572103a8feaSLen Brown 1573c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1574c98d5d94SLen Brown { 1575c98d5d94SLen Brown char path[80]; 1576c98d5d94SLen Brown FILE *filep; 1577e275b388SDasaratharaman Chandramouli int sib1; 1578e275b388SDasaratharaman Chandramouli int matches = 0; 1579c98d5d94SLen Brown char character; 1580e275b388SDasaratharaman Chandramouli char str[100]; 1581e275b388SDasaratharaman Chandramouli char *ch; 1582c98d5d94SLen Brown 1583c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 158457a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1585e275b388SDasaratharaman Chandramouli 1586103a8feaSLen Brown /* 1587c98d5d94SLen Brown * file format: 1588e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 1589e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 1590c98d5d94SLen Brown */ 1591e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 1592e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 1593e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 1594e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 1595e275b388SDasaratharaman Chandramouli while (ch != NULL) { 1596e275b388SDasaratharaman Chandramouli matches++; 1597e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 1598e275b388SDasaratharaman Chandramouli } 1599c98d5d94SLen Brown 1600c98d5d94SLen Brown fclose(filep); 1601e275b388SDasaratharaman Chandramouli return matches+1; 1602c98d5d94SLen Brown } 1603c98d5d94SLen Brown 1604c98d5d94SLen Brown /* 1605c98d5d94SLen Brown * run func(thread, core, package) in topology order 1606c98d5d94SLen Brown * skip non-present cpus 1607103a8feaSLen Brown */ 1608103a8feaSLen Brown 1609c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1610c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1611c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1612c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1613c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1614c98d5d94SLen Brown struct pkg_data *pkg_base2) 1615c98d5d94SLen Brown { 1616c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1617c98d5d94SLen Brown 1618c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1619c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1620c98d5d94SLen Brown for (thread_no = 0; thread_no < 1621c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1622c98d5d94SLen Brown struct thread_data *t, *t2; 1623c98d5d94SLen Brown struct core_data *c, *c2; 1624c98d5d94SLen Brown struct pkg_data *p, *p2; 1625c98d5d94SLen Brown 1626c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1627c98d5d94SLen Brown 1628c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1629c98d5d94SLen Brown continue; 1630c98d5d94SLen Brown 1631c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1632c98d5d94SLen Brown 1633c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1634c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1635c98d5d94SLen Brown 1636c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1637c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1638c98d5d94SLen Brown 1639c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1640c98d5d94SLen Brown if (retval) 1641c98d5d94SLen Brown return retval; 1642c98d5d94SLen Brown } 1643c98d5d94SLen Brown } 1644c98d5d94SLen Brown } 1645c98d5d94SLen Brown return 0; 1646c98d5d94SLen Brown } 1647c98d5d94SLen Brown 1648c98d5d94SLen Brown /* 1649c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1650c98d5d94SLen Brown * return max_cpu number 1651c98d5d94SLen Brown */ 1652c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1653103a8feaSLen Brown { 1654103a8feaSLen Brown FILE *fp; 1655c98d5d94SLen Brown int cpu_num; 1656103a8feaSLen Brown int retval; 1657103a8feaSLen Brown 165857a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1659103a8feaSLen Brown 1660103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1661b2c95d90SJosh Triplett if (retval != 0) 1662b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1663103a8feaSLen Brown 1664c98d5d94SLen Brown while (1) { 1665c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1666103a8feaSLen Brown if (retval != 1) 1667103a8feaSLen Brown break; 1668103a8feaSLen Brown 1669c98d5d94SLen Brown retval = func(cpu_num); 1670c98d5d94SLen Brown if (retval) { 1671c98d5d94SLen Brown fclose(fp); 1672c98d5d94SLen Brown return(retval); 1673c98d5d94SLen Brown } 1674103a8feaSLen Brown } 1675103a8feaSLen Brown fclose(fp); 1676c98d5d94SLen Brown return 0; 1677103a8feaSLen Brown } 1678103a8feaSLen Brown 1679103a8feaSLen Brown void re_initialize(void) 1680103a8feaSLen Brown { 1681c98d5d94SLen Brown free_all_buffers(); 1682c98d5d94SLen Brown setup_all_buffers(); 1683c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1684103a8feaSLen Brown } 1685103a8feaSLen Brown 1686c98d5d94SLen Brown 1687103a8feaSLen Brown /* 1688c98d5d94SLen Brown * count_cpus() 1689c98d5d94SLen Brown * remember the last one seen, it will be the max 1690103a8feaSLen Brown */ 1691c98d5d94SLen Brown int count_cpus(int cpu) 1692103a8feaSLen Brown { 1693c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1694c98d5d94SLen Brown topo.max_cpu_num = cpu; 1695103a8feaSLen Brown 1696c98d5d94SLen Brown topo.num_cpus += 1; 1697c98d5d94SLen Brown return 0; 1698103a8feaSLen Brown } 1699c98d5d94SLen Brown int mark_cpu_present(int cpu) 1700c98d5d94SLen Brown { 1701c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 170215aaa346SLen Brown return 0; 1703103a8feaSLen Brown } 1704103a8feaSLen Brown 1705103a8feaSLen Brown void turbostat_loop() 1706103a8feaSLen Brown { 1707c98d5d94SLen Brown int retval; 1708e52966c0SLen Brown int restarted = 0; 1709c98d5d94SLen Brown 1710103a8feaSLen Brown restart: 1711e52966c0SLen Brown restarted++; 1712e52966c0SLen Brown 1713c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1714d91bb17cSLen Brown if (retval < -1) { 1715d91bb17cSLen Brown exit(retval); 1716d91bb17cSLen Brown } else if (retval == -1) { 1717e52966c0SLen Brown if (restarted > 1) { 1718e52966c0SLen Brown exit(retval); 1719e52966c0SLen Brown } 1720c98d5d94SLen Brown re_initialize(); 1721c98d5d94SLen Brown goto restart; 1722c98d5d94SLen Brown } 1723e52966c0SLen Brown restarted = 0; 1724103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1725103a8feaSLen Brown 1726103a8feaSLen Brown while (1) { 1727c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1728103a8feaSLen Brown re_initialize(); 1729103a8feaSLen Brown goto restart; 1730103a8feaSLen Brown } 1731103a8feaSLen Brown sleep(interval_sec); 1732c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1733d91bb17cSLen Brown if (retval < -1) { 1734d91bb17cSLen Brown exit(retval); 1735d91bb17cSLen Brown } else if (retval == -1) { 173615aaa346SLen Brown re_initialize(); 173715aaa346SLen Brown goto restart; 173815aaa346SLen Brown } 1739103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1740103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1741c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1742c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1743c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1744c98d5d94SLen Brown flush_stdout(); 174515aaa346SLen Brown sleep(interval_sec); 1746c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1747d91bb17cSLen Brown if (retval < -1) { 1748d91bb17cSLen Brown exit(retval); 1749d91bb17cSLen Brown } else if (retval == -1) { 1750103a8feaSLen Brown re_initialize(); 1751103a8feaSLen Brown goto restart; 1752103a8feaSLen Brown } 1753103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1754103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1755c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1756c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1757c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1758c98d5d94SLen Brown flush_stdout(); 1759103a8feaSLen Brown } 1760103a8feaSLen Brown } 1761103a8feaSLen Brown 1762103a8feaSLen Brown void check_dev_msr() 1763103a8feaSLen Brown { 1764103a8feaSLen Brown struct stat sb; 17657ce7d5deSPrarit Bhargava char pathname[32]; 1766103a8feaSLen Brown 17677ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 17687ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 1769a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 1770d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 1771103a8feaSLen Brown } 1772103a8feaSLen Brown 177398481e79SLen Brown void check_permissions() 1774103a8feaSLen Brown { 177598481e79SLen Brown struct __user_cap_header_struct cap_header_data; 177698481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 177798481e79SLen Brown struct __user_cap_data_struct cap_data_data; 177898481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 177998481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 178098481e79SLen Brown int do_exit = 0; 17817ce7d5deSPrarit Bhargava char pathname[32]; 178298481e79SLen Brown 178398481e79SLen Brown /* check for CAP_SYS_RAWIO */ 178498481e79SLen Brown cap_header->pid = getpid(); 178598481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 178698481e79SLen Brown if (capget(cap_header, cap_data) < 0) 178798481e79SLen Brown err(-6, "capget(2) failed"); 178898481e79SLen Brown 178998481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 179098481e79SLen Brown do_exit++; 179198481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 179298481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 179398481e79SLen Brown } 179498481e79SLen Brown 179598481e79SLen Brown /* test file permissions */ 17967ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 17977ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 179898481e79SLen Brown do_exit++; 179998481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 180098481e79SLen Brown } 180198481e79SLen Brown 180298481e79SLen Brown /* if all else fails, thell them to be root */ 180398481e79SLen Brown if (do_exit) 1804b2c95d90SJosh Triplett if (getuid() != 0) 1805d7899447SLen Brown warnx("... or simply run as root"); 180698481e79SLen Brown 180798481e79SLen Brown if (do_exit) 180898481e79SLen Brown exit(-6); 1809103a8feaSLen Brown } 1810103a8feaSLen Brown 1811d7899447SLen Brown /* 1812d7899447SLen Brown * NHM adds support for additional MSRs: 1813d7899447SLen Brown * 1814d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 1815d7899447SLen Brown * 1816ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 1817d7899447SLen Brown * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 1818d7899447SLen Brown * 1819d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 1820d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 1821d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 1822d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 1823d7899447SLen Brown * 1824ee7e38e3SLen Brown * Side effect: 1825ee7e38e3SLen Brown * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL 1826d7899447SLen Brown */ 1827ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 1828103a8feaSLen Brown { 1829ee7e38e3SLen Brown unsigned long long msr; 183021ed5574SLen Brown unsigned int base_ratio; 1831ee7e38e3SLen Brown int *pkg_cstate_limits; 1832ee7e38e3SLen Brown 1833103a8feaSLen Brown if (!genuine_intel) 1834103a8feaSLen Brown return 0; 1835103a8feaSLen Brown 1836103a8feaSLen Brown if (family != 6) 1837103a8feaSLen Brown return 0; 1838103a8feaSLen Brown 183921ed5574SLen Brown bclk = discover_bclk(family, model); 184021ed5574SLen Brown 1841103a8feaSLen Brown switch (model) { 1842103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1843103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1844103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1845103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1846103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1847ee7e38e3SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1848ee7e38e3SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1849ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 1850ee7e38e3SLen Brown break; 1851103a8feaSLen Brown case 0x2A: /* SNB */ 1852103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1853553575f1SLen Brown case 0x3A: /* IVB */ 18541300651bSLen Brown case 0x3E: /* IVB Xeon */ 1855ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 1856ee7e38e3SLen Brown break; 185770b43400SLen Brown case 0x3C: /* HSW */ 1858e6f9bb3cSLen Brown case 0x3F: /* HSX */ 185970b43400SLen Brown case 0x45: /* HSW */ 1860149c2319SLen Brown case 0x46: /* HSW */ 18614e8e863fSLen Brown case 0x3D: /* BDW */ 186248a0631cSLen Brown case 0x47: /* BDW */ 18634e8e863fSLen Brown case 0x4F: /* BDX */ 18644e8e863fSLen Brown case 0x56: /* BDX-DE */ 18650b2bb692SLen Brown case 0x4E: /* SKL */ 18660b2bb692SLen Brown case 0x5E: /* SKL */ 1867ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 1868ee7e38e3SLen Brown break; 1869ee7e38e3SLen Brown case 0x37: /* BYT */ 1870ee7e38e3SLen Brown case 0x4D: /* AVN */ 1871ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 1872ee7e38e3SLen Brown break; 1873ee7e38e3SLen Brown case 0x4C: /* AMT */ 1874ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 1875ee7e38e3SLen Brown break; 1876ee7e38e3SLen Brown case 0x57: /* PHI */ 1877ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 1878ee7e38e3SLen Brown break; 1879103a8feaSLen Brown default: 1880103a8feaSLen Brown return 0; 1881103a8feaSLen Brown } 18827ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1883e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 1884ee7e38e3SLen Brown 1885ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 188621ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 188721ed5574SLen Brown 188821ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 188921ed5574SLen Brown has_base_hz = 1; 1890ee7e38e3SLen Brown return 1; 1891103a8feaSLen Brown } 1892d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) 1893d7899447SLen Brown { 1894d7899447SLen Brown switch (model) { 1895d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 1896d7899447SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1897d7899447SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1898d7899447SLen Brown return 0; 1899d7899447SLen Brown default: 1900d7899447SLen Brown return 1; 1901d7899447SLen Brown } 1902d7899447SLen Brown } 19036574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 19046574a5d5SLen Brown { 19056574a5d5SLen Brown if (!genuine_intel) 19066574a5d5SLen Brown return 0; 19076574a5d5SLen Brown 19086574a5d5SLen Brown if (family != 6) 19096574a5d5SLen Brown return 0; 19106574a5d5SLen Brown 19116574a5d5SLen Brown switch (model) { 19126574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 1913fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 19146574a5d5SLen Brown return 1; 19156574a5d5SLen Brown default: 19166574a5d5SLen Brown return 0; 19176574a5d5SLen Brown } 19186574a5d5SLen Brown } 1919fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 1920fcd17211SLen Brown { 1921fcd17211SLen Brown if (!genuine_intel) 1922fcd17211SLen Brown return 0; 1923fcd17211SLen Brown 1924fcd17211SLen Brown if (family != 6) 1925fcd17211SLen Brown return 0; 1926fcd17211SLen Brown 1927fcd17211SLen Brown switch (model) { 1928fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 1929fcd17211SLen Brown return 1; 1930fcd17211SLen Brown default: 1931fcd17211SLen Brown return 0; 1932fcd17211SLen Brown } 1933fcd17211SLen Brown } 1934fcd17211SLen Brown 1935fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 1936fb5d4327SDasaratharaman Chandramouli { 1937fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 1938fb5d4327SDasaratharaman Chandramouli return 0; 1939fb5d4327SDasaratharaman Chandramouli 1940fb5d4327SDasaratharaman Chandramouli if (family != 6) 1941fb5d4327SDasaratharaman Chandramouli return 0; 1942fb5d4327SDasaratharaman Chandramouli 1943fb5d4327SDasaratharaman Chandramouli switch (model) { 1944fb5d4327SDasaratharaman Chandramouli case 0x57: /* Knights Landing */ 1945fb5d4327SDasaratharaman Chandramouli return 1; 1946fb5d4327SDasaratharaman Chandramouli default: 1947fb5d4327SDasaratharaman Chandramouli return 0; 1948fb5d4327SDasaratharaman Chandramouli } 1949fb5d4327SDasaratharaman Chandramouli } 19506fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 19516fb3143bSLen Brown { 19526fb3143bSLen Brown if (!genuine_intel) 19536fb3143bSLen Brown return 0; 19546fb3143bSLen Brown 19556fb3143bSLen Brown if (family != 6) 19566fb3143bSLen Brown return 0; 19576fb3143bSLen Brown 19586fb3143bSLen Brown switch (model) { 19596fb3143bSLen Brown case 0x3A: /* IVB */ 19606fb3143bSLen Brown case 0x3C: /* HSW */ 19616fb3143bSLen Brown case 0x3F: /* HSX */ 19626fb3143bSLen Brown case 0x45: /* HSW */ 19636fb3143bSLen Brown case 0x46: /* HSW */ 19646fb3143bSLen Brown case 0x3D: /* BDW */ 19656fb3143bSLen Brown case 0x47: /* BDW */ 19666fb3143bSLen Brown case 0x4F: /* BDX */ 19676fb3143bSLen Brown case 0x56: /* BDX-DE */ 19686fb3143bSLen Brown case 0x4E: /* SKL */ 19696fb3143bSLen Brown case 0x5E: /* SKL */ 19706fb3143bSLen Brown 19716fb3143bSLen Brown case 0x57: /* Knights Landing */ 19726fb3143bSLen Brown return 1; 19736fb3143bSLen Brown default: 19746fb3143bSLen Brown return 0; 19756fb3143bSLen Brown } 19766fb3143bSLen Brown } 19776fb3143bSLen Brown 1978fcd17211SLen Brown static void 1979fcd17211SLen Brown dump_cstate_pstate_config_info(family, model) 1980fcd17211SLen Brown { 1981fcd17211SLen Brown if (!do_nhm_platform_info) 1982fcd17211SLen Brown return; 1983fcd17211SLen Brown 1984fcd17211SLen Brown dump_nhm_platform_info(); 1985fcd17211SLen Brown 1986fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 1987fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 1988fcd17211SLen Brown 1989fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 1990fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 1991fcd17211SLen Brown 1992fcd17211SLen Brown if (has_nhm_turbo_ratio_limit(family, model)) 1993fcd17211SLen Brown dump_nhm_turbo_ratio_limits(); 1994fcd17211SLen Brown 1995fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 1996fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 1997fb5d4327SDasaratharaman Chandramouli 19986fb3143bSLen Brown if (has_config_tdp(family, model)) 19996fb3143bSLen Brown dump_config_tdp(); 20006fb3143bSLen Brown 2001fcd17211SLen Brown dump_nhm_cst_cfg(); 2002fcd17211SLen Brown } 2003fcd17211SLen Brown 20046574a5d5SLen Brown 2005889facbeSLen Brown /* 2006889facbeSLen Brown * print_epb() 2007889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 2008889facbeSLen Brown */ 2009889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2010889facbeSLen Brown { 2011889facbeSLen Brown unsigned long long msr; 2012889facbeSLen Brown char *epb_string; 2013889facbeSLen Brown int cpu; 2014889facbeSLen Brown 2015889facbeSLen Brown if (!has_epb) 2016889facbeSLen Brown return 0; 2017889facbeSLen Brown 2018889facbeSLen Brown cpu = t->cpu_id; 2019889facbeSLen Brown 2020889facbeSLen Brown /* EPB is per-package */ 2021889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2022889facbeSLen Brown return 0; 2023889facbeSLen Brown 2024889facbeSLen Brown if (cpu_migrate(cpu)) { 2025889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2026889facbeSLen Brown return -1; 2027889facbeSLen Brown } 2028889facbeSLen Brown 2029889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 2030889facbeSLen Brown return 0; 2031889facbeSLen Brown 2032e9be7dd6SLen Brown switch (msr & 0xF) { 2033889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 2034889facbeSLen Brown epb_string = "performance"; 2035889facbeSLen Brown break; 2036889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 2037889facbeSLen Brown epb_string = "balanced"; 2038889facbeSLen Brown break; 2039889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 2040889facbeSLen Brown epb_string = "powersave"; 2041889facbeSLen Brown break; 2042889facbeSLen Brown default: 2043889facbeSLen Brown epb_string = "custom"; 2044889facbeSLen Brown break; 2045889facbeSLen Brown } 2046889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 2047889facbeSLen Brown 2048889facbeSLen Brown return 0; 2049889facbeSLen Brown } 2050*7f5c258eSLen Brown /* 2051*7f5c258eSLen Brown * print_hwp() 2052*7f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 2053*7f5c258eSLen Brown */ 2054*7f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2055*7f5c258eSLen Brown { 2056*7f5c258eSLen Brown unsigned long long msr; 2057*7f5c258eSLen Brown int cpu; 2058*7f5c258eSLen Brown 2059*7f5c258eSLen Brown if (!has_hwp) 2060*7f5c258eSLen Brown return 0; 2061*7f5c258eSLen Brown 2062*7f5c258eSLen Brown cpu = t->cpu_id; 2063*7f5c258eSLen Brown 2064*7f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 2065*7f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2066*7f5c258eSLen Brown return 0; 2067*7f5c258eSLen Brown 2068*7f5c258eSLen Brown if (cpu_migrate(cpu)) { 2069*7f5c258eSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2070*7f5c258eSLen Brown return -1; 2071*7f5c258eSLen Brown } 2072*7f5c258eSLen Brown 2073*7f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 2074*7f5c258eSLen Brown return 0; 2075*7f5c258eSLen Brown 2076*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 2077*7f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 2078*7f5c258eSLen Brown 2079*7f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 2080*7f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 2081*7f5c258eSLen Brown return 0; 2082*7f5c258eSLen Brown 2083*7f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 2084*7f5c258eSLen Brown return 0; 2085*7f5c258eSLen Brown 2086*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 2087*7f5c258eSLen Brown "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n", 2088*7f5c258eSLen Brown cpu, msr, 2089*7f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 2090*7f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 2091*7f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 2092*7f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 2093*7f5c258eSLen Brown 2094*7f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 2095*7f5c258eSLen Brown return 0; 2096*7f5c258eSLen Brown 2097*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 2098*7f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n", 2099*7f5c258eSLen Brown cpu, msr, 2100*7f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 2101*7f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 2102*7f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 2103*7f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 2104*7f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 2105*7f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 2106*7f5c258eSLen Brown 2107*7f5c258eSLen Brown if (has_hwp_pkg) { 2108*7f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 2109*7f5c258eSLen Brown return 0; 2110*7f5c258eSLen Brown 2111*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 2112*7f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n", 2113*7f5c258eSLen Brown cpu, msr, 2114*7f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 2115*7f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 2116*7f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 2117*7f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 2118*7f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 2119*7f5c258eSLen Brown } 2120*7f5c258eSLen Brown if (has_hwp_notify) { 2121*7f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 2122*7f5c258eSLen Brown return 0; 2123*7f5c258eSLen Brown 2124*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 2125*7f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 2126*7f5c258eSLen Brown cpu, msr, 2127*7f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 2128*7f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 2129*7f5c258eSLen Brown } 2130*7f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 2131*7f5c258eSLen Brown return 0; 2132*7f5c258eSLen Brown 2133*7f5c258eSLen Brown fprintf(stderr, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 2134*7f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 2135*7f5c258eSLen Brown cpu, msr, 2136*7f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 2137*7f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 2138*7f5c258eSLen Brown 2139*7f5c258eSLen Brown return 0; 2140*7f5c258eSLen Brown } 2141889facbeSLen Brown 21423a9a941dSLen Brown /* 21433a9a941dSLen Brown * print_perf_limit() 21443a9a941dSLen Brown */ 21453a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 21463a9a941dSLen Brown { 21473a9a941dSLen Brown unsigned long long msr; 21483a9a941dSLen Brown int cpu; 21493a9a941dSLen Brown 21503a9a941dSLen Brown cpu = t->cpu_id; 21513a9a941dSLen Brown 21523a9a941dSLen Brown /* per-package */ 21533a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 21543a9a941dSLen Brown return 0; 21553a9a941dSLen Brown 21563a9a941dSLen Brown if (cpu_migrate(cpu)) { 21573a9a941dSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 21583a9a941dSLen Brown return -1; 21593a9a941dSLen Brown } 21603a9a941dSLen Brown 21613a9a941dSLen Brown if (do_core_perf_limit_reasons) { 21623a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 21633a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 21643a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 2165e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 21663a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 2167e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 2168e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 2169e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 2170e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 2171e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 2172e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 2173e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 2174e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 2175e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 2176e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 2177e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 2178e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 21793a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 2180e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 21813a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 2182e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 2183e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 2184e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 2185e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 2186e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 2187e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 2188e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 2189e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 2190e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 2191e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 2192e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 2193e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 21943a9a941dSLen Brown 21953a9a941dSLen Brown } 21963a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 21973a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 21983a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 21993a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)", 22003a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 22013a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 22023a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 22033a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 22043a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 22053a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 22063a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 22073a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 22083a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n", 22093a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 22103a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 22113a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 22123a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 22133a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 22143a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 22153a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 22163a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 22173a9a941dSLen Brown } 22183a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 22193a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 22203a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 22213a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s)", 22223a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 22233a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 22243a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 22253a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 22263a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 22273a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 22283a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n", 22293a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 22303a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 22313a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 22323a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 22333a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 22343a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 22353a9a941dSLen Brown } 22363a9a941dSLen Brown return 0; 22373a9a941dSLen Brown } 22383a9a941dSLen Brown 2239889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 2240889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 2241889facbeSLen Brown 2242144b44b1SLen Brown double get_tdp(model) 2243144b44b1SLen Brown { 2244144b44b1SLen Brown unsigned long long msr; 2245144b44b1SLen Brown 2246144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 22477ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 2248144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 2249144b44b1SLen Brown 2250144b44b1SLen Brown switch (model) { 2251144b44b1SLen Brown case 0x37: 2252144b44b1SLen Brown case 0x4D: 2253144b44b1SLen Brown return 30.0; 2254144b44b1SLen Brown default: 2255144b44b1SLen Brown return 135.0; 2256144b44b1SLen Brown } 2257144b44b1SLen Brown } 2258144b44b1SLen Brown 225940ee8e3bSAndrey Semin /* 226040ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 226140ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 226240ee8e3bSAndrey Semin */ 226340ee8e3bSAndrey Semin static double 226440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 226540ee8e3bSAndrey Semin { 226640ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 226740ee8e3bSAndrey Semin 226840ee8e3bSAndrey Semin switch (model) { 226940ee8e3bSAndrey Semin case 0x3F: /* HSX */ 227040ee8e3bSAndrey Semin case 0x4F: /* BDX */ 227140ee8e3bSAndrey Semin case 0x56: /* BDX-DE */ 2272fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 227340ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 227440ee8e3bSAndrey Semin default: 227540ee8e3bSAndrey Semin return (rapl_energy_units); 227640ee8e3bSAndrey Semin } 227740ee8e3bSAndrey Semin } 227840ee8e3bSAndrey Semin 2279144b44b1SLen Brown 2280889facbeSLen Brown /* 2281889facbeSLen Brown * rapl_probe() 2282889facbeSLen Brown * 2283144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 2284889facbeSLen Brown */ 2285889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 2286889facbeSLen Brown { 2287889facbeSLen Brown unsigned long long msr; 2288144b44b1SLen Brown unsigned int time_unit; 2289889facbeSLen Brown double tdp; 2290889facbeSLen Brown 2291889facbeSLen Brown if (!genuine_intel) 2292889facbeSLen Brown return; 2293889facbeSLen Brown 2294889facbeSLen Brown if (family != 6) 2295889facbeSLen Brown return; 2296889facbeSLen Brown 2297889facbeSLen Brown switch (model) { 2298889facbeSLen Brown case 0x2A: 2299889facbeSLen Brown case 0x3A: 230070b43400SLen Brown case 0x3C: /* HSW */ 230170b43400SLen Brown case 0x45: /* HSW */ 2302149c2319SLen Brown case 0x46: /* HSW */ 23034e8e863fSLen Brown case 0x3D: /* BDW */ 230448a0631cSLen Brown case 0x47: /* BDW */ 2305144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 2306889facbeSLen Brown break; 23070b2bb692SLen Brown case 0x4E: /* SKL */ 23080b2bb692SLen Brown case 0x5E: /* SKL */ 23090b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 23100b2bb692SLen Brown break; 2311e6f9bb3cSLen Brown case 0x3F: /* HSX */ 23124e8e863fSLen Brown case 0x4F: /* BDX */ 23134e8e863fSLen Brown case 0x56: /* BDX-DE */ 2314fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 23150b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 2316e6f9bb3cSLen Brown break; 2317889facbeSLen Brown case 0x2D: 2318889facbeSLen Brown case 0x3E: 23190b2bb692SLen 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; 2320144b44b1SLen Brown break; 2321144b44b1SLen Brown case 0x37: /* BYT */ 2322144b44b1SLen Brown case 0x4D: /* AVN */ 2323144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES ; 2324889facbeSLen Brown break; 2325889facbeSLen Brown default: 2326889facbeSLen Brown return; 2327889facbeSLen Brown } 2328889facbeSLen Brown 2329889facbeSLen Brown /* units on package 0, verify later other packages match */ 23307ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 2331889facbeSLen Brown return; 2332889facbeSLen Brown 2333889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 2334144b44b1SLen Brown if (model == 0x37) 2335144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 2336144b44b1SLen Brown else 2337889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 2338889facbeSLen Brown 233940ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 234040ee8e3bSAndrey Semin 2341144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 2342144b44b1SLen Brown if (time_unit == 0) 2343144b44b1SLen Brown time_unit = 0xA; 2344889facbeSLen Brown 2345144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 2346144b44b1SLen Brown 2347144b44b1SLen Brown tdp = get_tdp(model); 2348889facbeSLen Brown 2349889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 2350d8af6f5fSLen Brown if (debug) 2351144b44b1SLen Brown fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 2352889facbeSLen Brown 2353889facbeSLen Brown return; 2354889facbeSLen Brown } 2355889facbeSLen Brown 23563a9a941dSLen Brown void perf_limit_reasons_probe(family, model) 23573a9a941dSLen Brown { 23583a9a941dSLen Brown if (!genuine_intel) 23593a9a941dSLen Brown return; 23603a9a941dSLen Brown 23613a9a941dSLen Brown if (family != 6) 23623a9a941dSLen Brown return; 23633a9a941dSLen Brown 23643a9a941dSLen Brown switch (model) { 23653a9a941dSLen Brown case 0x3C: /* HSW */ 23663a9a941dSLen Brown case 0x45: /* HSW */ 23673a9a941dSLen Brown case 0x46: /* HSW */ 23683a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 23693a9a941dSLen Brown case 0x3F: /* HSX */ 23703a9a941dSLen Brown do_core_perf_limit_reasons = 1; 23713a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 23723a9a941dSLen Brown default: 23733a9a941dSLen Brown return; 23743a9a941dSLen Brown } 23753a9a941dSLen Brown } 23763a9a941dSLen Brown 2377889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2378889facbeSLen Brown { 2379889facbeSLen Brown unsigned long long msr; 2380889facbeSLen Brown unsigned int dts; 2381889facbeSLen Brown int cpu; 2382889facbeSLen Brown 2383889facbeSLen Brown if (!(do_dts || do_ptm)) 2384889facbeSLen Brown return 0; 2385889facbeSLen Brown 2386889facbeSLen Brown cpu = t->cpu_id; 2387889facbeSLen Brown 2388889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 2389889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2390889facbeSLen Brown return 0; 2391889facbeSLen Brown 2392889facbeSLen Brown if (cpu_migrate(cpu)) { 2393889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2394889facbeSLen Brown return -1; 2395889facbeSLen Brown } 2396889facbeSLen Brown 2397889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 2398889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2399889facbeSLen Brown return 0; 2400889facbeSLen Brown 2401889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2402889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 2403889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 2404889facbeSLen Brown 2405889facbeSLen Brown #ifdef THERM_DEBUG 2406889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 2407889facbeSLen Brown return 0; 2408889facbeSLen Brown 2409889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2410889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2411889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2412889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2413889facbeSLen Brown #endif 2414889facbeSLen Brown } 2415889facbeSLen Brown 2416889facbeSLen Brown 2417889facbeSLen Brown if (do_dts) { 2418889facbeSLen Brown unsigned int resolution; 2419889facbeSLen Brown 2420889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2421889facbeSLen Brown return 0; 2422889facbeSLen Brown 2423889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2424889facbeSLen Brown resolution = (msr >> 27) & 0xF; 2425889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 2426889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 2427889facbeSLen Brown 2428889facbeSLen Brown #ifdef THERM_DEBUG 2429889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 2430889facbeSLen Brown return 0; 2431889facbeSLen Brown 2432889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2433889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2434889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2435889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2436889facbeSLen Brown #endif 2437889facbeSLen Brown } 2438889facbeSLen Brown 2439889facbeSLen Brown return 0; 2440889facbeSLen Brown } 2441889facbeSLen Brown 2442889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 2443889facbeSLen Brown { 2444889facbeSLen Brown fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 2445889facbeSLen Brown cpu, label, 2446889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 2447889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 2448889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 2449889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 2450889facbeSLen Brown 2451889facbeSLen Brown return; 2452889facbeSLen Brown } 2453889facbeSLen Brown 2454889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2455889facbeSLen Brown { 2456889facbeSLen Brown unsigned long long msr; 2457889facbeSLen Brown int cpu; 2458889facbeSLen Brown 2459889facbeSLen Brown if (!do_rapl) 2460889facbeSLen Brown return 0; 2461889facbeSLen Brown 2462889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 2463889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2464889facbeSLen Brown return 0; 2465889facbeSLen Brown 2466889facbeSLen Brown cpu = t->cpu_id; 2467889facbeSLen Brown if (cpu_migrate(cpu)) { 2468889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2469889facbeSLen Brown return -1; 2470889facbeSLen Brown } 2471889facbeSLen Brown 2472889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 2473889facbeSLen Brown return -1; 2474889facbeSLen Brown 2475d8af6f5fSLen Brown if (debug) { 2476889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 2477889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 2478144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 2479889facbeSLen Brown } 2480144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 2481144b44b1SLen Brown 2482889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 2483889facbeSLen Brown return -5; 2484889facbeSLen Brown 2485889facbeSLen Brown 2486889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2487889facbeSLen Brown cpu, msr, 2488889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2489889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2490889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2491889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2492889facbeSLen Brown 2493144b44b1SLen Brown } 2494144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 2495144b44b1SLen Brown 2496889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 2497889facbeSLen Brown return -9; 2498889facbeSLen Brown 2499889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 2500889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 2501889facbeSLen Brown 2502889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 2503889facbeSLen Brown fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 2504889facbeSLen Brown cpu, 2505889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 2506889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 2507889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 2508889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 2509889facbeSLen Brown } 2510889facbeSLen Brown 25110b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 2512889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 2513889facbeSLen Brown return -6; 2514889facbeSLen Brown 2515889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2516889facbeSLen Brown cpu, msr, 2517889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2518889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2519889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2520889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 25210b2bb692SLen Brown } 25220b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 2523889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 2524889facbeSLen Brown return -9; 2525889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 2526889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2527889facbeSLen Brown 2528889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 2529889facbeSLen Brown } 2530144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 2531d8af6f5fSLen Brown if (debug) { 2532889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 2533889facbeSLen Brown return -7; 2534889facbeSLen Brown 2535889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 2536144b44b1SLen Brown } 2537144b44b1SLen Brown } 2538144b44b1SLen Brown if (do_rapl & RAPL_CORES) { 2539d8af6f5fSLen Brown if (debug) { 2540889facbeSLen Brown 2541889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 2542889facbeSLen Brown return -9; 2543889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 2544889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2545889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 2546889facbeSLen Brown } 2547889facbeSLen Brown } 2548889facbeSLen Brown if (do_rapl & RAPL_GFX) { 2549d8af6f5fSLen Brown if (debug) { 2550889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 2551889facbeSLen Brown return -8; 2552889facbeSLen Brown 2553889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 2554889facbeSLen Brown 2555889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 2556889facbeSLen Brown return -9; 2557889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 2558889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2559889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 2560889facbeSLen Brown } 2561889facbeSLen Brown } 2562889facbeSLen Brown return 0; 2563889facbeSLen Brown } 2564889facbeSLen Brown 2565d7899447SLen Brown /* 2566d7899447SLen Brown * SNB adds support for additional MSRs: 2567d7899447SLen Brown * 2568d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 2569d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 2570d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 2571d7899447SLen Brown */ 2572103a8feaSLen Brown 2573d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 2574103a8feaSLen Brown { 2575103a8feaSLen Brown if (!genuine_intel) 2576103a8feaSLen Brown return 0; 2577103a8feaSLen Brown 2578103a8feaSLen Brown switch (model) { 2579103a8feaSLen Brown case 0x2A: 2580103a8feaSLen Brown case 0x2D: 2581650a37f3SLen Brown case 0x3A: /* IVB */ 25821300651bSLen Brown case 0x3E: /* IVB Xeon */ 258370b43400SLen Brown case 0x3C: /* HSW */ 258470b43400SLen Brown case 0x3F: /* HSW */ 258570b43400SLen Brown case 0x45: /* HSW */ 2586149c2319SLen Brown case 0x46: /* HSW */ 25874e8e863fSLen Brown case 0x3D: /* BDW */ 258848a0631cSLen Brown case 0x47: /* BDW */ 25894e8e863fSLen Brown case 0x4F: /* BDX */ 25904e8e863fSLen Brown case 0x56: /* BDX-DE */ 25910b2bb692SLen Brown case 0x4E: /* SKL */ 25920b2bb692SLen Brown case 0x5E: /* SKL */ 2593103a8feaSLen Brown return 1; 2594103a8feaSLen Brown } 2595103a8feaSLen Brown return 0; 2596103a8feaSLen Brown } 2597103a8feaSLen Brown 2598d7899447SLen Brown /* 2599d7899447SLen Brown * HSW adds support for additional MSRs: 2600d7899447SLen Brown * 2601d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 2602d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 2603d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 2604d7899447SLen Brown */ 2605d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 2606ca58710fSKristen Carlson Accardi { 2607ca58710fSKristen Carlson Accardi if (!genuine_intel) 2608ca58710fSKristen Carlson Accardi return 0; 2609ca58710fSKristen Carlson Accardi 2610ca58710fSKristen Carlson Accardi switch (model) { 26114e8e863fSLen Brown case 0x45: /* HSW */ 26124e8e863fSLen Brown case 0x3D: /* BDW */ 26130b2bb692SLen Brown case 0x4E: /* SKL */ 26140b2bb692SLen Brown case 0x5E: /* SKL */ 2615ca58710fSKristen Carlson Accardi return 1; 2616ca58710fSKristen Carlson Accardi } 2617ca58710fSKristen Carlson Accardi return 0; 2618ca58710fSKristen Carlson Accardi } 2619ca58710fSKristen Carlson Accardi 26200b2bb692SLen Brown /* 26210b2bb692SLen Brown * SKL adds support for additional MSRS: 26220b2bb692SLen Brown * 26230b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 26240b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 26250b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 26260b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 26270b2bb692SLen Brown */ 26280b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 26290b2bb692SLen Brown { 26300b2bb692SLen Brown if (!genuine_intel) 26310b2bb692SLen Brown return 0; 26320b2bb692SLen Brown 26330b2bb692SLen Brown switch (model) { 26340b2bb692SLen Brown case 0x4E: /* SKL */ 26350b2bb692SLen Brown case 0x5E: /* SKL */ 26360b2bb692SLen Brown return 1; 26370b2bb692SLen Brown } 26380b2bb692SLen Brown return 0; 26390b2bb692SLen Brown } 26400b2bb692SLen Brown 26410b2bb692SLen Brown 2642ca58710fSKristen Carlson Accardi 2643144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 2644144b44b1SLen Brown { 2645144b44b1SLen Brown if (!genuine_intel) 2646144b44b1SLen Brown return 0; 2647144b44b1SLen Brown switch (model) { 2648144b44b1SLen Brown case 0x37: /* BYT */ 2649144b44b1SLen Brown case 0x4D: /* AVN */ 2650144b44b1SLen Brown return 1; 2651144b44b1SLen Brown } 2652144b44b1SLen Brown return 0; 2653144b44b1SLen Brown } 2654144b44b1SLen Brown 2655fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 2656fb5d4327SDasaratharaman Chandramouli { 2657fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2658fb5d4327SDasaratharaman Chandramouli return 0; 2659fb5d4327SDasaratharaman Chandramouli switch (model) { 2660fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 2661fb5d4327SDasaratharaman Chandramouli return 1; 2662fb5d4327SDasaratharaman Chandramouli } 2663fb5d4327SDasaratharaman Chandramouli return 0; 2664fb5d4327SDasaratharaman Chandramouli } 2665fb5d4327SDasaratharaman Chandramouli 2666b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 2667b2b34dfeSHubert Chrzaniuk { 2668b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 2669b2b34dfeSHubert Chrzaniuk return 1024; 2670b2b34dfeSHubert Chrzaniuk return 1; 2671b2b34dfeSHubert Chrzaniuk } 2672b2b34dfeSHubert Chrzaniuk 2673144b44b1SLen Brown #define SLM_BCLK_FREQS 5 2674144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 2675144b44b1SLen Brown 2676144b44b1SLen Brown double slm_bclk(void) 2677144b44b1SLen Brown { 2678144b44b1SLen Brown unsigned long long msr = 3; 2679144b44b1SLen Brown unsigned int i; 2680144b44b1SLen Brown double freq; 2681144b44b1SLen Brown 26827ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 2683144b44b1SLen Brown fprintf(stderr, "SLM BCLK: unknown\n"); 2684144b44b1SLen Brown 2685144b44b1SLen Brown i = msr & 0xf; 2686144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 2687144b44b1SLen Brown fprintf(stderr, "SLM BCLK[%d] invalid\n", i); 2688144b44b1SLen Brown msr = 3; 2689144b44b1SLen Brown } 2690144b44b1SLen Brown freq = slm_freq_table[i]; 2691144b44b1SLen Brown 2692144b44b1SLen Brown fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); 2693144b44b1SLen Brown 2694144b44b1SLen Brown return freq; 2695144b44b1SLen Brown } 2696144b44b1SLen Brown 2697103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 2698103a8feaSLen Brown { 2699d7899447SLen Brown if (has_snb_msrs(family, model)) 2700103a8feaSLen Brown return 100.00; 2701144b44b1SLen Brown else if (is_slm(family, model)) 2702144b44b1SLen Brown return slm_bclk(); 2703103a8feaSLen Brown else 2704103a8feaSLen Brown return 133.33; 2705103a8feaSLen Brown } 2706103a8feaSLen Brown 2707889facbeSLen Brown /* 2708889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 2709889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 2710889facbeSLen Brown * This is usually equal to tjMax. 2711889facbeSLen Brown * 2712889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 2713889facbeSLen Brown * but also allow cmdline over-ride with -T. 2714889facbeSLen Brown * 2715889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 2716889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 2717889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 2718889facbeSLen Brown */ 2719889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2720889facbeSLen Brown { 2721889facbeSLen Brown unsigned long long msr; 2722889facbeSLen Brown unsigned int target_c_local; 2723889facbeSLen Brown int cpu; 2724889facbeSLen Brown 2725889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 2726889facbeSLen Brown if (!(do_dts || do_ptm)) 2727889facbeSLen Brown return 0; 2728889facbeSLen Brown 2729889facbeSLen Brown /* this is a per-package concept */ 2730889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2731889facbeSLen Brown return 0; 2732889facbeSLen Brown 2733889facbeSLen Brown cpu = t->cpu_id; 2734889facbeSLen Brown if (cpu_migrate(cpu)) { 2735889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2736889facbeSLen Brown return -1; 2737889facbeSLen Brown } 2738889facbeSLen Brown 2739889facbeSLen Brown if (tcc_activation_temp_override != 0) { 2740889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 2741889facbeSLen Brown fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 2742889facbeSLen Brown cpu, tcc_activation_temp); 2743889facbeSLen Brown return 0; 2744889facbeSLen Brown } 2745889facbeSLen Brown 2746889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 2747d7899447SLen Brown if (!do_nhm_platform_info) 2748889facbeSLen Brown goto guess; 2749889facbeSLen Brown 27507ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 2751889facbeSLen Brown goto guess; 2752889facbeSLen Brown 27533482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 2754889facbeSLen Brown 2755d8af6f5fSLen Brown if (debug) 2756889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 2757889facbeSLen Brown cpu, msr, target_c_local); 2758889facbeSLen Brown 27593482124aSJean Delvare if (!target_c_local) 2760889facbeSLen Brown goto guess; 2761889facbeSLen Brown 2762889facbeSLen Brown tcc_activation_temp = target_c_local; 2763889facbeSLen Brown 2764889facbeSLen Brown return 0; 2765889facbeSLen Brown 2766889facbeSLen Brown guess: 2767889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 2768889facbeSLen Brown fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 2769889facbeSLen Brown cpu, tcc_activation_temp); 2770889facbeSLen Brown 2771889facbeSLen Brown return 0; 2772889facbeSLen Brown } 277369807a63SLen Brown 277469807a63SLen Brown void decode_misc_enable_msr(void) 277569807a63SLen Brown { 277669807a63SLen Brown unsigned long long msr; 277769807a63SLen Brown 277869807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 277969807a63SLen Brown fprintf(stderr, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n", 278069807a63SLen Brown base_cpu, msr, 278169807a63SLen Brown msr & (1 << 3) ? "TCC" : "", 278269807a63SLen Brown msr & (1 << 16) ? "EIST" : "", 278369807a63SLen Brown msr & (1 << 18) ? "MONITOR" : ""); 278469807a63SLen Brown } 278569807a63SLen Brown 2786*7f5c258eSLen Brown 2787fcd17211SLen Brown void process_cpuid() 2788103a8feaSLen Brown { 278961a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 2790103a8feaSLen Brown unsigned int fms, family, model, stepping; 2791103a8feaSLen Brown 2792103a8feaSLen Brown eax = ebx = ecx = edx = 0; 2793103a8feaSLen Brown 27942b92865eSJosh Triplett __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 2795103a8feaSLen Brown 2796103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 2797103a8feaSLen Brown genuine_intel = 1; 2798103a8feaSLen Brown 2799d8af6f5fSLen Brown if (debug) 2800889facbeSLen Brown fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 2801103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 2802103a8feaSLen Brown 28032b92865eSJosh Triplett __get_cpuid(1, &fms, &ebx, &ecx, &edx); 2804103a8feaSLen Brown family = (fms >> 8) & 0xf; 2805103a8feaSLen Brown model = (fms >> 4) & 0xf; 2806103a8feaSLen Brown stepping = fms & 0xf; 2807103a8feaSLen Brown if (family == 6 || family == 0xf) 2808103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 2809103a8feaSLen Brown 281069807a63SLen Brown if (debug) { 2811103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 2812103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 281369807a63SLen Brown fprintf(stderr, "CPUID(1): %s %s %s %s %s %s %s %s\n", 281469807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 281569807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 281669807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 281769807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 281869807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 281969807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 282069807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 282169807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 282269807a63SLen Brown } 2823103a8feaSLen Brown 2824b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 2825b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 2826103a8feaSLen Brown 2827103a8feaSLen Brown /* 2828103a8feaSLen Brown * check max extended function levels of CPUID. 2829103a8feaSLen Brown * This is needed to check for invariant TSC. 2830103a8feaSLen Brown * This check is valid for both Intel and AMD. 2831103a8feaSLen Brown */ 2832103a8feaSLen Brown ebx = ecx = edx = 0; 283361a87ba7SLen Brown __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx); 2834103a8feaSLen Brown 283561a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 2836103a8feaSLen Brown 2837103a8feaSLen Brown /* 2838103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 2839103a8feaSLen Brown * this check is valid for both Intel and AMD 2840103a8feaSLen Brown */ 28412b92865eSJosh Triplett __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 28428209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 2843d7899447SLen Brown } 2844103a8feaSLen Brown 2845103a8feaSLen Brown /* 2846103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 2847103a8feaSLen Brown * this check is valid for both Intel and AMD 2848103a8feaSLen Brown */ 2849103a8feaSLen Brown 28502b92865eSJosh Triplett __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 28518209e054SThomas Renninger has_aperf = ecx & (1 << 0); 2852889facbeSLen Brown do_dts = eax & (1 << 0); 2853889facbeSLen Brown do_ptm = eax & (1 << 6); 2854*7f5c258eSLen Brown has_hwp = eax & (1 << 7); 2855*7f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 2856*7f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 2857*7f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 2858*7f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 2859889facbeSLen Brown has_epb = ecx & (1 << 3); 2860889facbeSLen Brown 2861d8af6f5fSLen Brown if (debug) 2862*7f5c258eSLen Brown fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, " 2863*7f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 2864*7f5c258eSLen Brown has_aperf ? "" : "No-", 2865*7f5c258eSLen Brown do_dts ? "" : "No-", 2866*7f5c258eSLen Brown do_ptm ? "" : "No-", 2867*7f5c258eSLen Brown has_hwp ? "" : "No-", 2868*7f5c258eSLen Brown has_hwp_notify ? "" : "No-", 2869*7f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 2870*7f5c258eSLen Brown has_hwp_epp ? "" : "No-", 2871*7f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 2872*7f5c258eSLen Brown has_epb ? "" : "No-"); 2873103a8feaSLen Brown 287469807a63SLen Brown if (debug) 287569807a63SLen Brown decode_misc_enable_msr(); 287669807a63SLen Brown 287761a87ba7SLen Brown if (max_level >= 0x15) { 28788a5bdf41SLen Brown unsigned int eax_crystal; 28798a5bdf41SLen Brown unsigned int ebx_tsc; 28808a5bdf41SLen Brown 28818a5bdf41SLen Brown /* 28828a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 28838a5bdf41SLen Brown */ 28848a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 28858a5bdf41SLen Brown __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx); 28868a5bdf41SLen Brown 28878a5bdf41SLen Brown if (ebx_tsc != 0) { 28888a5bdf41SLen Brown 28898a5bdf41SLen Brown if (debug && (ebx != 0)) 28908a5bdf41SLen Brown fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 28918a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 28928a5bdf41SLen Brown 28938a5bdf41SLen Brown if (crystal_hz == 0) 28948a5bdf41SLen Brown switch(model) { 28958a5bdf41SLen Brown case 0x4E: /* SKL */ 28968a5bdf41SLen Brown case 0x5E: /* SKL */ 28978a5bdf41SLen Brown crystal_hz = 24000000; /* 24 MHz */ 28988a5bdf41SLen Brown break; 28998a5bdf41SLen Brown default: 29008a5bdf41SLen Brown crystal_hz = 0; 29018a5bdf41SLen Brown } 29028a5bdf41SLen Brown 29038a5bdf41SLen Brown if (crystal_hz) { 29048a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 29058a5bdf41SLen Brown if (debug) 29068a5bdf41SLen Brown fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 29078a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 29088a5bdf41SLen Brown } 29098a5bdf41SLen Brown } 29108a5bdf41SLen Brown } 291161a87ba7SLen Brown if (max_level >= 0x16) { 291261a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 291361a87ba7SLen Brown 291461a87ba7SLen Brown /* 291561a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 291661a87ba7SLen Brown */ 291761a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 291861a87ba7SLen Brown 291961a87ba7SLen Brown __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx); 292061a87ba7SLen Brown if (debug) 292161a87ba7SLen Brown fprintf(stderr, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 292261a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 292361a87ba7SLen Brown } 29248a5bdf41SLen Brown 2925b2b34dfeSHubert Chrzaniuk if (has_aperf) 2926b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 2927b2b34dfeSHubert Chrzaniuk 2928ee7e38e3SLen Brown do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); 2929d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 2930ee7e38e3SLen Brown do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); 2931ee7e38e3SLen Brown do_pc3 = (pkg_cstate_limit >= PCL__3); 2932ee7e38e3SLen Brown do_pc6 = (pkg_cstate_limit >= PCL__6); 2933ee7e38e3SLen Brown do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); 2934d7899447SLen Brown do_c8_c9_c10 = has_hsw_msrs(family, model); 29350b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 2936144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 2937fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 2938103a8feaSLen Brown 2939889facbeSLen Brown rapl_probe(family, model); 29403a9a941dSLen Brown perf_limit_reasons_probe(family, model); 2941889facbeSLen Brown 2942fcd17211SLen Brown if (debug) 2943fcd17211SLen Brown dump_cstate_pstate_config_info(); 2944fcd17211SLen Brown 2945a2b7b749SLen Brown if (has_skl_msrs(family, model)) 2946a2b7b749SLen Brown calculate_tsc_tweak(); 2947a2b7b749SLen Brown 2948889facbeSLen Brown return; 2949103a8feaSLen Brown } 2950103a8feaSLen Brown 2951d8af6f5fSLen Brown void help() 2952103a8feaSLen Brown { 2953d8af6f5fSLen Brown fprintf(stderr, 2954d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 2955d8af6f5fSLen Brown "\n" 2956d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 2957d8af6f5fSLen Brown "when COMMAND completes.\n" 2958d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 2959d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 2960d8af6f5fSLen Brown "--debug run in \"debug\" mode\n" 2961d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 2962d8af6f5fSLen Brown "--help print this help message\n" 2963d8af6f5fSLen Brown "--counter msr print 32-bit counter at address \"msr\"\n" 2964d8af6f5fSLen Brown "--Counter msr print 64-bit Counter at address \"msr\"\n" 2965d8af6f5fSLen Brown "--msr msr print 32-bit value at address \"msr\"\n" 2966d8af6f5fSLen Brown "--MSR msr print 64-bit Value at address \"msr\"\n" 2967d8af6f5fSLen Brown "--version print version information\n" 2968d8af6f5fSLen Brown "\n" 2969d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 2970103a8feaSLen Brown } 2971103a8feaSLen Brown 2972103a8feaSLen Brown 2973103a8feaSLen Brown /* 2974103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 2975103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 2976103a8feaSLen Brown */ 2977103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 2978103a8feaSLen Brown { 2979103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 2980103a8feaSLen Brown return 1; 2981103a8feaSLen Brown else 2982103a8feaSLen Brown return 0; 2983103a8feaSLen Brown } 2984103a8feaSLen Brown 2985103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 2986103a8feaSLen Brown { 2987103a8feaSLen Brown return 0; 2988103a8feaSLen Brown } 2989103a8feaSLen Brown 2990c98d5d94SLen Brown void topology_probe() 2991c98d5d94SLen Brown { 2992c98d5d94SLen Brown int i; 2993c98d5d94SLen Brown int max_core_id = 0; 2994c98d5d94SLen Brown int max_package_id = 0; 2995c98d5d94SLen Brown int max_siblings = 0; 2996c98d5d94SLen Brown struct cpu_topology { 2997c98d5d94SLen Brown int core_id; 2998c98d5d94SLen Brown int physical_package_id; 2999c98d5d94SLen Brown } *cpus; 3000c98d5d94SLen Brown 3001c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 3002c98d5d94SLen Brown topo.num_cpus = 0; 3003c98d5d94SLen Brown topo.max_cpu_num = 0; 3004c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 3005c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 3006c98d5d94SLen Brown show_cpu = 1; 3007c98d5d94SLen Brown 3008d8af6f5fSLen Brown if (debug > 1) 3009c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 3010c98d5d94SLen Brown 3011c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 3012b2c95d90SJosh Triplett if (cpus == NULL) 3013b2c95d90SJosh Triplett err(1, "calloc cpus"); 3014c98d5d94SLen Brown 3015c98d5d94SLen Brown /* 3016c98d5d94SLen Brown * Allocate and initialize cpu_present_set 3017c98d5d94SLen Brown */ 3018c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3019b2c95d90SJosh Triplett if (cpu_present_set == NULL) 3020b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3021c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3022c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 3023c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 3024c98d5d94SLen Brown 3025c98d5d94SLen Brown /* 3026c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 3027c98d5d94SLen Brown */ 3028c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 3029b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 3030b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 3031c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 3032c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 3033c98d5d94SLen Brown 3034c98d5d94SLen Brown 3035c98d5d94SLen Brown /* 3036c98d5d94SLen Brown * For online cpus 3037c98d5d94SLen Brown * find max_core_id, max_package_id 3038c98d5d94SLen Brown */ 3039c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 3040c98d5d94SLen Brown int siblings; 3041c98d5d94SLen Brown 3042c98d5d94SLen Brown if (cpu_is_not_present(i)) { 3043d8af6f5fSLen Brown if (debug > 1) 3044c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 3045c98d5d94SLen Brown continue; 3046c98d5d94SLen Brown } 3047c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 3048c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 3049c98d5d94SLen Brown max_core_id = cpus[i].core_id; 3050c98d5d94SLen Brown 3051c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 3052c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 3053c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 3054c98d5d94SLen Brown 3055c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 3056c98d5d94SLen Brown if (siblings > max_siblings) 3057c98d5d94SLen Brown max_siblings = siblings; 3058d8af6f5fSLen Brown if (debug > 1) 3059c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 3060c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 3061c98d5d94SLen Brown } 3062c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 3063d8af6f5fSLen Brown if (debug > 1) 3064c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 3065c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 30661cc21f7bSLen Brown if (debug && !summary_only && topo.num_cores_per_pkg > 1) 3067c98d5d94SLen Brown show_core = 1; 3068c98d5d94SLen Brown 3069c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 3070d8af6f5fSLen Brown if (debug > 1) 3071c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 3072c98d5d94SLen Brown max_package_id, topo.num_packages); 30731cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 3074c98d5d94SLen Brown show_pkg = 1; 3075c98d5d94SLen Brown 3076c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 3077d8af6f5fSLen Brown if (debug > 1) 3078c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 3079c98d5d94SLen Brown 3080c98d5d94SLen Brown free(cpus); 3081c98d5d94SLen Brown } 3082c98d5d94SLen Brown 3083c98d5d94SLen Brown void 3084c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 3085c98d5d94SLen Brown { 3086c98d5d94SLen Brown int i; 3087c98d5d94SLen Brown 3088c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 3089c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 3090c98d5d94SLen Brown if (*t == NULL) 3091c98d5d94SLen Brown goto error; 3092c98d5d94SLen Brown 3093c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 3094c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 3095c98d5d94SLen Brown (*t)[i].cpu_id = -1; 3096c98d5d94SLen Brown 3097c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 3098c98d5d94SLen Brown sizeof(struct core_data)); 3099c98d5d94SLen Brown if (*c == NULL) 3100c98d5d94SLen Brown goto error; 3101c98d5d94SLen Brown 3102c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 3103c98d5d94SLen Brown (*c)[i].core_id = -1; 3104c98d5d94SLen Brown 3105c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 3106c98d5d94SLen Brown if (*p == NULL) 3107c98d5d94SLen Brown goto error; 3108c98d5d94SLen Brown 3109c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 3110c98d5d94SLen Brown (*p)[i].package_id = i; 3111c98d5d94SLen Brown 3112c98d5d94SLen Brown return; 3113c98d5d94SLen Brown error: 3114b2c95d90SJosh Triplett err(1, "calloc counters"); 3115c98d5d94SLen Brown } 3116c98d5d94SLen Brown /* 3117c98d5d94SLen Brown * init_counter() 3118c98d5d94SLen Brown * 3119c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 3120c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 3121c98d5d94SLen Brown * 3122c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 3123c98d5d94SLen Brown */ 3124c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 3125c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 3126c98d5d94SLen Brown int pkg_num, int cpu_id) 3127c98d5d94SLen Brown { 3128c98d5d94SLen Brown struct thread_data *t; 3129c98d5d94SLen Brown struct core_data *c; 3130c98d5d94SLen Brown struct pkg_data *p; 3131c98d5d94SLen Brown 3132c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 3133c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 3134c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 3135c98d5d94SLen Brown 3136c98d5d94SLen Brown t->cpu_id = cpu_id; 3137c98d5d94SLen Brown if (thread_num == 0) { 3138c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 3139c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 3140c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 3141c98d5d94SLen Brown } 3142c98d5d94SLen Brown 3143c98d5d94SLen Brown c->core_id = core_num; 3144c98d5d94SLen Brown p->package_id = pkg_num; 3145c98d5d94SLen Brown } 3146c98d5d94SLen Brown 3147c98d5d94SLen Brown 3148c98d5d94SLen Brown int initialize_counters(int cpu_id) 3149c98d5d94SLen Brown { 3150c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 3151c98d5d94SLen Brown 3152c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 3153c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 3154e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 3155e275b388SDasaratharaman Chandramouli if (!my_thread_id) 3156c98d5d94SLen Brown topo.num_cores++; 3157c98d5d94SLen Brown 3158c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3159c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 3160c98d5d94SLen Brown return 0; 3161c98d5d94SLen Brown } 3162c98d5d94SLen Brown 3163c98d5d94SLen Brown void allocate_output_buffer() 3164c98d5d94SLen Brown { 31653b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 3166c98d5d94SLen Brown outp = output_buffer; 3167b2c95d90SJosh Triplett if (outp == NULL) 3168b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 3169c98d5d94SLen Brown } 3170c98d5d94SLen Brown 3171c98d5d94SLen Brown void setup_all_buffers(void) 3172c98d5d94SLen Brown { 3173c98d5d94SLen Brown topology_probe(); 3174c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 3175c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 3176c98d5d94SLen Brown allocate_output_buffer(); 3177c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 3178c98d5d94SLen Brown } 31793b4d5c7fSAndy Shevchenko 31807ce7d5deSPrarit Bhargava void set_base_cpu(void) 31817ce7d5deSPrarit Bhargava { 31827ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 31837ce7d5deSPrarit Bhargava if (base_cpu < 0) 31847ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 31857ce7d5deSPrarit Bhargava 31867ce7d5deSPrarit Bhargava if (debug > 1) 31877ce7d5deSPrarit Bhargava fprintf(stderr, "base_cpu = %d\n", base_cpu); 31887ce7d5deSPrarit Bhargava } 31897ce7d5deSPrarit Bhargava 3190103a8feaSLen Brown void turbostat_init() 3191103a8feaSLen Brown { 31927ce7d5deSPrarit Bhargava setup_all_buffers(); 31937ce7d5deSPrarit Bhargava set_base_cpu(); 3194103a8feaSLen Brown check_dev_msr(); 319598481e79SLen Brown check_permissions(); 3196fcd17211SLen Brown process_cpuid(); 3197103a8feaSLen Brown 3198103a8feaSLen Brown 3199d8af6f5fSLen Brown if (debug) 3200*7f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 3201*7f5c258eSLen Brown 3202*7f5c258eSLen Brown if (debug) 3203889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 3204889facbeSLen Brown 3205d8af6f5fSLen Brown if (debug) 32063a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 32073a9a941dSLen Brown 3208d8af6f5fSLen Brown if (debug) 3209889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 3210889facbeSLen Brown 3211889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 3212889facbeSLen Brown 3213d8af6f5fSLen Brown if (debug) 3214889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 3215103a8feaSLen Brown } 3216103a8feaSLen Brown 3217103a8feaSLen Brown int fork_it(char **argv) 3218103a8feaSLen Brown { 3219103a8feaSLen Brown pid_t child_pid; 3220d91bb17cSLen Brown int status; 3221d15cf7c1SLen Brown 3222d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 3223d91bb17cSLen Brown if (status) 3224d91bb17cSLen Brown exit(status); 3225d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 3226d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 3227103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3228103a8feaSLen Brown 3229103a8feaSLen Brown child_pid = fork(); 3230103a8feaSLen Brown if (!child_pid) { 3231103a8feaSLen Brown /* child */ 3232103a8feaSLen Brown execvp(argv[0], argv); 3233103a8feaSLen Brown } else { 3234103a8feaSLen Brown 3235103a8feaSLen Brown /* parent */ 3236b2c95d90SJosh Triplett if (child_pid == -1) 3237b2c95d90SJosh Triplett err(1, "fork"); 3238103a8feaSLen Brown 3239103a8feaSLen Brown signal(SIGINT, SIG_IGN); 3240103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 3241b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 3242b2c95d90SJosh Triplett err(status, "waitpid"); 3243103a8feaSLen Brown } 3244c98d5d94SLen Brown /* 3245c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 3246c98d5d94SLen Brown * because re-starting is problematic when forking 3247c98d5d94SLen Brown */ 3248c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 3249103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3250103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3251c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 3252c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3253c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3254c98d5d94SLen Brown flush_stderr(); 3255103a8feaSLen Brown 32566eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 3257103a8feaSLen Brown 3258d91bb17cSLen Brown return status; 3259103a8feaSLen Brown } 3260103a8feaSLen Brown 32613b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 32623b4d5c7fSAndy Shevchenko { 32633b4d5c7fSAndy Shevchenko int status; 32643b4d5c7fSAndy Shevchenko 32653b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 32663b4d5c7fSAndy Shevchenko if (status) 32673b4d5c7fSAndy Shevchenko return status; 32683b4d5c7fSAndy Shevchenko 32693b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 32703b4d5c7fSAndy Shevchenko if (status) 32713b4d5c7fSAndy Shevchenko return status; 32723b4d5c7fSAndy Shevchenko 32733b4d5c7fSAndy Shevchenko flush_stdout(); 32743b4d5c7fSAndy Shevchenko 32753b4d5c7fSAndy Shevchenko return status; 32763b4d5c7fSAndy Shevchenko } 32773b4d5c7fSAndy Shevchenko 3278d8af6f5fSLen Brown void print_version() { 3279*7f5c258eSLen Brown fprintf(stderr, "turbostat version 4.10 10 Dec, 2015" 3280d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 3281d8af6f5fSLen Brown } 3282d8af6f5fSLen Brown 3283103a8feaSLen Brown void cmdline(int argc, char **argv) 3284103a8feaSLen Brown { 3285103a8feaSLen Brown int opt; 3286d8af6f5fSLen Brown int option_index = 0; 3287d8af6f5fSLen Brown static struct option long_options[] = { 3288d8af6f5fSLen Brown {"Counter", required_argument, 0, 'C'}, 3289d8af6f5fSLen Brown {"counter", required_argument, 0, 'c'}, 3290d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 3291d8af6f5fSLen Brown {"debug", no_argument, 0, 'd'}, 3292d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 3293d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 3294d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 3295d8af6f5fSLen Brown {"MSR", required_argument, 0, 'M'}, 3296d8af6f5fSLen Brown {"msr", required_argument, 0, 'm'}, 3297d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 3298d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 3299d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 3300d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 3301d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 3302d8af6f5fSLen Brown {0, 0, 0, 0 } 3303d8af6f5fSLen Brown }; 3304103a8feaSLen Brown 3305103a8feaSLen Brown progname = argv[0]; 3306103a8feaSLen Brown 3307a01e72fbSLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v", 3308d8af6f5fSLen Brown long_options, &option_index)) != -1) { 3309103a8feaSLen Brown switch (opt) { 3310d8af6f5fSLen Brown case 'C': 3311d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 3312103a8feaSLen Brown break; 3313f9240813SLen Brown case 'c': 33148e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 33158e180f3cSLen Brown break; 3316d8af6f5fSLen Brown case 'D': 3317d8af6f5fSLen Brown dump_only++; 33188e180f3cSLen Brown break; 3319d8af6f5fSLen Brown case 'd': 3320d8af6f5fSLen Brown debug++; 33212f32edf1SLen Brown break; 3322d8af6f5fSLen Brown case 'h': 3323d8af6f5fSLen Brown default: 3324d8af6f5fSLen Brown help(); 3325d8af6f5fSLen Brown exit(1); 3326d8af6f5fSLen Brown case 'i': 3327d8af6f5fSLen Brown interval_sec = atoi(optarg); 3328889facbeSLen Brown break; 33295c56be9aSDirk Brandewie case 'J': 33305c56be9aSDirk Brandewie rapl_joules++; 33315c56be9aSDirk Brandewie break; 3332d8af6f5fSLen Brown case 'M': 3333d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 3334d8af6f5fSLen Brown break; 3335d8af6f5fSLen Brown case 'm': 3336d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 3337d8af6f5fSLen Brown break; 3338d8af6f5fSLen Brown case 'P': 3339d8af6f5fSLen Brown show_pkg_only++; 3340d8af6f5fSLen Brown break; 3341d8af6f5fSLen Brown case 'p': 3342d8af6f5fSLen Brown show_core_only++; 3343d8af6f5fSLen Brown break; 3344d8af6f5fSLen Brown case 'S': 3345d8af6f5fSLen Brown summary_only++; 3346d8af6f5fSLen Brown break; 3347d8af6f5fSLen Brown case 'T': 3348d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 3349d8af6f5fSLen Brown break; 3350d8af6f5fSLen Brown case 'v': 3351d8af6f5fSLen Brown print_version(); 3352d8af6f5fSLen Brown exit(0); 3353d8af6f5fSLen Brown break; 3354103a8feaSLen Brown } 3355103a8feaSLen Brown } 3356103a8feaSLen Brown } 3357103a8feaSLen Brown 3358103a8feaSLen Brown int main(int argc, char **argv) 3359103a8feaSLen Brown { 3360103a8feaSLen Brown cmdline(argc, argv); 3361103a8feaSLen Brown 3362d8af6f5fSLen Brown if (debug) 3363d8af6f5fSLen Brown print_version(); 3364103a8feaSLen Brown 3365103a8feaSLen Brown turbostat_init(); 3366103a8feaSLen Brown 33673b4d5c7fSAndy Shevchenko /* dump counters and exit */ 33683b4d5c7fSAndy Shevchenko if (dump_only) 33693b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 33703b4d5c7fSAndy Shevchenko 3371103a8feaSLen Brown /* 3372103a8feaSLen Brown * if any params left, it must be a command to fork 3373103a8feaSLen Brown */ 3374103a8feaSLen Brown if (argc - optind) 3375103a8feaSLen Brown return fork_it(argv + optind); 3376103a8feaSLen Brown else 3377103a8feaSLen Brown turbostat_loop(); 3378103a8feaSLen Brown 3379103a8feaSLen Brown return 0; 3380103a8feaSLen Brown } 3381