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 23*9c63a650SLen Brown #include <asm/msr.h> 24103a8feaSLen Brown #include <stdio.h> 25103a8feaSLen Brown #include <unistd.h> 26103a8feaSLen Brown #include <sys/types.h> 27103a8feaSLen Brown #include <sys/wait.h> 28103a8feaSLen Brown #include <sys/stat.h> 29103a8feaSLen Brown #include <sys/resource.h> 30103a8feaSLen Brown #include <fcntl.h> 31103a8feaSLen Brown #include <signal.h> 32103a8feaSLen Brown #include <sys/time.h> 33103a8feaSLen Brown #include <stdlib.h> 34103a8feaSLen Brown #include <dirent.h> 35103a8feaSLen Brown #include <string.h> 36103a8feaSLen Brown #include <ctype.h> 3788c3281fSLen Brown #include <sched.h> 38103a8feaSLen Brown 39103a8feaSLen Brown char *proc_stat = "/proc/stat"; 40103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 41103a8feaSLen Brown unsigned int verbose; /* set with -v */ 42e23da037SLen Brown unsigned int summary_only; /* set with -s */ 43103a8feaSLen Brown unsigned int skip_c0; 44103a8feaSLen Brown unsigned int skip_c1; 45103a8feaSLen Brown unsigned int do_nhm_cstates; 46103a8feaSLen Brown unsigned int do_snb_cstates; 47103a8feaSLen Brown unsigned int has_aperf; 48103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 49103a8feaSLen Brown unsigned int genuine_intel; 50103a8feaSLen Brown unsigned int has_invariant_tsc; 51103a8feaSLen Brown unsigned int do_nehalem_platform_info; 52103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 536574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit; 542f32edf1SLen Brown unsigned int extra_msr_offset32; 552f32edf1SLen Brown unsigned int extra_msr_offset64; 568e180f3cSLen Brown unsigned int extra_delta_offset32; 578e180f3cSLen Brown unsigned int extra_delta_offset64; 58103a8feaSLen Brown double bclk; 59103a8feaSLen Brown unsigned int show_pkg; 60103a8feaSLen Brown unsigned int show_core; 61103a8feaSLen Brown unsigned int show_cpu; 62c98d5d94SLen Brown unsigned int show_pkg_only; 63c98d5d94SLen Brown unsigned int show_core_only; 64c98d5d94SLen Brown char *output_buffer, *outp; 65103a8feaSLen Brown 66103a8feaSLen Brown int aperf_mperf_unstable; 67103a8feaSLen Brown int backwards_count; 68103a8feaSLen Brown char *progname; 69103a8feaSLen Brown 70c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 71c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 72c98d5d94SLen Brown 73c98d5d94SLen Brown struct thread_data { 74c98d5d94SLen Brown unsigned long long tsc; 75c98d5d94SLen Brown unsigned long long aperf; 76c98d5d94SLen Brown unsigned long long mperf; 77c98d5d94SLen Brown unsigned long long c1; /* derived */ 782f32edf1SLen Brown unsigned long long extra_msr64; 798e180f3cSLen Brown unsigned long long extra_delta64; 808e180f3cSLen Brown unsigned long long extra_msr32; 818e180f3cSLen Brown unsigned long long extra_delta32; 82c98d5d94SLen Brown unsigned int cpu_id; 83c98d5d94SLen Brown unsigned int flags; 84c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 85c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 86c98d5d94SLen Brown } *thread_even, *thread_odd; 87c98d5d94SLen Brown 88c98d5d94SLen Brown struct core_data { 89c98d5d94SLen Brown unsigned long long c3; 90c98d5d94SLen Brown unsigned long long c6; 91c98d5d94SLen Brown unsigned long long c7; 92c98d5d94SLen Brown unsigned int core_id; 93c98d5d94SLen Brown } *core_even, *core_odd; 94c98d5d94SLen Brown 95c98d5d94SLen Brown struct pkg_data { 96c98d5d94SLen Brown unsigned long long pc2; 97c98d5d94SLen Brown unsigned long long pc3; 98c98d5d94SLen Brown unsigned long long pc6; 99c98d5d94SLen Brown unsigned long long pc7; 100c98d5d94SLen Brown unsigned int package_id; 101c98d5d94SLen Brown } *package_even, *package_odd; 102c98d5d94SLen Brown 103c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 104c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 105c98d5d94SLen Brown 106c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 107c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 108c98d5d94SLen Brown topo.num_threads_per_core + \ 109c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 110c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 111c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 112c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 113c98d5d94SLen Brown 114c98d5d94SLen Brown struct system_summary { 115c98d5d94SLen Brown struct thread_data threads; 116c98d5d94SLen Brown struct core_data cores; 117c98d5d94SLen Brown struct pkg_data packages; 118c98d5d94SLen Brown } sum, average; 119c98d5d94SLen Brown 120c98d5d94SLen Brown 121c98d5d94SLen Brown struct topo_params { 122c98d5d94SLen Brown int num_packages; 123103a8feaSLen Brown int num_cpus; 124c98d5d94SLen Brown int num_cores; 125c98d5d94SLen Brown int max_cpu_num; 126c98d5d94SLen Brown int num_cores_per_pkg; 127c98d5d94SLen Brown int num_threads_per_core; 128c98d5d94SLen Brown } topo; 129103a8feaSLen Brown 130c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 131103a8feaSLen Brown 132c98d5d94SLen Brown void setup_all_buffers(void); 133103a8feaSLen Brown 134c98d5d94SLen Brown int cpu_is_not_present(int cpu) 135d15cf7c1SLen Brown { 136c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 137c98d5d94SLen Brown } 138c98d5d94SLen Brown /* 139c98d5d94SLen Brown * run func(thread, core, package) in topology order 140c98d5d94SLen Brown * skip non-present cpus 141c98d5d94SLen Brown */ 142c98d5d94SLen Brown 143c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 144c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 145c98d5d94SLen Brown { 146c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 147c98d5d94SLen Brown 148c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 149c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 150c98d5d94SLen Brown for (thread_no = 0; thread_no < 151c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 152c98d5d94SLen Brown struct thread_data *t; 153c98d5d94SLen Brown struct core_data *c; 154c98d5d94SLen Brown struct pkg_data *p; 155c98d5d94SLen Brown 156c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 157c98d5d94SLen Brown 158c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 159c98d5d94SLen Brown continue; 160c98d5d94SLen Brown 161c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 162c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 163c98d5d94SLen Brown 164c98d5d94SLen Brown retval = func(t, c, p); 165c98d5d94SLen Brown if (retval) 166c98d5d94SLen Brown return retval; 167c98d5d94SLen Brown } 168c98d5d94SLen Brown } 169c98d5d94SLen Brown } 170d15cf7c1SLen Brown return 0; 171d15cf7c1SLen Brown } 172d15cf7c1SLen Brown 17388c3281fSLen Brown int cpu_migrate(int cpu) 17488c3281fSLen Brown { 175c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 176c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 177c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 17888c3281fSLen Brown return -1; 17988c3281fSLen Brown else 18088c3281fSLen Brown return 0; 18188c3281fSLen Brown } 18288c3281fSLen Brown 18315aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 184103a8feaSLen Brown { 185103a8feaSLen Brown ssize_t retval; 186103a8feaSLen Brown char pathname[32]; 187103a8feaSLen Brown int fd; 188103a8feaSLen Brown 189103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 190103a8feaSLen Brown fd = open(pathname, O_RDONLY); 19115aaa346SLen Brown if (fd < 0) 19215aaa346SLen Brown return -1; 193103a8feaSLen Brown 19415aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 195103a8feaSLen Brown close(fd); 19615aaa346SLen Brown 197d91bb17cSLen Brown if (retval != sizeof *msr) { 198d91bb17cSLen Brown fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset); 19915aaa346SLen Brown return -1; 200d91bb17cSLen Brown } 20115aaa346SLen Brown 20215aaa346SLen Brown return 0; 203103a8feaSLen Brown } 204103a8feaSLen Brown 205a829eb4dSLen Brown void print_header(void) 206103a8feaSLen Brown { 207103a8feaSLen Brown if (show_pkg) 208c98d5d94SLen Brown outp += sprintf(outp, "pk"); 209e23da037SLen Brown if (show_pkg) 210c98d5d94SLen Brown outp += sprintf(outp, " "); 211103a8feaSLen Brown if (show_core) 212c98d5d94SLen Brown outp += sprintf(outp, "cor"); 213103a8feaSLen Brown if (show_cpu) 214c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 215e23da037SLen Brown if (show_pkg || show_core || show_cpu) 216c98d5d94SLen Brown outp += sprintf(outp, " "); 217103a8feaSLen Brown if (do_nhm_cstates) 218c98d5d94SLen Brown outp += sprintf(outp, " %%c0"); 219103a8feaSLen Brown if (has_aperf) 220c98d5d94SLen Brown outp += sprintf(outp, " GHz"); 221c98d5d94SLen Brown outp += sprintf(outp, " TSC"); 2228e180f3cSLen Brown if (extra_delta_offset32) 223f9240813SLen Brown outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 2248e180f3cSLen Brown if (extra_delta_offset64) 225f9240813SLen Brown outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 2262f32edf1SLen Brown if (extra_msr_offset32) 2278e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 2282f32edf1SLen Brown if (extra_msr_offset64) 2298e180f3cSLen Brown outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 230103a8feaSLen Brown if (do_nhm_cstates) 231c98d5d94SLen Brown outp += sprintf(outp, " %%c1"); 232103a8feaSLen Brown if (do_nhm_cstates) 233c98d5d94SLen Brown outp += sprintf(outp, " %%c3"); 234103a8feaSLen Brown if (do_nhm_cstates) 235c98d5d94SLen Brown outp += sprintf(outp, " %%c6"); 236103a8feaSLen Brown if (do_snb_cstates) 237c98d5d94SLen Brown outp += sprintf(outp, " %%c7"); 238103a8feaSLen Brown if (do_snb_cstates) 239c98d5d94SLen Brown outp += sprintf(outp, " %%pc2"); 240103a8feaSLen Brown if (do_nhm_cstates) 241c98d5d94SLen Brown outp += sprintf(outp, " %%pc3"); 242103a8feaSLen Brown if (do_nhm_cstates) 243c98d5d94SLen Brown outp += sprintf(outp, " %%pc6"); 244103a8feaSLen Brown if (do_snb_cstates) 245c98d5d94SLen Brown outp += sprintf(outp, " %%pc7"); 246103a8feaSLen Brown 247c98d5d94SLen Brown outp += sprintf(outp, "\n"); 248103a8feaSLen Brown } 249103a8feaSLen Brown 250c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 251c98d5d94SLen Brown struct pkg_data *p) 252103a8feaSLen Brown { 253c98d5d94SLen Brown fprintf(stderr, "t %p, c %p, p %p\n", t, c, p); 254c98d5d94SLen Brown 255c98d5d94SLen Brown if (t) { 256c98d5d94SLen Brown fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 257c98d5d94SLen Brown fprintf(stderr, "TSC: %016llX\n", t->tsc); 258c98d5d94SLen Brown fprintf(stderr, "aperf: %016llX\n", t->aperf); 259c98d5d94SLen Brown fprintf(stderr, "mperf: %016llX\n", t->mperf); 260c98d5d94SLen Brown fprintf(stderr, "c1: %016llX\n", t->c1); 2618e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 2628e180f3cSLen Brown extra_delta_offset32, t->extra_delta32); 2638e180f3cSLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 2648e180f3cSLen Brown extra_delta_offset64, t->extra_delta64); 2658e180f3cSLen Brown fprintf(stderr, "msr0x%x: %08llX\n", 2662f32edf1SLen Brown extra_msr_offset32, t->extra_msr32); 267c98d5d94SLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 2682f32edf1SLen Brown extra_msr_offset64, t->extra_msr64); 269103a8feaSLen Brown } 270103a8feaSLen Brown 271c98d5d94SLen Brown if (c) { 272c98d5d94SLen Brown fprintf(stderr, "core: %d\n", c->core_id); 273c98d5d94SLen Brown fprintf(stderr, "c3: %016llX\n", c->c3); 274c98d5d94SLen Brown fprintf(stderr, "c6: %016llX\n", c->c6); 275c98d5d94SLen Brown fprintf(stderr, "c7: %016llX\n", c->c7); 276c98d5d94SLen Brown } 277103a8feaSLen Brown 278c98d5d94SLen Brown if (p) { 279c98d5d94SLen Brown fprintf(stderr, "package: %d\n", p->package_id); 280c98d5d94SLen Brown fprintf(stderr, "pc2: %016llX\n", p->pc2); 281c98d5d94SLen Brown fprintf(stderr, "pc3: %016llX\n", p->pc3); 282c98d5d94SLen Brown fprintf(stderr, "pc6: %016llX\n", p->pc6); 283c98d5d94SLen Brown fprintf(stderr, "pc7: %016llX\n", p->pc7); 284c98d5d94SLen Brown } 285c98d5d94SLen Brown return 0; 286103a8feaSLen Brown } 287103a8feaSLen Brown 288e23da037SLen Brown /* 289e23da037SLen Brown * column formatting convention & formats 290e23da037SLen Brown * package: "pk" 2 columns %2d 291e23da037SLen Brown * core: "cor" 3 columns %3d 292e23da037SLen Brown * CPU: "CPU" 3 columns %3d 293e23da037SLen Brown * GHz: "GHz" 3 columns %3.2 294e23da037SLen Brown * TSC: "TSC" 3 columns %3.2 295e23da037SLen Brown * percentage " %pc3" %6.2 296e23da037SLen Brown */ 297c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 298c98d5d94SLen Brown struct pkg_data *p) 299103a8feaSLen Brown { 300103a8feaSLen Brown double interval_float; 301103a8feaSLen Brown 302c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 303c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 304c98d5d94SLen Brown return 0; 305c98d5d94SLen Brown 306c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 307c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 308c98d5d94SLen Brown return 0; 309c98d5d94SLen Brown 310103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 311103a8feaSLen Brown 312c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 313c98d5d94SLen Brown if (t == &average.threads) { 314103a8feaSLen Brown if (show_pkg) 315c98d5d94SLen Brown outp += sprintf(outp, " "); 316e23da037SLen Brown if (show_pkg && show_core) 317c98d5d94SLen Brown outp += sprintf(outp, " "); 318103a8feaSLen Brown if (show_core) 319c98d5d94SLen Brown outp += sprintf(outp, " "); 320103a8feaSLen Brown if (show_cpu) 321c98d5d94SLen Brown outp += sprintf(outp, " " " "); 322103a8feaSLen Brown } else { 323c98d5d94SLen Brown if (show_pkg) { 324c98d5d94SLen Brown if (p) 325c98d5d94SLen Brown outp += sprintf(outp, "%2d", p->package_id); 326c98d5d94SLen Brown else 327c98d5d94SLen Brown outp += sprintf(outp, " "); 328c98d5d94SLen Brown } 329e23da037SLen Brown if (show_pkg && show_core) 330c98d5d94SLen Brown outp += sprintf(outp, " "); 331c98d5d94SLen Brown if (show_core) { 332c98d5d94SLen Brown if (c) 333c98d5d94SLen Brown outp += sprintf(outp, "%3d", c->core_id); 334c98d5d94SLen Brown else 335c98d5d94SLen Brown outp += sprintf(outp, " "); 336c98d5d94SLen Brown } 337103a8feaSLen Brown if (show_cpu) 338c98d5d94SLen Brown outp += sprintf(outp, " %3d", t->cpu_id); 339103a8feaSLen Brown } 340103a8feaSLen Brown 341103a8feaSLen Brown /* %c0 */ 342103a8feaSLen Brown if (do_nhm_cstates) { 343e23da037SLen Brown if (show_pkg || show_core || show_cpu) 344c98d5d94SLen Brown outp += sprintf(outp, " "); 345103a8feaSLen Brown if (!skip_c0) 346c98d5d94SLen Brown outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); 347103a8feaSLen Brown else 348c98d5d94SLen Brown outp += sprintf(outp, " ****"); 349103a8feaSLen Brown } 350103a8feaSLen Brown 351103a8feaSLen Brown /* GHz */ 352103a8feaSLen Brown if (has_aperf) { 353103a8feaSLen Brown if (!aperf_mperf_unstable) { 354c98d5d94SLen Brown outp += sprintf(outp, " %3.2f", 355c98d5d94SLen Brown 1.0 * t->tsc / units * t->aperf / 356c98d5d94SLen Brown t->mperf / interval_float); 357103a8feaSLen Brown } else { 358c98d5d94SLen Brown if (t->aperf > t->tsc || t->mperf > t->tsc) { 359c98d5d94SLen Brown outp += sprintf(outp, " ***"); 360103a8feaSLen Brown } else { 361c98d5d94SLen Brown outp += sprintf(outp, "%3.1f*", 362c98d5d94SLen Brown 1.0 * t->tsc / 363c98d5d94SLen Brown units * t->aperf / 364c98d5d94SLen Brown t->mperf / interval_float); 365103a8feaSLen Brown } 366103a8feaSLen Brown } 367103a8feaSLen Brown } 368103a8feaSLen Brown 369103a8feaSLen Brown /* TSC */ 370c98d5d94SLen Brown outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); 371103a8feaSLen Brown 3728e180f3cSLen Brown /* delta */ 3738e180f3cSLen Brown if (extra_delta_offset32) 3748e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta32); 3758e180f3cSLen Brown 3768e180f3cSLen Brown /* DELTA */ 3778e180f3cSLen Brown if (extra_delta_offset64) 3788e180f3cSLen Brown outp += sprintf(outp, " %11llu", t->extra_delta64); 3792f32edf1SLen Brown /* msr */ 3802f32edf1SLen Brown if (extra_msr_offset32) 3818e180f3cSLen Brown outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 3822f32edf1SLen Brown 383130ff304SLen Brown /* MSR */ 3842f32edf1SLen Brown if (extra_msr_offset64) 3852f32edf1SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 386130ff304SLen Brown 387103a8feaSLen Brown if (do_nhm_cstates) { 388103a8feaSLen Brown if (!skip_c1) 389c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); 390103a8feaSLen Brown else 391c98d5d94SLen Brown outp += sprintf(outp, " ****"); 392103a8feaSLen Brown } 393c98d5d94SLen Brown 394c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 395c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 396c98d5d94SLen Brown goto done; 397c98d5d94SLen Brown 398103a8feaSLen Brown if (do_nhm_cstates) 399c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); 400103a8feaSLen Brown if (do_nhm_cstates) 401c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); 402103a8feaSLen Brown if (do_snb_cstates) 403c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); 404c98d5d94SLen Brown 405c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 406c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 407c98d5d94SLen Brown goto done; 408c98d5d94SLen Brown 409103a8feaSLen Brown if (do_snb_cstates) 410c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); 411103a8feaSLen Brown if (do_nhm_cstates) 412c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); 413103a8feaSLen Brown if (do_nhm_cstates) 414c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); 415103a8feaSLen Brown if (do_snb_cstates) 416c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); 417c98d5d94SLen Brown done: 418c98d5d94SLen Brown outp += sprintf(outp, "\n"); 419c98d5d94SLen Brown 420c98d5d94SLen Brown return 0; 421103a8feaSLen Brown } 422103a8feaSLen Brown 423c98d5d94SLen Brown void flush_stdout() 424103a8feaSLen Brown { 425c98d5d94SLen Brown fputs(output_buffer, stdout); 426c98d5d94SLen Brown outp = output_buffer; 427c98d5d94SLen Brown } 428c98d5d94SLen Brown void flush_stderr() 429c98d5d94SLen Brown { 430c98d5d94SLen Brown fputs(output_buffer, stderr); 431c98d5d94SLen Brown outp = output_buffer; 432c98d5d94SLen Brown } 433c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 434c98d5d94SLen Brown { 435e23da037SLen Brown static int printed; 436103a8feaSLen Brown 437e23da037SLen Brown if (!printed || !summary_only) 438103a8feaSLen Brown print_header(); 439103a8feaSLen Brown 440c98d5d94SLen Brown if (topo.num_cpus > 1) 441c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 442c98d5d94SLen Brown &average.packages); 443103a8feaSLen Brown 444e23da037SLen Brown printed = 1; 445e23da037SLen Brown 446e23da037SLen Brown if (summary_only) 447e23da037SLen Brown return; 448e23da037SLen Brown 449c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 450103a8feaSLen Brown } 451103a8feaSLen Brown 452c98d5d94SLen Brown void 453c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 454103a8feaSLen Brown { 455c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 456c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 457c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 458c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 459103a8feaSLen Brown } 460103a8feaSLen Brown 461c98d5d94SLen Brown void 462c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 463c98d5d94SLen Brown { 464c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 465c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 466c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 467103a8feaSLen Brown } 468c98d5d94SLen Brown 469c3ae331dSLen Brown /* 470c3ae331dSLen Brown * old = new - old 471c3ae331dSLen Brown */ 472c98d5d94SLen Brown void 473c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 474c98d5d94SLen Brown struct core_data *core_delta) 475c98d5d94SLen Brown { 476c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 477c98d5d94SLen Brown 478103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 479c98d5d94SLen Brown if (old->tsc < (1000 * 1000)) { 480c98d5d94SLen Brown fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n"); 481c98d5d94SLen Brown fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n"); 482c98d5d94SLen Brown fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n"); 483103a8feaSLen Brown exit(-3); 484103a8feaSLen Brown } 485103a8feaSLen Brown 486c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 487c98d5d94SLen Brown 488c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 489c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 490c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 491c98d5d94SLen Brown } else { 492c98d5d94SLen Brown 493103a8feaSLen Brown if (!aperf_mperf_unstable) { 494103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 495103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 496103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 497103a8feaSLen Brown 498103a8feaSLen Brown aperf_mperf_unstable = 1; 499103a8feaSLen Brown } 500103a8feaSLen Brown /* 501103a8feaSLen Brown * mperf delta is likely a huge "positive" number 502103a8feaSLen Brown * can not use it for calculating c0 time 503103a8feaSLen Brown */ 504103a8feaSLen Brown skip_c0 = 1; 505103a8feaSLen Brown skip_c1 = 1; 506103a8feaSLen Brown } 507103a8feaSLen Brown 508c98d5d94SLen Brown 509103a8feaSLen Brown /* 510c3ae331dSLen Brown * As counter collection is not atomic, 511c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 512103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 513103a8feaSLen Brown */ 514c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 515c98d5d94SLen Brown old->c1 = 0; 516c98d5d94SLen Brown else { 517c98d5d94SLen Brown /* normal case, derive c1 */ 518c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 519c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 520c98d5d94SLen Brown } 521c3ae331dSLen Brown 522c98d5d94SLen Brown if (old->mperf == 0) { 523c3ae331dSLen Brown if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 524c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 525c98d5d94SLen Brown } 526103a8feaSLen Brown 5278e180f3cSLen Brown old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 5288e180f3cSLen Brown old->extra_delta32 &= 0xFFFFFFFF; 5298e180f3cSLen Brown 5308e180f3cSLen Brown old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 5318e180f3cSLen Brown 532103a8feaSLen Brown /* 5338e180f3cSLen Brown * Extra MSR is just a snapshot, simply copy latest w/o subtracting 534103a8feaSLen Brown */ 5352f32edf1SLen Brown old->extra_msr32 = new->extra_msr32; 5362f32edf1SLen Brown old->extra_msr64 = new->extra_msr64; 537103a8feaSLen Brown } 538c98d5d94SLen Brown 539c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 540c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 541c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 542c98d5d94SLen Brown { 543c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 544c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 545c98d5d94SLen Brown delta_core(c, c2); 546c98d5d94SLen Brown 547c98d5d94SLen Brown /* always calculate thread delta */ 548c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 549c98d5d94SLen Brown 550c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 551c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 552c98d5d94SLen Brown delta_package(p, p2); 553c98d5d94SLen Brown 554103a8feaSLen Brown return 0; 555103a8feaSLen Brown } 556103a8feaSLen Brown 557c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 558103a8feaSLen Brown { 559c98d5d94SLen Brown t->tsc = 0; 560c98d5d94SLen Brown t->aperf = 0; 561c98d5d94SLen Brown t->mperf = 0; 562c98d5d94SLen Brown t->c1 = 0; 563103a8feaSLen Brown 5648e180f3cSLen Brown t->extra_delta32 = 0; 5658e180f3cSLen Brown t->extra_delta64 = 0; 5668e180f3cSLen Brown 567c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 568c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 569c98d5d94SLen Brown 570c98d5d94SLen Brown c->c3 = 0; 571c98d5d94SLen Brown c->c6 = 0; 572c98d5d94SLen Brown c->c7 = 0; 573c98d5d94SLen Brown 574c98d5d94SLen Brown p->pc2 = 0; 575c98d5d94SLen Brown p->pc3 = 0; 576c98d5d94SLen Brown p->pc6 = 0; 577c98d5d94SLen Brown p->pc7 = 0; 578103a8feaSLen Brown } 579c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 580c98d5d94SLen Brown struct pkg_data *p) 581103a8feaSLen Brown { 582c98d5d94SLen Brown average.threads.tsc += t->tsc; 583c98d5d94SLen Brown average.threads.aperf += t->aperf; 584c98d5d94SLen Brown average.threads.mperf += t->mperf; 585c98d5d94SLen Brown average.threads.c1 += t->c1; 58615aaa346SLen Brown 5878e180f3cSLen Brown average.threads.extra_delta32 += t->extra_delta32; 5888e180f3cSLen Brown average.threads.extra_delta64 += t->extra_delta64; 5898e180f3cSLen Brown 590c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 591c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 592c98d5d94SLen Brown return 0; 593c98d5d94SLen Brown 594c98d5d94SLen Brown average.cores.c3 += c->c3; 595c98d5d94SLen Brown average.cores.c6 += c->c6; 596c98d5d94SLen Brown average.cores.c7 += c->c7; 597c98d5d94SLen Brown 598c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 599c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 600c98d5d94SLen Brown return 0; 601c98d5d94SLen Brown 602c98d5d94SLen Brown average.packages.pc2 += p->pc2; 603c98d5d94SLen Brown average.packages.pc3 += p->pc3; 604c98d5d94SLen Brown average.packages.pc6 += p->pc6; 605c98d5d94SLen Brown average.packages.pc7 += p->pc7; 606c98d5d94SLen Brown 607c98d5d94SLen Brown return 0; 608c98d5d94SLen Brown } 609c98d5d94SLen Brown /* 610c98d5d94SLen Brown * sum the counters for all cpus in the system 611c98d5d94SLen Brown * compute the weighted average 612c98d5d94SLen Brown */ 613c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 614c98d5d94SLen Brown struct pkg_data *p) 615c98d5d94SLen Brown { 616c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 617c98d5d94SLen Brown 618c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 619c98d5d94SLen Brown 620c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 621c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 622c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 623c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 624c98d5d94SLen Brown 6258e180f3cSLen Brown average.threads.extra_delta32 /= topo.num_cpus; 6268e180f3cSLen Brown average.threads.extra_delta32 &= 0xFFFFFFFF; 6278e180f3cSLen Brown 6288e180f3cSLen Brown average.threads.extra_delta64 /= topo.num_cpus; 6298e180f3cSLen Brown 630c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 631c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 632c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 633c98d5d94SLen Brown 634c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 635c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 636c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 637c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 638c98d5d94SLen Brown } 639c98d5d94SLen Brown 640c98d5d94SLen Brown static unsigned long long rdtsc(void) 641c98d5d94SLen Brown { 642c98d5d94SLen Brown unsigned int low, high; 643c98d5d94SLen Brown 644c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 645c98d5d94SLen Brown 646c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 647c98d5d94SLen Brown } 648c98d5d94SLen Brown 649c98d5d94SLen Brown 650c98d5d94SLen Brown /* 651c98d5d94SLen Brown * get_counters(...) 652c98d5d94SLen Brown * migrate to cpu 653c98d5d94SLen Brown * acquire and record local counters for that cpu 654c98d5d94SLen Brown */ 655c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 656c98d5d94SLen Brown { 657c98d5d94SLen Brown int cpu = t->cpu_id; 658c98d5d94SLen Brown 659c98d5d94SLen Brown if (cpu_migrate(cpu)) 66015aaa346SLen Brown return -1; 66115aaa346SLen Brown 662c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 66315aaa346SLen Brown 66415aaa346SLen Brown if (has_aperf) { 665*9c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 666c98d5d94SLen Brown return -3; 667*9c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 668c98d5d94SLen Brown return -4; 66988c3281fSLen Brown } 67088c3281fSLen Brown 6718e180f3cSLen Brown if (extra_delta_offset32) { 6728e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32)) 6732f32edf1SLen Brown return -5; 6748e180f3cSLen Brown t->extra_delta32 &= 0xFFFFFFFF; 6758e180f3cSLen Brown } 6768e180f3cSLen Brown 6778e180f3cSLen Brown if (extra_delta_offset64) 6788e180f3cSLen Brown if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 6798e180f3cSLen Brown return -5; 6808e180f3cSLen Brown 6818e180f3cSLen Brown if (extra_msr_offset32) { 6828e180f3cSLen Brown if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32)) 6838e180f3cSLen Brown return -5; 6848e180f3cSLen Brown t->extra_msr32 &= 0xFFFFFFFF; 6858e180f3cSLen Brown } 6862f32edf1SLen Brown 6872f32edf1SLen Brown if (extra_msr_offset64) 6882f32edf1SLen Brown if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 689c98d5d94SLen Brown return -5; 690c98d5d94SLen Brown 691c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 692c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 693c98d5d94SLen Brown return 0; 694c98d5d94SLen Brown 69515aaa346SLen Brown if (do_nhm_cstates) { 696c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 697c98d5d94SLen Brown return -6; 698c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 699c98d5d94SLen Brown return -7; 700103a8feaSLen Brown } 70115aaa346SLen Brown 70215aaa346SLen Brown if (do_snb_cstates) 703c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 704c98d5d94SLen Brown return -8; 705c98d5d94SLen Brown 706c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 707c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 708c98d5d94SLen Brown return 0; 70915aaa346SLen Brown 71015aaa346SLen Brown if (do_nhm_cstates) { 711c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 712c98d5d94SLen Brown return -9; 713c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 714c98d5d94SLen Brown return -10; 71515aaa346SLen Brown } 71615aaa346SLen Brown if (do_snb_cstates) { 717c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 718c98d5d94SLen Brown return -11; 719c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 720c98d5d94SLen Brown return -12; 72115aaa346SLen Brown } 72215aaa346SLen Brown return 0; 723103a8feaSLen Brown } 724103a8feaSLen Brown 725c98d5d94SLen Brown void print_verbose_header(void) 726103a8feaSLen Brown { 727103a8feaSLen Brown unsigned long long msr; 728103a8feaSLen Brown unsigned int ratio; 729103a8feaSLen Brown 730103a8feaSLen Brown if (!do_nehalem_platform_info) 731103a8feaSLen Brown return; 732103a8feaSLen Brown 733*9c63a650SLen Brown get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); 734103a8feaSLen Brown 7356574a5d5SLen Brown if (verbose > 1) 736*9c63a650SLen Brown fprintf(stderr, "MSR_NHM_PLATFORM_INFO: 0x%llx\n", msr); 7376574a5d5SLen Brown 738103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 739103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 740103a8feaSLen Brown ratio, bclk, ratio * bclk); 741103a8feaSLen Brown 742103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 743103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 744103a8feaSLen Brown ratio, bclk, ratio * bclk); 745103a8feaSLen Brown 7466574a5d5SLen Brown if (!do_ivt_turbo_ratio_limit) 7476574a5d5SLen Brown goto print_nhm_turbo_ratio_limits; 7486574a5d5SLen Brown 7496574a5d5SLen Brown get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 7506574a5d5SLen Brown 751103a8feaSLen Brown if (verbose > 1) 7526574a5d5SLen Brown fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr); 7536574a5d5SLen Brown 7546574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 7556574a5d5SLen Brown if (ratio) 7566574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 7576574a5d5SLen Brown ratio, bclk, ratio * bclk); 7586574a5d5SLen Brown 7596574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 7606574a5d5SLen Brown if (ratio) 7616574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 7626574a5d5SLen Brown ratio, bclk, ratio * bclk); 7636574a5d5SLen Brown 7646574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 7656574a5d5SLen Brown if (ratio) 7666574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 7676574a5d5SLen Brown ratio, bclk, ratio * bclk); 7686574a5d5SLen Brown 7696574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 7706574a5d5SLen Brown if (ratio) 7716574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 7726574a5d5SLen Brown ratio, bclk, ratio * bclk); 7736574a5d5SLen Brown 7746574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 7756574a5d5SLen Brown if (ratio) 7766574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 7776574a5d5SLen Brown ratio, bclk, ratio * bclk); 7786574a5d5SLen Brown 7796574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 7806574a5d5SLen Brown if (ratio) 7816574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 7826574a5d5SLen Brown ratio, bclk, ratio * bclk); 7836574a5d5SLen Brown 7846574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 7856574a5d5SLen Brown if (ratio) 7866574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 7876574a5d5SLen Brown ratio, bclk, ratio * bclk); 7886574a5d5SLen Brown 7896574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 7906574a5d5SLen Brown if (ratio) 7916574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 7926574a5d5SLen Brown ratio, bclk, ratio * bclk); 7936574a5d5SLen Brown 7946574a5d5SLen Brown print_nhm_turbo_ratio_limits: 795103a8feaSLen Brown 796103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 797103a8feaSLen Brown return; 798103a8feaSLen Brown 799*9c63a650SLen Brown get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 800103a8feaSLen Brown 8016574a5d5SLen Brown if (verbose > 1) 802*9c63a650SLen Brown fprintf(stderr, "MSR_NHM_TURBO_RATIO_LIMIT: 0x%llx\n", msr); 8036574a5d5SLen Brown 8046574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 8056574a5d5SLen Brown if (ratio) 8066574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 8076574a5d5SLen Brown ratio, bclk, ratio * bclk); 8086574a5d5SLen Brown 8096574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 8106574a5d5SLen Brown if (ratio) 8116574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 8126574a5d5SLen Brown ratio, bclk, ratio * bclk); 8136574a5d5SLen Brown 8146574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 8156574a5d5SLen Brown if (ratio) 8166574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 8176574a5d5SLen Brown ratio, bclk, ratio * bclk); 8186574a5d5SLen Brown 8196574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 8206574a5d5SLen Brown if (ratio) 8216574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 8226574a5d5SLen Brown ratio, bclk, ratio * bclk); 8236574a5d5SLen Brown 824103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 825103a8feaSLen Brown if (ratio) 826103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 827103a8feaSLen Brown ratio, bclk, ratio * bclk); 828103a8feaSLen Brown 829103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 830103a8feaSLen Brown if (ratio) 831103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 832103a8feaSLen Brown ratio, bclk, ratio * bclk); 833103a8feaSLen Brown 834103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 835103a8feaSLen Brown if (ratio) 836103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 837103a8feaSLen Brown ratio, bclk, ratio * bclk); 838103a8feaSLen Brown 839103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 840103a8feaSLen Brown if (ratio) 841103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 842103a8feaSLen Brown ratio, bclk, ratio * bclk); 843103a8feaSLen Brown } 844103a8feaSLen Brown 845c98d5d94SLen Brown void free_all_buffers(void) 846103a8feaSLen Brown { 847c98d5d94SLen Brown CPU_FREE(cpu_present_set); 848c98d5d94SLen Brown cpu_present_set = NULL; 849c98d5d94SLen Brown cpu_present_set = 0; 850103a8feaSLen Brown 851c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 852c98d5d94SLen Brown cpu_affinity_set = NULL; 853c98d5d94SLen Brown cpu_affinity_setsize = 0; 854103a8feaSLen Brown 855c98d5d94SLen Brown free(thread_even); 856c98d5d94SLen Brown free(core_even); 857c98d5d94SLen Brown free(package_even); 858103a8feaSLen Brown 859c98d5d94SLen Brown thread_even = NULL; 860c98d5d94SLen Brown core_even = NULL; 861c98d5d94SLen Brown package_even = NULL; 862103a8feaSLen Brown 863c98d5d94SLen Brown free(thread_odd); 864c98d5d94SLen Brown free(core_odd); 865c98d5d94SLen Brown free(package_odd); 866103a8feaSLen Brown 867c98d5d94SLen Brown thread_odd = NULL; 868c98d5d94SLen Brown core_odd = NULL; 869c98d5d94SLen Brown package_odd = NULL; 870103a8feaSLen Brown 871c98d5d94SLen Brown free(output_buffer); 872c98d5d94SLen Brown output_buffer = NULL; 873c98d5d94SLen Brown outp = NULL; 874103a8feaSLen Brown } 875103a8feaSLen Brown 876103a8feaSLen Brown /* 877c98d5d94SLen Brown * cpu_is_first_sibling_in_core(cpu) 878c98d5d94SLen Brown * return 1 if given CPU is 1st HT sibling in the core 879103a8feaSLen Brown */ 880c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu) 881103a8feaSLen Brown { 882c98d5d94SLen Brown char path[64]; 883c98d5d94SLen Brown FILE *filep; 884c98d5d94SLen Brown int first_cpu; 885103a8feaSLen Brown 886c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 887c98d5d94SLen Brown filep = fopen(path, "r"); 888c98d5d94SLen Brown if (filep == NULL) { 889c98d5d94SLen Brown perror(path); 890103a8feaSLen Brown exit(1); 891103a8feaSLen Brown } 892c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 893c98d5d94SLen Brown fclose(filep); 894c98d5d94SLen Brown return (cpu == first_cpu); 895c98d5d94SLen Brown } 896103a8feaSLen Brown 897c98d5d94SLen Brown /* 898c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 899c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 900c98d5d94SLen Brown */ 901c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 902c98d5d94SLen Brown { 903c98d5d94SLen Brown char path[64]; 904c98d5d94SLen Brown FILE *filep; 905c98d5d94SLen Brown int first_cpu; 906c98d5d94SLen Brown 907c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 908c98d5d94SLen Brown filep = fopen(path, "r"); 909c98d5d94SLen Brown if (filep == NULL) { 910c98d5d94SLen Brown perror(path); 911103a8feaSLen Brown exit(1); 912103a8feaSLen Brown } 913c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 914c98d5d94SLen Brown fclose(filep); 915c98d5d94SLen Brown return (cpu == first_cpu); 916103a8feaSLen Brown } 917103a8feaSLen Brown 918103a8feaSLen Brown int get_physical_package_id(int cpu) 919103a8feaSLen Brown { 920c98d5d94SLen Brown char path[80]; 921103a8feaSLen Brown FILE *filep; 922103a8feaSLen Brown int pkg; 923103a8feaSLen Brown 924103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 925103a8feaSLen Brown filep = fopen(path, "r"); 926103a8feaSLen Brown if (filep == NULL) { 927103a8feaSLen Brown perror(path); 928103a8feaSLen Brown exit(1); 929103a8feaSLen Brown } 930103a8feaSLen Brown fscanf(filep, "%d", &pkg); 931103a8feaSLen Brown fclose(filep); 932103a8feaSLen Brown return pkg; 933103a8feaSLen Brown } 934103a8feaSLen Brown 935103a8feaSLen Brown int get_core_id(int cpu) 936103a8feaSLen Brown { 937c98d5d94SLen Brown char path[80]; 938103a8feaSLen Brown FILE *filep; 939103a8feaSLen Brown int core; 940103a8feaSLen Brown 941103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 942103a8feaSLen Brown filep = fopen(path, "r"); 943103a8feaSLen Brown if (filep == NULL) { 944103a8feaSLen Brown perror(path); 945103a8feaSLen Brown exit(1); 946103a8feaSLen Brown } 947103a8feaSLen Brown fscanf(filep, "%d", &core); 948103a8feaSLen Brown fclose(filep); 949103a8feaSLen Brown return core; 950103a8feaSLen Brown } 951103a8feaSLen Brown 952c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 953c98d5d94SLen Brown { 954c98d5d94SLen Brown char path[80]; 955c98d5d94SLen Brown FILE *filep; 956c98d5d94SLen Brown int sib1, sib2; 957c98d5d94SLen Brown int matches; 958c98d5d94SLen Brown char character; 959c98d5d94SLen Brown 960c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 961c98d5d94SLen Brown filep = fopen(path, "r"); 962c98d5d94SLen Brown if (filep == NULL) { 963c98d5d94SLen Brown perror(path); 964c98d5d94SLen Brown exit(1); 965c98d5d94SLen Brown } 966103a8feaSLen Brown /* 967c98d5d94SLen Brown * file format: 968c98d5d94SLen Brown * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 969c98d5d94SLen Brown * otherwinse 1 sibling (self). 970c98d5d94SLen Brown */ 971c98d5d94SLen Brown matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 972c98d5d94SLen Brown 973c98d5d94SLen Brown fclose(filep); 974c98d5d94SLen Brown 975c98d5d94SLen Brown if (matches == 3) 976c98d5d94SLen Brown return 2; 977c98d5d94SLen Brown else 978c98d5d94SLen Brown return 1; 979c98d5d94SLen Brown } 980c98d5d94SLen Brown 981c98d5d94SLen Brown /* 982c98d5d94SLen Brown * run func(thread, core, package) in topology order 983c98d5d94SLen Brown * skip non-present cpus 984103a8feaSLen Brown */ 985103a8feaSLen Brown 986c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 987c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 988c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 989c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 990c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 991c98d5d94SLen Brown struct pkg_data *pkg_base2) 992c98d5d94SLen Brown { 993c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 994c98d5d94SLen Brown 995c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 996c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 997c98d5d94SLen Brown for (thread_no = 0; thread_no < 998c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 999c98d5d94SLen Brown struct thread_data *t, *t2; 1000c98d5d94SLen Brown struct core_data *c, *c2; 1001c98d5d94SLen Brown struct pkg_data *p, *p2; 1002c98d5d94SLen Brown 1003c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1004c98d5d94SLen Brown 1005c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 1006c98d5d94SLen Brown continue; 1007c98d5d94SLen Brown 1008c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1009c98d5d94SLen Brown 1010c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 1011c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 1012c98d5d94SLen Brown 1013c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 1014c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 1015c98d5d94SLen Brown 1016c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 1017c98d5d94SLen Brown if (retval) 1018c98d5d94SLen Brown return retval; 1019c98d5d94SLen Brown } 1020c98d5d94SLen Brown } 1021c98d5d94SLen Brown } 1022c98d5d94SLen Brown return 0; 1023c98d5d94SLen Brown } 1024c98d5d94SLen Brown 1025c98d5d94SLen Brown /* 1026c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 1027c98d5d94SLen Brown * return max_cpu number 1028c98d5d94SLen Brown */ 1029c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 1030103a8feaSLen Brown { 1031103a8feaSLen Brown FILE *fp; 1032c98d5d94SLen Brown int cpu_num; 1033103a8feaSLen Brown int retval; 1034103a8feaSLen Brown 1035103a8feaSLen Brown fp = fopen(proc_stat, "r"); 1036103a8feaSLen Brown if (fp == NULL) { 1037103a8feaSLen Brown perror(proc_stat); 1038103a8feaSLen Brown exit(1); 1039103a8feaSLen Brown } 1040103a8feaSLen Brown 1041103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1042103a8feaSLen Brown if (retval != 0) { 1043103a8feaSLen Brown perror("/proc/stat format"); 1044103a8feaSLen Brown exit(1); 1045103a8feaSLen Brown } 1046103a8feaSLen Brown 1047c98d5d94SLen Brown while (1) { 1048c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1049103a8feaSLen Brown if (retval != 1) 1050103a8feaSLen Brown break; 1051103a8feaSLen Brown 1052c98d5d94SLen Brown retval = func(cpu_num); 1053c98d5d94SLen Brown if (retval) { 1054c98d5d94SLen Brown fclose(fp); 1055c98d5d94SLen Brown return(retval); 1056c98d5d94SLen Brown } 1057103a8feaSLen Brown } 1058103a8feaSLen Brown fclose(fp); 1059c98d5d94SLen Brown return 0; 1060103a8feaSLen Brown } 1061103a8feaSLen Brown 1062103a8feaSLen Brown void re_initialize(void) 1063103a8feaSLen Brown { 1064c98d5d94SLen Brown free_all_buffers(); 1065c98d5d94SLen Brown setup_all_buffers(); 1066c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1067103a8feaSLen Brown } 1068103a8feaSLen Brown 1069c98d5d94SLen Brown 1070103a8feaSLen Brown /* 1071c98d5d94SLen Brown * count_cpus() 1072c98d5d94SLen Brown * remember the last one seen, it will be the max 1073103a8feaSLen Brown */ 1074c98d5d94SLen Brown int count_cpus(int cpu) 1075103a8feaSLen Brown { 1076c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1077c98d5d94SLen Brown topo.max_cpu_num = cpu; 1078103a8feaSLen Brown 1079c98d5d94SLen Brown topo.num_cpus += 1; 1080c98d5d94SLen Brown return 0; 1081103a8feaSLen Brown } 1082c98d5d94SLen Brown int mark_cpu_present(int cpu) 1083c98d5d94SLen Brown { 1084c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 108515aaa346SLen Brown return 0; 1086103a8feaSLen Brown } 1087103a8feaSLen Brown 1088103a8feaSLen Brown void turbostat_loop() 1089103a8feaSLen Brown { 1090c98d5d94SLen Brown int retval; 1091c98d5d94SLen Brown 1092103a8feaSLen Brown restart: 1093c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1094d91bb17cSLen Brown if (retval < -1) { 1095d91bb17cSLen Brown exit(retval); 1096d91bb17cSLen Brown } else if (retval == -1) { 1097c98d5d94SLen Brown re_initialize(); 1098c98d5d94SLen Brown goto restart; 1099c98d5d94SLen Brown } 1100103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1101103a8feaSLen Brown 1102103a8feaSLen Brown while (1) { 1103c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1104103a8feaSLen Brown re_initialize(); 1105103a8feaSLen Brown goto restart; 1106103a8feaSLen Brown } 1107103a8feaSLen Brown sleep(interval_sec); 1108c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1109d91bb17cSLen Brown if (retval < -1) { 1110d91bb17cSLen Brown exit(retval); 1111d91bb17cSLen Brown } else if (retval == -1) { 111215aaa346SLen Brown re_initialize(); 111315aaa346SLen Brown goto restart; 111415aaa346SLen Brown } 1115103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1116103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1117c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1118c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1119c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1120c98d5d94SLen Brown flush_stdout(); 112115aaa346SLen Brown sleep(interval_sec); 1122c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1123d91bb17cSLen Brown if (retval < -1) { 1124d91bb17cSLen Brown exit(retval); 1125d91bb17cSLen Brown } else if (retval == -1) { 1126103a8feaSLen Brown re_initialize(); 1127103a8feaSLen Brown goto restart; 1128103a8feaSLen Brown } 1129103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1130103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1131c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1132c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1133c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1134c98d5d94SLen Brown flush_stdout(); 1135103a8feaSLen Brown } 1136103a8feaSLen Brown } 1137103a8feaSLen Brown 1138103a8feaSLen Brown void check_dev_msr() 1139103a8feaSLen Brown { 1140103a8feaSLen Brown struct stat sb; 1141103a8feaSLen Brown 1142103a8feaSLen Brown if (stat("/dev/cpu/0/msr", &sb)) { 1143103a8feaSLen Brown fprintf(stderr, "no /dev/cpu/0/msr\n"); 1144103a8feaSLen Brown fprintf(stderr, "Try \"# modprobe msr\"\n"); 1145103a8feaSLen Brown exit(-5); 1146103a8feaSLen Brown } 1147103a8feaSLen Brown } 1148103a8feaSLen Brown 1149103a8feaSLen Brown void check_super_user() 1150103a8feaSLen Brown { 1151103a8feaSLen Brown if (getuid() != 0) { 1152103a8feaSLen Brown fprintf(stderr, "must be root\n"); 1153103a8feaSLen Brown exit(-6); 1154103a8feaSLen Brown } 1155103a8feaSLen Brown } 1156103a8feaSLen Brown 1157103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1158103a8feaSLen Brown { 1159103a8feaSLen Brown if (!genuine_intel) 1160103a8feaSLen Brown return 0; 1161103a8feaSLen Brown 1162103a8feaSLen Brown if (family != 6) 1163103a8feaSLen Brown return 0; 1164103a8feaSLen Brown 1165103a8feaSLen Brown switch (model) { 1166103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1167103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1168103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1169103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1170103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1171103a8feaSLen Brown case 0x2A: /* SNB */ 1172103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1173553575f1SLen Brown case 0x3A: /* IVB */ 11741300651bSLen Brown case 0x3E: /* IVB Xeon */ 1175103a8feaSLen Brown return 1; 1176103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1177103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1178103a8feaSLen Brown default: 1179103a8feaSLen Brown return 0; 1180103a8feaSLen Brown } 1181103a8feaSLen Brown } 11826574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 11836574a5d5SLen Brown { 11846574a5d5SLen Brown if (!genuine_intel) 11856574a5d5SLen Brown return 0; 11866574a5d5SLen Brown 11876574a5d5SLen Brown if (family != 6) 11886574a5d5SLen Brown return 0; 11896574a5d5SLen Brown 11906574a5d5SLen Brown switch (model) { 11916574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 11926574a5d5SLen Brown return 1; 11936574a5d5SLen Brown default: 11946574a5d5SLen Brown return 0; 11956574a5d5SLen Brown } 11966574a5d5SLen Brown } 11976574a5d5SLen Brown 1198103a8feaSLen Brown 1199103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 1200103a8feaSLen Brown { 1201103a8feaSLen Brown if (!genuine_intel) 1202103a8feaSLen Brown return 0; 1203103a8feaSLen Brown 1204103a8feaSLen Brown switch (model) { 1205103a8feaSLen Brown case 0x2A: 1206103a8feaSLen Brown case 0x2D: 1207650a37f3SLen Brown case 0x3A: /* IVB */ 12081300651bSLen Brown case 0x3E: /* IVB Xeon */ 1209103a8feaSLen Brown return 1; 1210103a8feaSLen Brown } 1211103a8feaSLen Brown return 0; 1212103a8feaSLen Brown } 1213103a8feaSLen Brown 1214103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 1215103a8feaSLen Brown { 1216103a8feaSLen Brown if (is_snb(family, model)) 1217103a8feaSLen Brown return 100.00; 1218103a8feaSLen Brown else 1219103a8feaSLen Brown return 133.33; 1220103a8feaSLen Brown } 1221103a8feaSLen Brown 1222103a8feaSLen Brown void check_cpuid() 1223103a8feaSLen Brown { 1224103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 1225103a8feaSLen Brown unsigned int fms, family, model, stepping; 1226103a8feaSLen Brown 1227103a8feaSLen Brown eax = ebx = ecx = edx = 0; 1228103a8feaSLen Brown 1229103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); 1230103a8feaSLen Brown 1231103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 1232103a8feaSLen Brown genuine_intel = 1; 1233103a8feaSLen Brown 1234103a8feaSLen Brown if (verbose) 1235103a8feaSLen Brown fprintf(stderr, "%.4s%.4s%.4s ", 1236103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 1237103a8feaSLen Brown 1238103a8feaSLen Brown asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); 1239103a8feaSLen Brown family = (fms >> 8) & 0xf; 1240103a8feaSLen Brown model = (fms >> 4) & 0xf; 1241103a8feaSLen Brown stepping = fms & 0xf; 1242103a8feaSLen Brown if (family == 6 || family == 0xf) 1243103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 1244103a8feaSLen Brown 1245103a8feaSLen Brown if (verbose) 1246103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 1247103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 1248103a8feaSLen Brown 1249103a8feaSLen Brown if (!(edx & (1 << 5))) { 1250103a8feaSLen Brown fprintf(stderr, "CPUID: no MSR\n"); 1251103a8feaSLen Brown exit(1); 1252103a8feaSLen Brown } 1253103a8feaSLen Brown 1254103a8feaSLen Brown /* 1255103a8feaSLen Brown * check max extended function levels of CPUID. 1256103a8feaSLen Brown * This is needed to check for invariant TSC. 1257103a8feaSLen Brown * This check is valid for both Intel and AMD. 1258103a8feaSLen Brown */ 1259103a8feaSLen Brown ebx = ecx = edx = 0; 1260103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); 1261103a8feaSLen Brown 1262103a8feaSLen Brown if (max_level < 0x80000007) { 1263103a8feaSLen Brown fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); 1264103a8feaSLen Brown exit(1); 1265103a8feaSLen Brown } 1266103a8feaSLen Brown 1267103a8feaSLen Brown /* 1268103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 1269103a8feaSLen Brown * this check is valid for both Intel and AMD 1270103a8feaSLen Brown */ 1271103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 12728209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 1273103a8feaSLen Brown 1274103a8feaSLen Brown if (!has_invariant_tsc) { 1275103a8feaSLen Brown fprintf(stderr, "No invariant TSC\n"); 1276103a8feaSLen Brown exit(1); 1277103a8feaSLen Brown } 1278103a8feaSLen Brown 1279103a8feaSLen Brown /* 1280103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 1281103a8feaSLen Brown * this check is valid for both Intel and AMD 1282103a8feaSLen Brown */ 1283103a8feaSLen Brown 1284103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 12858209e054SThomas Renninger has_aperf = ecx & (1 << 0); 1286103a8feaSLen Brown if (!has_aperf) { 1287103a8feaSLen Brown fprintf(stderr, "No APERF MSR\n"); 1288103a8feaSLen Brown exit(1); 1289103a8feaSLen Brown } 1290103a8feaSLen Brown 1291103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 1292103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 1293103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 1294103a8feaSLen Brown bclk = discover_bclk(family, model); 1295103a8feaSLen Brown 1296103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 12976574a5d5SLen Brown do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 1298103a8feaSLen Brown } 1299103a8feaSLen Brown 1300103a8feaSLen Brown 1301103a8feaSLen Brown void usage() 1302103a8feaSLen Brown { 1303f9240813SLen Brown fprintf(stderr, "%s: [-v][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", 1304103a8feaSLen Brown progname); 1305103a8feaSLen Brown exit(1); 1306103a8feaSLen Brown } 1307103a8feaSLen Brown 1308103a8feaSLen Brown 1309103a8feaSLen Brown /* 1310103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 1311103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 1312103a8feaSLen Brown */ 1313103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 1314103a8feaSLen Brown { 1315103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 1316103a8feaSLen Brown return 1; 1317103a8feaSLen Brown else 1318103a8feaSLen Brown return 0; 1319103a8feaSLen Brown } 1320103a8feaSLen Brown 1321103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 1322103a8feaSLen Brown { 1323103a8feaSLen Brown return 0; 1324103a8feaSLen Brown } 1325103a8feaSLen Brown 1326c98d5d94SLen Brown void topology_probe() 1327c98d5d94SLen Brown { 1328c98d5d94SLen Brown int i; 1329c98d5d94SLen Brown int max_core_id = 0; 1330c98d5d94SLen Brown int max_package_id = 0; 1331c98d5d94SLen Brown int max_siblings = 0; 1332c98d5d94SLen Brown struct cpu_topology { 1333c98d5d94SLen Brown int core_id; 1334c98d5d94SLen Brown int physical_package_id; 1335c98d5d94SLen Brown } *cpus; 1336c98d5d94SLen Brown 1337c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 1338c98d5d94SLen Brown topo.num_cpus = 0; 1339c98d5d94SLen Brown topo.max_cpu_num = 0; 1340c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 1341c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 1342c98d5d94SLen Brown show_cpu = 1; 1343c98d5d94SLen Brown 1344c98d5d94SLen Brown if (verbose > 1) 1345c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 1346c98d5d94SLen Brown 1347c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 1348c98d5d94SLen Brown if (cpus == NULL) { 1349c98d5d94SLen Brown perror("calloc cpus"); 1350c98d5d94SLen Brown exit(1); 1351c98d5d94SLen Brown } 1352c98d5d94SLen Brown 1353c98d5d94SLen Brown /* 1354c98d5d94SLen Brown * Allocate and initialize cpu_present_set 1355c98d5d94SLen Brown */ 1356c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1357c98d5d94SLen Brown if (cpu_present_set == NULL) { 1358c98d5d94SLen Brown perror("CPU_ALLOC"); 1359c98d5d94SLen Brown exit(3); 1360c98d5d94SLen Brown } 1361c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 1362c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 1363c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 1364c98d5d94SLen Brown 1365c98d5d94SLen Brown /* 1366c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 1367c98d5d94SLen Brown */ 1368c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1369c98d5d94SLen Brown if (cpu_affinity_set == NULL) { 1370c98d5d94SLen Brown perror("CPU_ALLOC"); 1371c98d5d94SLen Brown exit(3); 1372c98d5d94SLen Brown } 1373c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 1374c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 1375c98d5d94SLen Brown 1376c98d5d94SLen Brown 1377c98d5d94SLen Brown /* 1378c98d5d94SLen Brown * For online cpus 1379c98d5d94SLen Brown * find max_core_id, max_package_id 1380c98d5d94SLen Brown */ 1381c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 1382c98d5d94SLen Brown int siblings; 1383c98d5d94SLen Brown 1384c98d5d94SLen Brown if (cpu_is_not_present(i)) { 1385c98d5d94SLen Brown if (verbose > 1) 1386c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 1387c98d5d94SLen Brown continue; 1388c98d5d94SLen Brown } 1389c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 1390c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 1391c98d5d94SLen Brown max_core_id = cpus[i].core_id; 1392c98d5d94SLen Brown 1393c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 1394c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 1395c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 1396c98d5d94SLen Brown 1397c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 1398c98d5d94SLen Brown if (siblings > max_siblings) 1399c98d5d94SLen Brown max_siblings = siblings; 1400c98d5d94SLen Brown if (verbose > 1) 1401c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 1402c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 1403c98d5d94SLen Brown } 1404c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 1405c98d5d94SLen Brown if (verbose > 1) 1406c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 1407c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 1408c98d5d94SLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 1409c98d5d94SLen Brown show_core = 1; 1410c98d5d94SLen Brown 1411c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 1412c98d5d94SLen Brown if (verbose > 1) 1413c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 1414c98d5d94SLen Brown max_package_id, topo.num_packages); 1415c98d5d94SLen Brown if (!summary_only && topo.num_packages > 1) 1416c98d5d94SLen Brown show_pkg = 1; 1417c98d5d94SLen Brown 1418c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 1419c98d5d94SLen Brown if (verbose > 1) 1420c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 1421c98d5d94SLen Brown 1422c98d5d94SLen Brown free(cpus); 1423c98d5d94SLen Brown } 1424c98d5d94SLen Brown 1425c98d5d94SLen Brown void 1426c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 1427c98d5d94SLen Brown { 1428c98d5d94SLen Brown int i; 1429c98d5d94SLen Brown 1430c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 1431c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 1432c98d5d94SLen Brown if (*t == NULL) 1433c98d5d94SLen Brown goto error; 1434c98d5d94SLen Brown 1435c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 1436c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 1437c98d5d94SLen Brown (*t)[i].cpu_id = -1; 1438c98d5d94SLen Brown 1439c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 1440c98d5d94SLen Brown sizeof(struct core_data)); 1441c98d5d94SLen Brown if (*c == NULL) 1442c98d5d94SLen Brown goto error; 1443c98d5d94SLen Brown 1444c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 1445c98d5d94SLen Brown (*c)[i].core_id = -1; 1446c98d5d94SLen Brown 1447c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 1448c98d5d94SLen Brown if (*p == NULL) 1449c98d5d94SLen Brown goto error; 1450c98d5d94SLen Brown 1451c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 1452c98d5d94SLen Brown (*p)[i].package_id = i; 1453c98d5d94SLen Brown 1454c98d5d94SLen Brown return; 1455c98d5d94SLen Brown error: 1456c98d5d94SLen Brown perror("calloc counters"); 1457c98d5d94SLen Brown exit(1); 1458c98d5d94SLen Brown } 1459c98d5d94SLen Brown /* 1460c98d5d94SLen Brown * init_counter() 1461c98d5d94SLen Brown * 1462c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 1463c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 1464c98d5d94SLen Brown * 1465c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 1466c98d5d94SLen Brown */ 1467c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 1468c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 1469c98d5d94SLen Brown int pkg_num, int cpu_id) 1470c98d5d94SLen Brown { 1471c98d5d94SLen Brown struct thread_data *t; 1472c98d5d94SLen Brown struct core_data *c; 1473c98d5d94SLen Brown struct pkg_data *p; 1474c98d5d94SLen Brown 1475c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 1476c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 1477c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 1478c98d5d94SLen Brown 1479c98d5d94SLen Brown t->cpu_id = cpu_id; 1480c98d5d94SLen Brown if (thread_num == 0) { 1481c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 1482c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 1483c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 1484c98d5d94SLen Brown } 1485c98d5d94SLen Brown 1486c98d5d94SLen Brown c->core_id = core_num; 1487c98d5d94SLen Brown p->package_id = pkg_num; 1488c98d5d94SLen Brown } 1489c98d5d94SLen Brown 1490c98d5d94SLen Brown 1491c98d5d94SLen Brown int initialize_counters(int cpu_id) 1492c98d5d94SLen Brown { 1493c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 1494c98d5d94SLen Brown 1495c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 1496c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 1497c98d5d94SLen Brown 1498c98d5d94SLen Brown if (cpu_is_first_sibling_in_core(cpu_id)) { 1499c98d5d94SLen Brown my_thread_id = 0; 1500c98d5d94SLen Brown topo.num_cores++; 1501c98d5d94SLen Brown } else { 1502c98d5d94SLen Brown my_thread_id = 1; 1503c98d5d94SLen Brown } 1504c98d5d94SLen Brown 1505c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 1506c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 1507c98d5d94SLen Brown return 0; 1508c98d5d94SLen Brown } 1509c98d5d94SLen Brown 1510c98d5d94SLen Brown void allocate_output_buffer() 1511c98d5d94SLen Brown { 1512c98d5d94SLen Brown output_buffer = calloc(1, (1 + topo.num_cpus) * 128); 1513c98d5d94SLen Brown outp = output_buffer; 1514c98d5d94SLen Brown if (outp == NULL) { 1515c98d5d94SLen Brown perror("calloc"); 1516c98d5d94SLen Brown exit(-1); 1517c98d5d94SLen Brown } 1518c98d5d94SLen Brown } 1519c98d5d94SLen Brown 1520c98d5d94SLen Brown void setup_all_buffers(void) 1521c98d5d94SLen Brown { 1522c98d5d94SLen Brown topology_probe(); 1523c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 1524c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 1525c98d5d94SLen Brown allocate_output_buffer(); 1526c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 1527c98d5d94SLen Brown } 1528103a8feaSLen Brown void turbostat_init() 1529103a8feaSLen Brown { 1530103a8feaSLen Brown check_cpuid(); 1531103a8feaSLen Brown 1532103a8feaSLen Brown check_dev_msr(); 1533103a8feaSLen Brown check_super_user(); 1534103a8feaSLen Brown 1535c98d5d94SLen Brown setup_all_buffers(); 1536103a8feaSLen Brown 1537103a8feaSLen Brown if (verbose) 1538c98d5d94SLen Brown print_verbose_header(); 1539103a8feaSLen Brown } 1540103a8feaSLen Brown 1541103a8feaSLen Brown int fork_it(char **argv) 1542103a8feaSLen Brown { 1543103a8feaSLen Brown pid_t child_pid; 1544d91bb17cSLen Brown int status; 1545d15cf7c1SLen Brown 1546d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 1547d91bb17cSLen Brown if (status) 1548d91bb17cSLen Brown exit(status); 1549d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 1550d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 1551103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1552103a8feaSLen Brown 1553103a8feaSLen Brown child_pid = fork(); 1554103a8feaSLen Brown if (!child_pid) { 1555103a8feaSLen Brown /* child */ 1556103a8feaSLen Brown execvp(argv[0], argv); 1557103a8feaSLen Brown } else { 1558103a8feaSLen Brown 1559103a8feaSLen Brown /* parent */ 1560103a8feaSLen Brown if (child_pid == -1) { 1561103a8feaSLen Brown perror("fork"); 1562103a8feaSLen Brown exit(1); 1563103a8feaSLen Brown } 1564103a8feaSLen Brown 1565103a8feaSLen Brown signal(SIGINT, SIG_IGN); 1566103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 1567103a8feaSLen Brown if (waitpid(child_pid, &status, 0) == -1) { 1568103a8feaSLen Brown perror("wait"); 1569d91bb17cSLen Brown exit(status); 1570103a8feaSLen Brown } 1571103a8feaSLen Brown } 1572c98d5d94SLen Brown /* 1573c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 1574c98d5d94SLen Brown * because re-starting is problematic when forking 1575c98d5d94SLen Brown */ 1576c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 1577103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1578103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1579c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1580c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1581c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1582c98d5d94SLen Brown flush_stderr(); 1583103a8feaSLen Brown 15846eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 1585103a8feaSLen Brown 1586d91bb17cSLen Brown return status; 1587103a8feaSLen Brown } 1588103a8feaSLen Brown 1589103a8feaSLen Brown void cmdline(int argc, char **argv) 1590103a8feaSLen Brown { 1591103a8feaSLen Brown int opt; 1592103a8feaSLen Brown 1593103a8feaSLen Brown progname = argv[0]; 1594103a8feaSLen Brown 159539300ffbSLen Brown while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:")) != -1) { 1596103a8feaSLen Brown switch (opt) { 1597f9240813SLen Brown case 'p': 1598c98d5d94SLen Brown show_core_only++; 1599c98d5d94SLen Brown break; 1600f9240813SLen Brown case 'P': 1601c98d5d94SLen Brown show_pkg_only++; 1602c98d5d94SLen Brown break; 1603f9240813SLen Brown case 'S': 1604e23da037SLen Brown summary_only++; 1605e23da037SLen Brown break; 1606103a8feaSLen Brown case 'v': 1607103a8feaSLen Brown verbose++; 1608103a8feaSLen Brown break; 1609103a8feaSLen Brown case 'i': 1610103a8feaSLen Brown interval_sec = atoi(optarg); 1611103a8feaSLen Brown break; 1612f9240813SLen Brown case 'c': 16138e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset32); 16148e180f3cSLen Brown break; 1615f9240813SLen Brown case 's': 1616f9240813SLen Brown extra_delta_offset32 = 0x34; /* SMI counter */ 1617f9240813SLen Brown break; 1618f9240813SLen Brown case 'C': 16198e180f3cSLen Brown sscanf(optarg, "%x", &extra_delta_offset64); 16208e180f3cSLen Brown break; 16212f32edf1SLen Brown case 'm': 16222f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset32); 16232f32edf1SLen Brown break; 16242f32edf1SLen Brown case 'M': 16252f32edf1SLen Brown sscanf(optarg, "%x", &extra_msr_offset64); 1626103a8feaSLen Brown break; 1627103a8feaSLen Brown default: 1628103a8feaSLen Brown usage(); 1629103a8feaSLen Brown } 1630103a8feaSLen Brown } 1631103a8feaSLen Brown } 1632103a8feaSLen Brown 1633103a8feaSLen Brown int main(int argc, char **argv) 1634103a8feaSLen Brown { 1635103a8feaSLen Brown cmdline(argc, argv); 1636103a8feaSLen Brown 1637103a8feaSLen Brown if (verbose > 1) 1638f9240813SLen Brown fprintf(stderr, "turbostat v2.1 October 6, 2012" 1639103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 1640103a8feaSLen Brown 1641103a8feaSLen Brown turbostat_init(); 1642103a8feaSLen Brown 1643103a8feaSLen Brown /* 1644103a8feaSLen Brown * if any params left, it must be a command to fork 1645103a8feaSLen Brown */ 1646103a8feaSLen Brown if (argc - optind) 1647103a8feaSLen Brown return fork_it(argv + optind); 1648103a8feaSLen Brown else 1649103a8feaSLen Brown turbostat_loop(); 1650103a8feaSLen Brown 1651103a8feaSLen Brown return 0; 1652103a8feaSLen Brown } 1653