1103a8feaSLen Brown /* 2103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 3103a8feaSLen Brown * on modern Intel turbo-capable processors. 4103a8feaSLen Brown * 5103a8feaSLen Brown * Copyright (c) 2010, 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 22103a8feaSLen Brown #include <stdio.h> 23103a8feaSLen Brown #include <unistd.h> 24103a8feaSLen Brown #include <sys/types.h> 25103a8feaSLen Brown #include <sys/wait.h> 26103a8feaSLen Brown #include <sys/stat.h> 27103a8feaSLen Brown #include <sys/resource.h> 28103a8feaSLen Brown #include <fcntl.h> 29103a8feaSLen Brown #include <signal.h> 30103a8feaSLen Brown #include <sys/time.h> 31103a8feaSLen Brown #include <stdlib.h> 32103a8feaSLen Brown #include <dirent.h> 33103a8feaSLen Brown #include <string.h> 34103a8feaSLen Brown #include <ctype.h> 35103a8feaSLen Brown 36103a8feaSLen Brown #define MSR_TSC 0x10 37103a8feaSLen Brown #define MSR_NEHALEM_PLATFORM_INFO 0xCE 38103a8feaSLen Brown #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD 39103a8feaSLen Brown #define MSR_APERF 0xE8 40103a8feaSLen Brown #define MSR_MPERF 0xE7 41103a8feaSLen Brown #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */ 42103a8feaSLen Brown #define MSR_PKG_C3_RESIDENCY 0x3F8 43103a8feaSLen Brown #define MSR_PKG_C6_RESIDENCY 0x3F9 44103a8feaSLen Brown #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */ 45103a8feaSLen Brown #define MSR_CORE_C3_RESIDENCY 0x3FC 46103a8feaSLen Brown #define MSR_CORE_C6_RESIDENCY 0x3FD 47103a8feaSLen Brown #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */ 48103a8feaSLen Brown 49103a8feaSLen Brown char *proc_stat = "/proc/stat"; 50103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 51103a8feaSLen Brown unsigned int verbose; /* set with -v */ 52103a8feaSLen Brown unsigned int skip_c0; 53103a8feaSLen Brown unsigned int skip_c1; 54103a8feaSLen Brown unsigned int do_nhm_cstates; 55103a8feaSLen Brown unsigned int do_snb_cstates; 56103a8feaSLen Brown unsigned int has_aperf; 57103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 58103a8feaSLen Brown unsigned int genuine_intel; 59103a8feaSLen Brown unsigned int has_invariant_tsc; 60103a8feaSLen Brown unsigned int do_nehalem_platform_info; 61103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 62103a8feaSLen Brown unsigned int extra_msr_offset; 63103a8feaSLen Brown double bclk; 64103a8feaSLen Brown unsigned int show_pkg; 65103a8feaSLen Brown unsigned int show_core; 66103a8feaSLen Brown unsigned int show_cpu; 67103a8feaSLen Brown 68103a8feaSLen Brown int aperf_mperf_unstable; 69103a8feaSLen Brown int backwards_count; 70103a8feaSLen Brown char *progname; 71103a8feaSLen Brown int need_reinitialize; 72103a8feaSLen Brown 73103a8feaSLen Brown int num_cpus; 74103a8feaSLen Brown 75*a829eb4dSLen Brown struct counters { 76103a8feaSLen Brown unsigned long long tsc; /* per thread */ 77103a8feaSLen Brown unsigned long long aperf; /* per thread */ 78103a8feaSLen Brown unsigned long long mperf; /* per thread */ 79103a8feaSLen Brown unsigned long long c1; /* per thread (calculated) */ 80103a8feaSLen Brown unsigned long long c3; /* per core */ 81103a8feaSLen Brown unsigned long long c6; /* per core */ 82103a8feaSLen Brown unsigned long long c7; /* per core */ 83103a8feaSLen Brown unsigned long long pc2; /* per package */ 84103a8feaSLen Brown unsigned long long pc3; /* per package */ 85103a8feaSLen Brown unsigned long long pc6; /* per package */ 86103a8feaSLen Brown unsigned long long pc7; /* per package */ 87103a8feaSLen Brown unsigned long long extra_msr; /* per thread */ 88103a8feaSLen Brown int pkg; 89103a8feaSLen Brown int core; 90103a8feaSLen Brown int cpu; 91*a829eb4dSLen Brown struct counters *next; 92*a829eb4dSLen Brown }; 93103a8feaSLen Brown 94*a829eb4dSLen Brown struct counters *cnt_even; 95*a829eb4dSLen Brown struct counters *cnt_odd; 96*a829eb4dSLen Brown struct counters *cnt_delta; 97*a829eb4dSLen Brown struct counters *cnt_average; 98103a8feaSLen Brown struct timeval tv_even; 99103a8feaSLen Brown struct timeval tv_odd; 100103a8feaSLen Brown struct timeval tv_delta; 101103a8feaSLen Brown 102103a8feaSLen Brown unsigned long long get_msr(int cpu, off_t offset) 103103a8feaSLen Brown { 104103a8feaSLen Brown ssize_t retval; 105103a8feaSLen Brown unsigned long long msr; 106103a8feaSLen Brown char pathname[32]; 107103a8feaSLen Brown int fd; 108103a8feaSLen Brown 109103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 110103a8feaSLen Brown fd = open(pathname, O_RDONLY); 111103a8feaSLen Brown if (fd < 0) { 112103a8feaSLen Brown perror(pathname); 113103a8feaSLen Brown need_reinitialize = 1; 114103a8feaSLen Brown return 0; 115103a8feaSLen Brown } 116103a8feaSLen Brown 117103a8feaSLen Brown retval = pread(fd, &msr, sizeof msr, offset); 118103a8feaSLen Brown if (retval != sizeof msr) { 119103a8feaSLen Brown fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n", 120103a8feaSLen Brown cpu, offset, retval); 121103a8feaSLen Brown exit(-2); 122103a8feaSLen Brown } 123103a8feaSLen Brown 124103a8feaSLen Brown close(fd); 125103a8feaSLen Brown return msr; 126103a8feaSLen Brown } 127103a8feaSLen Brown 128*a829eb4dSLen Brown void print_header(void) 129103a8feaSLen Brown { 130103a8feaSLen Brown if (show_pkg) 131103a8feaSLen Brown fprintf(stderr, "pkg "); 132103a8feaSLen Brown if (show_core) 133103a8feaSLen Brown fprintf(stderr, "core"); 134103a8feaSLen Brown if (show_cpu) 135103a8feaSLen Brown fprintf(stderr, " CPU"); 136103a8feaSLen Brown if (do_nhm_cstates) 137103a8feaSLen Brown fprintf(stderr, " %%c0 "); 138103a8feaSLen Brown if (has_aperf) 139103a8feaSLen Brown fprintf(stderr, " GHz"); 140103a8feaSLen Brown fprintf(stderr, " TSC"); 141103a8feaSLen Brown if (do_nhm_cstates) 142103a8feaSLen Brown fprintf(stderr, " %%c1 "); 143103a8feaSLen Brown if (do_nhm_cstates) 144103a8feaSLen Brown fprintf(stderr, " %%c3 "); 145103a8feaSLen Brown if (do_nhm_cstates) 146103a8feaSLen Brown fprintf(stderr, " %%c6 "); 147103a8feaSLen Brown if (do_snb_cstates) 148103a8feaSLen Brown fprintf(stderr, " %%c7 "); 149103a8feaSLen Brown if (do_snb_cstates) 150103a8feaSLen Brown fprintf(stderr, " %%pc2 "); 151103a8feaSLen Brown if (do_nhm_cstates) 152103a8feaSLen Brown fprintf(stderr, " %%pc3 "); 153103a8feaSLen Brown if (do_nhm_cstates) 154103a8feaSLen Brown fprintf(stderr, " %%pc6 "); 155103a8feaSLen Brown if (do_snb_cstates) 156103a8feaSLen Brown fprintf(stderr, " %%pc7 "); 157103a8feaSLen Brown if (extra_msr_offset) 158103a8feaSLen Brown fprintf(stderr, " MSR 0x%x ", extra_msr_offset); 159103a8feaSLen Brown 160103a8feaSLen Brown putc('\n', stderr); 161103a8feaSLen Brown } 162103a8feaSLen Brown 163*a829eb4dSLen Brown void dump_cnt(struct counters *cnt) 164103a8feaSLen Brown { 165*a829eb4dSLen Brown fprintf(stderr, "package: %d ", cnt->pkg); 166*a829eb4dSLen Brown fprintf(stderr, "core:: %d ", cnt->core); 167*a829eb4dSLen Brown fprintf(stderr, "CPU: %d ", cnt->cpu); 168*a829eb4dSLen Brown fprintf(stderr, "TSC: %016llX\n", cnt->tsc); 169*a829eb4dSLen Brown fprintf(stderr, "c3: %016llX\n", cnt->c3); 170*a829eb4dSLen Brown fprintf(stderr, "c6: %016llX\n", cnt->c6); 171*a829eb4dSLen Brown fprintf(stderr, "c7: %016llX\n", cnt->c7); 172*a829eb4dSLen Brown fprintf(stderr, "aperf: %016llX\n", cnt->aperf); 173*a829eb4dSLen Brown fprintf(stderr, "pc2: %016llX\n", cnt->pc2); 174*a829eb4dSLen Brown fprintf(stderr, "pc3: %016llX\n", cnt->pc3); 175*a829eb4dSLen Brown fprintf(stderr, "pc6: %016llX\n", cnt->pc6); 176*a829eb4dSLen Brown fprintf(stderr, "pc7: %016llX\n", cnt->pc7); 177*a829eb4dSLen Brown fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr); 178103a8feaSLen Brown } 179103a8feaSLen Brown 180*a829eb4dSLen Brown void dump_list(struct counters *cnt) 181103a8feaSLen Brown { 182*a829eb4dSLen Brown printf("dump_list 0x%p\n", cnt); 183103a8feaSLen Brown 184*a829eb4dSLen Brown for (; cnt; cnt = cnt->next) 185*a829eb4dSLen Brown dump_cnt(cnt); 186103a8feaSLen Brown } 187103a8feaSLen Brown 188*a829eb4dSLen Brown void print_cnt(struct counters *p) 189103a8feaSLen Brown { 190103a8feaSLen Brown double interval_float; 191103a8feaSLen Brown 192103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 193103a8feaSLen Brown 194103a8feaSLen Brown /* topology columns, print blanks on 1st (average) line */ 195*a829eb4dSLen Brown if (p == cnt_average) { 196103a8feaSLen Brown if (show_pkg) 197103a8feaSLen Brown fprintf(stderr, " "); 198103a8feaSLen Brown if (show_core) 199103a8feaSLen Brown fprintf(stderr, " "); 200103a8feaSLen Brown if (show_cpu) 201103a8feaSLen Brown fprintf(stderr, " "); 202103a8feaSLen Brown } else { 203103a8feaSLen Brown if (show_pkg) 204103a8feaSLen Brown fprintf(stderr, "%4d", p->pkg); 205103a8feaSLen Brown if (show_core) 206103a8feaSLen Brown fprintf(stderr, "%4d", p->core); 207103a8feaSLen Brown if (show_cpu) 208103a8feaSLen Brown fprintf(stderr, "%4d", p->cpu); 209103a8feaSLen Brown } 210103a8feaSLen Brown 211103a8feaSLen Brown /* %c0 */ 212103a8feaSLen Brown if (do_nhm_cstates) { 213103a8feaSLen Brown if (!skip_c0) 214103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->mperf/p->tsc); 215103a8feaSLen Brown else 216103a8feaSLen Brown fprintf(stderr, " ****"); 217103a8feaSLen Brown } 218103a8feaSLen Brown 219103a8feaSLen Brown /* GHz */ 220103a8feaSLen Brown if (has_aperf) { 221103a8feaSLen Brown if (!aperf_mperf_unstable) { 222103a8feaSLen Brown fprintf(stderr, "%5.2f", 223103a8feaSLen Brown 1.0 * p->tsc / units * p->aperf / 224103a8feaSLen Brown p->mperf / interval_float); 225103a8feaSLen Brown } else { 226103a8feaSLen Brown if (p->aperf > p->tsc || p->mperf > p->tsc) { 227103a8feaSLen Brown fprintf(stderr, " ****"); 228103a8feaSLen Brown } else { 229103a8feaSLen Brown fprintf(stderr, "%4.1f*", 230103a8feaSLen Brown 1.0 * p->tsc / 231103a8feaSLen Brown units * p->aperf / 232103a8feaSLen Brown p->mperf / interval_float); 233103a8feaSLen Brown } 234103a8feaSLen Brown } 235103a8feaSLen Brown } 236103a8feaSLen Brown 237103a8feaSLen Brown /* TSC */ 238103a8feaSLen Brown fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float); 239103a8feaSLen Brown 240103a8feaSLen Brown if (do_nhm_cstates) { 241103a8feaSLen Brown if (!skip_c1) 242103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->c1/p->tsc); 243103a8feaSLen Brown else 244103a8feaSLen Brown fprintf(stderr, " ****"); 245103a8feaSLen Brown } 246103a8feaSLen Brown if (do_nhm_cstates) 247103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->c3/p->tsc); 248103a8feaSLen Brown if (do_nhm_cstates) 249103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->c6/p->tsc); 250103a8feaSLen Brown if (do_snb_cstates) 251103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->c7/p->tsc); 252103a8feaSLen Brown if (do_snb_cstates) 253103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->pc2/p->tsc); 254103a8feaSLen Brown if (do_nhm_cstates) 255103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->pc3/p->tsc); 256103a8feaSLen Brown if (do_nhm_cstates) 257103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->pc6/p->tsc); 258103a8feaSLen Brown if (do_snb_cstates) 259103a8feaSLen Brown fprintf(stderr, "%7.2f", 100.0 * p->pc7/p->tsc); 260103a8feaSLen Brown if (extra_msr_offset) 261103a8feaSLen Brown fprintf(stderr, " 0x%016llx", p->extra_msr); 262103a8feaSLen Brown putc('\n', stderr); 263103a8feaSLen Brown } 264103a8feaSLen Brown 265*a829eb4dSLen Brown void print_counters(struct counters *counters) 266103a8feaSLen Brown { 267*a829eb4dSLen Brown struct counters *cnt; 268103a8feaSLen Brown 269103a8feaSLen Brown print_header(); 270103a8feaSLen Brown 271103a8feaSLen Brown if (num_cpus > 1) 272*a829eb4dSLen Brown print_cnt(cnt_average); 273103a8feaSLen Brown 274*a829eb4dSLen Brown for (cnt = counters; cnt != NULL; cnt = cnt->next) 275*a829eb4dSLen Brown print_cnt(cnt); 276103a8feaSLen Brown 277103a8feaSLen Brown } 278103a8feaSLen Brown 279103a8feaSLen Brown #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after)) 280103a8feaSLen Brown 281*a829eb4dSLen Brown int compute_delta(struct counters *after, 282*a829eb4dSLen Brown struct counters *before, struct counters *delta) 283103a8feaSLen Brown { 284103a8feaSLen Brown int errors = 0; 285103a8feaSLen Brown int perf_err = 0; 286103a8feaSLen Brown 287103a8feaSLen Brown skip_c0 = skip_c1 = 0; 288103a8feaSLen Brown 289103a8feaSLen Brown for ( ; after && before && delta; 290103a8feaSLen Brown after = after->next, before = before->next, delta = delta->next) { 291103a8feaSLen Brown if (before->cpu != after->cpu) { 292103a8feaSLen Brown printf("cpu configuration changed: %d != %d\n", 293103a8feaSLen Brown before->cpu, after->cpu); 294103a8feaSLen Brown return -1; 295103a8feaSLen Brown } 296103a8feaSLen Brown 297103a8feaSLen Brown if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) { 298103a8feaSLen Brown fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n", 299103a8feaSLen Brown before->cpu, before->tsc, after->tsc); 300103a8feaSLen Brown errors++; 301103a8feaSLen Brown } 302103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 303103a8feaSLen Brown if (delta->tsc < (1000 * 1000)) { 304103a8feaSLen Brown fprintf(stderr, "Insanely slow TSC rate," 305103a8feaSLen Brown " TSC stops in idle?\n"); 306103a8feaSLen Brown fprintf(stderr, "You can disable all c-states" 307103a8feaSLen Brown " by booting with \"idle=poll\"\n"); 308103a8feaSLen Brown fprintf(stderr, "or just the deep ones with" 309103a8feaSLen Brown " \"processor.max_cstate=1\"\n"); 310103a8feaSLen Brown exit(-3); 311103a8feaSLen Brown } 312103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) { 313103a8feaSLen Brown fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n", 314103a8feaSLen Brown before->cpu, before->c3, after->c3); 315103a8feaSLen Brown errors++; 316103a8feaSLen Brown } 317103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) { 318103a8feaSLen Brown fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n", 319103a8feaSLen Brown before->cpu, before->c6, after->c6); 320103a8feaSLen Brown errors++; 321103a8feaSLen Brown } 322103a8feaSLen Brown if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) { 323103a8feaSLen Brown fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n", 324103a8feaSLen Brown before->cpu, before->c7, after->c7); 325103a8feaSLen Brown errors++; 326103a8feaSLen Brown } 327103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) { 328103a8feaSLen Brown fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n", 329103a8feaSLen Brown before->cpu, before->pc2, after->pc2); 330103a8feaSLen Brown errors++; 331103a8feaSLen Brown } 332103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) { 333103a8feaSLen Brown fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n", 334103a8feaSLen Brown before->cpu, before->pc3, after->pc3); 335103a8feaSLen Brown errors++; 336103a8feaSLen Brown } 337103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) { 338103a8feaSLen Brown fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n", 339103a8feaSLen Brown before->cpu, before->pc6, after->pc6); 340103a8feaSLen Brown errors++; 341103a8feaSLen Brown } 342103a8feaSLen Brown if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) { 343103a8feaSLen Brown fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n", 344103a8feaSLen Brown before->cpu, before->pc7, after->pc7); 345103a8feaSLen Brown errors++; 346103a8feaSLen Brown } 347103a8feaSLen Brown 348103a8feaSLen Brown perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf); 349103a8feaSLen Brown if (perf_err) { 350103a8feaSLen Brown fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n", 351103a8feaSLen Brown before->cpu, before->aperf, after->aperf); 352103a8feaSLen Brown } 353103a8feaSLen Brown perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf); 354103a8feaSLen Brown if (perf_err) { 355103a8feaSLen Brown fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n", 356103a8feaSLen Brown before->cpu, before->mperf, after->mperf); 357103a8feaSLen Brown } 358103a8feaSLen Brown if (perf_err) { 359103a8feaSLen Brown if (!aperf_mperf_unstable) { 360103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 361103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 362103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 363103a8feaSLen Brown 364103a8feaSLen Brown aperf_mperf_unstable = 1; 365103a8feaSLen Brown } 366103a8feaSLen Brown /* 367103a8feaSLen Brown * mperf delta is likely a huge "positive" number 368103a8feaSLen Brown * can not use it for calculating c0 time 369103a8feaSLen Brown */ 370103a8feaSLen Brown skip_c0 = 1; 371103a8feaSLen Brown skip_c1 = 1; 372103a8feaSLen Brown } 373103a8feaSLen Brown 374103a8feaSLen Brown /* 375103a8feaSLen Brown * As mperf and tsc collection are not atomic, 376103a8feaSLen Brown * it is possible for mperf's non-halted cycles 377103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 378103a8feaSLen Brown */ 379103a8feaSLen Brown if (delta->mperf > delta->tsc) 380103a8feaSLen Brown delta->c1 = 0; 381103a8feaSLen Brown else /* normal case, derive c1 */ 382103a8feaSLen Brown delta->c1 = delta->tsc - delta->mperf 383103a8feaSLen Brown - delta->c3 - delta->c6 - delta->c7; 384103a8feaSLen Brown 385103a8feaSLen Brown if (delta->mperf == 0) 386103a8feaSLen Brown delta->mperf = 1; /* divide by 0 protection */ 387103a8feaSLen Brown 388103a8feaSLen Brown /* 389103a8feaSLen Brown * for "extra msr", just copy the latest w/o subtracting 390103a8feaSLen Brown */ 391103a8feaSLen Brown delta->extra_msr = after->extra_msr; 392103a8feaSLen Brown if (errors) { 393103a8feaSLen Brown fprintf(stderr, "ERROR cpu%d before:\n", before->cpu); 394*a829eb4dSLen Brown dump_cnt(before); 395103a8feaSLen Brown fprintf(stderr, "ERROR cpu%d after:\n", before->cpu); 396*a829eb4dSLen Brown dump_cnt(after); 397103a8feaSLen Brown errors = 0; 398103a8feaSLen Brown } 399103a8feaSLen Brown } 400103a8feaSLen Brown return 0; 401103a8feaSLen Brown } 402103a8feaSLen Brown 403*a829eb4dSLen Brown void compute_average(struct counters *delta, struct counters *avg) 404103a8feaSLen Brown { 405*a829eb4dSLen Brown struct counters *sum; 406103a8feaSLen Brown 407*a829eb4dSLen Brown sum = calloc(1, sizeof(struct counters)); 408103a8feaSLen Brown if (sum == NULL) { 409103a8feaSLen Brown perror("calloc sum"); 410103a8feaSLen Brown exit(1); 411103a8feaSLen Brown } 412103a8feaSLen Brown 413103a8feaSLen Brown for (; delta; delta = delta->next) { 414103a8feaSLen Brown sum->tsc += delta->tsc; 415103a8feaSLen Brown sum->c1 += delta->c1; 416103a8feaSLen Brown sum->c3 += delta->c3; 417103a8feaSLen Brown sum->c6 += delta->c6; 418103a8feaSLen Brown sum->c7 += delta->c7; 419103a8feaSLen Brown sum->aperf += delta->aperf; 420103a8feaSLen Brown sum->mperf += delta->mperf; 421103a8feaSLen Brown sum->pc2 += delta->pc2; 422103a8feaSLen Brown sum->pc3 += delta->pc3; 423103a8feaSLen Brown sum->pc6 += delta->pc6; 424103a8feaSLen Brown sum->pc7 += delta->pc7; 425103a8feaSLen Brown } 426103a8feaSLen Brown avg->tsc = sum->tsc/num_cpus; 427103a8feaSLen Brown avg->c1 = sum->c1/num_cpus; 428103a8feaSLen Brown avg->c3 = sum->c3/num_cpus; 429103a8feaSLen Brown avg->c6 = sum->c6/num_cpus; 430103a8feaSLen Brown avg->c7 = sum->c7/num_cpus; 431103a8feaSLen Brown avg->aperf = sum->aperf/num_cpus; 432103a8feaSLen Brown avg->mperf = sum->mperf/num_cpus; 433103a8feaSLen Brown avg->pc2 = sum->pc2/num_cpus; 434103a8feaSLen Brown avg->pc3 = sum->pc3/num_cpus; 435103a8feaSLen Brown avg->pc6 = sum->pc6/num_cpus; 436103a8feaSLen Brown avg->pc7 = sum->pc7/num_cpus; 437103a8feaSLen Brown 438103a8feaSLen Brown free(sum); 439103a8feaSLen Brown } 440103a8feaSLen Brown 441*a829eb4dSLen Brown void get_counters(struct counters *cnt) 442103a8feaSLen Brown { 443*a829eb4dSLen Brown for ( ; cnt; cnt = cnt->next) { 444*a829eb4dSLen Brown cnt->tsc = get_msr(cnt->cpu, MSR_TSC); 445103a8feaSLen Brown if (do_nhm_cstates) 446*a829eb4dSLen Brown cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY); 447103a8feaSLen Brown if (do_nhm_cstates) 448*a829eb4dSLen Brown cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY); 449103a8feaSLen Brown if (do_snb_cstates) 450*a829eb4dSLen Brown cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY); 451103a8feaSLen Brown if (has_aperf) 452*a829eb4dSLen Brown cnt->aperf = get_msr(cnt->cpu, MSR_APERF); 453103a8feaSLen Brown if (has_aperf) 454*a829eb4dSLen Brown cnt->mperf = get_msr(cnt->cpu, MSR_MPERF); 455103a8feaSLen Brown if (do_snb_cstates) 456*a829eb4dSLen Brown cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY); 457103a8feaSLen Brown if (do_nhm_cstates) 458*a829eb4dSLen Brown cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY); 459103a8feaSLen Brown if (do_nhm_cstates) 460*a829eb4dSLen Brown cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY); 461103a8feaSLen Brown if (do_snb_cstates) 462*a829eb4dSLen Brown cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY); 463103a8feaSLen Brown if (extra_msr_offset) 464*a829eb4dSLen Brown cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset); 465103a8feaSLen Brown } 466103a8feaSLen Brown } 467103a8feaSLen Brown 468*a829eb4dSLen Brown void print_nehalem_info(void) 469103a8feaSLen Brown { 470103a8feaSLen Brown unsigned long long msr; 471103a8feaSLen Brown unsigned int ratio; 472103a8feaSLen Brown 473103a8feaSLen Brown if (!do_nehalem_platform_info) 474103a8feaSLen Brown return; 475103a8feaSLen Brown 476103a8feaSLen Brown msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO); 477103a8feaSLen Brown 478103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 479103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 480103a8feaSLen Brown ratio, bclk, ratio * bclk); 481103a8feaSLen Brown 482103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 483103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 484103a8feaSLen Brown ratio, bclk, ratio * bclk); 485103a8feaSLen Brown 486103a8feaSLen Brown if (verbose > 1) 487103a8feaSLen Brown fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr); 488103a8feaSLen Brown 489103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 490103a8feaSLen Brown return; 491103a8feaSLen Brown 492103a8feaSLen Brown msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT); 493103a8feaSLen Brown 494103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 495103a8feaSLen Brown if (ratio) 496103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 497103a8feaSLen Brown ratio, bclk, ratio * bclk); 498103a8feaSLen Brown 499103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 500103a8feaSLen Brown if (ratio) 501103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 502103a8feaSLen Brown ratio, bclk, ratio * bclk); 503103a8feaSLen Brown 504103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 505103a8feaSLen Brown if (ratio) 506103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 507103a8feaSLen Brown ratio, bclk, ratio * bclk); 508103a8feaSLen Brown 509103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 510103a8feaSLen Brown if (ratio) 511103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 512103a8feaSLen Brown ratio, bclk, ratio * bclk); 513103a8feaSLen Brown 514103a8feaSLen Brown } 515103a8feaSLen Brown 516*a829eb4dSLen Brown void free_counter_list(struct counters *list) 517103a8feaSLen Brown { 518*a829eb4dSLen Brown struct counters *p; 519103a8feaSLen Brown 520103a8feaSLen Brown for (p = list; p; ) { 521*a829eb4dSLen Brown struct counters *free_me; 522103a8feaSLen Brown 523103a8feaSLen Brown free_me = p; 524103a8feaSLen Brown p = p->next; 525103a8feaSLen Brown free(free_me); 526103a8feaSLen Brown } 527103a8feaSLen Brown } 528103a8feaSLen Brown 529103a8feaSLen Brown void free_all_counters(void) 530103a8feaSLen Brown { 531*a829eb4dSLen Brown free_counter_list(cnt_even); 532*a829eb4dSLen Brown cnt_even = NULL; 533103a8feaSLen Brown 534*a829eb4dSLen Brown free_counter_list(cnt_odd); 535*a829eb4dSLen Brown cnt_odd = NULL; 536103a8feaSLen Brown 537*a829eb4dSLen Brown free_counter_list(cnt_delta); 538*a829eb4dSLen Brown cnt_delta = NULL; 539103a8feaSLen Brown 540*a829eb4dSLen Brown free_counter_list(cnt_average); 541*a829eb4dSLen Brown cnt_average = NULL; 542103a8feaSLen Brown } 543103a8feaSLen Brown 544*a829eb4dSLen Brown void insert_counters(struct counters **list, 545*a829eb4dSLen Brown struct counters *new) 546103a8feaSLen Brown { 547*a829eb4dSLen Brown struct counters *prev; 548103a8feaSLen Brown 549103a8feaSLen Brown /* 550103a8feaSLen Brown * list was empty 551103a8feaSLen Brown */ 552103a8feaSLen Brown if (*list == NULL) { 553103a8feaSLen Brown new->next = *list; 554103a8feaSLen Brown *list = new; 555103a8feaSLen Brown return; 556103a8feaSLen Brown } 557103a8feaSLen Brown 558103a8feaSLen Brown show_cpu = 1; /* there is more than one CPU */ 559103a8feaSLen Brown 560103a8feaSLen Brown /* 561103a8feaSLen Brown * insert on front of list. 562103a8feaSLen Brown * It is sorted by ascending package#, core#, cpu# 563103a8feaSLen Brown */ 564103a8feaSLen Brown if (((*list)->pkg > new->pkg) || 565103a8feaSLen Brown (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) || 566103a8feaSLen Brown (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) { 567103a8feaSLen Brown new->next = *list; 568103a8feaSLen Brown *list = new; 569103a8feaSLen Brown return; 570103a8feaSLen Brown } 571103a8feaSLen Brown 572103a8feaSLen Brown prev = *list; 573103a8feaSLen Brown 574103a8feaSLen Brown while (prev->next && (prev->next->pkg < new->pkg)) { 575103a8feaSLen Brown prev = prev->next; 576103a8feaSLen Brown show_pkg = 1; /* there is more than 1 package */ 577103a8feaSLen Brown } 578103a8feaSLen Brown 579103a8feaSLen Brown while (prev->next && (prev->next->pkg == new->pkg) 580103a8feaSLen Brown && (prev->next->core < new->core)) { 581103a8feaSLen Brown prev = prev->next; 582103a8feaSLen Brown show_core = 1; /* there is more than 1 core */ 583103a8feaSLen Brown } 584103a8feaSLen Brown 585103a8feaSLen Brown while (prev->next && (prev->next->pkg == new->pkg) 586103a8feaSLen Brown && (prev->next->core == new->core) 587103a8feaSLen Brown && (prev->next->cpu < new->cpu)) { 588103a8feaSLen Brown prev = prev->next; 589103a8feaSLen Brown } 590103a8feaSLen Brown 591103a8feaSLen Brown /* 592103a8feaSLen Brown * insert after "prev" 593103a8feaSLen Brown */ 594103a8feaSLen Brown new->next = prev->next; 595103a8feaSLen Brown prev->next = new; 596103a8feaSLen Brown } 597103a8feaSLen Brown 598*a829eb4dSLen Brown void alloc_new_counters(int pkg, int core, int cpu) 599103a8feaSLen Brown { 600*a829eb4dSLen Brown struct counters *new; 601103a8feaSLen Brown 602103a8feaSLen Brown if (verbose > 1) 603103a8feaSLen Brown printf("pkg%d core%d, cpu%d\n", pkg, core, cpu); 604103a8feaSLen Brown 605*a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 606103a8feaSLen Brown if (new == NULL) { 607103a8feaSLen Brown perror("calloc"); 608103a8feaSLen Brown exit(1); 609103a8feaSLen Brown } 610103a8feaSLen Brown new->pkg = pkg; 611103a8feaSLen Brown new->core = core; 612103a8feaSLen Brown new->cpu = cpu; 613*a829eb4dSLen Brown insert_counters(&cnt_odd, new); 614103a8feaSLen Brown 615*a829eb4dSLen Brown new = (struct counters *)calloc(1, 616*a829eb4dSLen Brown sizeof(struct counters)); 617103a8feaSLen Brown if (new == NULL) { 618103a8feaSLen Brown perror("calloc"); 619103a8feaSLen Brown exit(1); 620103a8feaSLen Brown } 621103a8feaSLen Brown new->pkg = pkg; 622103a8feaSLen Brown new->core = core; 623103a8feaSLen Brown new->cpu = cpu; 624*a829eb4dSLen Brown insert_counters(&cnt_even, new); 625103a8feaSLen Brown 626*a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 627103a8feaSLen Brown if (new == NULL) { 628103a8feaSLen Brown perror("calloc"); 629103a8feaSLen Brown exit(1); 630103a8feaSLen Brown } 631103a8feaSLen Brown new->pkg = pkg; 632103a8feaSLen Brown new->core = core; 633103a8feaSLen Brown new->cpu = cpu; 634*a829eb4dSLen Brown insert_counters(&cnt_delta, new); 635103a8feaSLen Brown 636*a829eb4dSLen Brown new = (struct counters *)calloc(1, sizeof(struct counters)); 637103a8feaSLen Brown if (new == NULL) { 638103a8feaSLen Brown perror("calloc"); 639103a8feaSLen Brown exit(1); 640103a8feaSLen Brown } 641103a8feaSLen Brown new->pkg = pkg; 642103a8feaSLen Brown new->core = core; 643103a8feaSLen Brown new->cpu = cpu; 644*a829eb4dSLen Brown cnt_average = new; 645103a8feaSLen Brown } 646103a8feaSLen Brown 647103a8feaSLen Brown int get_physical_package_id(int cpu) 648103a8feaSLen Brown { 649103a8feaSLen Brown char path[64]; 650103a8feaSLen Brown FILE *filep; 651103a8feaSLen Brown int pkg; 652103a8feaSLen Brown 653103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 654103a8feaSLen Brown filep = fopen(path, "r"); 655103a8feaSLen Brown if (filep == NULL) { 656103a8feaSLen Brown perror(path); 657103a8feaSLen Brown exit(1); 658103a8feaSLen Brown } 659103a8feaSLen Brown fscanf(filep, "%d", &pkg); 660103a8feaSLen Brown fclose(filep); 661103a8feaSLen Brown return pkg; 662103a8feaSLen Brown } 663103a8feaSLen Brown 664103a8feaSLen Brown int get_core_id(int cpu) 665103a8feaSLen Brown { 666103a8feaSLen Brown char path[64]; 667103a8feaSLen Brown FILE *filep; 668103a8feaSLen Brown int core; 669103a8feaSLen Brown 670103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 671103a8feaSLen Brown filep = fopen(path, "r"); 672103a8feaSLen Brown if (filep == NULL) { 673103a8feaSLen Brown perror(path); 674103a8feaSLen Brown exit(1); 675103a8feaSLen Brown } 676103a8feaSLen Brown fscanf(filep, "%d", &core); 677103a8feaSLen Brown fclose(filep); 678103a8feaSLen Brown return core; 679103a8feaSLen Brown } 680103a8feaSLen Brown 681103a8feaSLen Brown /* 682103a8feaSLen Brown * run func(index, cpu) on every cpu in /proc/stat 683103a8feaSLen Brown */ 684103a8feaSLen Brown 685103a8feaSLen Brown int for_all_cpus(void (func)(int, int, int)) 686103a8feaSLen Brown { 687103a8feaSLen Brown FILE *fp; 688103a8feaSLen Brown int cpu_count; 689103a8feaSLen Brown int retval; 690103a8feaSLen Brown 691103a8feaSLen Brown fp = fopen(proc_stat, "r"); 692103a8feaSLen Brown if (fp == NULL) { 693103a8feaSLen Brown perror(proc_stat); 694103a8feaSLen Brown exit(1); 695103a8feaSLen Brown } 696103a8feaSLen Brown 697103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 698103a8feaSLen Brown if (retval != 0) { 699103a8feaSLen Brown perror("/proc/stat format"); 700103a8feaSLen Brown exit(1); 701103a8feaSLen Brown } 702103a8feaSLen Brown 703103a8feaSLen Brown for (cpu_count = 0; ; cpu_count++) { 704103a8feaSLen Brown int cpu; 705103a8feaSLen Brown 706103a8feaSLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu); 707103a8feaSLen Brown if (retval != 1) 708103a8feaSLen Brown break; 709103a8feaSLen Brown 710103a8feaSLen Brown func(get_physical_package_id(cpu), get_core_id(cpu), cpu); 711103a8feaSLen Brown } 712103a8feaSLen Brown fclose(fp); 713103a8feaSLen Brown return cpu_count; 714103a8feaSLen Brown } 715103a8feaSLen Brown 716103a8feaSLen Brown void re_initialize(void) 717103a8feaSLen Brown { 718103a8feaSLen Brown printf("turbostat: topology changed, re-initializing.\n"); 719103a8feaSLen Brown free_all_counters(); 720*a829eb4dSLen Brown num_cpus = for_all_cpus(alloc_new_counters); 721103a8feaSLen Brown need_reinitialize = 0; 722103a8feaSLen Brown printf("num_cpus is now %d\n", num_cpus); 723103a8feaSLen Brown } 724103a8feaSLen Brown 725103a8feaSLen Brown void dummy(int pkg, int core, int cpu) { return; } 726103a8feaSLen Brown /* 727103a8feaSLen Brown * check to see if a cpu came on-line 728103a8feaSLen Brown */ 729*a829eb4dSLen Brown void verify_num_cpus(void) 730103a8feaSLen Brown { 731103a8feaSLen Brown int new_num_cpus; 732103a8feaSLen Brown 733103a8feaSLen Brown new_num_cpus = for_all_cpus(dummy); 734103a8feaSLen Brown 735103a8feaSLen Brown if (new_num_cpus != num_cpus) { 736103a8feaSLen Brown if (verbose) 737103a8feaSLen Brown printf("num_cpus was %d, is now %d\n", 738103a8feaSLen Brown num_cpus, new_num_cpus); 739103a8feaSLen Brown need_reinitialize = 1; 740103a8feaSLen Brown } 741103a8feaSLen Brown } 742103a8feaSLen Brown 743103a8feaSLen Brown void turbostat_loop() 744103a8feaSLen Brown { 745103a8feaSLen Brown restart: 746*a829eb4dSLen Brown get_counters(cnt_even); 747103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 748103a8feaSLen Brown 749103a8feaSLen Brown while (1) { 750103a8feaSLen Brown verify_num_cpus(); 751103a8feaSLen Brown if (need_reinitialize) { 752103a8feaSLen Brown re_initialize(); 753103a8feaSLen Brown goto restart; 754103a8feaSLen Brown } 755103a8feaSLen Brown sleep(interval_sec); 756*a829eb4dSLen Brown get_counters(cnt_odd); 757103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 758103a8feaSLen Brown 759*a829eb4dSLen Brown compute_delta(cnt_odd, cnt_even, cnt_delta); 760103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 761*a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 762*a829eb4dSLen Brown print_counters(cnt_delta); 763103a8feaSLen Brown if (need_reinitialize) { 764103a8feaSLen Brown re_initialize(); 765103a8feaSLen Brown goto restart; 766103a8feaSLen Brown } 767103a8feaSLen Brown sleep(interval_sec); 768*a829eb4dSLen Brown get_counters(cnt_even); 769103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 770*a829eb4dSLen Brown compute_delta(cnt_even, cnt_odd, cnt_delta); 771103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 772*a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 773*a829eb4dSLen Brown print_counters(cnt_delta); 774103a8feaSLen Brown } 775103a8feaSLen Brown } 776103a8feaSLen Brown 777103a8feaSLen Brown void check_dev_msr() 778103a8feaSLen Brown { 779103a8feaSLen Brown struct stat sb; 780103a8feaSLen Brown 781103a8feaSLen Brown if (stat("/dev/cpu/0/msr", &sb)) { 782103a8feaSLen Brown fprintf(stderr, "no /dev/cpu/0/msr\n"); 783103a8feaSLen Brown fprintf(stderr, "Try \"# modprobe msr\"\n"); 784103a8feaSLen Brown exit(-5); 785103a8feaSLen Brown } 786103a8feaSLen Brown } 787103a8feaSLen Brown 788103a8feaSLen Brown void check_super_user() 789103a8feaSLen Brown { 790103a8feaSLen Brown if (getuid() != 0) { 791103a8feaSLen Brown fprintf(stderr, "must be root\n"); 792103a8feaSLen Brown exit(-6); 793103a8feaSLen Brown } 794103a8feaSLen Brown } 795103a8feaSLen Brown 796103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 797103a8feaSLen Brown { 798103a8feaSLen Brown if (!genuine_intel) 799103a8feaSLen Brown return 0; 800103a8feaSLen Brown 801103a8feaSLen Brown if (family != 6) 802103a8feaSLen Brown return 0; 803103a8feaSLen Brown 804103a8feaSLen Brown switch (model) { 805103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 806103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 807103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 808103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 809103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 810103a8feaSLen Brown case 0x2A: /* SNB */ 811103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 812103a8feaSLen Brown return 1; 813103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 814103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 815103a8feaSLen Brown default: 816103a8feaSLen Brown return 0; 817103a8feaSLen Brown } 818103a8feaSLen Brown } 819103a8feaSLen Brown 820103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 821103a8feaSLen Brown { 822103a8feaSLen Brown if (!genuine_intel) 823103a8feaSLen Brown return 0; 824103a8feaSLen Brown 825103a8feaSLen Brown switch (model) { 826103a8feaSLen Brown case 0x2A: 827103a8feaSLen Brown case 0x2D: 828103a8feaSLen Brown return 1; 829103a8feaSLen Brown } 830103a8feaSLen Brown return 0; 831103a8feaSLen Brown } 832103a8feaSLen Brown 833103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 834103a8feaSLen Brown { 835103a8feaSLen Brown if (is_snb(family, model)) 836103a8feaSLen Brown return 100.00; 837103a8feaSLen Brown else 838103a8feaSLen Brown return 133.33; 839103a8feaSLen Brown } 840103a8feaSLen Brown 841103a8feaSLen Brown void check_cpuid() 842103a8feaSLen Brown { 843103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 844103a8feaSLen Brown unsigned int fms, family, model, stepping; 845103a8feaSLen Brown 846103a8feaSLen Brown eax = ebx = ecx = edx = 0; 847103a8feaSLen Brown 848103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); 849103a8feaSLen Brown 850103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 851103a8feaSLen Brown genuine_intel = 1; 852103a8feaSLen Brown 853103a8feaSLen Brown if (verbose) 854103a8feaSLen Brown fprintf(stderr, "%.4s%.4s%.4s ", 855103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 856103a8feaSLen Brown 857103a8feaSLen Brown asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); 858103a8feaSLen Brown family = (fms >> 8) & 0xf; 859103a8feaSLen Brown model = (fms >> 4) & 0xf; 860103a8feaSLen Brown stepping = fms & 0xf; 861103a8feaSLen Brown if (family == 6 || family == 0xf) 862103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 863103a8feaSLen Brown 864103a8feaSLen Brown if (verbose) 865103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 866103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 867103a8feaSLen Brown 868103a8feaSLen Brown if (!(edx & (1 << 5))) { 869103a8feaSLen Brown fprintf(stderr, "CPUID: no MSR\n"); 870103a8feaSLen Brown exit(1); 871103a8feaSLen Brown } 872103a8feaSLen Brown 873103a8feaSLen Brown /* 874103a8feaSLen Brown * check max extended function levels of CPUID. 875103a8feaSLen Brown * This is needed to check for invariant TSC. 876103a8feaSLen Brown * This check is valid for both Intel and AMD. 877103a8feaSLen Brown */ 878103a8feaSLen Brown ebx = ecx = edx = 0; 879103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); 880103a8feaSLen Brown 881103a8feaSLen Brown if (max_level < 0x80000007) { 882103a8feaSLen Brown fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); 883103a8feaSLen Brown exit(1); 884103a8feaSLen Brown } 885103a8feaSLen Brown 886103a8feaSLen Brown /* 887103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 888103a8feaSLen Brown * this check is valid for both Intel and AMD 889103a8feaSLen Brown */ 890103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 8918209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 892103a8feaSLen Brown 893103a8feaSLen Brown if (!has_invariant_tsc) { 894103a8feaSLen Brown fprintf(stderr, "No invariant TSC\n"); 895103a8feaSLen Brown exit(1); 896103a8feaSLen Brown } 897103a8feaSLen Brown 898103a8feaSLen Brown /* 899103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 900103a8feaSLen Brown * this check is valid for both Intel and AMD 901103a8feaSLen Brown */ 902103a8feaSLen Brown 903103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 9048209e054SThomas Renninger has_aperf = ecx & (1 << 0); 905103a8feaSLen Brown if (!has_aperf) { 906103a8feaSLen Brown fprintf(stderr, "No APERF MSR\n"); 907103a8feaSLen Brown exit(1); 908103a8feaSLen Brown } 909103a8feaSLen Brown 910103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 911103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 912103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 913103a8feaSLen Brown bclk = discover_bclk(family, model); 914103a8feaSLen Brown 915103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 916103a8feaSLen Brown } 917103a8feaSLen Brown 918103a8feaSLen Brown 919103a8feaSLen Brown void usage() 920103a8feaSLen Brown { 921103a8feaSLen Brown fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n", 922103a8feaSLen Brown progname); 923103a8feaSLen Brown exit(1); 924103a8feaSLen Brown } 925103a8feaSLen Brown 926103a8feaSLen Brown 927103a8feaSLen Brown /* 928103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 929103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 930103a8feaSLen Brown */ 931103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 932103a8feaSLen Brown { 933103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 934103a8feaSLen Brown return 1; 935103a8feaSLen Brown else 936103a8feaSLen Brown return 0; 937103a8feaSLen Brown } 938103a8feaSLen Brown 939103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 940103a8feaSLen Brown { 941103a8feaSLen Brown return 0; 942103a8feaSLen Brown } 943103a8feaSLen Brown 944103a8feaSLen Brown void turbostat_init() 945103a8feaSLen Brown { 946103a8feaSLen Brown check_cpuid(); 947103a8feaSLen Brown 948103a8feaSLen Brown check_dev_msr(); 949103a8feaSLen Brown check_super_user(); 950103a8feaSLen Brown 951*a829eb4dSLen Brown num_cpus = for_all_cpus(alloc_new_counters); 952103a8feaSLen Brown 953103a8feaSLen Brown if (verbose) 954103a8feaSLen Brown print_nehalem_info(); 955103a8feaSLen Brown } 956103a8feaSLen Brown 957103a8feaSLen Brown int fork_it(char **argv) 958103a8feaSLen Brown { 959103a8feaSLen Brown int retval; 960103a8feaSLen Brown pid_t child_pid; 961*a829eb4dSLen Brown get_counters(cnt_even); 962103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 963103a8feaSLen Brown 964103a8feaSLen Brown child_pid = fork(); 965103a8feaSLen Brown if (!child_pid) { 966103a8feaSLen Brown /* child */ 967103a8feaSLen Brown execvp(argv[0], argv); 968103a8feaSLen Brown } else { 969103a8feaSLen Brown int status; 970103a8feaSLen Brown 971103a8feaSLen Brown /* parent */ 972103a8feaSLen Brown if (child_pid == -1) { 973103a8feaSLen Brown perror("fork"); 974103a8feaSLen Brown exit(1); 975103a8feaSLen Brown } 976103a8feaSLen Brown 977103a8feaSLen Brown signal(SIGINT, SIG_IGN); 978103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 979103a8feaSLen Brown if (waitpid(child_pid, &status, 0) == -1) { 980103a8feaSLen Brown perror("wait"); 981103a8feaSLen Brown exit(1); 982103a8feaSLen Brown } 983103a8feaSLen Brown } 984*a829eb4dSLen Brown get_counters(cnt_odd); 985103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 986*a829eb4dSLen Brown retval = compute_delta(cnt_odd, cnt_even, cnt_delta); 987103a8feaSLen Brown 988103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 989*a829eb4dSLen Brown compute_average(cnt_delta, cnt_average); 990103a8feaSLen Brown if (!retval) 991*a829eb4dSLen Brown print_counters(cnt_delta); 992103a8feaSLen Brown 993103a8feaSLen Brown fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);; 994103a8feaSLen Brown 995103a8feaSLen Brown return 0; 996103a8feaSLen Brown } 997103a8feaSLen Brown 998103a8feaSLen Brown void cmdline(int argc, char **argv) 999103a8feaSLen Brown { 1000103a8feaSLen Brown int opt; 1001103a8feaSLen Brown 1002103a8feaSLen Brown progname = argv[0]; 1003103a8feaSLen Brown 1004103a8feaSLen Brown while ((opt = getopt(argc, argv, "+vi:M:")) != -1) { 1005103a8feaSLen Brown switch (opt) { 1006103a8feaSLen Brown case 'v': 1007103a8feaSLen Brown verbose++; 1008103a8feaSLen Brown break; 1009103a8feaSLen Brown case 'i': 1010103a8feaSLen Brown interval_sec = atoi(optarg); 1011103a8feaSLen Brown break; 1012103a8feaSLen Brown case 'M': 1013103a8feaSLen Brown sscanf(optarg, "%x", &extra_msr_offset); 1014103a8feaSLen Brown if (verbose > 1) 1015103a8feaSLen Brown fprintf(stderr, "MSR 0x%X\n", extra_msr_offset); 1016103a8feaSLen Brown break; 1017103a8feaSLen Brown default: 1018103a8feaSLen Brown usage(); 1019103a8feaSLen Brown } 1020103a8feaSLen Brown } 1021103a8feaSLen Brown } 1022103a8feaSLen Brown 1023103a8feaSLen Brown int main(int argc, char **argv) 1024103a8feaSLen Brown { 1025103a8feaSLen Brown cmdline(argc, argv); 1026103a8feaSLen Brown 1027103a8feaSLen Brown if (verbose > 1) 1028103a8feaSLen Brown fprintf(stderr, "turbostat Dec 6, 2010" 1029103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 1030103a8feaSLen Brown if (verbose > 1) 1031103a8feaSLen Brown fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n"); 1032103a8feaSLen Brown 1033103a8feaSLen Brown turbostat_init(); 1034103a8feaSLen Brown 1035103a8feaSLen Brown /* 1036103a8feaSLen Brown * if any params left, it must be a command to fork 1037103a8feaSLen Brown */ 1038103a8feaSLen Brown if (argc - optind) 1039103a8feaSLen Brown return fork_it(argv + optind); 1040103a8feaSLen Brown else 1041103a8feaSLen Brown turbostat_loop(); 1042103a8feaSLen Brown 1043103a8feaSLen Brown return 0; 1044103a8feaSLen Brown } 1045