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; 55ee7e38e3SLen Brown unsigned int do_pc2; 56ee7e38e3SLen Brown unsigned int do_pc3; 57ee7e38e3SLen Brown unsigned int do_pc6; 58ee7e38e3SLen Brown unsigned int do_pc7; 59ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10; 60144b44b1SLen Brown unsigned int do_slm_cstates; 61144b44b1SLen Brown unsigned int use_c1_residency_msr; 62103a8feaSLen Brown unsigned int has_aperf; 63889facbeSLen Brown unsigned int has_epb; 64fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 65103a8feaSLen Brown unsigned int genuine_intel; 66103a8feaSLen Brown unsigned int has_invariant_tsc; 67d7899447SLen Brown unsigned int do_nhm_platform_info; 68d7899447SLen Brown unsigned int do_nhm_turbo_ratio_limit; 696574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit; 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; 87889facbeSLen Brown double rapl_power_units, rapl_energy_units, rapl_time_units; 88889facbeSLen Brown double rapl_joule_counter_range; 893a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 903a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 913a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 92889facbeSLen Brown 93889facbeSLen Brown #define RAPL_PKG (1 << 0) 94e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 95e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 96e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 97e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 98e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 99e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 100e6f9bb3cSLen Brown 101889facbeSLen Brown #define RAPL_DRAM (1 << 3) 102e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 103e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 104e6f9bb3cSLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 105e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 106e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 107e6f9bb3cSLen Brown 108e6f9bb3cSLen Brown #define RAPL_CORES (1 << 5) 109e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 110e6f9bb3cSLen Brown /* 0x639 MSR_PP0_ENERGY_STATUS */ 111e6f9bb3cSLen Brown #define RAPL_CORE_POLICY (1 << 6) 112e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 113e6f9bb3cSLen Brown 114e6f9bb3cSLen Brown 115e6f9bb3cSLen Brown #define RAPL_GFX (1 << 7) 116e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 117e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 118e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 119889facbeSLen Brown #define TJMAX_DEFAULT 100 120889facbeSLen Brown 121889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 122103a8feaSLen Brown 123103a8feaSLen Brown int aperf_mperf_unstable; 124103a8feaSLen Brown int backwards_count; 125103a8feaSLen Brown char *progname; 126103a8feaSLen Brown 127c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 128c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 129c98d5d94SLen Brown 130c98d5d94SLen Brown struct thread_data { 131c98d5d94SLen Brown unsigned long long tsc; 132c98d5d94SLen Brown unsigned long long aperf; 133c98d5d94SLen Brown unsigned long long mperf; 134144b44b1SLen Brown unsigned long long c1; 1352f32edf1SLen Brown unsigned long long extra_msr64; 1368e180f3cSLen Brown unsigned long long extra_delta64; 1378e180f3cSLen Brown unsigned long long extra_msr32; 1388e180f3cSLen Brown unsigned long long extra_delta32; 1391ed51011SLen Brown unsigned int smi_count; 140c98d5d94SLen Brown unsigned int cpu_id; 141c98d5d94SLen Brown unsigned int flags; 142c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 143c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 144c98d5d94SLen Brown } *thread_even, *thread_odd; 145c98d5d94SLen Brown 146c98d5d94SLen Brown struct core_data { 147c98d5d94SLen Brown unsigned long long c3; 148c98d5d94SLen Brown unsigned long long c6; 149c98d5d94SLen Brown unsigned long long c7; 150889facbeSLen Brown unsigned int core_temp_c; 151c98d5d94SLen Brown unsigned int core_id; 152c98d5d94SLen Brown } *core_even, *core_odd; 153c98d5d94SLen Brown 154c98d5d94SLen Brown struct pkg_data { 155c98d5d94SLen Brown unsigned long long pc2; 156c98d5d94SLen Brown unsigned long long pc3; 157c98d5d94SLen Brown unsigned long long pc6; 158c98d5d94SLen Brown unsigned long long pc7; 159ca58710fSKristen Carlson Accardi unsigned long long pc8; 160ca58710fSKristen Carlson Accardi unsigned long long pc9; 161ca58710fSKristen Carlson Accardi unsigned long long pc10; 162c98d5d94SLen Brown unsigned int package_id; 163889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 164889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 165889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 166889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 167889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 168889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 169889facbeSLen Brown unsigned int pkg_temp_c; 170889facbeSLen Brown 171c98d5d94SLen Brown } *package_even, *package_odd; 172c98d5d94SLen Brown 173c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 174c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 175c98d5d94SLen Brown 176c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 177c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 178c98d5d94SLen Brown topo.num_threads_per_core + \ 179c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 180c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 181c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 182c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 183c98d5d94SLen Brown 184c98d5d94SLen Brown struct system_summary { 185c98d5d94SLen Brown struct thread_data threads; 186c98d5d94SLen Brown struct core_data cores; 187c98d5d94SLen Brown struct pkg_data packages; 188c98d5d94SLen Brown } sum, average; 189c98d5d94SLen Brown 190c98d5d94SLen Brown 191c98d5d94SLen Brown struct topo_params { 192c98d5d94SLen Brown int num_packages; 193103a8feaSLen Brown int num_cpus; 194c98d5d94SLen Brown int num_cores; 195c98d5d94SLen Brown int max_cpu_num; 196c98d5d94SLen Brown int num_cores_per_pkg; 197c98d5d94SLen Brown int num_threads_per_core; 198c98d5d94SLen Brown } topo; 199103a8feaSLen Brown 200c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 201103a8feaSLen Brown 202c98d5d94SLen Brown void setup_all_buffers(void); 203103a8feaSLen Brown 204c98d5d94SLen Brown int cpu_is_not_present(int cpu) 205d15cf7c1SLen Brown { 206c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 207c98d5d94SLen Brown } 208c98d5d94SLen Brown /* 209c98d5d94SLen Brown * run func(thread, core, package) in topology order 210c98d5d94SLen Brown * skip non-present cpus 211c98d5d94SLen Brown */ 212c98d5d94SLen Brown 213c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 214c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 215c98d5d94SLen Brown { 216c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 217c98d5d94SLen Brown 218c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 219c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 220c98d5d94SLen Brown for (thread_no = 0; thread_no < 221c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 222c98d5d94SLen Brown struct thread_data *t; 223c98d5d94SLen Brown struct core_data *c; 224c98d5d94SLen Brown struct pkg_data *p; 225c98d5d94SLen Brown 226c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 227c98d5d94SLen Brown 228c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 229c98d5d94SLen Brown continue; 230c98d5d94SLen Brown 231c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 232c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 233c98d5d94SLen Brown 234c98d5d94SLen Brown retval = func(t, c, p); 235c98d5d94SLen Brown if (retval) 236c98d5d94SLen Brown return retval; 237c98d5d94SLen Brown } 238c98d5d94SLen Brown } 239c98d5d94SLen Brown } 240d15cf7c1SLen Brown return 0; 241d15cf7c1SLen Brown } 242d15cf7c1SLen Brown 24388c3281fSLen Brown int cpu_migrate(int cpu) 24488c3281fSLen Brown { 245c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 246c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 247c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 24888c3281fSLen Brown return -1; 24988c3281fSLen Brown else 25088c3281fSLen Brown return 0; 25188c3281fSLen Brown } 25288c3281fSLen Brown 25315aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 254103a8feaSLen Brown { 255103a8feaSLen Brown ssize_t retval; 256103a8feaSLen Brown char pathname[32]; 257103a8feaSLen Brown int fd; 258103a8feaSLen Brown 259103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 260103a8feaSLen Brown fd = open(pathname, O_RDONLY); 26115aaa346SLen Brown if (fd < 0) 26298481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 263103a8feaSLen Brown 26415aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 265103a8feaSLen Brown close(fd); 26615aaa346SLen Brown 26798481e79SLen Brown if (retval != sizeof *msr) 26898481e79SLen Brown err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset); 26915aaa346SLen Brown 27015aaa346SLen Brown return 0; 271103a8feaSLen Brown } 272103a8feaSLen Brown 273fc04cc67SLen Brown /* 274fc04cc67SLen Brown * Example Format w/ field column widths: 275fc04cc67SLen Brown * 276fc04cc67SLen 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 277e7c95ff3SLen Brown * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 278fc04cc67SLen Brown */ 279fc04cc67SLen Brown 280a829eb4dSLen Brown void print_header(void) 281103a8feaSLen Brown { 282103a8feaSLen Brown if (show_pkg) 283fc04cc67SLen Brown outp += sprintf(outp, " Package"); 284103a8feaSLen Brown if (show_core) 285fc04cc67SLen Brown outp += sprintf(outp, " Core"); 286103a8feaSLen Brown if (show_cpu) 287c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 288103a8feaSLen Brown if (has_aperf) 289fc04cc67SLen Brown outp += sprintf(outp, " Avg_MHz"); 290d7899447SLen Brown if (has_aperf) 291fc04cc67SLen Brown outp += sprintf(outp, " %%Busy"); 292fc04cc67SLen Brown if (has_aperf) 293fc04cc67SLen Brown outp += sprintf(outp, " Bzy_MHz"); 294fc04cc67SLen Brown outp += sprintf(outp, " TSC_MHz"); 2951cc21f7bSLen Brown 2968e180f3cSLen Brown if (extra_delta_offset32) 297f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 2988e180f3cSLen Brown if (extra_delta_offset64) 299f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 3002f32edf1SLen Brown if (extra_msr_offset32) 3018e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 3022f32edf1SLen Brown if (extra_msr_offset64) 3038e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 3041cc21f7bSLen Brown 3051cc21f7bSLen Brown if (!debug) 3061cc21f7bSLen Brown goto done; 3071cc21f7bSLen Brown 3081cc21f7bSLen Brown if (do_smi) 3091cc21f7bSLen Brown outp += sprintf(outp, " SMI"); 3101cc21f7bSLen Brown 311103a8feaSLen Brown if (do_nhm_cstates) 312fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c1"); 313144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 314fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c3"); 315103a8feaSLen Brown if (do_nhm_cstates) 316fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c6"); 317103a8feaSLen Brown if (do_snb_cstates) 318fc04cc67SLen Brown outp += sprintf(outp, " CPU%%c7"); 319889facbeSLen Brown 320889facbeSLen Brown if (do_dts) 321fc04cc67SLen Brown outp += sprintf(outp, " CoreTmp"); 322889facbeSLen Brown if (do_ptm) 323fc04cc67SLen Brown outp += sprintf(outp, " PkgTmp"); 324889facbeSLen Brown 325ee7e38e3SLen Brown if (do_pc2) 326fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc2"); 327ee7e38e3SLen Brown if (do_pc3) 328fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc3"); 329ee7e38e3SLen Brown if (do_pc6) 330fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc6"); 331ee7e38e3SLen Brown if (do_pc7) 332fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc7"); 333ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 334fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc8"); 335fc04cc67SLen Brown outp += sprintf(outp, " Pkg%%pc9"); 336fc04cc67SLen Brown outp += sprintf(outp, " Pk%%pc10"); 337ca58710fSKristen Carlson Accardi } 338103a8feaSLen Brown 3395c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 340889facbeSLen Brown if (do_rapl & RAPL_PKG) 341fc04cc67SLen Brown outp += sprintf(outp, " PkgWatt"); 342889facbeSLen Brown if (do_rapl & RAPL_CORES) 343fc04cc67SLen Brown outp += sprintf(outp, " CorWatt"); 344889facbeSLen Brown if (do_rapl & RAPL_GFX) 345fc04cc67SLen Brown outp += sprintf(outp, " GFXWatt"); 346889facbeSLen Brown if (do_rapl & RAPL_DRAM) 347fc04cc67SLen Brown outp += sprintf(outp, " RAMWatt"); 348889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 349889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 350889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 351889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 352d7899447SLen Brown } else if (do_rapl && rapl_joules) { 3535c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 3545c56be9aSDirk Brandewie outp += sprintf(outp, " Pkg_J"); 3555c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 3565c56be9aSDirk Brandewie outp += sprintf(outp, " Cor_J"); 3575c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 3585c56be9aSDirk Brandewie outp += sprintf(outp, " GFX_J"); 3595c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 3605c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_W"); 3615c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 3625c56be9aSDirk Brandewie outp += sprintf(outp, " PKG_%%"); 3635c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 3645c56be9aSDirk Brandewie outp += sprintf(outp, " RAM_%%"); 3655c56be9aSDirk Brandewie outp += sprintf(outp, " time"); 366889facbeSLen Brown 3675c56be9aSDirk Brandewie } 3681cc21f7bSLen Brown done: 369c98d5d94SLen Brown outp += sprintf(outp, "\n"); 370103a8feaSLen Brown } 371103a8feaSLen Brown 372c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 373c98d5d94SLen Brown struct pkg_data *p) 374103a8feaSLen Brown { 3753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 376c98d5d94SLen Brown 377c98d5d94SLen Brown if (t) { 3783b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 3793b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 3803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 3813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 3823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 3833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 3843b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 3858e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 3863b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 3878e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 3883b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %08llX\n", 3892f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 3903b4d5c7fSAndy Shevchenko outp += sprintf(outp, "msr0x%x: %016llX\n", 3912f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 3921ed51011SLen Brown if (do_smi) 3933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 394103a8feaSLen Brown } 395103a8feaSLen Brown 396c98d5d94SLen Brown if (c) { 3973b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 3983b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 3993b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 4003b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 4013b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 402c98d5d94SLen Brown } 403103a8feaSLen Brown 404c98d5d94SLen Brown if (p) { 4053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 4063b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 407ee7e38e3SLen Brown if (do_pc3) 4083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 409ee7e38e3SLen Brown if (do_pc6) 4103b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 411ee7e38e3SLen Brown if (do_pc7) 4123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 4133b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 4143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 4153b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 4163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 4173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 4183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 4193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 4203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 4213b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 4223b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 4233b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 4243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 425c98d5d94SLen Brown } 4263b4d5c7fSAndy Shevchenko 4273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 4283b4d5c7fSAndy Shevchenko 429c98d5d94SLen Brown return 0; 430103a8feaSLen Brown } 431103a8feaSLen Brown 432e23da037SLen Brown /* 433e23da037SLen Brown * column formatting convention & formats 434e23da037SLen Brown */ 435c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 436c98d5d94SLen Brown struct pkg_data *p) 437103a8feaSLen Brown { 438103a8feaSLen Brown double interval_float; 439fc04cc67SLen Brown char *fmt8; 440103a8feaSLen Brown 441c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 442c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 443c98d5d94SLen Brown return 0; 444c98d5d94SLen Brown 445c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 446c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 447c98d5d94SLen Brown return 0; 448c98d5d94SLen Brown 449103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 450103a8feaSLen Brown 451c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 452c98d5d94SLen Brown if (t == &average.threads) { 453103a8feaSLen Brown if (show_pkg) 454fc04cc67SLen Brown outp += sprintf(outp, " -"); 455103a8feaSLen Brown if (show_core) 456fc04cc67SLen Brown outp += sprintf(outp, " -"); 457103a8feaSLen Brown if (show_cpu) 458fc04cc67SLen Brown outp += sprintf(outp, " -"); 459103a8feaSLen Brown } else { 460c98d5d94SLen Brown if (show_pkg) { 461c98d5d94SLen Brown if (p) 462fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->package_id); 463c98d5d94SLen Brown else 464fc04cc67SLen Brown outp += sprintf(outp, " -"); 465c98d5d94SLen Brown } 466c98d5d94SLen Brown if (show_core) { 467c98d5d94SLen Brown if (c) 468fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_id); 469c98d5d94SLen Brown else 470fc04cc67SLen Brown outp += sprintf(outp, " -"); 471c98d5d94SLen Brown } 472103a8feaSLen Brown if (show_cpu) 473fc04cc67SLen Brown outp += sprintf(outp, "%8d", t->cpu_id); 474103a8feaSLen Brown } 475fc04cc67SLen Brown 476d7899447SLen Brown /* Avg_MHz */ 477fc04cc67SLen Brown if (has_aperf) 478fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 479fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 480fc04cc67SLen Brown 481d7899447SLen Brown /* %Busy */ 482d7899447SLen Brown if (has_aperf) { 483103a8feaSLen Brown if (!skip_c0) 484fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc); 485103a8feaSLen Brown else 486fc04cc67SLen Brown outp += sprintf(outp, "********"); 487103a8feaSLen Brown } 488103a8feaSLen Brown 489d7899447SLen Brown /* Bzy_MHz */ 490fc04cc67SLen Brown if (has_aperf) 491fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 492fc04cc67SLen Brown 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); 493103a8feaSLen Brown 494d7899447SLen Brown /* TSC_MHz */ 495fc04cc67SLen Brown outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float); 496103a8feaSLen Brown 4978e180f3cSLen Brown /* delta */ 4988e180f3cSLen Brown if (extra_delta_offset32) 4998e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 5008e180f3cSLen Brown 5018e180f3cSLen Brown /* DELTA */ 5028e180f3cSLen Brown if (extra_delta_offset64) 5038e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 5042f32edf1SLen Brown /* msr */ 5052f32edf1SLen Brown if (extra_msr_offset32) 5068e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 5072f32edf1SLen Brown 508130ff304SLen Brown /* MSR */ 5092f32edf1SLen Brown if (extra_msr_offset64) 5102f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 511130ff304SLen Brown 5121cc21f7bSLen Brown if (!debug) 5131cc21f7bSLen Brown goto done; 5141cc21f7bSLen Brown 5151cc21f7bSLen Brown /* SMI */ 5161cc21f7bSLen Brown if (do_smi) 5171cc21f7bSLen Brown outp += sprintf(outp, "%8d", t->smi_count); 5181cc21f7bSLen Brown 519103a8feaSLen Brown if (do_nhm_cstates) { 520103a8feaSLen Brown if (!skip_c1) 521fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc); 522103a8feaSLen Brown else 523fc04cc67SLen Brown outp += sprintf(outp, "********"); 524103a8feaSLen Brown } 525c98d5d94SLen Brown 526c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 527c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 528c98d5d94SLen Brown goto done; 529c98d5d94SLen Brown 530144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 531fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc); 532103a8feaSLen Brown if (do_nhm_cstates) 533fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc); 534103a8feaSLen Brown if (do_snb_cstates) 535fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc); 536c98d5d94SLen Brown 537889facbeSLen Brown if (do_dts) 538fc04cc67SLen Brown outp += sprintf(outp, "%8d", c->core_temp_c); 539889facbeSLen Brown 540c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 541c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 542c98d5d94SLen Brown goto done; 543c98d5d94SLen Brown 544889facbeSLen Brown if (do_ptm) 545fc04cc67SLen Brown outp += sprintf(outp, "%8d", p->pkg_temp_c); 546889facbeSLen Brown 547ee7e38e3SLen Brown if (do_pc2) 548fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc); 549ee7e38e3SLen Brown if (do_pc3) 550fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc); 551ee7e38e3SLen Brown if (do_pc6) 552fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc); 553ee7e38e3SLen Brown if (do_pc7) 554fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc); 555ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 556fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc); 557fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc); 558fc04cc67SLen Brown outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc); 559ca58710fSKristen Carlson Accardi } 560889facbeSLen Brown 561889facbeSLen Brown /* 562889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 563889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 564889facbeSLen Brown */ 565fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 566fc04cc67SLen Brown fmt8 = "%8.2f"; 567fc04cc67SLen Brown else 568fc04cc67SLen Brown fmt8 = " %6.0f**"; 569889facbeSLen Brown 5705c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 571889facbeSLen Brown if (do_rapl & RAPL_PKG) 572fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 573889facbeSLen Brown if (do_rapl & RAPL_CORES) 574fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 575889facbeSLen Brown if (do_rapl & RAPL_GFX) 576fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 577889facbeSLen Brown if (do_rapl & RAPL_DRAM) 578fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float); 579889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 580fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 581889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 582fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 583d7899447SLen Brown } else if (do_rapl && rapl_joules) { 5845c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG) 585fc04cc67SLen Brown outp += sprintf(outp, fmt8, 5865c56be9aSDirk Brandewie p->energy_pkg * rapl_energy_units); 5875c56be9aSDirk Brandewie if (do_rapl & RAPL_CORES) 588fc04cc67SLen Brown outp += sprintf(outp, fmt8, 5895c56be9aSDirk Brandewie p->energy_cores * rapl_energy_units); 5905c56be9aSDirk Brandewie if (do_rapl & RAPL_GFX) 591fc04cc67SLen Brown outp += sprintf(outp, fmt8, 5925c56be9aSDirk Brandewie p->energy_gfx * rapl_energy_units); 5935c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM) 594fc04cc67SLen Brown outp += sprintf(outp, fmt8, 5955c56be9aSDirk Brandewie p->energy_dram * rapl_energy_units); 5965c56be9aSDirk Brandewie if (do_rapl & RAPL_PKG_PERF_STATUS) 597fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 5985c56be9aSDirk Brandewie if (do_rapl & RAPL_DRAM_PERF_STATUS) 599fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 600889facbeSLen Brown 601d7899447SLen Brown outp += sprintf(outp, fmt8, interval_float); 6025c56be9aSDirk Brandewie } 603c98d5d94SLen Brown done: 604c98d5d94SLen Brown outp += sprintf(outp, "\n"); 605c98d5d94SLen Brown 606c98d5d94SLen Brown return 0; 607103a8feaSLen Brown } 608103a8feaSLen Brown 609c98d5d94SLen Brown void flush_stdout() 610103a8feaSLen Brown { 611c98d5d94SLen Brown fputs(output_buffer, stdout); 612ddac0d68SLen Brown fflush(stdout); 613c98d5d94SLen Brown outp = output_buffer; 614c98d5d94SLen Brown } 615c98d5d94SLen Brown void flush_stderr() 616c98d5d94SLen Brown { 617c98d5d94SLen Brown fputs(output_buffer, stderr); 618c98d5d94SLen Brown outp = output_buffer; 619c98d5d94SLen Brown } 620c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 621c98d5d94SLen Brown { 622e23da037SLen Brown static int printed; 623103a8feaSLen Brown 624e23da037SLen Brown if (!printed || !summary_only) 625103a8feaSLen Brown print_header(); 626103a8feaSLen Brown 627c98d5d94SLen Brown if (topo.num_cpus > 1) 628c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 629c98d5d94SLen Brown &average.packages); 630103a8feaSLen Brown 631e23da037SLen Brown printed = 1; 632e23da037SLen Brown 633e23da037SLen Brown if (summary_only) 634e23da037SLen Brown return; 635e23da037SLen Brown 636c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 637103a8feaSLen Brown } 638103a8feaSLen Brown 639889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 640889facbeSLen Brown if (new > old) { \ 641889facbeSLen Brown old = new - old; \ 642889facbeSLen Brown } else { \ 643889facbeSLen Brown old = 0x100000000 + new - old; \ 644889facbeSLen Brown } 645889facbeSLen Brown 646c98d5d94SLen Brown void 647c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 648103a8feaSLen Brown { 649c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 650ee7e38e3SLen Brown if (do_pc3) 651c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 652ee7e38e3SLen Brown if (do_pc6) 653c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 654ee7e38e3SLen Brown if (do_pc7) 655c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 656ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 657ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 658ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 659889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 660889facbeSLen Brown 661889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 662889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 663889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 664889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 665889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 666889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 667103a8feaSLen Brown } 668103a8feaSLen Brown 669c98d5d94SLen Brown void 670c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 671c98d5d94SLen Brown { 672c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 673c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 674c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 675889facbeSLen Brown old->core_temp_c = new->core_temp_c; 676103a8feaSLen Brown } 677c98d5d94SLen Brown 678c3ae331dSLen Brown /* 679c3ae331dSLen Brown * old = new - old 680c3ae331dSLen Brown */ 681c98d5d94SLen Brown void 682c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 683c98d5d94SLen Brown struct core_data *core_delta) 684c98d5d94SLen Brown { 685c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 686c98d5d94SLen Brown 687103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 688b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 689b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 690b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 691b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 692103a8feaSLen Brown 693c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 694c98d5d94SLen Brown 695a729617cSLen Brown if (has_aperf) { 696c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 697c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 698c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 699c98d5d94SLen Brown } else { 700c98d5d94SLen Brown 701103a8feaSLen Brown if (!aperf_mperf_unstable) { 702103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 703103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 704103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 705103a8feaSLen Brown 706103a8feaSLen Brown aperf_mperf_unstable = 1; 707103a8feaSLen Brown } 708103a8feaSLen Brown /* 709103a8feaSLen Brown * mperf delta is likely a huge "positive" number 710103a8feaSLen Brown * can not use it for calculating c0 time 711103a8feaSLen Brown */ 712103a8feaSLen Brown skip_c0 = 1; 713103a8feaSLen Brown skip_c1 = 1; 714103a8feaSLen Brown } 715a729617cSLen Brown } 716103a8feaSLen Brown 717c98d5d94SLen Brown 718144b44b1SLen Brown if (use_c1_residency_msr) { 719144b44b1SLen Brown /* 720144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 721144b44b1SLen Brown * which should be more accurate than the derivation below. 722144b44b1SLen Brown */ 723144b44b1SLen Brown } else { 724103a8feaSLen Brown /* 725c3ae331dSLen Brown * As counter collection is not atomic, 726c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 727103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 728103a8feaSLen Brown */ 729c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 730c98d5d94SLen Brown old->c1 = 0; 731c98d5d94SLen Brown else { 732c98d5d94SLen Brown /* normal case, derive c1 */ 733c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 734c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 735c98d5d94SLen Brown } 736144b44b1SLen Brown } 737c3ae331dSLen Brown 738c98d5d94SLen Brown if (old->mperf == 0) { 739d8af6f5fSLen Brown if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 740c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 741c98d5d94SLen Brown } 742103a8feaSLen Brown 7438e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 7448e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 7458e180f3cSLen Brown 7468e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 7478e180f3cSLen Brown 748103a8feaSLen Brown /* 7498e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 750103a8feaSLen Brown */ 7512f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 7522f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 7531ed51011SLen Brown 7541ed51011SLen Brown if (do_smi) 7551ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 756103a8feaSLen Brown } 757c98d5d94SLen Brown 758c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 759c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 760c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 761c98d5d94SLen Brown { 762c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 763c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 764c98d5d94SLen Brown delta_core(c, c2); 765c98d5d94SLen Brown 766c98d5d94SLen Brown /* always calculate thread delta */ 767c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 768c98d5d94SLen Brown 769c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 770c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 771c98d5d94SLen Brown delta_package(p, p2); 772c98d5d94SLen Brown 773103a8feaSLen Brown return 0; 774103a8feaSLen Brown } 775103a8feaSLen Brown 776c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 777103a8feaSLen Brown { 778c98d5d94SLen Brown t->tsc = 0; 779c98d5d94SLen Brown t->aperf = 0; 780c98d5d94SLen Brown t->mperf = 0; 781c98d5d94SLen Brown t->c1 = 0; 782103a8feaSLen Brown 7831ed51011SLen Brown t->smi_count = 0; 7848e180f3cSLen Brown t->extra_delta32 = 0; 7858e180f3cSLen Brown t->extra_delta64 = 0; 7868e180f3cSLen Brown 787c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 788c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 789c98d5d94SLen Brown 790c98d5d94SLen Brown c->c3 = 0; 791c98d5d94SLen Brown c->c6 = 0; 792c98d5d94SLen Brown c->c7 = 0; 793889facbeSLen Brown c->core_temp_c = 0; 794c98d5d94SLen Brown 795c98d5d94SLen Brown p->pc2 = 0; 796ee7e38e3SLen Brown if (do_pc3) 797c98d5d94SLen Brown p->pc3 = 0; 798ee7e38e3SLen Brown if (do_pc6) 799c98d5d94SLen Brown p->pc6 = 0; 800ee7e38e3SLen Brown if (do_pc7) 801c98d5d94SLen Brown p->pc7 = 0; 802ca58710fSKristen Carlson Accardi p->pc8 = 0; 803ca58710fSKristen Carlson Accardi p->pc9 = 0; 804ca58710fSKristen Carlson Accardi p->pc10 = 0; 805889facbeSLen Brown 806889facbeSLen Brown p->energy_pkg = 0; 807889facbeSLen Brown p->energy_dram = 0; 808889facbeSLen Brown p->energy_cores = 0; 809889facbeSLen Brown p->energy_gfx = 0; 810889facbeSLen Brown p->rapl_pkg_perf_status = 0; 811889facbeSLen Brown p->rapl_dram_perf_status = 0; 812889facbeSLen Brown p->pkg_temp_c = 0; 813103a8feaSLen Brown } 814c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 815c98d5d94SLen Brown struct pkg_data *p) 816103a8feaSLen Brown { 817c98d5d94SLen Brown average.threads.tsc += t->tsc; 818c98d5d94SLen Brown average.threads.aperf += t->aperf; 819c98d5d94SLen Brown average.threads.mperf += t->mperf; 820c98d5d94SLen Brown average.threads.c1 += t->c1; 82115aaa346SLen Brown 8228e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 8238e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 8248e180f3cSLen Brown 825c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 826c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 827c98d5d94SLen Brown return 0; 828c98d5d94SLen Brown 829c98d5d94SLen Brown average.cores.c3 += c->c3; 830c98d5d94SLen Brown average.cores.c6 += c->c6; 831c98d5d94SLen Brown average.cores.c7 += c->c7; 832c98d5d94SLen Brown 833889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 834889facbeSLen Brown 835c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 836c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 837c98d5d94SLen Brown return 0; 838c98d5d94SLen Brown 839c98d5d94SLen Brown average.packages.pc2 += p->pc2; 840ee7e38e3SLen Brown if (do_pc3) 841c98d5d94SLen Brown average.packages.pc3 += p->pc3; 842ee7e38e3SLen Brown if (do_pc6) 843c98d5d94SLen Brown average.packages.pc6 += p->pc6; 844ee7e38e3SLen Brown if (do_pc7) 845c98d5d94SLen Brown average.packages.pc7 += p->pc7; 846ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 847ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 848ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 849c98d5d94SLen Brown 850889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 851889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 852889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 853889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 854889facbeSLen Brown 855889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 856889facbeSLen Brown 857889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 858889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 859c98d5d94SLen Brown return 0; 860c98d5d94SLen Brown } 861c98d5d94SLen Brown /* 862c98d5d94SLen Brown * sum the counters for all cpus in the system 863c98d5d94SLen Brown * compute the weighted average 864c98d5d94SLen Brown */ 865c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 866c98d5d94SLen Brown struct pkg_data *p) 867c98d5d94SLen Brown { 868c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 869c98d5d94SLen Brown 870c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 871c98d5d94SLen Brown 872c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 873c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 874c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 875c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 876c98d5d94SLen Brown 8778e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 8788e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 8798e180f3cSLen Brown 8808e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 8818e180f3cSLen Brown 882c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 883c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 884c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 885c98d5d94SLen Brown 886c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 887ee7e38e3SLen Brown if (do_pc3) 888c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 889ee7e38e3SLen Brown if (do_pc6) 890c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 891ee7e38e3SLen Brown if (do_pc7) 892c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 893ca58710fSKristen Carlson Accardi 894ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 895ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 896ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 897c98d5d94SLen Brown } 898c98d5d94SLen Brown 899c98d5d94SLen Brown static unsigned long long rdtsc(void) 900c98d5d94SLen Brown { 901c98d5d94SLen Brown unsigned int low, high; 902c98d5d94SLen Brown 903c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 904c98d5d94SLen Brown 905c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 906c98d5d94SLen Brown } 907c98d5d94SLen Brown 908c98d5d94SLen Brown 909c98d5d94SLen Brown /* 910c98d5d94SLen Brown * get_counters(...) 911c98d5d94SLen Brown * migrate to cpu 912c98d5d94SLen Brown * acquire and record local counters for that cpu 913c98d5d94SLen Brown */ 914c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 915c98d5d94SLen Brown { 916c98d5d94SLen Brown int cpu = t->cpu_id; 917889facbeSLen Brown unsigned long long msr; 918c98d5d94SLen Brown 919e52966c0SLen Brown if (cpu_migrate(cpu)) { 920e52966c0SLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 92115aaa346SLen Brown return -1; 922e52966c0SLen Brown } 92315aaa346SLen Brown 924c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 92515aaa346SLen Brown 92615aaa346SLen Brown if (has_aperf) { 9279c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 928c98d5d94SLen Brown return -3; 9299c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 930c98d5d94SLen Brown return -4; 93188c3281fSLen Brown } 93288c3281fSLen Brown 9331ed51011SLen Brown if (do_smi) { 9341ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 9351ed51011SLen Brown return -5; 9361ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 9371ed51011SLen Brown } 9388e180f3cSLen Brown if (extra_delta_offset32) { 939889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 9402f32edf1SLen Brown return -5; 941889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 9428e180f3cSLen Brown } 9438e180f3cSLen Brown 9448e180f3cSLen Brown if (extra_delta_offset64) 9458e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 9468e180f3cSLen Brown return -5; 9478e180f3cSLen Brown 9488e180f3cSLen Brown if (extra_msr_offset32) { 949889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 9508e180f3cSLen Brown return -5; 951889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 9528e180f3cSLen Brown } 9532f32edf1SLen Brown 9542f32edf1SLen Brown if (extra_msr_offset64) 9552f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 956c98d5d94SLen Brown return -5; 957c98d5d94SLen Brown 958144b44b1SLen Brown if (use_c1_residency_msr) { 959144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 960144b44b1SLen Brown return -6; 961144b44b1SLen Brown } 962144b44b1SLen Brown 963c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 964c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 965c98d5d94SLen Brown return 0; 966c98d5d94SLen Brown 967144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) { 968c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 969c98d5d94SLen Brown return -6; 970144b44b1SLen Brown } 971144b44b1SLen Brown 972144b44b1SLen Brown if (do_nhm_cstates) { 973c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 974c98d5d94SLen Brown return -7; 975103a8feaSLen Brown } 97615aaa346SLen Brown 97715aaa346SLen Brown if (do_snb_cstates) 978c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 979c98d5d94SLen Brown return -8; 980c98d5d94SLen Brown 981889facbeSLen Brown if (do_dts) { 982889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 983889facbeSLen Brown return -9; 984889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 985889facbeSLen Brown } 986889facbeSLen Brown 987889facbeSLen Brown 988c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 989c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 990c98d5d94SLen Brown return 0; 99115aaa346SLen Brown 992ee7e38e3SLen Brown if (do_pc3) 993c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 994c98d5d94SLen Brown return -9; 995ee7e38e3SLen Brown if (do_pc6) 996c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 997c98d5d94SLen Brown return -10; 998ee7e38e3SLen Brown if (do_pc2) 999c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1000c98d5d94SLen Brown return -11; 1001ee7e38e3SLen Brown if (do_pc7) 1002c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1003c98d5d94SLen Brown return -12; 1004ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 1005ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1006ca58710fSKristen Carlson Accardi return -13; 1007ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1008ca58710fSKristen Carlson Accardi return -13; 1009ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1010ca58710fSKristen Carlson Accardi return -13; 1011ca58710fSKristen Carlson Accardi } 1012889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1013889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1014889facbeSLen Brown return -13; 1015889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1016889facbeSLen Brown } 1017889facbeSLen Brown if (do_rapl & RAPL_CORES) { 1018889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1019889facbeSLen Brown return -14; 1020889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1021889facbeSLen Brown } 1022889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1023889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1024889facbeSLen Brown return -15; 1025889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1026889facbeSLen Brown } 1027889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1028889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1029889facbeSLen Brown return -16; 1030889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1031889facbeSLen Brown } 1032889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1033889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1034889facbeSLen Brown return -16; 1035889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1036889facbeSLen Brown } 1037889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1038889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1039889facbeSLen Brown return -16; 1040889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1041889facbeSLen Brown } 1042889facbeSLen Brown if (do_ptm) { 1043889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1044889facbeSLen Brown return -17; 1045889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1046889facbeSLen Brown } 104715aaa346SLen Brown return 0; 1048103a8feaSLen Brown } 1049103a8feaSLen Brown 1050ee7e38e3SLen Brown /* 1051ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1052ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1053ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1054ee7e38e3SLen Brown */ 1055ee7e38e3SLen Brown 1056ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1057ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1058ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1059ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1060ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1061ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1062ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1063ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1064ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1065ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1066ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1067ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 1068ee7e38e3SLen Brown #define PCLUNL 12 /* Unlimited */ 1069ee7e38e3SLen Brown 1070ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1071ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 1072ee7e38e3SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "unlimited"}; 1073ee7e38e3SLen Brown 1074ee7e38e3SLen Brown int nhm_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL}; 1075ee7e38e3SLen Brown int snb_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL}; 1076ee7e38e3SLen Brown int hsw_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCLRSV, PCLUNL}; 1077ee7e38e3SLen Brown int slv_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7}; 1078ee7e38e3SLen Brown int amt_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7}; 1079ee7e38e3SLen Brown int phi_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL}; 1080ee7e38e3SLen Brown 10811cc21f7bSLen Brown void dump_system_config_info(void) 1082103a8feaSLen Brown { 1083103a8feaSLen Brown unsigned long long msr; 1084103a8feaSLen Brown unsigned int ratio; 1085103a8feaSLen Brown 1086d7899447SLen Brown if (!do_nhm_platform_info) 1087103a8feaSLen Brown return; 1088103a8feaSLen Brown 10899c63a650SLen Brown get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); 1090103a8feaSLen Brown 1091889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr); 10926574a5d5SLen Brown 1093103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1094103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 1095103a8feaSLen Brown ratio, bclk, ratio * bclk); 1096103a8feaSLen Brown 1097103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1098103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 1099103a8feaSLen Brown ratio, bclk, ratio * bclk); 1100103a8feaSLen Brown 110167920418SLen Brown get_msr(0, MSR_IA32_POWER_CTL, &msr); 1102144b44b1SLen Brown fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 110367920418SLen Brown msr, msr & 0x2 ? "EN" : "DIS"); 110467920418SLen Brown 11056574a5d5SLen Brown if (!do_ivt_turbo_ratio_limit) 11066574a5d5SLen Brown goto print_nhm_turbo_ratio_limits; 11076574a5d5SLen Brown 11086574a5d5SLen Brown get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 11096574a5d5SLen Brown 1110889facbeSLen Brown fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 11116574a5d5SLen Brown 11126574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 11136574a5d5SLen Brown if (ratio) 11146574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 11156574a5d5SLen Brown ratio, bclk, ratio * bclk); 11166574a5d5SLen Brown 11176574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 11186574a5d5SLen Brown if (ratio) 11196574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 11206574a5d5SLen Brown ratio, bclk, ratio * bclk); 11216574a5d5SLen Brown 11226574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 11236574a5d5SLen Brown if (ratio) 11246574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 11256574a5d5SLen Brown ratio, bclk, ratio * bclk); 11266574a5d5SLen Brown 11276574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 11286574a5d5SLen Brown if (ratio) 11296574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 11306574a5d5SLen Brown ratio, bclk, ratio * bclk); 11316574a5d5SLen Brown 11326574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 11336574a5d5SLen Brown if (ratio) 11346574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 11356574a5d5SLen Brown ratio, bclk, ratio * bclk); 11366574a5d5SLen Brown 11376574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 11386574a5d5SLen Brown if (ratio) 11396574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 11406574a5d5SLen Brown ratio, bclk, ratio * bclk); 11416574a5d5SLen Brown 11426574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 11436574a5d5SLen Brown if (ratio) 11446574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 11456574a5d5SLen Brown ratio, bclk, ratio * bclk); 11466574a5d5SLen Brown 11476574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 11486574a5d5SLen Brown if (ratio) 11496574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 11506574a5d5SLen Brown ratio, bclk, ratio * bclk); 11516574a5d5SLen Brown 11526574a5d5SLen Brown print_nhm_turbo_ratio_limits: 1153889facbeSLen Brown get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1154889facbeSLen Brown 1155889facbeSLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1156889facbeSLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1157889facbeSLen Brown 1158889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr); 1159889facbeSLen Brown 1160ee7e38e3SLen Brown fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1161889facbeSLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1162889facbeSLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1163889facbeSLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1164889facbeSLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1165889facbeSLen Brown (msr & (1 << 15)) ? "" : "UN", 1166ee7e38e3SLen Brown (unsigned int)msr & 7, 1167ee7e38e3SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1168103a8feaSLen Brown 1169d7899447SLen Brown if (!do_nhm_turbo_ratio_limit) 1170103a8feaSLen Brown return; 1171103a8feaSLen Brown 11729c63a650SLen Brown get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1173103a8feaSLen Brown 1174889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 11756574a5d5SLen Brown 11766574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 11776574a5d5SLen Brown if (ratio) 11786574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 11796574a5d5SLen Brown ratio, bclk, ratio * bclk); 11806574a5d5SLen Brown 11816574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 11826574a5d5SLen Brown if (ratio) 11836574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 11846574a5d5SLen Brown ratio, bclk, ratio * bclk); 11856574a5d5SLen Brown 11866574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 11876574a5d5SLen Brown if (ratio) 11886574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 11896574a5d5SLen Brown ratio, bclk, ratio * bclk); 11906574a5d5SLen Brown 11916574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 11926574a5d5SLen Brown if (ratio) 11936574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 11946574a5d5SLen Brown ratio, bclk, ratio * bclk); 11956574a5d5SLen Brown 1196103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1197103a8feaSLen Brown if (ratio) 1198103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1199103a8feaSLen Brown ratio, bclk, ratio * bclk); 1200103a8feaSLen Brown 1201103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1202103a8feaSLen Brown if (ratio) 1203103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1204103a8feaSLen Brown ratio, bclk, ratio * bclk); 1205103a8feaSLen Brown 1206103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1207103a8feaSLen Brown if (ratio) 1208103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1209103a8feaSLen Brown ratio, bclk, ratio * bclk); 1210103a8feaSLen Brown 1211103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1212103a8feaSLen Brown if (ratio) 1213103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1214103a8feaSLen Brown ratio, bclk, ratio * bclk); 12153a9a941dSLen Brown 1216103a8feaSLen Brown } 1217103a8feaSLen Brown 1218c98d5d94SLen Brown void free_all_buffers(void) 1219103a8feaSLen Brown { 1220c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1221c98d5d94SLen Brown cpu_present_set = NULL; 1222c98d5d94SLen Brown cpu_present_set = 0; 1223103a8feaSLen Brown 1224c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1225c98d5d94SLen Brown cpu_affinity_set = NULL; 1226c98d5d94SLen Brown cpu_affinity_setsize = 0; 1227103a8feaSLen Brown 1228c98d5d94SLen Brown free(thread_even); 1229c98d5d94SLen Brown free(core_even); 1230c98d5d94SLen Brown free(package_even); 1231103a8feaSLen Brown 1232c98d5d94SLen Brown thread_even = NULL; 1233c98d5d94SLen Brown core_even = NULL; 1234c98d5d94SLen Brown package_even = NULL; 1235103a8feaSLen Brown 1236c98d5d94SLen Brown free(thread_odd); 1237c98d5d94SLen Brown free(core_odd); 1238c98d5d94SLen Brown free(package_odd); 1239103a8feaSLen Brown 1240c98d5d94SLen Brown thread_odd = NULL; 1241c98d5d94SLen Brown core_odd = NULL; 1242c98d5d94SLen Brown package_odd = NULL; 1243103a8feaSLen Brown 1244c98d5d94SLen Brown free(output_buffer); 1245c98d5d94SLen Brown output_buffer = NULL; 1246c98d5d94SLen Brown outp = NULL; 1247103a8feaSLen Brown } 1248103a8feaSLen Brown 1249103a8feaSLen Brown /* 125057a42a34SJosh Triplett * Open a file, and exit on failure 125157a42a34SJosh Triplett */ 125257a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 125357a42a34SJosh Triplett { 125457a42a34SJosh Triplett FILE *filep = fopen(path, "r"); 1255b2c95d90SJosh Triplett if (!filep) 1256b2c95d90SJosh Triplett err(1, "%s: open failed", path); 125757a42a34SJosh Triplett return filep; 125857a42a34SJosh Triplett } 125957a42a34SJosh Triplett 126057a42a34SJosh Triplett /* 126195aebc44SJosh Triplett * Parse a file containing a single int. 126295aebc44SJosh Triplett */ 126395aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 126495aebc44SJosh Triplett { 126595aebc44SJosh Triplett va_list args; 126695aebc44SJosh Triplett char path[PATH_MAX]; 126795aebc44SJosh Triplett FILE *filep; 126895aebc44SJosh Triplett int value; 126995aebc44SJosh Triplett 127095aebc44SJosh Triplett va_start(args, fmt); 127195aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 127295aebc44SJosh Triplett va_end(args); 127357a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1274b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1275b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 127695aebc44SJosh Triplett fclose(filep); 127795aebc44SJosh Triplett return value; 127895aebc44SJosh Triplett } 127995aebc44SJosh Triplett 128095aebc44SJosh Triplett /* 1281c98d5d94SLen Brown * cpu_is_first_sibling_in_core(cpu) 1282c98d5d94SLen Brown * return 1 if given CPU is 1st HT sibling in the core 1283103a8feaSLen Brown */ 1284c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu) 1285103a8feaSLen Brown { 128695aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1287c98d5d94SLen Brown } 1288103a8feaSLen Brown 1289c98d5d94SLen Brown /* 1290c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1291c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1292c98d5d94SLen Brown */ 1293c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1294c98d5d94SLen Brown { 129595aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1296103a8feaSLen Brown } 1297103a8feaSLen Brown 1298103a8feaSLen Brown int get_physical_package_id(int cpu) 1299103a8feaSLen Brown { 130095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1301103a8feaSLen Brown } 1302103a8feaSLen Brown 1303103a8feaSLen Brown int get_core_id(int cpu) 1304103a8feaSLen Brown { 130595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1306103a8feaSLen Brown } 1307103a8feaSLen Brown 1308c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1309c98d5d94SLen Brown { 1310c98d5d94SLen Brown char path[80]; 1311c98d5d94SLen Brown FILE *filep; 1312c98d5d94SLen Brown int sib1, sib2; 1313c98d5d94SLen Brown int matches; 1314c98d5d94SLen Brown char character; 1315c98d5d94SLen Brown 1316c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 131757a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1318103a8feaSLen Brown /* 1319c98d5d94SLen Brown * file format: 1320c98d5d94SLen Brown * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 1321c98d5d94SLen Brown * otherwinse 1 sibling (self). 1322c98d5d94SLen Brown */ 1323c98d5d94SLen Brown matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 1324c98d5d94SLen Brown 1325c98d5d94SLen Brown fclose(filep); 1326c98d5d94SLen Brown 1327c98d5d94SLen Brown if (matches == 3) 1328c98d5d94SLen Brown return 2; 1329c98d5d94SLen Brown else 1330c98d5d94SLen Brown return 1; 1331c98d5d94SLen Brown } 1332c98d5d94SLen Brown 1333c98d5d94SLen Brown /* 1334c98d5d94SLen Brown * run func(thread, core, package) in topology order 1335c98d5d94SLen Brown * skip non-present cpus 1336103a8feaSLen Brown */ 1337103a8feaSLen Brown 1338c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1339c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1340c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1341c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1342c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1343c98d5d94SLen Brown struct pkg_data *pkg_base2) 1344c98d5d94SLen Brown { 1345c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1346c98d5d94SLen Brown 1347c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1348c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1349c98d5d94SLen Brown for (thread_no = 0; thread_no < 1350c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1351c98d5d94SLen Brown struct thread_data *t, *t2; 1352c98d5d94SLen Brown struct core_data *c, *c2; 1353c98d5d94SLen Brown struct pkg_data *p, *p2; 1354c98d5d94SLen Brown 1355c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1356c98d5d94SLen Brown 1357c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1358c98d5d94SLen Brown continue; 1359c98d5d94SLen Brown 1360c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1361c98d5d94SLen Brown 1362c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1363c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1364c98d5d94SLen Brown 1365c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1366c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1367c98d5d94SLen Brown 1368c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1369c98d5d94SLen Brown if (retval) 1370c98d5d94SLen Brown return retval; 1371c98d5d94SLen Brown } 1372c98d5d94SLen Brown } 1373c98d5d94SLen Brown } 1374c98d5d94SLen Brown return 0; 1375c98d5d94SLen Brown } 1376c98d5d94SLen Brown 1377c98d5d94SLen Brown /* 1378c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1379c98d5d94SLen Brown * return max_cpu number 1380c98d5d94SLen Brown */ 1381c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1382103a8feaSLen Brown { 1383103a8feaSLen Brown FILE *fp; 1384c98d5d94SLen Brown int cpu_num; 1385103a8feaSLen Brown int retval; 1386103a8feaSLen Brown 138757a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1388103a8feaSLen Brown 1389103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1390b2c95d90SJosh Triplett if (retval != 0) 1391b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1392103a8feaSLen Brown 1393c98d5d94SLen Brown while (1) { 1394c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1395103a8feaSLen Brown if (retval != 1) 1396103a8feaSLen Brown break; 1397103a8feaSLen Brown 1398c98d5d94SLen Brown retval = func(cpu_num); 1399c98d5d94SLen Brown if (retval) { 1400c98d5d94SLen Brown fclose(fp); 1401c98d5d94SLen Brown return(retval); 1402c98d5d94SLen Brown } 1403103a8feaSLen Brown } 1404103a8feaSLen Brown fclose(fp); 1405c98d5d94SLen Brown return 0; 1406103a8feaSLen Brown } 1407103a8feaSLen Brown 1408103a8feaSLen Brown void re_initialize(void) 1409103a8feaSLen Brown { 1410c98d5d94SLen Brown free_all_buffers(); 1411c98d5d94SLen Brown setup_all_buffers(); 1412c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1413103a8feaSLen Brown } 1414103a8feaSLen Brown 1415c98d5d94SLen Brown 1416103a8feaSLen Brown /* 1417c98d5d94SLen Brown * count_cpus() 1418c98d5d94SLen Brown * remember the last one seen, it will be the max 1419103a8feaSLen Brown */ 1420c98d5d94SLen Brown int count_cpus(int cpu) 1421103a8feaSLen Brown { 1422c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1423c98d5d94SLen Brown topo.max_cpu_num = cpu; 1424103a8feaSLen Brown 1425c98d5d94SLen Brown topo.num_cpus += 1; 1426c98d5d94SLen Brown return 0; 1427103a8feaSLen Brown } 1428c98d5d94SLen Brown int mark_cpu_present(int cpu) 1429c98d5d94SLen Brown { 1430c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 143115aaa346SLen Brown return 0; 1432103a8feaSLen Brown } 1433103a8feaSLen Brown 1434103a8feaSLen Brown void turbostat_loop() 1435103a8feaSLen Brown { 1436c98d5d94SLen Brown int retval; 1437e52966c0SLen Brown int restarted = 0; 1438c98d5d94SLen Brown 1439103a8feaSLen Brown restart: 1440e52966c0SLen Brown restarted++; 1441e52966c0SLen Brown 1442c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1443d91bb17cSLen Brown if (retval < -1) { 1444d91bb17cSLen Brown exit(retval); 1445d91bb17cSLen Brown } else if (retval == -1) { 1446e52966c0SLen Brown if (restarted > 1) { 1447e52966c0SLen Brown exit(retval); 1448e52966c0SLen Brown } 1449c98d5d94SLen Brown re_initialize(); 1450c98d5d94SLen Brown goto restart; 1451c98d5d94SLen Brown } 1452e52966c0SLen Brown restarted = 0; 1453103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1454103a8feaSLen Brown 1455103a8feaSLen Brown while (1) { 1456c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1457103a8feaSLen Brown re_initialize(); 1458103a8feaSLen Brown goto restart; 1459103a8feaSLen Brown } 1460103a8feaSLen Brown sleep(interval_sec); 1461c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1462d91bb17cSLen Brown if (retval < -1) { 1463d91bb17cSLen Brown exit(retval); 1464d91bb17cSLen Brown } else if (retval == -1) { 146515aaa346SLen Brown re_initialize(); 146615aaa346SLen Brown goto restart; 146715aaa346SLen Brown } 1468103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1469103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1470c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1471c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1472c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1473c98d5d94SLen Brown flush_stdout(); 147415aaa346SLen Brown sleep(interval_sec); 1475c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1476d91bb17cSLen Brown if (retval < -1) { 1477d91bb17cSLen Brown exit(retval); 1478d91bb17cSLen Brown } else if (retval == -1) { 1479103a8feaSLen Brown re_initialize(); 1480103a8feaSLen Brown goto restart; 1481103a8feaSLen Brown } 1482103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1483103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1484c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1485c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1486c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1487c98d5d94SLen Brown flush_stdout(); 1488103a8feaSLen Brown } 1489103a8feaSLen Brown } 1490103a8feaSLen Brown 1491103a8feaSLen Brown void check_dev_msr() 1492103a8feaSLen Brown { 1493103a8feaSLen Brown struct stat sb; 1494103a8feaSLen Brown 1495b2c95d90SJosh Triplett if (stat("/dev/cpu/0/msr", &sb)) 1496d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 1497103a8feaSLen Brown } 1498103a8feaSLen Brown 149998481e79SLen Brown void check_permissions() 1500103a8feaSLen Brown { 150198481e79SLen Brown struct __user_cap_header_struct cap_header_data; 150298481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 150398481e79SLen Brown struct __user_cap_data_struct cap_data_data; 150498481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 150598481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 150698481e79SLen Brown int do_exit = 0; 150798481e79SLen Brown 150898481e79SLen Brown /* check for CAP_SYS_RAWIO */ 150998481e79SLen Brown cap_header->pid = getpid(); 151098481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 151198481e79SLen Brown if (capget(cap_header, cap_data) < 0) 151298481e79SLen Brown err(-6, "capget(2) failed"); 151398481e79SLen Brown 151498481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 151598481e79SLen Brown do_exit++; 151698481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 151798481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 151898481e79SLen Brown } 151998481e79SLen Brown 152098481e79SLen Brown /* test file permissions */ 152198481e79SLen Brown if (euidaccess("/dev/cpu/0/msr", R_OK)) { 152298481e79SLen Brown do_exit++; 152398481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 152498481e79SLen Brown } 152598481e79SLen Brown 152698481e79SLen Brown /* if all else fails, thell them to be root */ 152798481e79SLen Brown if (do_exit) 1528b2c95d90SJosh Triplett if (getuid() != 0) 1529d7899447SLen Brown warnx("... or simply run as root"); 153098481e79SLen Brown 153198481e79SLen Brown if (do_exit) 153298481e79SLen Brown exit(-6); 1533103a8feaSLen Brown } 1534103a8feaSLen Brown 1535d7899447SLen Brown /* 1536d7899447SLen Brown * NHM adds support for additional MSRs: 1537d7899447SLen Brown * 1538d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 1539d7899447SLen Brown * 1540d7899447SLen Brown * MSR_NHM_PLATFORM_INFO 0x000000ce 1541d7899447SLen Brown * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 1542d7899447SLen Brown * 1543d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 1544d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 1545d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 1546d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 1547d7899447SLen Brown * 1548ee7e38e3SLen Brown * Side effect: 1549ee7e38e3SLen Brown * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL 1550d7899447SLen Brown */ 1551ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 1552103a8feaSLen Brown { 1553ee7e38e3SLen Brown unsigned long long msr; 1554ee7e38e3SLen Brown int *pkg_cstate_limits; 1555ee7e38e3SLen Brown 1556103a8feaSLen Brown if (!genuine_intel) 1557103a8feaSLen Brown return 0; 1558103a8feaSLen Brown 1559103a8feaSLen Brown if (family != 6) 1560103a8feaSLen Brown return 0; 1561103a8feaSLen Brown 1562103a8feaSLen Brown switch (model) { 1563103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1564103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1565103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1566103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1567103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1568ee7e38e3SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1569ee7e38e3SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1570ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 1571ee7e38e3SLen Brown break; 1572103a8feaSLen Brown case 0x2A: /* SNB */ 1573103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1574553575f1SLen Brown case 0x3A: /* IVB */ 15751300651bSLen Brown case 0x3E: /* IVB Xeon */ 1576ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 1577ee7e38e3SLen Brown break; 157870b43400SLen Brown case 0x3C: /* HSW */ 1579e6f9bb3cSLen Brown case 0x3F: /* HSX */ 158070b43400SLen Brown case 0x45: /* HSW */ 1581149c2319SLen Brown case 0x46: /* HSW */ 15824e8e863fSLen Brown case 0x3D: /* BDW */ 158348a0631cSLen Brown case 0x47: /* BDW */ 15844e8e863fSLen Brown case 0x4F: /* BDX */ 15854e8e863fSLen Brown case 0x56: /* BDX-DE */ 1586ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 1587ee7e38e3SLen Brown break; 1588ee7e38e3SLen Brown case 0x37: /* BYT */ 1589ee7e38e3SLen Brown case 0x4D: /* AVN */ 1590ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 1591ee7e38e3SLen Brown break; 1592ee7e38e3SLen Brown case 0x4C: /* AMT */ 1593ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 1594ee7e38e3SLen Brown break; 1595ee7e38e3SLen Brown case 0x57: /* PHI */ 1596ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 1597ee7e38e3SLen Brown break; 1598103a8feaSLen Brown default: 1599103a8feaSLen Brown return 0; 1600103a8feaSLen Brown } 1601ee7e38e3SLen Brown get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1602ee7e38e3SLen Brown 1603ee7e38e3SLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0x7]; 1604ee7e38e3SLen Brown 1605ee7e38e3SLen Brown return 1; 1606103a8feaSLen Brown } 1607d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) 1608d7899447SLen Brown { 1609d7899447SLen Brown switch (model) { 1610d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 1611d7899447SLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1612d7899447SLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1613d7899447SLen Brown return 0; 1614d7899447SLen Brown default: 1615d7899447SLen Brown return 1; 1616d7899447SLen Brown } 1617d7899447SLen Brown } 16186574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 16196574a5d5SLen Brown { 16206574a5d5SLen Brown if (!genuine_intel) 16216574a5d5SLen Brown return 0; 16226574a5d5SLen Brown 16236574a5d5SLen Brown if (family != 6) 16246574a5d5SLen Brown return 0; 16256574a5d5SLen Brown 16266574a5d5SLen Brown switch (model) { 16276574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 16286574a5d5SLen Brown return 1; 16296574a5d5SLen Brown default: 16306574a5d5SLen Brown return 0; 16316574a5d5SLen Brown } 16326574a5d5SLen Brown } 16336574a5d5SLen Brown 1634889facbeSLen Brown /* 1635889facbeSLen Brown * print_epb() 1636889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 1637889facbeSLen Brown */ 1638889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1639889facbeSLen Brown { 1640889facbeSLen Brown unsigned long long msr; 1641889facbeSLen Brown char *epb_string; 1642889facbeSLen Brown int cpu; 1643889facbeSLen Brown 1644889facbeSLen Brown if (!has_epb) 1645889facbeSLen Brown return 0; 1646889facbeSLen Brown 1647889facbeSLen Brown cpu = t->cpu_id; 1648889facbeSLen Brown 1649889facbeSLen Brown /* EPB is per-package */ 1650889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1651889facbeSLen Brown return 0; 1652889facbeSLen Brown 1653889facbeSLen Brown if (cpu_migrate(cpu)) { 1654889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1655889facbeSLen Brown return -1; 1656889facbeSLen Brown } 1657889facbeSLen Brown 1658889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 1659889facbeSLen Brown return 0; 1660889facbeSLen Brown 1661889facbeSLen Brown switch (msr & 0x7) { 1662889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 1663889facbeSLen Brown epb_string = "performance"; 1664889facbeSLen Brown break; 1665889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 1666889facbeSLen Brown epb_string = "balanced"; 1667889facbeSLen Brown break; 1668889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 1669889facbeSLen Brown epb_string = "powersave"; 1670889facbeSLen Brown break; 1671889facbeSLen Brown default: 1672889facbeSLen Brown epb_string = "custom"; 1673889facbeSLen Brown break; 1674889facbeSLen Brown } 1675889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 1676889facbeSLen Brown 1677889facbeSLen Brown return 0; 1678889facbeSLen Brown } 1679889facbeSLen Brown 16803a9a941dSLen Brown /* 16813a9a941dSLen Brown * print_perf_limit() 16823a9a941dSLen Brown */ 16833a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 16843a9a941dSLen Brown { 16853a9a941dSLen Brown unsigned long long msr; 16863a9a941dSLen Brown int cpu; 16873a9a941dSLen Brown 16883a9a941dSLen Brown cpu = t->cpu_id; 16893a9a941dSLen Brown 16903a9a941dSLen Brown /* per-package */ 16913a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 16923a9a941dSLen Brown return 0; 16933a9a941dSLen Brown 16943a9a941dSLen Brown if (cpu_migrate(cpu)) { 16953a9a941dSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 16963a9a941dSLen Brown return -1; 16973a9a941dSLen Brown } 16983a9a941dSLen Brown 16993a9a941dSLen Brown if (do_core_perf_limit_reasons) { 17003a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 17013a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 17023a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 1703*e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 17043a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 1705*e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 1706*e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 1707*e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 1708*e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 1709*e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 1710*e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 1711*e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 1712*e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 1713*e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 1714*e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 1715*e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 1716*e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 17173a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 1718*e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 17193a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 1720*e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 1721*e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 1722*e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 1723*e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 1724*e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 1725*e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 1726*e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 1727*e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 1728*e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 1729*e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 1730*e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 1731*e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 17323a9a941dSLen Brown 17333a9a941dSLen Brown } 17343a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 17353a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 17363a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 17373a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)", 17383a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 17393a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 17403a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 17413a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 17423a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 17433a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 17443a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 17453a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 17463a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n", 17473a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 17483a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 17493a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 17503a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 17513a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 17523a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 17533a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 17543a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 17553a9a941dSLen Brown } 17563a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 17573a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 17583a9a941dSLen Brown fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 17593a9a941dSLen Brown fprintf(stderr, " (Active: %s%s%s%s%s%s)", 17603a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 17613a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 17623a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 17633a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 17643a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 17653a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 17663a9a941dSLen Brown fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n", 17673a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 17683a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 17693a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 17703a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 17713a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 17723a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 17733a9a941dSLen Brown } 17743a9a941dSLen Brown return 0; 17753a9a941dSLen Brown } 17763a9a941dSLen Brown 1777889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 1778889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 1779889facbeSLen Brown 1780144b44b1SLen Brown double get_tdp(model) 1781144b44b1SLen Brown { 1782144b44b1SLen Brown unsigned long long msr; 1783144b44b1SLen Brown 1784144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 1785144b44b1SLen Brown if (!get_msr(0, MSR_PKG_POWER_INFO, &msr)) 1786144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 1787144b44b1SLen Brown 1788144b44b1SLen Brown switch (model) { 1789144b44b1SLen Brown case 0x37: 1790144b44b1SLen Brown case 0x4D: 1791144b44b1SLen Brown return 30.0; 1792144b44b1SLen Brown default: 1793144b44b1SLen Brown return 135.0; 1794144b44b1SLen Brown } 1795144b44b1SLen Brown } 1796144b44b1SLen Brown 1797144b44b1SLen Brown 1798889facbeSLen Brown /* 1799889facbeSLen Brown * rapl_probe() 1800889facbeSLen Brown * 1801144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 1802889facbeSLen Brown */ 1803889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 1804889facbeSLen Brown { 1805889facbeSLen Brown unsigned long long msr; 1806144b44b1SLen Brown unsigned int time_unit; 1807889facbeSLen Brown double tdp; 1808889facbeSLen Brown 1809889facbeSLen Brown if (!genuine_intel) 1810889facbeSLen Brown return; 1811889facbeSLen Brown 1812889facbeSLen Brown if (family != 6) 1813889facbeSLen Brown return; 1814889facbeSLen Brown 1815889facbeSLen Brown switch (model) { 1816889facbeSLen Brown case 0x2A: 1817889facbeSLen Brown case 0x3A: 181870b43400SLen Brown case 0x3C: /* HSW */ 181970b43400SLen Brown case 0x45: /* HSW */ 1820149c2319SLen Brown case 0x46: /* HSW */ 18214e8e863fSLen Brown case 0x3D: /* BDW */ 182248a0631cSLen Brown case 0x47: /* BDW */ 1823144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 1824889facbeSLen Brown break; 1825e6f9bb3cSLen Brown case 0x3F: /* HSX */ 18264e8e863fSLen Brown case 0x4F: /* BDX */ 18274e8e863fSLen Brown case 0x56: /* BDX-DE */ 1828e6f9bb3cSLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 1829e6f9bb3cSLen Brown break; 1830889facbeSLen Brown case 0x2D: 1831889facbeSLen Brown case 0x3E: 1832144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 1833144b44b1SLen Brown break; 1834144b44b1SLen Brown case 0x37: /* BYT */ 1835144b44b1SLen Brown case 0x4D: /* AVN */ 1836144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES ; 1837889facbeSLen Brown break; 1838889facbeSLen Brown default: 1839889facbeSLen Brown return; 1840889facbeSLen Brown } 1841889facbeSLen Brown 1842889facbeSLen Brown /* units on package 0, verify later other packages match */ 1843889facbeSLen Brown if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr)) 1844889facbeSLen Brown return; 1845889facbeSLen Brown 1846889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 1847144b44b1SLen Brown if (model == 0x37) 1848144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 1849144b44b1SLen Brown else 1850889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 1851889facbeSLen Brown 1852144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 1853144b44b1SLen Brown if (time_unit == 0) 1854144b44b1SLen Brown time_unit = 0xA; 1855889facbeSLen Brown 1856144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 1857144b44b1SLen Brown 1858144b44b1SLen Brown tdp = get_tdp(model); 1859889facbeSLen Brown 1860889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 1861d8af6f5fSLen Brown if (debug) 1862144b44b1SLen Brown fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 1863889facbeSLen Brown 1864889facbeSLen Brown return; 1865889facbeSLen Brown } 1866889facbeSLen Brown 18673a9a941dSLen Brown void perf_limit_reasons_probe(family, model) 18683a9a941dSLen Brown { 18693a9a941dSLen Brown if (!genuine_intel) 18703a9a941dSLen Brown return; 18713a9a941dSLen Brown 18723a9a941dSLen Brown if (family != 6) 18733a9a941dSLen Brown return; 18743a9a941dSLen Brown 18753a9a941dSLen Brown switch (model) { 18763a9a941dSLen Brown case 0x3C: /* HSW */ 18773a9a941dSLen Brown case 0x45: /* HSW */ 18783a9a941dSLen Brown case 0x46: /* HSW */ 18793a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 18803a9a941dSLen Brown case 0x3F: /* HSX */ 18813a9a941dSLen Brown do_core_perf_limit_reasons = 1; 18823a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 18833a9a941dSLen Brown default: 18843a9a941dSLen Brown return; 18853a9a941dSLen Brown } 18863a9a941dSLen Brown } 18873a9a941dSLen Brown 1888889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1889889facbeSLen Brown { 1890889facbeSLen Brown unsigned long long msr; 1891889facbeSLen Brown unsigned int dts; 1892889facbeSLen Brown int cpu; 1893889facbeSLen Brown 1894889facbeSLen Brown if (!(do_dts || do_ptm)) 1895889facbeSLen Brown return 0; 1896889facbeSLen Brown 1897889facbeSLen Brown cpu = t->cpu_id; 1898889facbeSLen Brown 1899889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 1900889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1901889facbeSLen Brown return 0; 1902889facbeSLen Brown 1903889facbeSLen Brown if (cpu_migrate(cpu)) { 1904889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1905889facbeSLen Brown return -1; 1906889facbeSLen Brown } 1907889facbeSLen Brown 1908889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 1909889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1910889facbeSLen Brown return 0; 1911889facbeSLen Brown 1912889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1913889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 1914889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 1915889facbeSLen Brown 1916889facbeSLen Brown #ifdef THERM_DEBUG 1917889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 1918889facbeSLen Brown return 0; 1919889facbeSLen Brown 1920889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1921889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1922889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1923889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1924889facbeSLen Brown #endif 1925889facbeSLen Brown } 1926889facbeSLen Brown 1927889facbeSLen Brown 1928889facbeSLen Brown if (do_dts) { 1929889facbeSLen Brown unsigned int resolution; 1930889facbeSLen Brown 1931889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1932889facbeSLen Brown return 0; 1933889facbeSLen Brown 1934889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1935889facbeSLen Brown resolution = (msr >> 27) & 0xF; 1936889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 1937889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 1938889facbeSLen Brown 1939889facbeSLen Brown #ifdef THERM_DEBUG 1940889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 1941889facbeSLen Brown return 0; 1942889facbeSLen Brown 1943889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1944889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1945889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1946889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1947889facbeSLen Brown #endif 1948889facbeSLen Brown } 1949889facbeSLen Brown 1950889facbeSLen Brown return 0; 1951889facbeSLen Brown } 1952889facbeSLen Brown 1953889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 1954889facbeSLen Brown { 1955889facbeSLen Brown fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 1956889facbeSLen Brown cpu, label, 1957889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 1958889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 1959889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 1960889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 1961889facbeSLen Brown 1962889facbeSLen Brown return; 1963889facbeSLen Brown } 1964889facbeSLen Brown 1965889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1966889facbeSLen Brown { 1967889facbeSLen Brown unsigned long long msr; 1968889facbeSLen Brown int cpu; 1969889facbeSLen Brown 1970889facbeSLen Brown if (!do_rapl) 1971889facbeSLen Brown return 0; 1972889facbeSLen Brown 1973889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 1974889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1975889facbeSLen Brown return 0; 1976889facbeSLen Brown 1977889facbeSLen Brown cpu = t->cpu_id; 1978889facbeSLen Brown if (cpu_migrate(cpu)) { 1979889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1980889facbeSLen Brown return -1; 1981889facbeSLen Brown } 1982889facbeSLen Brown 1983889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 1984889facbeSLen Brown return -1; 1985889facbeSLen Brown 1986d8af6f5fSLen Brown if (debug) { 1987889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 1988889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 1989144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 1990889facbeSLen Brown } 1991144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 1992144b44b1SLen Brown 1993889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 1994889facbeSLen Brown return -5; 1995889facbeSLen Brown 1996889facbeSLen Brown 1997889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1998889facbeSLen Brown cpu, msr, 1999889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2000889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2001889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2002889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2003889facbeSLen Brown 2004144b44b1SLen Brown } 2005144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 2006144b44b1SLen Brown 2007889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 2008889facbeSLen Brown return -9; 2009889facbeSLen Brown 2010889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 2011889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 2012889facbeSLen Brown 2013889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 2014889facbeSLen Brown fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 2015889facbeSLen Brown cpu, 2016889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 2017889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 2018889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 2019889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 2020889facbeSLen Brown } 2021889facbeSLen Brown 2022889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 2023889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 2024889facbeSLen Brown return -6; 2025889facbeSLen Brown 2026889facbeSLen Brown 2027889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 2028889facbeSLen Brown cpu, msr, 2029889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2030889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2031889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 2032889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 2033889facbeSLen Brown 2034889facbeSLen Brown 2035889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 2036889facbeSLen Brown return -9; 2037889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 2038889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2039889facbeSLen Brown 2040889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 2041889facbeSLen Brown } 2042144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 2043d8af6f5fSLen Brown if (debug) { 2044889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 2045889facbeSLen Brown return -7; 2046889facbeSLen Brown 2047889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 2048144b44b1SLen Brown } 2049144b44b1SLen Brown } 2050144b44b1SLen Brown if (do_rapl & RAPL_CORES) { 2051d8af6f5fSLen Brown if (debug) { 2052889facbeSLen Brown 2053889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 2054889facbeSLen Brown return -9; 2055889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 2056889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2057889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 2058889facbeSLen Brown } 2059889facbeSLen Brown } 2060889facbeSLen Brown if (do_rapl & RAPL_GFX) { 2061d8af6f5fSLen Brown if (debug) { 2062889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 2063889facbeSLen Brown return -8; 2064889facbeSLen Brown 2065889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 2066889facbeSLen Brown 2067889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 2068889facbeSLen Brown return -9; 2069889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 2070889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 2071889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 2072889facbeSLen Brown } 2073889facbeSLen Brown } 2074889facbeSLen Brown return 0; 2075889facbeSLen Brown } 2076889facbeSLen Brown 2077d7899447SLen Brown /* 2078d7899447SLen Brown * SNB adds support for additional MSRs: 2079d7899447SLen Brown * 2080d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 2081d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 2082d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 2083d7899447SLen Brown */ 2084103a8feaSLen Brown 2085d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 2086103a8feaSLen Brown { 2087103a8feaSLen Brown if (!genuine_intel) 2088103a8feaSLen Brown return 0; 2089103a8feaSLen Brown 2090103a8feaSLen Brown switch (model) { 2091103a8feaSLen Brown case 0x2A: 2092103a8feaSLen Brown case 0x2D: 2093650a37f3SLen Brown case 0x3A: /* IVB */ 20941300651bSLen Brown case 0x3E: /* IVB Xeon */ 209570b43400SLen Brown case 0x3C: /* HSW */ 209670b43400SLen Brown case 0x3F: /* HSW */ 209770b43400SLen Brown case 0x45: /* HSW */ 2098149c2319SLen Brown case 0x46: /* HSW */ 20994e8e863fSLen Brown case 0x3D: /* BDW */ 210048a0631cSLen Brown case 0x47: /* BDW */ 21014e8e863fSLen Brown case 0x4F: /* BDX */ 21024e8e863fSLen Brown case 0x56: /* BDX-DE */ 2103103a8feaSLen Brown return 1; 2104103a8feaSLen Brown } 2105103a8feaSLen Brown return 0; 2106103a8feaSLen Brown } 2107103a8feaSLen Brown 2108d7899447SLen Brown /* 2109d7899447SLen Brown * HSW adds support for additional MSRs: 2110d7899447SLen Brown * 2111d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 2112d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 2113d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 2114d7899447SLen Brown */ 2115d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 2116ca58710fSKristen Carlson Accardi { 2117ca58710fSKristen Carlson Accardi if (!genuine_intel) 2118ca58710fSKristen Carlson Accardi return 0; 2119ca58710fSKristen Carlson Accardi 2120ca58710fSKristen Carlson Accardi switch (model) { 21214e8e863fSLen Brown case 0x45: /* HSW */ 21224e8e863fSLen Brown case 0x3D: /* BDW */ 2123ca58710fSKristen Carlson Accardi return 1; 2124ca58710fSKristen Carlson Accardi } 2125ca58710fSKristen Carlson Accardi return 0; 2126ca58710fSKristen Carlson Accardi } 2127ca58710fSKristen Carlson Accardi 2128ca58710fSKristen Carlson Accardi 2129144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 2130144b44b1SLen Brown { 2131144b44b1SLen Brown if (!genuine_intel) 2132144b44b1SLen Brown return 0; 2133144b44b1SLen Brown switch (model) { 2134144b44b1SLen Brown case 0x37: /* BYT */ 2135144b44b1SLen Brown case 0x4D: /* AVN */ 2136144b44b1SLen Brown return 1; 2137144b44b1SLen Brown } 2138144b44b1SLen Brown return 0; 2139144b44b1SLen Brown } 2140144b44b1SLen Brown 2141144b44b1SLen Brown #define SLM_BCLK_FREQS 5 2142144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 2143144b44b1SLen Brown 2144144b44b1SLen Brown double slm_bclk(void) 2145144b44b1SLen Brown { 2146144b44b1SLen Brown unsigned long long msr = 3; 2147144b44b1SLen Brown unsigned int i; 2148144b44b1SLen Brown double freq; 2149144b44b1SLen Brown 2150144b44b1SLen Brown if (get_msr(0, MSR_FSB_FREQ, &msr)) 2151144b44b1SLen Brown fprintf(stderr, "SLM BCLK: unknown\n"); 2152144b44b1SLen Brown 2153144b44b1SLen Brown i = msr & 0xf; 2154144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 2155144b44b1SLen Brown fprintf(stderr, "SLM BCLK[%d] invalid\n", i); 2156144b44b1SLen Brown msr = 3; 2157144b44b1SLen Brown } 2158144b44b1SLen Brown freq = slm_freq_table[i]; 2159144b44b1SLen Brown 2160144b44b1SLen Brown fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); 2161144b44b1SLen Brown 2162144b44b1SLen Brown return freq; 2163144b44b1SLen Brown } 2164144b44b1SLen Brown 2165103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 2166103a8feaSLen Brown { 2167d7899447SLen Brown if (has_snb_msrs(family, model)) 2168103a8feaSLen Brown return 100.00; 2169144b44b1SLen Brown else if (is_slm(family, model)) 2170144b44b1SLen Brown return slm_bclk(); 2171103a8feaSLen Brown else 2172103a8feaSLen Brown return 133.33; 2173103a8feaSLen Brown } 2174103a8feaSLen Brown 2175889facbeSLen Brown /* 2176889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 2177889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 2178889facbeSLen Brown * This is usually equal to tjMax. 2179889facbeSLen Brown * 2180889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 2181889facbeSLen Brown * but also allow cmdline over-ride with -T. 2182889facbeSLen Brown * 2183889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 2184889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 2185889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 2186889facbeSLen Brown */ 2187889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2188889facbeSLen Brown { 2189889facbeSLen Brown unsigned long long msr; 2190889facbeSLen Brown unsigned int target_c_local; 2191889facbeSLen Brown int cpu; 2192889facbeSLen Brown 2193889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 2194889facbeSLen Brown if (!(do_dts || do_ptm)) 2195889facbeSLen Brown return 0; 2196889facbeSLen Brown 2197889facbeSLen Brown /* this is a per-package concept */ 2198889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2199889facbeSLen Brown return 0; 2200889facbeSLen Brown 2201889facbeSLen Brown cpu = t->cpu_id; 2202889facbeSLen Brown if (cpu_migrate(cpu)) { 2203889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 2204889facbeSLen Brown return -1; 2205889facbeSLen Brown } 2206889facbeSLen Brown 2207889facbeSLen Brown if (tcc_activation_temp_override != 0) { 2208889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 2209889facbeSLen Brown fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 2210889facbeSLen Brown cpu, tcc_activation_temp); 2211889facbeSLen Brown return 0; 2212889facbeSLen Brown } 2213889facbeSLen Brown 2214889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 2215d7899447SLen Brown if (!do_nhm_platform_info) 2216889facbeSLen Brown goto guess; 2217889facbeSLen Brown 2218889facbeSLen Brown if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr)) 2219889facbeSLen Brown goto guess; 2220889facbeSLen Brown 22213482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 2222889facbeSLen Brown 2223d8af6f5fSLen Brown if (debug) 2224889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 2225889facbeSLen Brown cpu, msr, target_c_local); 2226889facbeSLen Brown 22273482124aSJean Delvare if (!target_c_local) 2228889facbeSLen Brown goto guess; 2229889facbeSLen Brown 2230889facbeSLen Brown tcc_activation_temp = target_c_local; 2231889facbeSLen Brown 2232889facbeSLen Brown return 0; 2233889facbeSLen Brown 2234889facbeSLen Brown guess: 2235889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 2236889facbeSLen Brown fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 2237889facbeSLen Brown cpu, tcc_activation_temp); 2238889facbeSLen Brown 2239889facbeSLen Brown return 0; 2240889facbeSLen Brown } 2241103a8feaSLen Brown void check_cpuid() 2242103a8feaSLen Brown { 2243103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 2244103a8feaSLen Brown unsigned int fms, family, model, stepping; 2245103a8feaSLen Brown 2246103a8feaSLen Brown eax = ebx = ecx = edx = 0; 2247103a8feaSLen Brown 22482b92865eSJosh Triplett __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 2249103a8feaSLen Brown 2250103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 2251103a8feaSLen Brown genuine_intel = 1; 2252103a8feaSLen Brown 2253d8af6f5fSLen Brown if (debug) 2254889facbeSLen Brown fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 2255103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 2256103a8feaSLen Brown 22572b92865eSJosh Triplett __get_cpuid(1, &fms, &ebx, &ecx, &edx); 2258103a8feaSLen Brown family = (fms >> 8) & 0xf; 2259103a8feaSLen Brown model = (fms >> 4) & 0xf; 2260103a8feaSLen Brown stepping = fms & 0xf; 2261103a8feaSLen Brown if (family == 6 || family == 0xf) 2262103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 2263103a8feaSLen Brown 2264d8af6f5fSLen Brown if (debug) 2265103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 2266103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 2267103a8feaSLen Brown 2268b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 2269b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 2270103a8feaSLen Brown 2271103a8feaSLen Brown /* 2272103a8feaSLen Brown * check max extended function levels of CPUID. 2273103a8feaSLen Brown * This is needed to check for invariant TSC. 2274103a8feaSLen Brown * This check is valid for both Intel and AMD. 2275103a8feaSLen Brown */ 2276103a8feaSLen Brown ebx = ecx = edx = 0; 22772b92865eSJosh Triplett __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); 2278103a8feaSLen Brown 2279d7899447SLen Brown if (max_level >= 0x80000007) { 2280103a8feaSLen Brown 2281103a8feaSLen Brown /* 2282103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 2283103a8feaSLen Brown * this check is valid for both Intel and AMD 2284103a8feaSLen Brown */ 22852b92865eSJosh Triplett __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 22868209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 2287d7899447SLen Brown } 2288103a8feaSLen Brown 2289103a8feaSLen Brown /* 2290103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 2291103a8feaSLen Brown * this check is valid for both Intel and AMD 2292103a8feaSLen Brown */ 2293103a8feaSLen Brown 22942b92865eSJosh Triplett __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 22958209e054SThomas Renninger has_aperf = ecx & (1 << 0); 2296889facbeSLen Brown do_dts = eax & (1 << 0); 2297889facbeSLen Brown do_ptm = eax & (1 << 6); 2298889facbeSLen Brown has_epb = ecx & (1 << 3); 2299889facbeSLen Brown 2300d8af6f5fSLen Brown if (debug) 2301a729617cSLen Brown fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n", 2302a729617cSLen Brown has_aperf ? "" : "No ", 2303a729617cSLen Brown do_dts ? "" : "No ", 2304a729617cSLen Brown do_ptm ? "" : "No ", 2305a729617cSLen Brown has_epb ? "" : "No "); 2306103a8feaSLen Brown 2307ee7e38e3SLen Brown do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); 2308d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 2309ee7e38e3SLen Brown do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); 2310ee7e38e3SLen Brown do_pc3 = (pkg_cstate_limit >= PCL__3); 2311ee7e38e3SLen Brown do_pc6 = (pkg_cstate_limit >= PCL__6); 2312ee7e38e3SLen Brown do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); 2313d7899447SLen Brown do_c8_c9_c10 = has_hsw_msrs(family, model); 2314144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 2315103a8feaSLen Brown bclk = discover_bclk(family, model); 2316103a8feaSLen Brown 2317ee7e38e3SLen Brown do_nhm_turbo_ratio_limit = do_nhm_platform_info && has_nhm_turbo_ratio_limit(family, model); 23186574a5d5SLen Brown do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 2319889facbeSLen Brown rapl_probe(family, model); 23203a9a941dSLen Brown perf_limit_reasons_probe(family, model); 2321889facbeSLen Brown 2322889facbeSLen Brown return; 2323103a8feaSLen Brown } 2324103a8feaSLen Brown 2325103a8feaSLen Brown 2326d8af6f5fSLen Brown void help() 2327103a8feaSLen Brown { 2328d8af6f5fSLen Brown fprintf(stderr, 2329d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 2330d8af6f5fSLen Brown "\n" 2331d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 2332d8af6f5fSLen Brown "when COMMAND completes.\n" 2333d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 2334d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 2335d8af6f5fSLen Brown "--debug run in \"debug\" mode\n" 2336d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 2337d8af6f5fSLen Brown "--help print this help message\n" 2338d8af6f5fSLen Brown "--counter msr print 32-bit counter at address \"msr\"\n" 2339d8af6f5fSLen Brown "--Counter msr print 64-bit Counter at address \"msr\"\n" 2340d8af6f5fSLen Brown "--msr msr print 32-bit value at address \"msr\"\n" 2341d8af6f5fSLen Brown "--MSR msr print 64-bit Value at address \"msr\"\n" 2342d8af6f5fSLen Brown "--version print version information\n" 2343d8af6f5fSLen Brown "\n" 2344d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 2345103a8feaSLen Brown } 2346103a8feaSLen Brown 2347103a8feaSLen Brown 2348103a8feaSLen Brown /* 2349103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 2350103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 2351103a8feaSLen Brown */ 2352103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 2353103a8feaSLen Brown { 2354103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 2355103a8feaSLen Brown return 1; 2356103a8feaSLen Brown else 2357103a8feaSLen Brown return 0; 2358103a8feaSLen Brown } 2359103a8feaSLen Brown 2360103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 2361103a8feaSLen Brown { 2362103a8feaSLen Brown return 0; 2363103a8feaSLen Brown } 2364103a8feaSLen Brown 2365c98d5d94SLen Brown void topology_probe() 2366c98d5d94SLen Brown { 2367c98d5d94SLen Brown int i; 2368c98d5d94SLen Brown int max_core_id = 0; 2369c98d5d94SLen Brown int max_package_id = 0; 2370c98d5d94SLen Brown int max_siblings = 0; 2371c98d5d94SLen Brown struct cpu_topology { 2372c98d5d94SLen Brown int core_id; 2373c98d5d94SLen Brown int physical_package_id; 2374c98d5d94SLen Brown } *cpus; 2375c98d5d94SLen Brown 2376c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 2377c98d5d94SLen Brown topo.num_cpus = 0; 2378c98d5d94SLen Brown topo.max_cpu_num = 0; 2379c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 2380c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 2381c98d5d94SLen Brown show_cpu = 1; 2382c98d5d94SLen Brown 2383d8af6f5fSLen Brown if (debug > 1) 2384c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 2385c98d5d94SLen Brown 2386c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 2387b2c95d90SJosh Triplett if (cpus == NULL) 2388b2c95d90SJosh Triplett err(1, "calloc cpus"); 2389c98d5d94SLen Brown 2390c98d5d94SLen Brown /* 2391c98d5d94SLen Brown * Allocate and initialize cpu_present_set 2392c98d5d94SLen Brown */ 2393c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2394b2c95d90SJosh Triplett if (cpu_present_set == NULL) 2395b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2396c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2397c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 2398c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 2399c98d5d94SLen Brown 2400c98d5d94SLen Brown /* 2401c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 2402c98d5d94SLen Brown */ 2403c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2404b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 2405b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2406c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2407c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 2408c98d5d94SLen Brown 2409c98d5d94SLen Brown 2410c98d5d94SLen Brown /* 2411c98d5d94SLen Brown * For online cpus 2412c98d5d94SLen Brown * find max_core_id, max_package_id 2413c98d5d94SLen Brown */ 2414c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 2415c98d5d94SLen Brown int siblings; 2416c98d5d94SLen Brown 2417c98d5d94SLen Brown if (cpu_is_not_present(i)) { 2418d8af6f5fSLen Brown if (debug > 1) 2419c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 2420c98d5d94SLen Brown continue; 2421c98d5d94SLen Brown } 2422c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 2423c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 2424c98d5d94SLen Brown max_core_id = cpus[i].core_id; 2425c98d5d94SLen Brown 2426c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 2427c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 2428c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 2429c98d5d94SLen Brown 2430c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 2431c98d5d94SLen Brown if (siblings > max_siblings) 2432c98d5d94SLen Brown max_siblings = siblings; 2433d8af6f5fSLen Brown if (debug > 1) 2434c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 2435c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 2436c98d5d94SLen Brown } 2437c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 2438d8af6f5fSLen Brown if (debug > 1) 2439c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 2440c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 24411cc21f7bSLen Brown if (debug && !summary_only && topo.num_cores_per_pkg > 1) 2442c98d5d94SLen Brown show_core = 1; 2443c98d5d94SLen Brown 2444c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 2445d8af6f5fSLen Brown if (debug > 1) 2446c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 2447c98d5d94SLen Brown max_package_id, topo.num_packages); 24481cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 2449c98d5d94SLen Brown show_pkg = 1; 2450c98d5d94SLen Brown 2451c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 2452d8af6f5fSLen Brown if (debug > 1) 2453c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 2454c98d5d94SLen Brown 2455c98d5d94SLen Brown free(cpus); 2456c98d5d94SLen Brown } 2457c98d5d94SLen Brown 2458c98d5d94SLen Brown void 2459c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 2460c98d5d94SLen Brown { 2461c98d5d94SLen Brown int i; 2462c98d5d94SLen Brown 2463c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 2464c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 2465c98d5d94SLen Brown if (*t == NULL) 2466c98d5d94SLen Brown goto error; 2467c98d5d94SLen Brown 2468c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 2469c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 2470c98d5d94SLen Brown (*t)[i].cpu_id = -1; 2471c98d5d94SLen Brown 2472c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 2473c98d5d94SLen Brown sizeof(struct core_data)); 2474c98d5d94SLen Brown if (*c == NULL) 2475c98d5d94SLen Brown goto error; 2476c98d5d94SLen Brown 2477c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 2478c98d5d94SLen Brown (*c)[i].core_id = -1; 2479c98d5d94SLen Brown 2480c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 2481c98d5d94SLen Brown if (*p == NULL) 2482c98d5d94SLen Brown goto error; 2483c98d5d94SLen Brown 2484c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 2485c98d5d94SLen Brown (*p)[i].package_id = i; 2486c98d5d94SLen Brown 2487c98d5d94SLen Brown return; 2488c98d5d94SLen Brown error: 2489b2c95d90SJosh Triplett err(1, "calloc counters"); 2490c98d5d94SLen Brown } 2491c98d5d94SLen Brown /* 2492c98d5d94SLen Brown * init_counter() 2493c98d5d94SLen Brown * 2494c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 2495c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 2496c98d5d94SLen Brown * 2497c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 2498c98d5d94SLen Brown */ 2499c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 2500c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 2501c98d5d94SLen Brown int pkg_num, int cpu_id) 2502c98d5d94SLen Brown { 2503c98d5d94SLen Brown struct thread_data *t; 2504c98d5d94SLen Brown struct core_data *c; 2505c98d5d94SLen Brown struct pkg_data *p; 2506c98d5d94SLen Brown 2507c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 2508c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 2509c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 2510c98d5d94SLen Brown 2511c98d5d94SLen Brown t->cpu_id = cpu_id; 2512c98d5d94SLen Brown if (thread_num == 0) { 2513c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 2514c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 2515c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 2516c98d5d94SLen Brown } 2517c98d5d94SLen Brown 2518c98d5d94SLen Brown c->core_id = core_num; 2519c98d5d94SLen Brown p->package_id = pkg_num; 2520c98d5d94SLen Brown } 2521c98d5d94SLen Brown 2522c98d5d94SLen Brown 2523c98d5d94SLen Brown int initialize_counters(int cpu_id) 2524c98d5d94SLen Brown { 2525c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 2526c98d5d94SLen Brown 2527c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 2528c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 2529c98d5d94SLen Brown 2530c98d5d94SLen Brown if (cpu_is_first_sibling_in_core(cpu_id)) { 2531c98d5d94SLen Brown my_thread_id = 0; 2532c98d5d94SLen Brown topo.num_cores++; 2533c98d5d94SLen Brown } else { 2534c98d5d94SLen Brown my_thread_id = 1; 2535c98d5d94SLen Brown } 2536c98d5d94SLen Brown 2537c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2538c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2539c98d5d94SLen Brown return 0; 2540c98d5d94SLen Brown } 2541c98d5d94SLen Brown 2542c98d5d94SLen Brown void allocate_output_buffer() 2543c98d5d94SLen Brown { 25443b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 2545c98d5d94SLen Brown outp = output_buffer; 2546b2c95d90SJosh Triplett if (outp == NULL) 2547b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 2548c98d5d94SLen Brown } 2549c98d5d94SLen Brown 2550c98d5d94SLen Brown void setup_all_buffers(void) 2551c98d5d94SLen Brown { 2552c98d5d94SLen Brown topology_probe(); 2553c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 2554c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 2555c98d5d94SLen Brown allocate_output_buffer(); 2556c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 2557c98d5d94SLen Brown } 25583b4d5c7fSAndy Shevchenko 2559103a8feaSLen Brown void turbostat_init() 2560103a8feaSLen Brown { 2561103a8feaSLen Brown check_dev_msr(); 256298481e79SLen Brown check_permissions(); 256398481e79SLen Brown check_cpuid(); 2564103a8feaSLen Brown 2565c98d5d94SLen Brown setup_all_buffers(); 2566103a8feaSLen Brown 2567d8af6f5fSLen Brown if (debug) 25681cc21f7bSLen Brown dump_system_config_info(); 2569889facbeSLen Brown 2570d8af6f5fSLen Brown if (debug) 2571889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 2572889facbeSLen Brown 2573d8af6f5fSLen Brown if (debug) 25743a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 25753a9a941dSLen Brown 2576d8af6f5fSLen Brown if (debug) 2577889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 2578889facbeSLen Brown 2579889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 2580889facbeSLen Brown 2581d8af6f5fSLen Brown if (debug) 2582889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 2583103a8feaSLen Brown } 2584103a8feaSLen Brown 2585103a8feaSLen Brown int fork_it(char **argv) 2586103a8feaSLen Brown { 2587103a8feaSLen Brown pid_t child_pid; 2588d91bb17cSLen Brown int status; 2589d15cf7c1SLen Brown 2590d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 2591d91bb17cSLen Brown if (status) 2592d91bb17cSLen Brown exit(status); 2593d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 2594d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 2595103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2596103a8feaSLen Brown 2597103a8feaSLen Brown child_pid = fork(); 2598103a8feaSLen Brown if (!child_pid) { 2599103a8feaSLen Brown /* child */ 2600103a8feaSLen Brown execvp(argv[0], argv); 2601103a8feaSLen Brown } else { 2602103a8feaSLen Brown 2603103a8feaSLen Brown /* parent */ 2604b2c95d90SJosh Triplett if (child_pid == -1) 2605b2c95d90SJosh Triplett err(1, "fork"); 2606103a8feaSLen Brown 2607103a8feaSLen Brown signal(SIGINT, SIG_IGN); 2608103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 2609b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 2610b2c95d90SJosh Triplett err(status, "waitpid"); 2611103a8feaSLen Brown } 2612c98d5d94SLen Brown /* 2613c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 2614c98d5d94SLen Brown * because re-starting is problematic when forking 2615c98d5d94SLen Brown */ 2616c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 2617103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2618103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2619c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2620c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2621c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2622c98d5d94SLen Brown flush_stderr(); 2623103a8feaSLen Brown 26246eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 2625103a8feaSLen Brown 2626d91bb17cSLen Brown return status; 2627103a8feaSLen Brown } 2628103a8feaSLen Brown 26293b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 26303b4d5c7fSAndy Shevchenko { 26313b4d5c7fSAndy Shevchenko int status; 26323b4d5c7fSAndy Shevchenko 26333b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 26343b4d5c7fSAndy Shevchenko if (status) 26353b4d5c7fSAndy Shevchenko return status; 26363b4d5c7fSAndy Shevchenko 26373b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 26383b4d5c7fSAndy Shevchenko if (status) 26393b4d5c7fSAndy Shevchenko return status; 26403b4d5c7fSAndy Shevchenko 26413b4d5c7fSAndy Shevchenko flush_stdout(); 26423b4d5c7fSAndy Shevchenko 26433b4d5c7fSAndy Shevchenko return status; 26443b4d5c7fSAndy Shevchenko } 26453b4d5c7fSAndy Shevchenko 2646d8af6f5fSLen Brown void print_version() { 264748a0631cSLen Brown fprintf(stderr, "turbostat version 4.1 10-Feb, 2015" 2648d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 2649d8af6f5fSLen Brown } 2650d8af6f5fSLen Brown 2651103a8feaSLen Brown void cmdline(int argc, char **argv) 2652103a8feaSLen Brown { 2653103a8feaSLen Brown int opt; 2654d8af6f5fSLen Brown int option_index = 0; 2655d8af6f5fSLen Brown static struct option long_options[] = { 2656d8af6f5fSLen Brown {"Counter", required_argument, 0, 'C'}, 2657d8af6f5fSLen Brown {"counter", required_argument, 0, 'c'}, 2658d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 2659d8af6f5fSLen Brown {"debug", no_argument, 0, 'd'}, 2660d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 2661d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 2662d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 2663d8af6f5fSLen Brown {"MSR", required_argument, 0, 'M'}, 2664d8af6f5fSLen Brown {"msr", required_argument, 0, 'm'}, 2665d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 2666d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 2667d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 2668d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 2669d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 2670d8af6f5fSLen Brown {0, 0, 0, 0 } 2671d8af6f5fSLen Brown }; 2672103a8feaSLen Brown 2673103a8feaSLen Brown progname = argv[0]; 2674103a8feaSLen Brown 2675d8af6f5fSLen Brown while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v", 2676d8af6f5fSLen Brown long_options, &option_index)) != -1) { 2677103a8feaSLen Brown switch (opt) { 2678d8af6f5fSLen Brown case 'C': 2679d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 2680103a8feaSLen Brown break; 2681f9240813SLen Brown case 'c': 26828e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 26838e180f3cSLen Brown break; 2684d8af6f5fSLen Brown case 'D': 2685d8af6f5fSLen Brown dump_only++; 26868e180f3cSLen Brown break; 2687d8af6f5fSLen Brown case 'd': 2688d8af6f5fSLen Brown debug++; 26892f32edf1SLen Brown break; 2690d8af6f5fSLen Brown case 'h': 2691d8af6f5fSLen Brown default: 2692d8af6f5fSLen Brown help(); 2693d8af6f5fSLen Brown exit(1); 2694d8af6f5fSLen Brown case 'i': 2695d8af6f5fSLen Brown interval_sec = atoi(optarg); 2696889facbeSLen Brown break; 26975c56be9aSDirk Brandewie case 'J': 26985c56be9aSDirk Brandewie rapl_joules++; 26995c56be9aSDirk Brandewie break; 2700d8af6f5fSLen Brown case 'M': 2701d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 2702d8af6f5fSLen Brown break; 2703d8af6f5fSLen Brown case 'm': 2704d8af6f5fSLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 2705d8af6f5fSLen Brown break; 2706d8af6f5fSLen Brown case 'P': 2707d8af6f5fSLen Brown show_pkg_only++; 2708d8af6f5fSLen Brown break; 2709d8af6f5fSLen Brown case 'p': 2710d8af6f5fSLen Brown show_core_only++; 2711d8af6f5fSLen Brown break; 2712d8af6f5fSLen Brown case 'S': 2713d8af6f5fSLen Brown summary_only++; 2714d8af6f5fSLen Brown break; 2715d8af6f5fSLen Brown case 'T': 2716d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 2717d8af6f5fSLen Brown break; 2718d8af6f5fSLen Brown case 'v': 2719d8af6f5fSLen Brown print_version(); 2720d8af6f5fSLen Brown exit(0); 2721d8af6f5fSLen Brown break; 2722103a8feaSLen Brown } 2723103a8feaSLen Brown } 2724103a8feaSLen Brown } 2725103a8feaSLen Brown 2726103a8feaSLen Brown int main(int argc, char **argv) 2727103a8feaSLen Brown { 2728103a8feaSLen Brown cmdline(argc, argv); 2729103a8feaSLen Brown 2730d8af6f5fSLen Brown if (debug) 2731d8af6f5fSLen Brown print_version(); 2732103a8feaSLen Brown 2733103a8feaSLen Brown turbostat_init(); 2734103a8feaSLen Brown 27353b4d5c7fSAndy Shevchenko /* dump counters and exit */ 27363b4d5c7fSAndy Shevchenko if (dump_only) 27373b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 27383b4d5c7fSAndy Shevchenko 2739103a8feaSLen Brown /* 2740103a8feaSLen Brown * if any params left, it must be a command to fork 2741103a8feaSLen Brown */ 2742103a8feaSLen Brown if (argc - optind) 2743103a8feaSLen Brown return fork_it(argv + optind); 2744103a8feaSLen Brown else 2745103a8feaSLen Brown turbostat_loop(); 2746103a8feaSLen Brown 2747103a8feaSLen Brown return 0; 2748103a8feaSLen Brown } 2749