1 /* 2 * turbostat -- show CPU frequency and C-state residency 3 * on modern Intel turbo-capable processors. 4 * 5 * Copyright (c) 2013 Intel Corporation. 6 * Len Brown <len.brown@intel.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms and conditions of the GNU General Public License, 10 * version 2, as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 20 */ 21 22 #define _GNU_SOURCE 23 #include MSRHEADER 24 #include <stdarg.h> 25 #include <stdio.h> 26 #include <err.h> 27 #include <unistd.h> 28 #include <sys/types.h> 29 #include <sys/wait.h> 30 #include <sys/stat.h> 31 #include <sys/resource.h> 32 #include <fcntl.h> 33 #include <signal.h> 34 #include <sys/time.h> 35 #include <stdlib.h> 36 #include <dirent.h> 37 #include <string.h> 38 #include <ctype.h> 39 #include <sched.h> 40 #include <cpuid.h> 41 42 char *proc_stat = "/proc/stat"; 43 unsigned int interval_sec = 5; /* set with -i interval_sec */ 44 unsigned int verbose; /* set with -v */ 45 unsigned int rapl_verbose; /* set with -R */ 46 unsigned int rapl_joules; /* set with -J */ 47 unsigned int thermal_verbose; /* set with -T */ 48 unsigned int summary_only; /* set with -S */ 49 unsigned int dump_only; /* set with -s */ 50 unsigned int skip_c0; 51 unsigned int skip_c1; 52 unsigned int do_nhm_cstates; 53 unsigned int do_snb_cstates; 54 unsigned int do_c8_c9_c10; 55 unsigned int do_slm_cstates; 56 unsigned int use_c1_residency_msr; 57 unsigned int has_aperf; 58 unsigned int has_epb; 59 unsigned int units = 1000000000; /* Ghz etc */ 60 unsigned int genuine_intel; 61 unsigned int has_invariant_tsc; 62 unsigned int do_nehalem_platform_info; 63 unsigned int do_nehalem_turbo_ratio_limit; 64 unsigned int do_ivt_turbo_ratio_limit; 65 unsigned int extra_msr_offset32; 66 unsigned int extra_msr_offset64; 67 unsigned int extra_delta_offset32; 68 unsigned int extra_delta_offset64; 69 int do_smi; 70 double bclk; 71 unsigned int show_pkg; 72 unsigned int show_core; 73 unsigned int show_cpu; 74 unsigned int show_pkg_only; 75 unsigned int show_core_only; 76 char *output_buffer, *outp; 77 unsigned int do_rapl; 78 unsigned int do_dts; 79 unsigned int do_ptm; 80 unsigned int tcc_activation_temp; 81 unsigned int tcc_activation_temp_override; 82 double rapl_power_units, rapl_energy_units, rapl_time_units; 83 double rapl_joule_counter_range; 84 85 #define RAPL_PKG (1 << 0) 86 /* 0x610 MSR_PKG_POWER_LIMIT */ 87 /* 0x611 MSR_PKG_ENERGY_STATUS */ 88 #define RAPL_PKG_PERF_STATUS (1 << 1) 89 /* 0x613 MSR_PKG_PERF_STATUS */ 90 #define RAPL_PKG_POWER_INFO (1 << 2) 91 /* 0x614 MSR_PKG_POWER_INFO */ 92 93 #define RAPL_DRAM (1 << 3) 94 /* 0x618 MSR_DRAM_POWER_LIMIT */ 95 /* 0x619 MSR_DRAM_ENERGY_STATUS */ 96 /* 0x61c MSR_DRAM_POWER_INFO */ 97 #define RAPL_DRAM_PERF_STATUS (1 << 4) 98 /* 0x61b MSR_DRAM_PERF_STATUS */ 99 100 #define RAPL_CORES (1 << 5) 101 /* 0x638 MSR_PP0_POWER_LIMIT */ 102 /* 0x639 MSR_PP0_ENERGY_STATUS */ 103 #define RAPL_CORE_POLICY (1 << 6) 104 /* 0x63a MSR_PP0_POLICY */ 105 106 107 #define RAPL_GFX (1 << 7) 108 /* 0x640 MSR_PP1_POWER_LIMIT */ 109 /* 0x641 MSR_PP1_ENERGY_STATUS */ 110 /* 0x642 MSR_PP1_POLICY */ 111 #define TJMAX_DEFAULT 100 112 113 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 114 115 int aperf_mperf_unstable; 116 int backwards_count; 117 char *progname; 118 119 cpu_set_t *cpu_present_set, *cpu_affinity_set; 120 size_t cpu_present_setsize, cpu_affinity_setsize; 121 122 struct thread_data { 123 unsigned long long tsc; 124 unsigned long long aperf; 125 unsigned long long mperf; 126 unsigned long long c1; 127 unsigned long long extra_msr64; 128 unsigned long long extra_delta64; 129 unsigned long long extra_msr32; 130 unsigned long long extra_delta32; 131 unsigned int smi_count; 132 unsigned int cpu_id; 133 unsigned int flags; 134 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 135 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 136 } *thread_even, *thread_odd; 137 138 struct core_data { 139 unsigned long long c3; 140 unsigned long long c6; 141 unsigned long long c7; 142 unsigned int core_temp_c; 143 unsigned int core_id; 144 } *core_even, *core_odd; 145 146 struct pkg_data { 147 unsigned long long pc2; 148 unsigned long long pc3; 149 unsigned long long pc6; 150 unsigned long long pc7; 151 unsigned long long pc8; 152 unsigned long long pc9; 153 unsigned long long pc10; 154 unsigned int package_id; 155 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 156 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 157 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 158 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 159 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 160 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 161 unsigned int pkg_temp_c; 162 163 } *package_even, *package_odd; 164 165 #define ODD_COUNTERS thread_odd, core_odd, package_odd 166 #define EVEN_COUNTERS thread_even, core_even, package_even 167 168 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 169 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 170 topo.num_threads_per_core + \ 171 (core_no) * topo.num_threads_per_core + (thread_no)) 172 #define GET_CORE(core_base, core_no, pkg_no) \ 173 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 174 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 175 176 struct system_summary { 177 struct thread_data threads; 178 struct core_data cores; 179 struct pkg_data packages; 180 } sum, average; 181 182 183 struct topo_params { 184 int num_packages; 185 int num_cpus; 186 int num_cores; 187 int max_cpu_num; 188 int num_cores_per_pkg; 189 int num_threads_per_core; 190 } topo; 191 192 struct timeval tv_even, tv_odd, tv_delta; 193 194 void setup_all_buffers(void); 195 196 int cpu_is_not_present(int cpu) 197 { 198 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 199 } 200 /* 201 * run func(thread, core, package) in topology order 202 * skip non-present cpus 203 */ 204 205 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 206 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 207 { 208 int retval, pkg_no, core_no, thread_no; 209 210 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 211 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 212 for (thread_no = 0; thread_no < 213 topo.num_threads_per_core; ++thread_no) { 214 struct thread_data *t; 215 struct core_data *c; 216 struct pkg_data *p; 217 218 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 219 220 if (cpu_is_not_present(t->cpu_id)) 221 continue; 222 223 c = GET_CORE(core_base, core_no, pkg_no); 224 p = GET_PKG(pkg_base, pkg_no); 225 226 retval = func(t, c, p); 227 if (retval) 228 return retval; 229 } 230 } 231 } 232 return 0; 233 } 234 235 int cpu_migrate(int cpu) 236 { 237 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 238 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 239 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 240 return -1; 241 else 242 return 0; 243 } 244 245 int get_msr(int cpu, off_t offset, unsigned long long *msr) 246 { 247 ssize_t retval; 248 char pathname[32]; 249 int fd; 250 251 sprintf(pathname, "/dev/cpu/%d/msr", cpu); 252 fd = open(pathname, O_RDONLY); 253 if (fd < 0) 254 return -1; 255 256 retval = pread(fd, msr, sizeof *msr, offset); 257 close(fd); 258 259 if (retval != sizeof *msr) { 260 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset); 261 return -1; 262 } 263 264 return 0; 265 } 266 267 void print_header(void) 268 { 269 if (show_pkg) 270 outp += sprintf(outp, "pk"); 271 if (show_pkg) 272 outp += sprintf(outp, " "); 273 if (show_core) 274 outp += sprintf(outp, "cor"); 275 if (show_cpu) 276 outp += sprintf(outp, " CPU"); 277 if (show_pkg || show_core || show_cpu) 278 outp += sprintf(outp, " "); 279 if (do_nhm_cstates) 280 outp += sprintf(outp, " %%c0"); 281 if (has_aperf) 282 outp += sprintf(outp, " GHz"); 283 outp += sprintf(outp, " TSC"); 284 if (do_smi) 285 outp += sprintf(outp, " SMI"); 286 if (extra_delta_offset32) 287 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); 288 if (extra_delta_offset64) 289 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); 290 if (extra_msr_offset32) 291 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); 292 if (extra_msr_offset64) 293 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); 294 if (do_nhm_cstates) 295 outp += sprintf(outp, " %%c1"); 296 if (do_nhm_cstates && !do_slm_cstates) 297 outp += sprintf(outp, " %%c3"); 298 if (do_nhm_cstates) 299 outp += sprintf(outp, " %%c6"); 300 if (do_snb_cstates) 301 outp += sprintf(outp, " %%c7"); 302 303 if (do_dts) 304 outp += sprintf(outp, " CTMP"); 305 if (do_ptm) 306 outp += sprintf(outp, " PTMP"); 307 308 if (do_snb_cstates) 309 outp += sprintf(outp, " %%pc2"); 310 if (do_nhm_cstates && !do_slm_cstates) 311 outp += sprintf(outp, " %%pc3"); 312 if (do_nhm_cstates && !do_slm_cstates) 313 outp += sprintf(outp, " %%pc6"); 314 if (do_snb_cstates) 315 outp += sprintf(outp, " %%pc7"); 316 if (do_c8_c9_c10) { 317 outp += sprintf(outp, " %%pc8"); 318 outp += sprintf(outp, " %%pc9"); 319 outp += sprintf(outp, " %%pc10"); 320 } 321 322 if (do_rapl && !rapl_joules) { 323 if (do_rapl & RAPL_PKG) 324 outp += sprintf(outp, " Pkg_W"); 325 if (do_rapl & RAPL_CORES) 326 outp += sprintf(outp, " Cor_W"); 327 if (do_rapl & RAPL_GFX) 328 outp += sprintf(outp, " GFX_W"); 329 if (do_rapl & RAPL_DRAM) 330 outp += sprintf(outp, " RAM_W"); 331 if (do_rapl & RAPL_PKG_PERF_STATUS) 332 outp += sprintf(outp, " PKG_%%"); 333 if (do_rapl & RAPL_DRAM_PERF_STATUS) 334 outp += sprintf(outp, " RAM_%%"); 335 } else { 336 if (do_rapl & RAPL_PKG) 337 outp += sprintf(outp, " Pkg_J"); 338 if (do_rapl & RAPL_CORES) 339 outp += sprintf(outp, " Cor_J"); 340 if (do_rapl & RAPL_GFX) 341 outp += sprintf(outp, " GFX_J"); 342 if (do_rapl & RAPL_DRAM) 343 outp += sprintf(outp, " RAM_W"); 344 if (do_rapl & RAPL_PKG_PERF_STATUS) 345 outp += sprintf(outp, " PKG_%%"); 346 if (do_rapl & RAPL_DRAM_PERF_STATUS) 347 outp += sprintf(outp, " RAM_%%"); 348 outp += sprintf(outp, " time"); 349 350 } 351 outp += sprintf(outp, "\n"); 352 } 353 354 int dump_counters(struct thread_data *t, struct core_data *c, 355 struct pkg_data *p) 356 { 357 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 358 359 if (t) { 360 outp += sprintf(outp, "CPU: %d flags 0x%x\n", 361 t->cpu_id, t->flags); 362 outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 363 outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 364 outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 365 outp += sprintf(outp, "c1: %016llX\n", t->c1); 366 outp += sprintf(outp, "msr0x%x: %08llX\n", 367 extra_delta_offset32, t->extra_delta32); 368 outp += sprintf(outp, "msr0x%x: %016llX\n", 369 extra_delta_offset64, t->extra_delta64); 370 outp += sprintf(outp, "msr0x%x: %08llX\n", 371 extra_msr_offset32, t->extra_msr32); 372 outp += sprintf(outp, "msr0x%x: %016llX\n", 373 extra_msr_offset64, t->extra_msr64); 374 if (do_smi) 375 outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 376 } 377 378 if (c) { 379 outp += sprintf(outp, "core: %d\n", c->core_id); 380 outp += sprintf(outp, "c3: %016llX\n", c->c3); 381 outp += sprintf(outp, "c6: %016llX\n", c->c6); 382 outp += sprintf(outp, "c7: %016llX\n", c->c7); 383 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 384 } 385 386 if (p) { 387 outp += sprintf(outp, "package: %d\n", p->package_id); 388 outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 389 outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 390 outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 391 outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 392 outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 393 outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 394 outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 395 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 396 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 397 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 398 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 399 outp += sprintf(outp, "Throttle PKG: %0X\n", 400 p->rapl_pkg_perf_status); 401 outp += sprintf(outp, "Throttle RAM: %0X\n", 402 p->rapl_dram_perf_status); 403 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 404 } 405 406 outp += sprintf(outp, "\n"); 407 408 return 0; 409 } 410 411 /* 412 * column formatting convention & formats 413 * package: "pk" 2 columns %2d 414 * core: "cor" 3 columns %3d 415 * CPU: "CPU" 3 columns %3d 416 * Pkg_W: %6.2 417 * Cor_W: %6.2 418 * GFX_W: %5.2 419 * RAM_W: %5.2 420 * GHz: "GHz" 3 columns %3.2 421 * TSC: "TSC" 3 columns %3.2 422 * SMI: "SMI" 4 columns %4d 423 * percentage " %pc3" %6.2 424 * Perf Status percentage: %5.2 425 * "CTMP" 4 columns %4d 426 */ 427 int format_counters(struct thread_data *t, struct core_data *c, 428 struct pkg_data *p) 429 { 430 double interval_float; 431 char *fmt5, *fmt6; 432 433 /* if showing only 1st thread in core and this isn't one, bail out */ 434 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 435 return 0; 436 437 /* if showing only 1st thread in pkg and this isn't one, bail out */ 438 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 439 return 0; 440 441 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 442 443 /* topo columns, print blanks on 1st (average) line */ 444 if (t == &average.threads) { 445 if (show_pkg) 446 outp += sprintf(outp, " "); 447 if (show_pkg && show_core) 448 outp += sprintf(outp, " "); 449 if (show_core) 450 outp += sprintf(outp, " "); 451 if (show_cpu) 452 outp += sprintf(outp, " " " "); 453 } else { 454 if (show_pkg) { 455 if (p) 456 outp += sprintf(outp, "%2d", p->package_id); 457 else 458 outp += sprintf(outp, " "); 459 } 460 if (show_pkg && show_core) 461 outp += sprintf(outp, " "); 462 if (show_core) { 463 if (c) 464 outp += sprintf(outp, "%3d", c->core_id); 465 else 466 outp += sprintf(outp, " "); 467 } 468 if (show_cpu) 469 outp += sprintf(outp, " %3d", t->cpu_id); 470 } 471 /* %c0 */ 472 if (do_nhm_cstates) { 473 if (show_pkg || show_core || show_cpu) 474 outp += sprintf(outp, " "); 475 if (!skip_c0) 476 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); 477 else 478 outp += sprintf(outp, " ****"); 479 } 480 481 /* GHz */ 482 if (has_aperf) { 483 if (!aperf_mperf_unstable) { 484 outp += sprintf(outp, " %3.2f", 485 1.0 * t->tsc / units * t->aperf / 486 t->mperf / interval_float); 487 } else { 488 if (t->aperf > t->tsc || t->mperf > t->tsc) { 489 outp += sprintf(outp, " ***"); 490 } else { 491 outp += sprintf(outp, "%3.1f*", 492 1.0 * t->tsc / 493 units * t->aperf / 494 t->mperf / interval_float); 495 } 496 } 497 } 498 499 /* TSC */ 500 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); 501 502 /* SMI */ 503 if (do_smi) 504 outp += sprintf(outp, "%4d", t->smi_count); 505 506 /* delta */ 507 if (extra_delta_offset32) 508 outp += sprintf(outp, " %11llu", t->extra_delta32); 509 510 /* DELTA */ 511 if (extra_delta_offset64) 512 outp += sprintf(outp, " %11llu", t->extra_delta64); 513 /* msr */ 514 if (extra_msr_offset32) 515 outp += sprintf(outp, " 0x%08llx", t->extra_msr32); 516 517 /* MSR */ 518 if (extra_msr_offset64) 519 outp += sprintf(outp, " 0x%016llx", t->extra_msr64); 520 521 if (do_nhm_cstates) { 522 if (!skip_c1) 523 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); 524 else 525 outp += sprintf(outp, " ****"); 526 } 527 528 /* print per-core data only for 1st thread in core */ 529 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 530 goto done; 531 532 if (do_nhm_cstates && !do_slm_cstates) 533 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); 534 if (do_nhm_cstates) 535 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); 536 if (do_snb_cstates) 537 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); 538 539 if (do_dts) 540 outp += sprintf(outp, " %4d", c->core_temp_c); 541 542 /* print per-package data only for 1st core in package */ 543 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 544 goto done; 545 546 if (do_ptm) 547 outp += sprintf(outp, " %4d", p->pkg_temp_c); 548 549 if (do_snb_cstates) 550 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); 551 if (do_nhm_cstates && !do_slm_cstates) 552 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); 553 if (do_nhm_cstates && !do_slm_cstates) 554 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); 555 if (do_snb_cstates) 556 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); 557 if (do_c8_c9_c10) { 558 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc); 559 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc); 560 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc); 561 } 562 563 /* 564 * If measurement interval exceeds minimum RAPL Joule Counter range, 565 * indicate that results are suspect by printing "**" in fraction place. 566 */ 567 if (interval_float < rapl_joule_counter_range) { 568 fmt5 = " %5.2f"; 569 fmt6 = " %6.2f"; 570 } else { 571 fmt5 = " %3.0f**"; 572 fmt6 = " %4.0f**"; 573 } 574 575 if (do_rapl && !rapl_joules) { 576 if (do_rapl & RAPL_PKG) 577 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float); 578 if (do_rapl & RAPL_CORES) 579 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float); 580 if (do_rapl & RAPL_GFX) 581 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float); 582 if (do_rapl & RAPL_DRAM) 583 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float); 584 if (do_rapl & RAPL_PKG_PERF_STATUS) 585 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 586 if (do_rapl & RAPL_DRAM_PERF_STATUS) 587 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 588 } else { 589 if (do_rapl & RAPL_PKG) 590 outp += sprintf(outp, fmt6, 591 p->energy_pkg * rapl_energy_units); 592 if (do_rapl & RAPL_CORES) 593 outp += sprintf(outp, fmt6, 594 p->energy_cores * rapl_energy_units); 595 if (do_rapl & RAPL_GFX) 596 outp += sprintf(outp, fmt5, 597 p->energy_gfx * rapl_energy_units); 598 if (do_rapl & RAPL_DRAM) 599 outp += sprintf(outp, fmt5, 600 p->energy_dram * rapl_energy_units); 601 if (do_rapl & RAPL_PKG_PERF_STATUS) 602 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 603 if (do_rapl & RAPL_DRAM_PERF_STATUS) 604 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 605 outp += sprintf(outp, fmt5, interval_float); 606 607 } 608 done: 609 outp += sprintf(outp, "\n"); 610 611 return 0; 612 } 613 614 void flush_stdout() 615 { 616 fputs(output_buffer, stdout); 617 fflush(stdout); 618 outp = output_buffer; 619 } 620 void flush_stderr() 621 { 622 fputs(output_buffer, stderr); 623 outp = output_buffer; 624 } 625 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 626 { 627 static int printed; 628 629 if (!printed || !summary_only) 630 print_header(); 631 632 if (topo.num_cpus > 1) 633 format_counters(&average.threads, &average.cores, 634 &average.packages); 635 636 printed = 1; 637 638 if (summary_only) 639 return; 640 641 for_all_cpus(format_counters, t, c, p); 642 } 643 644 #define DELTA_WRAP32(new, old) \ 645 if (new > old) { \ 646 old = new - old; \ 647 } else { \ 648 old = 0x100000000 + new - old; \ 649 } 650 651 void 652 delta_package(struct pkg_data *new, struct pkg_data *old) 653 { 654 old->pc2 = new->pc2 - old->pc2; 655 old->pc3 = new->pc3 - old->pc3; 656 old->pc6 = new->pc6 - old->pc6; 657 old->pc7 = new->pc7 - old->pc7; 658 old->pc8 = new->pc8 - old->pc8; 659 old->pc9 = new->pc9 - old->pc9; 660 old->pc10 = new->pc10 - old->pc10; 661 old->pkg_temp_c = new->pkg_temp_c; 662 663 DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 664 DELTA_WRAP32(new->energy_cores, old->energy_cores); 665 DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 666 DELTA_WRAP32(new->energy_dram, old->energy_dram); 667 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 668 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 669 } 670 671 void 672 delta_core(struct core_data *new, struct core_data *old) 673 { 674 old->c3 = new->c3 - old->c3; 675 old->c6 = new->c6 - old->c6; 676 old->c7 = new->c7 - old->c7; 677 old->core_temp_c = new->core_temp_c; 678 } 679 680 /* 681 * old = new - old 682 */ 683 void 684 delta_thread(struct thread_data *new, struct thread_data *old, 685 struct core_data *core_delta) 686 { 687 old->tsc = new->tsc - old->tsc; 688 689 /* check for TSC < 1 Mcycles over interval */ 690 if (old->tsc < (1000 * 1000)) 691 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 692 "You can disable all c-states by booting with \"idle=poll\"\n" 693 "or just the deep ones with \"processor.max_cstate=1\""); 694 695 old->c1 = new->c1 - old->c1; 696 697 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 698 old->aperf = new->aperf - old->aperf; 699 old->mperf = new->mperf - old->mperf; 700 } else { 701 702 if (!aperf_mperf_unstable) { 703 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); 704 fprintf(stderr, "* Frequency results do not cover entire interval *\n"); 705 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); 706 707 aperf_mperf_unstable = 1; 708 } 709 /* 710 * mperf delta is likely a huge "positive" number 711 * can not use it for calculating c0 time 712 */ 713 skip_c0 = 1; 714 skip_c1 = 1; 715 } 716 717 718 if (use_c1_residency_msr) { 719 /* 720 * Some models have a dedicated C1 residency MSR, 721 * which should be more accurate than the derivation below. 722 */ 723 } else { 724 /* 725 * As counter collection is not atomic, 726 * it is possible for mperf's non-halted cycles + idle states 727 * to exceed TSC's all cycles: show c1 = 0% in that case. 728 */ 729 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 730 old->c1 = 0; 731 else { 732 /* normal case, derive c1 */ 733 old->c1 = old->tsc - old->mperf - core_delta->c3 734 - core_delta->c6 - core_delta->c7; 735 } 736 } 737 738 if (old->mperf == 0) { 739 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); 740 old->mperf = 1; /* divide by 0 protection */ 741 } 742 743 old->extra_delta32 = new->extra_delta32 - old->extra_delta32; 744 old->extra_delta32 &= 0xFFFFFFFF; 745 746 old->extra_delta64 = new->extra_delta64 - old->extra_delta64; 747 748 /* 749 * Extra MSR is just a snapshot, simply copy latest w/o subtracting 750 */ 751 old->extra_msr32 = new->extra_msr32; 752 old->extra_msr64 = new->extra_msr64; 753 754 if (do_smi) 755 old->smi_count = new->smi_count - old->smi_count; 756 } 757 758 int delta_cpu(struct thread_data *t, struct core_data *c, 759 struct pkg_data *p, struct thread_data *t2, 760 struct core_data *c2, struct pkg_data *p2) 761 { 762 /* calculate core delta only for 1st thread in core */ 763 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 764 delta_core(c, c2); 765 766 /* always calculate thread delta */ 767 delta_thread(t, t2, c2); /* c2 is core delta */ 768 769 /* calculate package delta only for 1st core in package */ 770 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 771 delta_package(p, p2); 772 773 return 0; 774 } 775 776 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 777 { 778 t->tsc = 0; 779 t->aperf = 0; 780 t->mperf = 0; 781 t->c1 = 0; 782 783 t->smi_count = 0; 784 t->extra_delta32 = 0; 785 t->extra_delta64 = 0; 786 787 /* tells format_counters to dump all fields from this set */ 788 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 789 790 c->c3 = 0; 791 c->c6 = 0; 792 c->c7 = 0; 793 c->core_temp_c = 0; 794 795 p->pc2 = 0; 796 p->pc3 = 0; 797 p->pc6 = 0; 798 p->pc7 = 0; 799 p->pc8 = 0; 800 p->pc9 = 0; 801 p->pc10 = 0; 802 803 p->energy_pkg = 0; 804 p->energy_dram = 0; 805 p->energy_cores = 0; 806 p->energy_gfx = 0; 807 p->rapl_pkg_perf_status = 0; 808 p->rapl_dram_perf_status = 0; 809 p->pkg_temp_c = 0; 810 } 811 int sum_counters(struct thread_data *t, struct core_data *c, 812 struct pkg_data *p) 813 { 814 average.threads.tsc += t->tsc; 815 average.threads.aperf += t->aperf; 816 average.threads.mperf += t->mperf; 817 average.threads.c1 += t->c1; 818 819 average.threads.extra_delta32 += t->extra_delta32; 820 average.threads.extra_delta64 += t->extra_delta64; 821 822 /* sum per-core values only for 1st thread in core */ 823 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 824 return 0; 825 826 average.cores.c3 += c->c3; 827 average.cores.c6 += c->c6; 828 average.cores.c7 += c->c7; 829 830 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 831 832 /* sum per-pkg values only for 1st core in pkg */ 833 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 834 return 0; 835 836 average.packages.pc2 += p->pc2; 837 average.packages.pc3 += p->pc3; 838 average.packages.pc6 += p->pc6; 839 average.packages.pc7 += p->pc7; 840 average.packages.pc8 += p->pc8; 841 average.packages.pc9 += p->pc9; 842 average.packages.pc10 += p->pc10; 843 844 average.packages.energy_pkg += p->energy_pkg; 845 average.packages.energy_dram += p->energy_dram; 846 average.packages.energy_cores += p->energy_cores; 847 average.packages.energy_gfx += p->energy_gfx; 848 849 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 850 851 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 852 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 853 return 0; 854 } 855 /* 856 * sum the counters for all cpus in the system 857 * compute the weighted average 858 */ 859 void compute_average(struct thread_data *t, struct core_data *c, 860 struct pkg_data *p) 861 { 862 clear_counters(&average.threads, &average.cores, &average.packages); 863 864 for_all_cpus(sum_counters, t, c, p); 865 866 average.threads.tsc /= topo.num_cpus; 867 average.threads.aperf /= topo.num_cpus; 868 average.threads.mperf /= topo.num_cpus; 869 average.threads.c1 /= topo.num_cpus; 870 871 average.threads.extra_delta32 /= topo.num_cpus; 872 average.threads.extra_delta32 &= 0xFFFFFFFF; 873 874 average.threads.extra_delta64 /= topo.num_cpus; 875 876 average.cores.c3 /= topo.num_cores; 877 average.cores.c6 /= topo.num_cores; 878 average.cores.c7 /= topo.num_cores; 879 880 average.packages.pc2 /= topo.num_packages; 881 average.packages.pc3 /= topo.num_packages; 882 average.packages.pc6 /= topo.num_packages; 883 average.packages.pc7 /= topo.num_packages; 884 885 average.packages.pc8 /= topo.num_packages; 886 average.packages.pc9 /= topo.num_packages; 887 average.packages.pc10 /= topo.num_packages; 888 } 889 890 static unsigned long long rdtsc(void) 891 { 892 unsigned int low, high; 893 894 asm volatile("rdtsc" : "=a" (low), "=d" (high)); 895 896 return low | ((unsigned long long)high) << 32; 897 } 898 899 900 /* 901 * get_counters(...) 902 * migrate to cpu 903 * acquire and record local counters for that cpu 904 */ 905 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 906 { 907 int cpu = t->cpu_id; 908 unsigned long long msr; 909 910 if (cpu_migrate(cpu)) { 911 fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 912 return -1; 913 } 914 915 t->tsc = rdtsc(); /* we are running on local CPU of interest */ 916 917 if (has_aperf) { 918 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 919 return -3; 920 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 921 return -4; 922 } 923 924 if (do_smi) { 925 if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 926 return -5; 927 t->smi_count = msr & 0xFFFFFFFF; 928 } 929 if (extra_delta_offset32) { 930 if (get_msr(cpu, extra_delta_offset32, &msr)) 931 return -5; 932 t->extra_delta32 = msr & 0xFFFFFFFF; 933 } 934 935 if (extra_delta_offset64) 936 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) 937 return -5; 938 939 if (extra_msr_offset32) { 940 if (get_msr(cpu, extra_msr_offset32, &msr)) 941 return -5; 942 t->extra_msr32 = msr & 0xFFFFFFFF; 943 } 944 945 if (extra_msr_offset64) 946 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) 947 return -5; 948 949 if (use_c1_residency_msr) { 950 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 951 return -6; 952 } 953 954 /* collect core counters only for 1st thread in core */ 955 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 956 return 0; 957 958 if (do_nhm_cstates && !do_slm_cstates) { 959 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 960 return -6; 961 } 962 963 if (do_nhm_cstates) { 964 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 965 return -7; 966 } 967 968 if (do_snb_cstates) 969 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 970 return -8; 971 972 if (do_dts) { 973 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 974 return -9; 975 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 976 } 977 978 979 /* collect package counters only for 1st core in package */ 980 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 981 return 0; 982 983 if (do_nhm_cstates && !do_slm_cstates) { 984 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 985 return -9; 986 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 987 return -10; 988 } 989 if (do_snb_cstates) { 990 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 991 return -11; 992 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 993 return -12; 994 } 995 if (do_c8_c9_c10) { 996 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 997 return -13; 998 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 999 return -13; 1000 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1001 return -13; 1002 } 1003 if (do_rapl & RAPL_PKG) { 1004 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1005 return -13; 1006 p->energy_pkg = msr & 0xFFFFFFFF; 1007 } 1008 if (do_rapl & RAPL_CORES) { 1009 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1010 return -14; 1011 p->energy_cores = msr & 0xFFFFFFFF; 1012 } 1013 if (do_rapl & RAPL_DRAM) { 1014 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1015 return -15; 1016 p->energy_dram = msr & 0xFFFFFFFF; 1017 } 1018 if (do_rapl & RAPL_GFX) { 1019 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1020 return -16; 1021 p->energy_gfx = msr & 0xFFFFFFFF; 1022 } 1023 if (do_rapl & RAPL_PKG_PERF_STATUS) { 1024 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1025 return -16; 1026 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1027 } 1028 if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1029 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1030 return -16; 1031 p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1032 } 1033 if (do_ptm) { 1034 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1035 return -17; 1036 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1037 } 1038 return 0; 1039 } 1040 1041 void print_verbose_header(void) 1042 { 1043 unsigned long long msr; 1044 unsigned int ratio; 1045 1046 if (!do_nehalem_platform_info) 1047 return; 1048 1049 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); 1050 1051 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr); 1052 1053 ratio = (msr >> 40) & 0xFF; 1054 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", 1055 ratio, bclk, ratio * bclk); 1056 1057 ratio = (msr >> 8) & 0xFF; 1058 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", 1059 ratio, bclk, ratio * bclk); 1060 1061 get_msr(0, MSR_IA32_POWER_CTL, &msr); 1062 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1063 msr, msr & 0x2 ? "EN" : "DIS"); 1064 1065 if (!do_ivt_turbo_ratio_limit) 1066 goto print_nhm_turbo_ratio_limits; 1067 1068 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); 1069 1070 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 1071 1072 ratio = (msr >> 56) & 0xFF; 1073 if (ratio) 1074 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", 1075 ratio, bclk, ratio * bclk); 1076 1077 ratio = (msr >> 48) & 0xFF; 1078 if (ratio) 1079 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", 1080 ratio, bclk, ratio * bclk); 1081 1082 ratio = (msr >> 40) & 0xFF; 1083 if (ratio) 1084 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", 1085 ratio, bclk, ratio * bclk); 1086 1087 ratio = (msr >> 32) & 0xFF; 1088 if (ratio) 1089 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", 1090 ratio, bclk, ratio * bclk); 1091 1092 ratio = (msr >> 24) & 0xFF; 1093 if (ratio) 1094 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", 1095 ratio, bclk, ratio * bclk); 1096 1097 ratio = (msr >> 16) & 0xFF; 1098 if (ratio) 1099 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", 1100 ratio, bclk, ratio * bclk); 1101 1102 ratio = (msr >> 8) & 0xFF; 1103 if (ratio) 1104 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", 1105 ratio, bclk, ratio * bclk); 1106 1107 ratio = (msr >> 0) & 0xFF; 1108 if (ratio) 1109 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", 1110 ratio, bclk, ratio * bclk); 1111 1112 print_nhm_turbo_ratio_limits: 1113 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); 1114 1115 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1116 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1117 1118 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr); 1119 1120 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ", 1121 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1122 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1123 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1124 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1125 (msr & (1 << 15)) ? "" : "UN", 1126 (unsigned int)msr & 7); 1127 1128 1129 switch(msr & 0x7) { 1130 case 0: 1131 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0"); 1132 break; 1133 case 1: 1134 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0"); 1135 break; 1136 case 2: 1137 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3"); 1138 break; 1139 case 3: 1140 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6"); 1141 break; 1142 case 4: 1143 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7"); 1144 break; 1145 case 5: 1146 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid"); 1147 break; 1148 case 6: 1149 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid"); 1150 break; 1151 case 7: 1152 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited"); 1153 break; 1154 default: 1155 fprintf(stderr, "invalid"); 1156 } 1157 fprintf(stderr, ")\n"); 1158 1159 if (!do_nehalem_turbo_ratio_limit) 1160 return; 1161 1162 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); 1163 1164 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); 1165 1166 ratio = (msr >> 56) & 0xFF; 1167 if (ratio) 1168 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", 1169 ratio, bclk, ratio * bclk); 1170 1171 ratio = (msr >> 48) & 0xFF; 1172 if (ratio) 1173 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", 1174 ratio, bclk, ratio * bclk); 1175 1176 ratio = (msr >> 40) & 0xFF; 1177 if (ratio) 1178 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", 1179 ratio, bclk, ratio * bclk); 1180 1181 ratio = (msr >> 32) & 0xFF; 1182 if (ratio) 1183 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", 1184 ratio, bclk, ratio * bclk); 1185 1186 ratio = (msr >> 24) & 0xFF; 1187 if (ratio) 1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", 1189 ratio, bclk, ratio * bclk); 1190 1191 ratio = (msr >> 16) & 0xFF; 1192 if (ratio) 1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", 1194 ratio, bclk, ratio * bclk); 1195 1196 ratio = (msr >> 8) & 0xFF; 1197 if (ratio) 1198 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", 1199 ratio, bclk, ratio * bclk); 1200 1201 ratio = (msr >> 0) & 0xFF; 1202 if (ratio) 1203 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", 1204 ratio, bclk, ratio * bclk); 1205 } 1206 1207 void free_all_buffers(void) 1208 { 1209 CPU_FREE(cpu_present_set); 1210 cpu_present_set = NULL; 1211 cpu_present_set = 0; 1212 1213 CPU_FREE(cpu_affinity_set); 1214 cpu_affinity_set = NULL; 1215 cpu_affinity_setsize = 0; 1216 1217 free(thread_even); 1218 free(core_even); 1219 free(package_even); 1220 1221 thread_even = NULL; 1222 core_even = NULL; 1223 package_even = NULL; 1224 1225 free(thread_odd); 1226 free(core_odd); 1227 free(package_odd); 1228 1229 thread_odd = NULL; 1230 core_odd = NULL; 1231 package_odd = NULL; 1232 1233 free(output_buffer); 1234 output_buffer = NULL; 1235 outp = NULL; 1236 } 1237 1238 /* 1239 * Open a file, and exit on failure 1240 */ 1241 FILE *fopen_or_die(const char *path, const char *mode) 1242 { 1243 FILE *filep = fopen(path, "r"); 1244 if (!filep) 1245 err(1, "%s: open failed", path); 1246 return filep; 1247 } 1248 1249 /* 1250 * Parse a file containing a single int. 1251 */ 1252 int parse_int_file(const char *fmt, ...) 1253 { 1254 va_list args; 1255 char path[PATH_MAX]; 1256 FILE *filep; 1257 int value; 1258 1259 va_start(args, fmt); 1260 vsnprintf(path, sizeof(path), fmt, args); 1261 va_end(args); 1262 filep = fopen_or_die(path, "r"); 1263 if (fscanf(filep, "%d", &value) != 1) 1264 err(1, "%s: failed to parse number from file", path); 1265 fclose(filep); 1266 return value; 1267 } 1268 1269 /* 1270 * cpu_is_first_sibling_in_core(cpu) 1271 * return 1 if given CPU is 1st HT sibling in the core 1272 */ 1273 int cpu_is_first_sibling_in_core(int cpu) 1274 { 1275 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1276 } 1277 1278 /* 1279 * cpu_is_first_core_in_package(cpu) 1280 * return 1 if given CPU is 1st core in package 1281 */ 1282 int cpu_is_first_core_in_package(int cpu) 1283 { 1284 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 1285 } 1286 1287 int get_physical_package_id(int cpu) 1288 { 1289 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 1290 } 1291 1292 int get_core_id(int cpu) 1293 { 1294 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 1295 } 1296 1297 int get_num_ht_siblings(int cpu) 1298 { 1299 char path[80]; 1300 FILE *filep; 1301 int sib1, sib2; 1302 int matches; 1303 char character; 1304 1305 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 1306 filep = fopen_or_die(path, "r"); 1307 /* 1308 * file format: 1309 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) 1310 * otherwinse 1 sibling (self). 1311 */ 1312 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); 1313 1314 fclose(filep); 1315 1316 if (matches == 3) 1317 return 2; 1318 else 1319 return 1; 1320 } 1321 1322 /* 1323 * run func(thread, core, package) in topology order 1324 * skip non-present cpus 1325 */ 1326 1327 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 1328 struct pkg_data *, struct thread_data *, struct core_data *, 1329 struct pkg_data *), struct thread_data *thread_base, 1330 struct core_data *core_base, struct pkg_data *pkg_base, 1331 struct thread_data *thread_base2, struct core_data *core_base2, 1332 struct pkg_data *pkg_base2) 1333 { 1334 int retval, pkg_no, core_no, thread_no; 1335 1336 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 1337 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 1338 for (thread_no = 0; thread_no < 1339 topo.num_threads_per_core; ++thread_no) { 1340 struct thread_data *t, *t2; 1341 struct core_data *c, *c2; 1342 struct pkg_data *p, *p2; 1343 1344 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 1345 1346 if (cpu_is_not_present(t->cpu_id)) 1347 continue; 1348 1349 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 1350 1351 c = GET_CORE(core_base, core_no, pkg_no); 1352 c2 = GET_CORE(core_base2, core_no, pkg_no); 1353 1354 p = GET_PKG(pkg_base, pkg_no); 1355 p2 = GET_PKG(pkg_base2, pkg_no); 1356 1357 retval = func(t, c, p, t2, c2, p2); 1358 if (retval) 1359 return retval; 1360 } 1361 } 1362 } 1363 return 0; 1364 } 1365 1366 /* 1367 * run func(cpu) on every cpu in /proc/stat 1368 * return max_cpu number 1369 */ 1370 int for_all_proc_cpus(int (func)(int)) 1371 { 1372 FILE *fp; 1373 int cpu_num; 1374 int retval; 1375 1376 fp = fopen_or_die(proc_stat, "r"); 1377 1378 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 1379 if (retval != 0) 1380 err(1, "%s: failed to parse format", proc_stat); 1381 1382 while (1) { 1383 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 1384 if (retval != 1) 1385 break; 1386 1387 retval = func(cpu_num); 1388 if (retval) { 1389 fclose(fp); 1390 return(retval); 1391 } 1392 } 1393 fclose(fp); 1394 return 0; 1395 } 1396 1397 void re_initialize(void) 1398 { 1399 free_all_buffers(); 1400 setup_all_buffers(); 1401 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 1402 } 1403 1404 1405 /* 1406 * count_cpus() 1407 * remember the last one seen, it will be the max 1408 */ 1409 int count_cpus(int cpu) 1410 { 1411 if (topo.max_cpu_num < cpu) 1412 topo.max_cpu_num = cpu; 1413 1414 topo.num_cpus += 1; 1415 return 0; 1416 } 1417 int mark_cpu_present(int cpu) 1418 { 1419 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 1420 return 0; 1421 } 1422 1423 void turbostat_loop() 1424 { 1425 int retval; 1426 int restarted = 0; 1427 1428 restart: 1429 restarted++; 1430 1431 retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1432 if (retval < -1) { 1433 exit(retval); 1434 } else if (retval == -1) { 1435 if (restarted > 1) { 1436 exit(retval); 1437 } 1438 re_initialize(); 1439 goto restart; 1440 } 1441 restarted = 0; 1442 gettimeofday(&tv_even, (struct timezone *)NULL); 1443 1444 while (1) { 1445 if (for_all_proc_cpus(cpu_is_not_present)) { 1446 re_initialize(); 1447 goto restart; 1448 } 1449 sleep(interval_sec); 1450 retval = for_all_cpus(get_counters, ODD_COUNTERS); 1451 if (retval < -1) { 1452 exit(retval); 1453 } else if (retval == -1) { 1454 re_initialize(); 1455 goto restart; 1456 } 1457 gettimeofday(&tv_odd, (struct timezone *)NULL); 1458 timersub(&tv_odd, &tv_even, &tv_delta); 1459 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 1460 compute_average(EVEN_COUNTERS); 1461 format_all_counters(EVEN_COUNTERS); 1462 flush_stdout(); 1463 sleep(interval_sec); 1464 retval = for_all_cpus(get_counters, EVEN_COUNTERS); 1465 if (retval < -1) { 1466 exit(retval); 1467 } else if (retval == -1) { 1468 re_initialize(); 1469 goto restart; 1470 } 1471 gettimeofday(&tv_even, (struct timezone *)NULL); 1472 timersub(&tv_even, &tv_odd, &tv_delta); 1473 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); 1474 compute_average(ODD_COUNTERS); 1475 format_all_counters(ODD_COUNTERS); 1476 flush_stdout(); 1477 } 1478 } 1479 1480 void check_dev_msr() 1481 { 1482 struct stat sb; 1483 1484 if (stat("/dev/cpu/0/msr", &sb)) 1485 err(-5, "no /dev/cpu/0/msr\n" 1486 "Try \"# modprobe msr\""); 1487 } 1488 1489 void check_super_user() 1490 { 1491 if (getuid() != 0) 1492 errx(-6, "must be root"); 1493 } 1494 1495 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1496 { 1497 if (!genuine_intel) 1498 return 0; 1499 1500 if (family != 6) 1501 return 0; 1502 1503 switch (model) { 1504 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 1505 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 1506 case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 1507 case 0x25: /* Westmere Client - Clarkdale, Arrandale */ 1508 case 0x2C: /* Westmere EP - Gulftown */ 1509 case 0x2A: /* SNB */ 1510 case 0x2D: /* SNB Xeon */ 1511 case 0x3A: /* IVB */ 1512 case 0x3E: /* IVB Xeon */ 1513 case 0x3C: /* HSW */ 1514 case 0x3F: /* HSX */ 1515 case 0x45: /* HSW */ 1516 case 0x46: /* HSW */ 1517 case 0x37: /* BYT */ 1518 case 0x4D: /* AVN */ 1519 return 1; 1520 case 0x2E: /* Nehalem-EX Xeon - Beckton */ 1521 case 0x2F: /* Westmere-EX Xeon - Eagleton */ 1522 default: 1523 return 0; 1524 } 1525 } 1526 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 1527 { 1528 if (!genuine_intel) 1529 return 0; 1530 1531 if (family != 6) 1532 return 0; 1533 1534 switch (model) { 1535 case 0x3E: /* IVB Xeon */ 1536 return 1; 1537 default: 1538 return 0; 1539 } 1540 } 1541 1542 /* 1543 * print_epb() 1544 * Decode the ENERGY_PERF_BIAS MSR 1545 */ 1546 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1547 { 1548 unsigned long long msr; 1549 char *epb_string; 1550 int cpu; 1551 1552 if (!has_epb) 1553 return 0; 1554 1555 cpu = t->cpu_id; 1556 1557 /* EPB is per-package */ 1558 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1559 return 0; 1560 1561 if (cpu_migrate(cpu)) { 1562 fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1563 return -1; 1564 } 1565 1566 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 1567 return 0; 1568 1569 switch (msr & 0x7) { 1570 case ENERGY_PERF_BIAS_PERFORMANCE: 1571 epb_string = "performance"; 1572 break; 1573 case ENERGY_PERF_BIAS_NORMAL: 1574 epb_string = "balanced"; 1575 break; 1576 case ENERGY_PERF_BIAS_POWERSAVE: 1577 epb_string = "powersave"; 1578 break; 1579 default: 1580 epb_string = "custom"; 1581 break; 1582 } 1583 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 1584 1585 return 0; 1586 } 1587 1588 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 1589 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 1590 1591 double get_tdp(model) 1592 { 1593 unsigned long long msr; 1594 1595 if (do_rapl & RAPL_PKG_POWER_INFO) 1596 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr)) 1597 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 1598 1599 switch (model) { 1600 case 0x37: 1601 case 0x4D: 1602 return 30.0; 1603 default: 1604 return 135.0; 1605 } 1606 } 1607 1608 1609 /* 1610 * rapl_probe() 1611 * 1612 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 1613 */ 1614 void rapl_probe(unsigned int family, unsigned int model) 1615 { 1616 unsigned long long msr; 1617 unsigned int time_unit; 1618 double tdp; 1619 1620 if (!genuine_intel) 1621 return; 1622 1623 if (family != 6) 1624 return; 1625 1626 switch (model) { 1627 case 0x2A: 1628 case 0x3A: 1629 case 0x3C: /* HSW */ 1630 case 0x45: /* HSW */ 1631 case 0x46: /* HSW */ 1632 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 1633 break; 1634 case 0x3F: /* HSX */ 1635 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 1636 break; 1637 case 0x2D: 1638 case 0x3E: 1639 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 1640 break; 1641 case 0x37: /* BYT */ 1642 case 0x4D: /* AVN */ 1643 do_rapl = RAPL_PKG | RAPL_CORES ; 1644 break; 1645 default: 1646 return; 1647 } 1648 1649 /* units on package 0, verify later other packages match */ 1650 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr)) 1651 return; 1652 1653 rapl_power_units = 1.0 / (1 << (msr & 0xF)); 1654 if (model == 0x37) 1655 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 1656 else 1657 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 1658 1659 time_unit = msr >> 16 & 0xF; 1660 if (time_unit == 0) 1661 time_unit = 0xA; 1662 1663 rapl_time_units = 1.0 / (1 << (time_unit)); 1664 1665 tdp = get_tdp(model); 1666 1667 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 1668 if (verbose) 1669 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 1670 1671 return; 1672 } 1673 1674 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1675 { 1676 unsigned long long msr; 1677 unsigned int dts; 1678 int cpu; 1679 1680 if (!(do_dts || do_ptm)) 1681 return 0; 1682 1683 cpu = t->cpu_id; 1684 1685 /* DTS is per-core, no need to print for each thread */ 1686 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1687 return 0; 1688 1689 if (cpu_migrate(cpu)) { 1690 fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1691 return -1; 1692 } 1693 1694 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 1695 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1696 return 0; 1697 1698 dts = (msr >> 16) & 0x7F; 1699 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 1700 cpu, msr, tcc_activation_temp - dts); 1701 1702 #ifdef THERM_DEBUG 1703 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 1704 return 0; 1705 1706 dts = (msr >> 16) & 0x7F; 1707 dts2 = (msr >> 8) & 0x7F; 1708 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1709 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1710 #endif 1711 } 1712 1713 1714 if (do_dts) { 1715 unsigned int resolution; 1716 1717 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1718 return 0; 1719 1720 dts = (msr >> 16) & 0x7F; 1721 resolution = (msr >> 27) & 0xF; 1722 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 1723 cpu, msr, tcc_activation_temp - dts, resolution); 1724 1725 #ifdef THERM_DEBUG 1726 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 1727 return 0; 1728 1729 dts = (msr >> 16) & 0x7F; 1730 dts2 = (msr >> 8) & 0x7F; 1731 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 1732 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 1733 #endif 1734 } 1735 1736 return 0; 1737 } 1738 1739 void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 1740 { 1741 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 1742 cpu, label, 1743 ((msr >> 15) & 1) ? "EN" : "DIS", 1744 ((msr >> 0) & 0x7FFF) * rapl_power_units, 1745 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 1746 (((msr >> 16) & 1) ? "EN" : "DIS")); 1747 1748 return; 1749 } 1750 1751 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1752 { 1753 unsigned long long msr; 1754 int cpu; 1755 1756 if (!do_rapl) 1757 return 0; 1758 1759 /* RAPL counters are per package, so print only for 1st thread/package */ 1760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1761 return 0; 1762 1763 cpu = t->cpu_id; 1764 if (cpu_migrate(cpu)) { 1765 fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1766 return -1; 1767 } 1768 1769 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 1770 return -1; 1771 1772 if (verbose) { 1773 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " 1774 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, 1775 rapl_power_units, rapl_energy_units, rapl_time_units); 1776 } 1777 if (do_rapl & RAPL_PKG_POWER_INFO) { 1778 1779 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 1780 return -5; 1781 1782 1783 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1784 cpu, msr, 1785 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1786 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1787 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1788 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1789 1790 } 1791 if (do_rapl & RAPL_PKG) { 1792 1793 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 1794 return -9; 1795 1796 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 1797 cpu, msr, (msr >> 63) & 1 ? "": "UN"); 1798 1799 print_power_limit_msr(cpu, msr, "PKG Limit #1"); 1800 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 1801 cpu, 1802 ((msr >> 47) & 1) ? "EN" : "DIS", 1803 ((msr >> 32) & 0x7FFF) * rapl_power_units, 1804 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 1805 ((msr >> 48) & 1) ? "EN" : "DIS"); 1806 } 1807 1808 if (do_rapl & RAPL_DRAM) { 1809 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 1810 return -6; 1811 1812 1813 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 1814 cpu, msr, 1815 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1816 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1817 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 1818 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 1819 1820 1821 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 1822 return -9; 1823 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 1824 cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1825 1826 print_power_limit_msr(cpu, msr, "DRAM Limit"); 1827 } 1828 if (do_rapl & RAPL_CORE_POLICY) { 1829 if (verbose) { 1830 if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 1831 return -7; 1832 1833 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 1834 } 1835 } 1836 if (do_rapl & RAPL_CORES) { 1837 if (verbose) { 1838 1839 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 1840 return -9; 1841 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 1842 cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1843 print_power_limit_msr(cpu, msr, "Cores Limit"); 1844 } 1845 } 1846 if (do_rapl & RAPL_GFX) { 1847 if (verbose) { 1848 if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 1849 return -8; 1850 1851 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 1852 1853 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 1854 return -9; 1855 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 1856 cpu, msr, (msr >> 31) & 1 ? "": "UN"); 1857 print_power_limit_msr(cpu, msr, "GFX Limit"); 1858 } 1859 } 1860 return 0; 1861 } 1862 1863 1864 int is_snb(unsigned int family, unsigned int model) 1865 { 1866 if (!genuine_intel) 1867 return 0; 1868 1869 switch (model) { 1870 case 0x2A: 1871 case 0x2D: 1872 case 0x3A: /* IVB */ 1873 case 0x3E: /* IVB Xeon */ 1874 case 0x3C: /* HSW */ 1875 case 0x3F: /* HSW */ 1876 case 0x45: /* HSW */ 1877 case 0x46: /* HSW */ 1878 return 1; 1879 } 1880 return 0; 1881 } 1882 1883 int has_c8_c9_c10(unsigned int family, unsigned int model) 1884 { 1885 if (!genuine_intel) 1886 return 0; 1887 1888 switch (model) { 1889 case 0x45: 1890 return 1; 1891 } 1892 return 0; 1893 } 1894 1895 1896 int is_slm(unsigned int family, unsigned int model) 1897 { 1898 if (!genuine_intel) 1899 return 0; 1900 switch (model) { 1901 case 0x37: /* BYT */ 1902 case 0x4D: /* AVN */ 1903 return 1; 1904 } 1905 return 0; 1906 } 1907 1908 #define SLM_BCLK_FREQS 5 1909 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 1910 1911 double slm_bclk(void) 1912 { 1913 unsigned long long msr = 3; 1914 unsigned int i; 1915 double freq; 1916 1917 if (get_msr(0, MSR_FSB_FREQ, &msr)) 1918 fprintf(stderr, "SLM BCLK: unknown\n"); 1919 1920 i = msr & 0xf; 1921 if (i >= SLM_BCLK_FREQS) { 1922 fprintf(stderr, "SLM BCLK[%d] invalid\n", i); 1923 msr = 3; 1924 } 1925 freq = slm_freq_table[i]; 1926 1927 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); 1928 1929 return freq; 1930 } 1931 1932 double discover_bclk(unsigned int family, unsigned int model) 1933 { 1934 if (is_snb(family, model)) 1935 return 100.00; 1936 else if (is_slm(family, model)) 1937 return slm_bclk(); 1938 else 1939 return 133.33; 1940 } 1941 1942 /* 1943 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 1944 * the Thermal Control Circuit (TCC) activates. 1945 * This is usually equal to tjMax. 1946 * 1947 * Older processors do not have this MSR, so there we guess, 1948 * but also allow cmdline over-ride with -T. 1949 * 1950 * Several MSR temperature values are in units of degrees-C 1951 * below this value, including the Digital Thermal Sensor (DTS), 1952 * Package Thermal Management Sensor (PTM), and thermal event thresholds. 1953 */ 1954 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1955 { 1956 unsigned long long msr; 1957 unsigned int target_c_local; 1958 int cpu; 1959 1960 /* tcc_activation_temp is used only for dts or ptm */ 1961 if (!(do_dts || do_ptm)) 1962 return 0; 1963 1964 /* this is a per-package concept */ 1965 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1966 return 0; 1967 1968 cpu = t->cpu_id; 1969 if (cpu_migrate(cpu)) { 1970 fprintf(stderr, "Could not migrate to CPU %d\n", cpu); 1971 return -1; 1972 } 1973 1974 if (tcc_activation_temp_override != 0) { 1975 tcc_activation_temp = tcc_activation_temp_override; 1976 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", 1977 cpu, tcc_activation_temp); 1978 return 0; 1979 } 1980 1981 /* Temperature Target MSR is Nehalem and newer only */ 1982 if (!do_nehalem_platform_info) 1983 goto guess; 1984 1985 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr)) 1986 goto guess; 1987 1988 target_c_local = (msr >> 16) & 0x7F; 1989 1990 if (verbose) 1991 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 1992 cpu, msr, target_c_local); 1993 1994 if (target_c_local < 85 || target_c_local > 127) 1995 goto guess; 1996 1997 tcc_activation_temp = target_c_local; 1998 1999 return 0; 2000 2001 guess: 2002 tcc_activation_temp = TJMAX_DEFAULT; 2003 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 2004 cpu, tcc_activation_temp); 2005 2006 return 0; 2007 } 2008 void check_cpuid() 2009 { 2010 unsigned int eax, ebx, ecx, edx, max_level; 2011 unsigned int fms, family, model, stepping; 2012 2013 eax = ebx = ecx = edx = 0; 2014 2015 __get_cpuid(0, &max_level, &ebx, &ecx, &edx); 2016 2017 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 2018 genuine_intel = 1; 2019 2020 if (verbose) 2021 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", 2022 (char *)&ebx, (char *)&edx, (char *)&ecx); 2023 2024 __get_cpuid(1, &fms, &ebx, &ecx, &edx); 2025 family = (fms >> 8) & 0xf; 2026 model = (fms >> 4) & 0xf; 2027 stepping = fms & 0xf; 2028 if (family == 6 || family == 0xf) 2029 model += ((fms >> 16) & 0xf) << 4; 2030 2031 if (verbose) 2032 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 2033 max_level, family, model, stepping, family, model, stepping); 2034 2035 if (!(edx & (1 << 5))) 2036 errx(1, "CPUID: no MSR"); 2037 2038 /* 2039 * check max extended function levels of CPUID. 2040 * This is needed to check for invariant TSC. 2041 * This check is valid for both Intel and AMD. 2042 */ 2043 ebx = ecx = edx = 0; 2044 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); 2045 2046 if (max_level < 0x80000007) 2047 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level); 2048 2049 /* 2050 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 2051 * this check is valid for both Intel and AMD 2052 */ 2053 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 2054 has_invariant_tsc = edx & (1 << 8); 2055 2056 if (!has_invariant_tsc) 2057 errx(1, "No invariant TSC"); 2058 2059 /* 2060 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 2061 * this check is valid for both Intel and AMD 2062 */ 2063 2064 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); 2065 has_aperf = ecx & (1 << 0); 2066 do_dts = eax & (1 << 0); 2067 do_ptm = eax & (1 << 6); 2068 has_epb = ecx & (1 << 3); 2069 2070 if (verbose) 2071 fprintf(stderr, "CPUID(6): %s%s%s%s\n", 2072 has_aperf ? "APERF" : "No APERF!", 2073 do_dts ? ", DTS" : "", 2074 do_ptm ? ", PTM": "", 2075 has_epb ? ", EPB": ""); 2076 2077 if (!has_aperf) 2078 errx(-1, "No APERF"); 2079 2080 do_nehalem_platform_info = genuine_intel && has_invariant_tsc; 2081 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ 2082 do_smi = do_nhm_cstates; 2083 do_snb_cstates = is_snb(family, model); 2084 do_c8_c9_c10 = has_c8_c9_c10(family, model); 2085 do_slm_cstates = is_slm(family, model); 2086 bclk = discover_bclk(family, model); 2087 2088 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); 2089 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); 2090 rapl_probe(family, model); 2091 2092 return; 2093 } 2094 2095 2096 void usage() 2097 { 2098 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", 2099 progname); 2100 } 2101 2102 2103 /* 2104 * in /dev/cpu/ return success for names that are numbers 2105 * ie. filter out ".", "..", "microcode". 2106 */ 2107 int dir_filter(const struct dirent *dirp) 2108 { 2109 if (isdigit(dirp->d_name[0])) 2110 return 1; 2111 else 2112 return 0; 2113 } 2114 2115 int open_dev_cpu_msr(int dummy1) 2116 { 2117 return 0; 2118 } 2119 2120 void topology_probe() 2121 { 2122 int i; 2123 int max_core_id = 0; 2124 int max_package_id = 0; 2125 int max_siblings = 0; 2126 struct cpu_topology { 2127 int core_id; 2128 int physical_package_id; 2129 } *cpus; 2130 2131 /* Initialize num_cpus, max_cpu_num */ 2132 topo.num_cpus = 0; 2133 topo.max_cpu_num = 0; 2134 for_all_proc_cpus(count_cpus); 2135 if (!summary_only && topo.num_cpus > 1) 2136 show_cpu = 1; 2137 2138 if (verbose > 1) 2139 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 2140 2141 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 2142 if (cpus == NULL) 2143 err(1, "calloc cpus"); 2144 2145 /* 2146 * Allocate and initialize cpu_present_set 2147 */ 2148 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2149 if (cpu_present_set == NULL) 2150 err(3, "CPU_ALLOC"); 2151 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2152 CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 2153 for_all_proc_cpus(mark_cpu_present); 2154 2155 /* 2156 * Allocate and initialize cpu_affinity_set 2157 */ 2158 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 2159 if (cpu_affinity_set == NULL) 2160 err(3, "CPU_ALLOC"); 2161 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2162 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 2163 2164 2165 /* 2166 * For online cpus 2167 * find max_core_id, max_package_id 2168 */ 2169 for (i = 0; i <= topo.max_cpu_num; ++i) { 2170 int siblings; 2171 2172 if (cpu_is_not_present(i)) { 2173 if (verbose > 1) 2174 fprintf(stderr, "cpu%d NOT PRESENT\n", i); 2175 continue; 2176 } 2177 cpus[i].core_id = get_core_id(i); 2178 if (cpus[i].core_id > max_core_id) 2179 max_core_id = cpus[i].core_id; 2180 2181 cpus[i].physical_package_id = get_physical_package_id(i); 2182 if (cpus[i].physical_package_id > max_package_id) 2183 max_package_id = cpus[i].physical_package_id; 2184 2185 siblings = get_num_ht_siblings(i); 2186 if (siblings > max_siblings) 2187 max_siblings = siblings; 2188 if (verbose > 1) 2189 fprintf(stderr, "cpu %d pkg %d core %d\n", 2190 i, cpus[i].physical_package_id, cpus[i].core_id); 2191 } 2192 topo.num_cores_per_pkg = max_core_id + 1; 2193 if (verbose > 1) 2194 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", 2195 max_core_id, topo.num_cores_per_pkg); 2196 if (!summary_only && topo.num_cores_per_pkg > 1) 2197 show_core = 1; 2198 2199 topo.num_packages = max_package_id + 1; 2200 if (verbose > 1) 2201 fprintf(stderr, "max_package_id %d, sizing for %d packages\n", 2202 max_package_id, topo.num_packages); 2203 if (!summary_only && topo.num_packages > 1) 2204 show_pkg = 1; 2205 2206 topo.num_threads_per_core = max_siblings; 2207 if (verbose > 1) 2208 fprintf(stderr, "max_siblings %d\n", max_siblings); 2209 2210 free(cpus); 2211 } 2212 2213 void 2214 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 2215 { 2216 int i; 2217 2218 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 2219 topo.num_packages, sizeof(struct thread_data)); 2220 if (*t == NULL) 2221 goto error; 2222 2223 for (i = 0; i < topo.num_threads_per_core * 2224 topo.num_cores_per_pkg * topo.num_packages; i++) 2225 (*t)[i].cpu_id = -1; 2226 2227 *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 2228 sizeof(struct core_data)); 2229 if (*c == NULL) 2230 goto error; 2231 2232 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 2233 (*c)[i].core_id = -1; 2234 2235 *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 2236 if (*p == NULL) 2237 goto error; 2238 2239 for (i = 0; i < topo.num_packages; i++) 2240 (*p)[i].package_id = i; 2241 2242 return; 2243 error: 2244 err(1, "calloc counters"); 2245 } 2246 /* 2247 * init_counter() 2248 * 2249 * set cpu_id, core_num, pkg_num 2250 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 2251 * 2252 * increment topo.num_cores when 1st core in pkg seen 2253 */ 2254 void init_counter(struct thread_data *thread_base, struct core_data *core_base, 2255 struct pkg_data *pkg_base, int thread_num, int core_num, 2256 int pkg_num, int cpu_id) 2257 { 2258 struct thread_data *t; 2259 struct core_data *c; 2260 struct pkg_data *p; 2261 2262 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 2263 c = GET_CORE(core_base, core_num, pkg_num); 2264 p = GET_PKG(pkg_base, pkg_num); 2265 2266 t->cpu_id = cpu_id; 2267 if (thread_num == 0) { 2268 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 2269 if (cpu_is_first_core_in_package(cpu_id)) 2270 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 2271 } 2272 2273 c->core_id = core_num; 2274 p->package_id = pkg_num; 2275 } 2276 2277 2278 int initialize_counters(int cpu_id) 2279 { 2280 int my_thread_id, my_core_id, my_package_id; 2281 2282 my_package_id = get_physical_package_id(cpu_id); 2283 my_core_id = get_core_id(cpu_id); 2284 2285 if (cpu_is_first_sibling_in_core(cpu_id)) { 2286 my_thread_id = 0; 2287 topo.num_cores++; 2288 } else { 2289 my_thread_id = 1; 2290 } 2291 2292 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2293 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 2294 return 0; 2295 } 2296 2297 void allocate_output_buffer() 2298 { 2299 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 2300 outp = output_buffer; 2301 if (outp == NULL) 2302 err(-1, "calloc output buffer"); 2303 } 2304 2305 void setup_all_buffers(void) 2306 { 2307 topology_probe(); 2308 allocate_counters(&thread_even, &core_even, &package_even); 2309 allocate_counters(&thread_odd, &core_odd, &package_odd); 2310 allocate_output_buffer(); 2311 for_all_proc_cpus(initialize_counters); 2312 } 2313 2314 void turbostat_init() 2315 { 2316 check_cpuid(); 2317 2318 check_dev_msr(); 2319 check_super_user(); 2320 2321 setup_all_buffers(); 2322 2323 if (verbose) 2324 print_verbose_header(); 2325 2326 if (verbose) 2327 for_all_cpus(print_epb, ODD_COUNTERS); 2328 2329 if (verbose) 2330 for_all_cpus(print_rapl, ODD_COUNTERS); 2331 2332 for_all_cpus(set_temperature_target, ODD_COUNTERS); 2333 2334 if (verbose) 2335 for_all_cpus(print_thermal, ODD_COUNTERS); 2336 } 2337 2338 int fork_it(char **argv) 2339 { 2340 pid_t child_pid; 2341 int status; 2342 2343 status = for_all_cpus(get_counters, EVEN_COUNTERS); 2344 if (status) 2345 exit(status); 2346 /* clear affinity side-effect of get_counters() */ 2347 sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 2348 gettimeofday(&tv_even, (struct timezone *)NULL); 2349 2350 child_pid = fork(); 2351 if (!child_pid) { 2352 /* child */ 2353 execvp(argv[0], argv); 2354 } else { 2355 2356 /* parent */ 2357 if (child_pid == -1) 2358 err(1, "fork"); 2359 2360 signal(SIGINT, SIG_IGN); 2361 signal(SIGQUIT, SIG_IGN); 2362 if (waitpid(child_pid, &status, 0) == -1) 2363 err(status, "waitpid"); 2364 } 2365 /* 2366 * n.b. fork_it() does not check for errors from for_all_cpus() 2367 * because re-starting is problematic when forking 2368 */ 2369 for_all_cpus(get_counters, ODD_COUNTERS); 2370 gettimeofday(&tv_odd, (struct timezone *)NULL); 2371 timersub(&tv_odd, &tv_even, &tv_delta); 2372 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); 2373 compute_average(EVEN_COUNTERS); 2374 format_all_counters(EVEN_COUNTERS); 2375 flush_stderr(); 2376 2377 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 2378 2379 return status; 2380 } 2381 2382 int get_and_dump_counters(void) 2383 { 2384 int status; 2385 2386 status = for_all_cpus(get_counters, ODD_COUNTERS); 2387 if (status) 2388 return status; 2389 2390 status = for_all_cpus(dump_counters, ODD_COUNTERS); 2391 if (status) 2392 return status; 2393 2394 flush_stdout(); 2395 2396 return status; 2397 } 2398 2399 void cmdline(int argc, char **argv) 2400 { 2401 int opt; 2402 2403 progname = argv[0]; 2404 2405 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) { 2406 switch (opt) { 2407 case 'p': 2408 show_core_only++; 2409 break; 2410 case 'P': 2411 show_pkg_only++; 2412 break; 2413 case 's': 2414 dump_only++; 2415 break; 2416 case 'S': 2417 summary_only++; 2418 break; 2419 case 'v': 2420 verbose++; 2421 break; 2422 case 'i': 2423 interval_sec = atoi(optarg); 2424 break; 2425 case 'c': 2426 sscanf(optarg, "%x", &extra_delta_offset32); 2427 break; 2428 case 'C': 2429 sscanf(optarg, "%x", &extra_delta_offset64); 2430 break; 2431 case 'm': 2432 sscanf(optarg, "%x", &extra_msr_offset32); 2433 break; 2434 case 'M': 2435 sscanf(optarg, "%x", &extra_msr_offset64); 2436 break; 2437 case 'R': 2438 rapl_verbose++; 2439 break; 2440 case 'T': 2441 tcc_activation_temp_override = atoi(optarg); 2442 break; 2443 case 'J': 2444 rapl_joules++; 2445 break; 2446 2447 default: 2448 usage(); 2449 } 2450 } 2451 } 2452 2453 int main(int argc, char **argv) 2454 { 2455 cmdline(argc, argv); 2456 2457 if (verbose) 2458 fprintf(stderr, "turbostat v3.6 Dec 2, 2013" 2459 " - Len Brown <lenb@kernel.org>\n"); 2460 2461 turbostat_init(); 2462 2463 /* dump counters and exit */ 2464 if (dump_only) 2465 return get_and_dump_counters(); 2466 2467 /* 2468 * if any params left, it must be a command to fork 2469 */ 2470 if (argc - optind) 2471 return fork_it(argv + optind); 2472 else 2473 turbostat_loop(); 2474 2475 return 0; 2476 } 2477