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> 36103a8feaSLen Brown #include <dirent.h> 37103a8feaSLen Brown #include <string.h> 38103a8feaSLen Brown #include <ctype.h> 3988c3281fSLen Brown #include <sched.h> 402b92865eSJosh Triplett #include <cpuid.h> 41103a8feaSLen Brown 42103a8feaSLen Brown char *proc_stat = "/proc/stat"; 43103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 44103a8feaSLen Brown unsigned int verbose; /* set with -v */ 45889facbeSLen Brown unsigned int rapl_verbose; /* set with -R */ 46889facbeSLen Brown unsigned int thermal_verbose; /* set with -T */ 47e23da037SLen Brown unsigned int summary_only; /* set with -s */ 48103a8feaSLen Brown unsigned int skip_c0; 49103a8feaSLen Brown unsigned int skip_c1; 50103a8feaSLen Brown unsigned int do_nhm_cstates; 51103a8feaSLen Brown unsigned int do_snb_cstates; 52ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10; 53144b44b1SLen Brown unsigned int do_slm_cstates; 54144b44b1SLen Brown unsigned int use_c1_residency_msr; 55103a8feaSLen Brown unsigned int has_aperf; 56889facbeSLen Brown unsigned int has_epb; 57103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 58103a8feaSLen Brown unsigned int genuine_intel; 59103a8feaSLen Brown unsigned int has_invariant_tsc; 60103a8feaSLen Brown unsigned int do_nehalem_platform_info; 61103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 626574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit; 632f32edf1SLen Brown unsigned int extra_msr_offset32; 642f32edf1SLen Brown unsigned int extra_msr_offset64; 658e180f3cSLen Brown unsigned int extra_delta_offset32; 668e180f3cSLen Brown unsigned int extra_delta_offset64; 671ed51011SLen Brown int do_smi; 68103a8feaSLen Brown double bclk; 69103a8feaSLen Brown unsigned int show_pkg; 70103a8feaSLen Brown unsigned int show_core; 71103a8feaSLen Brown unsigned int show_cpu; 72c98d5d94SLen Brown unsigned int show_pkg_only; 73c98d5d94SLen Brown unsigned int show_core_only; 74c98d5d94SLen Brown char *output_buffer, *outp; 75889facbeSLen Brown unsigned int do_rapl; 76889facbeSLen Brown unsigned int do_dts; 77889facbeSLen Brown unsigned int do_ptm; 78889facbeSLen Brown unsigned int tcc_activation_temp; 79889facbeSLen Brown unsigned int tcc_activation_temp_override; 80889facbeSLen Brown double rapl_power_units, rapl_energy_units, rapl_time_units; 81889facbeSLen Brown double rapl_joule_counter_range; 82889facbeSLen Brown 83889facbeSLen Brown #define RAPL_PKG (1 << 0) 84*e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 85*e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 86*e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 87*e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 88*e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 89*e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 90*e6f9bb3cSLen Brown 91889facbeSLen Brown #define RAPL_DRAM (1 << 3) 92*e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 93*e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 94*e6f9bb3cSLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 95*e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 96*e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 97*e6f9bb3cSLen Brown 98*e6f9bb3cSLen Brown #define RAPL_CORES (1 << 5) 99*e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 100*e6f9bb3cSLen Brown /* 0x639 MSR_PP0_ENERGY_STATUS */ 101*e6f9bb3cSLen Brown #define RAPL_CORE_POLICY (1 << 6) 102*e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 103*e6f9bb3cSLen Brown 104*e6f9bb3cSLen Brown 105*e6f9bb3cSLen Brown #define RAPL_GFX (1 << 7) 106*e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 107*e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 108*e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 109889facbeSLen Brown #define TJMAX_DEFAULT 100 110889facbeSLen Brown 111889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 112103a8feaSLen Brown 113103a8feaSLen Brown int aperf_mperf_unstable; 114103a8feaSLen Brown int backwards_count; 115103a8feaSLen Brown char *progname; 116103a8feaSLen Brown 117c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 118c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 119c98d5d94SLen Brown 120c98d5d94SLen Brown struct thread_data { 121c98d5d94SLen Brown unsigned long long tsc; 122c98d5d94SLen Brown unsigned long long aperf; 123c98d5d94SLen Brown unsigned long long mperf; 124144b44b1SLen Brown unsigned long long c1; 1252f32edf1SLen Brown unsigned long long extra_msr64; 1268e180f3cSLen Brown unsigned long long extra_delta64; 1278e180f3cSLen Brown unsigned long long extra_msr32; 1288e180f3cSLen Brown unsigned long long extra_delta32; 1291ed51011SLen Brown unsigned int smi_count; 130c98d5d94SLen Brown unsigned int cpu_id; 131c98d5d94SLen Brown unsigned int flags; 132c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 133c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 134c98d5d94SLen Brown } *thread_even, *thread_odd; 135c98d5d94SLen Brown 136c98d5d94SLen Brown struct core_data { 137c98d5d94SLen Brown unsigned long long c3; 138c98d5d94SLen Brown unsigned long long c6; 139c98d5d94SLen Brown unsigned long long c7; 140889facbeSLen Brown unsigned int core_temp_c; 141c98d5d94SLen Brown unsigned int core_id; 142c98d5d94SLen Brown } *core_even, *core_odd; 143c98d5d94SLen Brown 144c98d5d94SLen Brown struct pkg_data { 145c98d5d94SLen Brown unsigned long long pc2; 146c98d5d94SLen Brown unsigned long long pc3; 147c98d5d94SLen Brown unsigned long long pc6; 148c98d5d94SLen Brown unsigned long long pc7; 149ca58710fSKristen Carlson Accardi unsigned long long pc8; 150ca58710fSKristen Carlson Accardi unsigned long long pc9; 151ca58710fSKristen Carlson Accardi unsigned long long pc10; 152c98d5d94SLen Brown unsigned int package_id; 153889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 154889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 155889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 156889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 157889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 158889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 159889facbeSLen Brown unsigned int pkg_temp_c; 160889facbeSLen Brown 161c98d5d94SLen Brown } *package_even, *package_odd; 162c98d5d94SLen Brown 163c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 164c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 165c98d5d94SLen Brown 166c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 167c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 168c98d5d94SLen Brown topo.num_threads_per_core + \ 169c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 170c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 171c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 172c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 173c98d5d94SLen Brown 174c98d5d94SLen Brown struct system_summary { 175c98d5d94SLen Brown struct thread_data threads; 176c98d5d94SLen Brown struct core_data cores; 177c98d5d94SLen Brown struct pkg_data packages; 178c98d5d94SLen Brown } sum, average; 179c98d5d94SLen Brown 180c98d5d94SLen Brown 181c98d5d94SLen Brown struct topo_params { 182c98d5d94SLen Brown int num_packages; 183103a8feaSLen Brown int num_cpus; 184c98d5d94SLen Brown int num_cores; 185c98d5d94SLen Brown int max_cpu_num; 186c98d5d94SLen Brown int num_cores_per_pkg; 187c98d5d94SLen Brown int num_threads_per_core; 188c98d5d94SLen Brown } topo; 189103a8feaSLen Brown 190c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 191103a8feaSLen Brown 192c98d5d94SLen Brown void setup_all_buffers(void); 193103a8feaSLen Brown 194c98d5d94SLen Brown int cpu_is_not_present(int cpu) 195d15cf7c1SLen Brown { 196c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 197c98d5d94SLen Brown } 198c98d5d94SLen Brown /* 199c98d5d94SLen Brown * run func(thread, core, package) in topology order 200c98d5d94SLen Brown * skip non-present cpus 201c98d5d94SLen Brown */ 202c98d5d94SLen Brown 203c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 204c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 205c98d5d94SLen Brown { 206c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 207c98d5d94SLen Brown 208c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 209c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 210c98d5d94SLen Brown for (thread_no = 0; thread_no < 211c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 212c98d5d94SLen Brown struct thread_data *t; 213c98d5d94SLen Brown struct core_data *c; 214c98d5d94SLen Brown struct pkg_data *p; 215c98d5d94SLen Brown 216c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 217c98d5d94SLen Brown 218c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 219c98d5d94SLen Brown continue; 220c98d5d94SLen Brown 221c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 222c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 223c98d5d94SLen Brown 224c98d5d94SLen Brown retval = func(t, c, p); 225c98d5d94SLen Brown if (retval) 226c98d5d94SLen Brown return retval; 227c98d5d94SLen Brown } 228c98d5d94SLen Brown } 229c98d5d94SLen Brown } 230d15cf7c1SLen Brown return 0; 231d15cf7c1SLen Brown } 232d15cf7c1SLen Brown 23388c3281fSLen Brown int cpu_migrate(int cpu) 23488c3281fSLen Brown { 235c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 236c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 237c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 23888c3281fSLen Brown return -1; 23988c3281fSLen Brown else 24088c3281fSLen Brown return 0; 24188c3281fSLen Brown } 24288c3281fSLen Brown 24315aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 244103a8feaSLen Brown { 245103a8feaSLen Brown ssize_t retval; 246103a8feaSLen Brown char pathname[32]; 247103a8feaSLen Brown int fd; 248103a8feaSLen Brown 249103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 250103a8feaSLen Brown fd = open(pathname, O_RDONLY); 25115aaa346SLen Brown if (fd < 0) 25215aaa346SLen Brown return -1; 253103a8feaSLen Brown 25415aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 255103a8feaSLen Brown close(fd); 25615aaa346SLen Brown 257d91bb17cSLen Brown if (retval != sizeof *msr) { 2582e9c6bc7SJosh Triplett fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset); 25915aaa346SLen Brown return -1; 260d91bb17cSLen Brown } 26115aaa346SLen Brown 26215aaa346SLen Brown return 0; 263103a8feaSLen Brown } 264103a8feaSLen Brown 265a829eb4dSLen Brown void print_header(void) 266103a8feaSLen Brown { 267103a8feaSLen Brown if (show_pkg) 268c98d5d94SLen Brown outp += sprintf(outp, "pk"); 269e23da037SLen Brown if (show_pkg) 270c98d5d94SLen Brown outp += sprintf(outp, " "); 271103a8feaSLen Brown if (show_core) 272c98d5d94SLen Brown outp += sprintf(outp, "cor"); 273103a8feaSLen Brown if (show_cpu) 274c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 275e23da037SLen Brown if (show_pkg || show_core || show_cpu) 276c98d5d94SLen Brown outp += sprintf(outp, " "); 277103a8feaSLen Brown if (do_nhm_cstates) 278c98d5d94SLen Brown outp += sprintf(outp, " %%c0"); 279103a8feaSLen Brown if (has_aperf) 280c98d5d94SLen Brown outp += sprintf(outp, " GHz"); 281c98d5d94SLen Brown outp += sprintf(outp, " TSC"); 2821ed51011SLen Brown if (do_smi) 2831ed51011SLen Brown outp += sprintf(outp, " SMI"); 2848e180f3cSLen Brown if (extra_delta_offset32) 285f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 2868e180f3cSLen Brown if (extra_delta_offset64) 287f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 2882f32edf1SLen Brown if (extra_msr_offset32) 2898e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 2902f32edf1SLen Brown if (extra_msr_offset64) 2918e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 292103a8feaSLen Brown if (do_nhm_cstates) 293c98d5d94SLen Brown outp += sprintf(outp, " %%c1"); 294144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 295c98d5d94SLen Brown outp += sprintf(outp, " %%c3"); 296103a8feaSLen Brown if (do_nhm_cstates) 297c98d5d94SLen Brown outp += sprintf(outp, " %%c6"); 298103a8feaSLen Brown if (do_snb_cstates) 299c98d5d94SLen Brown outp += sprintf(outp, " %%c7"); 300889facbeSLen Brown 301889facbeSLen Brown if (do_dts) 302889facbeSLen Brown outp += sprintf(outp, " CTMP"); 303889facbeSLen Brown if (do_ptm) 304889facbeSLen Brown outp += sprintf(outp, " PTMP"); 305889facbeSLen Brown 306103a8feaSLen Brown if (do_snb_cstates) 307c98d5d94SLen Brown outp += sprintf(outp, " %%pc2"); 308144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 309c98d5d94SLen Brown outp += sprintf(outp, " %%pc3"); 310144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 311c98d5d94SLen Brown outp += sprintf(outp, " %%pc6"); 312103a8feaSLen Brown if (do_snb_cstates) 313c98d5d94SLen Brown outp += sprintf(outp, " %%pc7"); 314ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 315ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %%pc8"); 316ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %%pc9"); 317ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %%pc10"); 318ca58710fSKristen Carlson Accardi } 319103a8feaSLen Brown 320889facbeSLen Brown if (do_rapl & RAPL_PKG) 321889facbeSLen Brown outp += sprintf(outp, " Pkg_W"); 322889facbeSLen Brown if (do_rapl & RAPL_CORES) 323889facbeSLen Brown outp += sprintf(outp, " Cor_W"); 324889facbeSLen Brown if (do_rapl & RAPL_GFX) 325889facbeSLen Brown outp += sprintf(outp, " GFX_W"); 326889facbeSLen Brown if (do_rapl & RAPL_DRAM) 327889facbeSLen Brown outp += sprintf(outp, " RAM_W"); 328889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 329889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 330889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 331889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 332889facbeSLen Brown 333c98d5d94SLen Brown outp += sprintf(outp, "\n"); 334103a8feaSLen Brown } 335103a8feaSLen Brown 336c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 337c98d5d94SLen Brown struct pkg_data *p) 338103a8feaSLen Brown { 339c98d5d94SLen Brown fprintf(stderr, "t %p, c %p, p %p\n", t, c, p); 340c98d5d94SLen Brown 341c98d5d94SLen Brown if (t) { 342c98d5d94SLen Brown fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 343c98d5d94SLen Brown fprintf(stderr, "TSC: %016llX\n", t->tsc); 344c98d5d94SLen Brown fprintf(stderr, "aperf: %016llX\n", t->aperf); 345c98d5d94SLen Brown fprintf(stderr, "mperf: %016llX\n", t->mperf); 346c98d5d94SLen Brown fprintf(stderr, "c1: %016llX\n", t->c1); 3478e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 3488e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 3498e180f3cSLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 3508e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 3518e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 3522f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 353c98d5d94SLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 3542f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 3551ed51011SLen Brown if (do_smi) 3561ed51011SLen Brown fprintf(stderr, "SMI: %08X\n", t->smi_count); 357103a8feaSLen Brown } 358103a8feaSLen Brown 359c98d5d94SLen Brown if (c) { 360c98d5d94SLen Brown fprintf(stderr, "core: %d\n", c->core_id); 361c98d5d94SLen Brown fprintf(stderr, "c3: %016llX\n", c->c3); 362c98d5d94SLen Brown fprintf(stderr, "c6: %016llX\n", c->c6); 363c98d5d94SLen Brown fprintf(stderr, "c7: %016llX\n", c->c7); 364889facbeSLen Brown fprintf(stderr, "DTS: %dC\n", c->core_temp_c); 365c98d5d94SLen Brown } 366103a8feaSLen Brown 367c98d5d94SLen Brown if (p) { 368c98d5d94SLen Brown fprintf(stderr, "package: %d\n", p->package_id); 369c98d5d94SLen Brown fprintf(stderr, "pc2: %016llX\n", p->pc2); 370c98d5d94SLen Brown fprintf(stderr, "pc3: %016llX\n", p->pc3); 371c98d5d94SLen Brown fprintf(stderr, "pc6: %016llX\n", p->pc6); 372c98d5d94SLen Brown fprintf(stderr, "pc7: %016llX\n", p->pc7); 373ca58710fSKristen Carlson Accardi fprintf(stderr, "pc8: %016llX\n", p->pc8); 374ca58710fSKristen Carlson Accardi fprintf(stderr, "pc9: %016llX\n", p->pc9); 375ca58710fSKristen Carlson Accardi fprintf(stderr, "pc10: %016llX\n", p->pc10); 376889facbeSLen Brown fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg); 377889facbeSLen Brown fprintf(stderr, "Joules COR: %0X\n", p->energy_cores); 378889facbeSLen Brown fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx); 379889facbeSLen Brown fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram); 380889facbeSLen Brown fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status); 381889facbeSLen Brown fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status); 382889facbeSLen Brown fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c); 383c98d5d94SLen Brown } 384c98d5d94SLen Brown return 0; 385103a8feaSLen Brown } 386103a8feaSLen Brown 387e23da037SLen Brown /* 388e23da037SLen Brown * column formatting convention & formats 389e23da037SLen Brown * package: "pk" 2 columns %2d 390e23da037SLen Brown * core: "cor" 3 columns %3d 391e23da037SLen Brown * CPU: "CPU" 3 columns %3d 392889facbeSLen Brown * Pkg_W: %6.2 393889facbeSLen Brown * Cor_W: %6.2 394889facbeSLen Brown * GFX_W: %5.2 395889facbeSLen Brown * RAM_W: %5.2 396e23da037SLen Brown * GHz: "GHz" 3 columns %3.2 397e23da037SLen Brown * TSC: "TSC" 3 columns %3.2 3981ed51011SLen Brown * SMI: "SMI" 4 columns %4d 399e23da037SLen Brown * percentage " %pc3" %6.2 400889facbeSLen Brown * Perf Status percentage: %5.2 401889facbeSLen Brown * "CTMP" 4 columns %4d 402e23da037SLen Brown */ 403c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 404c98d5d94SLen Brown struct pkg_data *p) 405103a8feaSLen Brown { 406103a8feaSLen Brown double interval_float; 407889facbeSLen Brown char *fmt5, *fmt6; 408103a8feaSLen Brown 409c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 410c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 411c98d5d94SLen Brown return 0; 412c98d5d94SLen Brown 413c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 414c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 415c98d5d94SLen Brown return 0; 416c98d5d94SLen Brown 417103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 418103a8feaSLen Brown 419c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 420c98d5d94SLen Brown if (t == &average.threads) { 421103a8feaSLen Brown if (show_pkg) 422c98d5d94SLen Brown outp += sprintf(outp, " "); 423e23da037SLen Brown if (show_pkg && show_core) 424c98d5d94SLen Brown outp += sprintf(outp, " "); 425103a8feaSLen Brown if (show_core) 426c98d5d94SLen Brown outp += sprintf(outp, " "); 427103a8feaSLen Brown if (show_cpu) 428c98d5d94SLen Brown outp += sprintf(outp, " " " "); 429103a8feaSLen Brown } else { 430c98d5d94SLen Brown if (show_pkg) { 431c98d5d94SLen Brown if (p) 432c98d5d94SLen Brown outp += sprintf(outp, "%2d", p->package_id); 433c98d5d94SLen Brown else 434c98d5d94SLen Brown outp += sprintf(outp, " "); 435c98d5d94SLen Brown } 436e23da037SLen Brown if (show_pkg && show_core) 437c98d5d94SLen Brown outp += sprintf(outp, " "); 438c98d5d94SLen Brown if (show_core) { 439c98d5d94SLen Brown if (c) 440c98d5d94SLen Brown outp += sprintf(outp, "%3d", c->core_id); 441c98d5d94SLen Brown else 442c98d5d94SLen Brown outp += sprintf(outp, " "); 443c98d5d94SLen Brown } 444103a8feaSLen Brown if (show_cpu) 445c98d5d94SLen Brown outp += sprintf(outp, " %3d", t->cpu_id); 446103a8feaSLen Brown } 447103a8feaSLen Brown /* %c0 */ 448103a8feaSLen Brown if (do_nhm_cstates) { 449e23da037SLen Brown if (show_pkg || show_core || show_cpu) 450c98d5d94SLen Brown outp += sprintf(outp, " "); 451103a8feaSLen Brown if (!skip_c0) 452c98d5d94SLen Brown outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); 453103a8feaSLen Brown else 454c98d5d94SLen Brown outp += sprintf(outp, " ****"); 455103a8feaSLen Brown } 456103a8feaSLen Brown 457103a8feaSLen Brown /* GHz */ 458103a8feaSLen Brown if (has_aperf) { 459103a8feaSLen Brown if (!aperf_mperf_unstable) { 460c98d5d94SLen Brown outp += sprintf(outp, " %3.2f", 461c98d5d94SLen Brown 1.0 * t->tsc / units * t->aperf / 462c98d5d94SLen Brown t->mperf / interval_float); 463103a8feaSLen Brown } else { 464c98d5d94SLen Brown if (t->aperf > t->tsc || t->mperf > t->tsc) { 465c98d5d94SLen Brown outp += sprintf(outp, " ***"); 466103a8feaSLen Brown } else { 467c98d5d94SLen Brown outp += sprintf(outp, "%3.1f*", 468c98d5d94SLen Brown 1.0 * t->tsc / 469c98d5d94SLen Brown units * t->aperf / 470c98d5d94SLen Brown t->mperf / interval_float); 471103a8feaSLen Brown } 472103a8feaSLen Brown } 473103a8feaSLen Brown } 474103a8feaSLen Brown 475103a8feaSLen Brown /* TSC */ 476c98d5d94SLen Brown outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); 477103a8feaSLen Brown 4781ed51011SLen Brown /* SMI */ 4791ed51011SLen Brown if (do_smi) 4801ed51011SLen Brown outp += sprintf(outp, "%4d", t->smi_count); 4811ed51011SLen Brown 4828e180f3cSLen Brown /* delta */ 4838e180f3cSLen Brown if (extra_delta_offset32) 4848e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 4858e180f3cSLen Brown 4868e180f3cSLen Brown /* DELTA */ 4878e180f3cSLen Brown if (extra_delta_offset64) 4888e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 4892f32edf1SLen Brown /* msr */ 4902f32edf1SLen Brown if (extra_msr_offset32) 4918e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 4922f32edf1SLen Brown 493130ff304SLen Brown /* MSR */ 4942f32edf1SLen Brown if (extra_msr_offset64) 4952f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 496130ff304SLen Brown 497103a8feaSLen Brown if (do_nhm_cstates) { 498103a8feaSLen Brown if (!skip_c1) 499c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); 500103a8feaSLen Brown else 501c98d5d94SLen Brown outp += sprintf(outp, " ****"); 502103a8feaSLen Brown } 503c98d5d94SLen Brown 504c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 505c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 506c98d5d94SLen Brown goto done; 507c98d5d94SLen Brown 508144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 509c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); 510103a8feaSLen Brown if (do_nhm_cstates) 511c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); 512103a8feaSLen Brown if (do_snb_cstates) 513c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); 514c98d5d94SLen Brown 515889facbeSLen Brown if (do_dts) 516889facbeSLen Brown outp += sprintf(outp, " %4d", c->core_temp_c); 517889facbeSLen Brown 518c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 519c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 520c98d5d94SLen Brown goto done; 521c98d5d94SLen Brown 522889facbeSLen Brown if (do_ptm) 523889facbeSLen Brown outp += sprintf(outp, " %4d", p->pkg_temp_c); 524889facbeSLen Brown 525103a8feaSLen Brown if (do_snb_cstates) 526c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); 527144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 528c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); 529144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) 530c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); 531103a8feaSLen Brown if (do_snb_cstates) 532c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); 533ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 534ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc); 535ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc); 536ca58710fSKristen Carlson Accardi outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc); 537ca58710fSKristen Carlson Accardi } 538889facbeSLen Brown 539889facbeSLen Brown /* 540889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 541889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 542889facbeSLen Brown */ 543889facbeSLen Brown if (interval_float < rapl_joule_counter_range) { 544889facbeSLen Brown fmt5 = " %5.2f"; 545889facbeSLen Brown fmt6 = " %6.2f"; 546889facbeSLen Brown } else { 547889facbeSLen Brown fmt5 = " %3.0f**"; 548889facbeSLen Brown fmt6 = " %4.0f**"; 549889facbeSLen Brown } 550889facbeSLen Brown 551889facbeSLen Brown if (do_rapl & RAPL_PKG) 552889facbeSLen Brown outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float); 553889facbeSLen Brown if (do_rapl & RAPL_CORES) 554889facbeSLen Brown outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float); 555889facbeSLen Brown if (do_rapl & RAPL_GFX) 556889facbeSLen Brown outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float); 557889facbeSLen Brown if (do_rapl & RAPL_DRAM) 558889facbeSLen Brown outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float); 559889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS ) 560889facbeSLen Brown outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 561889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS ) 562889facbeSLen Brown outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 563889facbeSLen Brown 564c98d5d94SLen Brown done: 565c98d5d94SLen Brown outp += sprintf(outp, "\n"); 566c98d5d94SLen Brown 567c98d5d94SLen Brown return 0; 568103a8feaSLen Brown } 569103a8feaSLen Brown 570c98d5d94SLen Brown void flush_stdout() 571103a8feaSLen Brown { 572c98d5d94SLen Brown fputs(output_buffer, stdout); 573ddac0d68SLen Brown fflush(stdout); 574c98d5d94SLen Brown outp = output_buffer; 575c98d5d94SLen Brown } 576c98d5d94SLen Brown void flush_stderr() 577c98d5d94SLen Brown { 578c98d5d94SLen Brown fputs(output_buffer, stderr); 579c98d5d94SLen Brown outp = output_buffer; 580c98d5d94SLen Brown } 581c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 582c98d5d94SLen Brown { 583e23da037SLen Brown static int printed; 584103a8feaSLen Brown 585e23da037SLen Brown if (!printed || !summary_only) 586103a8feaSLen Brown print_header(); 587103a8feaSLen Brown 588c98d5d94SLen Brown if (topo.num_cpus > 1) 589c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 590c98d5d94SLen Brown &average.packages); 591103a8feaSLen Brown 592e23da037SLen Brown printed = 1; 593e23da037SLen Brown 594e23da037SLen Brown if (summary_only) 595e23da037SLen Brown return; 596e23da037SLen Brown 597c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 598103a8feaSLen Brown } 599103a8feaSLen Brown 600889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 601889facbeSLen Brown if (new > old) { \ 602889facbeSLen Brown old = new - old; \ 603889facbeSLen Brown } else { \ 604889facbeSLen Brown old = 0x100000000 + new - old; \ 605889facbeSLen Brown } 606889facbeSLen Brown 607c98d5d94SLen Brown void 608c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 609103a8feaSLen Brown { 610c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 611c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 612c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 613c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 614ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 615ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 616ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 617889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 618889facbeSLen Brown 619889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 620889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 621889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 622889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 623889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 624889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 625103a8feaSLen Brown } 626103a8feaSLen Brown 627c98d5d94SLen Brown void 628c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 629c98d5d94SLen Brown { 630c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 631c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 632c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 633889facbeSLen Brown old->core_temp_c = new->core_temp_c; 634103a8feaSLen Brown } 635c98d5d94SLen Brown 636c3ae331dSLen Brown /* 637c3ae331dSLen Brown * old = new - old 638c3ae331dSLen Brown */ 639c98d5d94SLen Brown void 640c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 641c98d5d94SLen Brown struct core_data *core_delta) 642c98d5d94SLen Brown { 643c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 644c98d5d94SLen Brown 645103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 646b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 647b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 648b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 649b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 650103a8feaSLen Brown 651c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 652c98d5d94SLen Brown 653c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 654c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 655c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 656c98d5d94SLen Brown } else { 657c98d5d94SLen Brown 658103a8feaSLen Brown if (!aperf_mperf_unstable) { 659103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 660103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 661103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 662103a8feaSLen Brown 663103a8feaSLen Brown aperf_mperf_unstable = 1; 664103a8feaSLen Brown } 665103a8feaSLen Brown /* 666103a8feaSLen Brown * mperf delta is likely a huge "positive" number 667103a8feaSLen Brown * can not use it for calculating c0 time 668103a8feaSLen Brown */ 669103a8feaSLen Brown skip_c0 = 1; 670103a8feaSLen Brown skip_c1 = 1; 671103a8feaSLen Brown } 672103a8feaSLen Brown 673c98d5d94SLen Brown 674144b44b1SLen Brown if (use_c1_residency_msr) { 675144b44b1SLen Brown /* 676144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 677144b44b1SLen Brown * which should be more accurate than the derivation below. 678144b44b1SLen Brown */ 679144b44b1SLen Brown } else { 680103a8feaSLen Brown /* 681c3ae331dSLen Brown * As counter collection is not atomic, 682c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 683103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 684103a8feaSLen Brown */ 685c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 686c98d5d94SLen Brown old->c1 = 0; 687c98d5d94SLen Brown else { 688c98d5d94SLen Brown /* normal case, derive c1 */ 689c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 690c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 691c98d5d94SLen Brown } 692144b44b1SLen Brown } 693c3ae331dSLen Brown 694c98d5d94SLen Brown if (old->mperf == 0) { 695c3ae331dSLen Brown if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 696c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 697c98d5d94SLen Brown } 698103a8feaSLen Brown 6998e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 7008e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 7018e180f3cSLen Brown 7028e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 7038e180f3cSLen Brown 704103a8feaSLen Brown /* 7058e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 706103a8feaSLen Brown */ 7072f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 7082f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 7091ed51011SLen Brown 7101ed51011SLen Brown if (do_smi) 7111ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 712103a8feaSLen Brown } 713c98d5d94SLen Brown 714c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 715c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 716c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 717c98d5d94SLen Brown { 718c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 719c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 720c98d5d94SLen Brown delta_core(c, c2); 721c98d5d94SLen Brown 722c98d5d94SLen Brown /* always calculate thread delta */ 723c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 724c98d5d94SLen Brown 725c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 726c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 727c98d5d94SLen Brown delta_package(p, p2); 728c98d5d94SLen Brown 729103a8feaSLen Brown return 0; 730103a8feaSLen Brown } 731103a8feaSLen Brown 732c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 733103a8feaSLen Brown { 734c98d5d94SLen Brown t->tsc = 0; 735c98d5d94SLen Brown t->aperf = 0; 736c98d5d94SLen Brown t->mperf = 0; 737c98d5d94SLen Brown t->c1 = 0; 738103a8feaSLen Brown 7391ed51011SLen Brown t->smi_count = 0; 7408e180f3cSLen Brown t->extra_delta32 = 0; 7418e180f3cSLen Brown t->extra_delta64 = 0; 7428e180f3cSLen Brown 743c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 744c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 745c98d5d94SLen Brown 746c98d5d94SLen Brown c->c3 = 0; 747c98d5d94SLen Brown c->c6 = 0; 748c98d5d94SLen Brown c->c7 = 0; 749889facbeSLen Brown c->core_temp_c = 0; 750c98d5d94SLen Brown 751c98d5d94SLen Brown p->pc2 = 0; 752c98d5d94SLen Brown p->pc3 = 0; 753c98d5d94SLen Brown p->pc6 = 0; 754c98d5d94SLen Brown p->pc7 = 0; 755ca58710fSKristen Carlson Accardi p->pc8 = 0; 756ca58710fSKristen Carlson Accardi p->pc9 = 0; 757ca58710fSKristen Carlson Accardi p->pc10 = 0; 758889facbeSLen Brown 759889facbeSLen Brown p->energy_pkg = 0; 760889facbeSLen Brown p->energy_dram = 0; 761889facbeSLen Brown p->energy_cores = 0; 762889facbeSLen Brown p->energy_gfx = 0; 763889facbeSLen Brown p->rapl_pkg_perf_status = 0; 764889facbeSLen Brown p->rapl_dram_perf_status = 0; 765889facbeSLen Brown p->pkg_temp_c = 0; 766103a8feaSLen Brown } 767c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 768c98d5d94SLen Brown struct pkg_data *p) 769103a8feaSLen Brown { 770c98d5d94SLen Brown average.threads.tsc += t->tsc; 771c98d5d94SLen Brown average.threads.aperf += t->aperf; 772c98d5d94SLen Brown average.threads.mperf += t->mperf; 773c98d5d94SLen Brown average.threads.c1 += t->c1; 77415aaa346SLen Brown 7758e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 7768e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 7778e180f3cSLen Brown 778c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 779c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 780c98d5d94SLen Brown return 0; 781c98d5d94SLen Brown 782c98d5d94SLen Brown average.cores.c3 += c->c3; 783c98d5d94SLen Brown average.cores.c6 += c->c6; 784c98d5d94SLen Brown average.cores.c7 += c->c7; 785c98d5d94SLen Brown 786889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 787889facbeSLen Brown 788c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 789c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 790c98d5d94SLen Brown return 0; 791c98d5d94SLen Brown 792c98d5d94SLen Brown average.packages.pc2 += p->pc2; 793c98d5d94SLen Brown average.packages.pc3 += p->pc3; 794c98d5d94SLen Brown average.packages.pc6 += p->pc6; 795c98d5d94SLen Brown average.packages.pc7 += p->pc7; 796ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 797ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 798ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 799c98d5d94SLen Brown 800889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 801889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 802889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 803889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 804889facbeSLen Brown 805889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 806889facbeSLen Brown 807889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 808889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 809c98d5d94SLen Brown return 0; 810c98d5d94SLen Brown } 811c98d5d94SLen Brown /* 812c98d5d94SLen Brown * sum the counters for all cpus in the system 813c98d5d94SLen Brown * compute the weighted average 814c98d5d94SLen Brown */ 815c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 816c98d5d94SLen Brown struct pkg_data *p) 817c98d5d94SLen Brown { 818c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 819c98d5d94SLen Brown 820c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 821c98d5d94SLen Brown 822c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 823c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 824c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 825c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 826c98d5d94SLen Brown 8278e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 8288e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 8298e180f3cSLen Brown 8308e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 8318e180f3cSLen Brown 832c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 833c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 834c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 835c98d5d94SLen Brown 836c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 837c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 838c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 839c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 840ca58710fSKristen Carlson Accardi 841ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 842ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 843ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 844c98d5d94SLen Brown } 845c98d5d94SLen Brown 846c98d5d94SLen Brown static unsigned long long rdtsc(void) 847c98d5d94SLen Brown { 848c98d5d94SLen Brown unsigned int low, high; 849c98d5d94SLen Brown 850c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 851c98d5d94SLen Brown 852c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 853c98d5d94SLen Brown } 854c98d5d94SLen Brown 855c98d5d94SLen Brown 856c98d5d94SLen Brown /* 857c98d5d94SLen Brown * get_counters(...) 858c98d5d94SLen Brown * migrate to cpu 859c98d5d94SLen Brown * acquire and record local counters for that cpu 860c98d5d94SLen Brown */ 861c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 862c98d5d94SLen Brown { 863c98d5d94SLen Brown int cpu = t->cpu_id; 864889facbeSLen Brown unsigned long long msr; 865c98d5d94SLen Brown 866e52966c0SLen Brown if (cpu_migrate(cpu)) { 867e52966c0SLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 86815aaa346SLen Brown return -1; 869e52966c0SLen Brown } 87015aaa346SLen Brown 871c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 87215aaa346SLen Brown 87315aaa346SLen Brown if (has_aperf) { 8749c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 875c98d5d94SLen Brown return -3; 8769c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 877c98d5d94SLen Brown return -4; 87888c3281fSLen Brown } 87988c3281fSLen Brown 8801ed51011SLen Brown if (do_smi) { 8811ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 8821ed51011SLen Brown return -5; 8831ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 8841ed51011SLen Brown } 8858e180f3cSLen Brown if (extra_delta_offset32) { 886889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 8872f32edf1SLen Brown return -5; 888889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 8898e180f3cSLen Brown } 8908e180f3cSLen Brown 8918e180f3cSLen Brown if (extra_delta_offset64) 8928e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 8938e180f3cSLen Brown return -5; 8948e180f3cSLen Brown 8958e180f3cSLen Brown if (extra_msr_offset32) { 896889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 8978e180f3cSLen Brown return -5; 898889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 8998e180f3cSLen Brown } 9002f32edf1SLen Brown 9012f32edf1SLen Brown if (extra_msr_offset64) 9022f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 903c98d5d94SLen Brown return -5; 904c98d5d94SLen Brown 905144b44b1SLen Brown if (use_c1_residency_msr) { 906144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 907144b44b1SLen Brown return -6; 908144b44b1SLen Brown } 909144b44b1SLen Brown 910c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 911c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 912c98d5d94SLen Brown return 0; 913c98d5d94SLen Brown 914144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) { 915c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 916c98d5d94SLen Brown return -6; 917144b44b1SLen Brown } 918144b44b1SLen Brown 919144b44b1SLen Brown if (do_nhm_cstates) { 920c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 921c98d5d94SLen Brown return -7; 922103a8feaSLen Brown } 92315aaa346SLen Brown 92415aaa346SLen Brown if (do_snb_cstates) 925c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 926c98d5d94SLen Brown return -8; 927c98d5d94SLen Brown 928889facbeSLen Brown if (do_dts) { 929889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 930889facbeSLen Brown return -9; 931889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 932889facbeSLen Brown } 933889facbeSLen Brown 934889facbeSLen Brown 935c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 936c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 937c98d5d94SLen Brown return 0; 93815aaa346SLen Brown 939144b44b1SLen Brown if (do_nhm_cstates && !do_slm_cstates) { 940c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 941c98d5d94SLen Brown return -9; 942c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 943c98d5d94SLen Brown return -10; 94415aaa346SLen Brown } 94515aaa346SLen Brown if (do_snb_cstates) { 946c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 947c98d5d94SLen Brown return -11; 948c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 949c98d5d94SLen Brown return -12; 95015aaa346SLen Brown } 951ca58710fSKristen Carlson Accardi if (do_c8_c9_c10) { 952ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 953ca58710fSKristen Carlson Accardi return -13; 954ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 955ca58710fSKristen Carlson Accardi return -13; 956ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 957ca58710fSKristen Carlson Accardi return -13; 958ca58710fSKristen Carlson Accardi } 959889facbeSLen Brown if (do_rapl & RAPL_PKG) { 960889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 961889facbeSLen Brown return -13; 962889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 963889facbeSLen Brown } 964889facbeSLen Brown if (do_rapl & RAPL_CORES) { 965889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 966889facbeSLen Brown return -14; 967889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 968889facbeSLen Brown } 969889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 970889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 971889facbeSLen Brown return -15; 972889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 973889facbeSLen Brown } 974889facbeSLen Brown if (do_rapl & RAPL_GFX) { 975889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 976889facbeSLen Brown return -16; 977889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 978889facbeSLen Brown } 979889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 980889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 981889facbeSLen Brown return -16; 982889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 983889facbeSLen Brown } 984889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 985889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 986889facbeSLen Brown return -16; 987889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 988889facbeSLen Brown } 989889facbeSLen Brown if (do_ptm) { 990889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 991889facbeSLen Brown return -17; 992889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 993889facbeSLen Brown } 99415aaa346SLen Brown return 0; 995103a8feaSLen Brown } 996103a8feaSLen Brown 997c98d5d94SLen Brown void print_verbose_header(void) 998103a8feaSLen Brown { 999103a8feaSLen Brown unsigned long long msr; 1000103a8feaSLen Brown unsigned int ratio; 1001103a8feaSLen Brown 1002103a8feaSLen Brown if (!do_nehalem_platform_info) 1003103a8feaSLen Brown return; 1004103a8feaSLen Brown 10059c63a650SLen Brown get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); 1006103a8feaSLen Brown 1007889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr); 10086574a5d5SLen Brown 1009103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1010103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 1011103a8feaSLen Brown ratio, bclk, ratio * bclk); 1012103a8feaSLen Brown 1013103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1014103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 1015103a8feaSLen Brown ratio, bclk, ratio * bclk); 1016103a8feaSLen Brown 101767920418SLen Brown get_msr(0, MSR_IA32_POWER_CTL, &msr); 1018144b44b1SLen Brown fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 101967920418SLen Brown msr, msr & 0x2 ? "EN" : "DIS"); 102067920418SLen Brown 10216574a5d5SLen Brown if (!do_ivt_turbo_ratio_limit) 10226574a5d5SLen Brown goto print_nhm_turbo_ratio_limits; 10236574a5d5SLen Brown 10246574a5d5SLen Brown get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 10256574a5d5SLen Brown 1026889facbeSLen Brown fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 10276574a5d5SLen Brown 10286574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 10296574a5d5SLen Brown if (ratio) 10306574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 10316574a5d5SLen Brown ratio, bclk, ratio * bclk); 10326574a5d5SLen Brown 10336574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 10346574a5d5SLen Brown if (ratio) 10356574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 10366574a5d5SLen Brown ratio, bclk, ratio * bclk); 10376574a5d5SLen Brown 10386574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 10396574a5d5SLen Brown if (ratio) 10406574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 10416574a5d5SLen Brown ratio, bclk, ratio * bclk); 10426574a5d5SLen Brown 10436574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 10446574a5d5SLen Brown if (ratio) 10456574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 10466574a5d5SLen Brown ratio, bclk, ratio * bclk); 10476574a5d5SLen Brown 10486574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 10496574a5d5SLen Brown if (ratio) 10506574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 10516574a5d5SLen Brown ratio, bclk, ratio * bclk); 10526574a5d5SLen Brown 10536574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 10546574a5d5SLen Brown if (ratio) 10556574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 10566574a5d5SLen Brown ratio, bclk, ratio * bclk); 10576574a5d5SLen Brown 10586574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 10596574a5d5SLen Brown if (ratio) 10606574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 10616574a5d5SLen Brown ratio, bclk, ratio * bclk); 10626574a5d5SLen Brown 10636574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 10646574a5d5SLen Brown if (ratio) 10656574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 10666574a5d5SLen Brown ratio, bclk, ratio * bclk); 10676574a5d5SLen Brown 10686574a5d5SLen Brown print_nhm_turbo_ratio_limits: 1069889facbeSLen Brown get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1070889facbeSLen Brown 1071889facbeSLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1072889facbeSLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1073889facbeSLen Brown 1074889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr); 1075889facbeSLen Brown 1076889facbeSLen Brown fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ", 1077889facbeSLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1078889facbeSLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1079889facbeSLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1080889facbeSLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1081889facbeSLen Brown (msr & (1 << 15)) ? "" : "UN", 1082889facbeSLen Brown (unsigned int)msr & 7); 1083889facbeSLen Brown 1084889facbeSLen Brown 1085889facbeSLen Brown switch(msr & 0x7) { 1086889facbeSLen Brown case 0: 1087144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0"); 1088889facbeSLen Brown break; 1089889facbeSLen Brown case 1: 1090144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0"); 1091889facbeSLen Brown break; 1092889facbeSLen Brown case 2: 1093144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3"); 1094889facbeSLen Brown break; 1095889facbeSLen Brown case 3: 1096144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "invalid" : "pc6"); 1097889facbeSLen Brown break; 1098889facbeSLen Brown case 4: 1099144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "pc4" : "pc7"); 1100889facbeSLen Brown break; 1101889facbeSLen Brown case 5: 1102144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid"); 1103144b44b1SLen Brown break; 1104144b44b1SLen Brown case 6: 1105144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "pc6" : "invalid"); 1106889facbeSLen Brown break; 1107889facbeSLen Brown case 7: 1108144b44b1SLen Brown fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited"); 1109889facbeSLen Brown break; 1110889facbeSLen Brown default: 1111889facbeSLen Brown fprintf(stderr, "invalid"); 1112889facbeSLen Brown } 1113889facbeSLen Brown fprintf(stderr, ")\n"); 1114103a8feaSLen Brown 1115103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 1116103a8feaSLen Brown return; 1117103a8feaSLen Brown 11189c63a650SLen Brown get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1119103a8feaSLen Brown 1120889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 11216574a5d5SLen Brown 11226574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 11236574a5d5SLen Brown if (ratio) 11246574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 11256574a5d5SLen Brown ratio, bclk, ratio * bclk); 11266574a5d5SLen Brown 11276574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 11286574a5d5SLen Brown if (ratio) 11296574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 11306574a5d5SLen Brown ratio, bclk, ratio * bclk); 11316574a5d5SLen Brown 11326574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 11336574a5d5SLen Brown if (ratio) 11346574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 11356574a5d5SLen Brown ratio, bclk, ratio * bclk); 11366574a5d5SLen Brown 11376574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 11386574a5d5SLen Brown if (ratio) 11396574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 11406574a5d5SLen Brown ratio, bclk, ratio * bclk); 11416574a5d5SLen Brown 1142103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1143103a8feaSLen Brown if (ratio) 1144103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1145103a8feaSLen Brown ratio, bclk, ratio * bclk); 1146103a8feaSLen Brown 1147103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1148103a8feaSLen Brown if (ratio) 1149103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1150103a8feaSLen Brown ratio, bclk, ratio * bclk); 1151103a8feaSLen Brown 1152103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1153103a8feaSLen Brown if (ratio) 1154103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1155103a8feaSLen Brown ratio, bclk, ratio * bclk); 1156103a8feaSLen Brown 1157103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1158103a8feaSLen Brown if (ratio) 1159103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1160103a8feaSLen Brown ratio, bclk, ratio * bclk); 1161103a8feaSLen Brown } 1162103a8feaSLen Brown 1163c98d5d94SLen Brown void free_all_buffers(void) 1164103a8feaSLen Brown { 1165c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1166c98d5d94SLen Brown cpu_present_set = NULL; 1167c98d5d94SLen Brown cpu_present_set = 0; 1168103a8feaSLen Brown 1169c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1170c98d5d94SLen Brown cpu_affinity_set = NULL; 1171c98d5d94SLen Brown cpu_affinity_setsize = 0; 1172103a8feaSLen Brown 1173c98d5d94SLen Brown free(thread_even); 1174c98d5d94SLen Brown free(core_even); 1175c98d5d94SLen Brown free(package_even); 1176103a8feaSLen Brown 1177c98d5d94SLen Brown thread_even = NULL; 1178c98d5d94SLen Brown core_even = NULL; 1179c98d5d94SLen Brown package_even = NULL; 1180103a8feaSLen Brown 1181c98d5d94SLen Brown free(thread_odd); 1182c98d5d94SLen Brown free(core_odd); 1183c98d5d94SLen Brown free(package_odd); 1184103a8feaSLen Brown 1185c98d5d94SLen Brown thread_odd = NULL; 1186c98d5d94SLen Brown core_odd = NULL; 1187c98d5d94SLen Brown package_odd = NULL; 1188103a8feaSLen Brown 1189c98d5d94SLen Brown free(output_buffer); 1190c98d5d94SLen Brown output_buffer = NULL; 1191c98d5d94SLen Brown outp = NULL; 1192103a8feaSLen Brown } 1193103a8feaSLen Brown 1194103a8feaSLen Brown /* 119557a42a34SJosh Triplett * Open a file, and exit on failure 119657a42a34SJosh Triplett */ 119757a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode) 119857a42a34SJosh Triplett { 119957a42a34SJosh Triplett FILE *filep = fopen(path, "r"); 1200b2c95d90SJosh Triplett if (!filep) 1201b2c95d90SJosh Triplett err(1, "%s: open failed", path); 120257a42a34SJosh Triplett return filep; 120357a42a34SJosh Triplett } 120457a42a34SJosh Triplett 120557a42a34SJosh Triplett /* 120695aebc44SJosh Triplett * Parse a file containing a single int. 120795aebc44SJosh Triplett */ 120895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 120995aebc44SJosh Triplett { 121095aebc44SJosh Triplett va_list args; 121195aebc44SJosh Triplett char path[PATH_MAX]; 121295aebc44SJosh Triplett FILE *filep; 121395aebc44SJosh Triplett int value; 121495aebc44SJosh Triplett 121595aebc44SJosh Triplett va_start(args, fmt); 121695aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 121795aebc44SJosh Triplett va_end(args); 121857a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1219b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 1220b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 122195aebc44SJosh Triplett fclose(filep); 122295aebc44SJosh Triplett return value; 122395aebc44SJosh Triplett } 122495aebc44SJosh Triplett 122595aebc44SJosh Triplett /* 1226c98d5d94SLen Brown * cpu_is_first_sibling_in_core(cpu) 1227c98d5d94SLen Brown * return 1 if given CPU is 1st HT sibling in the core 1228103a8feaSLen Brown */ 1229c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu) 1230103a8feaSLen Brown { 123195aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1232c98d5d94SLen Brown } 1233103a8feaSLen Brown 1234c98d5d94SLen Brown /* 1235c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1236c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1237c98d5d94SLen Brown */ 1238c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1239c98d5d94SLen Brown { 124095aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1241103a8feaSLen Brown } 1242103a8feaSLen Brown 1243103a8feaSLen Brown int get_physical_package_id(int cpu) 1244103a8feaSLen Brown { 124595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1246103a8feaSLen Brown } 1247103a8feaSLen Brown 1248103a8feaSLen Brown int get_core_id(int cpu) 1249103a8feaSLen Brown { 125095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1251103a8feaSLen Brown } 1252103a8feaSLen Brown 1253c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1254c98d5d94SLen Brown { 1255c98d5d94SLen Brown char path[80]; 1256c98d5d94SLen Brown FILE *filep; 1257c98d5d94SLen Brown int sib1, sib2; 1258c98d5d94SLen Brown int matches; 1259c98d5d94SLen Brown char character; 1260c98d5d94SLen Brown 1261c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 126257a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 1263103a8feaSLen Brown /* 1264c98d5d94SLen Brown * file format: 1265c98d5d94SLen Brown * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 1266c98d5d94SLen Brown * otherwinse 1 sibling (self). 1267c98d5d94SLen Brown */ 1268c98d5d94SLen Brown matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 1269c98d5d94SLen Brown 1270c98d5d94SLen Brown fclose(filep); 1271c98d5d94SLen Brown 1272c98d5d94SLen Brown if (matches == 3) 1273c98d5d94SLen Brown return 2; 1274c98d5d94SLen Brown else 1275c98d5d94SLen Brown return 1; 1276c98d5d94SLen Brown } 1277c98d5d94SLen Brown 1278c98d5d94SLen Brown /* 1279c98d5d94SLen Brown * run func(thread, core, package) in topology order 1280c98d5d94SLen Brown * skip non-present cpus 1281103a8feaSLen Brown */ 1282103a8feaSLen Brown 1283c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1284c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1285c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1286c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1287c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1288c98d5d94SLen Brown struct pkg_data *pkg_base2) 1289c98d5d94SLen Brown { 1290c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1291c98d5d94SLen Brown 1292c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1293c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1294c98d5d94SLen Brown for (thread_no = 0; thread_no < 1295c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1296c98d5d94SLen Brown struct thread_data *t, *t2; 1297c98d5d94SLen Brown struct core_data *c, *c2; 1298c98d5d94SLen Brown struct pkg_data *p, *p2; 1299c98d5d94SLen Brown 1300c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1301c98d5d94SLen Brown 1302c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1303c98d5d94SLen Brown continue; 1304c98d5d94SLen Brown 1305c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1306c98d5d94SLen Brown 1307c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1308c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1309c98d5d94SLen Brown 1310c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1311c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1312c98d5d94SLen Brown 1313c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1314c98d5d94SLen Brown if (retval) 1315c98d5d94SLen Brown return retval; 1316c98d5d94SLen Brown } 1317c98d5d94SLen Brown } 1318c98d5d94SLen Brown } 1319c98d5d94SLen Brown return 0; 1320c98d5d94SLen Brown } 1321c98d5d94SLen Brown 1322c98d5d94SLen Brown /* 1323c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1324c98d5d94SLen Brown * return max_cpu number 1325c98d5d94SLen Brown */ 1326c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1327103a8feaSLen Brown { 1328103a8feaSLen Brown FILE *fp; 1329c98d5d94SLen Brown int cpu_num; 1330103a8feaSLen Brown int retval; 1331103a8feaSLen Brown 133257a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 1333103a8feaSLen Brown 1334103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1335b2c95d90SJosh Triplett if (retval != 0) 1336b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 1337103a8feaSLen Brown 1338c98d5d94SLen Brown while (1) { 1339c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1340103a8feaSLen Brown if (retval != 1) 1341103a8feaSLen Brown break; 1342103a8feaSLen Brown 1343c98d5d94SLen Brown retval = func(cpu_num); 1344c98d5d94SLen Brown if (retval) { 1345c98d5d94SLen Brown fclose(fp); 1346c98d5d94SLen Brown return(retval); 1347c98d5d94SLen Brown } 1348103a8feaSLen Brown } 1349103a8feaSLen Brown fclose(fp); 1350c98d5d94SLen Brown return 0; 1351103a8feaSLen Brown } 1352103a8feaSLen Brown 1353103a8feaSLen Brown void re_initialize(void) 1354103a8feaSLen Brown { 1355c98d5d94SLen Brown free_all_buffers(); 1356c98d5d94SLen Brown setup_all_buffers(); 1357c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1358103a8feaSLen Brown } 1359103a8feaSLen Brown 1360c98d5d94SLen Brown 1361103a8feaSLen Brown /* 1362c98d5d94SLen Brown * count_cpus() 1363c98d5d94SLen Brown * remember the last one seen, it will be the max 1364103a8feaSLen Brown */ 1365c98d5d94SLen Brown int count_cpus(int cpu) 1366103a8feaSLen Brown { 1367c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1368c98d5d94SLen Brown topo.max_cpu_num = cpu; 1369103a8feaSLen Brown 1370c98d5d94SLen Brown topo.num_cpus += 1; 1371c98d5d94SLen Brown return 0; 1372103a8feaSLen Brown } 1373c98d5d94SLen Brown int mark_cpu_present(int cpu) 1374c98d5d94SLen Brown { 1375c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 137615aaa346SLen Brown return 0; 1377103a8feaSLen Brown } 1378103a8feaSLen Brown 1379103a8feaSLen Brown void turbostat_loop() 1380103a8feaSLen Brown { 1381c98d5d94SLen Brown int retval; 1382e52966c0SLen Brown int restarted = 0; 1383c98d5d94SLen Brown 1384103a8feaSLen Brown restart: 1385e52966c0SLen Brown restarted++; 1386e52966c0SLen Brown 1387c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1388d91bb17cSLen Brown if (retval < -1) { 1389d91bb17cSLen Brown exit(retval); 1390d91bb17cSLen Brown } else if (retval == -1) { 1391e52966c0SLen Brown if (restarted > 1) { 1392e52966c0SLen Brown exit(retval); 1393e52966c0SLen Brown } 1394c98d5d94SLen Brown re_initialize(); 1395c98d5d94SLen Brown goto restart; 1396c98d5d94SLen Brown } 1397e52966c0SLen Brown restarted = 0; 1398103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1399103a8feaSLen Brown 1400103a8feaSLen Brown while (1) { 1401c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1402103a8feaSLen Brown re_initialize(); 1403103a8feaSLen Brown goto restart; 1404103a8feaSLen Brown } 1405103a8feaSLen Brown sleep(interval_sec); 1406c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1407d91bb17cSLen Brown if (retval < -1) { 1408d91bb17cSLen Brown exit(retval); 1409d91bb17cSLen Brown } else if (retval == -1) { 141015aaa346SLen Brown re_initialize(); 141115aaa346SLen Brown goto restart; 141215aaa346SLen Brown } 1413103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1414103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1415c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1416c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1417c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1418c98d5d94SLen Brown flush_stdout(); 141915aaa346SLen Brown sleep(interval_sec); 1420c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1421d91bb17cSLen Brown if (retval < -1) { 1422d91bb17cSLen Brown exit(retval); 1423d91bb17cSLen Brown } else if (retval == -1) { 1424103a8feaSLen Brown re_initialize(); 1425103a8feaSLen Brown goto restart; 1426103a8feaSLen Brown } 1427103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1428103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1429c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1430c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1431c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1432c98d5d94SLen Brown flush_stdout(); 1433103a8feaSLen Brown } 1434103a8feaSLen Brown } 1435103a8feaSLen Brown 1436103a8feaSLen Brown void check_dev_msr() 1437103a8feaSLen Brown { 1438103a8feaSLen Brown struct stat sb; 1439103a8feaSLen Brown 1440b2c95d90SJosh Triplett if (stat("/dev/cpu/0/msr", &sb)) 1441b2c95d90SJosh Triplett err(-5, "no /dev/cpu/0/msr\n" 1442b2c95d90SJosh Triplett "Try \"# modprobe msr\""); 1443103a8feaSLen Brown } 1444103a8feaSLen Brown 1445103a8feaSLen Brown void check_super_user() 1446103a8feaSLen Brown { 1447b2c95d90SJosh Triplett if (getuid() != 0) 1448b2c95d90SJosh Triplett errx(-6, "must be root"); 1449103a8feaSLen Brown } 1450103a8feaSLen Brown 1451103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1452103a8feaSLen Brown { 1453103a8feaSLen Brown if (!genuine_intel) 1454103a8feaSLen Brown return 0; 1455103a8feaSLen Brown 1456103a8feaSLen Brown if (family != 6) 1457103a8feaSLen Brown return 0; 1458103a8feaSLen Brown 1459103a8feaSLen Brown switch (model) { 1460103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1461103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1462103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1463103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1464103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1465103a8feaSLen Brown case 0x2A: /* SNB */ 1466103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1467553575f1SLen Brown case 0x3A: /* IVB */ 14681300651bSLen Brown case 0x3E: /* IVB Xeon */ 146970b43400SLen Brown case 0x3C: /* HSW */ 1470*e6f9bb3cSLen Brown case 0x3F: /* HSX */ 147170b43400SLen Brown case 0x45: /* HSW */ 1472149c2319SLen Brown case 0x46: /* HSW */ 1473144b44b1SLen Brown case 0x37: /* BYT */ 1474144b44b1SLen Brown case 0x4D: /* AVN */ 1475103a8feaSLen Brown return 1; 1476103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1477103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1478103a8feaSLen Brown default: 1479103a8feaSLen Brown return 0; 1480103a8feaSLen Brown } 1481103a8feaSLen Brown } 14826574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 14836574a5d5SLen Brown { 14846574a5d5SLen Brown if (!genuine_intel) 14856574a5d5SLen Brown return 0; 14866574a5d5SLen Brown 14876574a5d5SLen Brown if (family != 6) 14886574a5d5SLen Brown return 0; 14896574a5d5SLen Brown 14906574a5d5SLen Brown switch (model) { 14916574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 14926574a5d5SLen Brown return 1; 14936574a5d5SLen Brown default: 14946574a5d5SLen Brown return 0; 14956574a5d5SLen Brown } 14966574a5d5SLen Brown } 14976574a5d5SLen Brown 1498889facbeSLen Brown /* 1499889facbeSLen Brown * print_epb() 1500889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 1501889facbeSLen Brown */ 1502889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1503889facbeSLen Brown { 1504889facbeSLen Brown unsigned long long msr; 1505889facbeSLen Brown char *epb_string; 1506889facbeSLen Brown int cpu; 1507889facbeSLen Brown 1508889facbeSLen Brown if (!has_epb) 1509889facbeSLen Brown return 0; 1510889facbeSLen Brown 1511889facbeSLen Brown cpu = t->cpu_id; 1512889facbeSLen Brown 1513889facbeSLen Brown /* EPB is per-package */ 1514889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1515889facbeSLen Brown return 0; 1516889facbeSLen Brown 1517889facbeSLen Brown if (cpu_migrate(cpu)) { 1518889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1519889facbeSLen Brown return -1; 1520889facbeSLen Brown } 1521889facbeSLen Brown 1522889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 1523889facbeSLen Brown return 0; 1524889facbeSLen Brown 1525889facbeSLen Brown switch (msr & 0x7) { 1526889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 1527889facbeSLen Brown epb_string = "performance"; 1528889facbeSLen Brown break; 1529889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 1530889facbeSLen Brown epb_string = "balanced"; 1531889facbeSLen Brown break; 1532889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 1533889facbeSLen Brown epb_string = "powersave"; 1534889facbeSLen Brown break; 1535889facbeSLen Brown default: 1536889facbeSLen Brown epb_string = "custom"; 1537889facbeSLen Brown break; 1538889facbeSLen Brown } 1539889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 1540889facbeSLen Brown 1541889facbeSLen Brown return 0; 1542889facbeSLen Brown } 1543889facbeSLen Brown 1544889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 1545889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 1546889facbeSLen Brown 1547144b44b1SLen Brown double get_tdp(model) 1548144b44b1SLen Brown { 1549144b44b1SLen Brown unsigned long long msr; 1550144b44b1SLen Brown 1551144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 1552144b44b1SLen Brown if (!get_msr(0, MSR_PKG_POWER_INFO, &msr)) 1553144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 1554144b44b1SLen Brown 1555144b44b1SLen Brown switch (model) { 1556144b44b1SLen Brown case 0x37: 1557144b44b1SLen Brown case 0x4D: 1558144b44b1SLen Brown return 30.0; 1559144b44b1SLen Brown default: 1560144b44b1SLen Brown return 135.0; 1561144b44b1SLen Brown } 1562144b44b1SLen Brown } 1563144b44b1SLen Brown 1564144b44b1SLen Brown 1565889facbeSLen Brown /* 1566889facbeSLen Brown * rapl_probe() 1567889facbeSLen Brown * 1568144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 1569889facbeSLen Brown */ 1570889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 1571889facbeSLen Brown { 1572889facbeSLen Brown unsigned long long msr; 1573144b44b1SLen Brown unsigned int time_unit; 1574889facbeSLen Brown double tdp; 1575889facbeSLen Brown 1576889facbeSLen Brown if (!genuine_intel) 1577889facbeSLen Brown return; 1578889facbeSLen Brown 1579889facbeSLen Brown if (family != 6) 1580889facbeSLen Brown return; 1581889facbeSLen Brown 1582889facbeSLen Brown switch (model) { 1583889facbeSLen Brown case 0x2A: 1584889facbeSLen Brown case 0x3A: 158570b43400SLen Brown case 0x3C: /* HSW */ 158670b43400SLen Brown case 0x45: /* HSW */ 1587149c2319SLen Brown case 0x46: /* HSW */ 1588144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 1589889facbeSLen Brown break; 1590*e6f9bb3cSLen Brown case 0x3F: /* HSX */ 1591*e6f9bb3cSLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 1592*e6f9bb3cSLen Brown break; 1593889facbeSLen Brown case 0x2D: 1594889facbeSLen Brown case 0x3E: 1595144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 1596144b44b1SLen Brown break; 1597144b44b1SLen Brown case 0x37: /* BYT */ 1598144b44b1SLen Brown case 0x4D: /* AVN */ 1599144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES ; 1600889facbeSLen Brown break; 1601889facbeSLen Brown default: 1602889facbeSLen Brown return; 1603889facbeSLen Brown } 1604889facbeSLen Brown 1605889facbeSLen Brown /* units on package 0, verify later other packages match */ 1606889facbeSLen Brown if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr)) 1607889facbeSLen Brown return; 1608889facbeSLen Brown 1609889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 1610144b44b1SLen Brown if (model == 0x37) 1611144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 1612144b44b1SLen Brown else 1613889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 1614889facbeSLen Brown 1615144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 1616144b44b1SLen Brown if (time_unit == 0) 1617144b44b1SLen Brown time_unit = 0xA; 1618889facbeSLen Brown 1619144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 1620144b44b1SLen Brown 1621144b44b1SLen Brown tdp = get_tdp(model); 1622889facbeSLen Brown 1623889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 1624889facbeSLen Brown if (verbose) 1625144b44b1SLen Brown fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 1626889facbeSLen Brown 1627889facbeSLen Brown return; 1628889facbeSLen Brown } 1629889facbeSLen Brown 1630889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1631889facbeSLen Brown { 1632889facbeSLen Brown unsigned long long msr; 1633889facbeSLen Brown unsigned int dts; 1634889facbeSLen Brown int cpu; 1635889facbeSLen Brown 1636889facbeSLen Brown if (!(do_dts || do_ptm)) 1637889facbeSLen Brown return 0; 1638889facbeSLen Brown 1639889facbeSLen Brown cpu = t->cpu_id; 1640889facbeSLen Brown 1641889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 1642889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1643889facbeSLen Brown return 0; 1644889facbeSLen Brown 1645889facbeSLen Brown if (cpu_migrate(cpu)) { 1646889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1647889facbeSLen Brown return -1; 1648889facbeSLen Brown } 1649889facbeSLen Brown 1650889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 1651889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1652889facbeSLen Brown return 0; 1653889facbeSLen Brown 1654889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1655889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 1656889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 1657889facbeSLen Brown 1658889facbeSLen Brown #ifdef THERM_DEBUG 1659889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 1660889facbeSLen Brown return 0; 1661889facbeSLen Brown 1662889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1663889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1664889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1665889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1666889facbeSLen Brown #endif 1667889facbeSLen Brown } 1668889facbeSLen Brown 1669889facbeSLen Brown 1670889facbeSLen Brown if (do_dts) { 1671889facbeSLen Brown unsigned int resolution; 1672889facbeSLen Brown 1673889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1674889facbeSLen Brown return 0; 1675889facbeSLen Brown 1676889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1677889facbeSLen Brown resolution = (msr >> 27) & 0xF; 1678889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 1679889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 1680889facbeSLen Brown 1681889facbeSLen Brown #ifdef THERM_DEBUG 1682889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 1683889facbeSLen Brown return 0; 1684889facbeSLen Brown 1685889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1686889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1687889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1688889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1689889facbeSLen Brown #endif 1690889facbeSLen Brown } 1691889facbeSLen Brown 1692889facbeSLen Brown return 0; 1693889facbeSLen Brown } 1694889facbeSLen Brown 1695889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 1696889facbeSLen Brown { 1697889facbeSLen Brown fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 1698889facbeSLen Brown cpu, label, 1699889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 1700889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 1701889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 1702889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 1703889facbeSLen Brown 1704889facbeSLen Brown return; 1705889facbeSLen Brown } 1706889facbeSLen Brown 1707889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1708889facbeSLen Brown { 1709889facbeSLen Brown unsigned long long msr; 1710889facbeSLen Brown int cpu; 1711889facbeSLen Brown 1712889facbeSLen Brown if (!do_rapl) 1713889facbeSLen Brown return 0; 1714889facbeSLen Brown 1715889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 1716889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1717889facbeSLen Brown return 0; 1718889facbeSLen Brown 1719889facbeSLen Brown cpu = t->cpu_id; 1720889facbeSLen Brown if (cpu_migrate(cpu)) { 1721889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1722889facbeSLen Brown return -1; 1723889facbeSLen Brown } 1724889facbeSLen Brown 1725889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 1726889facbeSLen Brown return -1; 1727889facbeSLen Brown 1728889facbeSLen Brown if (verbose) { 1729889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 1730889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 1731144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 1732889facbeSLen Brown } 1733144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 1734144b44b1SLen Brown 1735889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 1736889facbeSLen Brown return -5; 1737889facbeSLen Brown 1738889facbeSLen Brown 1739889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1740889facbeSLen Brown cpu, msr, 1741889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1742889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1743889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1744889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1745889facbeSLen Brown 1746144b44b1SLen Brown } 1747144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 1748144b44b1SLen Brown 1749889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 1750889facbeSLen Brown return -9; 1751889facbeSLen Brown 1752889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 1753889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 1754889facbeSLen Brown 1755889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 1756889facbeSLen Brown fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 1757889facbeSLen Brown cpu, 1758889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 1759889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 1760889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 1761889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 1762889facbeSLen Brown } 1763889facbeSLen Brown 1764889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1765889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 1766889facbeSLen Brown return -6; 1767889facbeSLen Brown 1768889facbeSLen Brown 1769889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1770889facbeSLen Brown cpu, msr, 1771889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1772889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1773889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1774889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1775889facbeSLen Brown 1776889facbeSLen Brown 1777889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 1778889facbeSLen Brown return -9; 1779889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 1780889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1781889facbeSLen Brown 1782889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 1783889facbeSLen Brown } 1784144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 1785889facbeSLen Brown if (verbose) { 1786889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 1787889facbeSLen Brown return -7; 1788889facbeSLen Brown 1789889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 1790144b44b1SLen Brown } 1791144b44b1SLen Brown } 1792144b44b1SLen Brown if (do_rapl & RAPL_CORES) { 1793144b44b1SLen Brown if (verbose) { 1794889facbeSLen Brown 1795889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 1796889facbeSLen Brown return -9; 1797889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 1798889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1799889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 1800889facbeSLen Brown } 1801889facbeSLen Brown } 1802889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1803889facbeSLen Brown if (verbose) { 1804889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 1805889facbeSLen Brown return -8; 1806889facbeSLen Brown 1807889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 1808889facbeSLen Brown 1809889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 1810889facbeSLen Brown return -9; 1811889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 1812889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1813889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 1814889facbeSLen Brown } 1815889facbeSLen Brown } 1816889facbeSLen Brown return 0; 1817889facbeSLen Brown } 1818889facbeSLen Brown 1819103a8feaSLen Brown 1820103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 1821103a8feaSLen Brown { 1822103a8feaSLen Brown if (!genuine_intel) 1823103a8feaSLen Brown return 0; 1824103a8feaSLen Brown 1825103a8feaSLen Brown switch (model) { 1826103a8feaSLen Brown case 0x2A: 1827103a8feaSLen Brown case 0x2D: 1828650a37f3SLen Brown case 0x3A: /* IVB */ 18291300651bSLen Brown case 0x3E: /* IVB Xeon */ 183070b43400SLen Brown case 0x3C: /* HSW */ 183170b43400SLen Brown case 0x3F: /* HSW */ 183270b43400SLen Brown case 0x45: /* HSW */ 1833149c2319SLen Brown case 0x46: /* HSW */ 1834103a8feaSLen Brown return 1; 1835103a8feaSLen Brown } 1836103a8feaSLen Brown return 0; 1837103a8feaSLen Brown } 1838103a8feaSLen Brown 1839ca58710fSKristen Carlson Accardi int has_c8_c9_c10(unsigned int family, unsigned int model) 1840ca58710fSKristen Carlson Accardi { 1841ca58710fSKristen Carlson Accardi if (!genuine_intel) 1842ca58710fSKristen Carlson Accardi return 0; 1843ca58710fSKristen Carlson Accardi 1844ca58710fSKristen Carlson Accardi switch (model) { 1845ca58710fSKristen Carlson Accardi case 0x45: 1846ca58710fSKristen Carlson Accardi return 1; 1847ca58710fSKristen Carlson Accardi } 1848ca58710fSKristen Carlson Accardi return 0; 1849ca58710fSKristen Carlson Accardi } 1850ca58710fSKristen Carlson Accardi 1851ca58710fSKristen Carlson Accardi 1852144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 1853144b44b1SLen Brown { 1854144b44b1SLen Brown if (!genuine_intel) 1855144b44b1SLen Brown return 0; 1856144b44b1SLen Brown switch (model) { 1857144b44b1SLen Brown case 0x37: /* BYT */ 1858144b44b1SLen Brown case 0x4D: /* AVN */ 1859144b44b1SLen Brown return 1; 1860144b44b1SLen Brown } 1861144b44b1SLen Brown return 0; 1862144b44b1SLen Brown } 1863144b44b1SLen Brown 1864144b44b1SLen Brown #define SLM_BCLK_FREQS 5 1865144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 1866144b44b1SLen Brown 1867144b44b1SLen Brown double slm_bclk(void) 1868144b44b1SLen Brown { 1869144b44b1SLen Brown unsigned long long msr = 3; 1870144b44b1SLen Brown unsigned int i; 1871144b44b1SLen Brown double freq; 1872144b44b1SLen Brown 1873144b44b1SLen Brown if (get_msr(0, MSR_FSB_FREQ, &msr)) 1874144b44b1SLen Brown fprintf(stderr, "SLM BCLK: unknown\n"); 1875144b44b1SLen Brown 1876144b44b1SLen Brown i = msr & 0xf; 1877144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 1878144b44b1SLen Brown fprintf(stderr, "SLM BCLK[%d] invalid\n", i); 1879144b44b1SLen Brown msr = 3; 1880144b44b1SLen Brown } 1881144b44b1SLen Brown freq = slm_freq_table[i]; 1882144b44b1SLen Brown 1883144b44b1SLen Brown fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); 1884144b44b1SLen Brown 1885144b44b1SLen Brown return freq; 1886144b44b1SLen Brown } 1887144b44b1SLen Brown 1888103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 1889103a8feaSLen Brown { 1890103a8feaSLen Brown if (is_snb(family, model)) 1891103a8feaSLen Brown return 100.00; 1892144b44b1SLen Brown else if (is_slm(family, model)) 1893144b44b1SLen Brown return slm_bclk(); 1894103a8feaSLen Brown else 1895103a8feaSLen Brown return 133.33; 1896103a8feaSLen Brown } 1897103a8feaSLen Brown 1898889facbeSLen Brown /* 1899889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 1900889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 1901889facbeSLen Brown * This is usually equal to tjMax. 1902889facbeSLen Brown * 1903889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 1904889facbeSLen Brown * but also allow cmdline over-ride with -T. 1905889facbeSLen Brown * 1906889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 1907889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 1908889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 1909889facbeSLen Brown */ 1910889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1911889facbeSLen Brown { 1912889facbeSLen Brown unsigned long long msr; 1913889facbeSLen Brown unsigned int target_c_local; 1914889facbeSLen Brown int cpu; 1915889facbeSLen Brown 1916889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 1917889facbeSLen Brown if (!(do_dts || do_ptm)) 1918889facbeSLen Brown return 0; 1919889facbeSLen Brown 1920889facbeSLen Brown /* this is a per-package concept */ 1921889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1922889facbeSLen Brown return 0; 1923889facbeSLen Brown 1924889facbeSLen Brown cpu = t->cpu_id; 1925889facbeSLen Brown if (cpu_migrate(cpu)) { 1926889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1927889facbeSLen Brown return -1; 1928889facbeSLen Brown } 1929889facbeSLen Brown 1930889facbeSLen Brown if (tcc_activation_temp_override != 0) { 1931889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 1932889facbeSLen Brown fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 1933889facbeSLen Brown cpu, tcc_activation_temp); 1934889facbeSLen Brown return 0; 1935889facbeSLen Brown } 1936889facbeSLen Brown 1937889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 1938889facbeSLen Brown if (!do_nehalem_platform_info) 1939889facbeSLen Brown goto guess; 1940889facbeSLen Brown 1941889facbeSLen Brown if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr)) 1942889facbeSLen Brown goto guess; 1943889facbeSLen Brown 1944889facbeSLen Brown target_c_local = (msr >> 16) & 0x7F; 1945889facbeSLen Brown 1946889facbeSLen Brown if (verbose) 1947889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 1948889facbeSLen Brown cpu, msr, target_c_local); 1949889facbeSLen Brown 1950144b44b1SLen Brown if (target_c_local < 85 || target_c_local > 127) 1951889facbeSLen Brown goto guess; 1952889facbeSLen Brown 1953889facbeSLen Brown tcc_activation_temp = target_c_local; 1954889facbeSLen Brown 1955889facbeSLen Brown return 0; 1956889facbeSLen Brown 1957889facbeSLen Brown guess: 1958889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 1959889facbeSLen Brown fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 1960889facbeSLen Brown cpu, tcc_activation_temp); 1961889facbeSLen Brown 1962889facbeSLen Brown return 0; 1963889facbeSLen Brown } 1964103a8feaSLen Brown void check_cpuid() 1965103a8feaSLen Brown { 1966103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 1967103a8feaSLen Brown unsigned int fms, family, model, stepping; 1968103a8feaSLen Brown 1969103a8feaSLen Brown eax = ebx = ecx = edx = 0; 1970103a8feaSLen Brown 19712b92865eSJosh Triplett __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 1972103a8feaSLen Brown 1973103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 1974103a8feaSLen Brown genuine_intel = 1; 1975103a8feaSLen Brown 1976103a8feaSLen Brown if (verbose) 1977889facbeSLen Brown fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 1978103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 1979103a8feaSLen Brown 19802b92865eSJosh Triplett __get_cpuid(1, &fms, &ebx, &ecx, &edx); 1981103a8feaSLen Brown family = (fms >> 8) & 0xf; 1982103a8feaSLen Brown model = (fms >> 4) & 0xf; 1983103a8feaSLen Brown stepping = fms & 0xf; 1984103a8feaSLen Brown if (family == 6 || family == 0xf) 1985103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 1986103a8feaSLen Brown 1987103a8feaSLen Brown if (verbose) 1988103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 1989103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 1990103a8feaSLen Brown 1991b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 1992b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 1993103a8feaSLen Brown 1994103a8feaSLen Brown /* 1995103a8feaSLen Brown * check max extended function levels of CPUID. 1996103a8feaSLen Brown * This is needed to check for invariant TSC. 1997103a8feaSLen Brown * This check is valid for both Intel and AMD. 1998103a8feaSLen Brown */ 1999103a8feaSLen Brown ebx = ecx = edx = 0; 20002b92865eSJosh Triplett __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); 2001103a8feaSLen Brown 2002b2c95d90SJosh Triplett if (max_level < 0x80000007) 2003b2c95d90SJosh Triplett errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level); 2004103a8feaSLen Brown 2005103a8feaSLen Brown /* 2006103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 2007103a8feaSLen Brown * this check is valid for both Intel and AMD 2008103a8feaSLen Brown */ 20092b92865eSJosh Triplett __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 20108209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 2011103a8feaSLen Brown 2012b2c95d90SJosh Triplett if (!has_invariant_tsc) 2013b2c95d90SJosh Triplett errx(1, "No invariant TSC"); 2014103a8feaSLen Brown 2015103a8feaSLen Brown /* 2016103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 2017103a8feaSLen Brown * this check is valid for both Intel and AMD 2018103a8feaSLen Brown */ 2019103a8feaSLen Brown 20202b92865eSJosh Triplett __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 20218209e054SThomas Renninger has_aperf = ecx & (1 << 0); 2022889facbeSLen Brown do_dts = eax & (1 << 0); 2023889facbeSLen Brown do_ptm = eax & (1 << 6); 2024889facbeSLen Brown has_epb = ecx & (1 << 3); 2025889facbeSLen Brown 2026889facbeSLen Brown if (verbose) 2027889facbeSLen Brown fprintf(stderr, "CPUID(6): %s%s%s%s\n", 2028889facbeSLen Brown has_aperf ? "APERF" : "No APERF!", 2029889facbeSLen Brown do_dts ? ", DTS" : "", 2030889facbeSLen Brown do_ptm ? ", PTM": "", 2031889facbeSLen Brown has_epb ? ", EPB": ""); 2032889facbeSLen Brown 2033889facbeSLen Brown if (!has_aperf) 2034b2c95d90SJosh Triplett errx(-1, "No APERF"); 2035103a8feaSLen Brown 2036103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 2037103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 20381ed51011SLen Brown do_smi = do_nhm_cstates; 2039103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 2040ca58710fSKristen Carlson Accardi do_c8_c9_c10 = has_c8_c9_c10(family, model); 2041144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 2042103a8feaSLen Brown bclk = discover_bclk(family, model); 2043103a8feaSLen Brown 2044103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 20456574a5d5SLen Brown do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 2046889facbeSLen Brown rapl_probe(family, model); 2047889facbeSLen Brown 2048889facbeSLen Brown return; 2049103a8feaSLen Brown } 2050103a8feaSLen Brown 2051103a8feaSLen Brown 2052103a8feaSLen Brown void usage() 2053103a8feaSLen Brown { 2054b2c95d90SJosh Triplett errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", 2055103a8feaSLen Brown progname); 2056103a8feaSLen Brown } 2057103a8feaSLen Brown 2058103a8feaSLen Brown 2059103a8feaSLen Brown /* 2060103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 2061103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 2062103a8feaSLen Brown */ 2063103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 2064103a8feaSLen Brown { 2065103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 2066103a8feaSLen Brown return 1; 2067103a8feaSLen Brown else 2068103a8feaSLen Brown return 0; 2069103a8feaSLen Brown } 2070103a8feaSLen Brown 2071103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 2072103a8feaSLen Brown { 2073103a8feaSLen Brown return 0; 2074103a8feaSLen Brown } 2075103a8feaSLen Brown 2076c98d5d94SLen Brown void topology_probe() 2077c98d5d94SLen Brown { 2078c98d5d94SLen Brown int i; 2079c98d5d94SLen Brown int max_core_id = 0; 2080c98d5d94SLen Brown int max_package_id = 0; 2081c98d5d94SLen Brown int max_siblings = 0; 2082c98d5d94SLen Brown struct cpu_topology { 2083c98d5d94SLen Brown int core_id; 2084c98d5d94SLen Brown int physical_package_id; 2085c98d5d94SLen Brown } *cpus; 2086c98d5d94SLen Brown 2087c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 2088c98d5d94SLen Brown topo.num_cpus = 0; 2089c98d5d94SLen Brown topo.max_cpu_num = 0; 2090c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 2091c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 2092c98d5d94SLen Brown show_cpu = 1; 2093c98d5d94SLen Brown 2094c98d5d94SLen Brown if (verbose > 1) 2095c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 2096c98d5d94SLen Brown 2097c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 2098b2c95d90SJosh Triplett if (cpus == NULL) 2099b2c95d90SJosh Triplett err(1, "calloc cpus"); 2100c98d5d94SLen Brown 2101c98d5d94SLen Brown /* 2102c98d5d94SLen Brown * Allocate and initialize cpu_present_set 2103c98d5d94SLen Brown */ 2104c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2105b2c95d90SJosh Triplett if (cpu_present_set == NULL) 2106b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2107c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2108c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 2109c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 2110c98d5d94SLen Brown 2111c98d5d94SLen Brown /* 2112c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 2113c98d5d94SLen Brown */ 2114c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2115b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 2116b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 2117c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2118c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 2119c98d5d94SLen Brown 2120c98d5d94SLen Brown 2121c98d5d94SLen Brown /* 2122c98d5d94SLen Brown * For online cpus 2123c98d5d94SLen Brown * find max_core_id, max_package_id 2124c98d5d94SLen Brown */ 2125c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 2126c98d5d94SLen Brown int siblings; 2127c98d5d94SLen Brown 2128c98d5d94SLen Brown if (cpu_is_not_present(i)) { 2129c98d5d94SLen Brown if (verbose > 1) 2130c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 2131c98d5d94SLen Brown continue; 2132c98d5d94SLen Brown } 2133c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 2134c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 2135c98d5d94SLen Brown max_core_id = cpus[i].core_id; 2136c98d5d94SLen Brown 2137c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 2138c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 2139c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 2140c98d5d94SLen Brown 2141c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 2142c98d5d94SLen Brown if (siblings > max_siblings) 2143c98d5d94SLen Brown max_siblings = siblings; 2144c98d5d94SLen Brown if (verbose > 1) 2145c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 2146c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 2147c98d5d94SLen Brown } 2148c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 2149c98d5d94SLen Brown if (verbose > 1) 2150c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 2151c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 2152c98d5d94SLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 2153c98d5d94SLen Brown show_core = 1; 2154c98d5d94SLen Brown 2155c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 2156c98d5d94SLen Brown if (verbose > 1) 2157c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 2158c98d5d94SLen Brown max_package_id, topo.num_packages); 2159c98d5d94SLen Brown if (!summary_only && topo.num_packages > 1) 2160c98d5d94SLen Brown show_pkg = 1; 2161c98d5d94SLen Brown 2162c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 2163c98d5d94SLen Brown if (verbose > 1) 2164c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 2165c98d5d94SLen Brown 2166c98d5d94SLen Brown free(cpus); 2167c98d5d94SLen Brown } 2168c98d5d94SLen Brown 2169c98d5d94SLen Brown void 2170c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 2171c98d5d94SLen Brown { 2172c98d5d94SLen Brown int i; 2173c98d5d94SLen Brown 2174c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 2175c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 2176c98d5d94SLen Brown if (*t == NULL) 2177c98d5d94SLen Brown goto error; 2178c98d5d94SLen Brown 2179c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 2180c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 2181c98d5d94SLen Brown (*t)[i].cpu_id = -1; 2182c98d5d94SLen Brown 2183c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 2184c98d5d94SLen Brown sizeof(struct core_data)); 2185c98d5d94SLen Brown if (*c == NULL) 2186c98d5d94SLen Brown goto error; 2187c98d5d94SLen Brown 2188c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 2189c98d5d94SLen Brown (*c)[i].core_id = -1; 2190c98d5d94SLen Brown 2191c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 2192c98d5d94SLen Brown if (*p == NULL) 2193c98d5d94SLen Brown goto error; 2194c98d5d94SLen Brown 2195c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 2196c98d5d94SLen Brown (*p)[i].package_id = i; 2197c98d5d94SLen Brown 2198c98d5d94SLen Brown return; 2199c98d5d94SLen Brown error: 2200b2c95d90SJosh Triplett err(1, "calloc counters"); 2201c98d5d94SLen Brown } 2202c98d5d94SLen Brown /* 2203c98d5d94SLen Brown * init_counter() 2204c98d5d94SLen Brown * 2205c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 2206c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 2207c98d5d94SLen Brown * 2208c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 2209c98d5d94SLen Brown */ 2210c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 2211c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 2212c98d5d94SLen Brown int pkg_num, int cpu_id) 2213c98d5d94SLen Brown { 2214c98d5d94SLen Brown struct thread_data *t; 2215c98d5d94SLen Brown struct core_data *c; 2216c98d5d94SLen Brown struct pkg_data *p; 2217c98d5d94SLen Brown 2218c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 2219c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 2220c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 2221c98d5d94SLen Brown 2222c98d5d94SLen Brown t->cpu_id = cpu_id; 2223c98d5d94SLen Brown if (thread_num == 0) { 2224c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 2225c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 2226c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 2227c98d5d94SLen Brown } 2228c98d5d94SLen Brown 2229c98d5d94SLen Brown c->core_id = core_num; 2230c98d5d94SLen Brown p->package_id = pkg_num; 2231c98d5d94SLen Brown } 2232c98d5d94SLen Brown 2233c98d5d94SLen Brown 2234c98d5d94SLen Brown int initialize_counters(int cpu_id) 2235c98d5d94SLen Brown { 2236c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 2237c98d5d94SLen Brown 2238c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 2239c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 2240c98d5d94SLen Brown 2241c98d5d94SLen Brown if (cpu_is_first_sibling_in_core(cpu_id)) { 2242c98d5d94SLen Brown my_thread_id = 0; 2243c98d5d94SLen Brown topo.num_cores++; 2244c98d5d94SLen Brown } else { 2245c98d5d94SLen Brown my_thread_id = 1; 2246c98d5d94SLen Brown } 2247c98d5d94SLen Brown 2248c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2249c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2250c98d5d94SLen Brown return 0; 2251c98d5d94SLen Brown } 2252c98d5d94SLen Brown 2253c98d5d94SLen Brown void allocate_output_buffer() 2254c98d5d94SLen Brown { 2255b844db31SJosh Triplett output_buffer = calloc(1, (1 + topo.num_cpus) * 256); 2256c98d5d94SLen Brown outp = output_buffer; 2257b2c95d90SJosh Triplett if (outp == NULL) 2258b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 2259c98d5d94SLen Brown } 2260c98d5d94SLen Brown 2261c98d5d94SLen Brown void setup_all_buffers(void) 2262c98d5d94SLen Brown { 2263c98d5d94SLen Brown topology_probe(); 2264c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 2265c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 2266c98d5d94SLen Brown allocate_output_buffer(); 2267c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 2268c98d5d94SLen Brown } 2269103a8feaSLen Brown void turbostat_init() 2270103a8feaSLen Brown { 2271103a8feaSLen Brown check_cpuid(); 2272103a8feaSLen Brown 2273103a8feaSLen Brown check_dev_msr(); 2274103a8feaSLen Brown check_super_user(); 2275103a8feaSLen Brown 2276c98d5d94SLen Brown setup_all_buffers(); 2277103a8feaSLen Brown 2278103a8feaSLen Brown if (verbose) 2279c98d5d94SLen Brown print_verbose_header(); 2280889facbeSLen Brown 2281889facbeSLen Brown if (verbose) 2282889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 2283889facbeSLen Brown 2284889facbeSLen Brown if (verbose) 2285889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 2286889facbeSLen Brown 2287889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 2288889facbeSLen Brown 2289889facbeSLen Brown if (verbose) 2290889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 2291103a8feaSLen Brown } 2292103a8feaSLen Brown 2293103a8feaSLen Brown int fork_it(char **argv) 2294103a8feaSLen Brown { 2295103a8feaSLen Brown pid_t child_pid; 2296d91bb17cSLen Brown int status; 2297d15cf7c1SLen Brown 2298d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 2299d91bb17cSLen Brown if (status) 2300d91bb17cSLen Brown exit(status); 2301d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 2302d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 2303103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2304103a8feaSLen Brown 2305103a8feaSLen Brown child_pid = fork(); 2306103a8feaSLen Brown if (!child_pid) { 2307103a8feaSLen Brown /* child */ 2308103a8feaSLen Brown execvp(argv[0], argv); 2309103a8feaSLen Brown } else { 2310103a8feaSLen Brown 2311103a8feaSLen Brown /* parent */ 2312b2c95d90SJosh Triplett if (child_pid == -1) 2313b2c95d90SJosh Triplett err(1, "fork"); 2314103a8feaSLen Brown 2315103a8feaSLen Brown signal(SIGINT, SIG_IGN); 2316103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 2317b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 2318b2c95d90SJosh Triplett err(status, "waitpid"); 2319103a8feaSLen Brown } 2320c98d5d94SLen Brown /* 2321c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 2322c98d5d94SLen Brown * because re-starting is problematic when forking 2323c98d5d94SLen Brown */ 2324c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 2325103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2326103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2327c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2328c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2329c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2330c98d5d94SLen Brown flush_stderr(); 2331103a8feaSLen Brown 23326eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 2333103a8feaSLen Brown 2334d91bb17cSLen Brown return status; 2335103a8feaSLen Brown } 2336103a8feaSLen Brown 2337103a8feaSLen Brown void cmdline(int argc, char **argv) 2338103a8feaSLen Brown { 2339103a8feaSLen Brown int opt; 2340103a8feaSLen Brown 2341103a8feaSLen Brown progname = argv[0]; 2342103a8feaSLen Brown 2343889facbeSLen Brown while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) { 2344103a8feaSLen Brown switch (opt) { 2345f9240813SLen Brown case 'p': 2346c98d5d94SLen Brown show_core_only++; 2347c98d5d94SLen Brown break; 2348f9240813SLen Brown case 'P': 2349c98d5d94SLen Brown show_pkg_only++; 2350c98d5d94SLen Brown break; 2351f9240813SLen Brown case 'S': 2352e23da037SLen Brown summary_only++; 2353e23da037SLen Brown break; 2354103a8feaSLen Brown case 'v': 2355103a8feaSLen Brown verbose++; 2356103a8feaSLen Brown break; 2357103a8feaSLen Brown case 'i': 2358103a8feaSLen Brown interval_sec = atoi(optarg); 2359103a8feaSLen Brown break; 2360f9240813SLen Brown case 'c': 23618e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 23628e180f3cSLen Brown break; 2363f9240813SLen Brown case 'C': 23648e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 23658e180f3cSLen Brown break; 23662f32edf1SLen Brown case 'm': 23672f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 23682f32edf1SLen Brown break; 23692f32edf1SLen Brown case 'M': 23702f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 2371103a8feaSLen Brown break; 2372889facbeSLen Brown case 'R': 2373889facbeSLen Brown rapl_verbose++; 2374889facbeSLen Brown break; 2375889facbeSLen Brown case 'T': 2376889facbeSLen Brown tcc_activation_temp_override = atoi(optarg); 2377889facbeSLen Brown break; 2378103a8feaSLen Brown default: 2379103a8feaSLen Brown usage(); 2380103a8feaSLen Brown } 2381103a8feaSLen Brown } 2382103a8feaSLen Brown } 2383103a8feaSLen Brown 2384103a8feaSLen Brown int main(int argc, char **argv) 2385103a8feaSLen Brown { 2386103a8feaSLen Brown cmdline(argc, argv); 2387103a8feaSLen Brown 2388889facbeSLen Brown if (verbose) 2389*e6f9bb3cSLen Brown fprintf(stderr, "turbostat v3.6 Dec 2, 2013" 2390103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 2391103a8feaSLen Brown 2392103a8feaSLen Brown turbostat_init(); 2393103a8feaSLen Brown 2394103a8feaSLen Brown /* 2395103a8feaSLen Brown * if any params left, it must be a command to fork 2396103a8feaSLen Brown */ 2397103a8feaSLen Brown if (argc - optind) 2398103a8feaSLen Brown return fork_it(argv + optind); 2399103a8feaSLen Brown else 2400103a8feaSLen Brown turbostat_loop(); 2401103a8feaSLen Brown 2402103a8feaSLen Brown return 0; 2403103a8feaSLen Brown } 2404