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 23103a8feaSLen Brown #include <stdio.h> 24103a8feaSLen Brown #include <unistd.h> 25103a8feaSLen Brown #include <sys/types.h> 26103a8feaSLen Brown #include <sys/wait.h> 27103a8feaSLen Brown #include <sys/stat.h> 28103a8feaSLen Brown #include <sys/resource.h> 29103a8feaSLen Brown #include <fcntl.h> 30103a8feaSLen Brown #include <signal.h> 31103a8feaSLen Brown #include <sys/time.h> 32103a8feaSLen Brown #include <stdlib.h> 33103a8feaSLen Brown #include <dirent.h> 34103a8feaSLen Brown #include <string.h> 35103a8feaSLen Brown #include <ctype.h> 3688c3281fSLen Brown #include <sched.h> 37103a8feaSLen Brown 38103a8feaSLen Brown #define MSR_TSC 0x10 39103a8feaSLen Brown #define MSR_NEHALEM_PLATFORM_INFO 0xCE 40103a8feaSLen Brown #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD 41103a8feaSLen Brown #define MSR_APERF 0xE8 42103a8feaSLen Brown #define MSR_MPERF 0xE7 43103a8feaSLen Brown #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */ 44103a8feaSLen Brown #define MSR_PKG_C3_RESIDENCY 0x3F8 45103a8feaSLen Brown #define MSR_PKG_C6_RESIDENCY 0x3F9 46103a8feaSLen Brown #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */ 47103a8feaSLen Brown #define MSR_CORE_C3_RESIDENCY 0x3FC 48103a8feaSLen Brown #define MSR_CORE_C6_RESIDENCY 0x3FD 49103a8feaSLen Brown #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */ 50103a8feaSLen Brown 51103a8feaSLen Brown char *proc_stat = "/proc/stat"; 52103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 53103a8feaSLen Brown unsigned int verbose; /* set with -v */ 54e23da037SLen Brown unsigned int summary_only; /* set with -s */ 55103a8feaSLen Brown unsigned int skip_c0; 56103a8feaSLen Brown unsigned int skip_c1; 57103a8feaSLen Brown unsigned int do_nhm_cstates; 58103a8feaSLen Brown unsigned int do_snb_cstates; 59103a8feaSLen Brown unsigned int has_aperf; 60103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 61103a8feaSLen Brown unsigned int genuine_intel; 62103a8feaSLen Brown unsigned int has_invariant_tsc; 63103a8feaSLen Brown unsigned int do_nehalem_platform_info; 64103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 65103a8feaSLen Brown unsigned int extra_msr_offset; 66103a8feaSLen Brown double bclk; 67103a8feaSLen Brown unsigned int show_pkg; 68103a8feaSLen Brown unsigned int show_core; 69103a8feaSLen Brown unsigned int show_cpu; 70103a8feaSLen Brown 71103a8feaSLen Brown int aperf_mperf_unstable; 72103a8feaSLen Brown int backwards_count; 73103a8feaSLen Brown char *progname; 74103a8feaSLen Brown 75103a8feaSLen Brown int num_cpus; 7688c3281fSLen Brown cpu_set_t *cpu_mask; 7788c3281fSLen Brown size_t cpu_mask_size; 78103a8feaSLen Brown 79a829eb4dSLen Brown struct counters { 80103a8feaSLen Brown unsigned long long tsc; /* per thread */ 81103a8feaSLen Brown unsigned long long aperf; /* per thread */ 82103a8feaSLen Brown unsigned long long mperf; /* per thread */ 83103a8feaSLen Brown unsigned long long c1; /* per thread (calculated) */ 84103a8feaSLen Brown unsigned long long c3; /* per core */ 85103a8feaSLen Brown unsigned long long c6; /* per core */ 86103a8feaSLen Brown unsigned long long c7; /* per core */ 87103a8feaSLen Brown unsigned long long pc2; /* per package */ 88103a8feaSLen Brown unsigned long long pc3; /* per package */ 89103a8feaSLen Brown unsigned long long pc6; /* per package */ 90103a8feaSLen Brown unsigned long long pc7; /* per package */ 91103a8feaSLen Brown unsigned long long extra_msr; /* per thread */ 92103a8feaSLen Brown int pkg; 93103a8feaSLen Brown int core; 94103a8feaSLen Brown int cpu; 95a829eb4dSLen Brown struct counters *next; 96a829eb4dSLen Brown }; 97103a8feaSLen Brown 98a829eb4dSLen Brown struct counters *cnt_even; 99a829eb4dSLen Brown struct counters *cnt_odd; 100a829eb4dSLen Brown struct counters *cnt_delta; 101a829eb4dSLen Brown struct counters *cnt_average; 102103a8feaSLen Brown struct timeval tv_even; 103103a8feaSLen Brown struct timeval tv_odd; 104103a8feaSLen Brown struct timeval tv_delta; 105103a8feaSLen Brown 10688c3281fSLen Brown /* 10788c3281fSLen Brown * cpu_mask_init(ncpus) 10888c3281fSLen Brown * 10988c3281fSLen Brown * allocate and clear cpu_mask 11088c3281fSLen Brown * set cpu_mask_size 11188c3281fSLen Brown */ 11288c3281fSLen Brown void cpu_mask_init(int ncpus) 11388c3281fSLen Brown { 11488c3281fSLen Brown cpu_mask = CPU_ALLOC(ncpus); 11588c3281fSLen Brown if (cpu_mask == NULL) { 11688c3281fSLen Brown perror("CPU_ALLOC"); 11788c3281fSLen Brown exit(3); 11888c3281fSLen Brown } 11988c3281fSLen Brown cpu_mask_size = CPU_ALLOC_SIZE(ncpus); 12088c3281fSLen Brown CPU_ZERO_S(cpu_mask_size, cpu_mask); 12188c3281fSLen Brown } 12288c3281fSLen Brown 12388c3281fSLen Brown void cpu_mask_uninit() 12488c3281fSLen Brown { 12588c3281fSLen Brown CPU_FREE(cpu_mask); 12688c3281fSLen Brown cpu_mask = NULL; 12788c3281fSLen Brown cpu_mask_size = 0; 12888c3281fSLen Brown } 12988c3281fSLen Brown 13088c3281fSLen Brown int cpu_migrate(int cpu) 13188c3281fSLen Brown { 13288c3281fSLen Brown CPU_ZERO_S(cpu_mask_size, cpu_mask); 13388c3281fSLen Brown CPU_SET_S(cpu, cpu_mask_size, cpu_mask); 13488c3281fSLen Brown if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1) 13588c3281fSLen Brown return -1; 13688c3281fSLen Brown else 13788c3281fSLen Brown return 0; 13888c3281fSLen Brown } 13988c3281fSLen Brown 140*15aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 141103a8feaSLen Brown { 142103a8feaSLen Brown ssize_t retval; 143103a8feaSLen Brown char pathname[32]; 144103a8feaSLen Brown int fd; 145103a8feaSLen Brown 146103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 147103a8feaSLen Brown fd = open(pathname, O_RDONLY); 148*15aaa346SLen Brown if (fd < 0) 149*15aaa346SLen Brown return -1; 150103a8feaSLen Brown 151*15aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 152103a8feaSLen Brown close(fd); 153*15aaa346SLen Brown 154*15aaa346SLen Brown if (retval != sizeof *msr) 155*15aaa346SLen Brown return -1; 156*15aaa346SLen Brown 157*15aaa346SLen Brown return 0; 158103a8feaSLen Brown } 159103a8feaSLen Brown 160a829eb4dSLen Brown void print_header(void) 161103a8feaSLen Brown { 162103a8feaSLen Brown if (show_pkg) 163d30c4b7aSLen Brown fprintf(stderr, "pk"); 164e23da037SLen Brown if (show_pkg) 165e23da037SLen Brown fprintf(stderr, " "); 166103a8feaSLen Brown if (show_core) 167e23da037SLen Brown fprintf(stderr, "cor"); 168103a8feaSLen Brown if (show_cpu) 169103a8feaSLen Brown fprintf(stderr, " CPU"); 170e23da037SLen Brown if (show_pkg || show_core || show_cpu) 171e23da037SLen Brown fprintf(stderr, " "); 172103a8feaSLen Brown if (do_nhm_cstates) 173103a8feaSLen Brown fprintf(stderr, " %%c0"); 174103a8feaSLen Brown if (has_aperf) 175103a8feaSLen Brown fprintf(stderr, " GHz"); 176103a8feaSLen Brown fprintf(stderr, " TSC"); 177103a8feaSLen Brown if (do_nhm_cstates) 178103a8feaSLen Brown fprintf(stderr, " %%c1"); 179103a8feaSLen Brown if (do_nhm_cstates) 180103a8feaSLen Brown fprintf(stderr, " %%c3"); 181103a8feaSLen Brown if (do_nhm_cstates) 182103a8feaSLen Brown fprintf(stderr, " %%c6"); 183103a8feaSLen Brown if (do_snb_cstates) 184103a8feaSLen Brown fprintf(stderr, " %%c7"); 185103a8feaSLen Brown if (do_snb_cstates) 186103a8feaSLen Brown fprintf(stderr, " %%pc2"); 187103a8feaSLen Brown if (do_nhm_cstates) 188103a8feaSLen Brown fprintf(stderr, " %%pc3"); 189103a8feaSLen Brown if (do_nhm_cstates) 190103a8feaSLen Brown fprintf(stderr, " %%pc6"); 191103a8feaSLen Brown if (do_snb_cstates) 192103a8feaSLen Brown fprintf(stderr, " %%pc7"); 193103a8feaSLen Brown if (extra_msr_offset) 194103a8feaSLen Brown fprintf(stderr, " MSR 0x%x ", extra_msr_offset); 195103a8feaSLen Brown 196103a8feaSLen Brown putc('\n', stderr); 197103a8feaSLen Brown } 198103a8feaSLen Brown 199a829eb4dSLen Brown void dump_cnt(struct counters *cnt) 200103a8feaSLen Brown { 201aeae1e92SLen Brown if (!cnt) 202aeae1e92SLen Brown return; 203aeae1e92SLen Brown if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg); 204aeae1e92SLen Brown if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core); 205aeae1e92SLen Brown if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu); 206aeae1e92SLen Brown if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc); 207aeae1e92SLen Brown if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3); 208aeae1e92SLen Brown if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6); 209aeae1e92SLen Brown if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7); 210aeae1e92SLen Brown if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf); 211aeae1e92SLen Brown if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2); 212aeae1e92SLen Brown if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3); 213aeae1e92SLen Brown if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6); 214aeae1e92SLen Brown if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7); 215aeae1e92SLen Brown if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr); 216103a8feaSLen Brown } 217103a8feaSLen Brown 218a829eb4dSLen Brown void dump_list(struct counters *cnt) 219103a8feaSLen Brown { 220a829eb4dSLen Brown printf("dump_list 0x%p\n", cnt); 221103a8feaSLen Brown 222a829eb4dSLen Brown for (; cnt; cnt = cnt->next) 223a829eb4dSLen Brown dump_cnt(cnt); 224103a8feaSLen Brown } 225103a8feaSLen Brown 226e23da037SLen Brown /* 227e23da037SLen Brown * column formatting convention & formats 228e23da037SLen Brown * package: "pk" 2 columns %2d 229e23da037SLen Brown * core: "cor" 3 columns %3d 230e23da037SLen Brown * CPU: "CPU" 3 columns %3d 231e23da037SLen Brown * GHz: "GHz" 3 columns %3.2 232e23da037SLen Brown * TSC: "TSC" 3 columns %3.2 233e23da037SLen Brown * percentage " %pc3" %6.2 234e23da037SLen Brown */ 235a829eb4dSLen Brown void print_cnt(struct counters *p) 236103a8feaSLen Brown { 237103a8feaSLen Brown double interval_float; 238103a8feaSLen Brown 239103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 240103a8feaSLen Brown 241103a8feaSLen Brown /* topology columns, print blanks on 1st (average) line */ 242a829eb4dSLen Brown if (p == cnt_average) { 243103a8feaSLen Brown if (show_pkg) 244103a8feaSLen Brown fprintf(stderr, " "); 245e23da037SLen Brown if (show_pkg && show_core) 246e23da037SLen Brown fprintf(stderr, " "); 247103a8feaSLen Brown if (show_core) 248103a8feaSLen Brown fprintf(stderr, " "); 249103a8feaSLen Brown if (show_cpu) 250e23da037SLen Brown fprintf(stderr, " " " "); 251103a8feaSLen Brown } else { 252103a8feaSLen Brown if (show_pkg) 253e23da037SLen Brown fprintf(stderr, "%2d", p->pkg); 254e23da037SLen Brown if (show_pkg && show_core) 255e23da037SLen Brown fprintf(stderr, " "); 256103a8feaSLen Brown if (show_core) 257e23da037SLen Brown fprintf(stderr, "%3d", p->core); 258103a8feaSLen Brown if (show_cpu) 259e23da037SLen Brown fprintf(stderr, " %3d", p->cpu); 260103a8feaSLen Brown } 261103a8feaSLen Brown 262103a8feaSLen Brown /* %c0 */ 263103a8feaSLen Brown if (do_nhm_cstates) { 264e23da037SLen Brown if (show_pkg || show_core || show_cpu) 265e23da037SLen Brown fprintf(stderr, " "); 266103a8feaSLen Brown if (!skip_c0) 267e23da037SLen Brown fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc); 268103a8feaSLen Brown else 269103a8feaSLen Brown fprintf(stderr, " ****"); 270103a8feaSLen Brown } 271103a8feaSLen Brown 272103a8feaSLen Brown /* GHz */ 273103a8feaSLen Brown if (has_aperf) { 274103a8feaSLen Brown if (!aperf_mperf_unstable) { 275e23da037SLen Brown fprintf(stderr, " %3.2f", 276103a8feaSLen Brown 1.0 * p->tsc / units * p->aperf / 277103a8feaSLen Brown p->mperf / interval_float); 278103a8feaSLen Brown } else { 279103a8feaSLen Brown if (p->aperf > p->tsc || p->mperf > p->tsc) { 280e23da037SLen Brown fprintf(stderr, " ***"); 281103a8feaSLen Brown } else { 282e23da037SLen Brown fprintf(stderr, "%3.1f*", 283103a8feaSLen Brown 1.0 * p->tsc / 284103a8feaSLen Brown units * p->aperf / 285103a8feaSLen Brown p->mperf / interval_float); 286103a8feaSLen Brown } 287103a8feaSLen Brown } 288103a8feaSLen Brown } 289103a8feaSLen Brown 290103a8feaSLen Brown /* TSC */ 291103a8feaSLen Brown fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float); 292103a8feaSLen Brown 293103a8feaSLen Brown if (do_nhm_cstates) { 294103a8feaSLen Brown if (!skip_c1) 295e23da037SLen Brown fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc); 296103a8feaSLen Brown else 297103a8feaSLen Brown fprintf(stderr, " ****"); 298103a8feaSLen Brown } 299103a8feaSLen Brown if (do_nhm_cstates) 300d30c4b7aSLen Brown fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc); 301103a8feaSLen Brown if (do_nhm_cstates) 302d30c4b7aSLen Brown fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc); 303103a8feaSLen Brown if (do_snb_cstates) 304d30c4b7aSLen Brown fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc); 305103a8feaSLen Brown if (do_snb_cstates) 306e23da037SLen Brown fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc); 307103a8feaSLen Brown if (do_nhm_cstates) 308e23da037SLen Brown fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc); 309103a8feaSLen Brown if (do_nhm_cstates) 310e23da037SLen Brown fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc); 311103a8feaSLen Brown if (do_snb_cstates) 312e23da037SLen Brown fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc); 313103a8feaSLen Brown if (extra_msr_offset) 314103a8feaSLen Brown fprintf(stderr, " 0x%016llx", p->extra_msr); 315103a8feaSLen Brown putc('\n', stderr); 316103a8feaSLen Brown } 317103a8feaSLen Brown 318a829eb4dSLen Brown void print_counters(struct counters *counters) 319103a8feaSLen Brown { 320a829eb4dSLen Brown struct counters *cnt; 321e23da037SLen Brown static int printed; 322103a8feaSLen Brown 323e23da037SLen Brown 324e23da037SLen Brown if (!printed || !summary_only) 325103a8feaSLen Brown print_header(); 326103a8feaSLen Brown 327103a8feaSLen Brown if (num_cpus > 1) 328a829eb4dSLen Brown print_cnt(cnt_average); 329103a8feaSLen Brown 330e23da037SLen Brown printed = 1; 331e23da037SLen Brown 332e23da037SLen Brown if (summary_only) 333e23da037SLen Brown return; 334e23da037SLen Brown 335a829eb4dSLen Brown for (cnt = counters; cnt != NULL; cnt = cnt->next) 336a829eb4dSLen Brown print_cnt(cnt); 337103a8feaSLen Brown 338103a8feaSLen Brown } 339103a8feaSLen Brown 340103a8feaSLen Brown #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after)) 341103a8feaSLen Brown 342a829eb4dSLen Brown int compute_delta(struct counters *after, 343a829eb4dSLen Brown struct counters *before, struct counters *delta) 344103a8feaSLen Brown { 345103a8feaSLen Brown int errors = 0; 346103a8feaSLen Brown int perf_err = 0; 347103a8feaSLen Brown 348103a8feaSLen Brown skip_c0 = skip_c1 = 0; 349103a8feaSLen Brown 350103a8feaSLen Brown for ( ; after && before && delta; 351103a8feaSLen Brown after = after->next, before = before->next, delta = delta->next) { 352103a8feaSLen Brown if (before->cpu != after->cpu) { 353103a8feaSLen Brown printf("cpu configuration changed: %d != %d\n", 354103a8feaSLen Brown before->cpu, after->cpu); 355103a8feaSLen Brown return -1; 356103a8feaSLen Brown } 357103a8feaSLen Brown 358103a8feaSLen Brown if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) { 359103a8feaSLen Brown fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n", 360103a8feaSLen Brown before->cpu, before->tsc, after->tsc); 361103a8feaSLen Brown errors++; 362103a8feaSLen Brown } 363103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 364103a8feaSLen Brown if (delta->tsc < (1000 * 1000)) { 365103a8feaSLen Brown fprintf(stderr, "Insanely slow TSC rate," 366103a8feaSLen Brown " TSC stops in idle?\n"); 367103a8feaSLen Brown fprintf(stderr, "You can disable all c-states" 368103a8feaSLen Brown " by booting with \"idle=poll\"\n"); 369103a8feaSLen Brown fprintf(stderr, "or just the deep ones with" 370103a8feaSLen Brown " \"processor.max_cstate=1\"\n"); 371103a8feaSLen Brown exit(-3); 372103a8feaSLen Brown } 373103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) { 374103a8feaSLen Brown fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n", 375103a8feaSLen Brown before->cpu, before->c3, after->c3); 376103a8feaSLen Brown errors++; 377103a8feaSLen Brown } 378103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) { 379103a8feaSLen Brown fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n", 380103a8feaSLen Brown before->cpu, before->c6, after->c6); 381103a8feaSLen Brown errors++; 382103a8feaSLen Brown } 383103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) { 384103a8feaSLen Brown fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n", 385103a8feaSLen Brown before->cpu, before->c7, after->c7); 386103a8feaSLen Brown errors++; 387103a8feaSLen Brown } 388103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) { 389103a8feaSLen Brown fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n", 390103a8feaSLen Brown before->cpu, before->pc2, after->pc2); 391103a8feaSLen Brown errors++; 392103a8feaSLen Brown } 393103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) { 394103a8feaSLen Brown fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n", 395103a8feaSLen Brown before->cpu, before->pc3, after->pc3); 396103a8feaSLen Brown errors++; 397103a8feaSLen Brown } 398103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) { 399103a8feaSLen Brown fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n", 400103a8feaSLen Brown before->cpu, before->pc6, after->pc6); 401103a8feaSLen Brown errors++; 402103a8feaSLen Brown } 403103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) { 404103a8feaSLen Brown fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n", 405103a8feaSLen Brown before->cpu, before->pc7, after->pc7); 406103a8feaSLen Brown errors++; 407103a8feaSLen Brown } 408103a8feaSLen Brown 409103a8feaSLen Brown perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf); 410103a8feaSLen Brown if (perf_err) { 411103a8feaSLen Brown fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n", 412103a8feaSLen Brown before->cpu, before->aperf, after->aperf); 413103a8feaSLen Brown } 414103a8feaSLen Brown perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf); 415103a8feaSLen Brown if (perf_err) { 416103a8feaSLen Brown fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n", 417103a8feaSLen Brown before->cpu, before->mperf, after->mperf); 418103a8feaSLen Brown } 419103a8feaSLen Brown if (perf_err) { 420103a8feaSLen Brown if (!aperf_mperf_unstable) { 421103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 422103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 423103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 424103a8feaSLen Brown 425103a8feaSLen Brown aperf_mperf_unstable = 1; 426103a8feaSLen Brown } 427103a8feaSLen Brown /* 428103a8feaSLen Brown * mperf delta is likely a huge "positive" number 429103a8feaSLen Brown * can not use it for calculating c0 time 430103a8feaSLen Brown */ 431103a8feaSLen Brown skip_c0 = 1; 432103a8feaSLen Brown skip_c1 = 1; 433103a8feaSLen Brown } 434103a8feaSLen Brown 435103a8feaSLen Brown /* 436103a8feaSLen Brown * As mperf and tsc collection are not atomic, 437103a8feaSLen Brown * it is possible for mperf's non-halted cycles 438103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 439103a8feaSLen Brown */ 440103a8feaSLen Brown if (delta->mperf > delta->tsc) 441103a8feaSLen Brown delta->c1 = 0; 442103a8feaSLen Brown else /* normal case, derive c1 */ 443103a8feaSLen Brown delta->c1 = delta->tsc - delta->mperf 444103a8feaSLen Brown - delta->c3 - delta->c6 - delta->c7; 445103a8feaSLen Brown 446103a8feaSLen Brown if (delta->mperf == 0) 447103a8feaSLen Brown delta->mperf = 1; /* divide by 0 protection */ 448103a8feaSLen Brown 449103a8feaSLen Brown /* 450103a8feaSLen Brown * for "extra msr", just copy the latest w/o subtracting 451103a8feaSLen Brown */ 452103a8feaSLen Brown delta->extra_msr = after->extra_msr; 453103a8feaSLen Brown if (errors) { 454103a8feaSLen Brown fprintf(stderr, "ERROR cpu%d before:\n", before->cpu); 455a829eb4dSLen Brown dump_cnt(before); 456103a8feaSLen Brown fprintf(stderr, "ERROR cpu%d after:\n", before->cpu); 457a829eb4dSLen Brown dump_cnt(after); 458103a8feaSLen Brown errors = 0; 459103a8feaSLen Brown } 460103a8feaSLen Brown } 461103a8feaSLen Brown return 0; 462103a8feaSLen Brown } 463103a8feaSLen Brown 464a829eb4dSLen Brown void compute_average(struct counters *delta, struct counters *avg) 465103a8feaSLen Brown { 466a829eb4dSLen Brown struct counters *sum; 467103a8feaSLen Brown 468a829eb4dSLen Brown sum = calloc(1, sizeof(struct counters)); 469103a8feaSLen Brown if (sum == NULL) { 470103a8feaSLen Brown perror("calloc sum"); 471103a8feaSLen Brown exit(1); 472103a8feaSLen Brown } 473103a8feaSLen Brown 474103a8feaSLen Brown for (; delta; delta = delta->next) { 475103a8feaSLen Brown sum->tsc += delta->tsc; 476103a8feaSLen Brown sum->c1 += delta->c1; 477103a8feaSLen Brown sum->c3 += delta->c3; 478103a8feaSLen Brown sum->c6 += delta->c6; 479103a8feaSLen Brown sum->c7 += delta->c7; 480103a8feaSLen Brown sum->aperf += delta->aperf; 481103a8feaSLen Brown sum->mperf += delta->mperf; 482103a8feaSLen Brown sum->pc2 += delta->pc2; 483103a8feaSLen Brown sum->pc3 += delta->pc3; 484103a8feaSLen Brown sum->pc6 += delta->pc6; 485103a8feaSLen Brown sum->pc7 += delta->pc7; 486103a8feaSLen Brown } 487103a8feaSLen Brown avg->tsc = sum->tsc/num_cpus; 488103a8feaSLen Brown avg->c1 = sum->c1/num_cpus; 489103a8feaSLen Brown avg->c3 = sum->c3/num_cpus; 490103a8feaSLen Brown avg->c6 = sum->c6/num_cpus; 491103a8feaSLen Brown avg->c7 = sum->c7/num_cpus; 492103a8feaSLen Brown avg->aperf = sum->aperf/num_cpus; 493103a8feaSLen Brown avg->mperf = sum->mperf/num_cpus; 494103a8feaSLen Brown avg->pc2 = sum->pc2/num_cpus; 495103a8feaSLen Brown avg->pc3 = sum->pc3/num_cpus; 496103a8feaSLen Brown avg->pc6 = sum->pc6/num_cpus; 497103a8feaSLen Brown avg->pc7 = sum->pc7/num_cpus; 498103a8feaSLen Brown 499103a8feaSLen Brown free(sum); 500103a8feaSLen Brown } 501103a8feaSLen Brown 502*15aaa346SLen Brown int get_counters(struct counters *cnt) 503103a8feaSLen Brown { 504a829eb4dSLen Brown for ( ; cnt; cnt = cnt->next) { 505*15aaa346SLen Brown 506*15aaa346SLen Brown if (cpu_migrate(cnt->cpu)) 507*15aaa346SLen Brown return -1; 508*15aaa346SLen Brown 509*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc)) 510*15aaa346SLen Brown return -1; 511*15aaa346SLen Brown 512*15aaa346SLen Brown if (has_aperf) { 513*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf)) 514*15aaa346SLen Brown return -1; 515*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf)) 516*15aaa346SLen Brown return -1; 51788c3281fSLen Brown } 51888c3281fSLen Brown 519*15aaa346SLen Brown if (do_nhm_cstates) { 520*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3)) 521*15aaa346SLen Brown return -1; 522*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6)) 523*15aaa346SLen Brown return -1; 524103a8feaSLen Brown } 525*15aaa346SLen Brown 526*15aaa346SLen Brown if (do_snb_cstates) 527*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7)) 528*15aaa346SLen Brown return -1; 529*15aaa346SLen Brown 530*15aaa346SLen Brown if (do_nhm_cstates) { 531*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3)) 532*15aaa346SLen Brown return -1; 533*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6)) 534*15aaa346SLen Brown return -1; 535*15aaa346SLen Brown } 536*15aaa346SLen Brown if (do_snb_cstates) { 537*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2)) 538*15aaa346SLen Brown return -1; 539*15aaa346SLen Brown if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7)) 540*15aaa346SLen Brown return -1; 541*15aaa346SLen Brown } 542*15aaa346SLen Brown if (extra_msr_offset) 543*15aaa346SLen Brown if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr)) 544*15aaa346SLen Brown return -1; 545*15aaa346SLen Brown } 546*15aaa346SLen Brown return 0; 547103a8feaSLen Brown } 548103a8feaSLen Brown 549a829eb4dSLen Brown void print_nehalem_info(void) 550103a8feaSLen Brown { 551103a8feaSLen Brown unsigned long long msr; 552103a8feaSLen Brown unsigned int ratio; 553103a8feaSLen Brown 554103a8feaSLen Brown if (!do_nehalem_platform_info) 555103a8feaSLen Brown return; 556103a8feaSLen Brown 557*15aaa346SLen Brown get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr); 558103a8feaSLen Brown 559103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 560103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 561103a8feaSLen Brown ratio, bclk, ratio * bclk); 562103a8feaSLen Brown 563103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 564103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 565103a8feaSLen Brown ratio, bclk, ratio * bclk); 566103a8feaSLen Brown 567103a8feaSLen Brown if (verbose > 1) 568103a8feaSLen Brown fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr); 569103a8feaSLen Brown 570103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 571103a8feaSLen Brown return; 572103a8feaSLen Brown 573*15aaa346SLen Brown get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr); 574103a8feaSLen Brown 575103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 576103a8feaSLen Brown if (ratio) 577103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 578103a8feaSLen Brown ratio, bclk, ratio * bclk); 579103a8feaSLen Brown 580103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 581103a8feaSLen Brown if (ratio) 582103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 583103a8feaSLen Brown ratio, bclk, ratio * bclk); 584103a8feaSLen Brown 585103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 586103a8feaSLen Brown if (ratio) 587103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 588103a8feaSLen Brown ratio, bclk, ratio * bclk); 589103a8feaSLen Brown 590103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 591103a8feaSLen Brown if (ratio) 592103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 593103a8feaSLen Brown ratio, bclk, ratio * bclk); 594103a8feaSLen Brown 595103a8feaSLen Brown } 596103a8feaSLen Brown 597a829eb4dSLen Brown void free_counter_list(struct counters *list) 598103a8feaSLen Brown { 599a829eb4dSLen Brown struct counters *p; 600103a8feaSLen Brown 601103a8feaSLen Brown for (p = list; p; ) { 602a829eb4dSLen Brown struct counters *free_me; 603103a8feaSLen Brown 604103a8feaSLen Brown free_me = p; 605103a8feaSLen Brown p = p->next; 606103a8feaSLen Brown free(free_me); 607103a8feaSLen Brown } 608103a8feaSLen Brown } 609103a8feaSLen Brown 610103a8feaSLen Brown void free_all_counters(void) 611103a8feaSLen Brown { 612a829eb4dSLen Brown free_counter_list(cnt_even); 613a829eb4dSLen Brown cnt_even = NULL; 614103a8feaSLen Brown 615a829eb4dSLen Brown free_counter_list(cnt_odd); 616a829eb4dSLen Brown cnt_odd = NULL; 617103a8feaSLen Brown 618a829eb4dSLen Brown free_counter_list(cnt_delta); 619a829eb4dSLen Brown cnt_delta = NULL; 620103a8feaSLen Brown 621a829eb4dSLen Brown free_counter_list(cnt_average); 622a829eb4dSLen Brown cnt_average = NULL; 623103a8feaSLen Brown } 624103a8feaSLen Brown 625a829eb4dSLen Brown void insert_counters(struct counters **list, 626a829eb4dSLen Brown struct counters *new) 627103a8feaSLen Brown { 628a829eb4dSLen Brown struct counters *prev; 629103a8feaSLen Brown 630103a8feaSLen Brown /* 631103a8feaSLen Brown * list was empty 632103a8feaSLen Brown */ 633103a8feaSLen Brown if (*list == NULL) { 634103a8feaSLen Brown new->next = *list; 635103a8feaSLen Brown *list = new; 636103a8feaSLen Brown return; 637103a8feaSLen Brown } 638103a8feaSLen Brown 639e23da037SLen Brown if (!summary_only) 640103a8feaSLen Brown show_cpu = 1; /* there is more than one CPU */ 641103a8feaSLen Brown 642103a8feaSLen Brown /* 643103a8feaSLen Brown * insert on front of list. 644103a8feaSLen Brown * It is sorted by ascending package#, core#, cpu# 645103a8feaSLen Brown */ 646103a8feaSLen Brown if (((*list)->pkg > new->pkg) || 647103a8feaSLen Brown (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) || 648103a8feaSLen Brown (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) { 649103a8feaSLen Brown new->next = *list; 650103a8feaSLen Brown *list = new; 651103a8feaSLen Brown return; 652103a8feaSLen Brown } 653103a8feaSLen Brown 654103a8feaSLen Brown prev = *list; 655103a8feaSLen Brown 656103a8feaSLen Brown while (prev->next && (prev->next->pkg < new->pkg)) { 657103a8feaSLen Brown prev = prev->next; 658e23da037SLen Brown if (!summary_only) 659103a8feaSLen Brown show_pkg = 1; /* there is more than 1 package */ 660103a8feaSLen Brown } 661103a8feaSLen Brown 662103a8feaSLen Brown while (prev->next && (prev->next->pkg == new->pkg) 663103a8feaSLen Brown && (prev->next->core < new->core)) { 664103a8feaSLen Brown prev = prev->next; 665e23da037SLen Brown if (!summary_only) 666103a8feaSLen Brown show_core = 1; /* there is more than 1 core */ 667103a8feaSLen Brown } 668103a8feaSLen Brown 669103a8feaSLen Brown while (prev->next && (prev->next->pkg == new->pkg) 670103a8feaSLen Brown && (prev->next->core == new->core) 671103a8feaSLen Brown && (prev->next->cpu < new->cpu)) { 672103a8feaSLen Brown prev = prev->next; 673103a8feaSLen Brown } 674103a8feaSLen Brown 675103a8feaSLen Brown /* 676103a8feaSLen Brown * insert after "prev" 677103a8feaSLen Brown */ 678103a8feaSLen Brown new->next = prev->next; 679103a8feaSLen Brown prev->next = new; 680103a8feaSLen Brown } 681103a8feaSLen Brown 682a829eb4dSLen Brown void alloc_new_counters(int pkg, int core, int cpu) 683103a8feaSLen Brown { 684a829eb4dSLen Brown struct counters *new; 685103a8feaSLen Brown 686103a8feaSLen Brown if (verbose > 1) 687103a8feaSLen Brown printf("pkg%d core%d, cpu%d\n", pkg, core, cpu); 688103a8feaSLen Brown 689a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 690103a8feaSLen Brown if (new == NULL) { 691103a8feaSLen Brown perror("calloc"); 692103a8feaSLen Brown exit(1); 693103a8feaSLen Brown } 694103a8feaSLen Brown new->pkg = pkg; 695103a8feaSLen Brown new->core = core; 696103a8feaSLen Brown new->cpu = cpu; 697a829eb4dSLen Brown insert_counters(&cnt_odd, new); 698103a8feaSLen Brown 699a829eb4dSLen Brown new = (struct counters *)calloc(1, 700a829eb4dSLen Brown sizeof(struct counters)); 701103a8feaSLen Brown if (new == NULL) { 702103a8feaSLen Brown perror("calloc"); 703103a8feaSLen Brown exit(1); 704103a8feaSLen Brown } 705103a8feaSLen Brown new->pkg = pkg; 706103a8feaSLen Brown new->core = core; 707103a8feaSLen Brown new->cpu = cpu; 708a829eb4dSLen Brown insert_counters(&cnt_even, new); 709103a8feaSLen Brown 710a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 711103a8feaSLen Brown if (new == NULL) { 712103a8feaSLen Brown perror("calloc"); 713103a8feaSLen Brown exit(1); 714103a8feaSLen Brown } 715103a8feaSLen Brown new->pkg = pkg; 716103a8feaSLen Brown new->core = core; 717103a8feaSLen Brown new->cpu = cpu; 718a829eb4dSLen Brown insert_counters(&cnt_delta, new); 719103a8feaSLen Brown 720a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 721103a8feaSLen Brown if (new == NULL) { 722103a8feaSLen Brown perror("calloc"); 723103a8feaSLen Brown exit(1); 724103a8feaSLen Brown } 725103a8feaSLen Brown new->pkg = pkg; 726103a8feaSLen Brown new->core = core; 727103a8feaSLen Brown new->cpu = cpu; 728a829eb4dSLen Brown cnt_average = new; 729103a8feaSLen Brown } 730103a8feaSLen Brown 731103a8feaSLen Brown int get_physical_package_id(int cpu) 732103a8feaSLen Brown { 733103a8feaSLen Brown char path[64]; 734103a8feaSLen Brown FILE *filep; 735103a8feaSLen Brown int pkg; 736103a8feaSLen Brown 737103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 738103a8feaSLen Brown filep = fopen(path, "r"); 739103a8feaSLen Brown if (filep == NULL) { 740103a8feaSLen Brown perror(path); 741103a8feaSLen Brown exit(1); 742103a8feaSLen Brown } 743103a8feaSLen Brown fscanf(filep, "%d", &pkg); 744103a8feaSLen Brown fclose(filep); 745103a8feaSLen Brown return pkg; 746103a8feaSLen Brown } 747103a8feaSLen Brown 748103a8feaSLen Brown int get_core_id(int cpu) 749103a8feaSLen Brown { 750103a8feaSLen Brown char path[64]; 751103a8feaSLen Brown FILE *filep; 752103a8feaSLen Brown int core; 753103a8feaSLen Brown 754103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 755103a8feaSLen Brown filep = fopen(path, "r"); 756103a8feaSLen Brown if (filep == NULL) { 757103a8feaSLen Brown perror(path); 758103a8feaSLen Brown exit(1); 759103a8feaSLen Brown } 760103a8feaSLen Brown fscanf(filep, "%d", &core); 761103a8feaSLen Brown fclose(filep); 762103a8feaSLen Brown return core; 763103a8feaSLen Brown } 764103a8feaSLen Brown 765103a8feaSLen Brown /* 766*15aaa346SLen Brown * run func(pkg, core, cpu) on every cpu in /proc/stat 767103a8feaSLen Brown */ 768103a8feaSLen Brown 769103a8feaSLen Brown int for_all_cpus(void (func)(int, int, int)) 770103a8feaSLen Brown { 771103a8feaSLen Brown FILE *fp; 772103a8feaSLen Brown int cpu_count; 773103a8feaSLen Brown int retval; 774103a8feaSLen Brown 775103a8feaSLen Brown fp = fopen(proc_stat, "r"); 776103a8feaSLen Brown if (fp == NULL) { 777103a8feaSLen Brown perror(proc_stat); 778103a8feaSLen Brown exit(1); 779103a8feaSLen Brown } 780103a8feaSLen Brown 781103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 782103a8feaSLen Brown if (retval != 0) { 783103a8feaSLen Brown perror("/proc/stat format"); 784103a8feaSLen Brown exit(1); 785103a8feaSLen Brown } 786103a8feaSLen Brown 787103a8feaSLen Brown for (cpu_count = 0; ; cpu_count++) { 788103a8feaSLen Brown int cpu; 789103a8feaSLen Brown 790103a8feaSLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu); 791103a8feaSLen Brown if (retval != 1) 792103a8feaSLen Brown break; 793103a8feaSLen Brown 794103a8feaSLen Brown func(get_physical_package_id(cpu), get_core_id(cpu), cpu); 795103a8feaSLen Brown } 796103a8feaSLen Brown fclose(fp); 797103a8feaSLen Brown return cpu_count; 798103a8feaSLen Brown } 799103a8feaSLen Brown 800103a8feaSLen Brown void re_initialize(void) 801103a8feaSLen Brown { 802103a8feaSLen Brown free_all_counters(); 803a829eb4dSLen Brown num_cpus = for_all_cpus(alloc_new_counters); 80488c3281fSLen Brown cpu_mask_uninit(); 80588c3281fSLen Brown cpu_mask_init(num_cpus); 806*15aaa346SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", num_cpus); 807103a8feaSLen Brown } 808103a8feaSLen Brown 809103a8feaSLen Brown void dummy(int pkg, int core, int cpu) { return; } 810103a8feaSLen Brown /* 811103a8feaSLen Brown * check to see if a cpu came on-line 812103a8feaSLen Brown */ 813*15aaa346SLen Brown int verify_num_cpus(void) 814103a8feaSLen Brown { 815103a8feaSLen Brown int new_num_cpus; 816103a8feaSLen Brown 817103a8feaSLen Brown new_num_cpus = for_all_cpus(dummy); 818103a8feaSLen Brown 819103a8feaSLen Brown if (new_num_cpus != num_cpus) { 820103a8feaSLen Brown if (verbose) 821103a8feaSLen Brown printf("num_cpus was %d, is now %d\n", 822103a8feaSLen Brown num_cpus, new_num_cpus); 823*15aaa346SLen Brown return -1; 824103a8feaSLen Brown } 825*15aaa346SLen Brown return 0; 826103a8feaSLen Brown } 827103a8feaSLen Brown 828103a8feaSLen Brown void turbostat_loop() 829103a8feaSLen Brown { 830103a8feaSLen Brown restart: 831a829eb4dSLen Brown get_counters(cnt_even); 832103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 833103a8feaSLen Brown 834103a8feaSLen Brown while (1) { 835*15aaa346SLen Brown if (verify_num_cpus()) { 836103a8feaSLen Brown re_initialize(); 837103a8feaSLen Brown goto restart; 838103a8feaSLen Brown } 839103a8feaSLen Brown sleep(interval_sec); 840*15aaa346SLen Brown if (get_counters(cnt_odd)) { 841*15aaa346SLen Brown re_initialize(); 842*15aaa346SLen Brown goto restart; 843*15aaa346SLen Brown } 844103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 845a829eb4dSLen Brown compute_delta(cnt_odd, cnt_even, cnt_delta); 846103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 847a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 848a829eb4dSLen Brown print_counters(cnt_delta); 849*15aaa346SLen Brown sleep(interval_sec); 850*15aaa346SLen Brown if (get_counters(cnt_even)) { 851103a8feaSLen Brown re_initialize(); 852103a8feaSLen Brown goto restart; 853103a8feaSLen Brown } 854103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 855a829eb4dSLen Brown compute_delta(cnt_even, cnt_odd, cnt_delta); 856103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 857a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 858a829eb4dSLen Brown print_counters(cnt_delta); 859103a8feaSLen Brown } 860103a8feaSLen Brown } 861103a8feaSLen Brown 862103a8feaSLen Brown void check_dev_msr() 863103a8feaSLen Brown { 864103a8feaSLen Brown struct stat sb; 865103a8feaSLen Brown 866103a8feaSLen Brown if (stat("/dev/cpu/0/msr", &sb)) { 867103a8feaSLen Brown fprintf(stderr, "no /dev/cpu/0/msr\n"); 868103a8feaSLen Brown fprintf(stderr, "Try \"# modprobe msr\"\n"); 869103a8feaSLen Brown exit(-5); 870103a8feaSLen Brown } 871103a8feaSLen Brown } 872103a8feaSLen Brown 873103a8feaSLen Brown void check_super_user() 874103a8feaSLen Brown { 875103a8feaSLen Brown if (getuid() != 0) { 876103a8feaSLen Brown fprintf(stderr, "must be root\n"); 877103a8feaSLen Brown exit(-6); 878103a8feaSLen Brown } 879103a8feaSLen Brown } 880103a8feaSLen Brown 881103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 882103a8feaSLen Brown { 883103a8feaSLen Brown if (!genuine_intel) 884103a8feaSLen Brown return 0; 885103a8feaSLen Brown 886103a8feaSLen Brown if (family != 6) 887103a8feaSLen Brown return 0; 888103a8feaSLen Brown 889103a8feaSLen Brown switch (model) { 890103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 891103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 892103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 893103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 894103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 895103a8feaSLen Brown case 0x2A: /* SNB */ 896103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 897553575f1SLen Brown case 0x3A: /* IVB */ 898553575f1SLen Brown case 0x3D: /* IVB Xeon */ 899103a8feaSLen Brown return 1; 900103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 901103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 902103a8feaSLen Brown default: 903103a8feaSLen Brown return 0; 904103a8feaSLen Brown } 905103a8feaSLen Brown } 906103a8feaSLen Brown 907103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 908103a8feaSLen Brown { 909103a8feaSLen Brown if (!genuine_intel) 910103a8feaSLen Brown return 0; 911103a8feaSLen Brown 912103a8feaSLen Brown switch (model) { 913103a8feaSLen Brown case 0x2A: 914103a8feaSLen Brown case 0x2D: 915103a8feaSLen Brown return 1; 916103a8feaSLen Brown } 917103a8feaSLen Brown return 0; 918103a8feaSLen Brown } 919103a8feaSLen Brown 920103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 921103a8feaSLen Brown { 922103a8feaSLen Brown if (is_snb(family, model)) 923103a8feaSLen Brown return 100.00; 924103a8feaSLen Brown else 925103a8feaSLen Brown return 133.33; 926103a8feaSLen Brown } 927103a8feaSLen Brown 928103a8feaSLen Brown void check_cpuid() 929103a8feaSLen Brown { 930103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 931103a8feaSLen Brown unsigned int fms, family, model, stepping; 932103a8feaSLen Brown 933103a8feaSLen Brown eax = ebx = ecx = edx = 0; 934103a8feaSLen Brown 935103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); 936103a8feaSLen Brown 937103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 938103a8feaSLen Brown genuine_intel = 1; 939103a8feaSLen Brown 940103a8feaSLen Brown if (verbose) 941103a8feaSLen Brown fprintf(stderr, "%.4s%.4s%.4s ", 942103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 943103a8feaSLen Brown 944103a8feaSLen Brown asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); 945103a8feaSLen Brown family = (fms >> 8) & 0xf; 946103a8feaSLen Brown model = (fms >> 4) & 0xf; 947103a8feaSLen Brown stepping = fms & 0xf; 948103a8feaSLen Brown if (family == 6 || family == 0xf) 949103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 950103a8feaSLen Brown 951103a8feaSLen Brown if (verbose) 952103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 953103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 954103a8feaSLen Brown 955103a8feaSLen Brown if (!(edx & (1 << 5))) { 956103a8feaSLen Brown fprintf(stderr, "CPUID: no MSR\n"); 957103a8feaSLen Brown exit(1); 958103a8feaSLen Brown } 959103a8feaSLen Brown 960103a8feaSLen Brown /* 961103a8feaSLen Brown * check max extended function levels of CPUID. 962103a8feaSLen Brown * This is needed to check for invariant TSC. 963103a8feaSLen Brown * This check is valid for both Intel and AMD. 964103a8feaSLen Brown */ 965103a8feaSLen Brown ebx = ecx = edx = 0; 966103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); 967103a8feaSLen Brown 968103a8feaSLen Brown if (max_level < 0x80000007) { 969103a8feaSLen Brown fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); 970103a8feaSLen Brown exit(1); 971103a8feaSLen Brown } 972103a8feaSLen Brown 973103a8feaSLen Brown /* 974103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 975103a8feaSLen Brown * this check is valid for both Intel and AMD 976103a8feaSLen Brown */ 977103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 9788209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 979103a8feaSLen Brown 980103a8feaSLen Brown if (!has_invariant_tsc) { 981103a8feaSLen Brown fprintf(stderr, "No invariant TSC\n"); 982103a8feaSLen Brown exit(1); 983103a8feaSLen Brown } 984103a8feaSLen Brown 985103a8feaSLen Brown /* 986103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 987103a8feaSLen Brown * this check is valid for both Intel and AMD 988103a8feaSLen Brown */ 989103a8feaSLen Brown 990103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 9918209e054SThomas Renninger has_aperf = ecx & (1 << 0); 992103a8feaSLen Brown if (!has_aperf) { 993103a8feaSLen Brown fprintf(stderr, "No APERF MSR\n"); 994103a8feaSLen Brown exit(1); 995103a8feaSLen Brown } 996103a8feaSLen Brown 997103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 998103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 999103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 1000103a8feaSLen Brown bclk = discover_bclk(family, model); 1001103a8feaSLen Brown 1002103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 1003103a8feaSLen Brown } 1004103a8feaSLen Brown 1005103a8feaSLen Brown 1006103a8feaSLen Brown void usage() 1007103a8feaSLen Brown { 1008103a8feaSLen Brown fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n", 1009103a8feaSLen Brown progname); 1010103a8feaSLen Brown exit(1); 1011103a8feaSLen Brown } 1012103a8feaSLen Brown 1013103a8feaSLen Brown 1014103a8feaSLen Brown /* 1015103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 1016103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 1017103a8feaSLen Brown */ 1018103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 1019103a8feaSLen Brown { 1020103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 1021103a8feaSLen Brown return 1; 1022103a8feaSLen Brown else 1023103a8feaSLen Brown return 0; 1024103a8feaSLen Brown } 1025103a8feaSLen Brown 1026103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 1027103a8feaSLen Brown { 1028103a8feaSLen Brown return 0; 1029103a8feaSLen Brown } 1030103a8feaSLen Brown 1031103a8feaSLen Brown void turbostat_init() 1032103a8feaSLen Brown { 1033103a8feaSLen Brown check_cpuid(); 1034103a8feaSLen Brown 1035103a8feaSLen Brown check_dev_msr(); 1036103a8feaSLen Brown check_super_user(); 1037103a8feaSLen Brown 1038a829eb4dSLen Brown num_cpus = for_all_cpus(alloc_new_counters); 103988c3281fSLen Brown cpu_mask_init(num_cpus); 1040103a8feaSLen Brown 1041103a8feaSLen Brown if (verbose) 1042103a8feaSLen Brown print_nehalem_info(); 1043103a8feaSLen Brown } 1044103a8feaSLen Brown 1045103a8feaSLen Brown int fork_it(char **argv) 1046103a8feaSLen Brown { 1047103a8feaSLen Brown int retval; 1048103a8feaSLen Brown pid_t child_pid; 1049a829eb4dSLen Brown get_counters(cnt_even); 1050103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1051103a8feaSLen Brown 1052103a8feaSLen Brown child_pid = fork(); 1053103a8feaSLen Brown if (!child_pid) { 1054103a8feaSLen Brown /* child */ 1055103a8feaSLen Brown execvp(argv[0], argv); 1056103a8feaSLen Brown } else { 1057103a8feaSLen Brown int status; 1058103a8feaSLen Brown 1059103a8feaSLen Brown /* parent */ 1060103a8feaSLen Brown if (child_pid == -1) { 1061103a8feaSLen Brown perror("fork"); 1062103a8feaSLen Brown exit(1); 1063103a8feaSLen Brown } 1064103a8feaSLen Brown 1065103a8feaSLen Brown signal(SIGINT, SIG_IGN); 1066103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 1067103a8feaSLen Brown if (waitpid(child_pid, &status, 0) == -1) { 1068103a8feaSLen Brown perror("wait"); 1069103a8feaSLen Brown exit(1); 1070103a8feaSLen Brown } 1071103a8feaSLen Brown } 1072a829eb4dSLen Brown get_counters(cnt_odd); 1073103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1074a829eb4dSLen Brown retval = compute_delta(cnt_odd, cnt_even, cnt_delta); 1075103a8feaSLen Brown 1076103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1077a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 1078103a8feaSLen Brown if (!retval) 1079a829eb4dSLen Brown print_counters(cnt_delta); 1080103a8feaSLen Brown 10816eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 1082103a8feaSLen Brown 1083103a8feaSLen Brown return 0; 1084103a8feaSLen Brown } 1085103a8feaSLen Brown 1086103a8feaSLen Brown void cmdline(int argc, char **argv) 1087103a8feaSLen Brown { 1088103a8feaSLen Brown int opt; 1089103a8feaSLen Brown 1090103a8feaSLen Brown progname = argv[0]; 1091103a8feaSLen Brown 1092e23da037SLen Brown while ((opt = getopt(argc, argv, "+svi:M:")) != -1) { 1093103a8feaSLen Brown switch (opt) { 1094e23da037SLen Brown case 's': 1095e23da037SLen Brown summary_only++; 1096e23da037SLen Brown break; 1097103a8feaSLen Brown case 'v': 1098103a8feaSLen Brown verbose++; 1099103a8feaSLen Brown break; 1100103a8feaSLen Brown case 'i': 1101103a8feaSLen Brown interval_sec = atoi(optarg); 1102103a8feaSLen Brown break; 1103103a8feaSLen Brown case 'M': 1104103a8feaSLen Brown sscanf(optarg, "%x", &extra_msr_offset); 1105103a8feaSLen Brown if (verbose > 1) 1106103a8feaSLen Brown fprintf(stderr, "MSR 0x%X\n", extra_msr_offset); 1107103a8feaSLen Brown break; 1108103a8feaSLen Brown default: 1109103a8feaSLen Brown usage(); 1110103a8feaSLen Brown } 1111103a8feaSLen Brown } 1112103a8feaSLen Brown } 1113103a8feaSLen Brown 1114103a8feaSLen Brown int main(int argc, char **argv) 1115103a8feaSLen Brown { 1116103a8feaSLen Brown cmdline(argc, argv); 1117103a8feaSLen Brown 1118103a8feaSLen Brown if (verbose > 1) 1119103a8feaSLen Brown fprintf(stderr, "turbostat Dec 6, 2010" 1120103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 1121103a8feaSLen Brown if (verbose > 1) 1122103a8feaSLen Brown fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n"); 1123103a8feaSLen Brown 1124103a8feaSLen Brown turbostat_init(); 1125103a8feaSLen Brown 1126103a8feaSLen Brown /* 1127103a8feaSLen Brown * if any params left, it must be a command to fork 1128103a8feaSLen Brown */ 1129103a8feaSLen Brown if (argc - optind) 1130103a8feaSLen Brown return fork_it(argv + optind); 1131103a8feaSLen Brown else 1132103a8feaSLen Brown turbostat_loop(); 1133103a8feaSLen Brown 1134103a8feaSLen Brown return 0; 1135103a8feaSLen Brown } 1136