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; 741ed51011SLen Brown int do_smi; 75103a8feaSLen Brown double bclk; 76103a8feaSLen Brown unsigned int show_pkg; 77103a8feaSLen Brown unsigned int show_core; 78103a8feaSLen Brown unsigned int show_cpu; 79c98d5d94SLen Brown unsigned int show_pkg_only; 80c98d5d94SLen Brown unsigned int show_core_only; 81c98d5d94SLen Brown char *output_buffer, *outp; 82889facbeSLen Brown unsigned int do_rapl; 83889facbeSLen Brown unsigned int do_dts; 84889facbeSLen Brown unsigned int do_ptm; 85889facbeSLen Brown unsigned int tcc_activation_temp; 86889facbeSLen Brown unsigned int tcc_activation_temp_override; 8740ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8840ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 89889facbeSLen Brown double rapl_joule_counter_range; 903a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 913a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 923a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 938a5bdf41SLen Brown unsigned int crystal_hz; 948a5bdf41SLen Brown unsigned long long tsc_hz; 957ce7d5deSPrarit Bhargava int base_cpu; 96889facbeSLen Brown 97889facbeSLen Brown #define RAPL_PKG (1 << 0) 98e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 99e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 100e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 101e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 102e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 103e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 104e6f9bb3cSLen Brown 105889facbeSLen Brown #define RAPL_DRAM (1 << 3) 106e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 107e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 108e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 109e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1100b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1110b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 112e6f9bb3cSLen Brown 1130b2bb692SLen Brown #define RAPL_CORES (1 << 6) 114e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 115e6f9bb3cSLen Brown /* 0x639 MSR_PP0_ENERGY_STATUS */ 1160b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 117e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 118e6f9bb3cSLen Brown 1190b2bb692SLen Brown #define RAPL_GFX (1 << 8) 120e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 121e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 122e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 123889facbeSLen Brown #define TJMAX_DEFAULT 100 124889facbeSLen Brown 125889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 126103a8feaSLen Brown 127103a8feaSLen Brown int aperf_mperf_unstable; 128103a8feaSLen Brown int backwards_count; 129103a8feaSLen Brown char *progname; 130103a8feaSLen Brown 131c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 132c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 133c98d5d94SLen Brown 134c98d5d94SLen Brown struct thread_data { 135c98d5d94SLen Brown unsigned long long tsc; 136c98d5d94SLen Brown unsigned long long aperf; 137c98d5d94SLen Brown unsigned long long mperf; 138144b44b1SLen Brown unsigned long long c1; 1392f32edf1SLen Brown unsigned long long extra_msr64; 1408e180f3cSLen Brown unsigned long long extra_delta64; 1418e180f3cSLen Brown unsigned long long extra_msr32; 1428e180f3cSLen Brown unsigned long long extra_delta32; 1431ed51011SLen Brown unsigned int smi_count; 144c98d5d94SLen Brown unsigned int cpu_id; 145c98d5d94SLen Brown unsigned int flags; 146c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 147c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 148c98d5d94SLen Brown } *thread_even, *thread_odd; 149c98d5d94SLen Brown 150c98d5d94SLen Brown struct core_data { 151c98d5d94SLen Brown unsigned long long c3; 152c98d5d94SLen Brown unsigned long long c6; 153c98d5d94SLen Brown unsigned long long c7; 154889facbeSLen Brown unsigned int core_temp_c; 155c98d5d94SLen Brown unsigned int core_id; 156c98d5d94SLen Brown } *core_even, *core_odd; 157c98d5d94SLen Brown 158c98d5d94SLen Brown struct pkg_data { 159c98d5d94SLen Brown unsigned long long pc2; 160c98d5d94SLen Brown unsigned long long pc3; 161c98d5d94SLen Brown unsigned long long pc6; 162c98d5d94SLen Brown unsigned long long pc7; 163ca58710fSKristen Carlson Accardi unsigned long long pc8; 164ca58710fSKristen Carlson Accardi unsigned long long pc9; 165ca58710fSKristen Carlson Accardi unsigned long long pc10; 1660b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1670b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1680b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1690b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 170c98d5d94SLen Brown unsigned int package_id; 171889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 172889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 173889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 174889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 175889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 176889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 177889facbeSLen Brown unsigned int pkg_temp_c; 178889facbeSLen Brown 179c98d5d94SLen Brown } *package_even, *package_odd; 180c98d5d94SLen Brown 181c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 182c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 183c98d5d94SLen Brown 184c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 185c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 186c98d5d94SLen Brown topo.num_threads_per_core + \ 187c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 188c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 189c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 190c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 191c98d5d94SLen Brown 192c98d5d94SLen Brown struct system_summary { 193c98d5d94SLen Brown struct thread_data threads; 194c98d5d94SLen Brown struct core_data cores; 195c98d5d94SLen Brown struct pkg_data packages; 196c98d5d94SLen Brown } sum, average; 197c98d5d94SLen Brown 198c98d5d94SLen Brown 199c98d5d94SLen Brown struct topo_params { 200c98d5d94SLen Brown int num_packages; 201103a8feaSLen Brown int num_cpus; 202c98d5d94SLen Brown int num_cores; 203c98d5d94SLen Brown int max_cpu_num; 204c98d5d94SLen Brown int num_cores_per_pkg; 205c98d5d94SLen Brown int num_threads_per_core; 206c98d5d94SLen Brown } topo; 207103a8feaSLen Brown 208c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 209103a8feaSLen Brown 210c98d5d94SLen Brown void setup_all_buffers(void); 211103a8feaSLen Brown 212c98d5d94SLen Brown int cpu_is_not_present(int cpu) 213d15cf7c1SLen Brown { 214c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 215c98d5d94SLen Brown } 216c98d5d94SLen Brown /* 217c98d5d94SLen Brown * run func(thread, core, package) in topology order 218c98d5d94SLen Brown * skip non-present cpus 219c98d5d94SLen Brown */ 220c98d5d94SLen Brown 221c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 222c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 223c98d5d94SLen Brown { 224c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 225c98d5d94SLen Brown 226c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 227c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 228c98d5d94SLen Brown for (thread_no = 0; thread_no < 229c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 230c98d5d94SLen Brown struct thread_data *t; 231c98d5d94SLen Brown struct core_data *c; 232c98d5d94SLen Brown struct pkg_data *p; 233c98d5d94SLen Brown 234c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 235c98d5d94SLen Brown 236c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 237c98d5d94SLen Brown continue; 238c98d5d94SLen Brown 239c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 240c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 241c98d5d94SLen Brown 242c98d5d94SLen Brown retval = func(t, c, p); 243c98d5d94SLen Brown if (retval) 244c98d5d94SLen Brown return retval; 245c98d5d94SLen Brown } 246c98d5d94SLen Brown } 247c98d5d94SLen Brown } 248d15cf7c1SLen Brown return 0; 249d15cf7c1SLen Brown } 250d15cf7c1SLen Brown 25188c3281fSLen Brown int cpu_migrate(int cpu) 25288c3281fSLen Brown { 253c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 254c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 255c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 25688c3281fSLen Brown return -1; 25788c3281fSLen Brown else 25888c3281fSLen Brown return 0; 25988c3281fSLen Brown } 26088c3281fSLen Brown 26115aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 262103a8feaSLen Brown { 263103a8feaSLen Brown ssize_t retval; 264103a8feaSLen Brown char pathname[32]; 265103a8feaSLen Brown int fd; 266103a8feaSLen Brown 267103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 268103a8feaSLen Brown fd = open(pathname, O_RDONLY); 26915aaa346SLen Brown if (fd < 0) 27098481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 271103a8feaSLen Brown 27215aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 273103a8feaSLen Brown close(fd); 27415aaa346SLen Brown 27598481e79SLen Brown if (retval != sizeof *msr) 27698481e79SLen Brown err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset); 27715aaa346SLen Brown 27815aaa346SLen Brown return 0; 279103a8feaSLen Brown } 280103a8feaSLen Brown 281fc04cc67SLen Brown /* 282fc04cc67SLen Brown * Example Format w/ field column widths: 283fc04cc67SLen Brown * 284fc04cc67SLen 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 285e7c95ff3SLen Brown * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 286fc04cc67SLen Brown */ 287fc04cc67SLen Brown 288a829eb4dSLen Brown void print_header(void) 289103a8feaSLen Brown { 290103a8feaSLen Brown if (show_pkg) 291fc04cc67SLen Brown outp += sprintf(outp, " Package"); 292103a8feaSLen Brown if (show_core) 293fc04cc67SLen Brown outp += sprintf(outp, " Core"); 294103a8feaSLen Brown if (show_cpu) 295c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 296103a8feaSLen Brown if (has_aperf) 297fc04cc67SLen Brown outp += sprintf(outp, " Avg_MHz"); 298d7899447SLen Brown if (has_aperf) 299fc04cc67SLen Brown outp += sprintf(outp, " %%Busy"); 300fc04cc67SLen Brown if (has_aperf) 301fc04cc67SLen Brown outp += sprintf(outp, " Bzy_MHz"); 302fc04cc67SLen Brown outp += sprintf(outp, " TSC_MHz"); 3031cc21f7bSLen Brown 3048e180f3cSLen Brown if (extra_delta_offset32) 305f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 3068e180f3cSLen Brown if (extra_delta_offset64) 307f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 3082f32edf1SLen Brown if (extra_msr_offset32) 3098e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 3102f32edf1SLen Brown if (extra_msr_offset64) 3118e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 3121cc21f7bSLen Brown 3131cc21f7bSLen Brown if (!debug) 3141cc21f7bSLen Brown goto done; 3151cc21f7bSLen Brown 3161cc21f7bSLen Brown if (do_smi) 3171cc21f7bSLen Brown outp += sprintf(outp, " SMI"); 3181cc21f7bSLen Brown 319103a8feaSLen Brown if (do_nhm_cstates) 320fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c1"); 321fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 322fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c3"); 323103a8feaSLen Brown if (do_nhm_cstates) 324fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c6"); 325103a8feaSLen Brown if (do_snb_cstates) 326fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c7"); 327889facbeSLen Brown 328889facbeSLen Brown if (do_dts) 329fc04cc67SLen Brown outp += sprintf(outp, " CoreTmp"); 330889facbeSLen Brown if (do_ptm) 331fc04cc67SLen Brown outp += sprintf(outp, " PkgTmp"); 332889facbeSLen Brown 3330b2bb692SLen Brown if (do_skl_residency) { 3340b2bb692SLen Brown outp += sprintf(outp, " Totl%%C0"); 3350b2bb692SLen Brown outp += sprintf(outp, " Any%%C0"); 3360b2bb692SLen Brown outp += sprintf(outp, " GFX%%C0"); 3370b2bb692SLen Brown outp += sprintf(outp, " CPUGFX%%"); 3380b2bb692SLen Brown } 3390b2bb692SLen Brown 340ee7e38e3SLen Brown if (do_pc2) 341fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc2"); 342ee7e38e3SLen Brown if (do_pc3) 343fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc3"); 344ee7e38e3SLen Brown if (do_pc6) 345fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc6"); 346ee7e38e3SLen Brown if (do_pc7) 347fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc7"); 348ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 349fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc8"); 350fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc9"); 351fc04cc67SLen Brown outp += sprintf(outp, " Pk%%pc10"); 352ca58710fSKristen Carlson Accardi } 353103a8feaSLen Brown 3545c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 355889facbeSLen Brown if (do_rapl & RAPL_PKG) 356fc04cc67SLen Brown outp += sprintf(outp, " PkgWatt"); 357889facbeSLen Brown if (do_rapl & RAPL_CORES) 358fc04cc67SLen Brown outp += sprintf(outp, " CorWatt"); 359889facbeSLen Brown if (do_rapl & RAPL_GFX) 360fc04cc67SLen Brown outp += sprintf(outp, " GFXWatt"); 361889facbeSLen Brown if (do_rapl & RAPL_DRAM) 362fc04cc67SLen Brown outp += sprintf(outp, " RAMWatt"); 363889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 364889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 365889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 366889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 367d7899447SLen Brown } else if (do_rapl && rapl_joules) { 3685c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 3695c56be9aSDirk Brandewie outp += sprintf(outp, " Pkg_J"); 3705c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 3715c56be9aSDirk Brandewie outp += sprintf(outp, " Cor_J"); 3725c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 3735c56be9aSDirk Brandewie outp += sprintf(outp, " GFX_J"); 3745c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 3755c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_W"); 3765c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 3775c56be9aSDirk Brandewie outp += sprintf(outp, " PKG_%%"); 3785c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 3795c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_%%"); 3805c56be9aSDirk Brandewie outp += sprintf(outp, " time"); 381889facbeSLen Brown 3825c56be9aSDirk Brandewie } 3831cc21f7bSLen Brown done: 384c98d5d94SLen Brown outp += sprintf(outp, "\n"); 385103a8feaSLen Brown } 386103a8feaSLen Brown 387c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 388c98d5d94SLen Brown struct pkg_data *p) 389103a8feaSLen Brown { 3903b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 391c98d5d94SLen Brown 392c98d5d94SLen Brown if (t) { 3933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 3943b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 3953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 3963b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 3973b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 3983b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 3993b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4008e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 4013b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4028e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 4033b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 4042f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 4053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 4062f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 4071ed51011SLen Brown if (do_smi) 4083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 409103a8feaSLen Brown } 410103a8feaSLen Brown 411c98d5d94SLen Brown if (c) { 4123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 4133b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 4143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 4153b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 4163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 417c98d5d94SLen Brown } 418103a8feaSLen Brown 419c98d5d94SLen Brown if (p) { 4203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 4210b2bb692SLen Brown 4220b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 4230b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 4240b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 4250b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 4260b2bb692SLen Brown 4273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 428ee7e38e3SLen Brown if (do_pc3) 4293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 430ee7e38e3SLen Brown if (do_pc6) 4313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 432ee7e38e3SLen Brown if (do_pc7) 4333b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 4343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 4353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 4363b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 4373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 4383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 4393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 4403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 4413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 4423b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 4433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 4443b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 4453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 446c98d5d94SLen Brown } 4473b4d5c7fSAndy Shevchenko 4483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 4493b4d5c7fSAndy Shevchenko 450c98d5d94SLen Brown return 0; 451103a8feaSLen Brown } 452103a8feaSLen Brown 453e23da037SLen Brown /* 454e23da037SLen Brown * column formatting convention & formats 455e23da037SLen Brown */ 456c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 457c98d5d94SLen Brown struct pkg_data *p) 458103a8feaSLen Brown { 459103a8feaSLen Brown double interval_float; 460fc04cc67SLen Brown char *fmt8; 461103a8feaSLen Brown 462c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 463c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 464c98d5d94SLen Brown return 0; 465c98d5d94SLen Brown 466c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 467c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 468c98d5d94SLen Brown return 0; 469c98d5d94SLen Brown 470103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 471103a8feaSLen Brown 472c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 473c98d5d94SLen Brown if (t == &average.threads) { 474103a8feaSLen Brown if (show_pkg) 475fc04cc67SLen Brown outp += sprintf(outp, " -"); 476103a8feaSLen Brown if (show_core) 477fc04cc67SLen Brown outp += sprintf(outp, " -"); 478103a8feaSLen Brown if (show_cpu) 479fc04cc67SLen Brown outp += sprintf(outp, " -"); 480103a8feaSLen Brown } else { 481c98d5d94SLen Brown if (show_pkg) { 482c98d5d94SLen Brown if (p) 483fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->package_id); 484c98d5d94SLen Brown else 485fc04cc67SLen Brown outp += sprintf(outp, " -"); 486c98d5d94SLen Brown } 487c98d5d94SLen Brown if (show_core) { 488c98d5d94SLen Brown if (c) 489fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_id); 490c98d5d94SLen Brown else 491fc04cc67SLen Brown outp += sprintf(outp, " -"); 492c98d5d94SLen Brown } 493103a8feaSLen Brown if (show_cpu) 494fc04cc67SLen Brown outp += sprintf(outp, "%8d", t->cpu_id); 495103a8feaSLen Brown } 496fc04cc67SLen Brown 497d7899447SLen Brown /* Avg_MHz */ 498fc04cc67SLen Brown if (has_aperf) 499fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 500fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 501fc04cc67SLen Brown 502d7899447SLen Brown /* %Busy */ 503d7899447SLen Brown if (has_aperf) { 504103a8feaSLen Brown if (!skip_c0) 505fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc); 506103a8feaSLen Brown else 507fc04cc67SLen Brown outp += sprintf(outp, "********"); 508103a8feaSLen Brown } 509103a8feaSLen Brown 510d7899447SLen Brown /* Bzy_MHz */ 511fc04cc67SLen Brown if (has_aperf) 512fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 513fc04cc67SLen Brown 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); 514103a8feaSLen Brown 515d7899447SLen Brown /* TSC_MHz */ 516fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float); 517103a8feaSLen Brown 5188e180f3cSLen Brown /* delta */ 5198e180f3cSLen Brown if (extra_delta_offset32) 5208e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 5218e180f3cSLen Brown 5228e180f3cSLen Brown /* DELTA */ 5238e180f3cSLen Brown if (extra_delta_offset64) 5248e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 5252f32edf1SLen Brown /* msr */ 5262f32edf1SLen Brown if (extra_msr_offset32) 5278e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 5282f32edf1SLen Brown 529130ff304SLen Brown /* MSR */ 5302f32edf1SLen Brown if (extra_msr_offset64) 5312f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 532130ff304SLen Brown 5331cc21f7bSLen Brown if (!debug) 5341cc21f7bSLen Brown goto done; 5351cc21f7bSLen Brown 5361cc21f7bSLen Brown /* SMI */ 5371cc21f7bSLen Brown if (do_smi) 5381cc21f7bSLen Brown outp += sprintf(outp, "%8d", t->smi_count); 5391cc21f7bSLen Brown 540103a8feaSLen Brown if (do_nhm_cstates) { 541103a8feaSLen Brown if (!skip_c1) 542fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc); 543103a8feaSLen Brown else 544fc04cc67SLen Brown outp += sprintf(outp, "********"); 545103a8feaSLen Brown } 546c98d5d94SLen Brown 547c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 548c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 549c98d5d94SLen Brown goto done; 550c98d5d94SLen Brown 551fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) 552fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc); 553103a8feaSLen Brown if (do_nhm_cstates) 554fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc); 555103a8feaSLen Brown if (do_snb_cstates) 556fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc); 557c98d5d94SLen Brown 558889facbeSLen Brown if (do_dts) 559fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_temp_c); 560889facbeSLen Brown 561c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 562c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 563c98d5d94SLen Brown goto done; 564c98d5d94SLen Brown 5650b2bb692SLen Brown /* PkgTmp */ 566889facbeSLen Brown if (do_ptm) 567fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->pkg_temp_c); 568889facbeSLen Brown 5690b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 5700b2bb692SLen Brown if (do_skl_residency) { 5710b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc); 5720b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc); 5730b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc); 5740b2bb692SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc); 5750b2bb692SLen Brown } 5760b2bb692SLen Brown 577ee7e38e3SLen Brown if (do_pc2) 578fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc); 579ee7e38e3SLen Brown if (do_pc3) 580fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc); 581ee7e38e3SLen Brown if (do_pc6) 582fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc); 583ee7e38e3SLen Brown if (do_pc7) 584fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc); 585ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 586fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc); 587fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc); 588fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc); 589ca58710fSKristen Carlson Accardi } 590889facbeSLen Brown 591889facbeSLen Brown /* 592889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 593889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 594889facbeSLen Brown */ 595fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 596fc04cc67SLen Brown fmt8 = "%8.2f"; 597fc04cc67SLen Brown else 598fc04cc67SLen Brown fmt8 = " %6.0f**"; 599889facbeSLen Brown 6005c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 601889facbeSLen Brown if (do_rapl & RAPL_PKG) 602fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 603889facbeSLen Brown if (do_rapl & RAPL_CORES) 604fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 605889facbeSLen Brown if (do_rapl & RAPL_GFX) 606fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 607889facbeSLen Brown if (do_rapl & RAPL_DRAM) 60840ee8e3bSAndrey Semin outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); 609889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 610fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 611889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 612fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 613d7899447SLen Brown } else if (do_rapl && rapl_joules) { 6145c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 615fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6165c56be9aSDirk Brandewie p->energy_pkg * rapl_energy_units); 6175c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 618fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6195c56be9aSDirk Brandewie p->energy_cores * rapl_energy_units); 6205c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 621fc04cc67SLen Brown outp += sprintf(outp, fmt8, 6225c56be9aSDirk Brandewie p->energy_gfx * rapl_energy_units); 6235c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 624fc04cc67SLen Brown outp += sprintf(outp, fmt8, 62540ee8e3bSAndrey Semin p->energy_dram * rapl_dram_energy_units); 6265c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 627fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 6285c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 629fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 630889facbeSLen Brown 631d7899447SLen Brown outp += sprintf(outp, fmt8, interval_float); 6325c56be9aSDirk Brandewie } 633c98d5d94SLen Brown done: 634c98d5d94SLen Brown outp += sprintf(outp, "\n"); 635c98d5d94SLen Brown 636c98d5d94SLen Brown return 0; 637103a8feaSLen Brown } 638103a8feaSLen Brown 639c98d5d94SLen Brown void flush_stdout() 640103a8feaSLen Brown { 641c98d5d94SLen Brown fputs(output_buffer, stdout); 642ddac0d68SLen Brown fflush(stdout); 643c98d5d94SLen Brown outp = output_buffer; 644c98d5d94SLen Brown } 645c98d5d94SLen Brown void flush_stderr() 646c98d5d94SLen Brown { 647c98d5d94SLen Brown fputs(output_buffer, stderr); 648c98d5d94SLen Brown outp = output_buffer; 649c98d5d94SLen Brown } 650c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 651c98d5d94SLen Brown { 652e23da037SLen Brown static int printed; 653103a8feaSLen Brown 654e23da037SLen Brown if (!printed || !summary_only) 655103a8feaSLen Brown print_header(); 656103a8feaSLen Brown 657c98d5d94SLen Brown if (topo.num_cpus > 1) 658c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 659c98d5d94SLen Brown &average.packages); 660103a8feaSLen Brown 661e23da037SLen Brown printed = 1; 662e23da037SLen Brown 663e23da037SLen Brown if (summary_only) 664e23da037SLen Brown return; 665e23da037SLen Brown 666c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 667103a8feaSLen Brown } 668103a8feaSLen Brown 669889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 670889facbeSLen Brown if (new > old) { \ 671889facbeSLen Brown old = new - old; \ 672889facbeSLen Brown } else { \ 673889facbeSLen Brown old = 0x100000000 + new - old; \ 674889facbeSLen Brown } 675889facbeSLen Brown 676c98d5d94SLen Brown void 677c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 678103a8feaSLen Brown { 6790b2bb692SLen Brown 6800b2bb692SLen Brown if (do_skl_residency) { 6810b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 6820b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 6830b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 6840b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 6850b2bb692SLen Brown } 686c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 687ee7e38e3SLen Brown if (do_pc3) 688c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 689ee7e38e3SLen Brown if (do_pc6) 690c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 691ee7e38e3SLen Brown if (do_pc7) 692c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 693ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 694ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 695ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 696889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 697889facbeSLen Brown 698889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 699889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 700889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 701889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 702889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 703889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 704103a8feaSLen Brown } 705103a8feaSLen Brown 706c98d5d94SLen Brown void 707c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 708c98d5d94SLen Brown { 709c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 710c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 711c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 712889facbeSLen Brown old->core_temp_c = new->core_temp_c; 713103a8feaSLen Brown } 714c98d5d94SLen Brown 715c3ae331dSLen Brown /* 716c3ae331dSLen Brown * old = new - old 717c3ae331dSLen Brown */ 718c98d5d94SLen Brown void 719c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 720c98d5d94SLen Brown struct core_data *core_delta) 721c98d5d94SLen Brown { 722c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 723c98d5d94SLen Brown 724103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 725b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 726b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 727b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 728b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 729103a8feaSLen Brown 730c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 731c98d5d94SLen Brown 732a729617cSLen Brown if (has_aperf) { 733c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 734c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 735c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 736c98d5d94SLen Brown } else { 737c98d5d94SLen Brown 738103a8feaSLen Brown if (!aperf_mperf_unstable) { 739103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 740103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 741103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 742103a8feaSLen Brown 743103a8feaSLen Brown aperf_mperf_unstable = 1; 744103a8feaSLen Brown } 745103a8feaSLen Brown /* 746103a8feaSLen Brown * mperf delta is likely a huge "positive" number 747103a8feaSLen Brown * can not use it for calculating c0 time 748103a8feaSLen Brown */ 749103a8feaSLen Brown skip_c0 = 1; 750103a8feaSLen Brown skip_c1 = 1; 751103a8feaSLen Brown } 752a729617cSLen Brown } 753103a8feaSLen Brown 754c98d5d94SLen Brown 755144b44b1SLen Brown if (use_c1_residency_msr) { 756144b44b1SLen Brown /* 757144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 758144b44b1SLen Brown * which should be more accurate than the derivation below. 759144b44b1SLen Brown */ 760144b44b1SLen Brown } else { 761103a8feaSLen Brown /* 762c3ae331dSLen Brown * As counter collection is not atomic, 763c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 764103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 765103a8feaSLen Brown */ 766c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 767c98d5d94SLen Brown old->c1 = 0; 768c98d5d94SLen Brown else { 769c98d5d94SLen Brown /* normal case, derive c1 */ 770c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 771c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 772c98d5d94SLen Brown } 773144b44b1SLen Brown } 774c3ae331dSLen Brown 775c98d5d94SLen Brown if (old->mperf == 0) { 776d8af6f5fSLen Brown if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 777c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 778c98d5d94SLen Brown } 779103a8feaSLen Brown 7808e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 7818e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 7828e180f3cSLen Brown 7838e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 7848e180f3cSLen Brown 785103a8feaSLen Brown /* 7868e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 787103a8feaSLen Brown */ 7882f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 7892f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 7901ed51011SLen Brown 7911ed51011SLen Brown if (do_smi) 7921ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 793103a8feaSLen Brown } 794c98d5d94SLen Brown 795c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 796c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 797c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 798c98d5d94SLen Brown { 799c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 800c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 801c98d5d94SLen Brown delta_core(c, c2); 802c98d5d94SLen Brown 803c98d5d94SLen Brown /* always calculate thread delta */ 804c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 805c98d5d94SLen Brown 806c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 807c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 808c98d5d94SLen Brown delta_package(p, p2); 809c98d5d94SLen Brown 810103a8feaSLen Brown return 0; 811103a8feaSLen Brown } 812103a8feaSLen Brown 813c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 814103a8feaSLen Brown { 815c98d5d94SLen Brown t->tsc = 0; 816c98d5d94SLen Brown t->aperf = 0; 817c98d5d94SLen Brown t->mperf = 0; 818c98d5d94SLen Brown t->c1 = 0; 819103a8feaSLen Brown 8201ed51011SLen Brown t->smi_count = 0; 8218e180f3cSLen Brown t->extra_delta32 = 0; 8228e180f3cSLen Brown t->extra_delta64 = 0; 8238e180f3cSLen Brown 824c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 825c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 826c98d5d94SLen Brown 827c98d5d94SLen Brown c->c3 = 0; 828c98d5d94SLen Brown c->c6 = 0; 829c98d5d94SLen Brown c->c7 = 0; 830889facbeSLen Brown c->core_temp_c = 0; 831c98d5d94SLen Brown 8320b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 8330b2bb692SLen Brown p->pkg_any_core_c0 = 0; 8340b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 8350b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 8360b2bb692SLen Brown 837c98d5d94SLen Brown p->pc2 = 0; 838ee7e38e3SLen Brown if (do_pc3) 839c98d5d94SLen Brown p->pc3 = 0; 840ee7e38e3SLen Brown if (do_pc6) 841c98d5d94SLen Brown p->pc6 = 0; 842ee7e38e3SLen Brown if (do_pc7) 843c98d5d94SLen Brown p->pc7 = 0; 844ca58710fSKristen Carlson Accardi p->pc8 = 0; 845ca58710fSKristen Carlson Accardi p->pc9 = 0; 846ca58710fSKristen Carlson Accardi p->pc10 = 0; 847889facbeSLen Brown 848889facbeSLen Brown p->energy_pkg = 0; 849889facbeSLen Brown p->energy_dram = 0; 850889facbeSLen Brown p->energy_cores = 0; 851889facbeSLen Brown p->energy_gfx = 0; 852889facbeSLen Brown p->rapl_pkg_perf_status = 0; 853889facbeSLen Brown p->rapl_dram_perf_status = 0; 854889facbeSLen Brown p->pkg_temp_c = 0; 855103a8feaSLen Brown } 856c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 857c98d5d94SLen Brown struct pkg_data *p) 858103a8feaSLen Brown { 859c98d5d94SLen Brown average.threads.tsc += t->tsc; 860c98d5d94SLen Brown average.threads.aperf += t->aperf; 861c98d5d94SLen Brown average.threads.mperf += t->mperf; 862c98d5d94SLen Brown average.threads.c1 += t->c1; 86315aaa346SLen Brown 8648e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 8658e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 8668e180f3cSLen Brown 867c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 868c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 869c98d5d94SLen Brown return 0; 870c98d5d94SLen Brown 871c98d5d94SLen Brown average.cores.c3 += c->c3; 872c98d5d94SLen Brown average.cores.c6 += c->c6; 873c98d5d94SLen Brown average.cores.c7 += c->c7; 874c98d5d94SLen Brown 875889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 876889facbeSLen Brown 877c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 878c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 879c98d5d94SLen Brown return 0; 880c98d5d94SLen Brown 8810b2bb692SLen Brown if (do_skl_residency) { 8820b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 8830b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 8840b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 8850b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 8860b2bb692SLen Brown } 8870b2bb692SLen Brown 888c98d5d94SLen Brown average.packages.pc2 += p->pc2; 889ee7e38e3SLen Brown if (do_pc3) 890c98d5d94SLen Brown average.packages.pc3 += p->pc3; 891ee7e38e3SLen Brown if (do_pc6) 892c98d5d94SLen Brown average.packages.pc6 += p->pc6; 893ee7e38e3SLen Brown if (do_pc7) 894c98d5d94SLen Brown average.packages.pc7 += p->pc7; 895ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 896ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 897ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 898c98d5d94SLen Brown 899889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 900889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 901889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 902889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 903889facbeSLen Brown 904889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 905889facbeSLen Brown 906889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 907889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 908c98d5d94SLen Brown return 0; 909c98d5d94SLen Brown } 910c98d5d94SLen Brown /* 911c98d5d94SLen Brown * sum the counters for all cpus in the system 912c98d5d94SLen Brown * compute the weighted average 913c98d5d94SLen Brown */ 914c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 915c98d5d94SLen Brown struct pkg_data *p) 916c98d5d94SLen Brown { 917c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 918c98d5d94SLen Brown 919c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 920c98d5d94SLen Brown 921c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 922c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 923c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 924c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 925c98d5d94SLen Brown 9268e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 9278e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 9288e180f3cSLen Brown 9298e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 9308e180f3cSLen Brown 931c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 932c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 933c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 934c98d5d94SLen Brown 9350b2bb692SLen Brown if (do_skl_residency) { 9360b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 9370b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 9380b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 9390b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 9400b2bb692SLen Brown } 9410b2bb692SLen Brown 942c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 943ee7e38e3SLen Brown if (do_pc3) 944c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 945ee7e38e3SLen Brown if (do_pc6) 946c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 947ee7e38e3SLen Brown if (do_pc7) 948c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 949ca58710fSKristen Carlson Accardi 950ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 951ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 952ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 953c98d5d94SLen Brown } 954c98d5d94SLen Brown 955c98d5d94SLen Brown static unsigned long long rdtsc(void) 956c98d5d94SLen Brown { 957c98d5d94SLen Brown unsigned int low, high; 958c98d5d94SLen Brown 959c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 960c98d5d94SLen Brown 961c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 962c98d5d94SLen Brown } 963c98d5d94SLen Brown 964c98d5d94SLen Brown 965c98d5d94SLen Brown /* 966c98d5d94SLen Brown * get_counters(...) 967c98d5d94SLen Brown * migrate to cpu 968c98d5d94SLen Brown * acquire and record local counters for that cpu 969c98d5d94SLen Brown */ 970c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 971c98d5d94SLen Brown { 972c98d5d94SLen Brown int cpu = t->cpu_id; 973889facbeSLen Brown unsigned long long msr; 974c98d5d94SLen Brown 975e52966c0SLen Brown if (cpu_migrate(cpu)) { 976e52966c0SLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 97715aaa346SLen Brown return -1; 978e52966c0SLen Brown } 97915aaa346SLen Brown 980c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 98115aaa346SLen Brown 98215aaa346SLen Brown if (has_aperf) { 9839c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 984c98d5d94SLen Brown return -3; 9859c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 986c98d5d94SLen Brown return -4; 98788c3281fSLen Brown } 98888c3281fSLen Brown 9891ed51011SLen Brown if (do_smi) { 9901ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 9911ed51011SLen Brown return -5; 9921ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 9931ed51011SLen Brown } 9948e180f3cSLen Brown if (extra_delta_offset32) { 995889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 9962f32edf1SLen Brown return -5; 997889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 9988e180f3cSLen Brown } 9998e180f3cSLen Brown 10008e180f3cSLen Brown if (extra_delta_offset64) 10018e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 10028e180f3cSLen Brown return -5; 10038e180f3cSLen Brown 10048e180f3cSLen Brown if (extra_msr_offset32) { 1005889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 10068e180f3cSLen Brown return -5; 1007889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 10088e180f3cSLen Brown } 10092f32edf1SLen Brown 10102f32edf1SLen Brown if (extra_msr_offset64) 10112f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 1012c98d5d94SLen Brown return -5; 1013c98d5d94SLen Brown 1014144b44b1SLen Brown if (use_c1_residency_msr) { 1015144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1016144b44b1SLen Brown return -6; 1017144b44b1SLen Brown } 1018144b44b1SLen Brown 1019c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1020c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1021c98d5d94SLen Brown return 0; 1022c98d5d94SLen Brown 1023fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) { 1024c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1025c98d5d94SLen Brown return -6; 1026144b44b1SLen Brown } 1027144b44b1SLen Brown 1028fb5d4327SDasaratharaman Chandramouli if (do_nhm_cstates && !do_knl_cstates) { 1029c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1030c98d5d94SLen Brown return -7; 1031fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1032fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1033fb5d4327SDasaratharaman Chandramouli return -7; 1034103a8feaSLen Brown } 103515aaa346SLen Brown 103615aaa346SLen Brown if (do_snb_cstates) 1037c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1038c98d5d94SLen Brown return -8; 1039c98d5d94SLen Brown 1040889facbeSLen Brown if (do_dts) { 1041889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1042889facbeSLen Brown return -9; 1043889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1044889facbeSLen Brown } 1045889facbeSLen Brown 1046889facbeSLen Brown 1047c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1048c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1049c98d5d94SLen Brown return 0; 105015aaa346SLen Brown 10510b2bb692SLen Brown if (do_skl_residency) { 10520b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 10530b2bb692SLen Brown return -10; 10540b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 10550b2bb692SLen Brown return -11; 10560b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 10570b2bb692SLen Brown return -12; 10580b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 10590b2bb692SLen Brown return -13; 10600b2bb692SLen Brown } 1061ee7e38e3SLen Brown if (do_pc3) 1062c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1063c98d5d94SLen Brown return -9; 1064ee7e38e3SLen Brown if (do_pc6) 1065c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1066c98d5d94SLen Brown return -10; 1067ee7e38e3SLen Brown if (do_pc2) 1068c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1069c98d5d94SLen Brown return -11; 1070ee7e38e3SLen Brown if (do_pc7) 1071c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1072c98d5d94SLen Brown return -12; 1073ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 1074ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1075ca58710fSKristen Carlson Accardi return -13; 1076ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1077ca58710fSKristen Carlson Accardi return -13; 1078ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1079ca58710fSKristen Carlson Accardi return -13; 1080ca58710fSKristen Carlson Accardi } 1081889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1082889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1083889facbeSLen Brown return -13; 1084889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1085889facbeSLen Brown } 1086889facbeSLen Brown if (do_rapl & RAPL_CORES) { 1087889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1088889facbeSLen Brown return -14; 1089889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1090889facbeSLen Brown } 1091889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1092889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1093889facbeSLen Brown return -15; 1094889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1095889facbeSLen Brown } 1096889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1097889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1098889facbeSLen Brown return -16; 1099889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1100889facbeSLen Brown } 1101889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1102889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1103889facbeSLen Brown return -16; 1104889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1105889facbeSLen Brown } 1106889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1107889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1108889facbeSLen Brown return -16; 1109889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1110889facbeSLen Brown } 1111889facbeSLen Brown if (do_ptm) { 1112889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1113889facbeSLen Brown return -17; 1114889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1115889facbeSLen Brown } 111615aaa346SLen Brown return 0; 1117103a8feaSLen Brown } 1118103a8feaSLen Brown 1119ee7e38e3SLen Brown /* 1120ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1121ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1122ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1123ee7e38e3SLen Brown */ 1124ee7e38e3SLen Brown 1125ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1126ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1127ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1128ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1129ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1130ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1131ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1132ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1133ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1134ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1135ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1136ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 11370b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 11380b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 11390b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1140ee7e38e3SLen Brown 1141ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1142ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 11430b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1144ee7e38e3SLen Brown 1145e9257f5fSLen 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}; 1146e9257f5fSLen 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}; 1147e9257f5fSLen 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}; 1148e9257f5fSLen 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}; 1149e9257f5fSLen 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}; 1150e9257f5fSLen 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}; 1151ee7e38e3SLen Brown 1152fcd17211SLen Brown static void 1153fcd17211SLen Brown dump_nhm_platform_info(void) 1154103a8feaSLen Brown { 1155103a8feaSLen Brown unsigned long long msr; 1156103a8feaSLen Brown unsigned int ratio; 1157103a8feaSLen Brown 11587ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_PLATFORM_INFO, &msr); 1159103a8feaSLen Brown 1160*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 11616574a5d5SLen Brown 1162103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 11638f61f359SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n", 1164103a8feaSLen Brown ratio, bclk, ratio * bclk); 1165103a8feaSLen Brown 1166103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 11678f61f359SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n", 1168103a8feaSLen Brown ratio, bclk, ratio * bclk); 1169103a8feaSLen Brown 11707ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1171*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1172*bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 117367920418SLen Brown 1174fcd17211SLen Brown return; 1175fcd17211SLen Brown } 1176fcd17211SLen Brown 1177fcd17211SLen Brown static void 1178fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1179fcd17211SLen Brown { 1180fcd17211SLen Brown unsigned long long msr; 1181fcd17211SLen Brown unsigned int ratio; 1182fcd17211SLen Brown 11837ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1184fcd17211SLen Brown 1185*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1186fcd17211SLen Brown 1187fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1188fcd17211SLen Brown if (ratio) 1189fcd17211SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n", 1190fcd17211SLen Brown ratio, bclk, ratio * bclk); 1191fcd17211SLen Brown 1192fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1193fcd17211SLen Brown if (ratio) 1194fcd17211SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n", 1195fcd17211SLen Brown ratio, bclk, ratio * bclk); 1196fcd17211SLen Brown return; 1197fcd17211SLen Brown } 1198fcd17211SLen Brown 1199fcd17211SLen Brown static void 1200fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1201fcd17211SLen Brown { 1202fcd17211SLen Brown unsigned long long msr; 1203fcd17211SLen Brown unsigned int ratio; 12046574a5d5SLen Brown 12057ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 12066574a5d5SLen Brown 1207*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 12086574a5d5SLen Brown 12096574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 12106574a5d5SLen Brown if (ratio) 12116574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 12126574a5d5SLen Brown ratio, bclk, ratio * bclk); 12136574a5d5SLen Brown 12146574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 12156574a5d5SLen Brown if (ratio) 12166574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 12176574a5d5SLen Brown ratio, bclk, ratio * bclk); 12186574a5d5SLen Brown 12196574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 12206574a5d5SLen Brown if (ratio) 12216574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 12226574a5d5SLen Brown ratio, bclk, ratio * bclk); 12236574a5d5SLen Brown 12246574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 12256574a5d5SLen Brown if (ratio) 12266574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 12276574a5d5SLen Brown ratio, bclk, ratio * bclk); 12286574a5d5SLen Brown 12296574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 12306574a5d5SLen Brown if (ratio) 12316574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 12326574a5d5SLen Brown ratio, bclk, ratio * bclk); 12336574a5d5SLen Brown 12346574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 12356574a5d5SLen Brown if (ratio) 12366574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 12376574a5d5SLen Brown ratio, bclk, ratio * bclk); 12386574a5d5SLen Brown 12396574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 12406574a5d5SLen Brown if (ratio) 12416574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 12426574a5d5SLen Brown ratio, bclk, ratio * bclk); 12436574a5d5SLen Brown 12446574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 12456574a5d5SLen Brown if (ratio) 12466574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 12476574a5d5SLen Brown ratio, bclk, ratio * bclk); 1248103a8feaSLen Brown return; 1249fcd17211SLen Brown } 1250fcd17211SLen Brown 1251fcd17211SLen Brown static void 1252fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void) 1253fcd17211SLen Brown { 1254fcd17211SLen Brown unsigned long long msr; 1255fcd17211SLen Brown unsigned int ratio; 1256103a8feaSLen Brown 12577ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1258103a8feaSLen Brown 1259*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 12606574a5d5SLen Brown 12616574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 12626574a5d5SLen Brown if (ratio) 12636574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 12646574a5d5SLen Brown ratio, bclk, ratio * bclk); 12656574a5d5SLen Brown 12666574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 12676574a5d5SLen Brown if (ratio) 12686574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 12696574a5d5SLen Brown ratio, bclk, ratio * bclk); 12706574a5d5SLen Brown 12716574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 12726574a5d5SLen Brown if (ratio) 12736574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 12746574a5d5SLen Brown ratio, bclk, ratio * bclk); 12756574a5d5SLen Brown 12766574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 12776574a5d5SLen Brown if (ratio) 12786574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 12796574a5d5SLen Brown ratio, bclk, ratio * bclk); 12806574a5d5SLen Brown 1281103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1282103a8feaSLen Brown if (ratio) 1283103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1284103a8feaSLen Brown ratio, bclk, ratio * bclk); 1285103a8feaSLen Brown 1286103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1287103a8feaSLen Brown if (ratio) 1288103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1289103a8feaSLen Brown ratio, bclk, ratio * bclk); 1290103a8feaSLen Brown 1291103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1292103a8feaSLen Brown if (ratio) 1293103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1294103a8feaSLen Brown ratio, bclk, ratio * bclk); 1295103a8feaSLen Brown 1296103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1297103a8feaSLen Brown if (ratio) 1298103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1299103a8feaSLen Brown ratio, bclk, ratio * bclk); 1300fcd17211SLen Brown return; 1301fcd17211SLen Brown } 13023a9a941dSLen Brown 1303fcd17211SLen Brown static void 1304fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1305fb5d4327SDasaratharaman Chandramouli { 1306fb5d4327SDasaratharaman Chandramouli int cores; 1307fb5d4327SDasaratharaman Chandramouli unsigned int ratio; 1308fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1309fb5d4327SDasaratharaman Chandramouli int delta_cores; 1310fb5d4327SDasaratharaman Chandramouli int delta_ratio; 1311fb5d4327SDasaratharaman Chandramouli int i; 1312fb5d4327SDasaratharaman Chandramouli 13137ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1314fb5d4327SDasaratharaman Chandramouli 1315*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", 1316*bfae2052SLen Brown base_cpu, msr); 1317fb5d4327SDasaratharaman Chandramouli 1318fb5d4327SDasaratharaman Chandramouli /** 1319fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1320fb5d4327SDasaratharaman Chandramouli * [7:0] -- Base value of number of active cores of bucket 1. 1321fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1322fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1323fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1324fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1325fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1326fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1327fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1328fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1329fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1330fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1331fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1332fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1333fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1334fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1335fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1336fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 1337fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 1338fb5d4327SDasaratharaman Chandramouli */ 1339fb5d4327SDasaratharaman Chandramouli cores = msr & 0xFF; 1340fb5d4327SDasaratharaman Chandramouli ratio = (msr >> 8) && 0xFF; 1341fb5d4327SDasaratharaman Chandramouli if (ratio > 0) 1342fb5d4327SDasaratharaman Chandramouli fprintf(stderr, 1343fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1344fb5d4327SDasaratharaman Chandramouli ratio, bclk, ratio * bclk, cores); 1345fb5d4327SDasaratharaman Chandramouli 1346fb5d4327SDasaratharaman Chandramouli for (i = 16; i < 64; i = i + 8) { 1347fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 1348fb5d4327SDasaratharaman Chandramouli delta_ratio = (msr >> (i + 5)) && 0x7; 1349fb5d4327SDasaratharaman Chandramouli if (!delta_cores || !delta_ratio) 1350fb5d4327SDasaratharaman Chandramouli return; 1351fb5d4327SDasaratharaman Chandramouli cores = cores + delta_cores; 1352fb5d4327SDasaratharaman Chandramouli ratio = ratio - delta_ratio; 1353fb5d4327SDasaratharaman Chandramouli 1354fb5d4327SDasaratharaman Chandramouli /** -ve ratios will make successive ratio calculations 1355fb5d4327SDasaratharaman Chandramouli * negative. Hence return instead of carrying on. 1356fb5d4327SDasaratharaman Chandramouli */ 1357fb5d4327SDasaratharaman Chandramouli if (ratio > 0) 1358fb5d4327SDasaratharaman Chandramouli fprintf(stderr, 1359fb5d4327SDasaratharaman Chandramouli "%d * %.0f = %.0f MHz max turbo %d active cores\n", 1360fb5d4327SDasaratharaman Chandramouli ratio, bclk, ratio * bclk, cores); 1361fb5d4327SDasaratharaman Chandramouli } 1362fb5d4327SDasaratharaman Chandramouli } 1363fb5d4327SDasaratharaman Chandramouli 1364fb5d4327SDasaratharaman Chandramouli static void 1365fcd17211SLen Brown dump_nhm_cst_cfg(void) 1366fcd17211SLen Brown { 1367fcd17211SLen Brown unsigned long long msr; 1368fcd17211SLen Brown 13697ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1370fcd17211SLen Brown 1371fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1372fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1373fcd17211SLen Brown 1374*bfae2052SLen Brown fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr); 1375fcd17211SLen Brown 1376fcd17211SLen Brown fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1377fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1378fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1379fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1380fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1381fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 1382fcd17211SLen Brown (unsigned int)msr & 7, 1383fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1384fcd17211SLen Brown return; 1385103a8feaSLen Brown } 1386103a8feaSLen Brown 1387c98d5d94SLen Brown void free_all_buffers(void) 1388103a8feaSLen Brown { 1389c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1390c98d5d94SLen Brown cpu_present_set = NULL; 1391c98d5d94SLen Brown cpu_present_set = 0; 1392103a8feaSLen Brown 1393c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1394c98d5d94SLen Brown cpu_affinity_set = NULL; 1395c98d5d94SLen Brown cpu_affinity_setsize = 0; 1396103a8feaSLen Brown 1397c98d5d94SLen Brown free(thread_even); 1398c98d5d94SLen Brown free(core_even); 1399c98d5d94SLen Brown free(package_even); 1400103a8feaSLen Brown 1401c98d5d94SLen Brown thread_even = NULL; 1402c98d5d94SLen Brown core_even = NULL; 1403c98d5d94SLen Brown package_even = NULL; 1404103a8feaSLen Brown 1405c98d5d94SLen Brown free(thread_odd); 1406c98d5d94SLen Brown free(core_odd); 1407c98d5d94SLen Brown free(package_odd); 1408103a8feaSLen Brown 1409c98d5d94SLen Brown thread_odd = NULL; 1410c98d5d94SLen Brown core_odd = NULL; 1411c98d5d94SLen Brown package_odd = NULL; 1412103a8feaSLen Brown 1413c98d5d94SLen Brown free(output_buffer); 1414c98d5d94SLen Brown output_buffer = NULL; 1415c98d5d94SLen Brown outp = NULL; 1416103a8feaSLen Brown } 1417103a8feaSLen Brown 1418103a8feaSLen Brown /* 141957a42a34SJosh Triplett * Open a file, and exit on failure 142057a42a34SJosh Triplett */ 142157a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 142257a42a34SJosh Triplett { 142357a42a34SJosh Triplett FILE *filep = fopen(path, "r"); 1424b2c95d90SJosh Triplett if (!filep) 1425b2c95d90SJosh Triplett err(1, "%s: open failed", path); 142657a42a34SJosh Triplett return filep; 142757a42a34SJosh Triplett } 142857a42a34SJosh Triplett 142957a42a34SJosh Triplett /* 143095aebc44SJosh Triplett * Parse a file containing a single int. 143195aebc44SJosh Triplett */ 143295aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 143395aebc44SJosh Triplett { 143495aebc44SJosh Triplett va_list args; 143595aebc44SJosh Triplett char path[PATH_MAX]; 143695aebc44SJosh Triplett FILE *filep; 143795aebc44SJosh Triplett int value; 143895aebc44SJosh Triplett 143995aebc44SJosh Triplett va_start(args, fmt); 144095aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 144195aebc44SJosh Triplett va_end(args); 144257a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1443b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1444b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 144595aebc44SJosh Triplett fclose(filep); 144695aebc44SJosh Triplett return value; 144795aebc44SJosh Triplett } 144895aebc44SJosh Triplett 144995aebc44SJosh Triplett /* 1450e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 1451e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 1452e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 1453103a8feaSLen Brown */ 1454e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 1455103a8feaSLen Brown { 1456e275b388SDasaratharaman Chandramouli char path[64]; 1457e275b388SDasaratharaman Chandramouli FILE *filep; 1458e275b388SDasaratharaman Chandramouli int this_cpu; 1459e275b388SDasaratharaman Chandramouli char character; 1460e275b388SDasaratharaman Chandramouli int i; 1461e275b388SDasaratharaman Chandramouli 1462e275b388SDasaratharaman Chandramouli sprintf(path, 1463e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 1464e275b388SDasaratharaman Chandramouli cpu); 1465e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 1466e275b388SDasaratharaman Chandramouli if (filep == NULL) { 1467e275b388SDasaratharaman Chandramouli perror(path); 1468e275b388SDasaratharaman Chandramouli exit(1); 1469e275b388SDasaratharaman Chandramouli } 1470e275b388SDasaratharaman Chandramouli 1471e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 1472e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 1473e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 1474e275b388SDasaratharaman Chandramouli fclose(filep); 1475e275b388SDasaratharaman Chandramouli return i; 1476e275b388SDasaratharaman Chandramouli } 1477e275b388SDasaratharaman Chandramouli 1478e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 1479e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 1480e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 1481e275b388SDasaratharaman Chandramouli } 1482e275b388SDasaratharaman Chandramouli 1483e275b388SDasaratharaman Chandramouli fclose(filep); 1484e275b388SDasaratharaman Chandramouli return -1; 1485c98d5d94SLen Brown } 1486103a8feaSLen Brown 1487c98d5d94SLen Brown /* 1488c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1489c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1490c98d5d94SLen Brown */ 1491c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1492c98d5d94SLen Brown { 149395aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1494103a8feaSLen Brown } 1495103a8feaSLen Brown 1496103a8feaSLen Brown int get_physical_package_id(int cpu) 1497103a8feaSLen Brown { 149895aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1499103a8feaSLen Brown } 1500103a8feaSLen Brown 1501103a8feaSLen Brown int get_core_id(int cpu) 1502103a8feaSLen Brown { 150395aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1504103a8feaSLen Brown } 1505103a8feaSLen Brown 1506c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1507c98d5d94SLen Brown { 1508c98d5d94SLen Brown char path[80]; 1509c98d5d94SLen Brown FILE *filep; 1510e275b388SDasaratharaman Chandramouli int sib1; 1511e275b388SDasaratharaman Chandramouli int matches = 0; 1512c98d5d94SLen Brown char character; 1513e275b388SDasaratharaman Chandramouli char str[100]; 1514e275b388SDasaratharaman Chandramouli char *ch; 1515c98d5d94SLen Brown 1516c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 151757a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1518e275b388SDasaratharaman Chandramouli 1519103a8feaSLen Brown /* 1520c98d5d94SLen Brown * file format: 1521e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 1522e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 1523c98d5d94SLen Brown */ 1524e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 1525e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 1526e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 1527e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 1528e275b388SDasaratharaman Chandramouli while (ch != NULL) { 1529e275b388SDasaratharaman Chandramouli matches++; 1530e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 1531e275b388SDasaratharaman Chandramouli } 1532c98d5d94SLen Brown 1533c98d5d94SLen Brown fclose(filep); 1534e275b388SDasaratharaman Chandramouli return matches+1; 1535c98d5d94SLen Brown } 1536c98d5d94SLen Brown 1537c98d5d94SLen Brown /* 1538c98d5d94SLen Brown * run func(thread, core, package) in topology order 1539c98d5d94SLen Brown * skip non-present cpus 1540103a8feaSLen Brown */ 1541103a8feaSLen Brown 1542c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1543c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1544c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1545c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1546c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1547c98d5d94SLen Brown struct pkg_data *pkg_base2) 1548c98d5d94SLen Brown { 1549c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1550c98d5d94SLen Brown 1551c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1552c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1553c98d5d94SLen Brown for (thread_no = 0; thread_no < 1554c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1555c98d5d94SLen Brown struct thread_data *t, *t2; 1556c98d5d94SLen Brown struct core_data *c, *c2; 1557c98d5d94SLen Brown struct pkg_data *p, *p2; 1558c98d5d94SLen Brown 1559c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1560c98d5d94SLen Brown 1561c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1562c98d5d94SLen Brown continue; 1563c98d5d94SLen Brown 1564c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1565c98d5d94SLen Brown 1566c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1567c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1568c98d5d94SLen Brown 1569c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1570c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1571c98d5d94SLen Brown 1572c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1573c98d5d94SLen Brown if (retval) 1574c98d5d94SLen Brown return retval; 1575c98d5d94SLen Brown } 1576c98d5d94SLen Brown } 1577c98d5d94SLen Brown } 1578c98d5d94SLen Brown return 0; 1579c98d5d94SLen Brown } 1580c98d5d94SLen Brown 1581c98d5d94SLen Brown /* 1582c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1583c98d5d94SLen Brown * return max_cpu number 1584c98d5d94SLen Brown */ 1585c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1586103a8feaSLen Brown { 1587103a8feaSLen Brown FILE *fp; 1588c98d5d94SLen Brown int cpu_num; 1589103a8feaSLen Brown int retval; 1590103a8feaSLen Brown 159157a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1592103a8feaSLen Brown 1593103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1594b2c95d90SJosh Triplett if (retval != 0) 1595b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1596103a8feaSLen Brown 1597c98d5d94SLen Brown while (1) { 1598c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1599103a8feaSLen Brown if (retval != 1) 1600103a8feaSLen Brown break; 1601103a8feaSLen Brown 1602c98d5d94SLen Brown retval = func(cpu_num); 1603c98d5d94SLen Brown if (retval) { 1604c98d5d94SLen Brown fclose(fp); 1605c98d5d94SLen Brown return(retval); 1606c98d5d94SLen Brown } 1607103a8feaSLen Brown } 1608103a8feaSLen Brown fclose(fp); 1609c98d5d94SLen Brown return 0; 1610103a8feaSLen Brown } 1611103a8feaSLen Brown 1612103a8feaSLen Brown void re_initialize(void) 1613103a8feaSLen Brown { 1614c98d5d94SLen Brown free_all_buffers(); 1615c98d5d94SLen Brown setup_all_buffers(); 1616c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1617103a8feaSLen Brown } 1618103a8feaSLen Brown 1619c98d5d94SLen Brown 1620103a8feaSLen Brown /* 1621c98d5d94SLen Brown * count_cpus() 1622c98d5d94SLen Brown * remember the last one seen, it will be the max 1623103a8feaSLen Brown */ 1624c98d5d94SLen Brown int count_cpus(int cpu) 1625103a8feaSLen Brown { 1626c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1627c98d5d94SLen Brown topo.max_cpu_num = cpu; 1628103a8feaSLen Brown 1629c98d5d94SLen Brown topo.num_cpus += 1; 1630c98d5d94SLen Brown return 0; 1631103a8feaSLen Brown } 1632c98d5d94SLen Brown int mark_cpu_present(int cpu) 1633c98d5d94SLen Brown { 1634c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 163515aaa346SLen Brown return 0; 1636103a8feaSLen Brown } 1637103a8feaSLen Brown 1638103a8feaSLen Brown void turbostat_loop() 1639103a8feaSLen Brown { 1640c98d5d94SLen Brown int retval; 1641e52966c0SLen Brown int restarted = 0; 1642c98d5d94SLen Brown 1643103a8feaSLen Brown restart: 1644e52966c0SLen Brown restarted++; 1645e52966c0SLen Brown 1646c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1647d91bb17cSLen Brown if (retval < -1) { 1648d91bb17cSLen Brown exit(retval); 1649d91bb17cSLen Brown } else if (retval == -1) { 1650e52966c0SLen Brown if (restarted > 1) { 1651e52966c0SLen Brown exit(retval); 1652e52966c0SLen Brown } 1653c98d5d94SLen Brown re_initialize(); 1654c98d5d94SLen Brown goto restart; 1655c98d5d94SLen Brown } 1656e52966c0SLen Brown restarted = 0; 1657103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1658103a8feaSLen Brown 1659103a8feaSLen Brown while (1) { 1660c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1661103a8feaSLen Brown re_initialize(); 1662103a8feaSLen Brown goto restart; 1663103a8feaSLen Brown } 1664103a8feaSLen Brown sleep(interval_sec); 1665c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1666d91bb17cSLen Brown if (retval < -1) { 1667d91bb17cSLen Brown exit(retval); 1668d91bb17cSLen Brown } else if (retval == -1) { 166915aaa346SLen Brown re_initialize(); 167015aaa346SLen Brown goto restart; 167115aaa346SLen Brown } 1672103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1673103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1674c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1675c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1676c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1677c98d5d94SLen Brown flush_stdout(); 167815aaa346SLen Brown sleep(interval_sec); 1679c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1680d91bb17cSLen Brown if (retval < -1) { 1681d91bb17cSLen Brown exit(retval); 1682d91bb17cSLen Brown } else if (retval == -1) { 1683103a8feaSLen Brown re_initialize(); 1684103a8feaSLen Brown goto restart; 1685103a8feaSLen Brown } 1686103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1687103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1688c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1689c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1690c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1691c98d5d94SLen Brown flush_stdout(); 1692103a8feaSLen Brown } 1693103a8feaSLen Brown } 1694103a8feaSLen Brown 1695103a8feaSLen Brown void check_dev_msr() 1696103a8feaSLen Brown { 1697103a8feaSLen Brown struct stat sb; 16987ce7d5deSPrarit Bhargava char pathname[32]; 1699103a8feaSLen Brown 17007ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 17017ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 1702a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 1703d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 1704103a8feaSLen Brown } 1705103a8feaSLen Brown 170698481e79SLen Brown void check_permissions() 1707103a8feaSLen Brown { 170898481e79SLen Brown struct __user_cap_header_struct cap_header_data; 170998481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 171098481e79SLen Brown struct __user_cap_data_struct cap_data_data; 171198481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 171298481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 171398481e79SLen Brown int do_exit = 0; 17147ce7d5deSPrarit Bhargava char pathname[32]; 171598481e79SLen Brown 171698481e79SLen Brown /* check for CAP_SYS_RAWIO */ 171798481e79SLen Brown cap_header->pid = getpid(); 171898481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 171998481e79SLen Brown if (capget(cap_header, cap_data) < 0) 172098481e79SLen Brown err(-6, "capget(2) failed"); 172198481e79SLen Brown 172298481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 172398481e79SLen Brown do_exit++; 172498481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 172598481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 172698481e79SLen Brown } 172798481e79SLen Brown 172898481e79SLen Brown /* test file permissions */ 17297ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 17307ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 173198481e79SLen Brown do_exit++; 173298481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 173398481e79SLen Brown } 173498481e79SLen Brown 173598481e79SLen Brown /* if all else fails, thell them to be root */ 173698481e79SLen Brown if (do_exit) 1737b2c95d90SJosh Triplett if (getuid() != 0) 1738d7899447SLen Brown warnx("... or simply run as root"); 173998481e79SLen Brown 174098481e79SLen Brown if (do_exit) 174198481e79SLen Brown exit(-6); 1742103a8feaSLen Brown } 1743103a8feaSLen Brown 1744d7899447SLen Brown /* 1745d7899447SLen Brown * NHM adds support for additional MSRs: 1746d7899447SLen Brown * 1747d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 1748d7899447SLen Brown * 1749d7899447SLen Brown * MSR_NHM_PLATFORM_INFO 0x000000ce 1750d7899447SLen Brown * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 1751d7899447SLen Brown * 1752d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 1753d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 1754d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 1755d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 1756d7899447SLen Brown * 1757ee7e38e3SLen Brown * Side effect: 1758ee7e38e3SLen Brown * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL 1759d7899447SLen Brown */ 1760ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 1761103a8feaSLen Brown { 1762ee7e38e3SLen Brown unsigned long long msr; 1763ee7e38e3SLen Brown int *pkg_cstate_limits; 1764ee7e38e3SLen Brown 1765103a8feaSLen Brown if (!genuine_intel) 1766103a8feaSLen Brown return 0; 1767103a8feaSLen Brown 1768103a8feaSLen Brown if (family != 6) 1769103a8feaSLen Brown return 0; 1770103a8feaSLen Brown 1771103a8feaSLen Brown switch (model) { 1772103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1773103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1774103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1775103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1776103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1777ee7e38e3SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1778ee7e38e3SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1779ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 1780ee7e38e3SLen Brown break; 1781103a8feaSLen Brown case 0x2A: /* SNB */ 1782103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1783553575f1SLen Brown case 0x3A: /* IVB */ 17841300651bSLen Brown case 0x3E: /* IVB Xeon */ 1785ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 1786ee7e38e3SLen Brown break; 178770b43400SLen Brown case 0x3C: /* HSW */ 1788e6f9bb3cSLen Brown case 0x3F: /* HSX */ 178970b43400SLen Brown case 0x45: /* HSW */ 1790149c2319SLen Brown case 0x46: /* HSW */ 17914e8e863fSLen Brown case 0x3D: /* BDW */ 179248a0631cSLen Brown case 0x47: /* BDW */ 17934e8e863fSLen Brown case 0x4F: /* BDX */ 17944e8e863fSLen Brown case 0x56: /* BDX-DE */ 17950b2bb692SLen Brown case 0x4E: /* SKL */ 17960b2bb692SLen Brown case 0x5E: /* SKL */ 1797ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 1798ee7e38e3SLen Brown break; 1799ee7e38e3SLen Brown case 0x37: /* BYT */ 1800ee7e38e3SLen Brown case 0x4D: /* AVN */ 1801ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 1802ee7e38e3SLen Brown break; 1803ee7e38e3SLen Brown case 0x4C: /* AMT */ 1804ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 1805ee7e38e3SLen Brown break; 1806ee7e38e3SLen Brown case 0x57: /* PHI */ 1807ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 1808ee7e38e3SLen Brown break; 1809103a8feaSLen Brown default: 1810103a8feaSLen Brown return 0; 1811103a8feaSLen Brown } 18127ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1813ee7e38e3SLen Brown 1814e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 1815ee7e38e3SLen Brown 1816ee7e38e3SLen Brown return 1; 1817103a8feaSLen Brown } 1818d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) 1819d7899447SLen Brown { 1820d7899447SLen Brown switch (model) { 1821d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 1822d7899447SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1823d7899447SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1824d7899447SLen Brown return 0; 1825d7899447SLen Brown default: 1826d7899447SLen Brown return 1; 1827d7899447SLen Brown } 1828d7899447SLen Brown } 18296574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 18306574a5d5SLen Brown { 18316574a5d5SLen Brown if (!genuine_intel) 18326574a5d5SLen Brown return 0; 18336574a5d5SLen Brown 18346574a5d5SLen Brown if (family != 6) 18356574a5d5SLen Brown return 0; 18366574a5d5SLen Brown 18376574a5d5SLen Brown switch (model) { 18386574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 1839fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 18406574a5d5SLen Brown return 1; 18416574a5d5SLen Brown default: 18426574a5d5SLen Brown return 0; 18436574a5d5SLen Brown } 18446574a5d5SLen Brown } 1845fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 1846fcd17211SLen Brown { 1847fcd17211SLen Brown if (!genuine_intel) 1848fcd17211SLen Brown return 0; 1849fcd17211SLen Brown 1850fcd17211SLen Brown if (family != 6) 1851fcd17211SLen Brown return 0; 1852fcd17211SLen Brown 1853fcd17211SLen Brown switch (model) { 1854fcd17211SLen Brown case 0x3F: /* HSW Xeon */ 1855fcd17211SLen Brown return 1; 1856fcd17211SLen Brown default: 1857fcd17211SLen Brown return 0; 1858fcd17211SLen Brown } 1859fcd17211SLen Brown } 1860fcd17211SLen Brown 1861fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 1862fb5d4327SDasaratharaman Chandramouli { 1863fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 1864fb5d4327SDasaratharaman Chandramouli return 0; 1865fb5d4327SDasaratharaman Chandramouli 1866fb5d4327SDasaratharaman Chandramouli if (family != 6) 1867fb5d4327SDasaratharaman Chandramouli return 0; 1868fb5d4327SDasaratharaman Chandramouli 1869fb5d4327SDasaratharaman Chandramouli switch (model) { 1870fb5d4327SDasaratharaman Chandramouli case 0x57: /* Knights Landing */ 1871fb5d4327SDasaratharaman Chandramouli return 1; 1872fb5d4327SDasaratharaman Chandramouli default: 1873fb5d4327SDasaratharaman Chandramouli return 0; 1874fb5d4327SDasaratharaman Chandramouli } 1875fb5d4327SDasaratharaman Chandramouli } 1876fcd17211SLen Brown static void 1877fcd17211SLen Brown dump_cstate_pstate_config_info(family, model) 1878fcd17211SLen Brown { 1879fcd17211SLen Brown if (!do_nhm_platform_info) 1880fcd17211SLen Brown return; 1881fcd17211SLen Brown 1882fcd17211SLen Brown dump_nhm_platform_info(); 1883fcd17211SLen Brown 1884fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 1885fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 1886fcd17211SLen Brown 1887fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 1888fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 1889fcd17211SLen Brown 1890fcd17211SLen Brown if (has_nhm_turbo_ratio_limit(family, model)) 1891fcd17211SLen Brown dump_nhm_turbo_ratio_limits(); 1892fcd17211SLen Brown 1893fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 1894fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 1895fb5d4327SDasaratharaman Chandramouli 1896fcd17211SLen Brown dump_nhm_cst_cfg(); 1897fcd17211SLen Brown } 1898fcd17211SLen Brown 18996574a5d5SLen Brown 1900889facbeSLen Brown /* 1901889facbeSLen Brown * print_epb() 1902889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 1903889facbeSLen Brown */ 1904889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1905889facbeSLen Brown { 1906889facbeSLen Brown unsigned long long msr; 1907889facbeSLen Brown char *epb_string; 1908889facbeSLen Brown int cpu; 1909889facbeSLen Brown 1910889facbeSLen Brown if (!has_epb) 1911889facbeSLen Brown return 0; 1912889facbeSLen Brown 1913889facbeSLen Brown cpu = t->cpu_id; 1914889facbeSLen Brown 1915889facbeSLen Brown /* EPB is per-package */ 1916889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1917889facbeSLen Brown return 0; 1918889facbeSLen Brown 1919889facbeSLen Brown if (cpu_migrate(cpu)) { 1920889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1921889facbeSLen Brown return -1; 1922889facbeSLen Brown } 1923889facbeSLen Brown 1924889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 1925889facbeSLen Brown return 0; 1926889facbeSLen Brown 1927e9be7dd6SLen Brown switch (msr & 0xF) { 1928889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 1929889facbeSLen Brown epb_string = "performance"; 1930889facbeSLen Brown break; 1931889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 1932889facbeSLen Brown epb_string = "balanced"; 1933889facbeSLen Brown break; 1934889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 1935889facbeSLen Brown epb_string = "powersave"; 1936889facbeSLen Brown break; 1937889facbeSLen Brown default: 1938889facbeSLen Brown epb_string = "custom"; 1939889facbeSLen Brown break; 1940889facbeSLen Brown } 1941889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 1942889facbeSLen Brown 1943889facbeSLen Brown return 0; 1944889facbeSLen Brown } 1945889facbeSLen Brown 19463a9a941dSLen Brown /* 19473a9a941dSLen Brown * print_perf_limit() 19483a9a941dSLen Brown */ 19493a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 19503a9a941dSLen Brown { 19513a9a941dSLen Brown unsigned long long msr; 19523a9a941dSLen Brown int cpu; 19533a9a941dSLen Brown 19543a9a941dSLen Brown cpu = t->cpu_id; 19553a9a941dSLen Brown 19563a9a941dSLen Brown /* per-package */ 19573a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 19583a9a941dSLen Brown return 0; 19593a9a941dSLen Brown 19603a9a941dSLen Brown if (cpu_migrate(cpu)) { 19613a9a941dSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 19623a9a941dSLen Brown return -1; 19633a9a941dSLen Brown } 19643a9a941dSLen Brown 19653a9a941dSLen Brown if (do_core_perf_limit_reasons) { 19663a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 19673a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 19683a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 1969e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 19703a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 1971e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 1972e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 1973e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 1974e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 1975e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 1976e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 1977e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 1978e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 1979e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 1980e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 1981e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 1982e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 19833a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 1984e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 19853a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 1986e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 1987e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 1988e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 1989e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 1990e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 1991e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 1992e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 1993e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 1994e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 1995e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 1996e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 1997e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 19983a9a941dSLen Brown 19993a9a941dSLen Brown } 20003a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 20013a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 20023a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 20033a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)", 20043a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 20053a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 20063a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 20073a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 20083a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 20093a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 20103a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 20113a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 20123a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n", 20133a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 20143a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 20153a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 20163a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 20173a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 20183a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 20193a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 20203a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 20213a9a941dSLen Brown } 20223a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 20233a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 20243a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 20253a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s)", 20263a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 20273a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 20283a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 20293a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 20303a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 20313a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 20323a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n", 20333a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 20343a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 20353a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 20363a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 20373a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 20383a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 20393a9a941dSLen Brown } 20403a9a941dSLen Brown return 0; 20413a9a941dSLen Brown } 20423a9a941dSLen Brown 2043889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 2044889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 2045889facbeSLen Brown 2046144b44b1SLen Brown double get_tdp(model) 2047144b44b1SLen Brown { 2048144b44b1SLen Brown unsigned long long msr; 2049144b44b1SLen Brown 2050144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 20517ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 2052144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 2053144b44b1SLen Brown 2054144b44b1SLen Brown switch (model) { 2055144b44b1SLen Brown case 0x37: 2056144b44b1SLen Brown case 0x4D: 2057144b44b1SLen Brown return 30.0; 2058144b44b1SLen Brown default: 2059144b44b1SLen Brown return 135.0; 2060144b44b1SLen Brown } 2061144b44b1SLen Brown } 2062144b44b1SLen Brown 206340ee8e3bSAndrey Semin /* 206440ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 206540ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 206640ee8e3bSAndrey Semin */ 206740ee8e3bSAndrey Semin static double 206840ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 206940ee8e3bSAndrey Semin { 207040ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 207140ee8e3bSAndrey Semin 207240ee8e3bSAndrey Semin switch (model) { 207340ee8e3bSAndrey Semin case 0x3F: /* HSX */ 207440ee8e3bSAndrey Semin case 0x4F: /* BDX */ 207540ee8e3bSAndrey Semin case 0x56: /* BDX-DE */ 2076fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 207740ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 207840ee8e3bSAndrey Semin default: 207940ee8e3bSAndrey Semin return (rapl_energy_units); 208040ee8e3bSAndrey Semin } 208140ee8e3bSAndrey Semin } 208240ee8e3bSAndrey Semin 2083144b44b1SLen Brown 2084889facbeSLen Brown /* 2085889facbeSLen Brown * rapl_probe() 2086889facbeSLen Brown * 2087144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 2088889facbeSLen Brown */ 2089889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 2090889facbeSLen Brown { 2091889facbeSLen Brown unsigned long long msr; 2092144b44b1SLen Brown unsigned int time_unit; 2093889facbeSLen Brown double tdp; 2094889facbeSLen Brown 2095889facbeSLen Brown if (!genuine_intel) 2096889facbeSLen Brown return; 2097889facbeSLen Brown 2098889facbeSLen Brown if (family != 6) 2099889facbeSLen Brown return; 2100889facbeSLen Brown 2101889facbeSLen Brown switch (model) { 2102889facbeSLen Brown case 0x2A: 2103889facbeSLen Brown case 0x3A: 210470b43400SLen Brown case 0x3C: /* HSW */ 210570b43400SLen Brown case 0x45: /* HSW */ 2106149c2319SLen Brown case 0x46: /* HSW */ 21074e8e863fSLen Brown case 0x3D: /* BDW */ 210848a0631cSLen Brown case 0x47: /* BDW */ 2109144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 2110889facbeSLen Brown break; 21110b2bb692SLen Brown case 0x4E: /* SKL */ 21120b2bb692SLen Brown case 0x5E: /* SKL */ 21130b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 21140b2bb692SLen Brown break; 2115e6f9bb3cSLen Brown case 0x3F: /* HSX */ 21164e8e863fSLen Brown case 0x4F: /* BDX */ 21174e8e863fSLen Brown case 0x56: /* BDX-DE */ 2118fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 21190b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 2120e6f9bb3cSLen Brown break; 2121889facbeSLen Brown case 0x2D: 2122889facbeSLen Brown case 0x3E: 21230b2bb692SLen 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; 2124144b44b1SLen Brown break; 2125144b44b1SLen Brown case 0x37: /* BYT */ 2126144b44b1SLen Brown case 0x4D: /* AVN */ 2127144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES ; 2128889facbeSLen Brown break; 2129889facbeSLen Brown default: 2130889facbeSLen Brown return; 2131889facbeSLen Brown } 2132889facbeSLen Brown 2133889facbeSLen Brown /* units on package 0, verify later other packages match */ 21347ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 2135889facbeSLen Brown return; 2136889facbeSLen Brown 2137889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 2138144b44b1SLen Brown if (model == 0x37) 2139144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 2140144b44b1SLen Brown else 2141889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 2142889facbeSLen Brown 214340ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 214440ee8e3bSAndrey Semin 2145144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 2146144b44b1SLen Brown if (time_unit == 0) 2147144b44b1SLen Brown time_unit = 0xA; 2148889facbeSLen Brown 2149144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 2150144b44b1SLen Brown 2151144b44b1SLen Brown tdp = get_tdp(model); 2152889facbeSLen Brown 2153889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 2154d8af6f5fSLen Brown if (debug) 2155144b44b1SLen Brown fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 2156889facbeSLen Brown 2157889facbeSLen Brown return; 2158889facbeSLen Brown } 2159889facbeSLen Brown 21603a9a941dSLen Brown void perf_limit_reasons_probe(family, model) 21613a9a941dSLen Brown { 21623a9a941dSLen Brown if (!genuine_intel) 21633a9a941dSLen Brown return; 21643a9a941dSLen Brown 21653a9a941dSLen Brown if (family != 6) 21663a9a941dSLen Brown return; 21673a9a941dSLen Brown 21683a9a941dSLen Brown switch (model) { 21693a9a941dSLen Brown case 0x3C: /* HSW */ 21703a9a941dSLen Brown case 0x45: /* HSW */ 21713a9a941dSLen Brown case 0x46: /* HSW */ 21723a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 21733a9a941dSLen Brown case 0x3F: /* HSX */ 21743a9a941dSLen Brown do_core_perf_limit_reasons = 1; 21753a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 21763a9a941dSLen Brown default: 21773a9a941dSLen Brown return; 21783a9a941dSLen Brown } 21793a9a941dSLen Brown } 21803a9a941dSLen Brown 2181889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2182889facbeSLen Brown { 2183889facbeSLen Brown unsigned long long msr; 2184889facbeSLen Brown unsigned int dts; 2185889facbeSLen Brown int cpu; 2186889facbeSLen Brown 2187889facbeSLen Brown if (!(do_dts || do_ptm)) 2188889facbeSLen Brown return 0; 2189889facbeSLen Brown 2190889facbeSLen Brown cpu = t->cpu_id; 2191889facbeSLen Brown 2192889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 2193889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2194889facbeSLen Brown return 0; 2195889facbeSLen Brown 2196889facbeSLen Brown if (cpu_migrate(cpu)) { 2197889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2198889facbeSLen Brown return -1; 2199889facbeSLen Brown } 2200889facbeSLen Brown 2201889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 2202889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2203889facbeSLen Brown return 0; 2204889facbeSLen Brown 2205889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2206889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 2207889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 2208889facbeSLen Brown 2209889facbeSLen Brown #ifdef THERM_DEBUG 2210889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 2211889facbeSLen Brown return 0; 2212889facbeSLen Brown 2213889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2214889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2215889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2216889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2217889facbeSLen Brown #endif 2218889facbeSLen Brown } 2219889facbeSLen Brown 2220889facbeSLen Brown 2221889facbeSLen Brown if (do_dts) { 2222889facbeSLen Brown unsigned int resolution; 2223889facbeSLen Brown 2224889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2225889facbeSLen Brown return 0; 2226889facbeSLen Brown 2227889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2228889facbeSLen Brown resolution = (msr >> 27) & 0xF; 2229889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 2230889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 2231889facbeSLen Brown 2232889facbeSLen Brown #ifdef THERM_DEBUG 2233889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 2234889facbeSLen Brown return 0; 2235889facbeSLen Brown 2236889facbeSLen Brown dts = (msr >> 16) & 0x7F; 2237889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 2238889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 2239889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 2240889facbeSLen Brown #endif 2241889facbeSLen Brown } 2242889facbeSLen Brown 2243889facbeSLen Brown return 0; 2244889facbeSLen Brown } 2245889facbeSLen Brown 2246889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 2247889facbeSLen Brown { 2248889facbeSLen Brown fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 2249889facbeSLen Brown cpu, label, 2250889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 2251889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 2252889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 2253889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 2254889facbeSLen Brown 2255889facbeSLen Brown return; 2256889facbeSLen Brown } 2257889facbeSLen Brown 2258889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2259889facbeSLen Brown { 2260889facbeSLen Brown unsigned long long msr; 2261889facbeSLen Brown int cpu; 2262889facbeSLen Brown 2263889facbeSLen Brown if (!do_rapl) 2264889facbeSLen Brown return 0; 2265889facbeSLen Brown 2266889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 2267889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2268889facbeSLen Brown return 0; 2269889facbeSLen Brown 2270889facbeSLen Brown cpu = t->cpu_id; 2271889facbeSLen Brown if (cpu_migrate(cpu)) { 2272889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2273889facbeSLen Brown return -1; 2274889facbeSLen Brown } 2275889facbeSLen Brown 2276889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 2277889facbeSLen Brown return -1; 2278889facbeSLen Brown 2279d8af6f5fSLen Brown if (debug) { 2280889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 2281889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 2282144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 2283889facbeSLen Brown } 2284144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 2285144b44b1SLen Brown 2286889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 2287889facbeSLen Brown return -5; 2288889facbeSLen Brown 2289889facbeSLen Brown 2290889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2291889facbeSLen Brown cpu, msr, 2292889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2293889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2294889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2295889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2296889facbeSLen Brown 2297144b44b1SLen Brown } 2298144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 2299144b44b1SLen Brown 2300889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 2301889facbeSLen Brown return -9; 2302889facbeSLen Brown 2303889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 2304889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 2305889facbeSLen Brown 2306889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 2307889facbeSLen Brown fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 2308889facbeSLen Brown cpu, 2309889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 2310889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 2311889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 2312889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 2313889facbeSLen Brown } 2314889facbeSLen Brown 23150b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 2316889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 2317889facbeSLen Brown return -6; 2318889facbeSLen Brown 2319889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2320889facbeSLen Brown cpu, msr, 2321889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2322889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2323889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2324889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 23250b2bb692SLen Brown } 23260b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 2327889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 2328889facbeSLen Brown return -9; 2329889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 2330889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2331889facbeSLen Brown 2332889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 2333889facbeSLen Brown } 2334144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 2335d8af6f5fSLen Brown if (debug) { 2336889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 2337889facbeSLen Brown return -7; 2338889facbeSLen Brown 2339889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 2340144b44b1SLen Brown } 2341144b44b1SLen Brown } 2342144b44b1SLen Brown if (do_rapl & RAPL_CORES) { 2343d8af6f5fSLen Brown if (debug) { 2344889facbeSLen Brown 2345889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 2346889facbeSLen Brown return -9; 2347889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 2348889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2349889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 2350889facbeSLen Brown } 2351889facbeSLen Brown } 2352889facbeSLen Brown if (do_rapl & RAPL_GFX) { 2353d8af6f5fSLen Brown if (debug) { 2354889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 2355889facbeSLen Brown return -8; 2356889facbeSLen Brown 2357889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 2358889facbeSLen Brown 2359889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 2360889facbeSLen Brown return -9; 2361889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 2362889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2363889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 2364889facbeSLen Brown } 2365889facbeSLen Brown } 2366889facbeSLen Brown return 0; 2367889facbeSLen Brown } 2368889facbeSLen Brown 2369d7899447SLen Brown /* 2370d7899447SLen Brown * SNB adds support for additional MSRs: 2371d7899447SLen Brown * 2372d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 2373d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 2374d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 2375d7899447SLen Brown */ 2376103a8feaSLen Brown 2377d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 2378103a8feaSLen Brown { 2379103a8feaSLen Brown if (!genuine_intel) 2380103a8feaSLen Brown return 0; 2381103a8feaSLen Brown 2382103a8feaSLen Brown switch (model) { 2383103a8feaSLen Brown case 0x2A: 2384103a8feaSLen Brown case 0x2D: 2385650a37f3SLen Brown case 0x3A: /* IVB */ 23861300651bSLen Brown case 0x3E: /* IVB Xeon */ 238770b43400SLen Brown case 0x3C: /* HSW */ 238870b43400SLen Brown case 0x3F: /* HSW */ 238970b43400SLen Brown case 0x45: /* HSW */ 2390149c2319SLen Brown case 0x46: /* HSW */ 23914e8e863fSLen Brown case 0x3D: /* BDW */ 239248a0631cSLen Brown case 0x47: /* BDW */ 23934e8e863fSLen Brown case 0x4F: /* BDX */ 23944e8e863fSLen Brown case 0x56: /* BDX-DE */ 23950b2bb692SLen Brown case 0x4E: /* SKL */ 23960b2bb692SLen Brown case 0x5E: /* SKL */ 2397103a8feaSLen Brown return 1; 2398103a8feaSLen Brown } 2399103a8feaSLen Brown return 0; 2400103a8feaSLen Brown } 2401103a8feaSLen Brown 2402d7899447SLen Brown /* 2403d7899447SLen Brown * HSW adds support for additional MSRs: 2404d7899447SLen Brown * 2405d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 2406d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 2407d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 2408d7899447SLen Brown */ 2409d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 2410ca58710fSKristen Carlson Accardi { 2411ca58710fSKristen Carlson Accardi if (!genuine_intel) 2412ca58710fSKristen Carlson Accardi return 0; 2413ca58710fSKristen Carlson Accardi 2414ca58710fSKristen Carlson Accardi switch (model) { 24154e8e863fSLen Brown case 0x45: /* HSW */ 24164e8e863fSLen Brown case 0x3D: /* BDW */ 24170b2bb692SLen Brown case 0x4E: /* SKL */ 24180b2bb692SLen Brown case 0x5E: /* SKL */ 2419ca58710fSKristen Carlson Accardi return 1; 2420ca58710fSKristen Carlson Accardi } 2421ca58710fSKristen Carlson Accardi return 0; 2422ca58710fSKristen Carlson Accardi } 2423ca58710fSKristen Carlson Accardi 24240b2bb692SLen Brown /* 24250b2bb692SLen Brown * SKL adds support for additional MSRS: 24260b2bb692SLen Brown * 24270b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 24280b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 24290b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 24300b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 24310b2bb692SLen Brown */ 24320b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 24330b2bb692SLen Brown { 24340b2bb692SLen Brown if (!genuine_intel) 24350b2bb692SLen Brown return 0; 24360b2bb692SLen Brown 24370b2bb692SLen Brown switch (model) { 24380b2bb692SLen Brown case 0x4E: /* SKL */ 24390b2bb692SLen Brown case 0x5E: /* SKL */ 24400b2bb692SLen Brown return 1; 24410b2bb692SLen Brown } 24420b2bb692SLen Brown return 0; 24430b2bb692SLen Brown } 24440b2bb692SLen Brown 24450b2bb692SLen Brown 2446ca58710fSKristen Carlson Accardi 2447144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 2448144b44b1SLen Brown { 2449144b44b1SLen Brown if (!genuine_intel) 2450144b44b1SLen Brown return 0; 2451144b44b1SLen Brown switch (model) { 2452144b44b1SLen Brown case 0x37: /* BYT */ 2453144b44b1SLen Brown case 0x4D: /* AVN */ 2454144b44b1SLen Brown return 1; 2455144b44b1SLen Brown } 2456144b44b1SLen Brown return 0; 2457144b44b1SLen Brown } 2458144b44b1SLen Brown 2459fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 2460fb5d4327SDasaratharaman Chandramouli { 2461fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2462fb5d4327SDasaratharaman Chandramouli return 0; 2463fb5d4327SDasaratharaman Chandramouli switch (model) { 2464fb5d4327SDasaratharaman Chandramouli case 0x57: /* KNL */ 2465fb5d4327SDasaratharaman Chandramouli return 1; 2466fb5d4327SDasaratharaman Chandramouli } 2467fb5d4327SDasaratharaman Chandramouli return 0; 2468fb5d4327SDasaratharaman Chandramouli } 2469fb5d4327SDasaratharaman Chandramouli 2470144b44b1SLen Brown #define SLM_BCLK_FREQS 5 2471144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 2472144b44b1SLen Brown 2473144b44b1SLen Brown double slm_bclk(void) 2474144b44b1SLen Brown { 2475144b44b1SLen Brown unsigned long long msr = 3; 2476144b44b1SLen Brown unsigned int i; 2477144b44b1SLen Brown double freq; 2478144b44b1SLen Brown 24797ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 2480144b44b1SLen Brown fprintf(stderr, "SLM BCLK: unknown\n"); 2481144b44b1SLen Brown 2482144b44b1SLen Brown i = msr & 0xf; 2483144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 2484144b44b1SLen Brown fprintf(stderr, "SLM BCLK[%d] invalid\n", i); 2485144b44b1SLen Brown msr = 3; 2486144b44b1SLen Brown } 2487144b44b1SLen Brown freq = slm_freq_table[i]; 2488144b44b1SLen Brown 2489144b44b1SLen Brown fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); 2490144b44b1SLen Brown 2491144b44b1SLen Brown return freq; 2492144b44b1SLen Brown } 2493144b44b1SLen Brown 2494103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 2495103a8feaSLen Brown { 2496d7899447SLen Brown if (has_snb_msrs(family, model)) 2497103a8feaSLen Brown return 100.00; 2498144b44b1SLen Brown else if (is_slm(family, model)) 2499144b44b1SLen Brown return slm_bclk(); 2500103a8feaSLen Brown else 2501103a8feaSLen Brown return 133.33; 2502103a8feaSLen Brown } 2503103a8feaSLen Brown 2504889facbeSLen Brown /* 2505889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 2506889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 2507889facbeSLen Brown * This is usually equal to tjMax. 2508889facbeSLen Brown * 2509889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 2510889facbeSLen Brown * but also allow cmdline over-ride with -T. 2511889facbeSLen Brown * 2512889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 2513889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 2514889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 2515889facbeSLen Brown */ 2516889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2517889facbeSLen Brown { 2518889facbeSLen Brown unsigned long long msr; 2519889facbeSLen Brown unsigned int target_c_local; 2520889facbeSLen Brown int cpu; 2521889facbeSLen Brown 2522889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 2523889facbeSLen Brown if (!(do_dts || do_ptm)) 2524889facbeSLen Brown return 0; 2525889facbeSLen Brown 2526889facbeSLen Brown /* this is a per-package concept */ 2527889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2528889facbeSLen Brown return 0; 2529889facbeSLen Brown 2530889facbeSLen Brown cpu = t->cpu_id; 2531889facbeSLen Brown if (cpu_migrate(cpu)) { 2532889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2533889facbeSLen Brown return -1; 2534889facbeSLen Brown } 2535889facbeSLen Brown 2536889facbeSLen Brown if (tcc_activation_temp_override != 0) { 2537889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 2538889facbeSLen Brown fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 2539889facbeSLen Brown cpu, tcc_activation_temp); 2540889facbeSLen Brown return 0; 2541889facbeSLen Brown } 2542889facbeSLen Brown 2543889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 2544d7899447SLen Brown if (!do_nhm_platform_info) 2545889facbeSLen Brown goto guess; 2546889facbeSLen Brown 25477ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 2548889facbeSLen Brown goto guess; 2549889facbeSLen Brown 25503482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 2551889facbeSLen Brown 2552d8af6f5fSLen Brown if (debug) 2553889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 2554889facbeSLen Brown cpu, msr, target_c_local); 2555889facbeSLen Brown 25563482124aSJean Delvare if (!target_c_local) 2557889facbeSLen Brown goto guess; 2558889facbeSLen Brown 2559889facbeSLen Brown tcc_activation_temp = target_c_local; 2560889facbeSLen Brown 2561889facbeSLen Brown return 0; 2562889facbeSLen Brown 2563889facbeSLen Brown guess: 2564889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 2565889facbeSLen Brown fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 2566889facbeSLen Brown cpu, tcc_activation_temp); 2567889facbeSLen Brown 2568889facbeSLen Brown return 0; 2569889facbeSLen Brown } 2570fcd17211SLen Brown void process_cpuid() 2571103a8feaSLen Brown { 2572103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 2573103a8feaSLen Brown unsigned int fms, family, model, stepping; 2574103a8feaSLen Brown 2575103a8feaSLen Brown eax = ebx = ecx = edx = 0; 2576103a8feaSLen Brown 25772b92865eSJosh Triplett __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 2578103a8feaSLen Brown 2579103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 2580103a8feaSLen Brown genuine_intel = 1; 2581103a8feaSLen Brown 2582d8af6f5fSLen Brown if (debug) 2583889facbeSLen Brown fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 2584103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 2585103a8feaSLen Brown 25862b92865eSJosh Triplett __get_cpuid(1, &fms, &ebx, &ecx, &edx); 2587103a8feaSLen Brown family = (fms >> 8) & 0xf; 2588103a8feaSLen Brown model = (fms >> 4) & 0xf; 2589103a8feaSLen Brown stepping = fms & 0xf; 2590103a8feaSLen Brown if (family == 6 || family == 0xf) 2591103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 2592103a8feaSLen Brown 2593d8af6f5fSLen Brown if (debug) 2594103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 2595103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 2596103a8feaSLen Brown 2597b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 2598b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 2599103a8feaSLen Brown 2600103a8feaSLen Brown /* 2601103a8feaSLen Brown * check max extended function levels of CPUID. 2602103a8feaSLen Brown * This is needed to check for invariant TSC. 2603103a8feaSLen Brown * This check is valid for both Intel and AMD. 2604103a8feaSLen Brown */ 2605103a8feaSLen Brown ebx = ecx = edx = 0; 26062b92865eSJosh Triplett __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); 2607103a8feaSLen Brown 2608d7899447SLen Brown if (max_level >= 0x80000007) { 2609103a8feaSLen Brown 2610103a8feaSLen Brown /* 2611103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 2612103a8feaSLen Brown * this check is valid for both Intel and AMD 2613103a8feaSLen Brown */ 26142b92865eSJosh Triplett __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 26158209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 2616d7899447SLen Brown } 2617103a8feaSLen Brown 2618103a8feaSLen Brown /* 2619103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 2620103a8feaSLen Brown * this check is valid for both Intel and AMD 2621103a8feaSLen Brown */ 2622103a8feaSLen Brown 26232b92865eSJosh Triplett __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 26248209e054SThomas Renninger has_aperf = ecx & (1 << 0); 2625889facbeSLen Brown do_dts = eax & (1 << 0); 2626889facbeSLen Brown do_ptm = eax & (1 << 6); 2627889facbeSLen Brown has_epb = ecx & (1 << 3); 2628889facbeSLen Brown 2629d8af6f5fSLen Brown if (debug) 2630a729617cSLen Brown fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n", 2631a729617cSLen Brown has_aperf ? "" : "No ", 2632a729617cSLen Brown do_dts ? "" : "No ", 2633a729617cSLen Brown do_ptm ? "" : "No ", 2634a729617cSLen Brown has_epb ? "" : "No "); 2635103a8feaSLen Brown 26368a5bdf41SLen Brown if (max_level > 0x15) { 26378a5bdf41SLen Brown unsigned int eax_crystal; 26388a5bdf41SLen Brown unsigned int ebx_tsc; 26398a5bdf41SLen Brown 26408a5bdf41SLen Brown /* 26418a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 26428a5bdf41SLen Brown */ 26438a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 26448a5bdf41SLen Brown __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx); 26458a5bdf41SLen Brown 26468a5bdf41SLen Brown if (ebx_tsc != 0) { 26478a5bdf41SLen Brown 26488a5bdf41SLen Brown if (debug && (ebx != 0)) 26498a5bdf41SLen Brown fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 26508a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 26518a5bdf41SLen Brown 26528a5bdf41SLen Brown if (crystal_hz == 0) 26538a5bdf41SLen Brown switch(model) { 26548a5bdf41SLen Brown case 0x4E: /* SKL */ 26558a5bdf41SLen Brown case 0x5E: /* SKL */ 26568a5bdf41SLen Brown crystal_hz = 24000000; /* 24 MHz */ 26578a5bdf41SLen Brown break; 26588a5bdf41SLen Brown default: 26598a5bdf41SLen Brown crystal_hz = 0; 26608a5bdf41SLen Brown } 26618a5bdf41SLen Brown 26628a5bdf41SLen Brown if (crystal_hz) { 26638a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 26648a5bdf41SLen Brown if (debug) 26658a5bdf41SLen Brown fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 26668a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 26678a5bdf41SLen Brown } 26688a5bdf41SLen Brown } 26698a5bdf41SLen Brown } 26708a5bdf41SLen Brown 2671ee7e38e3SLen Brown do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); 2672d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 2673ee7e38e3SLen Brown do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); 2674ee7e38e3SLen Brown do_pc3 = (pkg_cstate_limit >= PCL__3); 2675ee7e38e3SLen Brown do_pc6 = (pkg_cstate_limit >= PCL__6); 2676ee7e38e3SLen Brown do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); 2677d7899447SLen Brown do_c8_c9_c10 = has_hsw_msrs(family, model); 26780b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 2679144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 2680fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 2681103a8feaSLen Brown bclk = discover_bclk(family, model); 2682103a8feaSLen Brown 2683889facbeSLen Brown rapl_probe(family, model); 26843a9a941dSLen Brown perf_limit_reasons_probe(family, model); 2685889facbeSLen Brown 2686fcd17211SLen Brown if (debug) 2687fcd17211SLen Brown dump_cstate_pstate_config_info(); 2688fcd17211SLen Brown 2689889facbeSLen Brown return; 2690103a8feaSLen Brown } 2691103a8feaSLen Brown 2692d8af6f5fSLen Brown void help() 2693103a8feaSLen Brown { 2694d8af6f5fSLen Brown fprintf(stderr, 2695d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 2696d8af6f5fSLen Brown "\n" 2697d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 2698d8af6f5fSLen Brown "when COMMAND completes.\n" 2699d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 2700d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 2701d8af6f5fSLen Brown "--debug run in \"debug\" mode\n" 2702d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 2703d8af6f5fSLen Brown "--help print this help message\n" 2704d8af6f5fSLen Brown "--counter msr print 32-bit counter at address \"msr\"\n" 2705d8af6f5fSLen Brown "--Counter msr print 64-bit Counter at address \"msr\"\n" 2706d8af6f5fSLen Brown "--msr msr print 32-bit value at address \"msr\"\n" 2707d8af6f5fSLen Brown "--MSR msr print 64-bit Value at address \"msr\"\n" 2708d8af6f5fSLen Brown "--version print version information\n" 2709d8af6f5fSLen Brown "\n" 2710d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 2711103a8feaSLen Brown } 2712103a8feaSLen Brown 2713103a8feaSLen Brown 2714103a8feaSLen Brown /* 2715103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 2716103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 2717103a8feaSLen Brown */ 2718103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 2719103a8feaSLen Brown { 2720103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 2721103a8feaSLen Brown return 1; 2722103a8feaSLen Brown else 2723103a8feaSLen Brown return 0; 2724103a8feaSLen Brown } 2725103a8feaSLen Brown 2726103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 2727103a8feaSLen Brown { 2728103a8feaSLen Brown return 0; 2729103a8feaSLen Brown } 2730103a8feaSLen Brown 2731c98d5d94SLen Brown void topology_probe() 2732c98d5d94SLen Brown { 2733c98d5d94SLen Brown int i; 2734c98d5d94SLen Brown int max_core_id = 0; 2735c98d5d94SLen Brown int max_package_id = 0; 2736c98d5d94SLen Brown int max_siblings = 0; 2737c98d5d94SLen Brown struct cpu_topology { 2738c98d5d94SLen Brown int core_id; 2739c98d5d94SLen Brown int physical_package_id; 2740c98d5d94SLen Brown } *cpus; 2741c98d5d94SLen Brown 2742c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 2743c98d5d94SLen Brown topo.num_cpus = 0; 2744c98d5d94SLen Brown topo.max_cpu_num = 0; 2745c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 2746c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 2747c98d5d94SLen Brown show_cpu = 1; 2748c98d5d94SLen Brown 2749d8af6f5fSLen Brown if (debug > 1) 2750c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 2751c98d5d94SLen Brown 2752c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 2753b2c95d90SJosh Triplett if (cpus == NULL) 2754b2c95d90SJosh Triplett err(1, "calloc cpus"); 2755c98d5d94SLen Brown 2756c98d5d94SLen Brown /* 2757c98d5d94SLen Brown * Allocate and initialize cpu_present_set 2758c98d5d94SLen Brown */ 2759c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2760b2c95d90SJosh Triplett if (cpu_present_set == NULL) 2761b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2762c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2763c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 2764c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 2765c98d5d94SLen Brown 2766c98d5d94SLen Brown /* 2767c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 2768c98d5d94SLen Brown */ 2769c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2770b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 2771b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2772c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2773c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 2774c98d5d94SLen Brown 2775c98d5d94SLen Brown 2776c98d5d94SLen Brown /* 2777c98d5d94SLen Brown * For online cpus 2778c98d5d94SLen Brown * find max_core_id, max_package_id 2779c98d5d94SLen Brown */ 2780c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 2781c98d5d94SLen Brown int siblings; 2782c98d5d94SLen Brown 2783c98d5d94SLen Brown if (cpu_is_not_present(i)) { 2784d8af6f5fSLen Brown if (debug > 1) 2785c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 2786c98d5d94SLen Brown continue; 2787c98d5d94SLen Brown } 2788c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 2789c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 2790c98d5d94SLen Brown max_core_id = cpus[i].core_id; 2791c98d5d94SLen Brown 2792c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 2793c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 2794c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 2795c98d5d94SLen Brown 2796c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 2797c98d5d94SLen Brown if (siblings > max_siblings) 2798c98d5d94SLen Brown max_siblings = siblings; 2799d8af6f5fSLen Brown if (debug > 1) 2800c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 2801c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 2802c98d5d94SLen Brown } 2803c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 2804d8af6f5fSLen Brown if (debug > 1) 2805c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 2806c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 28071cc21f7bSLen Brown if (debug && !summary_only && topo.num_cores_per_pkg > 1) 2808c98d5d94SLen Brown show_core = 1; 2809c98d5d94SLen Brown 2810c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 2811d8af6f5fSLen Brown if (debug > 1) 2812c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 2813c98d5d94SLen Brown max_package_id, topo.num_packages); 28141cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 2815c98d5d94SLen Brown show_pkg = 1; 2816c98d5d94SLen Brown 2817c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 2818d8af6f5fSLen Brown if (debug > 1) 2819c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 2820c98d5d94SLen Brown 2821c98d5d94SLen Brown free(cpus); 2822c98d5d94SLen Brown } 2823c98d5d94SLen Brown 2824c98d5d94SLen Brown void 2825c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 2826c98d5d94SLen Brown { 2827c98d5d94SLen Brown int i; 2828c98d5d94SLen Brown 2829c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 2830c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 2831c98d5d94SLen Brown if (*t == NULL) 2832c98d5d94SLen Brown goto error; 2833c98d5d94SLen Brown 2834c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 2835c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 2836c98d5d94SLen Brown (*t)[i].cpu_id = -1; 2837c98d5d94SLen Brown 2838c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 2839c98d5d94SLen Brown sizeof(struct core_data)); 2840c98d5d94SLen Brown if (*c == NULL) 2841c98d5d94SLen Brown goto error; 2842c98d5d94SLen Brown 2843c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 2844c98d5d94SLen Brown (*c)[i].core_id = -1; 2845c98d5d94SLen Brown 2846c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 2847c98d5d94SLen Brown if (*p == NULL) 2848c98d5d94SLen Brown goto error; 2849c98d5d94SLen Brown 2850c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 2851c98d5d94SLen Brown (*p)[i].package_id = i; 2852c98d5d94SLen Brown 2853c98d5d94SLen Brown return; 2854c98d5d94SLen Brown error: 2855b2c95d90SJosh Triplett err(1, "calloc counters"); 2856c98d5d94SLen Brown } 2857c98d5d94SLen Brown /* 2858c98d5d94SLen Brown * init_counter() 2859c98d5d94SLen Brown * 2860c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 2861c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 2862c98d5d94SLen Brown * 2863c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 2864c98d5d94SLen Brown */ 2865c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 2866c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 2867c98d5d94SLen Brown int pkg_num, int cpu_id) 2868c98d5d94SLen Brown { 2869c98d5d94SLen Brown struct thread_data *t; 2870c98d5d94SLen Brown struct core_data *c; 2871c98d5d94SLen Brown struct pkg_data *p; 2872c98d5d94SLen Brown 2873c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 2874c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 2875c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 2876c98d5d94SLen Brown 2877c98d5d94SLen Brown t->cpu_id = cpu_id; 2878c98d5d94SLen Brown if (thread_num == 0) { 2879c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 2880c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 2881c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 2882c98d5d94SLen Brown } 2883c98d5d94SLen Brown 2884c98d5d94SLen Brown c->core_id = core_num; 2885c98d5d94SLen Brown p->package_id = pkg_num; 2886c98d5d94SLen Brown } 2887c98d5d94SLen Brown 2888c98d5d94SLen Brown 2889c98d5d94SLen Brown int initialize_counters(int cpu_id) 2890c98d5d94SLen Brown { 2891c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 2892c98d5d94SLen Brown 2893c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 2894c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 2895e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 2896e275b388SDasaratharaman Chandramouli if (!my_thread_id) 2897c98d5d94SLen Brown topo.num_cores++; 2898c98d5d94SLen Brown 2899c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2900c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2901c98d5d94SLen Brown return 0; 2902c98d5d94SLen Brown } 2903c98d5d94SLen Brown 2904c98d5d94SLen Brown void allocate_output_buffer() 2905c98d5d94SLen Brown { 29063b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 2907c98d5d94SLen Brown outp = output_buffer; 2908b2c95d90SJosh Triplett if (outp == NULL) 2909b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 2910c98d5d94SLen Brown } 2911c98d5d94SLen Brown 2912c98d5d94SLen Brown void setup_all_buffers(void) 2913c98d5d94SLen Brown { 2914c98d5d94SLen Brown topology_probe(); 2915c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 2916c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 2917c98d5d94SLen Brown allocate_output_buffer(); 2918c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 2919c98d5d94SLen Brown } 29203b4d5c7fSAndy Shevchenko 29217ce7d5deSPrarit Bhargava void set_base_cpu(void) 29227ce7d5deSPrarit Bhargava { 29237ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 29247ce7d5deSPrarit Bhargava if (base_cpu < 0) 29257ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 29267ce7d5deSPrarit Bhargava 29277ce7d5deSPrarit Bhargava if (debug > 1) 29287ce7d5deSPrarit Bhargava fprintf(stderr, "base_cpu = %d\n", base_cpu); 29297ce7d5deSPrarit Bhargava } 29307ce7d5deSPrarit Bhargava 2931103a8feaSLen Brown void turbostat_init() 2932103a8feaSLen Brown { 29337ce7d5deSPrarit Bhargava setup_all_buffers(); 29347ce7d5deSPrarit Bhargava set_base_cpu(); 2935103a8feaSLen Brown check_dev_msr(); 293698481e79SLen Brown check_permissions(); 2937fcd17211SLen Brown process_cpuid(); 2938103a8feaSLen Brown 2939103a8feaSLen Brown 2940d8af6f5fSLen Brown if (debug) 2941889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 2942889facbeSLen Brown 2943d8af6f5fSLen Brown if (debug) 29443a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 29453a9a941dSLen Brown 2946d8af6f5fSLen Brown if (debug) 2947889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 2948889facbeSLen Brown 2949889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 2950889facbeSLen Brown 2951d8af6f5fSLen Brown if (debug) 2952889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 2953103a8feaSLen Brown } 2954103a8feaSLen Brown 2955103a8feaSLen Brown int fork_it(char **argv) 2956103a8feaSLen Brown { 2957103a8feaSLen Brown pid_t child_pid; 2958d91bb17cSLen Brown int status; 2959d15cf7c1SLen Brown 2960d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 2961d91bb17cSLen Brown if (status) 2962d91bb17cSLen Brown exit(status); 2963d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 2964d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 2965103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2966103a8feaSLen Brown 2967103a8feaSLen Brown child_pid = fork(); 2968103a8feaSLen Brown if (!child_pid) { 2969103a8feaSLen Brown /* child */ 2970103a8feaSLen Brown execvp(argv[0], argv); 2971103a8feaSLen Brown } else { 2972103a8feaSLen Brown 2973103a8feaSLen Brown /* parent */ 2974b2c95d90SJosh Triplett if (child_pid == -1) 2975b2c95d90SJosh Triplett err(1, "fork"); 2976103a8feaSLen Brown 2977103a8feaSLen Brown signal(SIGINT, SIG_IGN); 2978103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 2979b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 2980b2c95d90SJosh Triplett err(status, "waitpid"); 2981103a8feaSLen Brown } 2982c98d5d94SLen Brown /* 2983c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 2984c98d5d94SLen Brown * because re-starting is problematic when forking 2985c98d5d94SLen Brown */ 2986c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 2987103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2988103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2989c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2990c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2991c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2992c98d5d94SLen Brown flush_stderr(); 2993103a8feaSLen Brown 29946eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 2995103a8feaSLen Brown 2996d91bb17cSLen Brown return status; 2997103a8feaSLen Brown } 2998103a8feaSLen Brown 29993b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 30003b4d5c7fSAndy Shevchenko { 30013b4d5c7fSAndy Shevchenko int status; 30023b4d5c7fSAndy Shevchenko 30033b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 30043b4d5c7fSAndy Shevchenko if (status) 30053b4d5c7fSAndy Shevchenko return status; 30063b4d5c7fSAndy Shevchenko 30073b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 30083b4d5c7fSAndy Shevchenko if (status) 30093b4d5c7fSAndy Shevchenko return status; 30103b4d5c7fSAndy Shevchenko 30113b4d5c7fSAndy Shevchenko flush_stdout(); 30123b4d5c7fSAndy Shevchenko 30133b4d5c7fSAndy Shevchenko return status; 30143b4d5c7fSAndy Shevchenko } 30153b4d5c7fSAndy Shevchenko 3016d8af6f5fSLen Brown void print_version() { 3017a68c7c3fSLen Brown fprintf(stderr, "turbostat version 4.7 27-May, 2015" 3018d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 3019d8af6f5fSLen Brown } 3020d8af6f5fSLen Brown 3021103a8feaSLen Brown void cmdline(int argc, char **argv) 3022103a8feaSLen Brown { 3023103a8feaSLen Brown int opt; 3024d8af6f5fSLen Brown int option_index = 0; 3025d8af6f5fSLen Brown static struct option long_options[] = { 3026d8af6f5fSLen Brown {"Counter", required_argument, 0, 'C'}, 3027d8af6f5fSLen Brown {"counter", required_argument, 0, 'c'}, 3028d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 3029d8af6f5fSLen Brown {"debug", no_argument, 0, 'd'}, 3030d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 3031d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 3032d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 3033d8af6f5fSLen Brown {"MSR", required_argument, 0, 'M'}, 3034d8af6f5fSLen Brown {"msr", required_argument, 0, 'm'}, 3035d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 3036d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 3037d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 3038d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 3039d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 3040d8af6f5fSLen Brown {0, 0, 0, 0 } 3041d8af6f5fSLen Brown }; 3042103a8feaSLen Brown 3043103a8feaSLen Brown progname = argv[0]; 3044103a8feaSLen Brown 3045d8af6f5fSLen Brown while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v", 3046d8af6f5fSLen Brown long_options, &option_index)) != -1) { 3047103a8feaSLen Brown switch (opt) { 3048d8af6f5fSLen Brown case 'C': 3049d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 3050103a8feaSLen Brown break; 3051f9240813SLen Brown case 'c': 30528e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 30538e180f3cSLen Brown break; 3054d8af6f5fSLen Brown case 'D': 3055d8af6f5fSLen Brown dump_only++; 30568e180f3cSLen Brown break; 3057d8af6f5fSLen Brown case 'd': 3058d8af6f5fSLen Brown debug++; 30592f32edf1SLen Brown break; 3060d8af6f5fSLen Brown case 'h': 3061d8af6f5fSLen Brown default: 3062d8af6f5fSLen Brown help(); 3063d8af6f5fSLen Brown exit(1); 3064d8af6f5fSLen Brown case 'i': 3065d8af6f5fSLen Brown interval_sec = atoi(optarg); 3066889facbeSLen Brown break; 30675c56be9aSDirk Brandewie case 'J': 30685c56be9aSDirk Brandewie rapl_joules++; 30695c56be9aSDirk Brandewie break; 3070d8af6f5fSLen Brown case 'M': 3071d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 3072d8af6f5fSLen Brown break; 3073d8af6f5fSLen Brown case 'm': 3074d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 3075d8af6f5fSLen Brown break; 3076d8af6f5fSLen Brown case 'P': 3077d8af6f5fSLen Brown show_pkg_only++; 3078d8af6f5fSLen Brown break; 3079d8af6f5fSLen Brown case 'p': 3080d8af6f5fSLen Brown show_core_only++; 3081d8af6f5fSLen Brown break; 3082d8af6f5fSLen Brown case 'S': 3083d8af6f5fSLen Brown summary_only++; 3084d8af6f5fSLen Brown break; 3085d8af6f5fSLen Brown case 'T': 3086d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 3087d8af6f5fSLen Brown break; 3088d8af6f5fSLen Brown case 'v': 3089d8af6f5fSLen Brown print_version(); 3090d8af6f5fSLen Brown exit(0); 3091d8af6f5fSLen Brown break; 3092103a8feaSLen Brown } 3093103a8feaSLen Brown } 3094103a8feaSLen Brown } 3095103a8feaSLen Brown 3096103a8feaSLen Brown int main(int argc, char **argv) 3097103a8feaSLen Brown { 3098103a8feaSLen Brown cmdline(argc, argv); 3099103a8feaSLen Brown 3100d8af6f5fSLen Brown if (debug) 3101d8af6f5fSLen Brown print_version(); 3102103a8feaSLen Brown 3103103a8feaSLen Brown turbostat_init(); 3104103a8feaSLen Brown 31053b4d5c7fSAndy Shevchenko /* dump counters and exit */ 31063b4d5c7fSAndy Shevchenko if (dump_only) 31073b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 31083b4d5c7fSAndy Shevchenko 3109103a8feaSLen Brown /* 3110103a8feaSLen Brown * if any params left, it must be a command to fork 3111103a8feaSLen Brown */ 3112103a8feaSLen Brown if (argc - optind) 3113103a8feaSLen Brown return fork_it(argv + optind); 3114103a8feaSLen Brown else 3115103a8feaSLen Brown turbostat_loop(); 3116103a8feaSLen Brown 3117103a8feaSLen Brown return 0; 3118103a8feaSLen Brown } 3119