1103a8feaSLen Brown /* 2103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 3103a8feaSLen Brown * on modern Intel turbo-capable processors. 4103a8feaSLen Brown * 5e23da037SLen Brown * Copyright (c) 2012 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 239c63a650SLen Brown #include <asm/msr.h> 24103a8feaSLen Brown #include <stdio.h> 25103a8feaSLen Brown #include <unistd.h> 26103a8feaSLen Brown #include <sys/types.h> 27103a8feaSLen Brown #include <sys/wait.h> 28103a8feaSLen Brown #include <sys/stat.h> 29103a8feaSLen Brown #include <sys/resource.h> 30103a8feaSLen Brown #include <fcntl.h> 31103a8feaSLen Brown #include <signal.h> 32103a8feaSLen Brown #include <sys/time.h> 33103a8feaSLen Brown #include <stdlib.h> 34103a8feaSLen Brown #include <dirent.h> 35103a8feaSLen Brown #include <string.h> 36103a8feaSLen Brown #include <ctype.h> 3788c3281fSLen Brown #include <sched.h> 38103a8feaSLen Brown 39103a8feaSLen Brown char *proc_stat = "/proc/stat"; 40103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 41103a8feaSLen Brown unsigned int verbose; /* set with -v */ 42889facbeSLen Brown unsigned int rapl_verbose; /* set with -R */ 43889facbeSLen Brown unsigned int thermal_verbose; /* set with -T */ 44e23da037SLen Brown unsigned int summary_only; /* set with -s */ 45103a8feaSLen Brown unsigned int skip_c0; 46103a8feaSLen Brown unsigned int skip_c1; 47103a8feaSLen Brown unsigned int do_nhm_cstates; 48103a8feaSLen Brown unsigned int do_snb_cstates; 49103a8feaSLen Brown unsigned int has_aperf; 50889facbeSLen Brown unsigned int has_epb; 51103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 52103a8feaSLen Brown unsigned int genuine_intel; 53103a8feaSLen Brown unsigned int has_invariant_tsc; 54103a8feaSLen Brown unsigned int do_nehalem_platform_info; 55103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 566574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit; 572f32edf1SLen Brown unsigned int extra_msr_offset32; 582f32edf1SLen Brown unsigned int extra_msr_offset64; 598e180f3cSLen Brown unsigned int extra_delta_offset32; 608e180f3cSLen Brown unsigned int extra_delta_offset64; 61*1ed51011SLen Brown int do_smi; 62103a8feaSLen Brown double bclk; 63103a8feaSLen Brown unsigned int show_pkg; 64103a8feaSLen Brown unsigned int show_core; 65103a8feaSLen Brown unsigned int show_cpu; 66c98d5d94SLen Brown unsigned int show_pkg_only; 67c98d5d94SLen Brown unsigned int show_core_only; 68c98d5d94SLen Brown char *output_buffer, *outp; 69889facbeSLen Brown unsigned int do_rapl; 70889facbeSLen Brown unsigned int do_dts; 71889facbeSLen Brown unsigned int do_ptm; 72889facbeSLen Brown unsigned int tcc_activation_temp; 73889facbeSLen Brown unsigned int tcc_activation_temp_override; 74889facbeSLen Brown double rapl_power_units, rapl_energy_units, rapl_time_units; 75889facbeSLen Brown double rapl_joule_counter_range; 76889facbeSLen Brown 77889facbeSLen Brown #define RAPL_PKG (1 << 0) 78889facbeSLen Brown #define RAPL_CORES (1 << 1) 79889facbeSLen Brown #define RAPL_GFX (1 << 2) 80889facbeSLen Brown #define RAPL_DRAM (1 << 3) 81889facbeSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 4) 82889facbeSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 5) 83889facbeSLen Brown #define TJMAX_DEFAULT 100 84889facbeSLen Brown 85889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 86103a8feaSLen Brown 87103a8feaSLen Brown int aperf_mperf_unstable; 88103a8feaSLen Brown int backwards_count; 89103a8feaSLen Brown char *progname; 90103a8feaSLen Brown 91c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 92c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 93c98d5d94SLen Brown 94c98d5d94SLen Brown struct thread_data { 95c98d5d94SLen Brown unsigned long long tsc; 96c98d5d94SLen Brown unsigned long long aperf; 97c98d5d94SLen Brown unsigned long long mperf; 98c98d5d94SLen Brown unsigned long long c1; /* derived */ 992f32edf1SLen Brown unsigned long long extra_msr64; 1008e180f3cSLen Brown unsigned long long extra_delta64; 1018e180f3cSLen Brown unsigned long long extra_msr32; 1028e180f3cSLen Brown unsigned long long extra_delta32; 103*1ed51011SLen Brown unsigned int smi_count; 104c98d5d94SLen Brown unsigned int cpu_id; 105c98d5d94SLen Brown unsigned int flags; 106c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 107c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 108c98d5d94SLen Brown } *thread_even, *thread_odd; 109c98d5d94SLen Brown 110c98d5d94SLen Brown struct core_data { 111c98d5d94SLen Brown unsigned long long c3; 112c98d5d94SLen Brown unsigned long long c6; 113c98d5d94SLen Brown unsigned long long c7; 114889facbeSLen Brown unsigned int core_temp_c; 115c98d5d94SLen Brown unsigned int core_id; 116c98d5d94SLen Brown } *core_even, *core_odd; 117c98d5d94SLen Brown 118c98d5d94SLen Brown struct pkg_data { 119c98d5d94SLen Brown unsigned long long pc2; 120c98d5d94SLen Brown unsigned long long pc3; 121c98d5d94SLen Brown unsigned long long pc6; 122c98d5d94SLen Brown unsigned long long pc7; 123c98d5d94SLen Brown unsigned int package_id; 124889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 125889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 126889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 127889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 128889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 129889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 130889facbeSLen Brown unsigned int pkg_temp_c; 131889facbeSLen Brown 132c98d5d94SLen Brown } *package_even, *package_odd; 133c98d5d94SLen Brown 134c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 135c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 136c98d5d94SLen Brown 137c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 138c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 139c98d5d94SLen Brown topo.num_threads_per_core + \ 140c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 141c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 142c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 143c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 144c98d5d94SLen Brown 145c98d5d94SLen Brown struct system_summary { 146c98d5d94SLen Brown struct thread_data threads; 147c98d5d94SLen Brown struct core_data cores; 148c98d5d94SLen Brown struct pkg_data packages; 149c98d5d94SLen Brown } sum, average; 150c98d5d94SLen Brown 151c98d5d94SLen Brown 152c98d5d94SLen Brown struct topo_params { 153c98d5d94SLen Brown int num_packages; 154103a8feaSLen Brown int num_cpus; 155c98d5d94SLen Brown int num_cores; 156c98d5d94SLen Brown int max_cpu_num; 157c98d5d94SLen Brown int num_cores_per_pkg; 158c98d5d94SLen Brown int num_threads_per_core; 159c98d5d94SLen Brown } topo; 160103a8feaSLen Brown 161c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 162103a8feaSLen Brown 163c98d5d94SLen Brown void setup_all_buffers(void); 164103a8feaSLen Brown 165c98d5d94SLen Brown int cpu_is_not_present(int cpu) 166d15cf7c1SLen Brown { 167c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 168c98d5d94SLen Brown } 169c98d5d94SLen Brown /* 170c98d5d94SLen Brown * run func(thread, core, package) in topology order 171c98d5d94SLen Brown * skip non-present cpus 172c98d5d94SLen Brown */ 173c98d5d94SLen Brown 174c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 175c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 176c98d5d94SLen Brown { 177c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 178c98d5d94SLen Brown 179c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 180c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 181c98d5d94SLen Brown for (thread_no = 0; thread_no < 182c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 183c98d5d94SLen Brown struct thread_data *t; 184c98d5d94SLen Brown struct core_data *c; 185c98d5d94SLen Brown struct pkg_data *p; 186c98d5d94SLen Brown 187c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 188c98d5d94SLen Brown 189c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 190c98d5d94SLen Brown continue; 191c98d5d94SLen Brown 192c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 193c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 194c98d5d94SLen Brown 195c98d5d94SLen Brown retval = func(t, c, p); 196c98d5d94SLen Brown if (retval) 197c98d5d94SLen Brown return retval; 198c98d5d94SLen Brown } 199c98d5d94SLen Brown } 200c98d5d94SLen Brown } 201d15cf7c1SLen Brown return 0; 202d15cf7c1SLen Brown } 203d15cf7c1SLen Brown 20488c3281fSLen Brown int cpu_migrate(int cpu) 20588c3281fSLen Brown { 206c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 207c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 208c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 20988c3281fSLen Brown return -1; 21088c3281fSLen Brown else 21188c3281fSLen Brown return 0; 21288c3281fSLen Brown } 21388c3281fSLen Brown 21415aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 215103a8feaSLen Brown { 216103a8feaSLen Brown ssize_t retval; 217103a8feaSLen Brown char pathname[32]; 218103a8feaSLen Brown int fd; 219103a8feaSLen Brown 220103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 221103a8feaSLen Brown fd = open(pathname, O_RDONLY); 22215aaa346SLen Brown if (fd < 0) 22315aaa346SLen Brown return -1; 224103a8feaSLen Brown 22515aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 226103a8feaSLen Brown close(fd); 22715aaa346SLen Brown 228d91bb17cSLen Brown if (retval != sizeof *msr) { 229d91bb17cSLen Brown fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset); 23015aaa346SLen Brown return -1; 231d91bb17cSLen Brown } 23215aaa346SLen Brown 23315aaa346SLen Brown return 0; 234103a8feaSLen Brown } 235103a8feaSLen Brown 236a829eb4dSLen Brown void print_header(void) 237103a8feaSLen Brown { 238103a8feaSLen Brown if (show_pkg) 239c98d5d94SLen Brown outp += sprintf(outp, "pk"); 240e23da037SLen Brown if (show_pkg) 241c98d5d94SLen Brown outp += sprintf(outp, " "); 242103a8feaSLen Brown if (show_core) 243c98d5d94SLen Brown outp += sprintf(outp, "cor"); 244103a8feaSLen Brown if (show_cpu) 245c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 246e23da037SLen Brown if (show_pkg || show_core || show_cpu) 247c98d5d94SLen Brown outp += sprintf(outp, " "); 248103a8feaSLen Brown if (do_nhm_cstates) 249c98d5d94SLen Brown outp += sprintf(outp, " %%c0"); 250103a8feaSLen Brown if (has_aperf) 251c98d5d94SLen Brown outp += sprintf(outp, " GHz"); 252c98d5d94SLen Brown outp += sprintf(outp, " TSC"); 253*1ed51011SLen Brown if (do_smi) 254*1ed51011SLen Brown outp += sprintf(outp, " SMI"); 2558e180f3cSLen Brown if (extra_delta_offset32) 256f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 2578e180f3cSLen Brown if (extra_delta_offset64) 258f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 2592f32edf1SLen Brown if (extra_msr_offset32) 2608e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 2612f32edf1SLen Brown if (extra_msr_offset64) 2628e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 263103a8feaSLen Brown if (do_nhm_cstates) 264c98d5d94SLen Brown outp += sprintf(outp, " %%c1"); 265103a8feaSLen Brown if (do_nhm_cstates) 266c98d5d94SLen Brown outp += sprintf(outp, " %%c3"); 267103a8feaSLen Brown if (do_nhm_cstates) 268c98d5d94SLen Brown outp += sprintf(outp, " %%c6"); 269103a8feaSLen Brown if (do_snb_cstates) 270c98d5d94SLen Brown outp += sprintf(outp, " %%c7"); 271889facbeSLen Brown 272889facbeSLen Brown if (do_dts) 273889facbeSLen Brown outp += sprintf(outp, " CTMP"); 274889facbeSLen Brown if (do_ptm) 275889facbeSLen Brown outp += sprintf(outp, " PTMP"); 276889facbeSLen Brown 277103a8feaSLen Brown if (do_snb_cstates) 278c98d5d94SLen Brown outp += sprintf(outp, " %%pc2"); 279103a8feaSLen Brown if (do_nhm_cstates) 280c98d5d94SLen Brown outp += sprintf(outp, " %%pc3"); 281103a8feaSLen Brown if (do_nhm_cstates) 282c98d5d94SLen Brown outp += sprintf(outp, " %%pc6"); 283103a8feaSLen Brown if (do_snb_cstates) 284c98d5d94SLen Brown outp += sprintf(outp, " %%pc7"); 285103a8feaSLen Brown 286889facbeSLen Brown if (do_rapl & RAPL_PKG) 287889facbeSLen Brown outp += sprintf(outp, " Pkg_W"); 288889facbeSLen Brown if (do_rapl & RAPL_CORES) 289889facbeSLen Brown outp += sprintf(outp, " Cor_W"); 290889facbeSLen Brown if (do_rapl & RAPL_GFX) 291889facbeSLen Brown outp += sprintf(outp, " GFX_W"); 292889facbeSLen Brown if (do_rapl & RAPL_DRAM) 293889facbeSLen Brown outp += sprintf(outp, " RAM_W"); 294889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) 295889facbeSLen Brown outp += sprintf(outp, " PKG_%%"); 296889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) 297889facbeSLen Brown outp += sprintf(outp, " RAM_%%"); 298889facbeSLen Brown 299c98d5d94SLen Brown outp += sprintf(outp, "\n"); 300103a8feaSLen Brown } 301103a8feaSLen Brown 302c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 303c98d5d94SLen Brown struct pkg_data *p) 304103a8feaSLen Brown { 305c98d5d94SLen Brown fprintf(stderr, "t %p, c %p, p %p\n", t, c, p); 306c98d5d94SLen Brown 307c98d5d94SLen Brown if (t) { 308c98d5d94SLen Brown fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 309c98d5d94SLen Brown fprintf(stderr, "TSC: %016llX\n", t->tsc); 310c98d5d94SLen Brown fprintf(stderr, "aperf: %016llX\n", t->aperf); 311c98d5d94SLen Brown fprintf(stderr, "mperf: %016llX\n", t->mperf); 312c98d5d94SLen Brown fprintf(stderr, "c1: %016llX\n", t->c1); 3138e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 3148e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 3158e180f3cSLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 3168e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 3178e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 3182f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 319c98d5d94SLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 3202f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 321*1ed51011SLen Brown if (do_smi) 322*1ed51011SLen Brown fprintf(stderr, "SMI: %08X\n", t->smi_count); 323103a8feaSLen Brown } 324103a8feaSLen Brown 325c98d5d94SLen Brown if (c) { 326c98d5d94SLen Brown fprintf(stderr, "core: %d\n", c->core_id); 327c98d5d94SLen Brown fprintf(stderr, "c3: %016llX\n", c->c3); 328c98d5d94SLen Brown fprintf(stderr, "c6: %016llX\n", c->c6); 329c98d5d94SLen Brown fprintf(stderr, "c7: %016llX\n", c->c7); 330889facbeSLen Brown fprintf(stderr, "DTS: %dC\n", c->core_temp_c); 331c98d5d94SLen Brown } 332103a8feaSLen Brown 333c98d5d94SLen Brown if (p) { 334c98d5d94SLen Brown fprintf(stderr, "package: %d\n", p->package_id); 335c98d5d94SLen Brown fprintf(stderr, "pc2: %016llX\n", p->pc2); 336c98d5d94SLen Brown fprintf(stderr, "pc3: %016llX\n", p->pc3); 337c98d5d94SLen Brown fprintf(stderr, "pc6: %016llX\n", p->pc6); 338c98d5d94SLen Brown fprintf(stderr, "pc7: %016llX\n", p->pc7); 339889facbeSLen Brown fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg); 340889facbeSLen Brown fprintf(stderr, "Joules COR: %0X\n", p->energy_cores); 341889facbeSLen Brown fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx); 342889facbeSLen Brown fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram); 343889facbeSLen Brown fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status); 344889facbeSLen Brown fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status); 345889facbeSLen Brown fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c); 346c98d5d94SLen Brown } 347c98d5d94SLen Brown return 0; 348103a8feaSLen Brown } 349103a8feaSLen Brown 350e23da037SLen Brown /* 351e23da037SLen Brown * column formatting convention & formats 352e23da037SLen Brown * package: "pk" 2 columns %2d 353e23da037SLen Brown * core: "cor" 3 columns %3d 354e23da037SLen Brown * CPU: "CPU" 3 columns %3d 355889facbeSLen Brown * Pkg_W: %6.2 356889facbeSLen Brown * Cor_W: %6.2 357889facbeSLen Brown * GFX_W: %5.2 358889facbeSLen Brown * RAM_W: %5.2 359e23da037SLen Brown * GHz: "GHz" 3 columns %3.2 360e23da037SLen Brown * TSC: "TSC" 3 columns %3.2 361*1ed51011SLen Brown * SMI: "SMI" 4 columns %4d 362e23da037SLen Brown * percentage " %pc3" %6.2 363889facbeSLen Brown * Perf Status percentage: %5.2 364889facbeSLen Brown * "CTMP" 4 columns %4d 365e23da037SLen Brown */ 366c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 367c98d5d94SLen Brown struct pkg_data *p) 368103a8feaSLen Brown { 369103a8feaSLen Brown double interval_float; 370889facbeSLen Brown char *fmt5, *fmt6; 371103a8feaSLen Brown 372c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 373c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 374c98d5d94SLen Brown return 0; 375c98d5d94SLen Brown 376c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 377c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 378c98d5d94SLen Brown return 0; 379c98d5d94SLen Brown 380103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 381103a8feaSLen Brown 382c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 383c98d5d94SLen Brown if (t == &average.threads) { 384103a8feaSLen Brown if (show_pkg) 385c98d5d94SLen Brown outp += sprintf(outp, " "); 386e23da037SLen Brown if (show_pkg && show_core) 387c98d5d94SLen Brown outp += sprintf(outp, " "); 388103a8feaSLen Brown if (show_core) 389c98d5d94SLen Brown outp += sprintf(outp, " "); 390103a8feaSLen Brown if (show_cpu) 391c98d5d94SLen Brown outp += sprintf(outp, " " " "); 392103a8feaSLen Brown } else { 393c98d5d94SLen Brown if (show_pkg) { 394c98d5d94SLen Brown if (p) 395c98d5d94SLen Brown outp += sprintf(outp, "%2d", p->package_id); 396c98d5d94SLen Brown else 397c98d5d94SLen Brown outp += sprintf(outp, " "); 398c98d5d94SLen Brown } 399e23da037SLen Brown if (show_pkg && show_core) 400c98d5d94SLen Brown outp += sprintf(outp, " "); 401c98d5d94SLen Brown if (show_core) { 402c98d5d94SLen Brown if (c) 403c98d5d94SLen Brown outp += sprintf(outp, "%3d", c->core_id); 404c98d5d94SLen Brown else 405c98d5d94SLen Brown outp += sprintf(outp, " "); 406c98d5d94SLen Brown } 407103a8feaSLen Brown if (show_cpu) 408c98d5d94SLen Brown outp += sprintf(outp, " %3d", t->cpu_id); 409103a8feaSLen Brown } 410103a8feaSLen Brown /* %c0 */ 411103a8feaSLen Brown if (do_nhm_cstates) { 412e23da037SLen Brown if (show_pkg || show_core || show_cpu) 413c98d5d94SLen Brown outp += sprintf(outp, " "); 414103a8feaSLen Brown if (!skip_c0) 415c98d5d94SLen Brown outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); 416103a8feaSLen Brown else 417c98d5d94SLen Brown outp += sprintf(outp, " ****"); 418103a8feaSLen Brown } 419103a8feaSLen Brown 420103a8feaSLen Brown /* GHz */ 421103a8feaSLen Brown if (has_aperf) { 422103a8feaSLen Brown if (!aperf_mperf_unstable) { 423c98d5d94SLen Brown outp += sprintf(outp, " %3.2f", 424c98d5d94SLen Brown 1.0 * t->tsc / units * t->aperf / 425c98d5d94SLen Brown t->mperf / interval_float); 426103a8feaSLen Brown } else { 427c98d5d94SLen Brown if (t->aperf > t->tsc || t->mperf > t->tsc) { 428c98d5d94SLen Brown outp += sprintf(outp, " ***"); 429103a8feaSLen Brown } else { 430c98d5d94SLen Brown outp += sprintf(outp, "%3.1f*", 431c98d5d94SLen Brown 1.0 * t->tsc / 432c98d5d94SLen Brown units * t->aperf / 433c98d5d94SLen Brown t->mperf / interval_float); 434103a8feaSLen Brown } 435103a8feaSLen Brown } 436103a8feaSLen Brown } 437103a8feaSLen Brown 438103a8feaSLen Brown /* TSC */ 439c98d5d94SLen Brown outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); 440103a8feaSLen Brown 441*1ed51011SLen Brown /* SMI */ 442*1ed51011SLen Brown if (do_smi) 443*1ed51011SLen Brown outp += sprintf(outp, "%4d", t->smi_count); 444*1ed51011SLen Brown 4458e180f3cSLen Brown /* delta */ 4468e180f3cSLen Brown if (extra_delta_offset32) 4478e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 4488e180f3cSLen Brown 4498e180f3cSLen Brown /* DELTA */ 4508e180f3cSLen Brown if (extra_delta_offset64) 4518e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 4522f32edf1SLen Brown /* msr */ 4532f32edf1SLen Brown if (extra_msr_offset32) 4548e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 4552f32edf1SLen Brown 456130ff304SLen Brown /* MSR */ 4572f32edf1SLen Brown if (extra_msr_offset64) 4582f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 459130ff304SLen Brown 460103a8feaSLen Brown if (do_nhm_cstates) { 461103a8feaSLen Brown if (!skip_c1) 462c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); 463103a8feaSLen Brown else 464c98d5d94SLen Brown outp += sprintf(outp, " ****"); 465103a8feaSLen Brown } 466c98d5d94SLen Brown 467c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 468c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 469c98d5d94SLen Brown goto done; 470c98d5d94SLen Brown 471103a8feaSLen Brown if (do_nhm_cstates) 472c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); 473103a8feaSLen Brown if (do_nhm_cstates) 474c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); 475103a8feaSLen Brown if (do_snb_cstates) 476c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); 477c98d5d94SLen Brown 478889facbeSLen Brown if (do_dts) 479889facbeSLen Brown outp += sprintf(outp, " %4d", c->core_temp_c); 480889facbeSLen Brown 481c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 482c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 483c98d5d94SLen Brown goto done; 484c98d5d94SLen Brown 485889facbeSLen Brown if (do_ptm) 486889facbeSLen Brown outp += sprintf(outp, " %4d", p->pkg_temp_c); 487889facbeSLen Brown 488103a8feaSLen Brown if (do_snb_cstates) 489c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); 490103a8feaSLen Brown if (do_nhm_cstates) 491c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); 492103a8feaSLen Brown if (do_nhm_cstates) 493c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); 494103a8feaSLen Brown if (do_snb_cstates) 495c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); 496889facbeSLen Brown 497889facbeSLen Brown /* 498889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 499889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 500889facbeSLen Brown */ 501889facbeSLen Brown if (interval_float < rapl_joule_counter_range) { 502889facbeSLen Brown fmt5 = " %5.2f"; 503889facbeSLen Brown fmt6 = " %6.2f"; 504889facbeSLen Brown } else { 505889facbeSLen Brown fmt5 = " %3.0f**"; 506889facbeSLen Brown fmt6 = " %4.0f**"; 507889facbeSLen Brown } 508889facbeSLen Brown 509889facbeSLen Brown if (do_rapl & RAPL_PKG) 510889facbeSLen Brown outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float); 511889facbeSLen Brown if (do_rapl & RAPL_CORES) 512889facbeSLen Brown outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float); 513889facbeSLen Brown if (do_rapl & RAPL_GFX) 514889facbeSLen Brown outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float); 515889facbeSLen Brown if (do_rapl & RAPL_DRAM) 516889facbeSLen Brown outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float); 517889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS ) 518889facbeSLen Brown outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 519889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS ) 520889facbeSLen Brown outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 521889facbeSLen Brown 522c98d5d94SLen Brown done: 523c98d5d94SLen Brown outp += sprintf(outp, "\n"); 524c98d5d94SLen Brown 525c98d5d94SLen Brown return 0; 526103a8feaSLen Brown } 527103a8feaSLen Brown 528c98d5d94SLen Brown void flush_stdout() 529103a8feaSLen Brown { 530c98d5d94SLen Brown fputs(output_buffer, stdout); 531ddac0d68SLen Brown fflush(stdout); 532c98d5d94SLen Brown outp = output_buffer; 533c98d5d94SLen Brown } 534c98d5d94SLen Brown void flush_stderr() 535c98d5d94SLen Brown { 536c98d5d94SLen Brown fputs(output_buffer, stderr); 537c98d5d94SLen Brown outp = output_buffer; 538c98d5d94SLen Brown } 539c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 540c98d5d94SLen Brown { 541e23da037SLen Brown static int printed; 542103a8feaSLen Brown 543e23da037SLen Brown if (!printed || !summary_only) 544103a8feaSLen Brown print_header(); 545103a8feaSLen Brown 546c98d5d94SLen Brown if (topo.num_cpus > 1) 547c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 548c98d5d94SLen Brown &average.packages); 549103a8feaSLen Brown 550e23da037SLen Brown printed = 1; 551e23da037SLen Brown 552e23da037SLen Brown if (summary_only) 553e23da037SLen Brown return; 554e23da037SLen Brown 555c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 556103a8feaSLen Brown } 557103a8feaSLen Brown 558889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 559889facbeSLen Brown if (new > old) { \ 560889facbeSLen Brown old = new - old; \ 561889facbeSLen Brown } else { \ 562889facbeSLen Brown old = 0x100000000 + new - old; \ 563889facbeSLen Brown } 564889facbeSLen Brown 565c98d5d94SLen Brown void 566c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 567103a8feaSLen Brown { 568c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 569c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 570c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 571c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 572889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 573889facbeSLen Brown 574889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 575889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 576889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 577889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 578889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 579889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 580103a8feaSLen Brown } 581103a8feaSLen Brown 582c98d5d94SLen Brown void 583c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 584c98d5d94SLen Brown { 585c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 586c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 587c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 588889facbeSLen Brown old->core_temp_c = new->core_temp_c; 589103a8feaSLen Brown } 590c98d5d94SLen Brown 591c3ae331dSLen Brown /* 592c3ae331dSLen Brown * old = new - old 593c3ae331dSLen Brown */ 594c98d5d94SLen Brown void 595c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 596c98d5d94SLen Brown struct core_data *core_delta) 597c98d5d94SLen Brown { 598c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 599c98d5d94SLen Brown 600103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 601c98d5d94SLen Brown if (old->tsc < (1000 * 1000)) { 602c98d5d94SLen Brown fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n"); 603c98d5d94SLen Brown fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n"); 604c98d5d94SLen Brown fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n"); 605103a8feaSLen Brown exit(-3); 606103a8feaSLen Brown } 607103a8feaSLen Brown 608c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 609c98d5d94SLen Brown 610c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 611c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 612c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 613c98d5d94SLen Brown } else { 614c98d5d94SLen Brown 615103a8feaSLen Brown if (!aperf_mperf_unstable) { 616103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 617103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 618103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 619103a8feaSLen Brown 620103a8feaSLen Brown aperf_mperf_unstable = 1; 621103a8feaSLen Brown } 622103a8feaSLen Brown /* 623103a8feaSLen Brown * mperf delta is likely a huge "positive" number 624103a8feaSLen Brown * can not use it for calculating c0 time 625103a8feaSLen Brown */ 626103a8feaSLen Brown skip_c0 = 1; 627103a8feaSLen Brown skip_c1 = 1; 628103a8feaSLen Brown } 629103a8feaSLen Brown 630c98d5d94SLen Brown 631103a8feaSLen Brown /* 632c3ae331dSLen Brown * As counter collection is not atomic, 633c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 634103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 635103a8feaSLen Brown */ 636c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 637c98d5d94SLen Brown old->c1 = 0; 638c98d5d94SLen Brown else { 639c98d5d94SLen Brown /* normal case, derive c1 */ 640c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 641c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 642c98d5d94SLen Brown } 643c3ae331dSLen Brown 644c98d5d94SLen Brown if (old->mperf == 0) { 645c3ae331dSLen Brown if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 646c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 647c98d5d94SLen Brown } 648103a8feaSLen Brown 6498e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 6508e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 6518e180f3cSLen Brown 6528e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 6538e180f3cSLen Brown 654103a8feaSLen Brown /* 6558e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 656103a8feaSLen Brown */ 6572f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 6582f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 659*1ed51011SLen Brown 660*1ed51011SLen Brown if (do_smi) 661*1ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 662103a8feaSLen Brown } 663c98d5d94SLen Brown 664c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 665c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 666c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 667c98d5d94SLen Brown { 668c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 669c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 670c98d5d94SLen Brown delta_core(c, c2); 671c98d5d94SLen Brown 672c98d5d94SLen Brown /* always calculate thread delta */ 673c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 674c98d5d94SLen Brown 675c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 676c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 677c98d5d94SLen Brown delta_package(p, p2); 678c98d5d94SLen Brown 679103a8feaSLen Brown return 0; 680103a8feaSLen Brown } 681103a8feaSLen Brown 682c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 683103a8feaSLen Brown { 684c98d5d94SLen Brown t->tsc = 0; 685c98d5d94SLen Brown t->aperf = 0; 686c98d5d94SLen Brown t->mperf = 0; 687c98d5d94SLen Brown t->c1 = 0; 688103a8feaSLen Brown 689*1ed51011SLen Brown t->smi_count = 0; 6908e180f3cSLen Brown t->extra_delta32 = 0; 6918e180f3cSLen Brown t->extra_delta64 = 0; 6928e180f3cSLen Brown 693c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 694c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 695c98d5d94SLen Brown 696c98d5d94SLen Brown c->c3 = 0; 697c98d5d94SLen Brown c->c6 = 0; 698c98d5d94SLen Brown c->c7 = 0; 699889facbeSLen Brown c->core_temp_c = 0; 700c98d5d94SLen Brown 701c98d5d94SLen Brown p->pc2 = 0; 702c98d5d94SLen Brown p->pc3 = 0; 703c98d5d94SLen Brown p->pc6 = 0; 704c98d5d94SLen Brown p->pc7 = 0; 705889facbeSLen Brown 706889facbeSLen Brown p->energy_pkg = 0; 707889facbeSLen Brown p->energy_dram = 0; 708889facbeSLen Brown p->energy_cores = 0; 709889facbeSLen Brown p->energy_gfx = 0; 710889facbeSLen Brown p->rapl_pkg_perf_status = 0; 711889facbeSLen Brown p->rapl_dram_perf_status = 0; 712889facbeSLen Brown p->pkg_temp_c = 0; 713103a8feaSLen Brown } 714c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 715c98d5d94SLen Brown struct pkg_data *p) 716103a8feaSLen Brown { 717c98d5d94SLen Brown average.threads.tsc += t->tsc; 718c98d5d94SLen Brown average.threads.aperf += t->aperf; 719c98d5d94SLen Brown average.threads.mperf += t->mperf; 720c98d5d94SLen Brown average.threads.c1 += t->c1; 72115aaa346SLen Brown 7228e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 7238e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 7248e180f3cSLen Brown 725c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 726c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 727c98d5d94SLen Brown return 0; 728c98d5d94SLen Brown 729c98d5d94SLen Brown average.cores.c3 += c->c3; 730c98d5d94SLen Brown average.cores.c6 += c->c6; 731c98d5d94SLen Brown average.cores.c7 += c->c7; 732c98d5d94SLen Brown 733889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 734889facbeSLen Brown 735c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 736c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 737c98d5d94SLen Brown return 0; 738c98d5d94SLen Brown 739c98d5d94SLen Brown average.packages.pc2 += p->pc2; 740c98d5d94SLen Brown average.packages.pc3 += p->pc3; 741c98d5d94SLen Brown average.packages.pc6 += p->pc6; 742c98d5d94SLen Brown average.packages.pc7 += p->pc7; 743c98d5d94SLen Brown 744889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 745889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 746889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 747889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 748889facbeSLen Brown 749889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 750889facbeSLen Brown 751889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 752889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 753c98d5d94SLen Brown return 0; 754c98d5d94SLen Brown } 755c98d5d94SLen Brown /* 756c98d5d94SLen Brown * sum the counters for all cpus in the system 757c98d5d94SLen Brown * compute the weighted average 758c98d5d94SLen Brown */ 759c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 760c98d5d94SLen Brown struct pkg_data *p) 761c98d5d94SLen Brown { 762c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 763c98d5d94SLen Brown 764c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 765c98d5d94SLen Brown 766c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 767c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 768c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 769c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 770c98d5d94SLen Brown 7718e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 7728e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 7738e180f3cSLen Brown 7748e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 7758e180f3cSLen Brown 776c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 777c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 778c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 779c98d5d94SLen Brown 780c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 781c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 782c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 783c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 784c98d5d94SLen Brown } 785c98d5d94SLen Brown 786c98d5d94SLen Brown static unsigned long long rdtsc(void) 787c98d5d94SLen Brown { 788c98d5d94SLen Brown unsigned int low, high; 789c98d5d94SLen Brown 790c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 791c98d5d94SLen Brown 792c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 793c98d5d94SLen Brown } 794c98d5d94SLen Brown 795c98d5d94SLen Brown 796c98d5d94SLen Brown /* 797c98d5d94SLen Brown * get_counters(...) 798c98d5d94SLen Brown * migrate to cpu 799c98d5d94SLen Brown * acquire and record local counters for that cpu 800c98d5d94SLen Brown */ 801c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 802c98d5d94SLen Brown { 803c98d5d94SLen Brown int cpu = t->cpu_id; 804889facbeSLen Brown unsigned long long msr; 805c98d5d94SLen Brown 806e52966c0SLen Brown if (cpu_migrate(cpu)) { 807e52966c0SLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 80815aaa346SLen Brown return -1; 809e52966c0SLen Brown } 81015aaa346SLen Brown 811c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 81215aaa346SLen Brown 81315aaa346SLen Brown if (has_aperf) { 8149c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 815c98d5d94SLen Brown return -3; 8169c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 817c98d5d94SLen Brown return -4; 81888c3281fSLen Brown } 81988c3281fSLen Brown 820*1ed51011SLen Brown if (do_smi) { 821*1ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 822*1ed51011SLen Brown return -5; 823*1ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 824*1ed51011SLen Brown } 8258e180f3cSLen Brown if (extra_delta_offset32) { 826889facbeSLen Brown if (get_msr(cpu, extra_delta_offset32, &msr)) 8272f32edf1SLen Brown return -5; 828889facbeSLen Brown t->extra_delta32 = msr & 0xFFFFFFFF; 8298e180f3cSLen Brown } 8308e180f3cSLen Brown 8318e180f3cSLen Brown if (extra_delta_offset64) 8328e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 8338e180f3cSLen Brown return -5; 8348e180f3cSLen Brown 8358e180f3cSLen Brown if (extra_msr_offset32) { 836889facbeSLen Brown if (get_msr(cpu, extra_msr_offset32, &msr)) 8378e180f3cSLen Brown return -5; 838889facbeSLen Brown t->extra_msr32 = msr & 0xFFFFFFFF; 8398e180f3cSLen Brown } 8402f32edf1SLen Brown 8412f32edf1SLen Brown if (extra_msr_offset64) 8422f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 843c98d5d94SLen Brown return -5; 844c98d5d94SLen Brown 845c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 846c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 847c98d5d94SLen Brown return 0; 848c98d5d94SLen Brown 84915aaa346SLen Brown if (do_nhm_cstates) { 850c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 851c98d5d94SLen Brown return -6; 852c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 853c98d5d94SLen Brown return -7; 854103a8feaSLen Brown } 85515aaa346SLen Brown 85615aaa346SLen Brown if (do_snb_cstates) 857c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 858c98d5d94SLen Brown return -8; 859c98d5d94SLen Brown 860889facbeSLen Brown if (do_dts) { 861889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 862889facbeSLen Brown return -9; 863889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 864889facbeSLen Brown } 865889facbeSLen Brown 866889facbeSLen Brown 867c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 868c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 869c98d5d94SLen Brown return 0; 87015aaa346SLen Brown 87115aaa346SLen Brown if (do_nhm_cstates) { 872c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 873c98d5d94SLen Brown return -9; 874c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 875c98d5d94SLen Brown return -10; 87615aaa346SLen Brown } 87715aaa346SLen Brown if (do_snb_cstates) { 878c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 879c98d5d94SLen Brown return -11; 880c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 881c98d5d94SLen Brown return -12; 88215aaa346SLen Brown } 883889facbeSLen Brown if (do_rapl & RAPL_PKG) { 884889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 885889facbeSLen Brown return -13; 886889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 887889facbeSLen Brown } 888889facbeSLen Brown if (do_rapl & RAPL_CORES) { 889889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 890889facbeSLen Brown return -14; 891889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 892889facbeSLen Brown } 893889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 894889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 895889facbeSLen Brown return -15; 896889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 897889facbeSLen Brown } 898889facbeSLen Brown if (do_rapl & RAPL_GFX) { 899889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 900889facbeSLen Brown return -16; 901889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 902889facbeSLen Brown } 903889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 904889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 905889facbeSLen Brown return -16; 906889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 907889facbeSLen Brown } 908889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 909889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 910889facbeSLen Brown return -16; 911889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 912889facbeSLen Brown } 913889facbeSLen Brown if (do_ptm) { 914889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 915889facbeSLen Brown return -17; 916889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 917889facbeSLen Brown } 91815aaa346SLen Brown return 0; 919103a8feaSLen Brown } 920103a8feaSLen Brown 921c98d5d94SLen Brown void print_verbose_header(void) 922103a8feaSLen Brown { 923103a8feaSLen Brown unsigned long long msr; 924103a8feaSLen Brown unsigned int ratio; 925103a8feaSLen Brown 926103a8feaSLen Brown if (!do_nehalem_platform_info) 927103a8feaSLen Brown return; 928103a8feaSLen Brown 9299c63a650SLen Brown get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); 930103a8feaSLen Brown 931889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr); 9326574a5d5SLen Brown 933103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 934103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 935103a8feaSLen Brown ratio, bclk, ratio * bclk); 936103a8feaSLen Brown 937103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 938103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 939103a8feaSLen Brown ratio, bclk, ratio * bclk); 940103a8feaSLen Brown 94167920418SLen Brown get_msr(0, MSR_IA32_POWER_CTL, &msr); 94267920418SLen Brown fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n", 94367920418SLen Brown msr, msr & 0x2 ? "EN" : "DIS"); 94467920418SLen Brown 9456574a5d5SLen Brown if (!do_ivt_turbo_ratio_limit) 9466574a5d5SLen Brown goto print_nhm_turbo_ratio_limits; 9476574a5d5SLen Brown 9486574a5d5SLen Brown get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 9496574a5d5SLen Brown 950889facbeSLen Brown fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 9516574a5d5SLen Brown 9526574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 9536574a5d5SLen Brown if (ratio) 9546574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 9556574a5d5SLen Brown ratio, bclk, ratio * bclk); 9566574a5d5SLen Brown 9576574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 9586574a5d5SLen Brown if (ratio) 9596574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 9606574a5d5SLen Brown ratio, bclk, ratio * bclk); 9616574a5d5SLen Brown 9626574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 9636574a5d5SLen Brown if (ratio) 9646574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 9656574a5d5SLen Brown ratio, bclk, ratio * bclk); 9666574a5d5SLen Brown 9676574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 9686574a5d5SLen Brown if (ratio) 9696574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 9706574a5d5SLen Brown ratio, bclk, ratio * bclk); 9716574a5d5SLen Brown 9726574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 9736574a5d5SLen Brown if (ratio) 9746574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 9756574a5d5SLen Brown ratio, bclk, ratio * bclk); 9766574a5d5SLen Brown 9776574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 9786574a5d5SLen Brown if (ratio) 9796574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 9806574a5d5SLen Brown ratio, bclk, ratio * bclk); 9816574a5d5SLen Brown 9826574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 9836574a5d5SLen Brown if (ratio) 9846574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 9856574a5d5SLen Brown ratio, bclk, ratio * bclk); 9866574a5d5SLen Brown 9876574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 9886574a5d5SLen Brown if (ratio) 9896574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 9906574a5d5SLen Brown ratio, bclk, ratio * bclk); 9916574a5d5SLen Brown 9926574a5d5SLen Brown print_nhm_turbo_ratio_limits: 993889facbeSLen Brown get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 994889facbeSLen Brown 995889facbeSLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 996889facbeSLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 997889facbeSLen Brown 998889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr); 999889facbeSLen Brown 1000889facbeSLen Brown fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ", 1001889facbeSLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1002889facbeSLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1003889facbeSLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1004889facbeSLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1005889facbeSLen Brown (msr & (1 << 15)) ? "" : "UN", 1006889facbeSLen Brown (unsigned int)msr & 7); 1007889facbeSLen Brown 1008889facbeSLen Brown 1009889facbeSLen Brown switch(msr & 0x7) { 1010889facbeSLen Brown case 0: 1011889facbeSLen Brown fprintf(stderr, "pc0"); 1012889facbeSLen Brown break; 1013889facbeSLen Brown case 1: 1014889facbeSLen Brown fprintf(stderr, do_snb_cstates ? "pc2" : "pc0"); 1015889facbeSLen Brown break; 1016889facbeSLen Brown case 2: 1017889facbeSLen Brown fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3"); 1018889facbeSLen Brown break; 1019889facbeSLen Brown case 3: 1020889facbeSLen Brown fprintf(stderr, "pc6"); 1021889facbeSLen Brown break; 1022889facbeSLen Brown case 4: 1023889facbeSLen Brown fprintf(stderr, "pc7"); 1024889facbeSLen Brown break; 1025889facbeSLen Brown case 5: 1026889facbeSLen Brown fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid"); 1027889facbeSLen Brown break; 1028889facbeSLen Brown case 7: 1029889facbeSLen Brown fprintf(stderr, "unlimited"); 1030889facbeSLen Brown break; 1031889facbeSLen Brown default: 1032889facbeSLen Brown fprintf(stderr, "invalid"); 1033889facbeSLen Brown } 1034889facbeSLen Brown fprintf(stderr, ")\n"); 1035103a8feaSLen Brown 1036103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 1037103a8feaSLen Brown return; 1038103a8feaSLen Brown 10399c63a650SLen Brown get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1040103a8feaSLen Brown 1041889facbeSLen Brown fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 10426574a5d5SLen Brown 10436574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 10446574a5d5SLen Brown if (ratio) 10456574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 10466574a5d5SLen Brown ratio, bclk, ratio * bclk); 10476574a5d5SLen Brown 10486574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 10496574a5d5SLen Brown if (ratio) 10506574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 10516574a5d5SLen Brown ratio, bclk, ratio * bclk); 10526574a5d5SLen Brown 10536574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 10546574a5d5SLen Brown if (ratio) 10556574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 10566574a5d5SLen Brown ratio, bclk, ratio * bclk); 10576574a5d5SLen Brown 10586574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 10596574a5d5SLen Brown if (ratio) 10606574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 10616574a5d5SLen Brown ratio, bclk, ratio * bclk); 10626574a5d5SLen Brown 1063103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 1064103a8feaSLen Brown if (ratio) 1065103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1066103a8feaSLen Brown ratio, bclk, ratio * bclk); 1067103a8feaSLen Brown 1068103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 1069103a8feaSLen Brown if (ratio) 1070103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1071103a8feaSLen Brown ratio, bclk, ratio * bclk); 1072103a8feaSLen Brown 1073103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1074103a8feaSLen Brown if (ratio) 1075103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1076103a8feaSLen Brown ratio, bclk, ratio * bclk); 1077103a8feaSLen Brown 1078103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 1079103a8feaSLen Brown if (ratio) 1080103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1081103a8feaSLen Brown ratio, bclk, ratio * bclk); 1082103a8feaSLen Brown } 1083103a8feaSLen Brown 1084c98d5d94SLen Brown void free_all_buffers(void) 1085103a8feaSLen Brown { 1086c98d5d94SLen Brown CPU_FREE(cpu_present_set); 1087c98d5d94SLen Brown cpu_present_set = NULL; 1088c98d5d94SLen Brown cpu_present_set = 0; 1089103a8feaSLen Brown 1090c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 1091c98d5d94SLen Brown cpu_affinity_set = NULL; 1092c98d5d94SLen Brown cpu_affinity_setsize = 0; 1093103a8feaSLen Brown 1094c98d5d94SLen Brown free(thread_even); 1095c98d5d94SLen Brown free(core_even); 1096c98d5d94SLen Brown free(package_even); 1097103a8feaSLen Brown 1098c98d5d94SLen Brown thread_even = NULL; 1099c98d5d94SLen Brown core_even = NULL; 1100c98d5d94SLen Brown package_even = NULL; 1101103a8feaSLen Brown 1102c98d5d94SLen Brown free(thread_odd); 1103c98d5d94SLen Brown free(core_odd); 1104c98d5d94SLen Brown free(package_odd); 1105103a8feaSLen Brown 1106c98d5d94SLen Brown thread_odd = NULL; 1107c98d5d94SLen Brown core_odd = NULL; 1108c98d5d94SLen Brown package_odd = NULL; 1109103a8feaSLen Brown 1110c98d5d94SLen Brown free(output_buffer); 1111c98d5d94SLen Brown output_buffer = NULL; 1112c98d5d94SLen Brown outp = NULL; 1113103a8feaSLen Brown } 1114103a8feaSLen Brown 1115103a8feaSLen Brown /* 1116c98d5d94SLen Brown * cpu_is_first_sibling_in_core(cpu) 1117c98d5d94SLen Brown * return 1 if given CPU is 1st HT sibling in the core 1118103a8feaSLen Brown */ 1119c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu) 1120103a8feaSLen Brown { 1121c98d5d94SLen Brown char path[64]; 1122c98d5d94SLen Brown FILE *filep; 1123c98d5d94SLen Brown int first_cpu; 1124103a8feaSLen Brown 1125c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1126c98d5d94SLen Brown filep = fopen(path, "r"); 1127c98d5d94SLen Brown if (filep == NULL) { 1128c98d5d94SLen Brown perror(path); 1129103a8feaSLen Brown exit(1); 1130103a8feaSLen Brown } 1131c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 1132c98d5d94SLen Brown fclose(filep); 1133c98d5d94SLen Brown return (cpu == first_cpu); 1134c98d5d94SLen Brown } 1135103a8feaSLen Brown 1136c98d5d94SLen Brown /* 1137c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 1138c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 1139c98d5d94SLen Brown */ 1140c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 1141c98d5d94SLen Brown { 1142c98d5d94SLen Brown char path[64]; 1143c98d5d94SLen Brown FILE *filep; 1144c98d5d94SLen Brown int first_cpu; 1145c98d5d94SLen Brown 1146c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1147c98d5d94SLen Brown filep = fopen(path, "r"); 1148c98d5d94SLen Brown if (filep == NULL) { 1149c98d5d94SLen Brown perror(path); 1150103a8feaSLen Brown exit(1); 1151103a8feaSLen Brown } 1152c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 1153c98d5d94SLen Brown fclose(filep); 1154c98d5d94SLen Brown return (cpu == first_cpu); 1155103a8feaSLen Brown } 1156103a8feaSLen Brown 1157103a8feaSLen Brown int get_physical_package_id(int cpu) 1158103a8feaSLen Brown { 1159c98d5d94SLen Brown char path[80]; 1160103a8feaSLen Brown FILE *filep; 1161103a8feaSLen Brown int pkg; 1162103a8feaSLen Brown 1163103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1164103a8feaSLen Brown filep = fopen(path, "r"); 1165103a8feaSLen Brown if (filep == NULL) { 1166103a8feaSLen Brown perror(path); 1167103a8feaSLen Brown exit(1); 1168103a8feaSLen Brown } 1169103a8feaSLen Brown fscanf(filep, "%d", &pkg); 1170103a8feaSLen Brown fclose(filep); 1171103a8feaSLen Brown return pkg; 1172103a8feaSLen Brown } 1173103a8feaSLen Brown 1174103a8feaSLen Brown int get_core_id(int cpu) 1175103a8feaSLen Brown { 1176c98d5d94SLen Brown char path[80]; 1177103a8feaSLen Brown FILE *filep; 1178103a8feaSLen Brown int core; 1179103a8feaSLen Brown 1180103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1181103a8feaSLen Brown filep = fopen(path, "r"); 1182103a8feaSLen Brown if (filep == NULL) { 1183103a8feaSLen Brown perror(path); 1184103a8feaSLen Brown exit(1); 1185103a8feaSLen Brown } 1186103a8feaSLen Brown fscanf(filep, "%d", &core); 1187103a8feaSLen Brown fclose(filep); 1188103a8feaSLen Brown return core; 1189103a8feaSLen Brown } 1190103a8feaSLen Brown 1191c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 1192c98d5d94SLen Brown { 1193c98d5d94SLen Brown char path[80]; 1194c98d5d94SLen Brown FILE *filep; 1195c98d5d94SLen Brown int sib1, sib2; 1196c98d5d94SLen Brown int matches; 1197c98d5d94SLen Brown char character; 1198c98d5d94SLen Brown 1199c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1200c98d5d94SLen Brown filep = fopen(path, "r"); 1201c98d5d94SLen Brown if (filep == NULL) { 1202c98d5d94SLen Brown perror(path); 1203c98d5d94SLen Brown exit(1); 1204c98d5d94SLen Brown } 1205103a8feaSLen Brown /* 1206c98d5d94SLen Brown * file format: 1207c98d5d94SLen Brown * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 1208c98d5d94SLen Brown * otherwinse 1 sibling (self). 1209c98d5d94SLen Brown */ 1210c98d5d94SLen Brown matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 1211c98d5d94SLen Brown 1212c98d5d94SLen Brown fclose(filep); 1213c98d5d94SLen Brown 1214c98d5d94SLen Brown if (matches == 3) 1215c98d5d94SLen Brown return 2; 1216c98d5d94SLen Brown else 1217c98d5d94SLen Brown return 1; 1218c98d5d94SLen Brown } 1219c98d5d94SLen Brown 1220c98d5d94SLen Brown /* 1221c98d5d94SLen Brown * run func(thread, core, package) in topology order 1222c98d5d94SLen Brown * skip non-present cpus 1223103a8feaSLen Brown */ 1224103a8feaSLen Brown 1225c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1226c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 1227c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 1228c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 1229c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 1230c98d5d94SLen Brown struct pkg_data *pkg_base2) 1231c98d5d94SLen Brown { 1232c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 1233c98d5d94SLen Brown 1234c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1235c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1236c98d5d94SLen Brown for (thread_no = 0; thread_no < 1237c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 1238c98d5d94SLen Brown struct thread_data *t, *t2; 1239c98d5d94SLen Brown struct core_data *c, *c2; 1240c98d5d94SLen Brown struct pkg_data *p, *p2; 1241c98d5d94SLen Brown 1242c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1243c98d5d94SLen Brown 1244c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1245c98d5d94SLen Brown continue; 1246c98d5d94SLen Brown 1247c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1248c98d5d94SLen Brown 1249c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1250c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1251c98d5d94SLen Brown 1252c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1253c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1254c98d5d94SLen Brown 1255c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1256c98d5d94SLen Brown if (retval) 1257c98d5d94SLen Brown return retval; 1258c98d5d94SLen Brown } 1259c98d5d94SLen Brown } 1260c98d5d94SLen Brown } 1261c98d5d94SLen Brown return 0; 1262c98d5d94SLen Brown } 1263c98d5d94SLen Brown 1264c98d5d94SLen Brown /* 1265c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1266c98d5d94SLen Brown * return max_cpu number 1267c98d5d94SLen Brown */ 1268c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1269103a8feaSLen Brown { 1270103a8feaSLen Brown FILE *fp; 1271c98d5d94SLen Brown int cpu_num; 1272103a8feaSLen Brown int retval; 1273103a8feaSLen Brown 1274103a8feaSLen Brown fp = fopen(proc_stat, "r"); 1275103a8feaSLen Brown if (fp == NULL) { 1276103a8feaSLen Brown perror(proc_stat); 1277103a8feaSLen Brown exit(1); 1278103a8feaSLen Brown } 1279103a8feaSLen Brown 1280103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1281103a8feaSLen Brown if (retval != 0) { 1282103a8feaSLen Brown perror("/proc/stat format"); 1283103a8feaSLen Brown exit(1); 1284103a8feaSLen Brown } 1285103a8feaSLen Brown 1286c98d5d94SLen Brown while (1) { 1287c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1288103a8feaSLen Brown if (retval != 1) 1289103a8feaSLen Brown break; 1290103a8feaSLen Brown 1291c98d5d94SLen Brown retval = func(cpu_num); 1292c98d5d94SLen Brown if (retval) { 1293c98d5d94SLen Brown fclose(fp); 1294c98d5d94SLen Brown return(retval); 1295c98d5d94SLen Brown } 1296103a8feaSLen Brown } 1297103a8feaSLen Brown fclose(fp); 1298c98d5d94SLen Brown return 0; 1299103a8feaSLen Brown } 1300103a8feaSLen Brown 1301103a8feaSLen Brown void re_initialize(void) 1302103a8feaSLen Brown { 1303c98d5d94SLen Brown free_all_buffers(); 1304c98d5d94SLen Brown setup_all_buffers(); 1305c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1306103a8feaSLen Brown } 1307103a8feaSLen Brown 1308c98d5d94SLen Brown 1309103a8feaSLen Brown /* 1310c98d5d94SLen Brown * count_cpus() 1311c98d5d94SLen Brown * remember the last one seen, it will be the max 1312103a8feaSLen Brown */ 1313c98d5d94SLen Brown int count_cpus(int cpu) 1314103a8feaSLen Brown { 1315c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1316c98d5d94SLen Brown topo.max_cpu_num = cpu; 1317103a8feaSLen Brown 1318c98d5d94SLen Brown topo.num_cpus += 1; 1319c98d5d94SLen Brown return 0; 1320103a8feaSLen Brown } 1321c98d5d94SLen Brown int mark_cpu_present(int cpu) 1322c98d5d94SLen Brown { 1323c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 132415aaa346SLen Brown return 0; 1325103a8feaSLen Brown } 1326103a8feaSLen Brown 1327103a8feaSLen Brown void turbostat_loop() 1328103a8feaSLen Brown { 1329c98d5d94SLen Brown int retval; 1330e52966c0SLen Brown int restarted = 0; 1331c98d5d94SLen Brown 1332103a8feaSLen Brown restart: 1333e52966c0SLen Brown restarted++; 1334e52966c0SLen Brown 1335c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1336d91bb17cSLen Brown if (retval < -1) { 1337d91bb17cSLen Brown exit(retval); 1338d91bb17cSLen Brown } else if (retval == -1) { 1339e52966c0SLen Brown if (restarted > 1) { 1340e52966c0SLen Brown exit(retval); 1341e52966c0SLen Brown } 1342c98d5d94SLen Brown re_initialize(); 1343c98d5d94SLen Brown goto restart; 1344c98d5d94SLen Brown } 1345e52966c0SLen Brown restarted = 0; 1346103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1347103a8feaSLen Brown 1348103a8feaSLen Brown while (1) { 1349c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1350103a8feaSLen Brown re_initialize(); 1351103a8feaSLen Brown goto restart; 1352103a8feaSLen Brown } 1353103a8feaSLen Brown sleep(interval_sec); 1354c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1355d91bb17cSLen Brown if (retval < -1) { 1356d91bb17cSLen Brown exit(retval); 1357d91bb17cSLen Brown } else if (retval == -1) { 135815aaa346SLen Brown re_initialize(); 135915aaa346SLen Brown goto restart; 136015aaa346SLen Brown } 1361103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1362103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1363c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1364c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1365c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1366c98d5d94SLen Brown flush_stdout(); 136715aaa346SLen Brown sleep(interval_sec); 1368c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1369d91bb17cSLen Brown if (retval < -1) { 1370d91bb17cSLen Brown exit(retval); 1371d91bb17cSLen Brown } else if (retval == -1) { 1372103a8feaSLen Brown re_initialize(); 1373103a8feaSLen Brown goto restart; 1374103a8feaSLen Brown } 1375103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1376103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1377c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1378c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1379c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1380c98d5d94SLen Brown flush_stdout(); 1381103a8feaSLen Brown } 1382103a8feaSLen Brown } 1383103a8feaSLen Brown 1384103a8feaSLen Brown void check_dev_msr() 1385103a8feaSLen Brown { 1386103a8feaSLen Brown struct stat sb; 1387103a8feaSLen Brown 1388103a8feaSLen Brown if (stat("/dev/cpu/0/msr", &sb)) { 1389103a8feaSLen Brown fprintf(stderr, "no /dev/cpu/0/msr\n"); 1390103a8feaSLen Brown fprintf(stderr, "Try \"# modprobe msr\"\n"); 1391103a8feaSLen Brown exit(-5); 1392103a8feaSLen Brown } 1393103a8feaSLen Brown } 1394103a8feaSLen Brown 1395103a8feaSLen Brown void check_super_user() 1396103a8feaSLen Brown { 1397103a8feaSLen Brown if (getuid() != 0) { 1398103a8feaSLen Brown fprintf(stderr, "must be root\n"); 1399103a8feaSLen Brown exit(-6); 1400103a8feaSLen Brown } 1401103a8feaSLen Brown } 1402103a8feaSLen Brown 1403103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1404103a8feaSLen Brown { 1405103a8feaSLen Brown if (!genuine_intel) 1406103a8feaSLen Brown return 0; 1407103a8feaSLen Brown 1408103a8feaSLen Brown if (family != 6) 1409103a8feaSLen Brown return 0; 1410103a8feaSLen Brown 1411103a8feaSLen Brown switch (model) { 1412103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1413103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1414103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1415103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1416103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1417103a8feaSLen Brown case 0x2A: /* SNB */ 1418103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1419553575f1SLen Brown case 0x3A: /* IVB */ 14201300651bSLen Brown case 0x3E: /* IVB Xeon */ 142170b43400SLen Brown case 0x3C: /* HSW */ 142270b43400SLen Brown case 0x3F: /* HSW */ 142370b43400SLen Brown case 0x45: /* HSW */ 1424103a8feaSLen Brown return 1; 1425103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1426103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1427103a8feaSLen Brown default: 1428103a8feaSLen Brown return 0; 1429103a8feaSLen Brown } 1430103a8feaSLen Brown } 14316574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 14326574a5d5SLen Brown { 14336574a5d5SLen Brown if (!genuine_intel) 14346574a5d5SLen Brown return 0; 14356574a5d5SLen Brown 14366574a5d5SLen Brown if (family != 6) 14376574a5d5SLen Brown return 0; 14386574a5d5SLen Brown 14396574a5d5SLen Brown switch (model) { 14406574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 14416574a5d5SLen Brown return 1; 14426574a5d5SLen Brown default: 14436574a5d5SLen Brown return 0; 14446574a5d5SLen Brown } 14456574a5d5SLen Brown } 14466574a5d5SLen Brown 1447889facbeSLen Brown /* 1448889facbeSLen Brown * print_epb() 1449889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 1450889facbeSLen Brown */ 1451889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1452889facbeSLen Brown { 1453889facbeSLen Brown unsigned long long msr; 1454889facbeSLen Brown char *epb_string; 1455889facbeSLen Brown int cpu; 1456889facbeSLen Brown 1457889facbeSLen Brown if (!has_epb) 1458889facbeSLen Brown return 0; 1459889facbeSLen Brown 1460889facbeSLen Brown cpu = t->cpu_id; 1461889facbeSLen Brown 1462889facbeSLen Brown /* EPB is per-package */ 1463889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1464889facbeSLen Brown return 0; 1465889facbeSLen Brown 1466889facbeSLen Brown if (cpu_migrate(cpu)) { 1467889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1468889facbeSLen Brown return -1; 1469889facbeSLen Brown } 1470889facbeSLen Brown 1471889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 1472889facbeSLen Brown return 0; 1473889facbeSLen Brown 1474889facbeSLen Brown switch (msr & 0x7) { 1475889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 1476889facbeSLen Brown epb_string = "performance"; 1477889facbeSLen Brown break; 1478889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 1479889facbeSLen Brown epb_string = "balanced"; 1480889facbeSLen Brown break; 1481889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 1482889facbeSLen Brown epb_string = "powersave"; 1483889facbeSLen Brown break; 1484889facbeSLen Brown default: 1485889facbeSLen Brown epb_string = "custom"; 1486889facbeSLen Brown break; 1487889facbeSLen Brown } 1488889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 1489889facbeSLen Brown 1490889facbeSLen Brown return 0; 1491889facbeSLen Brown } 1492889facbeSLen Brown 1493889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 1494889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 1495889facbeSLen Brown 1496889facbeSLen Brown /* 1497889facbeSLen Brown * rapl_probe() 1498889facbeSLen Brown * 1499889facbeSLen Brown * sets do_rapl 1500889facbeSLen Brown */ 1501889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 1502889facbeSLen Brown { 1503889facbeSLen Brown unsigned long long msr; 1504889facbeSLen Brown double tdp; 1505889facbeSLen Brown 1506889facbeSLen Brown if (!genuine_intel) 1507889facbeSLen Brown return; 1508889facbeSLen Brown 1509889facbeSLen Brown if (family != 6) 1510889facbeSLen Brown return; 1511889facbeSLen Brown 1512889facbeSLen Brown switch (model) { 1513889facbeSLen Brown case 0x2A: 1514889facbeSLen Brown case 0x3A: 151570b43400SLen Brown case 0x3C: /* HSW */ 151670b43400SLen Brown case 0x3F: /* HSW */ 151770b43400SLen Brown case 0x45: /* HSW */ 1518889facbeSLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX; 1519889facbeSLen Brown break; 1520889facbeSLen Brown case 0x2D: 1521889facbeSLen Brown case 0x3E: 1522889facbeSLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS; 1523889facbeSLen Brown break; 1524889facbeSLen Brown default: 1525889facbeSLen Brown return; 1526889facbeSLen Brown } 1527889facbeSLen Brown 1528889facbeSLen Brown /* units on package 0, verify later other packages match */ 1529889facbeSLen Brown if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr)) 1530889facbeSLen Brown return; 1531889facbeSLen Brown 1532889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 1533889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 1534889facbeSLen Brown rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF)); 1535889facbeSLen Brown 1536889facbeSLen Brown /* get TDP to determine energy counter range */ 1537889facbeSLen Brown if (get_msr(0, MSR_PKG_POWER_INFO, &msr)) 1538889facbeSLen Brown return; 1539889facbeSLen Brown 1540889facbeSLen Brown tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 1541889facbeSLen Brown 1542889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 1543889facbeSLen Brown 1544889facbeSLen Brown if (verbose) 1545889facbeSLen Brown fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range); 1546889facbeSLen Brown 1547889facbeSLen Brown return; 1548889facbeSLen Brown } 1549889facbeSLen Brown 1550889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1551889facbeSLen Brown { 1552889facbeSLen Brown unsigned long long msr; 1553889facbeSLen Brown unsigned int dts; 1554889facbeSLen Brown int cpu; 1555889facbeSLen Brown 1556889facbeSLen Brown if (!(do_dts || do_ptm)) 1557889facbeSLen Brown return 0; 1558889facbeSLen Brown 1559889facbeSLen Brown cpu = t->cpu_id; 1560889facbeSLen Brown 1561889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 1562889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1563889facbeSLen Brown return 0; 1564889facbeSLen Brown 1565889facbeSLen Brown if (cpu_migrate(cpu)) { 1566889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1567889facbeSLen Brown return -1; 1568889facbeSLen Brown } 1569889facbeSLen Brown 1570889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 1571889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1572889facbeSLen Brown return 0; 1573889facbeSLen Brown 1574889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1575889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 1576889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 1577889facbeSLen Brown 1578889facbeSLen Brown #ifdef THERM_DEBUG 1579889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 1580889facbeSLen Brown return 0; 1581889facbeSLen Brown 1582889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1583889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1584889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1585889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1586889facbeSLen Brown #endif 1587889facbeSLen Brown } 1588889facbeSLen Brown 1589889facbeSLen Brown 1590889facbeSLen Brown if (do_dts) { 1591889facbeSLen Brown unsigned int resolution; 1592889facbeSLen Brown 1593889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1594889facbeSLen Brown return 0; 1595889facbeSLen Brown 1596889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1597889facbeSLen Brown resolution = (msr >> 27) & 0xF; 1598889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 1599889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 1600889facbeSLen Brown 1601889facbeSLen Brown #ifdef THERM_DEBUG 1602889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 1603889facbeSLen Brown return 0; 1604889facbeSLen Brown 1605889facbeSLen Brown dts = (msr >> 16) & 0x7F; 1606889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 1607889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1608889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1609889facbeSLen Brown #endif 1610889facbeSLen Brown } 1611889facbeSLen Brown 1612889facbeSLen Brown return 0; 1613889facbeSLen Brown } 1614889facbeSLen Brown 1615889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 1616889facbeSLen Brown { 1617889facbeSLen Brown fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 1618889facbeSLen Brown cpu, label, 1619889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 1620889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 1621889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 1622889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 1623889facbeSLen Brown 1624889facbeSLen Brown return; 1625889facbeSLen Brown } 1626889facbeSLen Brown 1627889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1628889facbeSLen Brown { 1629889facbeSLen Brown unsigned long long msr; 1630889facbeSLen Brown int cpu; 1631889facbeSLen Brown double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units; 1632889facbeSLen Brown 1633889facbeSLen Brown if (!do_rapl) 1634889facbeSLen Brown return 0; 1635889facbeSLen Brown 1636889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 1637889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1638889facbeSLen Brown return 0; 1639889facbeSLen Brown 1640889facbeSLen Brown cpu = t->cpu_id; 1641889facbeSLen Brown if (cpu_migrate(cpu)) { 1642889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1643889facbeSLen Brown return -1; 1644889facbeSLen Brown } 1645889facbeSLen Brown 1646889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 1647889facbeSLen Brown return -1; 1648889facbeSLen Brown 1649889facbeSLen Brown local_rapl_power_units = 1.0 / (1 << (msr & 0xF)); 1650889facbeSLen Brown local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 1651889facbeSLen Brown local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF)); 1652889facbeSLen Brown 1653889facbeSLen Brown if (local_rapl_power_units != rapl_power_units) 1654889facbeSLen Brown fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu); 1655889facbeSLen Brown if (local_rapl_energy_units != rapl_energy_units) 1656889facbeSLen Brown fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu); 1657889facbeSLen Brown if (local_rapl_time_units != rapl_time_units) 1658889facbeSLen Brown fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu); 1659889facbeSLen Brown 1660889facbeSLen Brown if (verbose) { 1661889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 1662889facbeSLen Brown "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 1663889facbeSLen Brown local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units); 1664889facbeSLen Brown } 1665889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1666889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 1667889facbeSLen Brown return -5; 1668889facbeSLen Brown 1669889facbeSLen Brown 1670889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1671889facbeSLen Brown cpu, msr, 1672889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1673889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1674889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1675889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1676889facbeSLen Brown 1677889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 1678889facbeSLen Brown return -9; 1679889facbeSLen Brown 1680889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 1681889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "": "UN"); 1682889facbeSLen Brown 1683889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 1684889facbeSLen Brown fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 1685889facbeSLen Brown cpu, 1686889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 1687889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 1688889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 1689889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 1690889facbeSLen Brown } 1691889facbeSLen Brown 1692889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1693889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 1694889facbeSLen Brown return -6; 1695889facbeSLen Brown 1696889facbeSLen Brown 1697889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1698889facbeSLen Brown cpu, msr, 1699889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1700889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1701889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1702889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1703889facbeSLen Brown 1704889facbeSLen Brown 1705889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 1706889facbeSLen Brown return -9; 1707889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 1708889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1709889facbeSLen Brown 1710889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 1711889facbeSLen Brown } 1712889facbeSLen Brown if (do_rapl & RAPL_CORES) { 1713889facbeSLen Brown if (verbose) { 1714889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 1715889facbeSLen Brown return -7; 1716889facbeSLen Brown 1717889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 1718889facbeSLen Brown 1719889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 1720889facbeSLen Brown return -9; 1721889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 1722889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1723889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 1724889facbeSLen Brown } 1725889facbeSLen Brown } 1726889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1727889facbeSLen Brown if (verbose) { 1728889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 1729889facbeSLen Brown return -8; 1730889facbeSLen Brown 1731889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 1732889facbeSLen Brown 1733889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 1734889facbeSLen Brown return -9; 1735889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 1736889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1737889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 1738889facbeSLen Brown } 1739889facbeSLen Brown } 1740889facbeSLen Brown return 0; 1741889facbeSLen Brown } 1742889facbeSLen Brown 1743103a8feaSLen Brown 1744103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 1745103a8feaSLen Brown { 1746103a8feaSLen Brown if (!genuine_intel) 1747103a8feaSLen Brown return 0; 1748103a8feaSLen Brown 1749103a8feaSLen Brown switch (model) { 1750103a8feaSLen Brown case 0x2A: 1751103a8feaSLen Brown case 0x2D: 1752650a37f3SLen Brown case 0x3A: /* IVB */ 17531300651bSLen Brown case 0x3E: /* IVB Xeon */ 175470b43400SLen Brown case 0x3C: /* HSW */ 175570b43400SLen Brown case 0x3F: /* HSW */ 175670b43400SLen Brown case 0x45: /* HSW */ 1757103a8feaSLen Brown return 1; 1758103a8feaSLen Brown } 1759103a8feaSLen Brown return 0; 1760103a8feaSLen Brown } 1761103a8feaSLen Brown 1762103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 1763103a8feaSLen Brown { 1764103a8feaSLen Brown if (is_snb(family, model)) 1765103a8feaSLen Brown return 100.00; 1766103a8feaSLen Brown else 1767103a8feaSLen Brown return 133.33; 1768103a8feaSLen Brown } 1769103a8feaSLen Brown 1770889facbeSLen Brown /* 1771889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 1772889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 1773889facbeSLen Brown * This is usually equal to tjMax. 1774889facbeSLen Brown * 1775889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 1776889facbeSLen Brown * but also allow cmdline over-ride with -T. 1777889facbeSLen Brown * 1778889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 1779889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 1780889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 1781889facbeSLen Brown */ 1782889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1783889facbeSLen Brown { 1784889facbeSLen Brown unsigned long long msr; 1785889facbeSLen Brown unsigned int target_c_local; 1786889facbeSLen Brown int cpu; 1787889facbeSLen Brown 1788889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 1789889facbeSLen Brown if (!(do_dts || do_ptm)) 1790889facbeSLen Brown return 0; 1791889facbeSLen Brown 1792889facbeSLen Brown /* this is a per-package concept */ 1793889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1794889facbeSLen Brown return 0; 1795889facbeSLen Brown 1796889facbeSLen Brown cpu = t->cpu_id; 1797889facbeSLen Brown if (cpu_migrate(cpu)) { 1798889facbeSLen Brown fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1799889facbeSLen Brown return -1; 1800889facbeSLen Brown } 1801889facbeSLen Brown 1802889facbeSLen Brown if (tcc_activation_temp_override != 0) { 1803889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 1804889facbeSLen Brown fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 1805889facbeSLen Brown cpu, tcc_activation_temp); 1806889facbeSLen Brown return 0; 1807889facbeSLen Brown } 1808889facbeSLen Brown 1809889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 1810889facbeSLen Brown if (!do_nehalem_platform_info) 1811889facbeSLen Brown goto guess; 1812889facbeSLen Brown 1813889facbeSLen Brown if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr)) 1814889facbeSLen Brown goto guess; 1815889facbeSLen Brown 1816889facbeSLen Brown target_c_local = (msr >> 16) & 0x7F; 1817889facbeSLen Brown 1818889facbeSLen Brown if (verbose) 1819889facbeSLen Brown fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 1820889facbeSLen Brown cpu, msr, target_c_local); 1821889facbeSLen Brown 1822889facbeSLen Brown if (target_c_local < 85 || target_c_local > 120) 1823889facbeSLen Brown goto guess; 1824889facbeSLen Brown 1825889facbeSLen Brown tcc_activation_temp = target_c_local; 1826889facbeSLen Brown 1827889facbeSLen Brown return 0; 1828889facbeSLen Brown 1829889facbeSLen Brown guess: 1830889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 1831889facbeSLen Brown fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 1832889facbeSLen Brown cpu, tcc_activation_temp); 1833889facbeSLen Brown 1834889facbeSLen Brown return 0; 1835889facbeSLen Brown } 1836103a8feaSLen Brown void check_cpuid() 1837103a8feaSLen Brown { 1838103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 1839103a8feaSLen Brown unsigned int fms, family, model, stepping; 1840103a8feaSLen Brown 1841103a8feaSLen Brown eax = ebx = ecx = edx = 0; 1842103a8feaSLen Brown 1843103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); 1844103a8feaSLen Brown 1845103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 1846103a8feaSLen Brown genuine_intel = 1; 1847103a8feaSLen Brown 1848103a8feaSLen Brown if (verbose) 1849889facbeSLen Brown fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 1850103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 1851103a8feaSLen Brown 1852103a8feaSLen Brown asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); 1853103a8feaSLen Brown family = (fms >> 8) & 0xf; 1854103a8feaSLen Brown model = (fms >> 4) & 0xf; 1855103a8feaSLen Brown stepping = fms & 0xf; 1856103a8feaSLen Brown if (family == 6 || family == 0xf) 1857103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 1858103a8feaSLen Brown 1859103a8feaSLen Brown if (verbose) 1860103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 1861103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 1862103a8feaSLen Brown 1863103a8feaSLen Brown if (!(edx & (1 << 5))) { 1864103a8feaSLen Brown fprintf(stderr, "CPUID: no MSR\n"); 1865103a8feaSLen Brown exit(1); 1866103a8feaSLen Brown } 1867103a8feaSLen Brown 1868103a8feaSLen Brown /* 1869103a8feaSLen Brown * check max extended function levels of CPUID. 1870103a8feaSLen Brown * This is needed to check for invariant TSC. 1871103a8feaSLen Brown * This check is valid for both Intel and AMD. 1872103a8feaSLen Brown */ 1873103a8feaSLen Brown ebx = ecx = edx = 0; 1874103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); 1875103a8feaSLen Brown 1876103a8feaSLen Brown if (max_level < 0x80000007) { 1877103a8feaSLen Brown fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); 1878103a8feaSLen Brown exit(1); 1879103a8feaSLen Brown } 1880103a8feaSLen Brown 1881103a8feaSLen Brown /* 1882103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 1883103a8feaSLen Brown * this check is valid for both Intel and AMD 1884103a8feaSLen Brown */ 1885103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 18868209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 1887103a8feaSLen Brown 1888103a8feaSLen Brown if (!has_invariant_tsc) { 1889103a8feaSLen Brown fprintf(stderr, "No invariant TSC\n"); 1890103a8feaSLen Brown exit(1); 1891103a8feaSLen Brown } 1892103a8feaSLen Brown 1893103a8feaSLen Brown /* 1894103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 1895103a8feaSLen Brown * this check is valid for both Intel and AMD 1896103a8feaSLen Brown */ 1897103a8feaSLen Brown 1898103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 18998209e054SThomas Renninger has_aperf = ecx & (1 << 0); 1900889facbeSLen Brown do_dts = eax & (1 << 0); 1901889facbeSLen Brown do_ptm = eax & (1 << 6); 1902889facbeSLen Brown has_epb = ecx & (1 << 3); 1903889facbeSLen Brown 1904889facbeSLen Brown if (verbose) 1905889facbeSLen Brown fprintf(stderr, "CPUID(6): %s%s%s%s\n", 1906889facbeSLen Brown has_aperf ? "APERF" : "No APERF!", 1907889facbeSLen Brown do_dts ? ", DTS" : "", 1908889facbeSLen Brown do_ptm ? ", PTM": "", 1909889facbeSLen Brown has_epb ? ", EPB": ""); 1910889facbeSLen Brown 1911889facbeSLen Brown if (!has_aperf) 1912889facbeSLen Brown exit(-1); 1913103a8feaSLen Brown 1914103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 1915103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 1916*1ed51011SLen Brown do_smi = do_nhm_cstates; 1917103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 1918103a8feaSLen Brown bclk = discover_bclk(family, model); 1919103a8feaSLen Brown 1920103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 19216574a5d5SLen Brown do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 1922889facbeSLen Brown rapl_probe(family, model); 1923889facbeSLen Brown 1924889facbeSLen Brown return; 1925103a8feaSLen Brown } 1926103a8feaSLen Brown 1927103a8feaSLen Brown 1928103a8feaSLen Brown void usage() 1929103a8feaSLen Brown { 1930889facbeSLen Brown fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", 1931103a8feaSLen Brown progname); 1932103a8feaSLen Brown exit(1); 1933103a8feaSLen Brown } 1934103a8feaSLen Brown 1935103a8feaSLen Brown 1936103a8feaSLen Brown /* 1937103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 1938103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 1939103a8feaSLen Brown */ 1940103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 1941103a8feaSLen Brown { 1942103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 1943103a8feaSLen Brown return 1; 1944103a8feaSLen Brown else 1945103a8feaSLen Brown return 0; 1946103a8feaSLen Brown } 1947103a8feaSLen Brown 1948103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 1949103a8feaSLen Brown { 1950103a8feaSLen Brown return 0; 1951103a8feaSLen Brown } 1952103a8feaSLen Brown 1953c98d5d94SLen Brown void topology_probe() 1954c98d5d94SLen Brown { 1955c98d5d94SLen Brown int i; 1956c98d5d94SLen Brown int max_core_id = 0; 1957c98d5d94SLen Brown int max_package_id = 0; 1958c98d5d94SLen Brown int max_siblings = 0; 1959c98d5d94SLen Brown struct cpu_topology { 1960c98d5d94SLen Brown int core_id; 1961c98d5d94SLen Brown int physical_package_id; 1962c98d5d94SLen Brown } *cpus; 1963c98d5d94SLen Brown 1964c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 1965c98d5d94SLen Brown topo.num_cpus = 0; 1966c98d5d94SLen Brown topo.max_cpu_num = 0; 1967c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 1968c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 1969c98d5d94SLen Brown show_cpu = 1; 1970c98d5d94SLen Brown 1971c98d5d94SLen Brown if (verbose > 1) 1972c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 1973c98d5d94SLen Brown 1974c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 1975c98d5d94SLen Brown if (cpus == NULL) { 1976c98d5d94SLen Brown perror("calloc cpus"); 1977c98d5d94SLen Brown exit(1); 1978c98d5d94SLen Brown } 1979c98d5d94SLen Brown 1980c98d5d94SLen Brown /* 1981c98d5d94SLen Brown * Allocate and initialize cpu_present_set 1982c98d5d94SLen Brown */ 1983c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1984c98d5d94SLen Brown if (cpu_present_set == NULL) { 1985c98d5d94SLen Brown perror("CPU_ALLOC"); 1986c98d5d94SLen Brown exit(3); 1987c98d5d94SLen Brown } 1988c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 1989c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 1990c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 1991c98d5d94SLen Brown 1992c98d5d94SLen Brown /* 1993c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 1994c98d5d94SLen Brown */ 1995c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1996c98d5d94SLen Brown if (cpu_affinity_set == NULL) { 1997c98d5d94SLen Brown perror("CPU_ALLOC"); 1998c98d5d94SLen Brown exit(3); 1999c98d5d94SLen Brown } 2000c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2001c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 2002c98d5d94SLen Brown 2003c98d5d94SLen Brown 2004c98d5d94SLen Brown /* 2005c98d5d94SLen Brown * For online cpus 2006c98d5d94SLen Brown * find max_core_id, max_package_id 2007c98d5d94SLen Brown */ 2008c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 2009c98d5d94SLen Brown int siblings; 2010c98d5d94SLen Brown 2011c98d5d94SLen Brown if (cpu_is_not_present(i)) { 2012c98d5d94SLen Brown if (verbose > 1) 2013c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 2014c98d5d94SLen Brown continue; 2015c98d5d94SLen Brown } 2016c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 2017c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 2018c98d5d94SLen Brown max_core_id = cpus[i].core_id; 2019c98d5d94SLen Brown 2020c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 2021c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 2022c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 2023c98d5d94SLen Brown 2024c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 2025c98d5d94SLen Brown if (siblings > max_siblings) 2026c98d5d94SLen Brown max_siblings = siblings; 2027c98d5d94SLen Brown if (verbose > 1) 2028c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 2029c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 2030c98d5d94SLen Brown } 2031c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 2032c98d5d94SLen Brown if (verbose > 1) 2033c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 2034c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 2035c98d5d94SLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 2036c98d5d94SLen Brown show_core = 1; 2037c98d5d94SLen Brown 2038c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 2039c98d5d94SLen Brown if (verbose > 1) 2040c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 2041c98d5d94SLen Brown max_package_id, topo.num_packages); 2042c98d5d94SLen Brown if (!summary_only && topo.num_packages > 1) 2043c98d5d94SLen Brown show_pkg = 1; 2044c98d5d94SLen Brown 2045c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 2046c98d5d94SLen Brown if (verbose > 1) 2047c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 2048c98d5d94SLen Brown 2049c98d5d94SLen Brown free(cpus); 2050c98d5d94SLen Brown } 2051c98d5d94SLen Brown 2052c98d5d94SLen Brown void 2053c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 2054c98d5d94SLen Brown { 2055c98d5d94SLen Brown int i; 2056c98d5d94SLen Brown 2057c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 2058c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 2059c98d5d94SLen Brown if (*t == NULL) 2060c98d5d94SLen Brown goto error; 2061c98d5d94SLen Brown 2062c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 2063c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 2064c98d5d94SLen Brown (*t)[i].cpu_id = -1; 2065c98d5d94SLen Brown 2066c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 2067c98d5d94SLen Brown sizeof(struct core_data)); 2068c98d5d94SLen Brown if (*c == NULL) 2069c98d5d94SLen Brown goto error; 2070c98d5d94SLen Brown 2071c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 2072c98d5d94SLen Brown (*c)[i].core_id = -1; 2073c98d5d94SLen Brown 2074c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 2075c98d5d94SLen Brown if (*p == NULL) 2076c98d5d94SLen Brown goto error; 2077c98d5d94SLen Brown 2078c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 2079c98d5d94SLen Brown (*p)[i].package_id = i; 2080c98d5d94SLen Brown 2081c98d5d94SLen Brown return; 2082c98d5d94SLen Brown error: 2083c98d5d94SLen Brown perror("calloc counters"); 2084c98d5d94SLen Brown exit(1); 2085c98d5d94SLen Brown } 2086c98d5d94SLen Brown /* 2087c98d5d94SLen Brown * init_counter() 2088c98d5d94SLen Brown * 2089c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 2090c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 2091c98d5d94SLen Brown * 2092c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 2093c98d5d94SLen Brown */ 2094c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 2095c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 2096c98d5d94SLen Brown int pkg_num, int cpu_id) 2097c98d5d94SLen Brown { 2098c98d5d94SLen Brown struct thread_data *t; 2099c98d5d94SLen Brown struct core_data *c; 2100c98d5d94SLen Brown struct pkg_data *p; 2101c98d5d94SLen Brown 2102c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 2103c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 2104c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 2105c98d5d94SLen Brown 2106c98d5d94SLen Brown t->cpu_id = cpu_id; 2107c98d5d94SLen Brown if (thread_num == 0) { 2108c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 2109c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 2110c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 2111c98d5d94SLen Brown } 2112c98d5d94SLen Brown 2113c98d5d94SLen Brown c->core_id = core_num; 2114c98d5d94SLen Brown p->package_id = pkg_num; 2115c98d5d94SLen Brown } 2116c98d5d94SLen Brown 2117c98d5d94SLen Brown 2118c98d5d94SLen Brown int initialize_counters(int cpu_id) 2119c98d5d94SLen Brown { 2120c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 2121c98d5d94SLen Brown 2122c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 2123c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 2124c98d5d94SLen Brown 2125c98d5d94SLen Brown if (cpu_is_first_sibling_in_core(cpu_id)) { 2126c98d5d94SLen Brown my_thread_id = 0; 2127c98d5d94SLen Brown topo.num_cores++; 2128c98d5d94SLen Brown } else { 2129c98d5d94SLen Brown my_thread_id = 1; 2130c98d5d94SLen Brown } 2131c98d5d94SLen Brown 2132c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2133c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2134c98d5d94SLen Brown return 0; 2135c98d5d94SLen Brown } 2136c98d5d94SLen Brown 2137c98d5d94SLen Brown void allocate_output_buffer() 2138c98d5d94SLen Brown { 2139c98d5d94SLen Brown output_buffer = calloc(1, (1 + topo.num_cpus) * 128); 2140c98d5d94SLen Brown outp = output_buffer; 2141c98d5d94SLen Brown if (outp == NULL) { 2142c98d5d94SLen Brown perror("calloc"); 2143c98d5d94SLen Brown exit(-1); 2144c98d5d94SLen Brown } 2145c98d5d94SLen Brown } 2146c98d5d94SLen Brown 2147c98d5d94SLen Brown void setup_all_buffers(void) 2148c98d5d94SLen Brown { 2149c98d5d94SLen Brown topology_probe(); 2150c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 2151c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 2152c98d5d94SLen Brown allocate_output_buffer(); 2153c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 2154c98d5d94SLen Brown } 2155103a8feaSLen Brown void turbostat_init() 2156103a8feaSLen Brown { 2157103a8feaSLen Brown check_cpuid(); 2158103a8feaSLen Brown 2159103a8feaSLen Brown check_dev_msr(); 2160103a8feaSLen Brown check_super_user(); 2161103a8feaSLen Brown 2162c98d5d94SLen Brown setup_all_buffers(); 2163103a8feaSLen Brown 2164103a8feaSLen Brown if (verbose) 2165c98d5d94SLen Brown print_verbose_header(); 2166889facbeSLen Brown 2167889facbeSLen Brown if (verbose) 2168889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 2169889facbeSLen Brown 2170889facbeSLen Brown if (verbose) 2171889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 2172889facbeSLen Brown 2173889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 2174889facbeSLen Brown 2175889facbeSLen Brown if (verbose) 2176889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 2177103a8feaSLen Brown } 2178103a8feaSLen Brown 2179103a8feaSLen Brown int fork_it(char **argv) 2180103a8feaSLen Brown { 2181103a8feaSLen Brown pid_t child_pid; 2182d91bb17cSLen Brown int status; 2183d15cf7c1SLen Brown 2184d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 2185d91bb17cSLen Brown if (status) 2186d91bb17cSLen Brown exit(status); 2187d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 2188d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 2189103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2190103a8feaSLen Brown 2191103a8feaSLen Brown child_pid = fork(); 2192103a8feaSLen Brown if (!child_pid) { 2193103a8feaSLen Brown /* child */ 2194103a8feaSLen Brown execvp(argv[0], argv); 2195103a8feaSLen Brown } else { 2196103a8feaSLen Brown 2197103a8feaSLen Brown /* parent */ 2198103a8feaSLen Brown if (child_pid == -1) { 2199103a8feaSLen Brown perror("fork"); 2200103a8feaSLen Brown exit(1); 2201103a8feaSLen Brown } 2202103a8feaSLen Brown 2203103a8feaSLen Brown signal(SIGINT, SIG_IGN); 2204103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 2205103a8feaSLen Brown if (waitpid(child_pid, &status, 0) == -1) { 2206103a8feaSLen Brown perror("wait"); 2207d91bb17cSLen Brown exit(status); 2208103a8feaSLen Brown } 2209103a8feaSLen Brown } 2210c98d5d94SLen Brown /* 2211c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 2212c98d5d94SLen Brown * because re-starting is problematic when forking 2213c98d5d94SLen Brown */ 2214c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 2215103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2216103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2217c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2218c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2219c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2220c98d5d94SLen Brown flush_stderr(); 2221103a8feaSLen Brown 22226eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 2223103a8feaSLen Brown 2224d91bb17cSLen Brown return status; 2225103a8feaSLen Brown } 2226103a8feaSLen Brown 2227103a8feaSLen Brown void cmdline(int argc, char **argv) 2228103a8feaSLen Brown { 2229103a8feaSLen Brown int opt; 2230103a8feaSLen Brown 2231103a8feaSLen Brown progname = argv[0]; 2232103a8feaSLen Brown 2233889facbeSLen Brown while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) { 2234103a8feaSLen Brown switch (opt) { 2235f9240813SLen Brown case 'p': 2236c98d5d94SLen Brown show_core_only++; 2237c98d5d94SLen Brown break; 2238f9240813SLen Brown case 'P': 2239c98d5d94SLen Brown show_pkg_only++; 2240c98d5d94SLen Brown break; 2241f9240813SLen Brown case 'S': 2242e23da037SLen Brown summary_only++; 2243e23da037SLen Brown break; 2244103a8feaSLen Brown case 'v': 2245103a8feaSLen Brown verbose++; 2246103a8feaSLen Brown break; 2247103a8feaSLen Brown case 'i': 2248103a8feaSLen Brown interval_sec = atoi(optarg); 2249103a8feaSLen Brown break; 2250f9240813SLen Brown case 'c': 22518e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 22528e180f3cSLen Brown break; 2253f9240813SLen Brown case 'C': 22548e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 22558e180f3cSLen Brown break; 22562f32edf1SLen Brown case 'm': 22572f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 22582f32edf1SLen Brown break; 22592f32edf1SLen Brown case 'M': 22602f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 2261103a8feaSLen Brown break; 2262889facbeSLen Brown case 'R': 2263889facbeSLen Brown rapl_verbose++; 2264889facbeSLen Brown break; 2265889facbeSLen Brown case 'T': 2266889facbeSLen Brown tcc_activation_temp_override = atoi(optarg); 2267889facbeSLen Brown break; 2268103a8feaSLen Brown default: 2269103a8feaSLen Brown usage(); 2270103a8feaSLen Brown } 2271103a8feaSLen Brown } 2272103a8feaSLen Brown } 2273103a8feaSLen Brown 2274103a8feaSLen Brown int main(int argc, char **argv) 2275103a8feaSLen Brown { 2276103a8feaSLen Brown cmdline(argc, argv); 2277103a8feaSLen Brown 2278889facbeSLen Brown if (verbose) 2279*1ed51011SLen Brown fprintf(stderr, "turbostat v3.2 February 11, 2013" 2280103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 2281103a8feaSLen Brown 2282103a8feaSLen Brown turbostat_init(); 2283103a8feaSLen Brown 2284103a8feaSLen Brown /* 2285103a8feaSLen Brown * if any params left, it must be a command to fork 2286103a8feaSLen Brown */ 2287103a8feaSLen Brown if (argc - optind) 2288103a8feaSLen Brown return fork_it(argv + optind); 2289103a8feaSLen Brown else 2290103a8feaSLen Brown turbostat_loop(); 2291103a8feaSLen Brown 2292103a8feaSLen Brown return 0; 2293103a8feaSLen Brown } 2294