1103a8feaSLen Brown /* 2103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 3103a8feaSLen Brown * on modern Intel turbo-capable processors. 4103a8feaSLen Brown * 5e23da037SLen Brown * Copyright (c) 2012 Intel Corporation. 6103a8feaSLen Brown * Len Brown <len.brown@intel.com> 7103a8feaSLen Brown * 8103a8feaSLen Brown * This program is free software; you can redistribute it and/or modify it 9103a8feaSLen Brown * under the terms and conditions of the GNU General Public License, 10103a8feaSLen Brown * version 2, as published by the Free Software Foundation. 11103a8feaSLen Brown * 12103a8feaSLen Brown * This program is distributed in the hope it will be useful, but WITHOUT 13103a8feaSLen Brown * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14103a8feaSLen Brown * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15103a8feaSLen Brown * more details. 16103a8feaSLen Brown * 17103a8feaSLen Brown * You should have received a copy of the GNU General Public License along with 18103a8feaSLen Brown * this program; if not, write to the Free Software Foundation, Inc., 19103a8feaSLen Brown * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 20103a8feaSLen Brown */ 21103a8feaSLen Brown 2288c3281fSLen Brown #define _GNU_SOURCE 23103a8feaSLen Brown #include <stdio.h> 24103a8feaSLen Brown #include <unistd.h> 25103a8feaSLen Brown #include <sys/types.h> 26103a8feaSLen Brown #include <sys/wait.h> 27103a8feaSLen Brown #include <sys/stat.h> 28103a8feaSLen Brown #include <sys/resource.h> 29103a8feaSLen Brown #include <fcntl.h> 30103a8feaSLen Brown #include <signal.h> 31103a8feaSLen Brown #include <sys/time.h> 32103a8feaSLen Brown #include <stdlib.h> 33103a8feaSLen Brown #include <dirent.h> 34103a8feaSLen Brown #include <string.h> 35103a8feaSLen Brown #include <ctype.h> 3688c3281fSLen Brown #include <sched.h> 37103a8feaSLen Brown 38103a8feaSLen Brown #define MSR_NEHALEM_PLATFORM_INFO 0xCE 39103a8feaSLen Brown #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD 40*6574a5d5SLen Brown #define MSR_IVT_TURBO_RATIO_LIMIT 0x1AE 41103a8feaSLen Brown #define MSR_APERF 0xE8 42103a8feaSLen Brown #define MSR_MPERF 0xE7 43103a8feaSLen Brown #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */ 44103a8feaSLen Brown #define MSR_PKG_C3_RESIDENCY 0x3F8 45103a8feaSLen Brown #define MSR_PKG_C6_RESIDENCY 0x3F9 46103a8feaSLen Brown #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */ 47103a8feaSLen Brown #define MSR_CORE_C3_RESIDENCY 0x3FC 48103a8feaSLen Brown #define MSR_CORE_C6_RESIDENCY 0x3FD 49103a8feaSLen Brown #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */ 50103a8feaSLen Brown 51103a8feaSLen Brown char *proc_stat = "/proc/stat"; 52103a8feaSLen Brown unsigned int interval_sec = 5; /* set with -i interval_sec */ 53103a8feaSLen Brown unsigned int verbose; /* set with -v */ 54e23da037SLen Brown unsigned int summary_only; /* set with -s */ 55103a8feaSLen Brown unsigned int skip_c0; 56103a8feaSLen Brown unsigned int skip_c1; 57103a8feaSLen Brown unsigned int do_nhm_cstates; 58103a8feaSLen Brown unsigned int do_snb_cstates; 59103a8feaSLen Brown unsigned int has_aperf; 60103a8feaSLen Brown unsigned int units = 1000000000; /* Ghz etc */ 61103a8feaSLen Brown unsigned int genuine_intel; 62103a8feaSLen Brown unsigned int has_invariant_tsc; 63103a8feaSLen Brown unsigned int do_nehalem_platform_info; 64103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit; 65*6574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit; 66103a8feaSLen Brown unsigned int extra_msr_offset; 67103a8feaSLen Brown double bclk; 68103a8feaSLen Brown unsigned int show_pkg; 69103a8feaSLen Brown unsigned int show_core; 70103a8feaSLen Brown unsigned int show_cpu; 71c98d5d94SLen Brown unsigned int show_pkg_only; 72c98d5d94SLen Brown unsigned int show_core_only; 73c98d5d94SLen Brown char *output_buffer, *outp; 74103a8feaSLen Brown 75103a8feaSLen Brown int aperf_mperf_unstable; 76103a8feaSLen Brown int backwards_count; 77103a8feaSLen Brown char *progname; 78103a8feaSLen Brown 79c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set; 80c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize; 81c98d5d94SLen Brown 82c98d5d94SLen Brown struct thread_data { 83c98d5d94SLen Brown unsigned long long tsc; 84c98d5d94SLen Brown unsigned long long aperf; 85c98d5d94SLen Brown unsigned long long mperf; 86c98d5d94SLen Brown unsigned long long c1; /* derived */ 87c98d5d94SLen Brown unsigned long long extra_msr; 88c98d5d94SLen Brown unsigned int cpu_id; 89c98d5d94SLen Brown unsigned int flags; 90c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 91c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 92c98d5d94SLen Brown } *thread_even, *thread_odd; 93c98d5d94SLen Brown 94c98d5d94SLen Brown struct core_data { 95c98d5d94SLen Brown unsigned long long c3; 96c98d5d94SLen Brown unsigned long long c6; 97c98d5d94SLen Brown unsigned long long c7; 98c98d5d94SLen Brown unsigned int core_id; 99c98d5d94SLen Brown } *core_even, *core_odd; 100c98d5d94SLen Brown 101c98d5d94SLen Brown struct pkg_data { 102c98d5d94SLen Brown unsigned long long pc2; 103c98d5d94SLen Brown unsigned long long pc3; 104c98d5d94SLen Brown unsigned long long pc6; 105c98d5d94SLen Brown unsigned long long pc7; 106c98d5d94SLen Brown unsigned int package_id; 107c98d5d94SLen Brown } *package_even, *package_odd; 108c98d5d94SLen Brown 109c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 110c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 111c98d5d94SLen Brown 112c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 113c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 114c98d5d94SLen Brown topo.num_threads_per_core + \ 115c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 116c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 117c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 118c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 119c98d5d94SLen Brown 120c98d5d94SLen Brown struct system_summary { 121c98d5d94SLen Brown struct thread_data threads; 122c98d5d94SLen Brown struct core_data cores; 123c98d5d94SLen Brown struct pkg_data packages; 124c98d5d94SLen Brown } sum, average; 125c98d5d94SLen Brown 126c98d5d94SLen Brown 127c98d5d94SLen Brown struct topo_params { 128c98d5d94SLen Brown int num_packages; 129103a8feaSLen Brown int num_cpus; 130c98d5d94SLen Brown int num_cores; 131c98d5d94SLen Brown int max_cpu_num; 132c98d5d94SLen Brown int num_cores_per_pkg; 133c98d5d94SLen Brown int num_threads_per_core; 134c98d5d94SLen Brown } topo; 135103a8feaSLen Brown 136c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 137103a8feaSLen Brown 138c98d5d94SLen Brown void setup_all_buffers(void); 139103a8feaSLen Brown 140c98d5d94SLen Brown int cpu_is_not_present(int cpu) 141d15cf7c1SLen Brown { 142c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 143c98d5d94SLen Brown } 144c98d5d94SLen Brown /* 145c98d5d94SLen Brown * run func(thread, core, package) in topology order 146c98d5d94SLen Brown * skip non-present cpus 147c98d5d94SLen Brown */ 148c98d5d94SLen Brown 149c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 150c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 151c98d5d94SLen Brown { 152c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 153c98d5d94SLen Brown 154c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 155c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 156c98d5d94SLen Brown for (thread_no = 0; thread_no < 157c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 158c98d5d94SLen Brown struct thread_data *t; 159c98d5d94SLen Brown struct core_data *c; 160c98d5d94SLen Brown struct pkg_data *p; 161c98d5d94SLen Brown 162c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 163c98d5d94SLen Brown 164c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 165c98d5d94SLen Brown continue; 166c98d5d94SLen Brown 167c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 168c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 169c98d5d94SLen Brown 170c98d5d94SLen Brown retval = func(t, c, p); 171c98d5d94SLen Brown if (retval) 172c98d5d94SLen Brown return retval; 173c98d5d94SLen Brown } 174c98d5d94SLen Brown } 175c98d5d94SLen Brown } 176d15cf7c1SLen Brown return 0; 177d15cf7c1SLen Brown } 178d15cf7c1SLen Brown 17988c3281fSLen Brown int cpu_migrate(int cpu) 18088c3281fSLen Brown { 181c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 182c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 183c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 18488c3281fSLen Brown return -1; 18588c3281fSLen Brown else 18688c3281fSLen Brown return 0; 18788c3281fSLen Brown } 18888c3281fSLen Brown 18915aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 190103a8feaSLen Brown { 191103a8feaSLen Brown ssize_t retval; 192103a8feaSLen Brown char pathname[32]; 193103a8feaSLen Brown int fd; 194103a8feaSLen Brown 195103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 196103a8feaSLen Brown fd = open(pathname, O_RDONLY); 19715aaa346SLen Brown if (fd < 0) 19815aaa346SLen Brown return -1; 199103a8feaSLen Brown 20015aaa346SLen Brown retval = pread(fd, msr, sizeof *msr, offset); 201103a8feaSLen Brown close(fd); 20215aaa346SLen Brown 20315aaa346SLen Brown if (retval != sizeof *msr) 20415aaa346SLen Brown return -1; 20515aaa346SLen Brown 20615aaa346SLen Brown return 0; 207103a8feaSLen Brown } 208103a8feaSLen Brown 209a829eb4dSLen Brown void print_header(void) 210103a8feaSLen Brown { 211103a8feaSLen Brown if (show_pkg) 212c98d5d94SLen Brown outp += sprintf(outp, "pk"); 213e23da037SLen Brown if (show_pkg) 214c98d5d94SLen Brown outp += sprintf(outp, " "); 215103a8feaSLen Brown if (show_core) 216c98d5d94SLen Brown outp += sprintf(outp, "cor"); 217103a8feaSLen Brown if (show_cpu) 218c98d5d94SLen Brown outp += sprintf(outp, " CPU"); 219e23da037SLen Brown if (show_pkg || show_core || show_cpu) 220c98d5d94SLen Brown outp += sprintf(outp, " "); 221103a8feaSLen Brown if (do_nhm_cstates) 222c98d5d94SLen Brown outp += sprintf(outp, " %%c0"); 223103a8feaSLen Brown if (has_aperf) 224c98d5d94SLen Brown outp += sprintf(outp, " GHz"); 225c98d5d94SLen Brown outp += sprintf(outp, " TSC"); 226103a8feaSLen Brown if (do_nhm_cstates) 227c98d5d94SLen Brown outp += sprintf(outp, " %%c1"); 228103a8feaSLen Brown if (do_nhm_cstates) 229c98d5d94SLen Brown outp += sprintf(outp, " %%c3"); 230103a8feaSLen Brown if (do_nhm_cstates) 231c98d5d94SLen Brown outp += sprintf(outp, " %%c6"); 232103a8feaSLen Brown if (do_snb_cstates) 233c98d5d94SLen Brown outp += sprintf(outp, " %%c7"); 234103a8feaSLen Brown if (do_snb_cstates) 235c98d5d94SLen Brown outp += sprintf(outp, " %%pc2"); 236103a8feaSLen Brown if (do_nhm_cstates) 237c98d5d94SLen Brown outp += sprintf(outp, " %%pc3"); 238103a8feaSLen Brown if (do_nhm_cstates) 239c98d5d94SLen Brown outp += sprintf(outp, " %%pc6"); 240103a8feaSLen Brown if (do_snb_cstates) 241c98d5d94SLen Brown outp += sprintf(outp, " %%pc7"); 242103a8feaSLen Brown if (extra_msr_offset) 243c98d5d94SLen Brown outp += sprintf(outp, " MSR 0x%x ", extra_msr_offset); 244103a8feaSLen Brown 245c98d5d94SLen Brown outp += sprintf(outp, "\n"); 246103a8feaSLen Brown } 247103a8feaSLen Brown 248c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 249c98d5d94SLen Brown struct pkg_data *p) 250103a8feaSLen Brown { 251c98d5d94SLen Brown fprintf(stderr, "t %p, c %p, p %p\n", t, c, p); 252c98d5d94SLen Brown 253c98d5d94SLen Brown if (t) { 254c98d5d94SLen Brown fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 255c98d5d94SLen Brown fprintf(stderr, "TSC: %016llX\n", t->tsc); 256c98d5d94SLen Brown fprintf(stderr, "aperf: %016llX\n", t->aperf); 257c98d5d94SLen Brown fprintf(stderr, "mperf: %016llX\n", t->mperf); 258c98d5d94SLen Brown fprintf(stderr, "c1: %016llX\n", t->c1); 259c98d5d94SLen Brown fprintf(stderr, "msr0x%x: %016llX\n", 260c98d5d94SLen Brown extra_msr_offset, t->extra_msr); 261103a8feaSLen Brown } 262103a8feaSLen Brown 263c98d5d94SLen Brown if (c) { 264c98d5d94SLen Brown fprintf(stderr, "core: %d\n", c->core_id); 265c98d5d94SLen Brown fprintf(stderr, "c3: %016llX\n", c->c3); 266c98d5d94SLen Brown fprintf(stderr, "c6: %016llX\n", c->c6); 267c98d5d94SLen Brown fprintf(stderr, "c7: %016llX\n", c->c7); 268c98d5d94SLen Brown } 269103a8feaSLen Brown 270c98d5d94SLen Brown if (p) { 271c98d5d94SLen Brown fprintf(stderr, "package: %d\n", p->package_id); 272c98d5d94SLen Brown fprintf(stderr, "pc2: %016llX\n", p->pc2); 273c98d5d94SLen Brown fprintf(stderr, "pc3: %016llX\n", p->pc3); 274c98d5d94SLen Brown fprintf(stderr, "pc6: %016llX\n", p->pc6); 275c98d5d94SLen Brown fprintf(stderr, "pc7: %016llX\n", p->pc7); 276c98d5d94SLen Brown } 277c98d5d94SLen Brown return 0; 278103a8feaSLen Brown } 279103a8feaSLen Brown 280e23da037SLen Brown /* 281e23da037SLen Brown * column formatting convention & formats 282e23da037SLen Brown * package: "pk" 2 columns %2d 283e23da037SLen Brown * core: "cor" 3 columns %3d 284e23da037SLen Brown * CPU: "CPU" 3 columns %3d 285e23da037SLen Brown * GHz: "GHz" 3 columns %3.2 286e23da037SLen Brown * TSC: "TSC" 3 columns %3.2 287e23da037SLen Brown * percentage " %pc3" %6.2 288e23da037SLen Brown */ 289c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 290c98d5d94SLen Brown struct pkg_data *p) 291103a8feaSLen Brown { 292103a8feaSLen Brown double interval_float; 293103a8feaSLen Brown 294c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 295c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 296c98d5d94SLen Brown return 0; 297c98d5d94SLen Brown 298c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 299c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 300c98d5d94SLen Brown return 0; 301c98d5d94SLen Brown 302103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 303103a8feaSLen Brown 304c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 305c98d5d94SLen Brown if (t == &average.threads) { 306103a8feaSLen Brown if (show_pkg) 307c98d5d94SLen Brown outp += sprintf(outp, " "); 308e23da037SLen Brown if (show_pkg && show_core) 309c98d5d94SLen Brown outp += sprintf(outp, " "); 310103a8feaSLen Brown if (show_core) 311c98d5d94SLen Brown outp += sprintf(outp, " "); 312103a8feaSLen Brown if (show_cpu) 313c98d5d94SLen Brown outp += sprintf(outp, " " " "); 314103a8feaSLen Brown } else { 315c98d5d94SLen Brown if (show_pkg) { 316c98d5d94SLen Brown if (p) 317c98d5d94SLen Brown outp += sprintf(outp, "%2d", p->package_id); 318c98d5d94SLen Brown else 319c98d5d94SLen Brown outp += sprintf(outp, " "); 320c98d5d94SLen Brown } 321e23da037SLen Brown if (show_pkg && show_core) 322c98d5d94SLen Brown outp += sprintf(outp, " "); 323c98d5d94SLen Brown if (show_core) { 324c98d5d94SLen Brown if (c) 325c98d5d94SLen Brown outp += sprintf(outp, "%3d", c->core_id); 326c98d5d94SLen Brown else 327c98d5d94SLen Brown outp += sprintf(outp, " "); 328c98d5d94SLen Brown } 329103a8feaSLen Brown if (show_cpu) 330c98d5d94SLen Brown outp += sprintf(outp, " %3d", t->cpu_id); 331103a8feaSLen Brown } 332103a8feaSLen Brown 333103a8feaSLen Brown /* %c0 */ 334103a8feaSLen Brown if (do_nhm_cstates) { 335e23da037SLen Brown if (show_pkg || show_core || show_cpu) 336c98d5d94SLen Brown outp += sprintf(outp, " "); 337103a8feaSLen Brown if (!skip_c0) 338c98d5d94SLen Brown outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); 339103a8feaSLen Brown else 340c98d5d94SLen Brown outp += sprintf(outp, " ****"); 341103a8feaSLen Brown } 342103a8feaSLen Brown 343103a8feaSLen Brown /* GHz */ 344103a8feaSLen Brown if (has_aperf) { 345103a8feaSLen Brown if (!aperf_mperf_unstable) { 346c98d5d94SLen Brown outp += sprintf(outp, " %3.2f", 347c98d5d94SLen Brown 1.0 * t->tsc / units * t->aperf / 348c98d5d94SLen Brown t->mperf / interval_float); 349103a8feaSLen Brown } else { 350c98d5d94SLen Brown if (t->aperf > t->tsc || t->mperf > t->tsc) { 351c98d5d94SLen Brown outp += sprintf(outp, " ***"); 352103a8feaSLen Brown } else { 353c98d5d94SLen Brown outp += sprintf(outp, "%3.1f*", 354c98d5d94SLen Brown 1.0 * t->tsc / 355c98d5d94SLen Brown units * t->aperf / 356c98d5d94SLen Brown t->mperf / interval_float); 357103a8feaSLen Brown } 358103a8feaSLen Brown } 359103a8feaSLen Brown } 360103a8feaSLen Brown 361103a8feaSLen Brown /* TSC */ 362c98d5d94SLen Brown outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); 363103a8feaSLen Brown 364103a8feaSLen Brown if (do_nhm_cstates) { 365103a8feaSLen Brown if (!skip_c1) 366c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); 367103a8feaSLen Brown else 368c98d5d94SLen Brown outp += sprintf(outp, " ****"); 369103a8feaSLen Brown } 370c98d5d94SLen Brown 371c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 372c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 373c98d5d94SLen Brown goto done; 374c98d5d94SLen Brown 375103a8feaSLen Brown if (do_nhm_cstates) 376c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); 377103a8feaSLen Brown if (do_nhm_cstates) 378c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); 379103a8feaSLen Brown if (do_snb_cstates) 380c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); 381c98d5d94SLen Brown 382c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 383c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 384c98d5d94SLen Brown goto done; 385c98d5d94SLen Brown 386103a8feaSLen Brown if (do_snb_cstates) 387c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); 388103a8feaSLen Brown if (do_nhm_cstates) 389c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); 390103a8feaSLen Brown if (do_nhm_cstates) 391c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); 392103a8feaSLen Brown if (do_snb_cstates) 393c98d5d94SLen Brown outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); 394c98d5d94SLen Brown done: 395103a8feaSLen Brown if (extra_msr_offset) 396c98d5d94SLen Brown outp += sprintf(outp, " 0x%016llx", t->extra_msr); 397c98d5d94SLen Brown outp += sprintf(outp, "\n"); 398c98d5d94SLen Brown 399c98d5d94SLen Brown return 0; 400103a8feaSLen Brown } 401103a8feaSLen Brown 402c98d5d94SLen Brown void flush_stdout() 403103a8feaSLen Brown { 404c98d5d94SLen Brown fputs(output_buffer, stdout); 405c98d5d94SLen Brown outp = output_buffer; 406c98d5d94SLen Brown } 407c98d5d94SLen Brown void flush_stderr() 408c98d5d94SLen Brown { 409c98d5d94SLen Brown fputs(output_buffer, stderr); 410c98d5d94SLen Brown outp = output_buffer; 411c98d5d94SLen Brown } 412c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 413c98d5d94SLen Brown { 414e23da037SLen Brown static int printed; 415103a8feaSLen Brown 416e23da037SLen Brown if (!printed || !summary_only) 417103a8feaSLen Brown print_header(); 418103a8feaSLen Brown 419c98d5d94SLen Brown if (topo.num_cpus > 1) 420c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 421c98d5d94SLen Brown &average.packages); 422103a8feaSLen Brown 423e23da037SLen Brown printed = 1; 424e23da037SLen Brown 425e23da037SLen Brown if (summary_only) 426e23da037SLen Brown return; 427e23da037SLen Brown 428c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 429103a8feaSLen Brown } 430103a8feaSLen Brown 431c98d5d94SLen Brown void 432c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 433103a8feaSLen Brown { 434c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 435c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 436c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 437c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 438103a8feaSLen Brown } 439103a8feaSLen Brown 440c98d5d94SLen Brown void 441c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 442c98d5d94SLen Brown { 443c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 444c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 445c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 446103a8feaSLen Brown } 447c98d5d94SLen Brown 448c3ae331dSLen Brown /* 449c3ae331dSLen Brown * old = new - old 450c3ae331dSLen Brown */ 451c98d5d94SLen Brown void 452c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 453c98d5d94SLen Brown struct core_data *core_delta) 454c98d5d94SLen Brown { 455c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 456c98d5d94SLen Brown 457103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 458c98d5d94SLen Brown if (old->tsc < (1000 * 1000)) { 459c98d5d94SLen Brown fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n"); 460c98d5d94SLen Brown fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n"); 461c98d5d94SLen Brown fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n"); 462103a8feaSLen Brown exit(-3); 463103a8feaSLen Brown } 464103a8feaSLen Brown 465c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 466c98d5d94SLen Brown 467c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 468c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 469c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 470c98d5d94SLen Brown } else { 471c98d5d94SLen Brown 472103a8feaSLen Brown if (!aperf_mperf_unstable) { 473103a8feaSLen Brown fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 474103a8feaSLen Brown fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 475103a8feaSLen Brown fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 476103a8feaSLen Brown 477103a8feaSLen Brown aperf_mperf_unstable = 1; 478103a8feaSLen Brown } 479103a8feaSLen Brown /* 480103a8feaSLen Brown * mperf delta is likely a huge "positive" number 481103a8feaSLen Brown * can not use it for calculating c0 time 482103a8feaSLen Brown */ 483103a8feaSLen Brown skip_c0 = 1; 484103a8feaSLen Brown skip_c1 = 1; 485103a8feaSLen Brown } 486103a8feaSLen Brown 487c98d5d94SLen Brown 488103a8feaSLen Brown /* 489c3ae331dSLen Brown * As counter collection is not atomic, 490c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 491103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 492103a8feaSLen Brown */ 493c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 494c98d5d94SLen Brown old->c1 = 0; 495c98d5d94SLen Brown else { 496c98d5d94SLen Brown /* normal case, derive c1 */ 497c98d5d94SLen Brown old->c1 = old->tsc - old->mperf - core_delta->c3 498c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 499c98d5d94SLen Brown } 500c3ae331dSLen Brown 501c98d5d94SLen Brown if (old->mperf == 0) { 502c3ae331dSLen Brown if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 503c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 504c98d5d94SLen Brown } 505103a8feaSLen Brown 506103a8feaSLen Brown /* 507103a8feaSLen Brown * for "extra msr", just copy the latest w/o subtracting 508103a8feaSLen Brown */ 509c98d5d94SLen Brown old->extra_msr = new->extra_msr; 510103a8feaSLen Brown } 511c98d5d94SLen Brown 512c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 513c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 514c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 515c98d5d94SLen Brown { 516c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 517c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 518c98d5d94SLen Brown delta_core(c, c2); 519c98d5d94SLen Brown 520c98d5d94SLen Brown /* always calculate thread delta */ 521c98d5d94SLen Brown delta_thread(t, t2, c2); /* c2 is core delta */ 522c98d5d94SLen Brown 523c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 524c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 525c98d5d94SLen Brown delta_package(p, p2); 526c98d5d94SLen Brown 527103a8feaSLen Brown return 0; 528103a8feaSLen Brown } 529103a8feaSLen Brown 530c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 531103a8feaSLen Brown { 532c98d5d94SLen Brown t->tsc = 0; 533c98d5d94SLen Brown t->aperf = 0; 534c98d5d94SLen Brown t->mperf = 0; 535c98d5d94SLen Brown t->c1 = 0; 536103a8feaSLen Brown 537c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 538c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 539c98d5d94SLen Brown 540c98d5d94SLen Brown c->c3 = 0; 541c98d5d94SLen Brown c->c6 = 0; 542c98d5d94SLen Brown c->c7 = 0; 543c98d5d94SLen Brown 544c98d5d94SLen Brown p->pc2 = 0; 545c98d5d94SLen Brown p->pc3 = 0; 546c98d5d94SLen Brown p->pc6 = 0; 547c98d5d94SLen Brown p->pc7 = 0; 548103a8feaSLen Brown } 549c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 550c98d5d94SLen Brown struct pkg_data *p) 551103a8feaSLen Brown { 552c98d5d94SLen Brown average.threads.tsc += t->tsc; 553c98d5d94SLen Brown average.threads.aperf += t->aperf; 554c98d5d94SLen Brown average.threads.mperf += t->mperf; 555c98d5d94SLen Brown average.threads.c1 += t->c1; 55615aaa346SLen Brown 557c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 558c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 559c98d5d94SLen Brown return 0; 560c98d5d94SLen Brown 561c98d5d94SLen Brown average.cores.c3 += c->c3; 562c98d5d94SLen Brown average.cores.c6 += c->c6; 563c98d5d94SLen Brown average.cores.c7 += c->c7; 564c98d5d94SLen Brown 565c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 566c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 567c98d5d94SLen Brown return 0; 568c98d5d94SLen Brown 569c98d5d94SLen Brown average.packages.pc2 += p->pc2; 570c98d5d94SLen Brown average.packages.pc3 += p->pc3; 571c98d5d94SLen Brown average.packages.pc6 += p->pc6; 572c98d5d94SLen Brown average.packages.pc7 += p->pc7; 573c98d5d94SLen Brown 574c98d5d94SLen Brown return 0; 575c98d5d94SLen Brown } 576c98d5d94SLen Brown /* 577c98d5d94SLen Brown * sum the counters for all cpus in the system 578c98d5d94SLen Brown * compute the weighted average 579c98d5d94SLen Brown */ 580c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 581c98d5d94SLen Brown struct pkg_data *p) 582c98d5d94SLen Brown { 583c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 584c98d5d94SLen Brown 585c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 586c98d5d94SLen Brown 587c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 588c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 589c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 590c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 591c98d5d94SLen Brown 592c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 593c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 594c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 595c98d5d94SLen Brown 596c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 597c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 598c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 599c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 600c98d5d94SLen Brown } 601c98d5d94SLen Brown 602c98d5d94SLen Brown static unsigned long long rdtsc(void) 603c98d5d94SLen Brown { 604c98d5d94SLen Brown unsigned int low, high; 605c98d5d94SLen Brown 606c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 607c98d5d94SLen Brown 608c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 609c98d5d94SLen Brown } 610c98d5d94SLen Brown 611c98d5d94SLen Brown 612c98d5d94SLen Brown /* 613c98d5d94SLen Brown * get_counters(...) 614c98d5d94SLen Brown * migrate to cpu 615c98d5d94SLen Brown * acquire and record local counters for that cpu 616c98d5d94SLen Brown */ 617c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 618c98d5d94SLen Brown { 619c98d5d94SLen Brown int cpu = t->cpu_id; 620c98d5d94SLen Brown 621c98d5d94SLen Brown if (cpu_migrate(cpu)) 62215aaa346SLen Brown return -1; 62315aaa346SLen Brown 624c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 62515aaa346SLen Brown 62615aaa346SLen Brown if (has_aperf) { 627c98d5d94SLen Brown if (get_msr(cpu, MSR_APERF, &t->aperf)) 628c98d5d94SLen Brown return -3; 629c98d5d94SLen Brown if (get_msr(cpu, MSR_MPERF, &t->mperf)) 630c98d5d94SLen Brown return -4; 63188c3281fSLen Brown } 63288c3281fSLen Brown 633c98d5d94SLen Brown if (extra_msr_offset) 634c98d5d94SLen Brown if (get_msr(cpu, extra_msr_offset, &t->extra_msr)) 635c98d5d94SLen Brown return -5; 636c98d5d94SLen Brown 637c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 638c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 639c98d5d94SLen Brown return 0; 640c98d5d94SLen Brown 64115aaa346SLen Brown if (do_nhm_cstates) { 642c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 643c98d5d94SLen Brown return -6; 644c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 645c98d5d94SLen Brown return -7; 646103a8feaSLen Brown } 64715aaa346SLen Brown 64815aaa346SLen Brown if (do_snb_cstates) 649c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 650c98d5d94SLen Brown return -8; 651c98d5d94SLen Brown 652c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 653c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 654c98d5d94SLen Brown return 0; 65515aaa346SLen Brown 65615aaa346SLen Brown if (do_nhm_cstates) { 657c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 658c98d5d94SLen Brown return -9; 659c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 660c98d5d94SLen Brown return -10; 66115aaa346SLen Brown } 66215aaa346SLen Brown if (do_snb_cstates) { 663c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 664c98d5d94SLen Brown return -11; 665c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 666c98d5d94SLen Brown return -12; 66715aaa346SLen Brown } 66815aaa346SLen Brown return 0; 669103a8feaSLen Brown } 670103a8feaSLen Brown 671c98d5d94SLen Brown void print_verbose_header(void) 672103a8feaSLen Brown { 673103a8feaSLen Brown unsigned long long msr; 674103a8feaSLen Brown unsigned int ratio; 675103a8feaSLen Brown 676103a8feaSLen Brown if (!do_nehalem_platform_info) 677103a8feaSLen Brown return; 678103a8feaSLen Brown 67915aaa346SLen Brown get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr); 680103a8feaSLen Brown 681*6574a5d5SLen Brown if (verbose > 1) 682*6574a5d5SLen Brown fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr); 683*6574a5d5SLen Brown 684103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 685103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 686103a8feaSLen Brown ratio, bclk, ratio * bclk); 687103a8feaSLen Brown 688103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 689103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 690103a8feaSLen Brown ratio, bclk, ratio * bclk); 691103a8feaSLen Brown 692*6574a5d5SLen Brown if (!do_ivt_turbo_ratio_limit) 693*6574a5d5SLen Brown goto print_nhm_turbo_ratio_limits; 694*6574a5d5SLen Brown 695*6574a5d5SLen Brown get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 696*6574a5d5SLen Brown 697103a8feaSLen Brown if (verbose > 1) 698*6574a5d5SLen Brown fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr); 699*6574a5d5SLen Brown 700*6574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 701*6574a5d5SLen Brown if (ratio) 702*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 703*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 704*6574a5d5SLen Brown 705*6574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 706*6574a5d5SLen Brown if (ratio) 707*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 708*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 709*6574a5d5SLen Brown 710*6574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 711*6574a5d5SLen Brown if (ratio) 712*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 713*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 714*6574a5d5SLen Brown 715*6574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 716*6574a5d5SLen Brown if (ratio) 717*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 718*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 719*6574a5d5SLen Brown 720*6574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 721*6574a5d5SLen Brown if (ratio) 722*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 723*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 724*6574a5d5SLen Brown 725*6574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 726*6574a5d5SLen Brown if (ratio) 727*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 728*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 729*6574a5d5SLen Brown 730*6574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 731*6574a5d5SLen Brown if (ratio) 732*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 733*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 734*6574a5d5SLen Brown 735*6574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 736*6574a5d5SLen Brown if (ratio) 737*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 738*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 739*6574a5d5SLen Brown 740*6574a5d5SLen Brown print_nhm_turbo_ratio_limits: 741103a8feaSLen Brown 742103a8feaSLen Brown if (!do_nehalem_turbo_ratio_limit) 743103a8feaSLen Brown return; 744103a8feaSLen Brown 74515aaa346SLen Brown get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr); 746103a8feaSLen Brown 747*6574a5d5SLen Brown if (verbose > 1) 748*6574a5d5SLen Brown fprintf(stderr, "MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr); 749*6574a5d5SLen Brown 750*6574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 751*6574a5d5SLen Brown if (ratio) 752*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 753*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 754*6574a5d5SLen Brown 755*6574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 756*6574a5d5SLen Brown if (ratio) 757*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 758*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 759*6574a5d5SLen Brown 760*6574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 761*6574a5d5SLen Brown if (ratio) 762*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 763*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 764*6574a5d5SLen Brown 765*6574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 766*6574a5d5SLen Brown if (ratio) 767*6574a5d5SLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 768*6574a5d5SLen Brown ratio, bclk, ratio * bclk); 769*6574a5d5SLen Brown 770103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 771103a8feaSLen Brown if (ratio) 772103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 773103a8feaSLen Brown ratio, bclk, ratio * bclk); 774103a8feaSLen Brown 775103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 776103a8feaSLen Brown if (ratio) 777103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 778103a8feaSLen Brown ratio, bclk, ratio * bclk); 779103a8feaSLen Brown 780103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 781103a8feaSLen Brown if (ratio) 782103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 783103a8feaSLen Brown ratio, bclk, ratio * bclk); 784103a8feaSLen Brown 785103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 786103a8feaSLen Brown if (ratio) 787103a8feaSLen Brown fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 788103a8feaSLen Brown ratio, bclk, ratio * bclk); 789103a8feaSLen Brown } 790103a8feaSLen Brown 791c98d5d94SLen Brown void free_all_buffers(void) 792103a8feaSLen Brown { 793c98d5d94SLen Brown CPU_FREE(cpu_present_set); 794c98d5d94SLen Brown cpu_present_set = NULL; 795c98d5d94SLen Brown cpu_present_set = 0; 796103a8feaSLen Brown 797c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 798c98d5d94SLen Brown cpu_affinity_set = NULL; 799c98d5d94SLen Brown cpu_affinity_setsize = 0; 800103a8feaSLen Brown 801c98d5d94SLen Brown free(thread_even); 802c98d5d94SLen Brown free(core_even); 803c98d5d94SLen Brown free(package_even); 804103a8feaSLen Brown 805c98d5d94SLen Brown thread_even = NULL; 806c98d5d94SLen Brown core_even = NULL; 807c98d5d94SLen Brown package_even = NULL; 808103a8feaSLen Brown 809c98d5d94SLen Brown free(thread_odd); 810c98d5d94SLen Brown free(core_odd); 811c98d5d94SLen Brown free(package_odd); 812103a8feaSLen Brown 813c98d5d94SLen Brown thread_odd = NULL; 814c98d5d94SLen Brown core_odd = NULL; 815c98d5d94SLen Brown package_odd = NULL; 816103a8feaSLen Brown 817c98d5d94SLen Brown free(output_buffer); 818c98d5d94SLen Brown output_buffer = NULL; 819c98d5d94SLen Brown outp = NULL; 820103a8feaSLen Brown } 821103a8feaSLen Brown 822103a8feaSLen Brown /* 823c98d5d94SLen Brown * cpu_is_first_sibling_in_core(cpu) 824c98d5d94SLen Brown * return 1 if given CPU is 1st HT sibling in the core 825103a8feaSLen Brown */ 826c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu) 827103a8feaSLen Brown { 828c98d5d94SLen Brown char path[64]; 829c98d5d94SLen Brown FILE *filep; 830c98d5d94SLen Brown int first_cpu; 831103a8feaSLen Brown 832c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 833c98d5d94SLen Brown filep = fopen(path, "r"); 834c98d5d94SLen Brown if (filep == NULL) { 835c98d5d94SLen Brown perror(path); 836103a8feaSLen Brown exit(1); 837103a8feaSLen Brown } 838c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 839c98d5d94SLen Brown fclose(filep); 840c98d5d94SLen Brown return (cpu == first_cpu); 841c98d5d94SLen Brown } 842103a8feaSLen Brown 843c98d5d94SLen Brown /* 844c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 845c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 846c98d5d94SLen Brown */ 847c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 848c98d5d94SLen Brown { 849c98d5d94SLen Brown char path[64]; 850c98d5d94SLen Brown FILE *filep; 851c98d5d94SLen Brown int first_cpu; 852c98d5d94SLen Brown 853c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 854c98d5d94SLen Brown filep = fopen(path, "r"); 855c98d5d94SLen Brown if (filep == NULL) { 856c98d5d94SLen Brown perror(path); 857103a8feaSLen Brown exit(1); 858103a8feaSLen Brown } 859c98d5d94SLen Brown fscanf(filep, "%d", &first_cpu); 860c98d5d94SLen Brown fclose(filep); 861c98d5d94SLen Brown return (cpu == first_cpu); 862103a8feaSLen Brown } 863103a8feaSLen Brown 864103a8feaSLen Brown int get_physical_package_id(int cpu) 865103a8feaSLen Brown { 866c98d5d94SLen Brown char path[80]; 867103a8feaSLen Brown FILE *filep; 868103a8feaSLen Brown int pkg; 869103a8feaSLen Brown 870103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 871103a8feaSLen Brown filep = fopen(path, "r"); 872103a8feaSLen Brown if (filep == NULL) { 873103a8feaSLen Brown perror(path); 874103a8feaSLen Brown exit(1); 875103a8feaSLen Brown } 876103a8feaSLen Brown fscanf(filep, "%d", &pkg); 877103a8feaSLen Brown fclose(filep); 878103a8feaSLen Brown return pkg; 879103a8feaSLen Brown } 880103a8feaSLen Brown 881103a8feaSLen Brown int get_core_id(int cpu) 882103a8feaSLen Brown { 883c98d5d94SLen Brown char path[80]; 884103a8feaSLen Brown FILE *filep; 885103a8feaSLen Brown int core; 886103a8feaSLen Brown 887103a8feaSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 888103a8feaSLen Brown filep = fopen(path, "r"); 889103a8feaSLen Brown if (filep == NULL) { 890103a8feaSLen Brown perror(path); 891103a8feaSLen Brown exit(1); 892103a8feaSLen Brown } 893103a8feaSLen Brown fscanf(filep, "%d", &core); 894103a8feaSLen Brown fclose(filep); 895103a8feaSLen Brown return core; 896103a8feaSLen Brown } 897103a8feaSLen Brown 898c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 899c98d5d94SLen Brown { 900c98d5d94SLen Brown char path[80]; 901c98d5d94SLen Brown FILE *filep; 902c98d5d94SLen Brown int sib1, sib2; 903c98d5d94SLen Brown int matches; 904c98d5d94SLen Brown char character; 905c98d5d94SLen Brown 906c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 907c98d5d94SLen Brown filep = fopen(path, "r"); 908c98d5d94SLen Brown if (filep == NULL) { 909c98d5d94SLen Brown perror(path); 910c98d5d94SLen Brown exit(1); 911c98d5d94SLen Brown } 912103a8feaSLen Brown /* 913c98d5d94SLen Brown * file format: 914c98d5d94SLen Brown * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 915c98d5d94SLen Brown * otherwinse 1 sibling (self). 916c98d5d94SLen Brown */ 917c98d5d94SLen Brown matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 918c98d5d94SLen Brown 919c98d5d94SLen Brown fclose(filep); 920c98d5d94SLen Brown 921c98d5d94SLen Brown if (matches == 3) 922c98d5d94SLen Brown return 2; 923c98d5d94SLen Brown else 924c98d5d94SLen Brown return 1; 925c98d5d94SLen Brown } 926c98d5d94SLen Brown 927c98d5d94SLen Brown /* 928c98d5d94SLen Brown * run func(thread, core, package) in topology order 929c98d5d94SLen Brown * skip non-present cpus 930103a8feaSLen Brown */ 931103a8feaSLen Brown 932c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 933c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 934c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 935c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 936c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 937c98d5d94SLen Brown struct pkg_data *pkg_base2) 938c98d5d94SLen Brown { 939c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 940c98d5d94SLen Brown 941c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 942c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 943c98d5d94SLen Brown for (thread_no = 0; thread_no < 944c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 945c98d5d94SLen Brown struct thread_data *t, *t2; 946c98d5d94SLen Brown struct core_data *c, *c2; 947c98d5d94SLen Brown struct pkg_data *p, *p2; 948c98d5d94SLen Brown 949c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 950c98d5d94SLen Brown 951c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 952c98d5d94SLen Brown continue; 953c98d5d94SLen Brown 954c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 955c98d5d94SLen Brown 956c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 957c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 958c98d5d94SLen Brown 959c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 960c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 961c98d5d94SLen Brown 962c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 963c98d5d94SLen Brown if (retval) 964c98d5d94SLen Brown return retval; 965c98d5d94SLen Brown } 966c98d5d94SLen Brown } 967c98d5d94SLen Brown } 968c98d5d94SLen Brown return 0; 969c98d5d94SLen Brown } 970c98d5d94SLen Brown 971c98d5d94SLen Brown /* 972c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 973c98d5d94SLen Brown * return max_cpu number 974c98d5d94SLen Brown */ 975c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 976103a8feaSLen Brown { 977103a8feaSLen Brown FILE *fp; 978c98d5d94SLen Brown int cpu_num; 979103a8feaSLen Brown int retval; 980103a8feaSLen Brown 981103a8feaSLen Brown fp = fopen(proc_stat, "r"); 982103a8feaSLen Brown if (fp == NULL) { 983103a8feaSLen Brown perror(proc_stat); 984103a8feaSLen Brown exit(1); 985103a8feaSLen Brown } 986103a8feaSLen Brown 987103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 988103a8feaSLen Brown if (retval != 0) { 989103a8feaSLen Brown perror("/proc/stat format"); 990103a8feaSLen Brown exit(1); 991103a8feaSLen Brown } 992103a8feaSLen Brown 993c98d5d94SLen Brown while (1) { 994c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 995103a8feaSLen Brown if (retval != 1) 996103a8feaSLen Brown break; 997103a8feaSLen Brown 998c98d5d94SLen Brown retval = func(cpu_num); 999c98d5d94SLen Brown if (retval) { 1000c98d5d94SLen Brown fclose(fp); 1001c98d5d94SLen Brown return(retval); 1002c98d5d94SLen Brown } 1003103a8feaSLen Brown } 1004103a8feaSLen Brown fclose(fp); 1005c98d5d94SLen Brown return 0; 1006103a8feaSLen Brown } 1007103a8feaSLen Brown 1008103a8feaSLen Brown void re_initialize(void) 1009103a8feaSLen Brown { 1010c98d5d94SLen Brown free_all_buffers(); 1011c98d5d94SLen Brown setup_all_buffers(); 1012c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1013103a8feaSLen Brown } 1014103a8feaSLen Brown 1015c98d5d94SLen Brown 1016103a8feaSLen Brown /* 1017c98d5d94SLen Brown * count_cpus() 1018c98d5d94SLen Brown * remember the last one seen, it will be the max 1019103a8feaSLen Brown */ 1020c98d5d94SLen Brown int count_cpus(int cpu) 1021103a8feaSLen Brown { 1022c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 1023c98d5d94SLen Brown topo.max_cpu_num = cpu; 1024103a8feaSLen Brown 1025c98d5d94SLen Brown topo.num_cpus += 1; 1026c98d5d94SLen Brown return 0; 1027103a8feaSLen Brown } 1028c98d5d94SLen Brown int mark_cpu_present(int cpu) 1029c98d5d94SLen Brown { 1030c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 103115aaa346SLen Brown return 0; 1032103a8feaSLen Brown } 1033103a8feaSLen Brown 1034103a8feaSLen Brown void turbostat_loop() 1035103a8feaSLen Brown { 1036c98d5d94SLen Brown int retval; 1037c98d5d94SLen Brown 1038103a8feaSLen Brown restart: 1039c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1040c98d5d94SLen Brown if (retval) { 1041c98d5d94SLen Brown re_initialize(); 1042c98d5d94SLen Brown goto restart; 1043c98d5d94SLen Brown } 1044103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1045103a8feaSLen Brown 1046103a8feaSLen Brown while (1) { 1047c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 1048103a8feaSLen Brown re_initialize(); 1049103a8feaSLen Brown goto restart; 1050103a8feaSLen Brown } 1051103a8feaSLen Brown sleep(interval_sec); 1052c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 1053c98d5d94SLen Brown if (retval) { 105415aaa346SLen Brown re_initialize(); 105515aaa346SLen Brown goto restart; 105615aaa346SLen Brown } 1057103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1058103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1059c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1060c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1061c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1062c98d5d94SLen Brown flush_stdout(); 106315aaa346SLen Brown sleep(interval_sec); 1064c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1065c98d5d94SLen Brown if (retval) { 1066103a8feaSLen Brown re_initialize(); 1067103a8feaSLen Brown goto restart; 1068103a8feaSLen Brown } 1069103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1070103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 1071c98d5d94SLen Brown for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1072c98d5d94SLen Brown compute_average(ODD_COUNTERS); 1073c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 1074c98d5d94SLen Brown flush_stdout(); 1075103a8feaSLen Brown } 1076103a8feaSLen Brown } 1077103a8feaSLen Brown 1078103a8feaSLen Brown void check_dev_msr() 1079103a8feaSLen Brown { 1080103a8feaSLen Brown struct stat sb; 1081103a8feaSLen Brown 1082103a8feaSLen Brown if (stat("/dev/cpu/0/msr", &sb)) { 1083103a8feaSLen Brown fprintf(stderr, "no /dev/cpu/0/msr\n"); 1084103a8feaSLen Brown fprintf(stderr, "Try \"# modprobe msr\"\n"); 1085103a8feaSLen Brown exit(-5); 1086103a8feaSLen Brown } 1087103a8feaSLen Brown } 1088103a8feaSLen Brown 1089103a8feaSLen Brown void check_super_user() 1090103a8feaSLen Brown { 1091103a8feaSLen Brown if (getuid() != 0) { 1092103a8feaSLen Brown fprintf(stderr, "must be root\n"); 1093103a8feaSLen Brown exit(-6); 1094103a8feaSLen Brown } 1095103a8feaSLen Brown } 1096103a8feaSLen Brown 1097103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1098103a8feaSLen Brown { 1099103a8feaSLen Brown if (!genuine_intel) 1100103a8feaSLen Brown return 0; 1101103a8feaSLen Brown 1102103a8feaSLen Brown if (family != 6) 1103103a8feaSLen Brown return 0; 1104103a8feaSLen Brown 1105103a8feaSLen Brown switch (model) { 1106103a8feaSLen Brown case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1107103a8feaSLen Brown case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1108103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1109103a8feaSLen Brown case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1110103a8feaSLen Brown case 0x2C: /* Westmere EP - Gulftown */ 1111103a8feaSLen Brown case 0x2A: /* SNB */ 1112103a8feaSLen Brown case 0x2D: /* SNB Xeon */ 1113553575f1SLen Brown case 0x3A: /* IVB */ 11141300651bSLen Brown case 0x3E: /* IVB Xeon */ 1115103a8feaSLen Brown return 1; 1116103a8feaSLen Brown case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1117103a8feaSLen Brown case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1118103a8feaSLen Brown default: 1119103a8feaSLen Brown return 0; 1120103a8feaSLen Brown } 1121103a8feaSLen Brown } 1122*6574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 1123*6574a5d5SLen Brown { 1124*6574a5d5SLen Brown if (!genuine_intel) 1125*6574a5d5SLen Brown return 0; 1126*6574a5d5SLen Brown 1127*6574a5d5SLen Brown if (family != 6) 1128*6574a5d5SLen Brown return 0; 1129*6574a5d5SLen Brown 1130*6574a5d5SLen Brown switch (model) { 1131*6574a5d5SLen Brown case 0x3E: /* IVB Xeon */ 1132*6574a5d5SLen Brown return 1; 1133*6574a5d5SLen Brown default: 1134*6574a5d5SLen Brown return 0; 1135*6574a5d5SLen Brown } 1136*6574a5d5SLen Brown } 1137*6574a5d5SLen Brown 1138103a8feaSLen Brown 1139103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model) 1140103a8feaSLen Brown { 1141103a8feaSLen Brown if (!genuine_intel) 1142103a8feaSLen Brown return 0; 1143103a8feaSLen Brown 1144103a8feaSLen Brown switch (model) { 1145103a8feaSLen Brown case 0x2A: 1146103a8feaSLen Brown case 0x2D: 1147650a37f3SLen Brown case 0x3A: /* IVB */ 11481300651bSLen Brown case 0x3E: /* IVB Xeon */ 1149103a8feaSLen Brown return 1; 1150103a8feaSLen Brown } 1151103a8feaSLen Brown return 0; 1152103a8feaSLen Brown } 1153103a8feaSLen Brown 1154103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 1155103a8feaSLen Brown { 1156103a8feaSLen Brown if (is_snb(family, model)) 1157103a8feaSLen Brown return 100.00; 1158103a8feaSLen Brown else 1159103a8feaSLen Brown return 133.33; 1160103a8feaSLen Brown } 1161103a8feaSLen Brown 1162103a8feaSLen Brown void check_cpuid() 1163103a8feaSLen Brown { 1164103a8feaSLen Brown unsigned int eax, ebx, ecx, edx, max_level; 1165103a8feaSLen Brown unsigned int fms, family, model, stepping; 1166103a8feaSLen Brown 1167103a8feaSLen Brown eax = ebx = ecx = edx = 0; 1168103a8feaSLen Brown 1169103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); 1170103a8feaSLen Brown 1171103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 1172103a8feaSLen Brown genuine_intel = 1; 1173103a8feaSLen Brown 1174103a8feaSLen Brown if (verbose) 1175103a8feaSLen Brown fprintf(stderr, "%.4s%.4s%.4s ", 1176103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 1177103a8feaSLen Brown 1178103a8feaSLen Brown asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); 1179103a8feaSLen Brown family = (fms >> 8) & 0xf; 1180103a8feaSLen Brown model = (fms >> 4) & 0xf; 1181103a8feaSLen Brown stepping = fms & 0xf; 1182103a8feaSLen Brown if (family == 6 || family == 0xf) 1183103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 1184103a8feaSLen Brown 1185103a8feaSLen Brown if (verbose) 1186103a8feaSLen Brown fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 1187103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 1188103a8feaSLen Brown 1189103a8feaSLen Brown if (!(edx & (1 << 5))) { 1190103a8feaSLen Brown fprintf(stderr, "CPUID: no MSR\n"); 1191103a8feaSLen Brown exit(1); 1192103a8feaSLen Brown } 1193103a8feaSLen Brown 1194103a8feaSLen Brown /* 1195103a8feaSLen Brown * check max extended function levels of CPUID. 1196103a8feaSLen Brown * This is needed to check for invariant TSC. 1197103a8feaSLen Brown * This check is valid for both Intel and AMD. 1198103a8feaSLen Brown */ 1199103a8feaSLen Brown ebx = ecx = edx = 0; 1200103a8feaSLen Brown asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); 1201103a8feaSLen Brown 1202103a8feaSLen Brown if (max_level < 0x80000007) { 1203103a8feaSLen Brown fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); 1204103a8feaSLen Brown exit(1); 1205103a8feaSLen Brown } 1206103a8feaSLen Brown 1207103a8feaSLen Brown /* 1208103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 1209103a8feaSLen Brown * this check is valid for both Intel and AMD 1210103a8feaSLen Brown */ 1211103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 12128209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 1213103a8feaSLen Brown 1214103a8feaSLen Brown if (!has_invariant_tsc) { 1215103a8feaSLen Brown fprintf(stderr, "No invariant TSC\n"); 1216103a8feaSLen Brown exit(1); 1217103a8feaSLen Brown } 1218103a8feaSLen Brown 1219103a8feaSLen Brown /* 1220103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 1221103a8feaSLen Brown * this check is valid for both Intel and AMD 1222103a8feaSLen Brown */ 1223103a8feaSLen Brown 1224103a8feaSLen Brown asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 12258209e054SThomas Renninger has_aperf = ecx & (1 << 0); 1226103a8feaSLen Brown if (!has_aperf) { 1227103a8feaSLen Brown fprintf(stderr, "No APERF MSR\n"); 1228103a8feaSLen Brown exit(1); 1229103a8feaSLen Brown } 1230103a8feaSLen Brown 1231103a8feaSLen Brown do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 1232103a8feaSLen Brown do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 1233103a8feaSLen Brown do_snb_cstates = is_snb(family, model); 1234103a8feaSLen Brown bclk = discover_bclk(family, model); 1235103a8feaSLen Brown 1236103a8feaSLen Brown do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 1237*6574a5d5SLen Brown do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 1238103a8feaSLen Brown } 1239103a8feaSLen Brown 1240103a8feaSLen Brown 1241103a8feaSLen Brown void usage() 1242103a8feaSLen Brown { 1243103a8feaSLen Brown fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n", 1244103a8feaSLen Brown progname); 1245103a8feaSLen Brown exit(1); 1246103a8feaSLen Brown } 1247103a8feaSLen Brown 1248103a8feaSLen Brown 1249103a8feaSLen Brown /* 1250103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 1251103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 1252103a8feaSLen Brown */ 1253103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 1254103a8feaSLen Brown { 1255103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 1256103a8feaSLen Brown return 1; 1257103a8feaSLen Brown else 1258103a8feaSLen Brown return 0; 1259103a8feaSLen Brown } 1260103a8feaSLen Brown 1261103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 1262103a8feaSLen Brown { 1263103a8feaSLen Brown return 0; 1264103a8feaSLen Brown } 1265103a8feaSLen Brown 1266c98d5d94SLen Brown void topology_probe() 1267c98d5d94SLen Brown { 1268c98d5d94SLen Brown int i; 1269c98d5d94SLen Brown int max_core_id = 0; 1270c98d5d94SLen Brown int max_package_id = 0; 1271c98d5d94SLen Brown int max_siblings = 0; 1272c98d5d94SLen Brown struct cpu_topology { 1273c98d5d94SLen Brown int core_id; 1274c98d5d94SLen Brown int physical_package_id; 1275c98d5d94SLen Brown } *cpus; 1276c98d5d94SLen Brown 1277c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 1278c98d5d94SLen Brown topo.num_cpus = 0; 1279c98d5d94SLen Brown topo.max_cpu_num = 0; 1280c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 1281c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 1282c98d5d94SLen Brown show_cpu = 1; 1283c98d5d94SLen Brown 1284c98d5d94SLen Brown if (verbose > 1) 1285c98d5d94SLen Brown fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 1286c98d5d94SLen Brown 1287c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 1288c98d5d94SLen Brown if (cpus == NULL) { 1289c98d5d94SLen Brown perror("calloc cpus"); 1290c98d5d94SLen Brown exit(1); 1291c98d5d94SLen Brown } 1292c98d5d94SLen Brown 1293c98d5d94SLen Brown /* 1294c98d5d94SLen Brown * Allocate and initialize cpu_present_set 1295c98d5d94SLen Brown */ 1296c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1297c98d5d94SLen Brown if (cpu_present_set == NULL) { 1298c98d5d94SLen Brown perror("CPU_ALLOC"); 1299c98d5d94SLen Brown exit(3); 1300c98d5d94SLen Brown } 1301c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 1302c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 1303c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 1304c98d5d94SLen Brown 1305c98d5d94SLen Brown /* 1306c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 1307c98d5d94SLen Brown */ 1308c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 1309c98d5d94SLen Brown if (cpu_affinity_set == NULL) { 1310c98d5d94SLen Brown perror("CPU_ALLOC"); 1311c98d5d94SLen Brown exit(3); 1312c98d5d94SLen Brown } 1313c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 1314c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 1315c98d5d94SLen Brown 1316c98d5d94SLen Brown 1317c98d5d94SLen Brown /* 1318c98d5d94SLen Brown * For online cpus 1319c98d5d94SLen Brown * find max_core_id, max_package_id 1320c98d5d94SLen Brown */ 1321c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 1322c98d5d94SLen Brown int siblings; 1323c98d5d94SLen Brown 1324c98d5d94SLen Brown if (cpu_is_not_present(i)) { 1325c98d5d94SLen Brown if (verbose > 1) 1326c98d5d94SLen Brown fprintf(stderr, "cpu%d NOT PRESENT\n", i); 1327c98d5d94SLen Brown continue; 1328c98d5d94SLen Brown } 1329c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 1330c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 1331c98d5d94SLen Brown max_core_id = cpus[i].core_id; 1332c98d5d94SLen Brown 1333c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 1334c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 1335c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 1336c98d5d94SLen Brown 1337c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 1338c98d5d94SLen Brown if (siblings > max_siblings) 1339c98d5d94SLen Brown max_siblings = siblings; 1340c98d5d94SLen Brown if (verbose > 1) 1341c98d5d94SLen Brown fprintf(stderr, "cpu %d pkg %d core %d\n", 1342c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 1343c98d5d94SLen Brown } 1344c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 1345c98d5d94SLen Brown if (verbose > 1) 1346c98d5d94SLen Brown fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 1347c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 1348c98d5d94SLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 1349c98d5d94SLen Brown show_core = 1; 1350c98d5d94SLen Brown 1351c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 1352c98d5d94SLen Brown if (verbose > 1) 1353c98d5d94SLen Brown fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 1354c98d5d94SLen Brown max_package_id, topo.num_packages); 1355c98d5d94SLen Brown if (!summary_only && topo.num_packages > 1) 1356c98d5d94SLen Brown show_pkg = 1; 1357c98d5d94SLen Brown 1358c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 1359c98d5d94SLen Brown if (verbose > 1) 1360c98d5d94SLen Brown fprintf(stderr, "max_siblings %d\n", max_siblings); 1361c98d5d94SLen Brown 1362c98d5d94SLen Brown free(cpus); 1363c98d5d94SLen Brown } 1364c98d5d94SLen Brown 1365c98d5d94SLen Brown void 1366c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 1367c98d5d94SLen Brown { 1368c98d5d94SLen Brown int i; 1369c98d5d94SLen Brown 1370c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 1371c98d5d94SLen Brown topo.num_packages, sizeof(struct thread_data)); 1372c98d5d94SLen Brown if (*t == NULL) 1373c98d5d94SLen Brown goto error; 1374c98d5d94SLen Brown 1375c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 1376c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 1377c98d5d94SLen Brown (*t)[i].cpu_id = -1; 1378c98d5d94SLen Brown 1379c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 1380c98d5d94SLen Brown sizeof(struct core_data)); 1381c98d5d94SLen Brown if (*c == NULL) 1382c98d5d94SLen Brown goto error; 1383c98d5d94SLen Brown 1384c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 1385c98d5d94SLen Brown (*c)[i].core_id = -1; 1386c98d5d94SLen Brown 1387c98d5d94SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 1388c98d5d94SLen Brown if (*p == NULL) 1389c98d5d94SLen Brown goto error; 1390c98d5d94SLen Brown 1391c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 1392c98d5d94SLen Brown (*p)[i].package_id = i; 1393c98d5d94SLen Brown 1394c98d5d94SLen Brown return; 1395c98d5d94SLen Brown error: 1396c98d5d94SLen Brown perror("calloc counters"); 1397c98d5d94SLen Brown exit(1); 1398c98d5d94SLen Brown } 1399c98d5d94SLen Brown /* 1400c98d5d94SLen Brown * init_counter() 1401c98d5d94SLen Brown * 1402c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 1403c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 1404c98d5d94SLen Brown * 1405c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 1406c98d5d94SLen Brown */ 1407c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 1408c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 1409c98d5d94SLen Brown int pkg_num, int cpu_id) 1410c98d5d94SLen Brown { 1411c98d5d94SLen Brown struct thread_data *t; 1412c98d5d94SLen Brown struct core_data *c; 1413c98d5d94SLen Brown struct pkg_data *p; 1414c98d5d94SLen Brown 1415c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 1416c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 1417c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 1418c98d5d94SLen Brown 1419c98d5d94SLen Brown t->cpu_id = cpu_id; 1420c98d5d94SLen Brown if (thread_num == 0) { 1421c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 1422c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 1423c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 1424c98d5d94SLen Brown } 1425c98d5d94SLen Brown 1426c98d5d94SLen Brown c->core_id = core_num; 1427c98d5d94SLen Brown p->package_id = pkg_num; 1428c98d5d94SLen Brown } 1429c98d5d94SLen Brown 1430c98d5d94SLen Brown 1431c98d5d94SLen Brown int initialize_counters(int cpu_id) 1432c98d5d94SLen Brown { 1433c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 1434c98d5d94SLen Brown 1435c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 1436c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 1437c98d5d94SLen Brown 1438c98d5d94SLen Brown if (cpu_is_first_sibling_in_core(cpu_id)) { 1439c98d5d94SLen Brown my_thread_id = 0; 1440c98d5d94SLen Brown topo.num_cores++; 1441c98d5d94SLen Brown } else { 1442c98d5d94SLen Brown my_thread_id = 1; 1443c98d5d94SLen Brown } 1444c98d5d94SLen Brown 1445c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 1446c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 1447c98d5d94SLen Brown return 0; 1448c98d5d94SLen Brown } 1449c98d5d94SLen Brown 1450c98d5d94SLen Brown void allocate_output_buffer() 1451c98d5d94SLen Brown { 1452c98d5d94SLen Brown output_buffer = calloc(1, (1 + topo.num_cpus) * 128); 1453c98d5d94SLen Brown outp = output_buffer; 1454c98d5d94SLen Brown if (outp == NULL) { 1455c98d5d94SLen Brown perror("calloc"); 1456c98d5d94SLen Brown exit(-1); 1457c98d5d94SLen Brown } 1458c98d5d94SLen Brown } 1459c98d5d94SLen Brown 1460c98d5d94SLen Brown void setup_all_buffers(void) 1461c98d5d94SLen Brown { 1462c98d5d94SLen Brown topology_probe(); 1463c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 1464c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 1465c98d5d94SLen Brown allocate_output_buffer(); 1466c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 1467c98d5d94SLen Brown } 1468103a8feaSLen Brown void turbostat_init() 1469103a8feaSLen Brown { 1470103a8feaSLen Brown check_cpuid(); 1471103a8feaSLen Brown 1472103a8feaSLen Brown check_dev_msr(); 1473103a8feaSLen Brown check_super_user(); 1474103a8feaSLen Brown 1475c98d5d94SLen Brown setup_all_buffers(); 1476103a8feaSLen Brown 1477103a8feaSLen Brown if (verbose) 1478c98d5d94SLen Brown print_verbose_header(); 1479103a8feaSLen Brown } 1480103a8feaSLen Brown 1481103a8feaSLen Brown int fork_it(char **argv) 1482103a8feaSLen Brown { 1483103a8feaSLen Brown pid_t child_pid; 1484d15cf7c1SLen Brown 1485c98d5d94SLen Brown for_all_cpus(get_counters, EVEN_COUNTERS); 1486d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 1487d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 1488103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 1489103a8feaSLen Brown 1490103a8feaSLen Brown child_pid = fork(); 1491103a8feaSLen Brown if (!child_pid) { 1492103a8feaSLen Brown /* child */ 1493103a8feaSLen Brown execvp(argv[0], argv); 1494103a8feaSLen Brown } else { 1495103a8feaSLen Brown int status; 1496103a8feaSLen Brown 1497103a8feaSLen Brown /* parent */ 1498103a8feaSLen Brown if (child_pid == -1) { 1499103a8feaSLen Brown perror("fork"); 1500103a8feaSLen Brown exit(1); 1501103a8feaSLen Brown } 1502103a8feaSLen Brown 1503103a8feaSLen Brown signal(SIGINT, SIG_IGN); 1504103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 1505103a8feaSLen Brown if (waitpid(child_pid, &status, 0) == -1) { 1506103a8feaSLen Brown perror("wait"); 1507103a8feaSLen Brown exit(1); 1508103a8feaSLen Brown } 1509103a8feaSLen Brown } 1510c98d5d94SLen Brown /* 1511c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 1512c98d5d94SLen Brown * because re-starting is problematic when forking 1513c98d5d94SLen Brown */ 1514c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 1515103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 1516103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 1517c98d5d94SLen Brown for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1518c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 1519c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 1520c98d5d94SLen Brown flush_stderr(); 1521103a8feaSLen Brown 15226eab04a8SJustin P. Mattock fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 1523103a8feaSLen Brown 1524103a8feaSLen Brown return 0; 1525103a8feaSLen Brown } 1526103a8feaSLen Brown 1527103a8feaSLen Brown void cmdline(int argc, char **argv) 1528103a8feaSLen Brown { 1529103a8feaSLen Brown int opt; 1530103a8feaSLen Brown 1531103a8feaSLen Brown progname = argv[0]; 1532103a8feaSLen Brown 1533c98d5d94SLen Brown while ((opt = getopt(argc, argv, "+cpsvi:M:")) != -1) { 1534103a8feaSLen Brown switch (opt) { 1535c98d5d94SLen Brown case 'c': 1536c98d5d94SLen Brown show_core_only++; 1537c98d5d94SLen Brown break; 1538c98d5d94SLen Brown case 'p': 1539c98d5d94SLen Brown show_pkg_only++; 1540c98d5d94SLen Brown break; 1541e23da037SLen Brown case 's': 1542e23da037SLen Brown summary_only++; 1543e23da037SLen Brown break; 1544103a8feaSLen Brown case 'v': 1545103a8feaSLen Brown verbose++; 1546103a8feaSLen Brown break; 1547103a8feaSLen Brown case 'i': 1548103a8feaSLen Brown interval_sec = atoi(optarg); 1549103a8feaSLen Brown break; 1550103a8feaSLen Brown case 'M': 1551103a8feaSLen Brown sscanf(optarg, "%x", &extra_msr_offset); 1552103a8feaSLen Brown if (verbose > 1) 1553103a8feaSLen Brown fprintf(stderr, "MSR 0x%X\n", extra_msr_offset); 1554103a8feaSLen Brown break; 1555103a8feaSLen Brown default: 1556103a8feaSLen Brown usage(); 1557103a8feaSLen Brown } 1558103a8feaSLen Brown } 1559103a8feaSLen Brown } 1560103a8feaSLen Brown 1561103a8feaSLen Brown int main(int argc, char **argv) 1562103a8feaSLen Brown { 1563103a8feaSLen Brown cmdline(argc, argv); 1564103a8feaSLen Brown 1565103a8feaSLen Brown if (verbose > 1) 1566c98d5d94SLen Brown fprintf(stderr, "turbostat v2.0 May 16, 2012" 1567103a8feaSLen Brown " - Len Brown <lenb@kernel.org>\n"); 1568103a8feaSLen Brown 1569103a8feaSLen Brown turbostat_init(); 1570103a8feaSLen Brown 1571103a8feaSLen Brown /* 1572103a8feaSLen Brown * if any params left, it must be a command to fork 1573103a8feaSLen Brown */ 1574103a8feaSLen Brown if (argc - optind) 1575103a8feaSLen Brown return fork_it(argv + optind); 1576103a8feaSLen Brown else 1577103a8feaSLen Brown turbostat_loop(); 1578103a8feaSLen Brown 1579103a8feaSLen Brown return 0; 1580103a8feaSLen Brown } 1581