1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * turbostat -- show CPU frequency and C-state residency 4 * on modern Intel and AMD processors. 5 * 6 * Copyright (c) 2010 - 2026 Intel Corporation 7 * Len Brown <len.brown@intel.com> 8 */ 9 10 #define _GNU_SOURCE 11 #include MSRHEADER 12 13 // copied from arch/x86/include/asm/cpu_device_id.h 14 #define VFM_MODEL_BIT 0 15 #define VFM_FAMILY_BIT 8 16 #define VFM_VENDOR_BIT 16 17 #define VFM_RSVD_BIT 24 18 19 #define VFM_MODEL_MASK GENMASK(VFM_FAMILY_BIT - 1, VFM_MODEL_BIT) 20 #define VFM_FAMILY_MASK GENMASK(VFM_VENDOR_BIT - 1, VFM_FAMILY_BIT) 21 #define VFM_VENDOR_MASK GENMASK(VFM_RSVD_BIT - 1, VFM_VENDOR_BIT) 22 23 #define VFM_MODEL(vfm) (((vfm) & VFM_MODEL_MASK) >> VFM_MODEL_BIT) 24 #define VFM_FAMILY(vfm) (((vfm) & VFM_FAMILY_MASK) >> VFM_FAMILY_BIT) 25 #define VFM_VENDOR(vfm) (((vfm) & VFM_VENDOR_MASK) >> VFM_VENDOR_BIT) 26 27 #define VFM_MAKE(_vendor, _family, _model) ( \ 28 ((_model) << VFM_MODEL_BIT) | \ 29 ((_family) << VFM_FAMILY_BIT) | \ 30 ((_vendor) << VFM_VENDOR_BIT) \ 31 ) 32 // end copied section 33 34 #define CPUID_LEAF_MODEL_ID 0x1A 35 #define CPUID_LEAF_MODEL_ID_CORE_TYPE_SHIFT 24 36 37 #define X86_VENDOR_INTEL 0 38 39 #include INTEL_FAMILY_HEADER 40 #include BUILD_BUG_HEADER 41 #include <stdarg.h> 42 #include <stdio.h> 43 #include <err.h> 44 #include <unistd.h> 45 #include <sys/types.h> 46 #include <sys/wait.h> 47 #include <sys/stat.h> 48 #include <sys/select.h> 49 #include <sys/resource.h> 50 #include <sys/mman.h> 51 #include <fcntl.h> 52 #include <signal.h> 53 #include <sys/time.h> 54 #include <stdlib.h> 55 #include <getopt.h> 56 #include <dirent.h> 57 #include <string.h> 58 #include <ctype.h> 59 #include <sched.h> 60 #include <time.h> 61 #include <cpuid.h> 62 #include <sys/capability.h> 63 #include <errno.h> 64 #include <math.h> 65 #include <linux/perf_event.h> 66 #include <asm/unistd.h> 67 #include <stdbool.h> 68 #include <assert.h> 69 #include <linux/kernel.h> 70 #include <limits.h> 71 72 #define UNUSED(x) (void)(x) 73 74 /* 75 * This list matches the column headers, except 76 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 77 * 2. Core and CPU are moved to the end, we can't have strings that contain them 78 * matching on them for --show and --hide. 79 */ 80 81 /* 82 * buffer size used by sscanf() for added column names 83 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 84 */ 85 #define NAME_BYTES 20 86 #define PATH_BYTES 128 87 #define PERF_NAME_BYTES 128 88 89 #define MAX_NOFILE 0x8000 90 91 #define COUNTER_KIND_PERF_PREFIX "perf/" 92 #define COUNTER_KIND_PERF_PREFIX_LEN strlen(COUNTER_KIND_PERF_PREFIX) 93 #define PERF_DEV_NAME_BYTES 32 94 #define PERF_EVT_NAME_BYTES 32 95 96 #define INTEL_ECORE_TYPE 0x20 97 #define INTEL_PCORE_TYPE 0x40 98 99 #define ROUND_UP_TO_PAGE_SIZE(n) (((n) + 0x1000UL-1UL) & ~(0x1000UL-1UL)) 100 101 FILE *outf; 102 103 enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 104 enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC, COUNTER_K2M }; 105 enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT, FORMAT_AVERAGE }; 106 enum counter_source { COUNTER_SOURCE_NONE, COUNTER_SOURCE_PERF, COUNTER_SOURCE_MSR }; 107 108 struct perf_counter_info { 109 struct perf_counter_info *next; 110 111 /* How to open the counter / What counter it is. */ 112 char device[PERF_DEV_NAME_BYTES]; 113 char event[PERF_EVT_NAME_BYTES]; 114 115 /* How to show/format the counter. */ 116 char name[PERF_NAME_BYTES]; 117 unsigned int width; 118 enum counter_scope scope; 119 enum counter_type type; 120 enum counter_format format; 121 double scale; 122 123 /* For reading the counter. */ 124 int *fd_perf_per_domain; 125 size_t num_domains; 126 }; 127 128 struct sysfs_path { 129 char path[PATH_BYTES]; 130 int id; 131 struct sysfs_path *next; 132 }; 133 134 struct msr_counter { 135 unsigned int msr_num; 136 char name[NAME_BYTES]; 137 struct sysfs_path *sp; 138 unsigned int width; 139 enum counter_type type; 140 enum counter_format format; 141 struct msr_counter *next; 142 unsigned int flags; 143 #define SYSFS_PERCPU (1 << 1) 144 }; 145 static int use_android_msr_path; 146 147 struct msr_counter bic[] = { 148 { 0x0, "usec", NULL, 0, 0, 0, NULL, 0 }, 149 { 0x0, "Time_Of_Day_Seconds", NULL, 0, 0, 0, NULL, 0 }, 150 { 0x0, "Package", NULL, 0, 0, 0, NULL, 0 }, 151 { 0x0, "Node", NULL, 0, 0, 0, NULL, 0 }, 152 { 0x0, "Avg_MHz", NULL, 0, 0, 0, NULL, 0 }, 153 { 0x0, "Busy%", NULL, 0, 0, 0, NULL, 0 }, 154 { 0x0, "Bzy_MHz", NULL, 0, 0, 0, NULL, 0 }, 155 { 0x0, "TSC_MHz", NULL, 0, 0, 0, NULL, 0 }, 156 { 0x0, "IRQ", NULL, 0, 0, 0, NULL, 0 }, 157 { 0x0, "SMI", NULL, 32, 0, FORMAT_DELTA, NULL, 0 }, 158 { 0x0, "cpuidle", NULL, 0, 0, 0, NULL, 0 }, 159 { 0x0, "CPU%c1", NULL, 0, 0, 0, NULL, 0 }, 160 { 0x0, "CPU%c3", NULL, 0, 0, 0, NULL, 0 }, 161 { 0x0, "CPU%c6", NULL, 0, 0, 0, NULL, 0 }, 162 { 0x0, "CPU%c7", NULL, 0, 0, 0, NULL, 0 }, 163 { 0x0, "ThreadC", NULL, 0, 0, 0, NULL, 0 }, 164 { 0x0, "CoreTmp", NULL, 0, 0, 0, NULL, 0 }, 165 { 0x0, "CoreCnt", NULL, 0, 0, 0, NULL, 0 }, 166 { 0x0, "PkgTmp", NULL, 0, 0, 0, NULL, 0 }, 167 { 0x0, "GFX%rc6", NULL, 0, 0, 0, NULL, 0 }, 168 { 0x0, "GFXMHz", NULL, 0, 0, 0, NULL, 0 }, 169 { 0x0, "Pkg%pc2", NULL, 0, 0, 0, NULL, 0 }, 170 { 0x0, "Pkg%pc3", NULL, 0, 0, 0, NULL, 0 }, 171 { 0x0, "Pkg%pc6", NULL, 0, 0, 0, NULL, 0 }, 172 { 0x0, "Pkg%pc7", NULL, 0, 0, 0, NULL, 0 }, 173 { 0x0, "Pkg%pc8", NULL, 0, 0, 0, NULL, 0 }, 174 { 0x0, "Pkg%pc9", NULL, 0, 0, 0, NULL, 0 }, 175 { 0x0, "Pk%pc10", NULL, 0, 0, 0, NULL, 0 }, 176 { 0x0, "CPU%LPI", NULL, 0, 0, 0, NULL, 0 }, 177 { 0x0, "SYS%LPI", NULL, 0, 0, 0, NULL, 0 }, 178 { 0x0, "PkgWatt", NULL, 0, 0, 0, NULL, 0 }, 179 { 0x0, "CorWatt", NULL, 0, 0, 0, NULL, 0 }, 180 { 0x0, "GFXWatt", NULL, 0, 0, 0, NULL, 0 }, 181 { 0x0, "PkgCnt", NULL, 0, 0, 0, NULL, 0 }, 182 { 0x0, "RAMWatt", NULL, 0, 0, 0, NULL, 0 }, 183 { 0x0, "PKG_%", NULL, 0, 0, 0, NULL, 0 }, 184 { 0x0, "RAM_%", NULL, 0, 0, 0, NULL, 0 }, 185 { 0x0, "Pkg_J", NULL, 0, 0, 0, NULL, 0 }, 186 { 0x0, "Cor_J", NULL, 0, 0, 0, NULL, 0 }, 187 { 0x0, "GFX_J", NULL, 0, 0, 0, NULL, 0 }, 188 { 0x0, "RAM_J", NULL, 0, 0, 0, NULL, 0 }, 189 { 0x0, "Mod%c6", NULL, 0, 0, 0, NULL, 0 }, 190 { 0x0, "Totl%C0", NULL, 0, 0, 0, NULL, 0 }, 191 { 0x0, "Any%C0", NULL, 0, 0, 0, NULL, 0 }, 192 { 0x0, "GFX%C0", NULL, 0, 0, 0, NULL, 0 }, 193 { 0x0, "CPUGFX%", NULL, 0, 0, 0, NULL, 0 }, 194 { 0x0, "Module", NULL, 0, 0, 0, NULL, 0 }, 195 { 0x0, "Core", NULL, 0, 0, 0, NULL, 0 }, 196 { 0x0, "CPU", NULL, 0, 0, 0, NULL, 0 }, 197 { 0x0, "APIC", NULL, 0, 0, 0, NULL, 0 }, 198 { 0x0, "X2APIC", NULL, 0, 0, 0, NULL, 0 }, 199 { 0x0, "Die", NULL, 0, 0, 0, NULL, 0 }, 200 { 0x0, "L3", NULL, 0, 0, 0, NULL, 0 }, 201 { 0x0, "GFXAMHz", NULL, 0, 0, 0, NULL, 0 }, 202 { 0x0, "IPC", NULL, 0, 0, 0, NULL, 0 }, 203 { 0x0, "CoreThr", NULL, 0, 0, 0, NULL, 0 }, 204 { 0x0, "UncMHz", NULL, 0, 0, 0, NULL, 0 }, 205 { 0x0, "SAM%mc6", NULL, 0, 0, 0, NULL, 0 }, 206 { 0x0, "SAMMHz", NULL, 0, 0, 0, NULL, 0 }, 207 { 0x0, "SAMAMHz", NULL, 0, 0, 0, NULL, 0 }, 208 { 0x0, "Die%c6", NULL, 0, 0, 0, NULL, 0 }, 209 { 0x0, "SysWatt", NULL, 0, 0, 0, NULL, 0 }, 210 { 0x0, "Sys_J", NULL, 0, 0, 0, NULL, 0 }, 211 { 0x0, "NMI", NULL, 0, 0, 0, NULL, 0 }, 212 { 0x0, "CPU%c1e", NULL, 0, 0, 0, NULL, 0 }, 213 { 0x0, "pct_idle", NULL, 0, 0, 0, NULL, 0 }, 214 { 0x0, "LLCMRPS", NULL, 0, 0, 0, NULL, 0 }, 215 { 0x0, "LLC%hit", NULL, 0, 0, 0, NULL, 0 }, 216 { 0x0, "L2MRPS", NULL, 0, 0, 0, NULL, 0 }, 217 { 0x0, "L2%hit", NULL, 0, 0, 0, NULL, 0 }, 218 }; 219 220 /* n.b. bic_names must match the order in bic[], above */ 221 enum bic_names { 222 BIC_USEC, 223 BIC_TOD, 224 BIC_Package, 225 BIC_Node, 226 BIC_Avg_MHz, 227 BIC_Busy, 228 BIC_Bzy_MHz, 229 BIC_TSC_MHz, 230 BIC_IRQ, 231 BIC_SMI, 232 BIC_cpuidle, 233 BIC_CPU_c1, 234 BIC_CPU_c3, 235 BIC_CPU_c6, 236 BIC_CPU_c7, 237 BIC_ThreadC, 238 BIC_CoreTmp, 239 BIC_CoreCnt, 240 BIC_PkgTmp, 241 BIC_GFX_rc6, 242 BIC_GFXMHz, 243 BIC_Pkgpc2, 244 BIC_Pkgpc3, 245 BIC_Pkgpc6, 246 BIC_Pkgpc7, 247 BIC_Pkgpc8, 248 BIC_Pkgpc9, 249 BIC_Pkgpc10, 250 BIC_CPU_LPI, 251 BIC_SYS_LPI, 252 BIC_PkgWatt, 253 BIC_CorWatt, 254 BIC_GFXWatt, 255 BIC_PkgCnt, 256 BIC_RAMWatt, 257 BIC_PKG__, 258 BIC_RAM__, 259 BIC_Pkg_J, 260 BIC_Cor_J, 261 BIC_GFX_J, 262 BIC_RAM_J, 263 BIC_Mod_c6, 264 BIC_Totl_c0, 265 BIC_Any_c0, 266 BIC_GFX_c0, 267 BIC_CPUGFX, 268 BIC_Module, 269 BIC_Core, 270 BIC_CPU, 271 BIC_APIC, 272 BIC_X2APIC, 273 BIC_Die, 274 BIC_L3, 275 BIC_GFXACTMHz, 276 BIC_IPC, 277 BIC_CORE_THROT_CNT, 278 BIC_UNCORE_MHZ, 279 BIC_SAM_mc6, 280 BIC_SAMMHz, 281 BIC_SAMACTMHz, 282 BIC_Diec6, 283 BIC_SysWatt, 284 BIC_Sys_J, 285 BIC_NMI, 286 BIC_CPU_c1e, 287 BIC_pct_idle, 288 BIC_LLC_MRPS, 289 BIC_LLC_HIT, 290 BIC_L2_MRPS, 291 BIC_L2_HIT, 292 MAX_BIC 293 }; 294 295 typedef cpu_set_t bic_set_t; /* implement bic_set_t using cpu_set_t */ 296 297 /* 298 * debug helper 299 */ 300 static void print_bic_set(const char *s, const bic_set_t *set) 301 { 302 int i; 303 304 assert(MAX_BIC < CPU_SETSIZE); 305 306 fprintf(outf, "%s:", s); 307 308 for (i = 0; i < MAX_BIC; ++i) { 309 310 if (CPU_ISSET(i, set)) 311 fprintf(outf, " %s", bic[i].name); 312 } 313 fputc('\n', outf); 314 } 315 316 static bic_set_t bic_group_topology; 317 static bic_set_t bic_group_thermal_pwr; 318 static bic_set_t bic_group_frequency; 319 static bic_set_t bic_group_hw_idle; 320 static bic_set_t bic_group_sw_idle; 321 static bic_set_t bic_group_idle; 322 static bic_set_t bic_group_cache; 323 static bic_set_t bic_group_other; 324 static bic_set_t bic_group_disabled_by_default; 325 static bic_set_t bic_enabled; 326 static bic_set_t bic_present; 327 328 static void dump_bic_sets(void) 329 { 330 print_bic_set("bic_enabled", &bic_enabled); 331 print_bic_set("bic_present", &bic_present); 332 print_bic_set("bic_group_topology", &bic_group_topology); 333 print_bic_set("bic_group_thermal_pwr", &bic_group_thermal_pwr); 334 print_bic_set("bic_group_frequency", &bic_group_frequency); 335 print_bic_set("bic_group_hw_idle", &bic_group_hw_idle); 336 print_bic_set("bic_group_sw_idle", &bic_group_sw_idle); 337 print_bic_set("bic_group_idle", &bic_group_idle); 338 print_bic_set("bic_group_cache", &bic_group_cache); 339 print_bic_set("bic_group_other", &bic_group_other); 340 print_bic_set("bic_group_disabled_by_default", &bic_group_disabled_by_default); 341 } 342 343 /* modify */ 344 #define BIC_INIT(set) CPU_ZERO(set) 345 346 #define SET_BIC(COUNTER_NUMBER, set) CPU_SET(COUNTER_NUMBER, set) 347 #define CLR_BIC(COUNTER_NUMBER, set) CPU_CLR(COUNTER_NUMBER, set) 348 349 #define BIC_PRESENT(COUNTER_NUMBER) SET_BIC(COUNTER_NUMBER, &bic_present) 350 #define BIC_NOT_PRESENT(COUNTER_NUMBER) CPU_CLR(COUNTER_NUMBER, &bic_present) 351 352 /* test */ 353 #define BIC_IS_ENABLED(COUNTER_NUMBER) CPU_ISSET(COUNTER_NUMBER, &bic_enabled) 354 #define DO_BIC_READ(COUNTER_NUMBER) CPU_ISSET(COUNTER_NUMBER, &bic_present) 355 #define DO_BIC(COUNTER_NUMBER) (CPU_ISSET(COUNTER_NUMBER, &bic_enabled) && CPU_ISSET(COUNTER_NUMBER, &bic_present)) 356 357 static void bic_set_all(bic_set_t *set) 358 { 359 int i; 360 361 assert(MAX_BIC < CPU_SETSIZE); 362 363 for (i = 0; i < MAX_BIC; ++i) 364 SET_BIC(i, set); 365 } 366 367 /* 368 * bic_clear_bits() 369 * clear all the bits from "clr" in "dst" 370 */ 371 static void bic_clear_bits(bic_set_t *dst, bic_set_t *clr) 372 { 373 int i; 374 375 assert(MAX_BIC < CPU_SETSIZE); 376 377 for (i = 0; i < MAX_BIC; ++i) 378 if (CPU_ISSET(i, clr)) 379 CLR_BIC(i, dst); 380 } 381 382 static void bic_groups_init(void) 383 { 384 BIC_INIT(&bic_group_topology); 385 SET_BIC(BIC_Package, &bic_group_topology); 386 SET_BIC(BIC_Node, &bic_group_topology); 387 SET_BIC(BIC_CoreCnt, &bic_group_topology); 388 SET_BIC(BIC_PkgCnt, &bic_group_topology); 389 SET_BIC(BIC_Module, &bic_group_topology); 390 SET_BIC(BIC_Core, &bic_group_topology); 391 SET_BIC(BIC_CPU, &bic_group_topology); 392 SET_BIC(BIC_Die, &bic_group_topology); 393 SET_BIC(BIC_L3, &bic_group_topology); 394 395 BIC_INIT(&bic_group_thermal_pwr); 396 SET_BIC(BIC_CoreTmp, &bic_group_thermal_pwr); 397 SET_BIC(BIC_PkgTmp, &bic_group_thermal_pwr); 398 SET_BIC(BIC_PkgWatt, &bic_group_thermal_pwr); 399 SET_BIC(BIC_CorWatt, &bic_group_thermal_pwr); 400 SET_BIC(BIC_GFXWatt, &bic_group_thermal_pwr); 401 SET_BIC(BIC_RAMWatt, &bic_group_thermal_pwr); 402 SET_BIC(BIC_Pkg_J, &bic_group_thermal_pwr); 403 SET_BIC(BIC_Cor_J, &bic_group_thermal_pwr); 404 SET_BIC(BIC_GFX_J, &bic_group_thermal_pwr); 405 SET_BIC(BIC_RAM_J, &bic_group_thermal_pwr); 406 SET_BIC(BIC_Sys_J, &bic_group_thermal_pwr); 407 SET_BIC(BIC_PKG__, &bic_group_thermal_pwr); 408 SET_BIC(BIC_RAM__, &bic_group_thermal_pwr); 409 SET_BIC(BIC_SysWatt, &bic_group_thermal_pwr); 410 411 BIC_INIT(&bic_group_frequency); 412 SET_BIC(BIC_Avg_MHz, &bic_group_frequency); 413 SET_BIC(BIC_Busy, &bic_group_frequency); 414 SET_BIC(BIC_Bzy_MHz, &bic_group_frequency); 415 SET_BIC(BIC_TSC_MHz, &bic_group_frequency); 416 SET_BIC(BIC_GFXMHz, &bic_group_frequency); 417 SET_BIC(BIC_GFXACTMHz, &bic_group_frequency); 418 SET_BIC(BIC_SAMMHz, &bic_group_frequency); 419 SET_BIC(BIC_SAMACTMHz, &bic_group_frequency); 420 SET_BIC(BIC_UNCORE_MHZ, &bic_group_frequency); 421 422 BIC_INIT(&bic_group_hw_idle); 423 SET_BIC(BIC_Busy, &bic_group_hw_idle); 424 SET_BIC(BIC_CPU_c1, &bic_group_hw_idle); 425 SET_BIC(BIC_CPU_c3, &bic_group_hw_idle); 426 SET_BIC(BIC_CPU_c6, &bic_group_hw_idle); 427 SET_BIC(BIC_CPU_c7, &bic_group_hw_idle); 428 SET_BIC(BIC_GFX_rc6, &bic_group_hw_idle); 429 SET_BIC(BIC_Pkgpc2, &bic_group_hw_idle); 430 SET_BIC(BIC_Pkgpc3, &bic_group_hw_idle); 431 SET_BIC(BIC_Pkgpc6, &bic_group_hw_idle); 432 SET_BIC(BIC_Pkgpc7, &bic_group_hw_idle); 433 SET_BIC(BIC_Pkgpc8, &bic_group_hw_idle); 434 SET_BIC(BIC_Pkgpc9, &bic_group_hw_idle); 435 SET_BIC(BIC_Pkgpc10, &bic_group_hw_idle); 436 SET_BIC(BIC_CPU_LPI, &bic_group_hw_idle); 437 SET_BIC(BIC_SYS_LPI, &bic_group_hw_idle); 438 SET_BIC(BIC_Mod_c6, &bic_group_hw_idle); 439 SET_BIC(BIC_Totl_c0, &bic_group_hw_idle); 440 SET_BIC(BIC_Any_c0, &bic_group_hw_idle); 441 SET_BIC(BIC_GFX_c0, &bic_group_hw_idle); 442 SET_BIC(BIC_CPUGFX, &bic_group_hw_idle); 443 SET_BIC(BIC_SAM_mc6, &bic_group_hw_idle); 444 SET_BIC(BIC_Diec6, &bic_group_hw_idle); 445 446 BIC_INIT(&bic_group_sw_idle); 447 SET_BIC(BIC_Busy, &bic_group_sw_idle); 448 SET_BIC(BIC_cpuidle, &bic_group_sw_idle); 449 SET_BIC(BIC_pct_idle, &bic_group_sw_idle); 450 451 BIC_INIT(&bic_group_idle); 452 453 CPU_OR(&bic_group_idle, &bic_group_idle, &bic_group_hw_idle); 454 SET_BIC(BIC_pct_idle, &bic_group_idle); 455 456 BIC_INIT(&bic_group_cache); 457 SET_BIC(BIC_LLC_MRPS, &bic_group_cache); 458 SET_BIC(BIC_LLC_HIT, &bic_group_cache); 459 SET_BIC(BIC_L2_MRPS, &bic_group_cache); 460 SET_BIC(BIC_L2_HIT, &bic_group_cache); 461 462 BIC_INIT(&bic_group_other); 463 SET_BIC(BIC_IRQ, &bic_group_other); 464 SET_BIC(BIC_NMI, &bic_group_other); 465 SET_BIC(BIC_SMI, &bic_group_other); 466 SET_BIC(BIC_ThreadC, &bic_group_other); 467 SET_BIC(BIC_CoreTmp, &bic_group_other); 468 SET_BIC(BIC_IPC, &bic_group_other); 469 470 BIC_INIT(&bic_group_disabled_by_default); 471 SET_BIC(BIC_USEC, &bic_group_disabled_by_default); 472 SET_BIC(BIC_TOD, &bic_group_disabled_by_default); 473 SET_BIC(BIC_cpuidle, &bic_group_disabled_by_default); 474 SET_BIC(BIC_APIC, &bic_group_disabled_by_default); 475 SET_BIC(BIC_X2APIC, &bic_group_disabled_by_default); 476 477 BIC_INIT(&bic_enabled); 478 bic_set_all(&bic_enabled); 479 bic_clear_bits(&bic_enabled, &bic_group_disabled_by_default); 480 481 BIC_INIT(&bic_present); 482 SET_BIC(BIC_USEC, &bic_present); 483 SET_BIC(BIC_TOD, &bic_present); 484 SET_BIC(BIC_cpuidle, &bic_present); 485 SET_BIC(BIC_APIC, &bic_present); 486 SET_BIC(BIC_X2APIC, &bic_present); 487 SET_BIC(BIC_pct_idle, &bic_present); 488 } 489 490 /* 491 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 492 * If you change the values, note they are used both in comparisons 493 * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 494 */ 495 #define PCLUKN 0 /* Unknown */ 496 #define PCLRSV 1 /* Reserved */ 497 #define PCL__0 2 /* PC0 */ 498 #define PCL__1 3 /* PC1 */ 499 #define PCL__2 4 /* PC2 */ 500 #define PCL__3 5 /* PC3 */ 501 #define PCL__4 6 /* PC4 */ 502 #define PCL__6 7 /* PC6 */ 503 #define PCL_6N 8 /* PC6 No Retention */ 504 #define PCL_6R 9 /* PC6 Retention */ 505 #define PCL__7 10 /* PC7 */ 506 #define PCL_7S 11 /* PC7 Shrink */ 507 #define PCL__8 12 /* PC8 */ 508 #define PCL__9 13 /* PC9 */ 509 #define PCL_10 14 /* PC10 */ 510 #define PCLUNL 15 /* Unlimited */ 511 512 char *proc_stat = "/proc/stat"; 513 int *fd_percpu; 514 int *fd_instr_count_percpu; 515 int *fd_llc_percpu; 516 int *fd_l2_percpu; 517 struct timeval interval_tv = { 5, 0 }; 518 struct timespec interval_ts = { 5, 0 }; 519 520 unsigned int num_iterations; 521 unsigned int header_iterations; 522 unsigned int debug; 523 unsigned int quiet; 524 unsigned int shown; 525 unsigned int sums_need_wide_columns; 526 unsigned int rapl_joules; 527 unsigned int valid_rapl_msrs; 528 unsigned int summary_only; 529 unsigned int list_header_only; 530 unsigned int dump_only; 531 bool force_load; 532 unsigned int cpuid_has_aperf_mperf; 533 unsigned int cpuid_has_hv; 534 unsigned int has_aperf_access; 535 unsigned int has_epb; 536 unsigned int has_turbo; 537 unsigned int is_hybrid; 538 unsigned int units = 1000000; /* MHz etc */ 539 unsigned int genuine_intel; 540 unsigned int authentic_amd; 541 unsigned int hygon_genuine; 542 unsigned int max_level, max_extended_level; 543 unsigned int has_invariant_tsc; 544 unsigned int aperf_mperf_multiplier = 1; 545 double bclk; 546 double base_hz; 547 unsigned int has_base_hz; 548 double tsc_tweak = 1.0; 549 unsigned int show_pkg_only; 550 unsigned int show_core_only; 551 char *output_buffer, *outp; 552 unsigned int do_dts; 553 unsigned int do_ptm; 554 unsigned int do_ipc; 555 unsigned long long cpuidle_cur_cpu_lpi_us; 556 unsigned long long cpuidle_cur_sys_lpi_us; 557 unsigned int tj_max; 558 unsigned int tj_max_override; 559 double rapl_power_units, rapl_time_units; 560 double rapl_dram_energy_units, rapl_energy_units, rapl_psys_energy_units; 561 double rapl_joule_counter_range; 562 unsigned int crystal_hz; 563 unsigned long long tsc_hz; 564 int master_cpu; 565 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 566 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 567 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 568 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 569 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 570 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 571 unsigned int first_counter_read = 1; 572 573 static struct timeval procsysfs_tv_begin; 574 575 int ignore_stdin; 576 bool no_msr; 577 bool no_perf; 578 579 enum gfx_sysfs_idx { 580 GFX_rc6, 581 GFX_MHz, 582 GFX_ACTMHz, 583 SAM_mc6, 584 SAM_MHz, 585 SAM_ACTMHz, 586 GFX_MAX 587 }; 588 589 struct gfx_sysfs_info { 590 FILE *fp; 591 unsigned int val; 592 unsigned long long val_ull; 593 }; 594 595 static struct gfx_sysfs_info gfx_info[GFX_MAX]; 596 597 void get_msr(int cpu, off_t offset, unsigned long long *msr); 598 int add_counter(unsigned int msr_num, const char *path, const char *name, 599 unsigned int width, enum counter_scope scope, enum counter_type type, enum counter_format format, int flags, int id); 600 601 /* Model specific support Start */ 602 603 /* List of features that may diverge among different platforms */ 604 struct platform_features { 605 bool has_msr_misc_feature_control; /* MSR_MISC_FEATURE_CONTROL */ 606 bool has_msr_misc_pwr_mgmt; /* MSR_MISC_PWR_MGMT */ 607 bool has_nhm_msrs; /* MSR_PLATFORM_INFO, MSR_IA32_TEMPERATURE_TARGET, MSR_SMI_COUNT, MSR_PKG_CST_CONFIG_CONTROL, MSR_IA32_POWER_CTL, TRL MSRs */ 608 bool has_config_tdp; /* MSR_CONFIG_TDP_NOMINAL/LEVEL_1/LEVEL_2/CONTROL, MSR_TURBO_ACTIVATION_RATIO */ 609 int bclk_freq; /* CPU base clock */ 610 int crystal_freq; /* Crystal clock to use when not available from CPUID.15 */ 611 int supported_cstates; /* Core cstates and Package cstates supported */ 612 int cst_limit; /* MSR_PKG_CST_CONFIG_CONTROL */ 613 bool has_cst_auto_convension; /* AUTOMATIC_CSTATE_CONVERSION bit in MSR_PKG_CST_CONFIG_CONTROL */ 614 bool has_irtl_msrs; /* MSR_PKGC3/PKGC6/PKGC7/PKGC8/PKGC9/PKGC10_IRTL */ 615 bool has_msr_core_c1_res; /* MSR_CORE_C1_RES */ 616 bool has_msr_module_c6_res_ms; /* MSR_MODULE_C6_RES_MS */ 617 bool has_msr_c6_demotion_policy_config; /* MSR_CC6_DEMOTION_POLICY_CONFIG/MSR_MC6_DEMOTION_POLICY_CONFIG */ 618 bool has_msr_atom_pkg_c6_residency; /* MSR_ATOM_PKG_C6_RESIDENCY */ 619 bool has_msr_knl_core_c6_residency; /* MSR_KNL_CORE_C6_RESIDENCY */ 620 bool has_ext_cst_msrs; /* MSR_PKG_WEIGHTED_CORE_C0_RES/MSR_PKG_ANY_CORE_C0_RES/MSR_PKG_ANY_GFXE_C0_RES/MSR_PKG_BOTH_CORE_GFXE_C0_RES */ 621 bool has_cst_prewake_bit; /* Cstate prewake bit in MSR_IA32_POWER_CTL */ 622 int trl_msrs; /* MSR_TURBO_RATIO_LIMIT/LIMIT1/LIMIT2/SECONDARY, Atom TRL MSRs */ 623 int plr_msrs; /* MSR_CORE/GFX/RING_PERF_LIMIT_REASONS */ 624 int plat_rapl_msrs; /* RAPL PKG/DRAM/CORE/GFX MSRs, AMD RAPL MSRs */ 625 bool has_per_core_rapl; /* Indicates cores energy collection is per-core, not per-package. AMD specific for now */ 626 bool has_rapl_divisor; /* Divisor for Energy unit raw value from MSR_RAPL_POWER_UNIT */ 627 bool has_fixed_rapl_unit; /* Fixed Energy Unit used for DRAM RAPL Domain */ 628 bool has_fixed_rapl_psys_unit; /* Fixed Energy Unit used for PSYS RAPL Domain */ 629 int rapl_quirk_tdp; /* Hardcoded TDP value when cannot be retrieved from hardware */ 630 int tcc_offset_bits; /* TCC Offset bits in MSR_IA32_TEMPERATURE_TARGET */ 631 bool enable_tsc_tweak; /* Use CPU Base freq instead of TSC freq for aperf/mperf counter */ 632 bool need_perf_multiplier; /* mperf/aperf multiplier */ 633 }; 634 635 struct platform_data { 636 unsigned int vfm; 637 const struct platform_features *features; 638 }; 639 640 /* For BCLK */ 641 enum bclk_freq { 642 BCLK_100MHZ = 1, 643 BCLK_133MHZ, 644 BCLK_SLV, 645 }; 646 647 #define SLM_BCLK_FREQS 5 648 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 649 650 double slm_bclk(void) 651 { 652 unsigned long long msr = 3; 653 unsigned int i; 654 double freq; 655 656 get_msr(master_cpu, MSR_FSB_FREQ, &msr); 657 658 i = msr & 0xf; 659 if (i >= SLM_BCLK_FREQS) { 660 fprintf(outf, "SLM BCLK[%d] invalid\n", i); 661 i = 3; 662 } 663 freq = slm_freq_table[i]; 664 665 if (!quiet) 666 fprintf(outf, "SLM BCLK: %.1f MHz\n", freq); 667 668 return freq; 669 } 670 671 /* For Package cstate limit */ 672 enum package_cstate_limit { 673 CST_LIMIT_NHM = 1, 674 CST_LIMIT_SNB, 675 CST_LIMIT_HSW, 676 CST_LIMIT_SKX, 677 CST_LIMIT_ICX, 678 CST_LIMIT_SLV, 679 CST_LIMIT_AMT, 680 CST_LIMIT_KNL, 681 CST_LIMIT_GMT, 682 }; 683 684 /* For Turbo Ratio Limit MSRs */ 685 enum turbo_ratio_limit_msrs { 686 TRL_BASE = BIT(0), 687 TRL_LIMIT1 = BIT(1), 688 TRL_LIMIT2 = BIT(2), 689 TRL_ATOM = BIT(3), 690 TRL_KNL = BIT(4), 691 TRL_CORECOUNT = BIT(5), 692 }; 693 694 /* For Perf Limit Reason MSRs */ 695 enum perf_limit_reason_msrs { 696 PLR_CORE = BIT(0), 697 PLR_GFX = BIT(1), 698 PLR_RING = BIT(2), 699 }; 700 701 /* For RAPL MSRs */ 702 enum rapl_msrs { 703 RAPL_PKG_POWER_LIMIT = BIT(0), /* 0x610 MSR_PKG_POWER_LIMIT */ 704 RAPL_PKG_ENERGY_STATUS = BIT(1), /* 0x611 MSR_PKG_ENERGY_STATUS */ 705 RAPL_PKG_PERF_STATUS = BIT(2), /* 0x613 MSR_PKG_PERF_STATUS */ 706 RAPL_PKG_POWER_INFO = BIT(3), /* 0x614 MSR_PKG_POWER_INFO */ 707 RAPL_DRAM_POWER_LIMIT = BIT(4), /* 0x618 MSR_DRAM_POWER_LIMIT */ 708 RAPL_DRAM_ENERGY_STATUS = BIT(5), /* 0x619 MSR_DRAM_ENERGY_STATUS */ 709 RAPL_DRAM_PERF_STATUS = BIT(6), /* 0x61b MSR_DRAM_PERF_STATUS */ 710 RAPL_DRAM_POWER_INFO = BIT(7), /* 0x61c MSR_DRAM_POWER_INFO */ 711 RAPL_CORE_POWER_LIMIT = BIT(8), /* 0x638 MSR_PP0_POWER_LIMIT */ 712 RAPL_CORE_ENERGY_STATUS = BIT(9), /* 0x639 MSR_PP0_ENERGY_STATUS */ 713 RAPL_CORE_POLICY = BIT(10), /* 0x63a MSR_PP0_POLICY */ 714 RAPL_GFX_POWER_LIMIT = BIT(11), /* 0x640 MSR_PP1_POWER_LIMIT */ 715 RAPL_GFX_ENERGY_STATUS = BIT(12), /* 0x641 MSR_PP1_ENERGY_STATUS */ 716 RAPL_GFX_POLICY = BIT(13), /* 0x642 MSR_PP1_POLICY */ 717 RAPL_AMD_PWR_UNIT = BIT(14), /* 0xc0010299 MSR_AMD_RAPL_POWER_UNIT */ 718 RAPL_AMD_CORE_ENERGY_STAT = BIT(15), /* 0xc001029a MSR_AMD_CORE_ENERGY_STATUS */ 719 RAPL_AMD_PKG_ENERGY_STAT = BIT(16), /* 0xc001029b MSR_AMD_PKG_ENERGY_STATUS */ 720 RAPL_PLATFORM_ENERGY_LIMIT = BIT(17), /* 0x64c MSR_PLATFORM_ENERGY_LIMIT */ 721 RAPL_PLATFORM_ENERGY_STATUS = BIT(18), /* 0x64d MSR_PLATFORM_ENERGY_STATUS */ 722 }; 723 724 #define RAPL_PKG (RAPL_PKG_ENERGY_STATUS | RAPL_PKG_POWER_LIMIT) 725 #define RAPL_DRAM (RAPL_DRAM_ENERGY_STATUS | RAPL_DRAM_POWER_LIMIT) 726 #define RAPL_CORE (RAPL_CORE_ENERGY_STATUS | RAPL_CORE_POWER_LIMIT) 727 #define RAPL_GFX (RAPL_GFX_POWER_LIMIT | RAPL_GFX_ENERGY_STATUS) 728 #define RAPL_PSYS (RAPL_PLATFORM_ENERGY_STATUS | RAPL_PLATFORM_ENERGY_LIMIT) 729 730 #define RAPL_PKG_ALL (RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO) 731 #define RAPL_DRAM_ALL (RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_DRAM_POWER_INFO) 732 #define RAPL_CORE_ALL (RAPL_CORE | RAPL_CORE_POLICY) 733 #define RAPL_GFX_ALL (RAPL_GFX | RAPL_GFX_POLICY) 734 735 #define RAPL_AMD_F17H (RAPL_AMD_PWR_UNIT | RAPL_AMD_CORE_ENERGY_STAT | RAPL_AMD_PKG_ENERGY_STAT) 736 737 /* For Cstates */ 738 enum cstates { 739 CC1 = BIT(0), 740 CC3 = BIT(1), 741 CC6 = BIT(2), 742 CC7 = BIT(3), 743 PC2 = BIT(4), 744 PC3 = BIT(5), 745 PC6 = BIT(6), 746 PC7 = BIT(7), 747 PC8 = BIT(8), 748 PC9 = BIT(9), 749 PC10 = BIT(10), 750 }; 751 752 static const struct platform_features nhm_features = { 753 .has_msr_misc_pwr_mgmt = 1, 754 .has_nhm_msrs = 1, 755 .bclk_freq = BCLK_133MHZ, 756 .supported_cstates = CC1 | CC3 | CC6 | PC3 | PC6, 757 .cst_limit = CST_LIMIT_NHM, 758 .trl_msrs = TRL_BASE, 759 }; 760 761 static const struct platform_features nhx_features = { 762 .has_msr_misc_pwr_mgmt = 1, 763 .has_nhm_msrs = 1, 764 .bclk_freq = BCLK_133MHZ, 765 .supported_cstates = CC1 | CC3 | CC6 | PC3 | PC6, 766 .cst_limit = CST_LIMIT_NHM, 767 }; 768 769 static const struct platform_features snb_features = { 770 .has_msr_misc_feature_control = 1, 771 .has_msr_misc_pwr_mgmt = 1, 772 .has_nhm_msrs = 1, 773 .bclk_freq = BCLK_100MHZ, 774 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 775 .cst_limit = CST_LIMIT_SNB, 776 .has_irtl_msrs = 1, 777 .trl_msrs = TRL_BASE, 778 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 779 }; 780 781 static const struct platform_features snx_features = { 782 .has_msr_misc_feature_control = 1, 783 .has_msr_misc_pwr_mgmt = 1, 784 .has_nhm_msrs = 1, 785 .bclk_freq = BCLK_100MHZ, 786 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 787 .cst_limit = CST_LIMIT_SNB, 788 .has_irtl_msrs = 1, 789 .trl_msrs = TRL_BASE, 790 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_CORE_ALL | RAPL_DRAM_ALL, 791 }; 792 793 static const struct platform_features ivb_features = { 794 .has_msr_misc_feature_control = 1, 795 .has_msr_misc_pwr_mgmt = 1, 796 .has_nhm_msrs = 1, 797 .has_config_tdp = 1, 798 .bclk_freq = BCLK_100MHZ, 799 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 800 .cst_limit = CST_LIMIT_SNB, 801 .has_irtl_msrs = 1, 802 .trl_msrs = TRL_BASE, 803 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 804 }; 805 806 static const struct platform_features ivx_features = { 807 .has_msr_misc_feature_control = 1, 808 .has_msr_misc_pwr_mgmt = 1, 809 .has_nhm_msrs = 1, 810 .bclk_freq = BCLK_100MHZ, 811 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 812 .cst_limit = CST_LIMIT_SNB, 813 .has_irtl_msrs = 1, 814 .trl_msrs = TRL_BASE | TRL_LIMIT1, 815 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_CORE_ALL | RAPL_DRAM_ALL, 816 }; 817 818 static const struct platform_features hsw_features = { 819 .has_msr_misc_feature_control = 1, 820 .has_msr_misc_pwr_mgmt = 1, 821 .has_nhm_msrs = 1, 822 .has_config_tdp = 1, 823 .bclk_freq = BCLK_100MHZ, 824 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 825 .cst_limit = CST_LIMIT_HSW, 826 .has_irtl_msrs = 1, 827 .trl_msrs = TRL_BASE, 828 .plr_msrs = PLR_CORE | PLR_GFX | PLR_RING, 829 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 830 }; 831 832 static const struct platform_features hsx_features = { 833 .has_msr_misc_feature_control = 1, 834 .has_msr_misc_pwr_mgmt = 1, 835 .has_nhm_msrs = 1, 836 .has_config_tdp = 1, 837 .bclk_freq = BCLK_100MHZ, 838 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 839 .cst_limit = CST_LIMIT_HSW, 840 .has_irtl_msrs = 1, 841 .trl_msrs = TRL_BASE | TRL_LIMIT1 | TRL_LIMIT2, 842 .plr_msrs = PLR_CORE | PLR_RING, 843 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL, 844 .has_fixed_rapl_unit = 1, 845 }; 846 847 static const struct platform_features hswl_features = { 848 .has_msr_misc_feature_control = 1, 849 .has_msr_misc_pwr_mgmt = 1, 850 .has_nhm_msrs = 1, 851 .has_config_tdp = 1, 852 .bclk_freq = BCLK_100MHZ, 853 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 854 .cst_limit = CST_LIMIT_HSW, 855 .has_irtl_msrs = 1, 856 .trl_msrs = TRL_BASE, 857 .plr_msrs = PLR_CORE | PLR_GFX | PLR_RING, 858 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 859 }; 860 861 static const struct platform_features hswg_features = { 862 .has_msr_misc_feature_control = 1, 863 .has_msr_misc_pwr_mgmt = 1, 864 .has_nhm_msrs = 1, 865 .has_config_tdp = 1, 866 .bclk_freq = BCLK_100MHZ, 867 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 868 .cst_limit = CST_LIMIT_HSW, 869 .has_irtl_msrs = 1, 870 .trl_msrs = TRL_BASE, 871 .plr_msrs = PLR_CORE | PLR_GFX | PLR_RING, 872 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 873 }; 874 875 static const struct platform_features bdw_features = { 876 .has_msr_misc_feature_control = 1, 877 .has_msr_misc_pwr_mgmt = 1, 878 .has_nhm_msrs = 1, 879 .has_config_tdp = 1, 880 .bclk_freq = BCLK_100MHZ, 881 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 882 .cst_limit = CST_LIMIT_HSW, 883 .has_irtl_msrs = 1, 884 .trl_msrs = TRL_BASE, 885 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 886 }; 887 888 static const struct platform_features bdwg_features = { 889 .has_msr_misc_feature_control = 1, 890 .has_msr_misc_pwr_mgmt = 1, 891 .has_nhm_msrs = 1, 892 .has_config_tdp = 1, 893 .bclk_freq = BCLK_100MHZ, 894 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7, 895 .cst_limit = CST_LIMIT_HSW, 896 .has_irtl_msrs = 1, 897 .trl_msrs = TRL_BASE, 898 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE_ALL | RAPL_GFX | RAPL_PKG_POWER_INFO, 899 }; 900 901 static const struct platform_features bdx_features = { 902 .has_msr_misc_feature_control = 1, 903 .has_msr_misc_pwr_mgmt = 1, 904 .has_nhm_msrs = 1, 905 .has_config_tdp = 1, 906 .bclk_freq = BCLK_100MHZ, 907 .supported_cstates = CC1 | CC3 | CC6 | PC2 | PC3 | PC6, 908 .cst_limit = CST_LIMIT_HSW, 909 .has_irtl_msrs = 1, 910 .has_cst_auto_convension = 1, 911 .trl_msrs = TRL_BASE, 912 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL, 913 .has_fixed_rapl_unit = 1, 914 }; 915 916 static const struct platform_features skl_features = { 917 .has_msr_misc_feature_control = 1, 918 .has_msr_misc_pwr_mgmt = 1, 919 .has_nhm_msrs = 1, 920 .has_config_tdp = 1, 921 .bclk_freq = BCLK_100MHZ, 922 .crystal_freq = 24000000, 923 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 924 .cst_limit = CST_LIMIT_HSW, 925 .has_irtl_msrs = 1, 926 .has_ext_cst_msrs = 1, 927 .trl_msrs = TRL_BASE, 928 .tcc_offset_bits = 6, 929 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_CORE_ALL | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_GFX | RAPL_PSYS, 930 .enable_tsc_tweak = 1, 931 }; 932 933 static const struct platform_features cnl_features = { 934 .has_msr_misc_feature_control = 1, 935 .has_msr_misc_pwr_mgmt = 1, 936 .has_nhm_msrs = 1, 937 .has_config_tdp = 1, 938 .bclk_freq = BCLK_100MHZ, 939 .supported_cstates = CC1 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 940 .cst_limit = CST_LIMIT_HSW, 941 .has_irtl_msrs = 1, 942 .has_msr_core_c1_res = 1, 943 .has_ext_cst_msrs = 1, 944 .trl_msrs = TRL_BASE, 945 .tcc_offset_bits = 6, 946 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_CORE_ALL | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_GFX | RAPL_PSYS, 947 .enable_tsc_tweak = 1, 948 }; 949 950 /* Copied from cnl_features, with PC7/PC9 removed */ 951 static const struct platform_features adl_features = { 952 .has_msr_misc_feature_control = cnl_features.has_msr_misc_feature_control, 953 .has_msr_misc_pwr_mgmt = cnl_features.has_msr_misc_pwr_mgmt, 954 .has_nhm_msrs = cnl_features.has_nhm_msrs, 955 .has_config_tdp = cnl_features.has_config_tdp, 956 .bclk_freq = cnl_features.bclk_freq, 957 .supported_cstates = CC1 | CC6 | CC7 | PC2 | PC3 | PC6 | PC8 | PC10, 958 .cst_limit = cnl_features.cst_limit, 959 .has_irtl_msrs = cnl_features.has_irtl_msrs, 960 .has_msr_core_c1_res = cnl_features.has_msr_core_c1_res, 961 .has_ext_cst_msrs = cnl_features.has_ext_cst_msrs, 962 .trl_msrs = cnl_features.trl_msrs, 963 .tcc_offset_bits = cnl_features.tcc_offset_bits, 964 .plat_rapl_msrs = cnl_features.plat_rapl_msrs, 965 .enable_tsc_tweak = cnl_features.enable_tsc_tweak, 966 }; 967 968 /* Copied from adl_features, with PC3/PC8 removed */ 969 static const struct platform_features lnl_features = { 970 .has_msr_misc_feature_control = adl_features.has_msr_misc_feature_control, 971 .has_msr_misc_pwr_mgmt = adl_features.has_msr_misc_pwr_mgmt, 972 .has_nhm_msrs = adl_features.has_nhm_msrs, 973 .has_config_tdp = adl_features.has_config_tdp, 974 .bclk_freq = adl_features.bclk_freq, 975 .supported_cstates = CC1 | CC6 | CC7 | PC2 | PC6 | PC10, 976 .cst_limit = adl_features.cst_limit, 977 .has_irtl_msrs = adl_features.has_irtl_msrs, 978 .has_msr_core_c1_res = adl_features.has_msr_core_c1_res, 979 .has_ext_cst_msrs = adl_features.has_ext_cst_msrs, 980 .trl_msrs = adl_features.trl_msrs, 981 .tcc_offset_bits = adl_features.tcc_offset_bits, 982 .plat_rapl_msrs = adl_features.plat_rapl_msrs, 983 .enable_tsc_tweak = adl_features.enable_tsc_tweak, 984 }; 985 986 static const struct platform_features skx_features = { 987 .has_msr_misc_feature_control = 1, 988 .has_msr_misc_pwr_mgmt = 1, 989 .has_nhm_msrs = 1, 990 .has_config_tdp = 1, 991 .bclk_freq = BCLK_100MHZ, 992 .supported_cstates = CC1 | CC6 | PC2 | PC6, 993 .cst_limit = CST_LIMIT_SKX, 994 .has_irtl_msrs = 1, 995 .has_cst_auto_convension = 1, 996 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 997 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL, 998 .has_fixed_rapl_unit = 1, 999 }; 1000 1001 static const struct platform_features icx_features = { 1002 .has_msr_misc_feature_control = 1, 1003 .has_msr_misc_pwr_mgmt = 1, 1004 .has_nhm_msrs = 1, 1005 .has_config_tdp = 1, 1006 .bclk_freq = BCLK_100MHZ, 1007 .supported_cstates = CC1 | CC6 | PC2 | PC6, 1008 .cst_limit = CST_LIMIT_ICX, 1009 .has_msr_core_c1_res = 1, 1010 .has_irtl_msrs = 1, 1011 .has_cst_prewake_bit = 1, 1012 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1013 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL | RAPL_PSYS, 1014 .has_fixed_rapl_unit = 1, 1015 }; 1016 1017 static const struct platform_features spr_features = { 1018 .has_msr_misc_feature_control = 1, 1019 .has_msr_misc_pwr_mgmt = 1, 1020 .has_nhm_msrs = 1, 1021 .has_config_tdp = 1, 1022 .bclk_freq = BCLK_100MHZ, 1023 .supported_cstates = CC1 | CC6 | PC2 | PC6, 1024 .cst_limit = CST_LIMIT_SKX, 1025 .has_msr_core_c1_res = 1, 1026 .has_irtl_msrs = 1, 1027 .has_cst_prewake_bit = 1, 1028 .has_fixed_rapl_psys_unit = 1, 1029 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1030 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL | RAPL_PSYS, 1031 }; 1032 1033 static const struct platform_features dmr_features = { 1034 .has_msr_misc_feature_control = spr_features.has_msr_misc_feature_control, 1035 .has_msr_misc_pwr_mgmt = spr_features.has_msr_misc_pwr_mgmt, 1036 .has_nhm_msrs = spr_features.has_nhm_msrs, 1037 .bclk_freq = spr_features.bclk_freq, 1038 .supported_cstates = spr_features.supported_cstates, 1039 .cst_limit = spr_features.cst_limit, 1040 .has_msr_core_c1_res = spr_features.has_msr_core_c1_res, 1041 .has_cst_prewake_bit = spr_features.has_cst_prewake_bit, 1042 .has_fixed_rapl_psys_unit = spr_features.has_fixed_rapl_psys_unit, 1043 .trl_msrs = spr_features.trl_msrs, 1044 .has_msr_module_c6_res_ms = 1, /* DMR has Dual-Core-Module and MC6 MSR */ 1045 .plat_rapl_msrs = 0, /* DMR does not have RAPL MSRs */ 1046 .plr_msrs = 0, /* DMR does not have PLR MSRs */ 1047 .has_irtl_msrs = 0, /* DMR does not have IRTL MSRs */ 1048 .has_config_tdp = 0, /* DMR does not have CTDP MSRs */ 1049 }; 1050 1051 static const struct platform_features srf_features = { 1052 .has_msr_misc_feature_control = 1, 1053 .has_msr_misc_pwr_mgmt = 1, 1054 .has_nhm_msrs = 1, 1055 .has_config_tdp = 1, 1056 .bclk_freq = BCLK_100MHZ, 1057 .supported_cstates = CC1 | CC6 | PC2 | PC6, 1058 .cst_limit = CST_LIMIT_SKX, 1059 .has_msr_core_c1_res = 1, 1060 .has_msr_module_c6_res_ms = 1, 1061 .has_irtl_msrs = 1, 1062 .has_cst_prewake_bit = 1, 1063 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1064 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL | RAPL_PSYS, 1065 }; 1066 1067 static const struct platform_features grr_features = { 1068 .has_msr_misc_feature_control = 1, 1069 .has_msr_misc_pwr_mgmt = 1, 1070 .has_nhm_msrs = 1, 1071 .has_config_tdp = 1, 1072 .bclk_freq = BCLK_100MHZ, 1073 .supported_cstates = CC1 | CC6, 1074 .cst_limit = CST_LIMIT_SKX, 1075 .has_msr_core_c1_res = 1, 1076 .has_msr_module_c6_res_ms = 1, 1077 .has_irtl_msrs = 1, 1078 .has_cst_prewake_bit = 1, 1079 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1080 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL | RAPL_PSYS, 1081 }; 1082 1083 static const struct platform_features slv_features = { 1084 .has_nhm_msrs = 1, 1085 .bclk_freq = BCLK_SLV, 1086 .supported_cstates = CC1 | CC6 | PC6, 1087 .cst_limit = CST_LIMIT_SLV, 1088 .has_msr_core_c1_res = 1, 1089 .has_msr_module_c6_res_ms = 1, 1090 .has_msr_c6_demotion_policy_config = 1, 1091 .has_msr_atom_pkg_c6_residency = 1, 1092 .trl_msrs = TRL_ATOM, 1093 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE, 1094 .has_rapl_divisor = 1, 1095 .rapl_quirk_tdp = 30, 1096 }; 1097 1098 static const struct platform_features slvd_features = { 1099 .has_msr_misc_pwr_mgmt = 1, 1100 .has_nhm_msrs = 1, 1101 .bclk_freq = BCLK_SLV, 1102 .supported_cstates = CC1 | CC6 | PC3 | PC6, 1103 .cst_limit = CST_LIMIT_SLV, 1104 .has_msr_atom_pkg_c6_residency = 1, 1105 .trl_msrs = TRL_BASE, 1106 .plat_rapl_msrs = RAPL_PKG | RAPL_CORE, 1107 .rapl_quirk_tdp = 30, 1108 }; 1109 1110 static const struct platform_features amt_features = { 1111 .has_nhm_msrs = 1, 1112 .bclk_freq = BCLK_133MHZ, 1113 .supported_cstates = CC1 | CC3 | CC6 | PC3 | PC6, 1114 .cst_limit = CST_LIMIT_AMT, 1115 .trl_msrs = TRL_BASE, 1116 }; 1117 1118 static const struct platform_features gmt_features = { 1119 .has_msr_misc_pwr_mgmt = 1, 1120 .has_nhm_msrs = 1, 1121 .bclk_freq = BCLK_100MHZ, 1122 .crystal_freq = 19200000, 1123 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 1124 .cst_limit = CST_LIMIT_GMT, 1125 .has_irtl_msrs = 1, 1126 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1127 .plat_rapl_msrs = RAPL_PKG | RAPL_PKG_POWER_INFO, 1128 }; 1129 1130 static const struct platform_features gmtd_features = { 1131 .has_msr_misc_pwr_mgmt = 1, 1132 .has_nhm_msrs = 1, 1133 .bclk_freq = BCLK_100MHZ, 1134 .crystal_freq = 25000000, 1135 .supported_cstates = CC1 | CC6 | PC2 | PC6, 1136 .cst_limit = CST_LIMIT_GMT, 1137 .has_irtl_msrs = 1, 1138 .has_msr_core_c1_res = 1, 1139 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1140 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL | RAPL_CORE_ENERGY_STATUS, 1141 }; 1142 1143 static const struct platform_features gmtp_features = { 1144 .has_msr_misc_pwr_mgmt = 1, 1145 .has_nhm_msrs = 1, 1146 .bclk_freq = BCLK_100MHZ, 1147 .crystal_freq = 19200000, 1148 .supported_cstates = CC1 | CC3 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 1149 .cst_limit = CST_LIMIT_GMT, 1150 .has_irtl_msrs = 1, 1151 .trl_msrs = TRL_BASE, 1152 .plat_rapl_msrs = RAPL_PKG | RAPL_PKG_POWER_INFO, 1153 }; 1154 1155 static const struct platform_features tmt_features = { 1156 .has_msr_misc_pwr_mgmt = 1, 1157 .has_nhm_msrs = 1, 1158 .bclk_freq = BCLK_100MHZ, 1159 .supported_cstates = CC1 | CC6 | CC7 | PC2 | PC3 | PC6 | PC7 | PC8 | PC9 | PC10, 1160 .cst_limit = CST_LIMIT_GMT, 1161 .has_irtl_msrs = 1, 1162 .trl_msrs = TRL_BASE, 1163 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_CORE_ALL | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_GFX, 1164 .enable_tsc_tweak = 1, 1165 }; 1166 1167 static const struct platform_features tmtd_features = { 1168 .has_msr_misc_pwr_mgmt = 1, 1169 .has_nhm_msrs = 1, 1170 .bclk_freq = BCLK_100MHZ, 1171 .supported_cstates = CC1 | CC6, 1172 .cst_limit = CST_LIMIT_GMT, 1173 .has_irtl_msrs = 1, 1174 .trl_msrs = TRL_BASE | TRL_CORECOUNT, 1175 .plat_rapl_msrs = RAPL_PKG_ALL, 1176 }; 1177 1178 static const struct platform_features knl_features = { 1179 .has_msr_misc_pwr_mgmt = 1, 1180 .has_nhm_msrs = 1, 1181 .has_config_tdp = 1, 1182 .bclk_freq = BCLK_100MHZ, 1183 .supported_cstates = CC1 | CC6 | PC3 | PC6, 1184 .cst_limit = CST_LIMIT_KNL, 1185 .has_msr_knl_core_c6_residency = 1, 1186 .trl_msrs = TRL_KNL, 1187 .plat_rapl_msrs = RAPL_PKG_ALL | RAPL_DRAM_ALL, 1188 .has_fixed_rapl_unit = 1, 1189 .need_perf_multiplier = 1, 1190 }; 1191 1192 static const struct platform_features default_features = { 1193 }; 1194 1195 static const struct platform_features amd_features_with_rapl = { 1196 .plat_rapl_msrs = RAPL_AMD_F17H, 1197 .has_per_core_rapl = 1, 1198 .rapl_quirk_tdp = 280, /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 1199 }; 1200 1201 static const struct platform_data turbostat_pdata[] = { 1202 { INTEL_NEHALEM, &nhm_features }, 1203 { INTEL_NEHALEM_G, &nhm_features }, 1204 { INTEL_NEHALEM_EP, &nhm_features }, 1205 { INTEL_NEHALEM_EX, &nhx_features }, 1206 { INTEL_WESTMERE, &nhm_features }, 1207 { INTEL_WESTMERE_EP, &nhm_features }, 1208 { INTEL_WESTMERE_EX, &nhx_features }, 1209 { INTEL_SANDYBRIDGE, &snb_features }, 1210 { INTEL_SANDYBRIDGE_X, &snx_features }, 1211 { INTEL_IVYBRIDGE, &ivb_features }, 1212 { INTEL_IVYBRIDGE_X, &ivx_features }, 1213 { INTEL_HASWELL, &hsw_features }, 1214 { INTEL_HASWELL_X, &hsx_features }, 1215 { INTEL_HASWELL_L, &hswl_features }, 1216 { INTEL_HASWELL_G, &hswg_features }, 1217 { INTEL_BROADWELL, &bdw_features }, 1218 { INTEL_BROADWELL_G, &bdwg_features }, 1219 { INTEL_BROADWELL_X, &bdx_features }, 1220 { INTEL_BROADWELL_D, &bdx_features }, 1221 { INTEL_SKYLAKE_L, &skl_features }, 1222 { INTEL_SKYLAKE, &skl_features }, 1223 { INTEL_SKYLAKE_X, &skx_features }, 1224 { INTEL_KABYLAKE_L, &skl_features }, 1225 { INTEL_KABYLAKE, &skl_features }, 1226 { INTEL_COMETLAKE, &skl_features }, 1227 { INTEL_COMETLAKE_L, &skl_features }, 1228 { INTEL_CANNONLAKE_L, &cnl_features }, 1229 { INTEL_ICELAKE_X, &icx_features }, 1230 { INTEL_ICELAKE_D, &icx_features }, 1231 { INTEL_ICELAKE_L, &cnl_features }, 1232 { INTEL_ICELAKE_NNPI, &cnl_features }, 1233 { INTEL_ROCKETLAKE, &cnl_features }, 1234 { INTEL_TIGERLAKE_L, &cnl_features }, 1235 { INTEL_TIGERLAKE, &cnl_features }, 1236 { INTEL_SAPPHIRERAPIDS_X, &spr_features }, 1237 { INTEL_EMERALDRAPIDS_X, &spr_features }, 1238 { INTEL_GRANITERAPIDS_X, &spr_features }, 1239 { INTEL_GRANITERAPIDS_D, &spr_features }, 1240 { INTEL_DIAMONDRAPIDS_X, &dmr_features }, 1241 { INTEL_LAKEFIELD, &cnl_features }, 1242 { INTEL_ALDERLAKE, &adl_features }, 1243 { INTEL_ALDERLAKE_L, &adl_features }, 1244 { INTEL_RAPTORLAKE, &adl_features }, 1245 { INTEL_RAPTORLAKE_P, &adl_features }, 1246 { INTEL_RAPTORLAKE_S, &adl_features }, 1247 { INTEL_BARTLETTLAKE, &adl_features }, 1248 { INTEL_METEORLAKE, &adl_features }, 1249 { INTEL_METEORLAKE_L, &adl_features }, 1250 { INTEL_ARROWLAKE_H, &adl_features }, 1251 { INTEL_ARROWLAKE_U, &adl_features }, 1252 { INTEL_ARROWLAKE, &adl_features }, 1253 { INTEL_LUNARLAKE_M, &lnl_features }, 1254 { INTEL_PANTHERLAKE_L, &lnl_features }, 1255 { INTEL_PANTHERLAKE_R, &lnl_features }, 1256 { INTEL_NOVALAKE, &lnl_features }, 1257 { INTEL_NOVALAKE_L, &lnl_features }, 1258 { INTEL_WILDCATLAKE_L, &lnl_features }, 1259 { INTEL_ATOM_SILVERMONT, &slv_features }, 1260 { INTEL_ATOM_SILVERMONT_D, &slvd_features }, 1261 { INTEL_ATOM_AIRMONT, &amt_features }, 1262 { INTEL_ATOM_GOLDMONT, &gmt_features }, 1263 { INTEL_ATOM_GOLDMONT_D, &gmtd_features }, 1264 { INTEL_ATOM_GOLDMONT_PLUS, &gmtp_features }, 1265 { INTEL_ATOM_TREMONT_D, &tmtd_features }, 1266 { INTEL_ATOM_TREMONT, &tmt_features }, 1267 { INTEL_ATOM_TREMONT_L, &tmt_features }, 1268 { INTEL_ATOM_GRACEMONT, &adl_features }, 1269 { INTEL_ATOM_CRESTMONT_X, &srf_features }, 1270 { INTEL_ATOM_CRESTMONT, &grr_features }, 1271 { INTEL_ATOM_DARKMONT_X, &srf_features }, 1272 { INTEL_XEON_PHI_KNL, &knl_features }, 1273 { INTEL_XEON_PHI_KNM, &knl_features }, 1274 /* 1275 * Missing support for 1276 * INTEL_ICELAKE 1277 * INTEL_ATOM_SILVERMONT_MID 1278 * INTEL_ATOM_SILVERMONT_MID2 1279 * INTEL_ATOM_AIRMONT_NP 1280 */ 1281 { 0, NULL }, 1282 }; 1283 1284 struct { 1285 unsigned int uniform; 1286 unsigned int pcore; 1287 unsigned int ecore; 1288 unsigned int lcore; 1289 } perf_pmu_types; 1290 1291 /* 1292 * Events are enumerated in https://github.com/intel/perfmon 1293 * and tools/perf/pmu-events/arch/x86/.../cache.json 1294 */ 1295 struct perf_l2_events { 1296 unsigned long long refs; /* L2_REQUEST.ALL */ 1297 unsigned long long hits; /* L2_REQUEST.HIT */ 1298 }; 1299 1300 struct perf_model_support { 1301 unsigned int vfm; 1302 struct perf_l2_events first; 1303 struct perf_l2_events second; 1304 struct perf_l2_events third; 1305 } *perf_model_support; 1306 1307 /* Perf Cache Events */ 1308 #define PCE(ext_umask, umask) (((unsigned long long) ext_umask) << 40 | umask << 8 | 0x24) 1309 1310 /* 1311 * Enumerate up to three perf CPU PMU's in a system. 1312 * The first, second, and third columns are populated without skipping, describing 1313 * pcore, ecore, lcore PMUs, in order, if present. (The associated PMU "type" field is 1314 * read from sysfs in all cases.) Eg. 1315 * 1316 * non-hybrid: 1317 * GNR: pcore, {}, {} 1318 * ADL-N: ecore, {}, {} 1319 * hybrid: 1320 * MTL: pcore, ecore, {}% 1321 * ARL-H: pcore, ecore, lcore 1322 * LNL: ecore, ecore%%, {} 1323 * 1324 * % MTL physical lcore share architecture and PMU with ecore, and are thus not enumerated separately. 1325 * %% LNL physical lcore is enumerated by perf as ecore 1326 */ 1327 static struct perf_model_support turbostat_perf_model_support[] = { 1328 { INTEL_SAPPHIRERAPIDS_X, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, {}, {} }, 1329 { INTEL_EMERALDRAPIDS_X, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, {}, {} }, 1330 { INTEL_GRANITERAPIDS_X, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, {}, {} }, 1331 { INTEL_GRANITERAPIDS_D, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, {}, {} }, 1332 { INTEL_DIAMONDRAPIDS_X, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, {}, {} }, 1333 1334 { INTEL_ATOM_GRACEMONT, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {}, {} }, /* ADL-N */ 1335 { INTEL_ATOM_CRESTMONT_X, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {}, {} }, /* SRF */ 1336 { INTEL_ATOM_CRESTMONT, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {}, {} }, /* GRR */ 1337 { INTEL_ATOM_DARKMONT_X, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {}, {} }, /* CWF */ 1338 1339 { INTEL_ALDERLAKE, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1340 { INTEL_ALDERLAKE_L, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1341 { INTEL_RAPTORLAKE, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1342 { INTEL_RAPTORLAKE_P, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1343 { INTEL_RAPTORLAKE_S, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1344 { INTEL_METEORLAKE_L, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1345 { INTEL_METEORLAKE, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1346 { INTEL_ARROWLAKE_U, { PCE(0x00, 0xFF), PCE(0x00, 0xDF)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)}, {} }, 1347 1348 { INTEL_LUNARLAKE_M, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x00, 0x07), PCE(0x00, 0x02)}, {} }, 1349 { INTEL_ARROWLAKE_H, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x00, 0x07), PCE(0x00, 0x02)}, { PCE(0x00, 0x00), PCE(0x00, 0x02)} }, 1350 { INTEL_ARROWLAKE, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x00, 0x07), PCE(0x00, 0x02)}, {} }, 1351 1352 { INTEL_PANTHERLAKE_L, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {} }, 1353 { INTEL_PANTHERLAKE_R, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {} }, 1354 { INTEL_WILDCATLAKE_L, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {} }, 1355 1356 { INTEL_NOVALAKE, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {} }, 1357 { INTEL_NOVALAKE_L, { PCE(0x00, 0xFF), PCE(0x00, 0x5F)}, { PCE(0x01, 0xFF), PCE(0x01, 0xBF)}, {} }, 1358 1359 { 0, {}, {}, {} } 1360 }; 1361 1362 static const struct platform_features *platform; 1363 1364 void probe_platform_features(unsigned int family, unsigned int model) 1365 { 1366 int i; 1367 1368 if (authentic_amd || hygon_genuine) { 1369 /* fallback to default features on unsupported models */ 1370 force_load = true; 1371 if (max_extended_level >= 0x80000007) { 1372 unsigned int eax, ebx, ecx, edx; 1373 1374 __cpuid(0x80000007, eax, ebx, ecx, edx); 1375 /* RAPL (Fam 17h+) */ 1376 if ((edx & (1 << 14)) && family >= 0x17) 1377 platform = &amd_features_with_rapl; 1378 } 1379 goto end; 1380 } 1381 1382 if (!genuine_intel) 1383 goto end; 1384 1385 for (i = 0; turbostat_pdata[i].features; i++) { 1386 if (VFM_FAMILY(turbostat_pdata[i].vfm) == family && VFM_MODEL(turbostat_pdata[i].vfm) == model) { 1387 platform = turbostat_pdata[i].features; 1388 return; 1389 } 1390 } 1391 1392 end: 1393 if ((force_load == true) && !platform) { 1394 fprintf(outf, "Forced to run on unsupported platform!\n"); 1395 platform = &default_features; 1396 } 1397 1398 if (platform) 1399 return; 1400 1401 fprintf(stderr, "Unsupported platform detected.\n\tSee RUN THE LATEST VERSION on turbostat(8)\n"); 1402 exit(1); 1403 } 1404 1405 void init_perf_model_support(unsigned int family, unsigned int model) 1406 { 1407 int i; 1408 1409 if (!genuine_intel) 1410 return; 1411 1412 for (i = 0; turbostat_perf_model_support[i].vfm; i++) { 1413 if (VFM_FAMILY(turbostat_perf_model_support[i].vfm) == family && VFM_MODEL(turbostat_perf_model_support[i].vfm) == model) { 1414 perf_model_support = &turbostat_perf_model_support[i]; 1415 return; 1416 } 1417 } 1418 } 1419 1420 /* Model specific support End */ 1421 1422 #define TJMAX_DEFAULT 100 1423 1424 /* MSRs that are not yet in the kernel-provided header. */ 1425 #define MSR_RAPL_PWR_UNIT 0xc0010299 1426 #define MSR_CORE_ENERGY_STAT 0xc001029a 1427 #define MSR_PKG_ENERGY_STAT 0xc001029b 1428 1429 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 1430 1431 int backwards_count; 1432 char *progname; 1433 1434 cpu_set_t *cpu_present_set, *cpu_possible_set, *cpu_effective_set, *cpu_allowed_set, *cpu_affinity_set, *cpuset_cmdline; 1435 cpu_set_t *perf_pcore_set, *perf_ecore_set, *perf_lcore_set; 1436 size_t cpu_setsize; 1437 #define MAX_ADDED_THREAD_COUNTERS 24 1438 #define MAX_ADDED_CORE_COUNTERS 8 1439 #define MAX_ADDED_PACKAGE_COUNTERS 16 1440 #define PMT_MAX_ADDED_THREAD_COUNTERS 24 1441 #define PMT_MAX_ADDED_CORE_COUNTERS 8 1442 #define PMT_MAX_ADDED_PACKAGE_COUNTERS 16 1443 #define BITMASK_SIZE 32 1444 1445 #define ZERO_ARRAY(arr) (memset(arr, 0, sizeof(arr)) + __must_be_array(arr)) 1446 1447 /* Indexes used to map data read from perf and MSRs into global variables */ 1448 enum rapl_rci_index { 1449 RAPL_RCI_INDEX_ENERGY_PKG = 0, 1450 RAPL_RCI_INDEX_ENERGY_CORES = 1, 1451 RAPL_RCI_INDEX_DRAM = 2, 1452 RAPL_RCI_INDEX_GFX = 3, 1453 RAPL_RCI_INDEX_PKG_PERF_STATUS = 4, 1454 RAPL_RCI_INDEX_DRAM_PERF_STATUS = 5, 1455 RAPL_RCI_INDEX_CORE_ENERGY = 6, 1456 RAPL_RCI_INDEX_ENERGY_PLATFORM = 7, 1457 NUM_RAPL_COUNTERS, 1458 }; 1459 1460 enum rapl_unit { 1461 RAPL_UNIT_INVALID, 1462 RAPL_UNIT_JOULES, 1463 RAPL_UNIT_WATTS, 1464 }; 1465 1466 struct rapl_counter_info_t { 1467 unsigned long long data[NUM_RAPL_COUNTERS]; 1468 enum counter_source source[NUM_RAPL_COUNTERS]; 1469 unsigned long long flags[NUM_RAPL_COUNTERS]; 1470 double scale[NUM_RAPL_COUNTERS]; 1471 enum rapl_unit unit[NUM_RAPL_COUNTERS]; 1472 unsigned long long msr[NUM_RAPL_COUNTERS]; 1473 unsigned long long msr_mask[NUM_RAPL_COUNTERS]; 1474 int msr_shift[NUM_RAPL_COUNTERS]; 1475 1476 int fd_perf; 1477 }; 1478 1479 /* struct rapl_counter_info_t for each RAPL domain */ 1480 struct rapl_counter_info_t *rapl_counter_info_perdomain; 1481 unsigned int rapl_counter_info_perdomain_size; 1482 1483 #define RAPL_COUNTER_FLAG_PLATFORM_COUNTER (1u << 0) 1484 #define RAPL_COUNTER_FLAG_USE_MSR_SUM (1u << 1) 1485 1486 struct rapl_counter_arch_info { 1487 int feature_mask; /* Mask for testing if the counter is supported on host */ 1488 const char *perf_subsys; 1489 const char *perf_name; 1490 unsigned long long msr; 1491 unsigned long long msr_mask; 1492 int msr_shift; /* Positive mean shift right, negative mean shift left */ 1493 double *platform_rapl_msr_scale; /* Scale applied to values read by MSR (platform dependent, filled at runtime) */ 1494 unsigned int rci_index; /* Maps data from perf counters to global variables */ 1495 unsigned int bic_number; 1496 double compat_scale; /* Some counters require constant scaling to be in the same range as other, similar ones */ 1497 unsigned long long flags; 1498 }; 1499 1500 static const struct rapl_counter_arch_info rapl_counter_arch_infos[] = { 1501 { 1502 .feature_mask = RAPL_PKG, 1503 .perf_subsys = "power", 1504 .perf_name = "energy-pkg", 1505 .msr = MSR_PKG_ENERGY_STATUS, 1506 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1507 .msr_shift = 0, 1508 .platform_rapl_msr_scale = &rapl_energy_units, 1509 .rci_index = RAPL_RCI_INDEX_ENERGY_PKG, 1510 .bic_number = BIC_PkgWatt, 1511 .compat_scale = 1.0, 1512 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1513 }, 1514 { 1515 .feature_mask = RAPL_PKG, 1516 .perf_subsys = "power", 1517 .perf_name = "energy-pkg", 1518 .msr = MSR_PKG_ENERGY_STATUS, 1519 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1520 .msr_shift = 0, 1521 .platform_rapl_msr_scale = &rapl_energy_units, 1522 .rci_index = RAPL_RCI_INDEX_ENERGY_PKG, 1523 .bic_number = BIC_Pkg_J, 1524 .compat_scale = 1.0, 1525 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1526 }, 1527 { 1528 .feature_mask = RAPL_AMD_F17H, 1529 .perf_subsys = "power", 1530 .perf_name = "energy-pkg", 1531 .msr = MSR_PKG_ENERGY_STAT, 1532 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1533 .msr_shift = 0, 1534 .platform_rapl_msr_scale = &rapl_energy_units, 1535 .rci_index = RAPL_RCI_INDEX_ENERGY_PKG, 1536 .bic_number = BIC_PkgWatt, 1537 .compat_scale = 1.0, 1538 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1539 }, 1540 { 1541 .feature_mask = RAPL_AMD_F17H, 1542 .perf_subsys = "power", 1543 .perf_name = "energy-pkg", 1544 .msr = MSR_PKG_ENERGY_STAT, 1545 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1546 .msr_shift = 0, 1547 .platform_rapl_msr_scale = &rapl_energy_units, 1548 .rci_index = RAPL_RCI_INDEX_ENERGY_PKG, 1549 .bic_number = BIC_Pkg_J, 1550 .compat_scale = 1.0, 1551 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1552 }, 1553 { 1554 .feature_mask = RAPL_CORE_ENERGY_STATUS, 1555 .perf_subsys = "power", 1556 .perf_name = "energy-cores", 1557 .msr = MSR_PP0_ENERGY_STATUS, 1558 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1559 .msr_shift = 0, 1560 .platform_rapl_msr_scale = &rapl_energy_units, 1561 .rci_index = RAPL_RCI_INDEX_ENERGY_CORES, 1562 .bic_number = BIC_CorWatt, 1563 .compat_scale = 1.0, 1564 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1565 }, 1566 { 1567 .feature_mask = RAPL_CORE_ENERGY_STATUS, 1568 .perf_subsys = "power", 1569 .perf_name = "energy-cores", 1570 .msr = MSR_PP0_ENERGY_STATUS, 1571 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1572 .msr_shift = 0, 1573 .platform_rapl_msr_scale = &rapl_energy_units, 1574 .rci_index = RAPL_RCI_INDEX_ENERGY_CORES, 1575 .bic_number = BIC_Cor_J, 1576 .compat_scale = 1.0, 1577 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1578 }, 1579 { 1580 .feature_mask = RAPL_DRAM, 1581 .perf_subsys = "power", 1582 .perf_name = "energy-ram", 1583 .msr = MSR_DRAM_ENERGY_STATUS, 1584 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1585 .msr_shift = 0, 1586 .platform_rapl_msr_scale = &rapl_dram_energy_units, 1587 .rci_index = RAPL_RCI_INDEX_DRAM, 1588 .bic_number = BIC_RAMWatt, 1589 .compat_scale = 1.0, 1590 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1591 }, 1592 { 1593 .feature_mask = RAPL_DRAM, 1594 .perf_subsys = "power", 1595 .perf_name = "energy-ram", 1596 .msr = MSR_DRAM_ENERGY_STATUS, 1597 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1598 .msr_shift = 0, 1599 .platform_rapl_msr_scale = &rapl_dram_energy_units, 1600 .rci_index = RAPL_RCI_INDEX_DRAM, 1601 .bic_number = BIC_RAM_J, 1602 .compat_scale = 1.0, 1603 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1604 }, 1605 { 1606 .feature_mask = RAPL_GFX, 1607 .perf_subsys = "power", 1608 .perf_name = "energy-gpu", 1609 .msr = MSR_PP1_ENERGY_STATUS, 1610 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1611 .msr_shift = 0, 1612 .platform_rapl_msr_scale = &rapl_energy_units, 1613 .rci_index = RAPL_RCI_INDEX_GFX, 1614 .bic_number = BIC_GFXWatt, 1615 .compat_scale = 1.0, 1616 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1617 }, 1618 { 1619 .feature_mask = RAPL_GFX, 1620 .perf_subsys = "power", 1621 .perf_name = "energy-gpu", 1622 .msr = MSR_PP1_ENERGY_STATUS, 1623 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1624 .msr_shift = 0, 1625 .platform_rapl_msr_scale = &rapl_energy_units, 1626 .rci_index = RAPL_RCI_INDEX_GFX, 1627 .bic_number = BIC_GFX_J, 1628 .compat_scale = 1.0, 1629 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1630 }, 1631 { 1632 .feature_mask = RAPL_PKG_PERF_STATUS, 1633 .perf_subsys = NULL, 1634 .perf_name = NULL, 1635 .msr = MSR_PKG_PERF_STATUS, 1636 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1637 .msr_shift = 0, 1638 .platform_rapl_msr_scale = &rapl_time_units, 1639 .rci_index = RAPL_RCI_INDEX_PKG_PERF_STATUS, 1640 .bic_number = BIC_PKG__, 1641 .compat_scale = 100.0, 1642 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1643 }, 1644 { 1645 .feature_mask = RAPL_DRAM_PERF_STATUS, 1646 .perf_subsys = NULL, 1647 .perf_name = NULL, 1648 .msr = MSR_DRAM_PERF_STATUS, 1649 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1650 .msr_shift = 0, 1651 .platform_rapl_msr_scale = &rapl_time_units, 1652 .rci_index = RAPL_RCI_INDEX_DRAM_PERF_STATUS, 1653 .bic_number = BIC_RAM__, 1654 .compat_scale = 100.0, 1655 .flags = RAPL_COUNTER_FLAG_USE_MSR_SUM, 1656 }, 1657 { 1658 .feature_mask = RAPL_AMD_F17H, 1659 .perf_subsys = NULL, 1660 .perf_name = NULL, 1661 .msr = MSR_CORE_ENERGY_STAT, 1662 .msr_mask = 0xFFFFFFFF, 1663 .msr_shift = 0, 1664 .platform_rapl_msr_scale = &rapl_energy_units, 1665 .rci_index = RAPL_RCI_INDEX_CORE_ENERGY, 1666 .bic_number = BIC_CorWatt, 1667 .compat_scale = 1.0, 1668 .flags = 0, 1669 }, 1670 { 1671 .feature_mask = RAPL_AMD_F17H, 1672 .perf_subsys = NULL, 1673 .perf_name = NULL, 1674 .msr = MSR_CORE_ENERGY_STAT, 1675 .msr_mask = 0xFFFFFFFF, 1676 .msr_shift = 0, 1677 .platform_rapl_msr_scale = &rapl_energy_units, 1678 .rci_index = RAPL_RCI_INDEX_CORE_ENERGY, 1679 .bic_number = BIC_Cor_J, 1680 .compat_scale = 1.0, 1681 .flags = 0, 1682 }, 1683 { 1684 .feature_mask = RAPL_PSYS, 1685 .perf_subsys = "power", 1686 .perf_name = "energy-psys", 1687 .msr = MSR_PLATFORM_ENERGY_STATUS, 1688 .msr_mask = 0x00000000FFFFFFFF, 1689 .msr_shift = 0, 1690 .platform_rapl_msr_scale = &rapl_psys_energy_units, 1691 .rci_index = RAPL_RCI_INDEX_ENERGY_PLATFORM, 1692 .bic_number = BIC_SysWatt, 1693 .compat_scale = 1.0, 1694 .flags = RAPL_COUNTER_FLAG_PLATFORM_COUNTER | RAPL_COUNTER_FLAG_USE_MSR_SUM, 1695 }, 1696 { 1697 .feature_mask = RAPL_PSYS, 1698 .perf_subsys = "power", 1699 .perf_name = "energy-psys", 1700 .msr = MSR_PLATFORM_ENERGY_STATUS, 1701 .msr_mask = 0x00000000FFFFFFFF, 1702 .msr_shift = 0, 1703 .platform_rapl_msr_scale = &rapl_psys_energy_units, 1704 .rci_index = RAPL_RCI_INDEX_ENERGY_PLATFORM, 1705 .bic_number = BIC_Sys_J, 1706 .compat_scale = 1.0, 1707 .flags = RAPL_COUNTER_FLAG_PLATFORM_COUNTER | RAPL_COUNTER_FLAG_USE_MSR_SUM, 1708 }, 1709 }; 1710 1711 struct rapl_counter { 1712 unsigned long long raw_value; 1713 enum rapl_unit unit; 1714 double scale; 1715 }; 1716 1717 /* Indexes used to map data read from perf and MSRs into global variables */ 1718 enum ccstate_rci_index { 1719 CCSTATE_RCI_INDEX_C1_RESIDENCY = 0, 1720 CCSTATE_RCI_INDEX_C3_RESIDENCY = 1, 1721 CCSTATE_RCI_INDEX_C6_RESIDENCY = 2, 1722 CCSTATE_RCI_INDEX_C7_RESIDENCY = 3, 1723 PCSTATE_RCI_INDEX_C2_RESIDENCY = 4, 1724 PCSTATE_RCI_INDEX_C3_RESIDENCY = 5, 1725 PCSTATE_RCI_INDEX_C6_RESIDENCY = 6, 1726 PCSTATE_RCI_INDEX_C7_RESIDENCY = 7, 1727 PCSTATE_RCI_INDEX_C8_RESIDENCY = 8, 1728 PCSTATE_RCI_INDEX_C9_RESIDENCY = 9, 1729 PCSTATE_RCI_INDEX_C10_RESIDENCY = 10, 1730 NUM_CSTATE_COUNTERS, 1731 }; 1732 1733 struct cstate_counter_info_t { 1734 unsigned long long data[NUM_CSTATE_COUNTERS]; 1735 enum counter_source source[NUM_CSTATE_COUNTERS]; 1736 unsigned long long msr[NUM_CSTATE_COUNTERS]; 1737 int fd_perf_core; 1738 int fd_perf_pkg; 1739 }; 1740 1741 struct cstate_counter_info_t *ccstate_counter_info; 1742 unsigned int ccstate_counter_info_size; 1743 1744 #define CSTATE_COUNTER_FLAG_COLLECT_PER_CORE (1u << 0) 1745 #define CSTATE_COUNTER_FLAG_COLLECT_PER_THREAD ((1u << 1) | CSTATE_COUNTER_FLAG_COLLECT_PER_CORE) 1746 #define CSTATE_COUNTER_FLAG_SOFT_C1_DEPENDENCY (1u << 2) 1747 1748 struct cstate_counter_arch_info { 1749 int feature_mask; /* Mask for testing if the counter is supported on host */ 1750 const char *perf_subsys; 1751 const char *perf_name; 1752 unsigned long long msr; 1753 unsigned int rci_index; /* Maps data from perf counters to global variables */ 1754 unsigned int bic_number; 1755 unsigned long long flags; 1756 int pkg_cstate_limit; 1757 }; 1758 1759 static struct cstate_counter_arch_info ccstate_counter_arch_infos[] = { 1760 { 1761 .feature_mask = CC1, 1762 .perf_subsys = "cstate_core", 1763 .perf_name = "c1-residency", 1764 .msr = MSR_CORE_C1_RES, 1765 .rci_index = CCSTATE_RCI_INDEX_C1_RESIDENCY, 1766 .bic_number = BIC_CPU_c1, 1767 .flags = CSTATE_COUNTER_FLAG_COLLECT_PER_THREAD, 1768 .pkg_cstate_limit = 0, 1769 }, 1770 { 1771 .feature_mask = CC3, 1772 .perf_subsys = "cstate_core", 1773 .perf_name = "c3-residency", 1774 .msr = MSR_CORE_C3_RESIDENCY, 1775 .rci_index = CCSTATE_RCI_INDEX_C3_RESIDENCY, 1776 .bic_number = BIC_CPU_c3, 1777 .flags = CSTATE_COUNTER_FLAG_COLLECT_PER_CORE | CSTATE_COUNTER_FLAG_SOFT_C1_DEPENDENCY, 1778 .pkg_cstate_limit = 0, 1779 }, 1780 { 1781 .feature_mask = CC6, 1782 .perf_subsys = "cstate_core", 1783 .perf_name = "c6-residency", 1784 .msr = MSR_CORE_C6_RESIDENCY, 1785 .rci_index = CCSTATE_RCI_INDEX_C6_RESIDENCY, 1786 .bic_number = BIC_CPU_c6, 1787 .flags = CSTATE_COUNTER_FLAG_COLLECT_PER_CORE | CSTATE_COUNTER_FLAG_SOFT_C1_DEPENDENCY, 1788 .pkg_cstate_limit = 0, 1789 }, 1790 { 1791 .feature_mask = CC7, 1792 .perf_subsys = "cstate_core", 1793 .perf_name = "c7-residency", 1794 .msr = MSR_CORE_C7_RESIDENCY, 1795 .rci_index = CCSTATE_RCI_INDEX_C7_RESIDENCY, 1796 .bic_number = BIC_CPU_c7, 1797 .flags = CSTATE_COUNTER_FLAG_COLLECT_PER_CORE | CSTATE_COUNTER_FLAG_SOFT_C1_DEPENDENCY, 1798 .pkg_cstate_limit = 0, 1799 }, 1800 { 1801 .feature_mask = PC2, 1802 .perf_subsys = "cstate_pkg", 1803 .perf_name = "c2-residency", 1804 .msr = MSR_PKG_C2_RESIDENCY, 1805 .rci_index = PCSTATE_RCI_INDEX_C2_RESIDENCY, 1806 .bic_number = BIC_Pkgpc2, 1807 .flags = 0, 1808 .pkg_cstate_limit = PCL__2, 1809 }, 1810 { 1811 .feature_mask = PC3, 1812 .perf_subsys = "cstate_pkg", 1813 .perf_name = "c3-residency", 1814 .msr = MSR_PKG_C3_RESIDENCY, 1815 .rci_index = PCSTATE_RCI_INDEX_C3_RESIDENCY, 1816 .bic_number = BIC_Pkgpc3, 1817 .flags = 0, 1818 .pkg_cstate_limit = PCL__3, 1819 }, 1820 { 1821 .feature_mask = PC6, 1822 .perf_subsys = "cstate_pkg", 1823 .perf_name = "c6-residency", 1824 .msr = MSR_PKG_C6_RESIDENCY, 1825 .rci_index = PCSTATE_RCI_INDEX_C6_RESIDENCY, 1826 .bic_number = BIC_Pkgpc6, 1827 .flags = 0, 1828 .pkg_cstate_limit = PCL__6, 1829 }, 1830 { 1831 .feature_mask = PC7, 1832 .perf_subsys = "cstate_pkg", 1833 .perf_name = "c7-residency", 1834 .msr = MSR_PKG_C7_RESIDENCY, 1835 .rci_index = PCSTATE_RCI_INDEX_C7_RESIDENCY, 1836 .bic_number = BIC_Pkgpc7, 1837 .flags = 0, 1838 .pkg_cstate_limit = PCL__7, 1839 }, 1840 { 1841 .feature_mask = PC8, 1842 .perf_subsys = "cstate_pkg", 1843 .perf_name = "c8-residency", 1844 .msr = MSR_PKG_C8_RESIDENCY, 1845 .rci_index = PCSTATE_RCI_INDEX_C8_RESIDENCY, 1846 .bic_number = BIC_Pkgpc8, 1847 .flags = 0, 1848 .pkg_cstate_limit = PCL__8, 1849 }, 1850 { 1851 .feature_mask = PC9, 1852 .perf_subsys = "cstate_pkg", 1853 .perf_name = "c9-residency", 1854 .msr = MSR_PKG_C9_RESIDENCY, 1855 .rci_index = PCSTATE_RCI_INDEX_C9_RESIDENCY, 1856 .bic_number = BIC_Pkgpc9, 1857 .flags = 0, 1858 .pkg_cstate_limit = PCL__9, 1859 }, 1860 { 1861 .feature_mask = PC10, 1862 .perf_subsys = "cstate_pkg", 1863 .perf_name = "c10-residency", 1864 .msr = MSR_PKG_C10_RESIDENCY, 1865 .rci_index = PCSTATE_RCI_INDEX_C10_RESIDENCY, 1866 .bic_number = BIC_Pkgpc10, 1867 .flags = 0, 1868 .pkg_cstate_limit = PCL_10, 1869 }, 1870 }; 1871 1872 /* Indexes used to map data read from perf and MSRs into global variables */ 1873 enum msr_rci_index { 1874 MSR_RCI_INDEX_APERF = 0, 1875 MSR_RCI_INDEX_MPERF = 1, 1876 MSR_RCI_INDEX_SMI = 2, 1877 NUM_MSR_COUNTERS, 1878 }; 1879 1880 struct msr_counter_info_t { 1881 unsigned long long data[NUM_MSR_COUNTERS]; 1882 enum counter_source source[NUM_MSR_COUNTERS]; 1883 unsigned long long msr[NUM_MSR_COUNTERS]; 1884 unsigned long long msr_mask[NUM_MSR_COUNTERS]; 1885 int fd_perf; 1886 }; 1887 1888 struct msr_counter_info_t *msr_counter_info; 1889 unsigned int msr_counter_info_size; 1890 1891 struct msr_counter_arch_info { 1892 const char *perf_subsys; 1893 const char *perf_name; 1894 unsigned long long msr; 1895 unsigned long long msr_mask; 1896 unsigned int rci_index; /* Maps data from perf counters to global variables */ 1897 bool needed; 1898 bool present; 1899 }; 1900 1901 enum msr_arch_info_index { 1902 MSR_ARCH_INFO_APERF_INDEX = 0, 1903 MSR_ARCH_INFO_MPERF_INDEX = 1, 1904 MSR_ARCH_INFO_SMI_INDEX = 2, 1905 }; 1906 1907 static struct msr_counter_arch_info msr_counter_arch_infos[] = { 1908 [MSR_ARCH_INFO_APERF_INDEX] = { 1909 .perf_subsys = "msr", 1910 .perf_name = "aperf", 1911 .msr = MSR_IA32_APERF, 1912 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1913 .rci_index = MSR_RCI_INDEX_APERF, 1914 }, 1915 1916 [MSR_ARCH_INFO_MPERF_INDEX] = { 1917 .perf_subsys = "msr", 1918 .perf_name = "mperf", 1919 .msr = MSR_IA32_MPERF, 1920 .msr_mask = 0xFFFFFFFFFFFFFFFF, 1921 .rci_index = MSR_RCI_INDEX_MPERF, 1922 }, 1923 1924 [MSR_ARCH_INFO_SMI_INDEX] = { 1925 .perf_subsys = "msr", 1926 .perf_name = "smi", 1927 .msr = MSR_SMI_COUNT, 1928 .msr_mask = 0xFFFFFFFF, 1929 .rci_index = MSR_RCI_INDEX_SMI, 1930 }, 1931 }; 1932 1933 /* Can be redefined when compiling, useful for testing. */ 1934 #ifndef SYSFS_TELEM_PATH 1935 #define SYSFS_TELEM_PATH "/sys/class/intel_pmt" 1936 #endif 1937 1938 #define PMT_COUNTER_MTL_DC6_OFFSET 120 1939 #define PMT_COUNTER_MTL_DC6_LSB 0 1940 #define PMT_COUNTER_MTL_DC6_MSB 63 1941 #define PMT_MTL_DC6_GUID 0x1a067102 1942 #define PMT_MTL_DC6_SEQ 0 1943 1944 #define PMT_COUNTER_CWF_MC1E_OFFSET_BASE 20936 1945 #define PMT_COUNTER_CWF_MC1E_OFFSET_INCREMENT 24 1946 #define PMT_COUNTER_CWF_MC1E_NUM_MODULES_PER_FILE 12 1947 #define PMT_COUNTER_CWF_CPUS_PER_MODULE 4 1948 #define PMT_COUNTER_CWF_MC1E_LSB 0 1949 #define PMT_COUNTER_CWF_MC1E_MSB 63 1950 #define PMT_CWF_MC1E_GUID 0x14421519 1951 1952 unsigned long long tcore_clock_freq_hz = 800000000; 1953 1954 #define PMT_COUNTER_NAME_SIZE_BYTES 16 1955 #define PMT_COUNTER_TYPE_NAME_SIZE_BYTES 32 1956 1957 struct pmt_mmio { 1958 struct pmt_mmio *next; 1959 1960 unsigned int guid; 1961 unsigned int size; 1962 1963 /* Base pointer to the mmaped memory. */ 1964 void *mmio_base; 1965 1966 /* 1967 * Offset to be applied to the mmio_base 1968 * to get the beginning of the PMT counters for given GUID. 1969 */ 1970 unsigned long pmt_offset; 1971 } *pmt_mmios; 1972 1973 enum pmt_datatype { 1974 PMT_TYPE_RAW, 1975 PMT_TYPE_XTAL_TIME, 1976 PMT_TYPE_TCORE_CLOCK, 1977 }; 1978 1979 struct pmt_domain_info { 1980 /* 1981 * Pointer to the MMIO obtained by applying a counter offset 1982 * to the mmio_base of the mmaped region for the given GUID. 1983 * 1984 * This is where to read the raw value of the counter from. 1985 */ 1986 unsigned long *pcounter; 1987 }; 1988 1989 struct pmt_counter { 1990 struct pmt_counter *next; 1991 1992 /* PMT metadata */ 1993 char name[PMT_COUNTER_NAME_SIZE_BYTES]; 1994 enum pmt_datatype type; 1995 enum counter_scope scope; 1996 unsigned int lsb; 1997 unsigned int msb; 1998 1999 /* BIC-like metadata */ 2000 enum counter_format format; 2001 2002 unsigned int num_domains; 2003 struct pmt_domain_info *domains; 2004 }; 2005 2006 /* 2007 * PMT telemetry directory iterator. 2008 * Used to iterate telemetry files in sysfs in correct order. 2009 */ 2010 struct pmt_diriter_t { 2011 DIR *dir; 2012 struct dirent **namelist; 2013 unsigned int num_names; 2014 unsigned int current_name_idx; 2015 }; 2016 2017 int pmt_telemdir_filter(const struct dirent *e) 2018 { 2019 unsigned int dummy; 2020 2021 return (sscanf(e->d_name, "telem%u", &dummy) == 1); 2022 } 2023 2024 int pmt_telemdir_sort(const struct dirent **a, const struct dirent **b) 2025 { 2026 unsigned int aidx = 0, bidx = 0; 2027 2028 if (sscanf((*a)->d_name, "telem%u", &aidx) != 1) 2029 aidx = 0; 2030 if (sscanf((*b)->d_name, "telem%u", &bidx) != 1) 2031 bidx = 0; 2032 2033 return (aidx > bidx) ? 1 : (aidx < bidx) ? -1 : 0; 2034 } 2035 2036 const struct dirent *pmt_diriter_next(struct pmt_diriter_t *iter) 2037 { 2038 const struct dirent *ret = NULL; 2039 2040 if (!iter->dir) 2041 return NULL; 2042 2043 if (iter->current_name_idx >= iter->num_names) 2044 return NULL; 2045 2046 ret = iter->namelist[iter->current_name_idx]; 2047 ++iter->current_name_idx; 2048 2049 return ret; 2050 } 2051 2052 const struct dirent *pmt_diriter_begin(struct pmt_diriter_t *iter, const char *pmt_root_path) 2053 { 2054 int num_names = iter->num_names; 2055 2056 if (!iter->dir) { 2057 iter->dir = opendir(pmt_root_path); 2058 if (iter->dir == NULL) 2059 return NULL; 2060 2061 num_names = scandir(pmt_root_path, &iter->namelist, pmt_telemdir_filter, pmt_telemdir_sort); 2062 if (num_names == -1) { 2063 closedir(iter->dir); 2064 iter->dir = NULL; 2065 return NULL; 2066 } 2067 } 2068 2069 iter->current_name_idx = 0; 2070 iter->num_names = num_names; 2071 2072 return pmt_diriter_next(iter); 2073 } 2074 2075 void pmt_diriter_init(struct pmt_diriter_t *iter) 2076 { 2077 memset(iter, 0, sizeof(*iter)); 2078 } 2079 2080 void pmt_diriter_remove(struct pmt_diriter_t *iter) 2081 { 2082 if (iter->namelist) { 2083 for (unsigned int i = 0; i < iter->num_names; i++) { 2084 free(iter->namelist[i]); 2085 iter->namelist[i] = NULL; 2086 } 2087 } 2088 2089 free(iter->namelist); 2090 iter->namelist = NULL; 2091 iter->num_names = 0; 2092 iter->current_name_idx = 0; 2093 2094 if (iter->dir) { 2095 closedir(iter->dir); 2096 iter->dir = NULL; 2097 } 2098 } 2099 2100 unsigned int pmt_counter_get_width(const struct pmt_counter *p) 2101 { 2102 return (p->msb - p->lsb) + 1; 2103 } 2104 2105 void pmt_counter_resize_(struct pmt_counter *pcounter, unsigned int new_size) 2106 { 2107 struct pmt_domain_info *new_mem; 2108 2109 new_mem = (struct pmt_domain_info *)reallocarray(pcounter->domains, new_size, sizeof(*pcounter->domains)); 2110 if (!new_mem) { 2111 fprintf(stderr, "%s: failed to allocate memory for PMT counters\n", __func__); 2112 exit(1); 2113 } 2114 2115 /* Zero initialize just allocated memory. */ 2116 const size_t num_new_domains = new_size - pcounter->num_domains; 2117 2118 memset(&new_mem[pcounter->num_domains], 0, num_new_domains * sizeof(*pcounter->domains)); 2119 2120 pcounter->num_domains = new_size; 2121 pcounter->domains = new_mem; 2122 } 2123 2124 void pmt_counter_resize(struct pmt_counter *pcounter, unsigned int new_size) 2125 { 2126 /* 2127 * Allocate more memory ahead of time. 2128 * 2129 * Always allocate space for at least 8 elements 2130 * and double the size when growing. 2131 */ 2132 if (new_size < 8) 2133 new_size = 8; 2134 new_size = MAX(new_size, pcounter->num_domains * 2); 2135 2136 pmt_counter_resize_(pcounter, new_size); 2137 } 2138 2139 struct llc_stats { 2140 unsigned long long references; 2141 unsigned long long misses; 2142 }; 2143 struct l2_stats { 2144 unsigned long long references; 2145 unsigned long long hits; 2146 }; 2147 struct thread_data { 2148 struct timeval tv_begin; 2149 struct timeval tv_end; 2150 struct timeval tv_delta; 2151 unsigned long long tsc; 2152 unsigned long long aperf; 2153 unsigned long long mperf; 2154 unsigned long long c1; 2155 unsigned long long instr_count; 2156 unsigned long long irq_count; 2157 unsigned long long nmi_count; 2158 unsigned int smi_count; 2159 struct llc_stats llc; 2160 struct l2_stats l2; 2161 unsigned int cpu_id; 2162 unsigned int apic_id; 2163 unsigned int x2apic_id; 2164 unsigned int flags; 2165 bool is_atom; 2166 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 2167 unsigned long long perf_counter[MAX_ADDED_THREAD_COUNTERS]; 2168 unsigned long long pmt_counter[PMT_MAX_ADDED_THREAD_COUNTERS]; 2169 }; 2170 2171 struct core_data { 2172 int first_cpu; 2173 unsigned long long c3; 2174 unsigned long long c6; 2175 unsigned long long c7; 2176 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 2177 unsigned int core_temp_c; 2178 struct rapl_counter core_energy; /* MSR_CORE_ENERGY_STAT */ 2179 unsigned long long core_throt_cnt; 2180 unsigned long long counter[MAX_ADDED_CORE_COUNTERS]; 2181 unsigned long long perf_counter[MAX_ADDED_CORE_COUNTERS]; 2182 unsigned long long pmt_counter[PMT_MAX_ADDED_CORE_COUNTERS]; 2183 }; 2184 2185 struct pkg_data { 2186 int first_cpu; 2187 unsigned long long pc2; 2188 unsigned long long pc3; 2189 unsigned long long pc6; 2190 unsigned long long pc7; 2191 unsigned long long pc8; 2192 unsigned long long pc9; 2193 unsigned long long pc10; 2194 long long cpu_lpi; 2195 long long sys_lpi; 2196 unsigned long long pkg_wtd_core_c0; 2197 unsigned long long pkg_any_core_c0; 2198 unsigned long long pkg_any_gfxe_c0; 2199 unsigned long long pkg_both_core_gfxe_c0; 2200 long long gfx_rc6_ms; 2201 unsigned int gfx_mhz; 2202 unsigned int gfx_act_mhz; 2203 long long sam_mc6_ms; 2204 unsigned int sam_mhz; 2205 unsigned int sam_act_mhz; 2206 struct rapl_counter energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2207 struct rapl_counter energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2208 struct rapl_counter energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2209 struct rapl_counter energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2210 struct rapl_counter rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2211 struct rapl_counter rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 2212 unsigned int pkg_temp_c; 2213 unsigned int uncore_mhz; 2214 unsigned long long die_c6; 2215 unsigned long long counter[MAX_ADDED_PACKAGE_COUNTERS]; 2216 unsigned long long perf_counter[MAX_ADDED_PACKAGE_COUNTERS]; 2217 unsigned long long pmt_counter[PMT_MAX_ADDED_PACKAGE_COUNTERS]; 2218 }; 2219 2220 #define ODD_COUNTERS odd.threads, odd.cores, odd.packages 2221 #define EVEN_COUNTERS even.threads, even.cores, even.packages 2222 2223 /* 2224 * The accumulated sum of MSR is defined as a monotonic 2225 * increasing MSR, it will be accumulated periodically, 2226 * despite its register's bit width. 2227 */ 2228 enum { 2229 IDX_PKG_ENERGY, 2230 IDX_DRAM_ENERGY, 2231 IDX_PP0_ENERGY, 2232 IDX_PP1_ENERGY, 2233 IDX_PKG_PERF, 2234 IDX_DRAM_PERF, 2235 IDX_PSYS_ENERGY, 2236 IDX_COUNT, 2237 }; 2238 2239 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 2240 2241 struct msr_sum_array { 2242 /* get_msr_sum() = sum + (get_msr() - last) */ 2243 struct { 2244 /*The accumulated MSR value is updated by the timer */ 2245 unsigned long long sum; 2246 /*The MSR footprint recorded in last timer */ 2247 unsigned long long last; 2248 } entries[IDX_COUNT]; 2249 }; 2250 2251 /* The percpu MSR sum array.*/ 2252 struct msr_sum_array *per_cpu_msr_sum; 2253 2254 off_t idx_to_offset(int idx) 2255 { 2256 off_t offset; 2257 2258 switch (idx) { 2259 case IDX_PKG_ENERGY: 2260 if (platform->plat_rapl_msrs & RAPL_AMD_F17H) 2261 offset = MSR_PKG_ENERGY_STAT; 2262 else 2263 offset = MSR_PKG_ENERGY_STATUS; 2264 break; 2265 case IDX_DRAM_ENERGY: 2266 offset = MSR_DRAM_ENERGY_STATUS; 2267 break; 2268 case IDX_PP0_ENERGY: 2269 offset = MSR_PP0_ENERGY_STATUS; 2270 break; 2271 case IDX_PP1_ENERGY: 2272 offset = MSR_PP1_ENERGY_STATUS; 2273 break; 2274 case IDX_PKG_PERF: 2275 offset = MSR_PKG_PERF_STATUS; 2276 break; 2277 case IDX_DRAM_PERF: 2278 offset = MSR_DRAM_PERF_STATUS; 2279 break; 2280 case IDX_PSYS_ENERGY: 2281 offset = MSR_PLATFORM_ENERGY_STATUS; 2282 break; 2283 default: 2284 offset = -1; 2285 } 2286 return offset; 2287 } 2288 2289 int offset_to_idx(off_t offset) 2290 { 2291 int idx; 2292 2293 switch (offset) { 2294 case MSR_PKG_ENERGY_STATUS: 2295 case MSR_PKG_ENERGY_STAT: 2296 idx = IDX_PKG_ENERGY; 2297 break; 2298 case MSR_DRAM_ENERGY_STATUS: 2299 idx = IDX_DRAM_ENERGY; 2300 break; 2301 case MSR_PP0_ENERGY_STATUS: 2302 idx = IDX_PP0_ENERGY; 2303 break; 2304 case MSR_PP1_ENERGY_STATUS: 2305 idx = IDX_PP1_ENERGY; 2306 break; 2307 case MSR_PKG_PERF_STATUS: 2308 idx = IDX_PKG_PERF; 2309 break; 2310 case MSR_DRAM_PERF_STATUS: 2311 idx = IDX_DRAM_PERF; 2312 break; 2313 case MSR_PLATFORM_ENERGY_STATUS: 2314 idx = IDX_PSYS_ENERGY; 2315 break; 2316 default: 2317 idx = -1; 2318 } 2319 return idx; 2320 } 2321 2322 int idx_valid(int idx) 2323 { 2324 switch (idx) { 2325 case IDX_PKG_ENERGY: 2326 return valid_rapl_msrs & (RAPL_PKG | RAPL_AMD_F17H); 2327 case IDX_DRAM_ENERGY: 2328 return valid_rapl_msrs & RAPL_DRAM; 2329 case IDX_PP0_ENERGY: 2330 return valid_rapl_msrs & RAPL_CORE_ENERGY_STATUS; 2331 case IDX_PP1_ENERGY: 2332 return valid_rapl_msrs & RAPL_GFX; 2333 case IDX_PKG_PERF: 2334 return valid_rapl_msrs & RAPL_PKG_PERF_STATUS; 2335 case IDX_DRAM_PERF: 2336 return valid_rapl_msrs & RAPL_DRAM_PERF_STATUS; 2337 case IDX_PSYS_ENERGY: 2338 return valid_rapl_msrs & RAPL_PSYS; 2339 default: 2340 return 0; 2341 } 2342 } 2343 2344 struct sys_counters { 2345 /* MSR added counters */ 2346 unsigned int added_thread_counters; 2347 unsigned int added_core_counters; 2348 unsigned int added_package_counters; 2349 struct msr_counter *tp; 2350 struct msr_counter *cp; 2351 struct msr_counter *pp; 2352 2353 /* perf added counters */ 2354 unsigned int added_thread_perf_counters; 2355 unsigned int added_core_perf_counters; 2356 unsigned int added_package_perf_counters; 2357 struct perf_counter_info *perf_tp; 2358 struct perf_counter_info *perf_cp; 2359 struct perf_counter_info *perf_pp; 2360 2361 struct pmt_counter *pmt_tp; 2362 struct pmt_counter *pmt_cp; 2363 struct pmt_counter *pmt_pp; 2364 } sys; 2365 2366 static size_t free_msr_counters_(struct msr_counter **pp) 2367 { 2368 struct msr_counter *p = NULL; 2369 size_t num_freed = 0; 2370 2371 while (*pp) { 2372 p = *pp; 2373 2374 if (p->msr_num != 0) { 2375 *pp = p->next; 2376 2377 free(p); 2378 ++num_freed; 2379 2380 continue; 2381 } 2382 2383 pp = &p->next; 2384 } 2385 2386 return num_freed; 2387 } 2388 2389 /* 2390 * Free all added counters accessed via msr. 2391 */ 2392 static void free_sys_msr_counters(void) 2393 { 2394 /* Thread counters */ 2395 sys.added_thread_counters -= free_msr_counters_(&sys.tp); 2396 2397 /* Core counters */ 2398 sys.added_core_counters -= free_msr_counters_(&sys.cp); 2399 2400 /* Package counters */ 2401 sys.added_package_counters -= free_msr_counters_(&sys.pp); 2402 } 2403 2404 struct counters { 2405 struct thread_data *threads; 2406 struct core_data *cores; 2407 struct pkg_data *packages; 2408 } average, even, odd; 2409 2410 struct platform_counters { 2411 struct rapl_counter energy_psys; /* MSR_PLATFORM_ENERGY_STATUS */ 2412 } platform_counters_odd, platform_counters_even; 2413 2414 #define MAX_HT_ID 3 /* support SMT-4 */ 2415 2416 struct cpu_topology { 2417 int cpu_id; 2418 int core_id; /* unique within a package */ 2419 int module_id; 2420 int package_id; 2421 int die_id; 2422 int l3_id; 2423 int physical_node_id; 2424 int logical_node_id; /* 0-based count within the package */ 2425 int ht_id; /* unique within a core */ 2426 int ht_sibling_cpu_id[MAX_HT_ID + 1]; 2427 int type; 2428 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 2429 } *cpus; 2430 2431 struct topo_params { 2432 int num_packages; 2433 int num_die; 2434 int num_cpus; 2435 int num_cores; /* system wide */ 2436 int allowed_packages; 2437 int allowed_cpus; 2438 int allowed_cores; 2439 int max_cpu_num; 2440 int max_core_id; /* within a package */ 2441 int min_module_id; /* system wide */ 2442 int max_module_id; /* system wide */ 2443 int max_package_id; 2444 int max_die_id; 2445 int max_l3_id; 2446 int max_node_num; 2447 int nodes_per_pkg; 2448 int cores_per_pkg; 2449 int threads_per_core; 2450 } topo; 2451 2452 struct timeval tv_even, tv_odd, tv_delta; 2453 2454 int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 2455 int *irqs_per_cpu; /* indexed by cpu_num */ 2456 int *nmi_per_cpu; /* indexed by cpu_num */ 2457 2458 void setup_all_buffers(bool startup); 2459 2460 char *sys_lpi_file; 2461 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 2462 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 2463 2464 int cpu_is_not_present(int cpu) 2465 { 2466 if (cpu < 0) 2467 return 1; 2468 2469 return !CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set); 2470 } 2471 2472 int cpu_is_not_allowed(int cpu) 2473 { 2474 if (cpu < 0) 2475 return 1; 2476 2477 return !CPU_ISSET_S(cpu, cpu_setsize, cpu_allowed_set); 2478 } 2479 2480 #define GLOBAL_CORE_ID(core_id, pkg_id) (core_id + pkg_id * (topo.max_core_id + 1)) 2481 /* 2482 * run func(thread, core, package) in topology order 2483 * skip non-present cpus 2484 */ 2485 2486 #define PER_THREAD_PARAMS struct thread_data *t, struct core_data *c, struct pkg_data *p 2487 2488 int has_allowed_lower_ht_sibling(int cpu) 2489 { 2490 int i; 2491 2492 for (i = 0; i <= cpus[cpu].ht_id; ++i) { 2493 int sibling_cpu_id = cpus[cpu].ht_sibling_cpu_id[i]; 2494 2495 if (sibling_cpu_id == cpu) 2496 return 0; 2497 2498 if (!cpu_is_not_allowed(sibling_cpu_id)) 2499 return 1; 2500 } 2501 return 0; 2502 } 2503 2504 int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 2505 { 2506 int cpu, retval; 2507 2508 retval = 0; 2509 2510 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 2511 struct thread_data *t; 2512 struct core_data *c; 2513 struct pkg_data *p; 2514 2515 int pkg_id = cpus[cpu].package_id; 2516 2517 if (cpu_is_not_allowed(cpu)) 2518 continue; 2519 2520 if (has_allowed_lower_ht_sibling(cpu)) /* skip HT sibling */ 2521 continue; 2522 2523 t = &thread_base[cpu]; 2524 c = &core_base[GLOBAL_CORE_ID(cpus[cpu].core_id, pkg_id)]; 2525 p = &pkg_base[pkg_id]; 2526 2527 retval |= func(t, c, p); 2528 2529 /* Handle other HT siblings now */ 2530 int i; 2531 2532 for (i = 0; i <= MAX_HT_ID; ++i) { 2533 int sibling_cpu_id = cpus[cpu].ht_sibling_cpu_id[i]; 2534 2535 if (sibling_cpu_id < 0) 2536 break; 2537 2538 if (sibling_cpu_id == cpu) 2539 continue; 2540 2541 if (cpu_is_not_allowed(sibling_cpu_id)) 2542 continue; 2543 2544 t = &thread_base[sibling_cpu_id]; 2545 2546 retval |= func(t, c, p); 2547 } 2548 } 2549 return retval; 2550 } 2551 2552 int is_cpu_first_thread_in_core(struct thread_data *t, struct core_data *c) 2553 { 2554 return ((int)t->cpu_id == c->first_cpu || c->first_cpu < 0); 2555 } 2556 2557 int is_cpu_first_core_in_package(struct thread_data *t, struct pkg_data *p) 2558 { 2559 return ((int)t->cpu_id == p->first_cpu || p->first_cpu < 0); 2560 } 2561 2562 int is_cpu_first_thread_in_package(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2563 { 2564 return is_cpu_first_thread_in_core(t, c) && is_cpu_first_core_in_package(t, p); 2565 } 2566 2567 int cpu_migrate(int cpu) 2568 { 2569 CPU_ZERO_S(cpu_setsize, cpu_affinity_set); 2570 CPU_SET_S(cpu, cpu_setsize, cpu_affinity_set); 2571 if (sched_setaffinity(0, cpu_setsize, cpu_affinity_set) == -1) 2572 return -1; 2573 else 2574 return 0; 2575 } 2576 2577 int get_msr_fd(int cpu) 2578 { 2579 char pathname[32]; 2580 int fd; 2581 2582 fd = fd_percpu[cpu]; 2583 2584 if (fd) 2585 return fd; 2586 sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", cpu); 2587 fd = open(pathname, O_RDONLY); 2588 if (fd < 0) 2589 err(-1, "%s open failed, try chown or chmod +r %s, " "or run with --no-msr, or run as root", pathname, use_android_msr_path ? "/dev/msr*" : "/dev/cpu/*/msr"); 2590 fd_percpu[cpu] = fd; 2591 2592 return fd; 2593 } 2594 2595 static void bic_disable_msr_access(void) 2596 { 2597 CLR_BIC(BIC_Mod_c6, &bic_enabled); 2598 CLR_BIC(BIC_CoreTmp, &bic_enabled); 2599 CLR_BIC(BIC_Totl_c0, &bic_enabled); 2600 CLR_BIC(BIC_Any_c0, &bic_enabled); 2601 CLR_BIC(BIC_GFX_c0, &bic_enabled); 2602 CLR_BIC(BIC_CPUGFX, &bic_enabled); 2603 CLR_BIC(BIC_PkgTmp, &bic_enabled); 2604 2605 free_sys_msr_counters(); 2606 } 2607 2608 static void bic_disable_perf_access(void) 2609 { 2610 CLR_BIC(BIC_IPC, &bic_enabled); 2611 CLR_BIC(BIC_LLC_MRPS, &bic_enabled); 2612 CLR_BIC(BIC_LLC_HIT, &bic_enabled); 2613 CLR_BIC(BIC_L2_MRPS, &bic_enabled); 2614 CLR_BIC(BIC_L2_HIT, &bic_enabled); 2615 } 2616 2617 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 2618 { 2619 assert(!no_perf); 2620 2621 return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 2622 } 2623 2624 static long open_perf_counter(int cpu, unsigned int type, unsigned int config, int group_fd, __u64 read_format) 2625 { 2626 struct perf_event_attr attr; 2627 const pid_t pid = -1; 2628 const unsigned long flags = 0; 2629 2630 assert(!no_perf); 2631 2632 memset(&attr, 0, sizeof(struct perf_event_attr)); 2633 2634 attr.type = type; 2635 attr.size = sizeof(struct perf_event_attr); 2636 attr.config = config; 2637 attr.disabled = 0; 2638 attr.sample_type = PERF_SAMPLE_IDENTIFIER; 2639 attr.read_format = read_format; 2640 2641 const int fd = perf_event_open(&attr, pid, cpu, group_fd, flags); 2642 2643 return fd; 2644 } 2645 2646 int get_instr_count_fd(int cpu) 2647 { 2648 if (fd_instr_count_percpu[cpu]) 2649 return fd_instr_count_percpu[cpu]; 2650 2651 fd_instr_count_percpu[cpu] = open_perf_counter(cpu, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS, -1, 0); 2652 2653 return fd_instr_count_percpu[cpu]; 2654 } 2655 2656 void get_msr(int cpu, off_t offset, unsigned long long *msr) 2657 { 2658 assert(!no_msr); 2659 2660 if (pread(get_msr_fd(cpu), msr, sizeof(*msr), offset) != sizeof(*msr)) 2661 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 2662 } 2663 2664 int add_msr_counter(int cpu, off_t offset) 2665 { 2666 ssize_t retval; 2667 unsigned long long value; 2668 2669 if (no_msr) 2670 return -1; 2671 2672 if (!offset) 2673 return -1; 2674 2675 retval = pread(get_msr_fd(cpu), &value, sizeof(value), offset); 2676 2677 /* if the read failed, the probe fails */ 2678 if (retval != sizeof(value)) 2679 return -1; 2680 2681 if (value == 0) 2682 return 0; 2683 2684 return 1; 2685 } 2686 2687 int add_rapl_msr_counter(int cpu, const struct rapl_counter_arch_info *cai) 2688 { 2689 int ret; 2690 2691 if (!(valid_rapl_msrs & cai->feature_mask)) 2692 return -1; 2693 2694 ret = add_msr_counter(cpu, cai->msr); 2695 if (ret < 0) 2696 return -1; 2697 2698 switch (cai->rci_index) { 2699 case RAPL_RCI_INDEX_ENERGY_PKG: 2700 case RAPL_RCI_INDEX_ENERGY_CORES: 2701 case RAPL_RCI_INDEX_DRAM: 2702 case RAPL_RCI_INDEX_GFX: 2703 case RAPL_RCI_INDEX_ENERGY_PLATFORM: 2704 if (ret == 0) 2705 return 1; 2706 } 2707 2708 /* PKG,DRAM_PERF_STATUS MSRs, can return any value */ 2709 return 1; 2710 } 2711 2712 /* Convert CPU ID to domain ID for given added perf counter. */ 2713 unsigned int cpu_to_domain(const struct perf_counter_info *pc, int cpu) 2714 { 2715 switch (pc->scope) { 2716 case SCOPE_CPU: 2717 return cpu; 2718 2719 case SCOPE_CORE: 2720 return cpus[cpu].core_id; 2721 2722 case SCOPE_PACKAGE: 2723 return cpus[cpu].package_id; 2724 } 2725 2726 __builtin_unreachable(); 2727 } 2728 2729 #define MAX_DEFERRED 16 2730 char *deferred_add_names[MAX_DEFERRED]; 2731 char *deferred_skip_names[MAX_DEFERRED]; 2732 int deferred_add_index; 2733 int deferred_skip_index; 2734 unsigned int deferred_add_consumed; 2735 unsigned int deferred_skip_consumed; 2736 2737 /* 2738 * HIDE_LIST - hide this list of counters, show the rest [default] 2739 * SHOW_LIST - show this list of counters, hide the rest 2740 */ 2741 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 2742 2743 void help(void) 2744 { 2745 fprintf(outf, 2746 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 2747 "\n" 2748 "Turbostat forks the specified COMMAND and prints statistics\n" 2749 "when COMMAND completes.\n" 2750 "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 2751 "to print statistics, until interrupted.\n" 2752 " -a, --add counter\n" 2753 " add a counter\n" 2754 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 2755 " eg. --add perf/cstate_pkg/c2-residency,package,delta,percent,perfPC2\n" 2756 " eg. --add pmt,name=XTAL,type=raw,domain=package0,offset=0,lsb=0,msb=63,guid=0x1a067102\n" 2757 " -c, --cpu cpu-set\n" 2758 " limit output to summary plus cpu-set:\n" 2759 " {core | package | j,k,l..m,n-p }\n" 2760 " -d, --debug\n" 2761 " displays usec, Time_Of_Day_Seconds and more debugging\n" 2762 " debug messages are printed to stderr\n" 2763 " -D, --Dump\n" 2764 " displays the raw counter values\n" 2765 " -e, --enable [all | column]\n" 2766 " shows all or the specified disabled column\n" 2767 " -f, --force\n" 2768 " force load turbostat with minimum default features on unsupported platforms.\n" 2769 " -H, --hide [column | column,column,...]\n" 2770 " hide the specified column(s)\n" 2771 " -i, --interval sec.subsec\n" 2772 " override default 5-second measurement interval\n" 2773 " -J, --Joules\n" 2774 " displays energy in Joules instead of Watts\n" 2775 " -l, --list\n" 2776 " list column headers only\n" 2777 " -M, --no-msr\n" 2778 " disable all uses of the MSR driver\n" 2779 " -P, --no-perf\n" 2780 " disable all uses of the perf API\n" 2781 " -n, --num_iterations num\n" 2782 " number of the measurement iterations\n" 2783 " -N, --header_iterations num\n" 2784 " print header every num iterations\n" 2785 " -o, --out file\n" 2786 " create or truncate \"file\" for all output\n" 2787 " -q, --quiet\n" 2788 " skip decoding system configuration header\n" 2789 " -s, --show [column | column,column,...]\n" 2790 " show only the specified column(s)\n" 2791 " -S, --Summary\n" 2792 " limits output to 1-line system summary per interval\n" 2793 " -T, --TCC temperature\n" 2794 " sets the Thermal Control Circuit temperature in\n" 2795 " degrees Celsius\n" 2796 " -h, --help\n" " print this help message\n -v, --version\n\t\tprint version information\n\nFor more help, run \"man turbostat\"\n"); 2797 } 2798 2799 /* 2800 * bic_lookup 2801 * for all the strings in comma separate name_list, 2802 * set the approprate bit in return value. 2803 */ 2804 void bic_lookup(bic_set_t *ret_set, char *name_list, enum show_hide_mode mode) 2805 { 2806 unsigned int i; 2807 2808 while (name_list) { 2809 char *comma; 2810 2811 comma = strchr(name_list, ','); 2812 2813 if (comma) 2814 *comma = '\0'; 2815 2816 for (i = 0; i < MAX_BIC; ++i) { 2817 if (!strcmp(name_list, bic[i].name)) { 2818 SET_BIC(i, ret_set); 2819 break; 2820 } 2821 if (!strcmp(name_list, "all")) { 2822 bic_set_all(ret_set); 2823 break; 2824 } else if (!strcmp(name_list, "topology")) { 2825 CPU_OR(ret_set, ret_set, &bic_group_topology); 2826 break; 2827 } else if (!strcmp(name_list, "power")) { 2828 CPU_OR(ret_set, ret_set, &bic_group_thermal_pwr); 2829 break; 2830 } else if (!strcmp(name_list, "idle")) { 2831 CPU_OR(ret_set, ret_set, &bic_group_idle); 2832 break; 2833 } else if (!strcmp(name_list, "cache")) { 2834 CPU_OR(ret_set, ret_set, &bic_group_cache); 2835 break; 2836 } else if (!strcmp(name_list, "llc")) { 2837 CPU_OR(ret_set, ret_set, &bic_group_cache); 2838 break; 2839 } else if (!strcmp(name_list, "swidle")) { 2840 CPU_OR(ret_set, ret_set, &bic_group_sw_idle); 2841 break; 2842 } else if (!strcmp(name_list, "sysfs")) { /* legacy compatibility */ 2843 CPU_OR(ret_set, ret_set, &bic_group_sw_idle); 2844 break; 2845 } else if (!strcmp(name_list, "hwidle")) { 2846 CPU_OR(ret_set, ret_set, &bic_group_hw_idle); 2847 break; 2848 } else if (!strcmp(name_list, "frequency")) { 2849 CPU_OR(ret_set, ret_set, &bic_group_frequency); 2850 break; 2851 } else if (!strcmp(name_list, "other")) { 2852 CPU_OR(ret_set, ret_set, &bic_group_other); 2853 break; 2854 } 2855 } 2856 if (i == MAX_BIC) { 2857 if (mode == SHOW_LIST) { 2858 deferred_add_names[deferred_add_index++] = name_list; 2859 if (deferred_add_index >= MAX_DEFERRED) { 2860 fprintf(stderr, "More than max %d un-recognized --add options '%s'\n", MAX_DEFERRED, name_list); 2861 help(); 2862 exit(1); 2863 } 2864 } else { 2865 deferred_skip_names[deferred_skip_index++] = name_list; 2866 if (debug) 2867 fprintf(stderr, "deferred \"%s\"\n", name_list); 2868 if (deferred_skip_index >= MAX_DEFERRED) { 2869 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", MAX_DEFERRED, name_list); 2870 help(); 2871 exit(1); 2872 } 2873 } 2874 } 2875 2876 name_list = comma; 2877 if (name_list) 2878 name_list++; 2879 2880 } 2881 } 2882 2883 /* 2884 * print_name() 2885 * Print column header name for raw 64-bit counter in 16 columns (at least 8-char plus a tab) 2886 * Otherwise, allow the name + tab to fit within 8-coumn tab-stop. 2887 * In both cases, left justififed, just like other turbostat columns, 2888 * to allow the column values to consume the tab. 2889 * 2890 * Yes, 32-bit counters can overflow 8-columns, and 2891 * 64-bit counters can overflow 16-columns, but that is uncommon. 2892 */ 2893 static inline int print_name(int width, int *printed, char *delim, char *name, enum counter_type type, enum counter_format format) 2894 { 2895 UNUSED(type); 2896 char *sep = (*printed)++ ? delim : ""; 2897 2898 if (format == FORMAT_RAW && width >= 64) 2899 return sprintf(outp, "%s%-8s", sep, name); 2900 else 2901 return sprintf(outp, "%s%s", sep, name); 2902 } 2903 2904 static inline int print_hex_value(int width, int *printed, char *delim, unsigned long long value) 2905 { 2906 char *sep = (*printed)++ ? delim : ""; 2907 2908 if (width <= 32) 2909 return sprintf(outp, "%s%08llx", sep, value); 2910 else 2911 return sprintf(outp, "%s%016llx", sep, value); 2912 } 2913 2914 static inline int print_decimal_value(int width, int *printed, char *delim, unsigned long long value) 2915 { 2916 char *sep = (*printed)++ ? delim : ""; 2917 2918 UNUSED(width); 2919 2920 return sprintf(outp, "%s%lld", sep, value); 2921 } 2922 2923 static inline int print_float_value(int *printed, char *delim, double value) 2924 { 2925 char *sep = (*printed)++ ? delim : ""; 2926 2927 return sprintf(outp, "%s%0.2f", sep, value); 2928 } 2929 2930 void print_header(char *delim) 2931 { 2932 struct msr_counter *mp; 2933 struct perf_counter_info *pp; 2934 struct pmt_counter *ppmt; 2935 int printed = 0; 2936 2937 if (DO_BIC(BIC_USEC)) 2938 outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 2939 if (DO_BIC(BIC_TOD)) 2940 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 2941 if (DO_BIC(BIC_Package)) 2942 outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 2943 if (DO_BIC(BIC_Die)) 2944 outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 2945 if (DO_BIC(BIC_L3)) 2946 outp += sprintf(outp, "%sL3", (printed++ ? delim : "")); 2947 if (DO_BIC(BIC_Node)) 2948 outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 2949 if (DO_BIC(BIC_Module)) 2950 outp += sprintf(outp, "%sModule", (printed++ ? delim : "")); 2951 if (DO_BIC(BIC_Core)) 2952 outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 2953 if (DO_BIC(BIC_CPU)) 2954 outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 2955 if (DO_BIC(BIC_APIC)) 2956 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 2957 if (DO_BIC(BIC_X2APIC)) 2958 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 2959 if (DO_BIC(BIC_Avg_MHz)) 2960 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 2961 if (DO_BIC(BIC_Busy)) 2962 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 2963 if (DO_BIC(BIC_Bzy_MHz)) 2964 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 2965 if (DO_BIC(BIC_TSC_MHz)) 2966 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 2967 2968 if (DO_BIC(BIC_IPC)) 2969 outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 2970 2971 if (DO_BIC(BIC_IRQ)) { 2972 if (sums_need_wide_columns) 2973 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 2974 else 2975 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 2976 } 2977 if (DO_BIC(BIC_NMI)) { 2978 if (sums_need_wide_columns) 2979 outp += sprintf(outp, "%s NMI", (printed++ ? delim : "")); 2980 else 2981 outp += sprintf(outp, "%sNMI", (printed++ ? delim : "")); 2982 } 2983 2984 if (DO_BIC(BIC_SMI)) 2985 outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 2986 2987 if (DO_BIC(BIC_LLC_MRPS)) 2988 outp += sprintf(outp, "%sLLCMRPS", (printed++ ? delim : "")); 2989 2990 if (DO_BIC(BIC_LLC_HIT)) 2991 outp += sprintf(outp, "%sLLC%%hit", (printed++ ? delim : "")); 2992 2993 if (DO_BIC(BIC_L2_MRPS)) 2994 outp += sprintf(outp, "%sL2MRPS", (printed++ ? delim : "")); 2995 2996 if (DO_BIC(BIC_L2_HIT)) 2997 outp += sprintf(outp, "%sL2%%hit", (printed++ ? delim : "")); 2998 2999 for (mp = sys.tp; mp; mp = mp->next) 3000 outp += print_name(mp->width, &printed, delim, mp->name, mp->type, mp->format); 3001 3002 for (pp = sys.perf_tp; pp; pp = pp->next) 3003 outp += print_name(pp->width, &printed, delim, pp->name, pp->type, pp->format); 3004 3005 ppmt = sys.pmt_tp; 3006 while (ppmt) { 3007 switch (ppmt->type) { 3008 case PMT_TYPE_RAW: 3009 outp += print_name(pmt_counter_get_width(ppmt), &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3010 break; 3011 3012 case PMT_TYPE_XTAL_TIME: 3013 case PMT_TYPE_TCORE_CLOCK: 3014 outp += print_name(32, &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3015 break; 3016 } 3017 3018 ppmt = ppmt->next; 3019 } 3020 3021 if (DO_BIC(BIC_CPU_c1)) 3022 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 3023 if (DO_BIC(BIC_CPU_c3)) 3024 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 3025 if (DO_BIC(BIC_CPU_c6)) 3026 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 3027 if (DO_BIC(BIC_CPU_c7)) 3028 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 3029 3030 if (DO_BIC(BIC_Mod_c6)) 3031 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 3032 3033 if (DO_BIC(BIC_CoreTmp)) 3034 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 3035 3036 if (DO_BIC(BIC_CORE_THROT_CNT)) 3037 outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : "")); 3038 3039 if (valid_rapl_msrs && !rapl_joules) { 3040 if (DO_BIC(BIC_CorWatt) && platform->has_per_core_rapl) 3041 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 3042 } else if (valid_rapl_msrs && rapl_joules) { 3043 if (DO_BIC(BIC_Cor_J) && platform->has_per_core_rapl) 3044 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 3045 } 3046 3047 for (mp = sys.cp; mp; mp = mp->next) 3048 outp += print_name(mp->width, &printed, delim, mp->name, mp->type, mp->format); 3049 3050 for (pp = sys.perf_cp; pp; pp = pp->next) 3051 outp += print_name(pp->width, &printed, delim, pp->name, pp->type, pp->format); 3052 3053 ppmt = sys.pmt_cp; 3054 while (ppmt) { 3055 switch (ppmt->type) { 3056 case PMT_TYPE_RAW: 3057 outp += print_name(pmt_counter_get_width(ppmt), &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3058 3059 break; 3060 3061 case PMT_TYPE_XTAL_TIME: 3062 case PMT_TYPE_TCORE_CLOCK: 3063 outp += print_name(32, &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3064 break; 3065 } 3066 3067 ppmt = ppmt->next; 3068 } 3069 if (DO_BIC(BIC_PkgTmp)) 3070 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 3071 3072 if (DO_BIC(BIC_GFX_rc6)) 3073 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 3074 3075 if (DO_BIC(BIC_GFXMHz)) 3076 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 3077 3078 if (DO_BIC(BIC_GFXACTMHz)) 3079 outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 3080 3081 if (DO_BIC(BIC_SAM_mc6)) 3082 outp += sprintf(outp, "%sSAM%%mc6", (printed++ ? delim : "")); 3083 3084 if (DO_BIC(BIC_SAMMHz)) 3085 outp += sprintf(outp, "%sSAMMHz", (printed++ ? delim : "")); 3086 3087 if (DO_BIC(BIC_SAMACTMHz)) 3088 outp += sprintf(outp, "%sSAMAMHz", (printed++ ? delim : "")); 3089 3090 if (DO_BIC(BIC_Totl_c0)) 3091 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 3092 if (DO_BIC(BIC_Any_c0)) 3093 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 3094 if (DO_BIC(BIC_GFX_c0)) 3095 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 3096 if (DO_BIC(BIC_CPUGFX)) 3097 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 3098 3099 if (DO_BIC(BIC_Pkgpc2)) 3100 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 3101 if (DO_BIC(BIC_Pkgpc3)) 3102 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 3103 if (DO_BIC(BIC_Pkgpc6)) 3104 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 3105 if (DO_BIC(BIC_Pkgpc7)) 3106 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 3107 if (DO_BIC(BIC_Pkgpc8)) 3108 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 3109 if (DO_BIC(BIC_Pkgpc9)) 3110 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 3111 if (DO_BIC(BIC_Pkgpc10)) 3112 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 3113 if (DO_BIC(BIC_Diec6)) 3114 outp += sprintf(outp, "%sDie%%c6", (printed++ ? delim : "")); 3115 if (DO_BIC(BIC_CPU_LPI)) 3116 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 3117 if (DO_BIC(BIC_SYS_LPI)) 3118 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 3119 3120 if (!rapl_joules) { 3121 if (DO_BIC(BIC_PkgWatt)) 3122 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 3123 if (DO_BIC(BIC_CorWatt) && !platform->has_per_core_rapl) 3124 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 3125 if (DO_BIC(BIC_GFXWatt)) 3126 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 3127 if (DO_BIC(BIC_RAMWatt)) 3128 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 3129 if (DO_BIC(BIC_PKG__)) 3130 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 3131 if (DO_BIC(BIC_RAM__)) 3132 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 3133 } else { 3134 if (DO_BIC(BIC_Pkg_J)) 3135 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 3136 if (DO_BIC(BIC_Cor_J) && !platform->has_per_core_rapl) 3137 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 3138 if (DO_BIC(BIC_GFX_J)) 3139 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 3140 if (DO_BIC(BIC_RAM_J)) 3141 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 3142 if (DO_BIC(BIC_PKG__)) 3143 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 3144 if (DO_BIC(BIC_RAM__)) 3145 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 3146 } 3147 if (DO_BIC(BIC_UNCORE_MHZ)) 3148 outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : "")); 3149 3150 for (mp = sys.pp; mp; mp = mp->next) 3151 outp += print_name(mp->width, &printed, delim, mp->name, mp->type, mp->format); 3152 3153 for (pp = sys.perf_pp; pp; pp = pp->next) 3154 outp += print_name(pp->width, &printed, delim, pp->name, pp->type, pp->format); 3155 3156 ppmt = sys.pmt_pp; 3157 while (ppmt) { 3158 switch (ppmt->type) { 3159 case PMT_TYPE_RAW: 3160 outp += print_name(pmt_counter_get_width(ppmt), &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3161 break; 3162 3163 case PMT_TYPE_XTAL_TIME: 3164 case PMT_TYPE_TCORE_CLOCK: 3165 outp += print_name(32, &printed, delim, ppmt->name, COUNTER_ITEMS, ppmt->format); 3166 break; 3167 } 3168 3169 ppmt = ppmt->next; 3170 } 3171 3172 if (DO_BIC(BIC_SysWatt)) 3173 outp += sprintf(outp, "%sSysWatt", (printed++ ? delim : "")); 3174 if (DO_BIC(BIC_Sys_J)) 3175 outp += sprintf(outp, "%sSys_J", (printed++ ? delim : "")); 3176 3177 outp += sprintf(outp, "\n"); 3178 } 3179 3180 /* 3181 * pct(numerator, denominator) 3182 * 3183 * Return sanity checked percentage (100.0 * numerator/denominotor) 3184 * 3185 * n < 0: nan 3186 * d <= 0: nan 3187 * n/d > 1.1: nan 3188 */ 3189 double pct(double numerator, double denominator) 3190 { 3191 double retval; 3192 3193 if (numerator < 0) 3194 return nan(""); 3195 3196 if (denominator <= 0) 3197 return nan(""); 3198 3199 retval = 100.0 * numerator / denominator; 3200 3201 if (retval > 110.0) 3202 return nan(""); 3203 3204 return retval; 3205 } 3206 3207 int dump_counters(PER_THREAD_PARAMS) 3208 { 3209 int i; 3210 struct msr_counter *mp; 3211 struct platform_counters *pplat_cnt = p == odd.packages ? &platform_counters_odd : &platform_counters_even; 3212 3213 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 3214 3215 if (t) { 3216 outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 3217 outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 3218 outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 3219 outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 3220 outp += sprintf(outp, "c1: %016llX\n", t->c1); 3221 3222 if (DO_BIC(BIC_IPC)) 3223 outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 3224 3225 if (DO_BIC(BIC_IRQ)) 3226 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 3227 if (DO_BIC(BIC_NMI)) 3228 outp += sprintf(outp, "IRQ: %lld\n", t->nmi_count); 3229 if (DO_BIC(BIC_SMI)) 3230 outp += sprintf(outp, "SMI: %d\n", t->smi_count); 3231 3232 outp += sprintf(outp, "LLC refs: %lld", t->llc.references); 3233 outp += sprintf(outp, "LLC miss: %lld", t->llc.misses); 3234 outp += sprintf(outp, "LLC Hit%%: %.2f", pct((t->llc.references - t->llc.misses), t->llc.references)); 3235 3236 outp += sprintf(outp, "L2 refs: %lld", t->l2.references); 3237 outp += sprintf(outp, "L2 hits: %lld", t->l2.hits); 3238 outp += sprintf(outp, "L2 Hit%%: %.2f", pct(t->l2.hits, t->l2.references)); 3239 3240 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 3241 outp += sprintf(outp, "tADDED [%d] %8s msr0x%x: %08llX %s\n", i, mp->name, mp->msr_num, t->counter[i], mp->sp->path); 3242 } 3243 } 3244 3245 if (c && is_cpu_first_thread_in_core(t, c)) { 3246 outp += sprintf(outp, "core: 0x%x\n", cpus[t->cpu_id].core_id); 3247 outp += sprintf(outp, "c3: %016llX\n", c->c3); 3248 outp += sprintf(outp, "c6: %016llX\n", c->c6); 3249 outp += sprintf(outp, "c7: %016llX\n", c->c7); 3250 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 3251 outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt); 3252 3253 const unsigned long long energy_value = c->core_energy.raw_value * c->core_energy.scale; 3254 const double energy_scale = c->core_energy.scale; 3255 3256 if (c->core_energy.unit == RAPL_UNIT_JOULES) 3257 outp += sprintf(outp, "Joules: %0llX (scale: %lf)\n", energy_value, energy_scale); 3258 3259 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 3260 outp += sprintf(outp, "cADDED [%d] %8s msr0x%x: %08llX %s\n", i, mp->name, mp->msr_num, c->counter[i], mp->sp->path); 3261 } 3262 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 3263 } 3264 3265 if (p && is_cpu_first_core_in_package(t, p)) { 3266 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 3267 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 3268 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 3269 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 3270 3271 outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 3272 if (DO_BIC(BIC_Pkgpc3)) 3273 outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 3274 if (DO_BIC(BIC_Pkgpc6)) 3275 outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 3276 if (DO_BIC(BIC_Pkgpc7)) 3277 outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 3278 outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 3279 outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 3280 outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 3281 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 3282 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 3283 outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg.raw_value); 3284 outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores.raw_value); 3285 outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx.raw_value); 3286 outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram.raw_value); 3287 outp += sprintf(outp, "Joules PSYS: %0llX\n", pplat_cnt->energy_psys.raw_value); 3288 outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status.raw_value); 3289 outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status.raw_value); 3290 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 3291 3292 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 3293 outp += sprintf(outp, "pADDED [%d] %8s msr0x%x: %08llX %s\n", i, mp->name, mp->msr_num, p->counter[i], mp->sp->path); 3294 } 3295 } 3296 3297 outp += sprintf(outp, "\n"); 3298 3299 return 0; 3300 } 3301 3302 double rapl_counter_get_value(const struct rapl_counter *c, enum rapl_unit desired_unit, double interval) 3303 { 3304 /* 3305 * For now we don't expect anything other than joules, 3306 * so just simplify the logic. 3307 */ 3308 assert(c->unit == RAPL_UNIT_JOULES); 3309 3310 const double scaled = c->raw_value * c->scale; 3311 3312 if (desired_unit == RAPL_UNIT_WATTS) 3313 return scaled / interval; 3314 return scaled; 3315 } 3316 3317 void get_perf_llc_stats(int cpu, struct llc_stats *llc) 3318 { 3319 struct read_format { 3320 unsigned long long num_read; 3321 struct llc_stats llc; 3322 } r; 3323 const ssize_t expected_read_size = sizeof(r); 3324 ssize_t actual_read_size; 3325 3326 actual_read_size = read(fd_llc_percpu[cpu], &r, expected_read_size); 3327 3328 if (actual_read_size == -1) 3329 err(-1, "%s(cpu%d,) %d,,%ld", __func__, cpu, fd_llc_percpu[cpu], expected_read_size); 3330 3331 llc->references = r.llc.references; 3332 llc->misses = r.llc.misses; 3333 if (actual_read_size != expected_read_size) 3334 warn("%s: failed to read perf_data (req %zu act %zu)", __func__, expected_read_size, actual_read_size); 3335 } 3336 3337 void get_perf_l2_stats(int cpu, struct l2_stats *l2) 3338 { 3339 struct read_format { 3340 unsigned long long num_read; 3341 struct l2_stats l2; 3342 } r; 3343 const ssize_t expected_read_size = sizeof(r); 3344 ssize_t actual_read_size; 3345 3346 actual_read_size = read(fd_l2_percpu[cpu], &r, expected_read_size); 3347 3348 if (actual_read_size == -1) 3349 err(-1, "%s(cpu%d,) %d,,%ld", __func__, cpu, fd_l2_percpu[cpu], expected_read_size); 3350 3351 l2->references = r.l2.references; 3352 l2->hits = r.l2.hits; 3353 if (actual_read_size != expected_read_size) 3354 warn("%s: cpu%d: failed to read(%d) perf_data (req %zu act %zu)", __func__, cpu, fd_l2_percpu[cpu], expected_read_size, actual_read_size); 3355 } 3356 3357 /* 3358 * column formatting convention & formats 3359 */ 3360 int format_counters(PER_THREAD_PARAMS) 3361 { 3362 static int count; 3363 3364 struct platform_counters *pplat_cnt = NULL; 3365 double interval_float, tsc; 3366 char *fmt8 = "%s%.2f"; 3367 3368 int i; 3369 struct msr_counter *mp; 3370 struct perf_counter_info *pp; 3371 struct pmt_counter *ppmt; 3372 char *delim = "\t"; 3373 int printed = 0; 3374 3375 if (t == average.threads) { 3376 pplat_cnt = count & 1 ? &platform_counters_odd : &platform_counters_even; 3377 ++count; 3378 } 3379 3380 /* if showing only 1st thread in core and this isn't one, bail out */ 3381 if (show_core_only && !is_cpu_first_thread_in_core(t, c)) 3382 return 0; 3383 3384 /* if showing only 1st thread in pkg and this isn't one, bail out */ 3385 if (show_pkg_only && !is_cpu_first_core_in_package(t, p)) 3386 return 0; 3387 3388 /*if not summary line and --cpu is used */ 3389 if ((t != average.threads) && (cpuset_cmdline && !CPU_ISSET_S(t->cpu_id, cpu_setsize, cpuset_cmdline))) 3390 return 0; 3391 3392 if (DO_BIC(BIC_USEC)) { 3393 /* on each row, print how many usec each timestamp took to gather */ 3394 struct timeval tv; 3395 3396 timersub(&t->tv_end, &t->tv_begin, &tv); 3397 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 3398 } 3399 3400 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 3401 if (DO_BIC(BIC_TOD)) 3402 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 3403 3404 interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 3405 3406 tsc = t->tsc * tsc_tweak; 3407 3408 /* topo columns, print blanks on 1st (average) line */ 3409 if (t == average.threads) { 3410 if (DO_BIC(BIC_Package)) 3411 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3412 if (DO_BIC(BIC_Die)) 3413 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3414 if (DO_BIC(BIC_L3)) 3415 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3416 if (DO_BIC(BIC_Node)) 3417 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3418 if (DO_BIC(BIC_Module)) 3419 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3420 if (DO_BIC(BIC_Core)) 3421 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3422 if (DO_BIC(BIC_CPU)) 3423 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3424 if (DO_BIC(BIC_APIC)) 3425 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3426 if (DO_BIC(BIC_X2APIC)) 3427 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3428 } else { 3429 if (DO_BIC(BIC_Package)) { 3430 if (p) 3431 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].package_id); 3432 else 3433 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3434 } 3435 if (DO_BIC(BIC_Die)) { 3436 if (c) 3437 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 3438 else 3439 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3440 } 3441 if (DO_BIC(BIC_L3)) { 3442 if (c) 3443 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].l3_id); 3444 else 3445 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3446 } 3447 if (DO_BIC(BIC_Node)) { 3448 if (t) 3449 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 3450 else 3451 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3452 } 3453 if (DO_BIC(BIC_Module)) { 3454 if (c) 3455 outp += sprintf(outp, "%s0x%x", (printed++ ? delim : ""), cpus[t->cpu_id].module_id); 3456 else 3457 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3458 } 3459 if (DO_BIC(BIC_Core)) { 3460 if (c) 3461 outp += sprintf(outp, "%s0x%x", (printed++ ? delim : ""), cpus[t->cpu_id].core_id); 3462 else 3463 outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 3464 } 3465 if (DO_BIC(BIC_CPU)) 3466 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 3467 if (DO_BIC(BIC_APIC)) 3468 outp += sprintf(outp, "%s0x%x", (printed++ ? delim : ""), t->apic_id); 3469 if (DO_BIC(BIC_X2APIC)) 3470 outp += sprintf(outp, "%s0x%x", (printed++ ? delim : ""), t->x2apic_id); 3471 } 3472 3473 if (DO_BIC(BIC_Avg_MHz)) 3474 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 3475 3476 if (DO_BIC(BIC_Busy)) 3477 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(t->mperf, tsc)); 3478 3479 if (DO_BIC(BIC_Bzy_MHz)) { 3480 if (has_base_hz) 3481 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 3482 else 3483 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), tsc / units * t->aperf / t->mperf / interval_float); 3484 } 3485 3486 if (DO_BIC(BIC_TSC_MHz)) 3487 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 3488 3489 if (DO_BIC(BIC_IPC)) 3490 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 3491 3492 /* IRQ */ 3493 if (DO_BIC(BIC_IRQ)) { 3494 if (sums_need_wide_columns) 3495 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 3496 else 3497 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 3498 } 3499 3500 /* NMI */ 3501 if (DO_BIC(BIC_NMI)) { 3502 if (sums_need_wide_columns) 3503 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->nmi_count); 3504 else 3505 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->nmi_count); 3506 } 3507 3508 /* SMI */ 3509 if (DO_BIC(BIC_SMI)) 3510 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 3511 3512 /* LLC Stats */ 3513 if (DO_BIC(BIC_LLC_MRPS)) 3514 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), t->llc.references / interval_float / 1000000); 3515 3516 if (DO_BIC(BIC_LLC_HIT)) 3517 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), pct((t->llc.references - t->llc.misses), t->llc.references)); 3518 3519 /* L2 Stats */ 3520 if (DO_BIC(BIC_L2_MRPS)) 3521 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), t->l2.references / interval_float / 1000000); 3522 3523 if (DO_BIC(BIC_L2_HIT)) 3524 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), pct(t->l2.hits, t->l2.references)); 3525 3526 /* Added Thread Counters */ 3527 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 3528 if (mp->format == FORMAT_RAW) 3529 outp += print_hex_value(mp->width, &printed, delim, t->counter[i]); 3530 else if (mp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE) 3531 outp += print_decimal_value(mp->width, &printed, delim, t->counter[i]); 3532 else if (mp->format == FORMAT_PERCENT) { 3533 if (mp->type == COUNTER_USEC) 3534 outp += print_float_value(&printed, delim, t->counter[i] / interval_float / 10000); 3535 else 3536 outp += print_float_value(&printed, delim, pct(t->counter[i], tsc)); 3537 } 3538 } 3539 3540 /* Added perf Thread Counters */ 3541 for (i = 0, pp = sys.perf_tp; pp; ++i, pp = pp->next) { 3542 if (pp->format == FORMAT_RAW) 3543 outp += print_hex_value(pp->width, &printed, delim, t->perf_counter[i]); 3544 else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE) 3545 outp += print_decimal_value(pp->width, &printed, delim, t->perf_counter[i]); 3546 else if (pp->format == FORMAT_PERCENT) { 3547 if (pp->type == COUNTER_USEC) 3548 outp += print_float_value(&printed, delim, t->perf_counter[i] / interval_float / 10000); 3549 else 3550 outp += print_float_value(&printed, delim, pct(t->perf_counter[i], tsc)); 3551 } 3552 } 3553 3554 /* Added PMT Thread Counters */ 3555 for (i = 0, ppmt = sys.pmt_tp; ppmt; i++, ppmt = ppmt->next) { 3556 const unsigned long value_raw = t->pmt_counter[i]; 3557 double value_converted; 3558 switch (ppmt->type) { 3559 case PMT_TYPE_RAW: 3560 outp += print_hex_value(pmt_counter_get_width(ppmt), &printed, delim, t->pmt_counter[i]); 3561 break; 3562 3563 case PMT_TYPE_XTAL_TIME: 3564 value_converted = pct(value_raw / crystal_hz, interval_float); 3565 outp += print_float_value(&printed, delim, value_converted); 3566 break; 3567 3568 case PMT_TYPE_TCORE_CLOCK: 3569 value_converted = pct(value_raw / tcore_clock_freq_hz, interval_float); 3570 outp += print_float_value(&printed, delim, value_converted); 3571 } 3572 } 3573 3574 /* C1 */ 3575 if (DO_BIC(BIC_CPU_c1)) 3576 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(t->c1, tsc)); 3577 3578 /* print per-core data only for 1st thread in core */ 3579 if (!is_cpu_first_thread_in_core(t, c)) 3580 goto done; 3581 3582 if (DO_BIC(BIC_CPU_c3)) 3583 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c3, tsc)); 3584 if (DO_BIC(BIC_CPU_c6)) 3585 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c6, tsc)); 3586 if (DO_BIC(BIC_CPU_c7)) 3587 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c7, tsc)); 3588 3589 /* Mod%c6 */ 3590 if (DO_BIC(BIC_Mod_c6)) 3591 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->mc6_us, tsc)); 3592 3593 if (DO_BIC(BIC_CoreTmp)) 3594 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 3595 3596 /* Core throttle count */ 3597 if (DO_BIC(BIC_CORE_THROT_CNT)) 3598 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt); 3599 3600 /* Added Core Counters */ 3601 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 3602 if (mp->format == FORMAT_RAW) 3603 outp += print_hex_value(mp->width, &printed, delim, c->counter[i]); 3604 else if (mp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE) 3605 outp += print_decimal_value(mp->width, &printed, delim, c->counter[i]); 3606 else if (mp->format == FORMAT_PERCENT) 3607 outp += print_float_value(&printed, delim, pct(c->counter[i], tsc)); 3608 } 3609 3610 /* Added perf Core counters */ 3611 for (i = 0, pp = sys.perf_cp; pp; i++, pp = pp->next) { 3612 if (pp->format == FORMAT_RAW) 3613 outp += print_hex_value(pp->width, &printed, delim, c->perf_counter[i]); 3614 else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE) 3615 outp += print_decimal_value(pp->width, &printed, delim, c->perf_counter[i]); 3616 else if (pp->format == FORMAT_PERCENT) 3617 outp += print_float_value(&printed, delim, pct(c->perf_counter[i], tsc)); 3618 } 3619 3620 /* Added PMT Core counters */ 3621 for (i = 0, ppmt = sys.pmt_cp; ppmt; i++, ppmt = ppmt->next) { 3622 const unsigned long value_raw = c->pmt_counter[i]; 3623 double value_converted; 3624 switch (ppmt->type) { 3625 case PMT_TYPE_RAW: 3626 outp += print_hex_value(pmt_counter_get_width(ppmt), &printed, delim, c->pmt_counter[i]); 3627 break; 3628 3629 case PMT_TYPE_XTAL_TIME: 3630 value_converted = pct(value_raw / crystal_hz, interval_float); 3631 outp += print_float_value(&printed, delim, value_converted); 3632 break; 3633 3634 case PMT_TYPE_TCORE_CLOCK: 3635 value_converted = pct(value_raw / tcore_clock_freq_hz, interval_float); 3636 outp += print_float_value(&printed, delim, value_converted); 3637 } 3638 } 3639 3640 if (DO_BIC(BIC_CorWatt) && platform->has_per_core_rapl) 3641 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&c->core_energy, RAPL_UNIT_WATTS, interval_float)); 3642 if (DO_BIC(BIC_Cor_J) && platform->has_per_core_rapl) 3643 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&c->core_energy, RAPL_UNIT_JOULES, interval_float)); 3644 3645 /* print per-package data only for 1st core in package */ 3646 if (!is_cpu_first_core_in_package(t, p)) 3647 goto done; 3648 3649 /* PkgTmp */ 3650 if (DO_BIC(BIC_PkgTmp)) 3651 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 3652 3653 /* GFXrc6 */ 3654 if (DO_BIC(BIC_GFX_rc6)) { 3655 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 3656 outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 3657 } else { 3658 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->gfx_rc6_ms / 10.0, interval_float)); 3659 } 3660 } 3661 3662 /* GFXMHz */ 3663 if (DO_BIC(BIC_GFXMHz)) 3664 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 3665 3666 /* GFXACTMHz */ 3667 if (DO_BIC(BIC_GFXACTMHz)) 3668 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 3669 3670 /* SAMmc6 */ 3671 if (DO_BIC(BIC_SAM_mc6)) { 3672 if (p->sam_mc6_ms == -1) { /* detect GFX counter reset */ 3673 outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 3674 } else { 3675 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->sam_mc6_ms / 10.0, interval_float)); 3676 } 3677 } 3678 3679 /* SAMMHz */ 3680 if (DO_BIC(BIC_SAMMHz)) 3681 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->sam_mhz); 3682 3683 /* SAMACTMHz */ 3684 if (DO_BIC(BIC_SAMACTMHz)) 3685 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->sam_act_mhz); 3686 3687 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 3688 if (DO_BIC(BIC_Totl_c0)) 3689 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100 * p->pkg_wtd_core_c0 / tsc); /* can exceed 100% */ 3690 if (DO_BIC(BIC_Any_c0)) 3691 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_any_core_c0, tsc)); 3692 if (DO_BIC(BIC_GFX_c0)) 3693 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_any_gfxe_c0, tsc)); 3694 if (DO_BIC(BIC_CPUGFX)) 3695 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_both_core_gfxe_c0, tsc)); 3696 3697 if (DO_BIC(BIC_Pkgpc2)) 3698 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc2, tsc)); 3699 if (DO_BIC(BIC_Pkgpc3)) 3700 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc3, tsc)); 3701 if (DO_BIC(BIC_Pkgpc6)) 3702 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc6, tsc)); 3703 if (DO_BIC(BIC_Pkgpc7)) 3704 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc7, tsc)); 3705 if (DO_BIC(BIC_Pkgpc8)) 3706 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc8, tsc)); 3707 if (DO_BIC(BIC_Pkgpc9)) 3708 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc9, tsc)); 3709 if (DO_BIC(BIC_Pkgpc10)) 3710 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc10, tsc)); 3711 3712 if (DO_BIC(BIC_Diec6)) 3713 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->die_c6 / crystal_hz, interval_float)); 3714 3715 if (DO_BIC(BIC_CPU_LPI)) { 3716 if (p->cpu_lpi >= 0) 3717 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->cpu_lpi / 1000000.0, interval_float)); 3718 else 3719 outp += sprintf(outp, "%s(neg)", (printed++ ? delim : "")); 3720 } 3721 if (DO_BIC(BIC_SYS_LPI)) { 3722 if (p->sys_lpi >= 0) 3723 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->sys_lpi / 1000000.0, interval_float)); 3724 else 3725 outp += sprintf(outp, "%s(neg)", (printed++ ? delim : "")); 3726 } 3727 3728 if (DO_BIC(BIC_PkgWatt)) 3729 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_pkg, RAPL_UNIT_WATTS, interval_float)); 3730 if (DO_BIC(BIC_CorWatt) && !platform->has_per_core_rapl) 3731 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_cores, RAPL_UNIT_WATTS, interval_float)); 3732 if (DO_BIC(BIC_GFXWatt)) 3733 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_gfx, RAPL_UNIT_WATTS, interval_float)); 3734 if (DO_BIC(BIC_RAMWatt)) 3735 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_dram, RAPL_UNIT_WATTS, interval_float)); 3736 if (DO_BIC(BIC_Pkg_J)) 3737 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_pkg, RAPL_UNIT_JOULES, interval_float)); 3738 if (DO_BIC(BIC_Cor_J) && !platform->has_per_core_rapl) 3739 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_cores, RAPL_UNIT_JOULES, interval_float)); 3740 if (DO_BIC(BIC_GFX_J)) 3741 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_gfx, RAPL_UNIT_JOULES, interval_float)); 3742 if (DO_BIC(BIC_RAM_J)) 3743 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->energy_dram, RAPL_UNIT_JOULES, interval_float)); 3744 if (DO_BIC(BIC_PKG__)) 3745 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->rapl_pkg_perf_status, RAPL_UNIT_WATTS, interval_float)); 3746 if (DO_BIC(BIC_RAM__)) 3747 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&p->rapl_dram_perf_status, RAPL_UNIT_WATTS, interval_float)); 3748 /* UncMHz */ 3749 if (DO_BIC(BIC_UNCORE_MHZ)) 3750 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz); 3751 3752 /* Added Package Counters */ 3753 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 3754 if (mp->format == FORMAT_RAW) 3755 outp += print_hex_value(mp->width, &printed, delim, p->counter[i]); 3756 else if (mp->type == COUNTER_K2M) 3757 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), (unsigned int)p->counter[i] / 1000); 3758 else if (mp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE) 3759 outp += print_decimal_value(mp->width, &printed, delim, p->counter[i]); 3760 else if (mp->format == FORMAT_PERCENT) 3761 outp += print_float_value(&printed, delim, pct(p->counter[i], tsc)); 3762 } 3763 3764 /* Added perf Package Counters */ 3765 for (i = 0, pp = sys.perf_pp; pp; i++, pp = pp->next) { 3766 if (pp->format == FORMAT_RAW) 3767 outp += print_hex_value(pp->width, &printed, delim, p->perf_counter[i]); 3768 else if (pp->type == COUNTER_K2M) 3769 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), (unsigned int)p->perf_counter[i] / 1000); 3770 else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE) 3771 outp += print_decimal_value(pp->width, &printed, delim, p->perf_counter[i]); 3772 else if (pp->format == FORMAT_PERCENT) 3773 outp += print_float_value(&printed, delim, pct(p->perf_counter[i], tsc)); 3774 } 3775 3776 /* Added PMT Package Counters */ 3777 for (i = 0, ppmt = sys.pmt_pp; ppmt; i++, ppmt = ppmt->next) { 3778 const unsigned long value_raw = p->pmt_counter[i]; 3779 double value_converted; 3780 switch (ppmt->type) { 3781 case PMT_TYPE_RAW: 3782 outp += print_hex_value(pmt_counter_get_width(ppmt), &printed, delim, p->pmt_counter[i]); 3783 break; 3784 3785 case PMT_TYPE_XTAL_TIME: 3786 value_converted = pct(value_raw / crystal_hz, interval_float); 3787 outp += print_float_value(&printed, delim, value_converted); 3788 break; 3789 3790 case PMT_TYPE_TCORE_CLOCK: 3791 value_converted = pct(value_raw / tcore_clock_freq_hz, interval_float); 3792 outp += print_float_value(&printed, delim, value_converted); 3793 } 3794 } 3795 3796 if (DO_BIC(BIC_SysWatt) && (t == average.threads)) 3797 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&pplat_cnt->energy_psys, RAPL_UNIT_WATTS, interval_float)); 3798 if (DO_BIC(BIC_Sys_J) && (t == average.threads)) 3799 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), rapl_counter_get_value(&pplat_cnt->energy_psys, RAPL_UNIT_JOULES, interval_float)); 3800 3801 done: 3802 if (*(outp - 1) != '\n') 3803 outp += sprintf(outp, "\n"); 3804 3805 return 0; 3806 } 3807 3808 void flush_output_stdout(void) 3809 { 3810 FILE *filep; 3811 3812 if (outf == stderr) 3813 filep = stdout; 3814 else 3815 filep = outf; 3816 3817 fputs(output_buffer, filep); 3818 fflush(filep); 3819 3820 outp = output_buffer; 3821 } 3822 3823 void flush_output_stderr(void) 3824 { 3825 fputs(output_buffer, outf); 3826 fflush(outf); 3827 outp = output_buffer; 3828 } 3829 3830 void format_all_counters(PER_THREAD_PARAMS) 3831 { 3832 static int count; 3833 3834 if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only) 3835 print_header("\t"); 3836 3837 format_counters(average.threads, average.cores, average.packages); 3838 3839 count++; 3840 3841 if (summary_only) 3842 return; 3843 3844 for_all_cpus(format_counters, t, c, p); 3845 } 3846 3847 #define DELTA_WRAP32(new, old) \ 3848 old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 3849 3850 int delta_package(struct pkg_data *new, struct pkg_data *old) 3851 { 3852 int i; 3853 struct msr_counter *mp; 3854 struct perf_counter_info *pp; 3855 struct pmt_counter *ppmt; 3856 3857 if (DO_BIC(BIC_Totl_c0)) 3858 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 3859 if (DO_BIC(BIC_Any_c0)) 3860 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 3861 if (DO_BIC(BIC_GFX_c0)) 3862 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 3863 if (DO_BIC(BIC_CPUGFX)) 3864 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 3865 3866 old->pc2 = new->pc2 - old->pc2; 3867 if (DO_BIC(BIC_Pkgpc3)) 3868 old->pc3 = new->pc3 - old->pc3; 3869 if (DO_BIC(BIC_Pkgpc6)) 3870 old->pc6 = new->pc6 - old->pc6; 3871 if (DO_BIC(BIC_Pkgpc7)) 3872 old->pc7 = new->pc7 - old->pc7; 3873 old->pc8 = new->pc8 - old->pc8; 3874 old->pc9 = new->pc9 - old->pc9; 3875 old->pc10 = new->pc10 - old->pc10; 3876 old->die_c6 = new->die_c6 - old->die_c6; 3877 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 3878 old->sys_lpi = new->sys_lpi - old->sys_lpi; 3879 old->pkg_temp_c = new->pkg_temp_c; 3880 3881 /* flag an error when rc6 counter resets/wraps */ 3882 if (old->gfx_rc6_ms > new->gfx_rc6_ms) 3883 old->gfx_rc6_ms = -1; 3884 else 3885 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 3886 3887 old->uncore_mhz = new->uncore_mhz; 3888 old->gfx_mhz = new->gfx_mhz; 3889 old->gfx_act_mhz = new->gfx_act_mhz; 3890 3891 /* flag an error when mc6 counter resets/wraps */ 3892 if (old->sam_mc6_ms > new->sam_mc6_ms) 3893 old->sam_mc6_ms = -1; 3894 else 3895 old->sam_mc6_ms = new->sam_mc6_ms - old->sam_mc6_ms; 3896 3897 old->sam_mhz = new->sam_mhz; 3898 old->sam_act_mhz = new->sam_act_mhz; 3899 3900 old->energy_pkg.raw_value = new->energy_pkg.raw_value - old->energy_pkg.raw_value; 3901 old->energy_cores.raw_value = new->energy_cores.raw_value - old->energy_cores.raw_value; 3902 old->energy_gfx.raw_value = new->energy_gfx.raw_value - old->energy_gfx.raw_value; 3903 old->energy_dram.raw_value = new->energy_dram.raw_value - old->energy_dram.raw_value; 3904 old->rapl_pkg_perf_status.raw_value = new->rapl_pkg_perf_status.raw_value - old->rapl_pkg_perf_status.raw_value; 3905 old->rapl_dram_perf_status.raw_value = new->rapl_dram_perf_status.raw_value - old->rapl_dram_perf_status.raw_value; 3906 3907 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 3908 if (mp->format == FORMAT_RAW) 3909 old->counter[i] = new->counter[i]; 3910 else if (mp->format == FORMAT_AVERAGE) 3911 old->counter[i] = new->counter[i]; 3912 else 3913 old->counter[i] = new->counter[i] - old->counter[i]; 3914 } 3915 3916 for (i = 0, pp = sys.perf_pp; pp; i++, pp = pp->next) { 3917 if (pp->format == FORMAT_RAW) 3918 old->perf_counter[i] = new->perf_counter[i]; 3919 else if (pp->format == FORMAT_AVERAGE) 3920 old->perf_counter[i] = new->perf_counter[i]; 3921 else 3922 old->perf_counter[i] = new->perf_counter[i] - old->perf_counter[i]; 3923 } 3924 3925 for (i = 0, ppmt = sys.pmt_pp; ppmt; i++, ppmt = ppmt->next) { 3926 if (ppmt->format == FORMAT_RAW) 3927 old->pmt_counter[i] = new->pmt_counter[i]; 3928 else 3929 old->pmt_counter[i] = new->pmt_counter[i] - old->pmt_counter[i]; 3930 } 3931 3932 return 0; 3933 } 3934 3935 void delta_core(struct core_data *new, struct core_data *old) 3936 { 3937 int i; 3938 struct msr_counter *mp; 3939 struct perf_counter_info *pp; 3940 struct pmt_counter *ppmt; 3941 3942 old->c3 = new->c3 - old->c3; 3943 old->c6 = new->c6 - old->c6; 3944 old->c7 = new->c7 - old->c7; 3945 old->core_temp_c = new->core_temp_c; 3946 old->core_throt_cnt = new->core_throt_cnt - old->core_throt_cnt; 3947 old->mc6_us = new->mc6_us - old->mc6_us; 3948 3949 DELTA_WRAP32(new->core_energy.raw_value, old->core_energy.raw_value); 3950 3951 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 3952 if (mp->format == FORMAT_RAW || mp->format == FORMAT_AVERAGE) 3953 old->counter[i] = new->counter[i]; 3954 else 3955 old->counter[i] = new->counter[i] - old->counter[i]; 3956 } 3957 3958 for (i = 0, pp = sys.perf_cp; pp; i++, pp = pp->next) { 3959 if (pp->format == FORMAT_RAW) 3960 old->perf_counter[i] = new->perf_counter[i]; 3961 else 3962 old->perf_counter[i] = new->perf_counter[i] - old->perf_counter[i]; 3963 } 3964 3965 for (i = 0, ppmt = sys.pmt_cp; ppmt; i++, ppmt = ppmt->next) { 3966 if (ppmt->format == FORMAT_RAW) 3967 old->pmt_counter[i] = new->pmt_counter[i]; 3968 else 3969 old->pmt_counter[i] = new->pmt_counter[i] - old->pmt_counter[i]; 3970 } 3971 } 3972 3973 int soft_c1_residency_display(int bic) 3974 { 3975 if (!DO_BIC(BIC_CPU_c1) || platform->has_msr_core_c1_res) 3976 return 0; 3977 3978 return DO_BIC_READ(bic); 3979 } 3980 3981 /* 3982 * old = new - old 3983 */ 3984 int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 3985 { 3986 int i; 3987 struct msr_counter *mp; 3988 struct perf_counter_info *pp; 3989 struct pmt_counter *ppmt; 3990 3991 /* we run cpuid just the 1st time, copy the results */ 3992 if (DO_BIC(BIC_APIC)) 3993 new->apic_id = old->apic_id; 3994 if (DO_BIC(BIC_X2APIC)) 3995 new->x2apic_id = old->x2apic_id; 3996 3997 /* 3998 * the timestamps from start of measurement interval are in "old" 3999 * the timestamp from end of measurement interval are in "new" 4000 * over-write old w/ new so we can print end of interval values 4001 */ 4002 4003 timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 4004 old->tv_begin = new->tv_begin; 4005 old->tv_end = new->tv_end; 4006 4007 old->tsc = new->tsc - old->tsc; 4008 4009 /* check for TSC < 1 Mcycles over interval */ 4010 if (old->tsc < (1000 * 1000)) 4011 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" "You can disable all c-states by booting with \"idle=poll\"\nor just the deep ones with \"processor.max_cstate=1\""); 4012 4013 old->c1 = new->c1 - old->c1; 4014 4015 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || DO_BIC(BIC_IPC) 4016 || soft_c1_residency_display(BIC_Avg_MHz)) { 4017 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 4018 old->aperf = new->aperf - old->aperf; 4019 old->mperf = new->mperf - old->mperf; 4020 } else { 4021 return -1; 4022 } 4023 } 4024 4025 if (platform->has_msr_core_c1_res) { 4026 /* 4027 * Some models have a dedicated C1 residency MSR, 4028 * which should be more accurate than the derivation below. 4029 */ 4030 } else { 4031 /* 4032 * As counter collection is not atomic, 4033 * it is possible for mperf's non-halted cycles + idle states 4034 * to exceed TSC's all cycles: show c1 = 0% in that case. 4035 */ 4036 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 4037 old->c1 = 0; 4038 else { 4039 /* normal case, derive c1 */ 4040 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 - core_delta->c6 - core_delta->c7; 4041 } 4042 } 4043 4044 if (old->mperf == 0) { 4045 if (debug > 1) 4046 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 4047 old->mperf = 1; /* divide by 0 protection */ 4048 } 4049 4050 if (DO_BIC(BIC_IPC)) 4051 old->instr_count = new->instr_count - old->instr_count; 4052 4053 if (DO_BIC(BIC_IRQ)) 4054 old->irq_count = new->irq_count - old->irq_count; 4055 4056 if (DO_BIC(BIC_NMI)) 4057 old->nmi_count = new->nmi_count - old->nmi_count; 4058 4059 if (DO_BIC(BIC_SMI)) 4060 old->smi_count = new->smi_count - old->smi_count; 4061 4062 if (DO_BIC(BIC_LLC_MRPS) || DO_BIC(BIC_LLC_HIT)) 4063 old->llc.references = new->llc.references - old->llc.references; 4064 4065 if (DO_BIC(BIC_LLC_HIT)) 4066 old->llc.misses = new->llc.misses - old->llc.misses; 4067 4068 if (DO_BIC(BIC_L2_MRPS) || DO_BIC(BIC_L2_HIT)) 4069 old->l2.references = new->l2.references - old->l2.references; 4070 4071 if (DO_BIC(BIC_L2_HIT)) 4072 old->l2.hits = new->l2.hits - old->l2.hits; 4073 4074 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 4075 if (mp->format == FORMAT_RAW || mp->format == FORMAT_AVERAGE) 4076 old->counter[i] = new->counter[i]; 4077 else 4078 old->counter[i] = new->counter[i] - old->counter[i]; 4079 } 4080 4081 for (i = 0, pp = sys.perf_tp; pp; i++, pp = pp->next) { 4082 if (pp->format == FORMAT_RAW) 4083 old->perf_counter[i] = new->perf_counter[i]; 4084 else 4085 old->perf_counter[i] = new->perf_counter[i] - old->perf_counter[i]; 4086 } 4087 4088 for (i = 0, ppmt = sys.pmt_tp; ppmt; i++, ppmt = ppmt->next) { 4089 if (ppmt->format == FORMAT_RAW) 4090 old->pmt_counter[i] = new->pmt_counter[i]; 4091 else 4092 old->pmt_counter[i] = new->pmt_counter[i] - old->pmt_counter[i]; 4093 } 4094 4095 return 0; 4096 } 4097 4098 int delta_cpu(struct thread_data *t, struct core_data *c, struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 4099 { 4100 int retval = 0; 4101 4102 /* calculate core delta only for 1st thread in core */ 4103 if (is_cpu_first_thread_in_core(t, c)) 4104 delta_core(c, c2); 4105 4106 /* always calculate thread delta */ 4107 retval = delta_thread(t, t2, c2); /* c2 is core delta */ 4108 4109 /* calculate package delta only for 1st core in package */ 4110 if (is_cpu_first_core_in_package(t, p)) 4111 retval |= delta_package(p, p2); 4112 4113 return retval; 4114 } 4115 4116 void delta_platform(struct platform_counters *new, struct platform_counters *old) 4117 { 4118 old->energy_psys.raw_value = new->energy_psys.raw_value - old->energy_psys.raw_value; 4119 } 4120 4121 void rapl_counter_clear(struct rapl_counter *c) 4122 { 4123 c->raw_value = 0; 4124 c->scale = 0.0; 4125 c->unit = RAPL_UNIT_INVALID; 4126 } 4127 4128 void clear_counters(PER_THREAD_PARAMS) 4129 { 4130 int i; 4131 struct msr_counter *mp; 4132 4133 t->tv_begin.tv_sec = 0; 4134 t->tv_begin.tv_usec = 0; 4135 t->tv_end.tv_sec = 0; 4136 t->tv_end.tv_usec = 0; 4137 t->tv_delta.tv_sec = 0; 4138 t->tv_delta.tv_usec = 0; 4139 4140 t->tsc = 0; 4141 t->aperf = 0; 4142 t->mperf = 0; 4143 t->c1 = 0; 4144 4145 t->instr_count = 0; 4146 4147 t->irq_count = 0; 4148 t->nmi_count = 0; 4149 t->smi_count = 0; 4150 4151 t->llc.references = 0; 4152 t->llc.misses = 0; 4153 4154 t->l2.references = 0; 4155 t->l2.hits = 0; 4156 4157 c->c3 = 0; 4158 c->c6 = 0; 4159 c->c7 = 0; 4160 c->mc6_us = 0; 4161 c->core_temp_c = 0; 4162 rapl_counter_clear(&c->core_energy); 4163 c->core_throt_cnt = 0; 4164 4165 p->pkg_wtd_core_c0 = 0; 4166 p->pkg_any_core_c0 = 0; 4167 p->pkg_any_gfxe_c0 = 0; 4168 p->pkg_both_core_gfxe_c0 = 0; 4169 4170 p->pc2 = 0; 4171 if (DO_BIC(BIC_Pkgpc3)) 4172 p->pc3 = 0; 4173 if (DO_BIC(BIC_Pkgpc6)) 4174 p->pc6 = 0; 4175 if (DO_BIC(BIC_Pkgpc7)) 4176 p->pc7 = 0; 4177 p->pc8 = 0; 4178 p->pc9 = 0; 4179 p->pc10 = 0; 4180 p->die_c6 = 0; 4181 p->cpu_lpi = 0; 4182 p->sys_lpi = 0; 4183 4184 rapl_counter_clear(&p->energy_pkg); 4185 rapl_counter_clear(&p->energy_dram); 4186 rapl_counter_clear(&p->energy_cores); 4187 rapl_counter_clear(&p->energy_gfx); 4188 rapl_counter_clear(&p->rapl_pkg_perf_status); 4189 rapl_counter_clear(&p->rapl_dram_perf_status); 4190 p->pkg_temp_c = 0; 4191 4192 p->gfx_rc6_ms = 0; 4193 p->uncore_mhz = 0; 4194 p->gfx_mhz = 0; 4195 p->gfx_act_mhz = 0; 4196 p->sam_mc6_ms = 0; 4197 p->sam_mhz = 0; 4198 p->sam_act_mhz = 0; 4199 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 4200 t->counter[i] = 0; 4201 4202 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 4203 c->counter[i] = 0; 4204 4205 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 4206 p->counter[i] = 0; 4207 4208 memset(&t->perf_counter[0], 0, sizeof(t->perf_counter)); 4209 memset(&c->perf_counter[0], 0, sizeof(c->perf_counter)); 4210 memset(&p->perf_counter[0], 0, sizeof(p->perf_counter)); 4211 4212 memset(&t->pmt_counter[0], 0, ARRAY_SIZE(t->pmt_counter)); 4213 memset(&c->pmt_counter[0], 0, ARRAY_SIZE(c->pmt_counter)); 4214 memset(&p->pmt_counter[0], 0, ARRAY_SIZE(p->pmt_counter)); 4215 } 4216 4217 void rapl_counter_accumulate(struct rapl_counter *dst, const struct rapl_counter *src) 4218 { 4219 /* Copy unit and scale from src if dst is not initialized */ 4220 if (dst->unit == RAPL_UNIT_INVALID) { 4221 dst->unit = src->unit; 4222 dst->scale = src->scale; 4223 } 4224 4225 assert(dst->unit == src->unit); 4226 assert(dst->scale == src->scale); 4227 4228 dst->raw_value += src->raw_value; 4229 } 4230 4231 int sum_counters(PER_THREAD_PARAMS) 4232 { 4233 int i; 4234 struct msr_counter *mp; 4235 struct perf_counter_info *pp; 4236 struct pmt_counter *ppmt; 4237 4238 /* copy un-changing apic_id's */ 4239 if (DO_BIC(BIC_APIC)) 4240 average.threads->apic_id = t->apic_id; 4241 if (DO_BIC(BIC_X2APIC)) 4242 average.threads->x2apic_id = t->x2apic_id; 4243 4244 /* remember first tv_begin */ 4245 if (average.threads->tv_begin.tv_sec == 0) 4246 average.threads->tv_begin = procsysfs_tv_begin; 4247 4248 /* remember last tv_end */ 4249 average.threads->tv_end = t->tv_end; 4250 4251 average.threads->tsc += t->tsc; 4252 average.threads->aperf += t->aperf; 4253 average.threads->mperf += t->mperf; 4254 average.threads->c1 += t->c1; 4255 4256 average.threads->instr_count += t->instr_count; 4257 4258 average.threads->irq_count += t->irq_count; 4259 average.threads->nmi_count += t->nmi_count; 4260 average.threads->smi_count += t->smi_count; 4261 4262 average.threads->llc.references += t->llc.references; 4263 average.threads->llc.misses += t->llc.misses; 4264 4265 average.threads->l2.references += t->l2.references; 4266 average.threads->l2.hits += t->l2.hits; 4267 4268 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 4269 if (mp->format == FORMAT_RAW) 4270 continue; 4271 average.threads->counter[i] += t->counter[i]; 4272 } 4273 4274 for (i = 0, pp = sys.perf_tp; pp; i++, pp = pp->next) { 4275 if (pp->format == FORMAT_RAW) 4276 continue; 4277 average.threads->perf_counter[i] += t->perf_counter[i]; 4278 } 4279 4280 for (i = 0, ppmt = sys.pmt_tp; ppmt; i++, ppmt = ppmt->next) { 4281 average.threads->pmt_counter[i] += t->pmt_counter[i]; 4282 } 4283 4284 /* sum per-core values only for 1st thread in core */ 4285 if (!is_cpu_first_thread_in_core(t, c)) 4286 return 0; 4287 4288 average.cores->c3 += c->c3; 4289 average.cores->c6 += c->c6; 4290 average.cores->c7 += c->c7; 4291 average.cores->mc6_us += c->mc6_us; 4292 4293 average.cores->core_temp_c = MAX(average.cores->core_temp_c, c->core_temp_c); 4294 average.cores->core_throt_cnt = MAX(average.cores->core_throt_cnt, c->core_throt_cnt); 4295 4296 rapl_counter_accumulate(&average.cores->core_energy, &c->core_energy); 4297 4298 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 4299 if (mp->format == FORMAT_RAW) 4300 continue; 4301 average.cores->counter[i] += c->counter[i]; 4302 } 4303 4304 for (i = 0, pp = sys.perf_cp; pp; i++, pp = pp->next) { 4305 if (pp->format == FORMAT_RAW) 4306 continue; 4307 average.cores->perf_counter[i] += c->perf_counter[i]; 4308 } 4309 4310 for (i = 0, ppmt = sys.pmt_cp; ppmt; i++, ppmt = ppmt->next) { 4311 average.cores->pmt_counter[i] += c->pmt_counter[i]; 4312 } 4313 4314 /* sum per-pkg values only for 1st core in pkg */ 4315 if (!is_cpu_first_core_in_package(t, p)) 4316 return 0; 4317 4318 if (DO_BIC(BIC_Totl_c0)) 4319 average.packages->pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 4320 if (DO_BIC(BIC_Any_c0)) 4321 average.packages->pkg_any_core_c0 += p->pkg_any_core_c0; 4322 if (DO_BIC(BIC_GFX_c0)) 4323 average.packages->pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 4324 if (DO_BIC(BIC_CPUGFX)) 4325 average.packages->pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 4326 4327 average.packages->pc2 += p->pc2; 4328 if (DO_BIC(BIC_Pkgpc3)) 4329 average.packages->pc3 += p->pc3; 4330 if (DO_BIC(BIC_Pkgpc6)) 4331 average.packages->pc6 += p->pc6; 4332 if (DO_BIC(BIC_Pkgpc7)) 4333 average.packages->pc7 += p->pc7; 4334 average.packages->pc8 += p->pc8; 4335 average.packages->pc9 += p->pc9; 4336 average.packages->pc10 += p->pc10; 4337 average.packages->die_c6 += p->die_c6; 4338 4339 average.packages->cpu_lpi = p->cpu_lpi; 4340 average.packages->sys_lpi = p->sys_lpi; 4341 4342 rapl_counter_accumulate(&average.packages->energy_pkg, &p->energy_pkg); 4343 rapl_counter_accumulate(&average.packages->energy_dram, &p->energy_dram); 4344 rapl_counter_accumulate(&average.packages->energy_cores, &p->energy_cores); 4345 rapl_counter_accumulate(&average.packages->energy_gfx, &p->energy_gfx); 4346 4347 average.packages->gfx_rc6_ms = p->gfx_rc6_ms; 4348 average.packages->uncore_mhz = p->uncore_mhz; 4349 average.packages->gfx_mhz = p->gfx_mhz; 4350 average.packages->gfx_act_mhz = p->gfx_act_mhz; 4351 average.packages->sam_mc6_ms = p->sam_mc6_ms; 4352 average.packages->sam_mhz = p->sam_mhz; 4353 average.packages->sam_act_mhz = p->sam_act_mhz; 4354 4355 average.packages->pkg_temp_c = MAX(average.packages->pkg_temp_c, p->pkg_temp_c); 4356 4357 rapl_counter_accumulate(&average.packages->rapl_pkg_perf_status, &p->rapl_pkg_perf_status); 4358 rapl_counter_accumulate(&average.packages->rapl_dram_perf_status, &p->rapl_dram_perf_status); 4359 4360 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 4361 if ((mp->format == FORMAT_RAW) && (topo.num_packages == 0)) 4362 average.packages->counter[i] = p->counter[i]; 4363 else 4364 average.packages->counter[i] += p->counter[i]; 4365 } 4366 4367 for (i = 0, pp = sys.perf_pp; pp; i++, pp = pp->next) { 4368 if ((pp->format == FORMAT_RAW) && (topo.num_packages == 0)) 4369 average.packages->perf_counter[i] = p->perf_counter[i]; 4370 else 4371 average.packages->perf_counter[i] += p->perf_counter[i]; 4372 } 4373 4374 for (i = 0, ppmt = sys.pmt_pp; ppmt; i++, ppmt = ppmt->next) { 4375 average.packages->pmt_counter[i] += p->pmt_counter[i]; 4376 } 4377 4378 return 0; 4379 } 4380 4381 /* 4382 * sum the counters for all cpus in the system 4383 * compute the weighted average 4384 */ 4385 void compute_average(PER_THREAD_PARAMS) 4386 { 4387 int i; 4388 struct msr_counter *mp; 4389 struct perf_counter_info *pp; 4390 struct pmt_counter *ppmt; 4391 4392 clear_counters(average.threads, average.cores, average.packages); 4393 4394 for_all_cpus(sum_counters, t, c, p); 4395 4396 /* Use the global time delta for the average. */ 4397 average.threads->tv_delta = tv_delta; 4398 4399 average.threads->tsc /= topo.allowed_cpus; 4400 average.threads->aperf /= topo.allowed_cpus; 4401 average.threads->mperf /= topo.allowed_cpus; 4402 average.threads->instr_count /= topo.allowed_cpus; 4403 average.threads->c1 /= topo.allowed_cpus; 4404 4405 if (average.threads->irq_count > 9999999) 4406 sums_need_wide_columns = 1; 4407 if (average.threads->nmi_count > 9999999) 4408 sums_need_wide_columns = 1; 4409 4410 average.cores->c3 /= topo.allowed_cores; 4411 average.cores->c6 /= topo.allowed_cores; 4412 average.cores->c7 /= topo.allowed_cores; 4413 average.cores->mc6_us /= topo.allowed_cores; 4414 4415 if (DO_BIC(BIC_Totl_c0)) 4416 average.packages->pkg_wtd_core_c0 /= topo.allowed_packages; 4417 if (DO_BIC(BIC_Any_c0)) 4418 average.packages->pkg_any_core_c0 /= topo.allowed_packages; 4419 if (DO_BIC(BIC_GFX_c0)) 4420 average.packages->pkg_any_gfxe_c0 /= topo.allowed_packages; 4421 if (DO_BIC(BIC_CPUGFX)) 4422 average.packages->pkg_both_core_gfxe_c0 /= topo.allowed_packages; 4423 4424 average.packages->pc2 /= topo.allowed_packages; 4425 if (DO_BIC(BIC_Pkgpc3)) 4426 average.packages->pc3 /= topo.allowed_packages; 4427 if (DO_BIC(BIC_Pkgpc6)) 4428 average.packages->pc6 /= topo.allowed_packages; 4429 if (DO_BIC(BIC_Pkgpc7)) 4430 average.packages->pc7 /= topo.allowed_packages; 4431 4432 average.packages->pc8 /= topo.allowed_packages; 4433 average.packages->pc9 /= topo.allowed_packages; 4434 average.packages->pc10 /= topo.allowed_packages; 4435 average.packages->die_c6 /= topo.allowed_packages; 4436 4437 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 4438 if (mp->format == FORMAT_RAW) 4439 continue; 4440 if (mp->type == COUNTER_ITEMS) { 4441 if (average.threads->counter[i] > 9999999) 4442 sums_need_wide_columns = 1; 4443 continue; 4444 } 4445 average.threads->counter[i] /= topo.allowed_cpus; 4446 } 4447 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 4448 if (mp->format == FORMAT_RAW) 4449 continue; 4450 if (mp->type == COUNTER_ITEMS) { 4451 if (average.cores->counter[i] > 9999999) 4452 sums_need_wide_columns = 1; 4453 } 4454 average.cores->counter[i] /= topo.allowed_cores; 4455 } 4456 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 4457 if (mp->format == FORMAT_RAW) 4458 continue; 4459 if (mp->type == COUNTER_ITEMS) { 4460 if (average.packages->counter[i] > 9999999) 4461 sums_need_wide_columns = 1; 4462 } 4463 average.packages->counter[i] /= topo.allowed_packages; 4464 } 4465 4466 for (i = 0, pp = sys.perf_tp; pp; i++, pp = pp->next) { 4467 if (pp->format == FORMAT_RAW) 4468 continue; 4469 if (pp->type == COUNTER_ITEMS) { 4470 if (average.threads->perf_counter[i] > 9999999) 4471 sums_need_wide_columns = 1; 4472 continue; 4473 } 4474 average.threads->perf_counter[i] /= topo.allowed_cpus; 4475 } 4476 for (i = 0, pp = sys.perf_cp; pp; i++, pp = pp->next) { 4477 if (pp->format == FORMAT_RAW) 4478 continue; 4479 if (pp->type == COUNTER_ITEMS) { 4480 if (average.cores->perf_counter[i] > 9999999) 4481 sums_need_wide_columns = 1; 4482 } 4483 average.cores->perf_counter[i] /= topo.allowed_cores; 4484 } 4485 for (i = 0, pp = sys.perf_pp; pp; i++, pp = pp->next) { 4486 if (pp->format == FORMAT_RAW) 4487 continue; 4488 if (pp->type == COUNTER_ITEMS) { 4489 if (average.packages->perf_counter[i] > 9999999) 4490 sums_need_wide_columns = 1; 4491 } 4492 average.packages->perf_counter[i] /= topo.allowed_packages; 4493 } 4494 4495 for (i = 0, ppmt = sys.pmt_tp; ppmt; i++, ppmt = ppmt->next) { 4496 average.threads->pmt_counter[i] /= topo.allowed_cpus; 4497 } 4498 for (i = 0, ppmt = sys.pmt_cp; ppmt; i++, ppmt = ppmt->next) { 4499 average.cores->pmt_counter[i] /= topo.allowed_cores; 4500 } 4501 for (i = 0, ppmt = sys.pmt_pp; ppmt; i++, ppmt = ppmt->next) { 4502 average.packages->pmt_counter[i] /= topo.allowed_packages; 4503 } 4504 } 4505 4506 static unsigned long long rdtsc(void) 4507 { 4508 unsigned int low, high; 4509 4510 asm volatile ("rdtsc":"=a" (low), "=d"(high)); 4511 4512 return low | ((unsigned long long)high) << 32; 4513 } 4514 4515 /* 4516 * Open a file, and exit on failure 4517 */ 4518 FILE *fopen_or_die(const char *path, const char *mode) 4519 { 4520 FILE *filep = fopen(path, mode); 4521 4522 if (!filep) 4523 err(1, "%s: open failed", path); 4524 return filep; 4525 } 4526 4527 /* 4528 * snapshot_sysfs_counter() 4529 * 4530 * return snapshot of given counter 4531 */ 4532 unsigned long long snapshot_sysfs_counter(char *path) 4533 { 4534 FILE *fp; 4535 int retval; 4536 unsigned long long counter; 4537 4538 fp = fopen_or_die(path, "r"); 4539 4540 retval = fscanf(fp, "%lld", &counter); 4541 if (retval != 1) 4542 err(1, "snapshot_sysfs_counter(%s)", path); 4543 4544 fclose(fp); 4545 4546 return counter; 4547 } 4548 4549 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp, char *counter_path) 4550 { 4551 if (mp->msr_num != 0) { 4552 assert(!no_msr); 4553 get_msr(cpu, mp->msr_num, counterp); 4554 } else { 4555 char path[128 + PATH_BYTES]; 4556 4557 if (mp->flags & SYSFS_PERCPU) { 4558 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->sp->path); 4559 4560 *counterp = snapshot_sysfs_counter(path); 4561 } else { 4562 *counterp = snapshot_sysfs_counter(counter_path); 4563 } 4564 } 4565 4566 return 0; 4567 } 4568 4569 unsigned long long get_legacy_uncore_mhz(int package) 4570 { 4571 char path[128]; 4572 int die; 4573 static int warn_once; 4574 4575 /* 4576 * for this package, use the first die_id that exists 4577 */ 4578 for (die = 0; die <= topo.max_die_id; ++die) { 4579 4580 sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d/current_freq_khz", package, die); 4581 4582 if (access(path, R_OK) == 0) 4583 return (snapshot_sysfs_counter(path) / 1000); 4584 } 4585 if (!warn_once) { 4586 warnx("BUG: %s: No %s", __func__, path); 4587 warn_once = 1; 4588 } 4589 4590 return 0; 4591 } 4592 4593 int get_epb(int cpu) 4594 { 4595 char path[128 + PATH_BYTES]; 4596 unsigned long long msr; 4597 int ret, epb = -1; 4598 FILE *fp; 4599 4600 sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 4601 4602 fp = fopen(path, "r"); 4603 if (!fp) 4604 goto msr_fallback; 4605 4606 ret = fscanf(fp, "%d", &epb); 4607 if (ret != 1) 4608 err(1, "%s(%s)", __func__, path); 4609 4610 fclose(fp); 4611 4612 return epb; 4613 4614 msr_fallback: 4615 if (no_msr) 4616 return -1; 4617 4618 get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 4619 4620 return msr & 0xf; 4621 } 4622 4623 void get_apic_id(struct thread_data *t) 4624 { 4625 unsigned int eax, ebx, ecx, edx; 4626 4627 if (DO_BIC(BIC_APIC)) { 4628 eax = ebx = ecx = edx = 0; 4629 __cpuid(1, eax, ebx, ecx, edx); 4630 4631 t->apic_id = (ebx >> 24) & 0xff; 4632 } 4633 4634 if (!DO_BIC(BIC_X2APIC)) 4635 return; 4636 4637 if (authentic_amd || hygon_genuine) { 4638 unsigned int topology_extensions; 4639 4640 if (max_extended_level < 0x8000001e) 4641 return; 4642 4643 eax = ebx = ecx = edx = 0; 4644 __cpuid(0x80000001, eax, ebx, ecx, edx); 4645 topology_extensions = ecx & (1 << 22); 4646 4647 if (topology_extensions == 0) 4648 return; 4649 4650 eax = ebx = ecx = edx = 0; 4651 __cpuid(0x8000001e, eax, ebx, ecx, edx); 4652 4653 t->x2apic_id = eax; 4654 return; 4655 } 4656 4657 if (!genuine_intel) 4658 return; 4659 4660 if (max_level < 0xb) 4661 return; 4662 4663 ecx = 0; 4664 __cpuid(0xb, eax, ebx, ecx, edx); 4665 t->x2apic_id = edx; 4666 4667 if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 4668 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 4669 } 4670 4671 int get_core_throt_cnt(int cpu, unsigned long long *cnt) 4672 { 4673 char path[128 + PATH_BYTES]; 4674 unsigned long long tmp; 4675 FILE *fp; 4676 int ret; 4677 4678 sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu); 4679 fp = fopen(path, "r"); 4680 if (!fp) 4681 return -1; 4682 ret = fscanf(fp, "%lld", &tmp); 4683 fclose(fp); 4684 if (ret != 1) 4685 return -1; 4686 *cnt = tmp; 4687 4688 return 0; 4689 } 4690 4691 static int read_perf_counter_info(const char *const path, const char *const parse_format, void *value_ptr) 4692 { 4693 int fdmt; 4694 int bytes_read; 4695 char buf[64]; 4696 int ret = -1; 4697 4698 fdmt = open(path, O_RDONLY, 0); 4699 if (fdmt == -1) { 4700 if (debug) 4701 fprintf(stderr, "Failed to parse perf counter info %s\n", path); 4702 ret = -1; 4703 goto cleanup_and_exit; 4704 } 4705 4706 bytes_read = read(fdmt, buf, sizeof(buf) - 1); 4707 if (bytes_read <= 0 || bytes_read >= (int)sizeof(buf)) { 4708 if (debug) 4709 fprintf(stderr, "Failed to parse perf counter info %s\n", path); 4710 ret = -1; 4711 goto cleanup_and_exit; 4712 } 4713 4714 buf[bytes_read] = '\0'; 4715 4716 if (sscanf(buf, parse_format, value_ptr) != 1) { 4717 if (debug) 4718 fprintf(stderr, "Failed to parse perf counter info %s\n", path); 4719 ret = -1; 4720 goto cleanup_and_exit; 4721 } 4722 4723 ret = 0; 4724 4725 cleanup_and_exit: 4726 close(fdmt); 4727 return ret; 4728 } 4729 4730 static unsigned int read_perf_counter_info_n(const char *const path, const char *const parse_format) 4731 { 4732 unsigned int v; 4733 int status; 4734 4735 status = read_perf_counter_info(path, parse_format, &v); 4736 if (status) 4737 v = -1; 4738 4739 return v; 4740 } 4741 4742 static unsigned int read_perf_type(const char *subsys) 4743 { 4744 const char *const path_format = "/sys/bus/event_source/devices/%s/type"; 4745 const char *const format = "%u"; 4746 char path[128]; 4747 4748 snprintf(path, sizeof(path), path_format, subsys); 4749 4750 return read_perf_counter_info_n(path, format); 4751 } 4752 4753 static unsigned int read_perf_config(const char *subsys, const char *event_name) 4754 { 4755 const char *const path_format = "/sys/bus/event_source/devices/%s/events/%s"; 4756 FILE *fconfig = NULL; 4757 char path[128]; 4758 char config_str[64]; 4759 unsigned int config; 4760 unsigned int umask; 4761 bool has_config = false; 4762 bool has_umask = false; 4763 unsigned int ret = -1; 4764 4765 snprintf(path, sizeof(path), path_format, subsys, event_name); 4766 4767 fconfig = fopen(path, "r"); 4768 if (!fconfig) 4769 return -1; 4770 4771 if (fgets(config_str, ARRAY_SIZE(config_str), fconfig) != config_str) 4772 goto cleanup_and_exit; 4773 4774 for (char *pconfig_str = &config_str[0]; pconfig_str;) { 4775 if (sscanf(pconfig_str, "event=%x", &config) == 1) { 4776 has_config = true; 4777 goto next; 4778 } 4779 4780 if (sscanf(pconfig_str, "umask=%x", &umask) == 1) { 4781 has_umask = true; 4782 goto next; 4783 } 4784 4785 next: 4786 pconfig_str = strchr(pconfig_str, ','); 4787 if (pconfig_str) { 4788 *pconfig_str = '\0'; 4789 ++pconfig_str; 4790 } 4791 } 4792 4793 if (!has_umask) 4794 umask = 0; 4795 4796 if (has_config) 4797 ret = (umask << 8) | config; 4798 4799 cleanup_and_exit: 4800 fclose(fconfig); 4801 return ret; 4802 } 4803 4804 static unsigned int read_perf_rapl_unit(const char *subsys, const char *event_name) 4805 { 4806 const char *const path_format = "/sys/bus/event_source/devices/%s/events/%s.unit"; 4807 const char *const format = "%s"; 4808 char path[128]; 4809 char unit_buffer[16]; 4810 4811 snprintf(path, sizeof(path), path_format, subsys, event_name); 4812 4813 read_perf_counter_info(path, format, &unit_buffer); 4814 if (strcmp("Joules", unit_buffer) == 0) 4815 return RAPL_UNIT_JOULES; 4816 4817 return RAPL_UNIT_INVALID; 4818 } 4819 4820 static double read_perf_scale(const char *subsys, const char *event_name) 4821 { 4822 const char *const path_format = "/sys/bus/event_source/devices/%s/events/%s.scale"; 4823 const char *const format = "%lf"; 4824 char path[128]; 4825 double scale; 4826 4827 snprintf(path, sizeof(path), path_format, subsys, event_name); 4828 4829 if (read_perf_counter_info(path, format, &scale)) 4830 return 0.0; 4831 4832 return scale; 4833 } 4834 4835 size_t rapl_counter_info_count_perf(const struct rapl_counter_info_t *rci) 4836 { 4837 size_t ret = 0; 4838 4839 for (int i = 0; i < NUM_RAPL_COUNTERS; ++i) 4840 if (rci->source[i] == COUNTER_SOURCE_PERF) 4841 ++ret; 4842 4843 return ret; 4844 } 4845 4846 static size_t cstate_counter_info_count_perf(const struct cstate_counter_info_t *cci) 4847 { 4848 size_t ret = 0; 4849 4850 for (int i = 0; i < NUM_CSTATE_COUNTERS; ++i) 4851 if (cci->source[i] == COUNTER_SOURCE_PERF) 4852 ++ret; 4853 4854 return ret; 4855 } 4856 4857 void write_rapl_counter(struct rapl_counter *rc, struct rapl_counter_info_t *rci, unsigned int idx) 4858 { 4859 if (rci->source[idx] == COUNTER_SOURCE_NONE) 4860 return; 4861 4862 rc->raw_value = rci->data[idx]; 4863 rc->unit = rci->unit[idx]; 4864 rc->scale = rci->scale[idx]; 4865 } 4866 4867 int get_rapl_counters(int cpu, unsigned int domain, struct core_data *c, struct pkg_data *p) 4868 { 4869 struct platform_counters *pplat_cnt = p == odd.packages ? &platform_counters_odd : &platform_counters_even; 4870 unsigned long long perf_data[NUM_RAPL_COUNTERS + 1]; 4871 struct rapl_counter_info_t *rci; 4872 4873 if (debug >= 2) 4874 fprintf(stderr, "%s: cpu%d domain%d\n", __func__, cpu, domain); 4875 4876 assert(rapl_counter_info_perdomain); 4877 assert(domain < rapl_counter_info_perdomain_size); 4878 4879 rci = &rapl_counter_info_perdomain[domain]; 4880 4881 /* 4882 * If we have any perf counters to read, read them all now, in bulk 4883 */ 4884 if (rci->fd_perf != -1) { 4885 size_t num_perf_counters = rapl_counter_info_count_perf(rci); 4886 const ssize_t expected_read_size = (num_perf_counters + 1) * sizeof(unsigned long long); 4887 const ssize_t actual_read_size = read(rci->fd_perf, &perf_data[0], sizeof(perf_data)); 4888 4889 if (actual_read_size != expected_read_size) 4890 err(-1, "%s: failed to read perf_data (%zu %zu)", __func__, expected_read_size, actual_read_size); 4891 } 4892 4893 for (unsigned int i = 0, pi = 1; i < NUM_RAPL_COUNTERS; ++i) { 4894 switch (rci->source[i]) { 4895 case COUNTER_SOURCE_NONE: 4896 rci->data[i] = 0; 4897 break; 4898 4899 case COUNTER_SOURCE_PERF: 4900 assert(pi < ARRAY_SIZE(perf_data)); 4901 assert(rci->fd_perf != -1); 4902 4903 if (debug >= 2) 4904 fprintf(stderr, "Reading rapl counter via perf at %u (%llu %e %lf)\n", i, perf_data[pi], rci->scale[i], perf_data[pi] * rci->scale[i]); 4905 4906 rci->data[i] = perf_data[pi]; 4907 4908 ++pi; 4909 break; 4910 4911 case COUNTER_SOURCE_MSR: 4912 if (debug >= 2) 4913 fprintf(stderr, "Reading rapl counter via msr at %u\n", i); 4914 4915 assert(!no_msr); 4916 if (rci->flags[i] & RAPL_COUNTER_FLAG_USE_MSR_SUM) 4917 get_msr_sum(cpu, rci->msr[i], &rci->data[i]); 4918 else 4919 get_msr(cpu, rci->msr[i], &rci->data[i]); 4920 4921 rci->data[i] &= rci->msr_mask[i]; 4922 if (rci->msr_shift[i] >= 0) 4923 rci->data[i] >>= abs(rci->msr_shift[i]); 4924 else 4925 rci->data[i] <<= abs(rci->msr_shift[i]); 4926 4927 break; 4928 } 4929 } 4930 4931 BUILD_BUG_ON(NUM_RAPL_COUNTERS != 8); 4932 write_rapl_counter(&p->energy_pkg, rci, RAPL_RCI_INDEX_ENERGY_PKG); 4933 write_rapl_counter(&p->energy_cores, rci, RAPL_RCI_INDEX_ENERGY_CORES); 4934 write_rapl_counter(&p->energy_dram, rci, RAPL_RCI_INDEX_DRAM); 4935 write_rapl_counter(&p->energy_gfx, rci, RAPL_RCI_INDEX_GFX); 4936 write_rapl_counter(&p->rapl_pkg_perf_status, rci, RAPL_RCI_INDEX_PKG_PERF_STATUS); 4937 write_rapl_counter(&p->rapl_dram_perf_status, rci, RAPL_RCI_INDEX_DRAM_PERF_STATUS); 4938 write_rapl_counter(&c->core_energy, rci, RAPL_RCI_INDEX_CORE_ENERGY); 4939 write_rapl_counter(&pplat_cnt->energy_psys, rci, RAPL_RCI_INDEX_ENERGY_PLATFORM); 4940 4941 return 0; 4942 } 4943 4944 char *find_sysfs_path_by_id(struct sysfs_path *sp, int id) 4945 { 4946 while (sp) { 4947 if (sp->id == id) 4948 return (sp->path); 4949 sp = sp->next; 4950 } 4951 if (debug) 4952 warnx("%s: id%d not found", __func__, id); 4953 return NULL; 4954 } 4955 4956 int get_cstate_counters(unsigned int cpu, PER_THREAD_PARAMS) 4957 { 4958 /* 4959 * Overcommit memory a little bit here, 4960 * but skip calculating exact sizes for the buffers. 4961 */ 4962 unsigned long long perf_data[NUM_CSTATE_COUNTERS]; 4963 unsigned long long perf_data_core[NUM_CSTATE_COUNTERS + 1]; 4964 unsigned long long perf_data_pkg[NUM_CSTATE_COUNTERS + 1]; 4965 4966 struct cstate_counter_info_t *cci; 4967 4968 if (debug >= 2) 4969 fprintf(stderr, "%s: cpu%d\n", __func__, cpu); 4970 4971 assert(ccstate_counter_info); 4972 assert(cpu <= ccstate_counter_info_size); 4973 4974 ZERO_ARRAY(perf_data); 4975 ZERO_ARRAY(perf_data_core); 4976 ZERO_ARRAY(perf_data_pkg); 4977 4978 cci = &ccstate_counter_info[cpu]; 4979 4980 /* 4981 * If we have any perf counters to read, read them all now, in bulk 4982 */ 4983 const size_t num_perf_counters = cstate_counter_info_count_perf(cci); 4984 ssize_t expected_read_size = num_perf_counters * sizeof(unsigned long long); 4985 ssize_t actual_read_size_core = 0, actual_read_size_pkg = 0; 4986 4987 if (cci->fd_perf_core != -1) { 4988 /* Each descriptor read begins with number of counters read. */ 4989 expected_read_size += sizeof(unsigned long long); 4990 4991 actual_read_size_core = read(cci->fd_perf_core, &perf_data_core[0], sizeof(perf_data_core)); 4992 4993 if (actual_read_size_core <= 0) 4994 err(-1, "%s: read perf %s: %ld", __func__, "core", actual_read_size_core); 4995 } 4996 4997 if (cci->fd_perf_pkg != -1) { 4998 /* Each descriptor read begins with number of counters read. */ 4999 expected_read_size += sizeof(unsigned long long); 5000 5001 actual_read_size_pkg = read(cci->fd_perf_pkg, &perf_data_pkg[0], sizeof(perf_data_pkg)); 5002 5003 if (actual_read_size_pkg <= 0) 5004 err(-1, "%s: read perf %s: %ld", __func__, "pkg", actual_read_size_pkg); 5005 } 5006 5007 const ssize_t actual_read_size_total = actual_read_size_core + actual_read_size_pkg; 5008 5009 if (actual_read_size_total != expected_read_size) 5010 err(-1, "%s: failed to read perf_data (%zu %zu)", __func__, expected_read_size, actual_read_size_total); 5011 5012 /* 5013 * Copy ccstate and pcstate data into unified buffer. 5014 * 5015 * Skip first element from core and pkg buffers. 5016 * Kernel puts there how many counters were read. 5017 */ 5018 const size_t num_core_counters = perf_data_core[0]; 5019 const size_t num_pkg_counters = perf_data_pkg[0]; 5020 5021 assert(num_perf_counters == num_core_counters + num_pkg_counters); 5022 5023 /* Copy ccstate perf data */ 5024 memcpy(&perf_data[0], &perf_data_core[1], num_core_counters * sizeof(unsigned long long)); 5025 5026 /* Copy pcstate perf data */ 5027 memcpy(&perf_data[num_core_counters], &perf_data_pkg[1], num_pkg_counters * sizeof(unsigned long long)); 5028 5029 for (unsigned int i = 0, pi = 0; i < NUM_CSTATE_COUNTERS; ++i) { 5030 switch (cci->source[i]) { 5031 case COUNTER_SOURCE_NONE: 5032 break; 5033 5034 case COUNTER_SOURCE_PERF: 5035 assert(pi < ARRAY_SIZE(perf_data)); 5036 assert(cci->fd_perf_core != -1 || cci->fd_perf_pkg != -1); 5037 5038 if (debug >= 2) 5039 fprintf(stderr, "cstate via %s %u: %llu\n", "perf", i, perf_data[pi]); 5040 5041 cci->data[i] = perf_data[pi]; 5042 5043 ++pi; 5044 break; 5045 5046 case COUNTER_SOURCE_MSR: 5047 assert(!no_msr); 5048 get_msr(cpu, cci->msr[i], &cci->data[i]); 5049 5050 if (debug >= 2) 5051 fprintf(stderr, "cstate via %s0x%llx %u: %llu\n", "msr", cci->msr[i], i, cci->data[i]); 5052 5053 break; 5054 } 5055 } 5056 5057 /* 5058 * Helper to write the data only if the source of 5059 * the counter for the current cpu is not none. 5060 * 5061 * Otherwise we would overwrite core data with 0 (default value), 5062 * when invoked for the thread sibling. 5063 */ 5064 #define PERF_COUNTER_WRITE_DATA(out_counter, index) do { \ 5065 if (cci->source[index] != COUNTER_SOURCE_NONE) \ 5066 out_counter = cci->data[index]; \ 5067 } while (0) 5068 5069 BUILD_BUG_ON(NUM_CSTATE_COUNTERS != 11); 5070 5071 PERF_COUNTER_WRITE_DATA(t->c1, CCSTATE_RCI_INDEX_C1_RESIDENCY); 5072 PERF_COUNTER_WRITE_DATA(c->c3, CCSTATE_RCI_INDEX_C3_RESIDENCY); 5073 PERF_COUNTER_WRITE_DATA(c->c6, CCSTATE_RCI_INDEX_C6_RESIDENCY); 5074 PERF_COUNTER_WRITE_DATA(c->c7, CCSTATE_RCI_INDEX_C7_RESIDENCY); 5075 5076 PERF_COUNTER_WRITE_DATA(p->pc2, PCSTATE_RCI_INDEX_C2_RESIDENCY); 5077 PERF_COUNTER_WRITE_DATA(p->pc3, PCSTATE_RCI_INDEX_C3_RESIDENCY); 5078 PERF_COUNTER_WRITE_DATA(p->pc6, PCSTATE_RCI_INDEX_C6_RESIDENCY); 5079 PERF_COUNTER_WRITE_DATA(p->pc7, PCSTATE_RCI_INDEX_C7_RESIDENCY); 5080 PERF_COUNTER_WRITE_DATA(p->pc8, PCSTATE_RCI_INDEX_C8_RESIDENCY); 5081 PERF_COUNTER_WRITE_DATA(p->pc9, PCSTATE_RCI_INDEX_C9_RESIDENCY); 5082 PERF_COUNTER_WRITE_DATA(p->pc10, PCSTATE_RCI_INDEX_C10_RESIDENCY); 5083 5084 #undef PERF_COUNTER_WRITE_DATA 5085 5086 return 0; 5087 } 5088 5089 size_t msr_counter_info_count_perf(const struct msr_counter_info_t *mci) 5090 { 5091 size_t ret = 0; 5092 5093 for (int i = 0; i < NUM_MSR_COUNTERS; ++i) 5094 if (mci->source[i] == COUNTER_SOURCE_PERF) 5095 ++ret; 5096 5097 return ret; 5098 } 5099 5100 int get_smi_aperf_mperf(unsigned int cpu, struct thread_data *t) 5101 { 5102 unsigned long long perf_data[NUM_MSR_COUNTERS + 1]; 5103 5104 struct msr_counter_info_t *mci; 5105 5106 if (debug >= 2) 5107 fprintf(stderr, "%s: cpu%d\n", __func__, cpu); 5108 5109 assert(msr_counter_info); 5110 assert(cpu <= msr_counter_info_size); 5111 5112 mci = &msr_counter_info[cpu]; 5113 5114 ZERO_ARRAY(perf_data); 5115 ZERO_ARRAY(mci->data); 5116 5117 if (mci->fd_perf != -1) { 5118 const size_t num_perf_counters = msr_counter_info_count_perf(mci); 5119 const ssize_t expected_read_size = (num_perf_counters + 1) * sizeof(unsigned long long); 5120 const ssize_t actual_read_size = read(mci->fd_perf, &perf_data[0], sizeof(perf_data)); 5121 5122 if (actual_read_size != expected_read_size) 5123 err(-1, "%s: failed to read perf_data (%zu %zu)", __func__, expected_read_size, actual_read_size); 5124 } 5125 5126 for (unsigned int i = 0, pi = 1; i < NUM_MSR_COUNTERS; ++i) { 5127 switch (mci->source[i]) { 5128 case COUNTER_SOURCE_NONE: 5129 break; 5130 5131 case COUNTER_SOURCE_PERF: 5132 assert(pi < ARRAY_SIZE(perf_data)); 5133 assert(mci->fd_perf != -1); 5134 5135 if (debug >= 2) 5136 fprintf(stderr, "Reading msr counter via perf at %u: %llu\n", i, perf_data[pi]); 5137 5138 mci->data[i] = perf_data[pi]; 5139 5140 ++pi; 5141 break; 5142 5143 case COUNTER_SOURCE_MSR: 5144 assert(!no_msr); 5145 5146 get_msr(cpu, mci->msr[i], &mci->data[i]); 5147 5148 mci->data[i] &= mci->msr_mask[i]; 5149 5150 if (debug >= 2) 5151 fprintf(stderr, "Reading msr counter via msr at %u: %llu\n", i, mci->data[i]); 5152 5153 break; 5154 } 5155 } 5156 5157 BUILD_BUG_ON(NUM_MSR_COUNTERS != 3); 5158 t->aperf = mci->data[MSR_RCI_INDEX_APERF]; 5159 t->mperf = mci->data[MSR_RCI_INDEX_MPERF]; 5160 t->smi_count = mci->data[MSR_RCI_INDEX_SMI]; 5161 5162 return 0; 5163 } 5164 5165 int perf_counter_info_read_values(struct perf_counter_info *pp, int cpu, unsigned long long *out, size_t out_size) 5166 { 5167 unsigned int domain; 5168 unsigned long long value; 5169 int fd_counter; 5170 5171 for (size_t i = 0; pp; ++i, pp = pp->next) { 5172 domain = cpu_to_domain(pp, cpu); 5173 assert(domain < pp->num_domains); 5174 5175 fd_counter = pp->fd_perf_per_domain[domain]; 5176 5177 if (fd_counter == -1) 5178 continue; 5179 5180 if (read(fd_counter, &value, sizeof(value)) != sizeof(value)) 5181 return 1; 5182 5183 assert(i < out_size); 5184 out[i] = value * pp->scale; 5185 } 5186 5187 return 0; 5188 } 5189 5190 unsigned long pmt_gen_value_mask(unsigned int lsb, unsigned int msb) 5191 { 5192 unsigned long mask; 5193 5194 if (msb == 63) 5195 mask = 0xffffffffffffffff; 5196 else 5197 mask = ((1ULL << (msb + 1)) - 1); 5198 5199 mask -= (1ULL << lsb) - 1; 5200 5201 return mask; 5202 } 5203 5204 unsigned long pmt_read_counter(struct pmt_counter *ppmt, unsigned int domain_id) 5205 { 5206 if (domain_id >= ppmt->num_domains) 5207 return 0; 5208 5209 const unsigned long *pmmio = ppmt->domains[domain_id].pcounter; 5210 const unsigned long value = pmmio ? *pmmio : 0; 5211 const unsigned long value_mask = pmt_gen_value_mask(ppmt->lsb, ppmt->msb); 5212 const unsigned long value_shift = ppmt->lsb; 5213 5214 return (value & value_mask) >> value_shift; 5215 } 5216 5217 /* Rapl domain enumeration helpers */ 5218 static inline int get_rapl_num_domains(void) 5219 { 5220 if (!platform->has_per_core_rapl) 5221 return topo.num_packages; 5222 5223 return GLOBAL_CORE_ID(topo.max_core_id, topo.num_packages) + 1; 5224 } 5225 5226 static inline int get_rapl_domain_id(int cpu) 5227 { 5228 if (!platform->has_per_core_rapl) 5229 return cpus[cpu].package_id; 5230 5231 return GLOBAL_CORE_ID(cpus[cpu].core_id, cpus[cpu].package_id); 5232 } 5233 5234 /* 5235 * get_counters(...) 5236 * migrate to cpu 5237 * acquire and record local counters for that cpu 5238 */ 5239 int get_counters(PER_THREAD_PARAMS) 5240 { 5241 int cpu = t->cpu_id; 5242 unsigned long long msr; 5243 struct msr_counter *mp; 5244 struct pmt_counter *pp; 5245 int i; 5246 int status; 5247 5248 if (cpu_migrate(cpu)) { 5249 fprintf(outf, "%s: Could not migrate to CPU %d\n", __func__, cpu); 5250 return -1; 5251 } 5252 5253 gettimeofday(&t->tv_begin, (struct timezone *)NULL); 5254 5255 if (first_counter_read) 5256 get_apic_id(t); 5257 5258 t->tsc = rdtsc(); /* we are running on local CPU of interest */ 5259 5260 get_smi_aperf_mperf(cpu, t); 5261 5262 if (DO_BIC(BIC_LLC_MRPS) || DO_BIC(BIC_LLC_HIT)) 5263 get_perf_llc_stats(cpu, &t->llc); 5264 5265 if (DO_BIC(BIC_L2_MRPS) || DO_BIC(BIC_L2_HIT)) 5266 get_perf_l2_stats(cpu, &t->l2); 5267 5268 if (DO_BIC(BIC_IPC)) 5269 if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 5270 return -4; 5271 5272 if (DO_BIC(BIC_IRQ)) 5273 t->irq_count = irqs_per_cpu[cpu]; 5274 if (DO_BIC(BIC_NMI)) 5275 t->nmi_count = nmi_per_cpu[cpu]; 5276 5277 get_cstate_counters(cpu, t, c, p); 5278 5279 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 5280 if (get_mp(cpu, mp, &t->counter[i], mp->sp->path)) 5281 return -10; 5282 } 5283 5284 if (perf_counter_info_read_values(sys.perf_tp, cpu, t->perf_counter, MAX_ADDED_THREAD_COUNTERS)) 5285 return -10; 5286 5287 for (i = 0, pp = sys.pmt_tp; pp; i++, pp = pp->next) 5288 t->pmt_counter[i] = pmt_read_counter(pp, t->cpu_id); 5289 5290 /* collect core counters only for 1st thread in core */ 5291 if (!is_cpu_first_thread_in_core(t, c)) 5292 goto done; 5293 5294 if (platform->has_per_core_rapl) { 5295 status = get_rapl_counters(cpu, get_rapl_domain_id(cpu), c, p); 5296 if (status != 0) 5297 return status; 5298 } 5299 5300 if (DO_BIC(BIC_CPU_c7) && t->is_atom) { 5301 /* 5302 * For Atom CPUs that has core cstate deeper than c6, 5303 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 5304 * Minus CC7 (and deeper cstates) residency to get 5305 * accturate cc6 residency. 5306 */ 5307 c->c6 -= c->c7; 5308 } 5309 5310 if (DO_BIC(BIC_Mod_c6)) 5311 get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us); 5312 5313 if (DO_BIC(BIC_CoreTmp)) { 5314 get_msr(cpu, MSR_IA32_THERM_STATUS, &msr); 5315 c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 5316 } 5317 5318 if (DO_BIC(BIC_CORE_THROT_CNT)) 5319 get_core_throt_cnt(cpu, &c->core_throt_cnt); 5320 5321 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 5322 if (get_mp(cpu, mp, &c->counter[i], mp->sp->path)) 5323 return -10; 5324 } 5325 5326 if (perf_counter_info_read_values(sys.perf_cp, cpu, c->perf_counter, MAX_ADDED_CORE_COUNTERS)) 5327 return -10; 5328 5329 for (i = 0, pp = sys.pmt_cp; pp; i++, pp = pp->next) 5330 c->pmt_counter[i] = pmt_read_counter(pp, cpus[t->cpu_id].core_id); 5331 5332 /* collect package counters only for 1st core in package */ 5333 if (!is_cpu_first_core_in_package(t, p)) 5334 goto done; 5335 5336 if (DO_BIC(BIC_Totl_c0)) 5337 get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0); 5338 if (DO_BIC(BIC_Any_c0)) 5339 get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0); 5340 if (DO_BIC(BIC_GFX_c0)) 5341 get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0); 5342 if (DO_BIC(BIC_CPUGFX)) 5343 get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0); 5344 5345 if (DO_BIC(BIC_CPU_LPI)) 5346 p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 5347 if (DO_BIC(BIC_SYS_LPI)) 5348 p->sys_lpi = cpuidle_cur_sys_lpi_us; 5349 5350 if (!platform->has_per_core_rapl) { 5351 status = get_rapl_counters(cpu, get_rapl_domain_id(cpu), c, p); 5352 if (status != 0) 5353 return status; 5354 } 5355 5356 if (DO_BIC(BIC_PkgTmp)) { 5357 get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr); 5358 p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 5359 } 5360 5361 if (DO_BIC(BIC_UNCORE_MHZ)) 5362 p->uncore_mhz = get_legacy_uncore_mhz(cpus[t->cpu_id].package_id); 5363 5364 if (DO_BIC(BIC_GFX_rc6)) 5365 p->gfx_rc6_ms = gfx_info[GFX_rc6].val_ull; 5366 5367 if (DO_BIC(BIC_GFXMHz)) 5368 p->gfx_mhz = gfx_info[GFX_MHz].val; 5369 5370 if (DO_BIC(BIC_GFXACTMHz)) 5371 p->gfx_act_mhz = gfx_info[GFX_ACTMHz].val; 5372 5373 if (DO_BIC(BIC_SAM_mc6)) 5374 p->sam_mc6_ms = gfx_info[SAM_mc6].val_ull; 5375 5376 if (DO_BIC(BIC_SAMMHz)) 5377 p->sam_mhz = gfx_info[SAM_MHz].val; 5378 5379 if (DO_BIC(BIC_SAMACTMHz)) 5380 p->sam_act_mhz = gfx_info[SAM_ACTMHz].val; 5381 5382 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 5383 char *path = NULL; 5384 5385 if (mp->msr_num == 0) { 5386 path = find_sysfs_path_by_id(mp->sp, cpus[t->cpu_id].package_id); 5387 if (path == NULL) { 5388 warnx("%s: package_id %d not found", __func__, cpus[t->cpu_id].package_id); 5389 return -10; 5390 } 5391 } 5392 if (get_mp(cpu, mp, &p->counter[i], path)) 5393 return -10; 5394 } 5395 5396 if (perf_counter_info_read_values(sys.perf_pp, cpu, p->perf_counter, MAX_ADDED_PACKAGE_COUNTERS)) 5397 return -10; 5398 5399 for (i = 0, pp = sys.pmt_pp; pp; i++, pp = pp->next) 5400 p->pmt_counter[i] = pmt_read_counter(pp, cpus[t->cpu_id].package_id); 5401 5402 done: 5403 gettimeofday(&t->tv_end, (struct timezone *)NULL); 5404 5405 return 0; 5406 } 5407 5408 int pkg_cstate_limit = PCLUKN; 5409 char *pkg_cstate_limit_strings[] = { "unknown", "reserved", "pc0", "pc1", "pc2", 5410 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 5411 }; 5412 5413 int nhm_pkg_cstate_limits[16] = { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5414 PCLRSV, PCLRSV 5415 }; 5416 5417 int snb_pkg_cstate_limits[16] = { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5418 PCLRSV, PCLRSV 5419 }; 5420 5421 int hsw_pkg_cstate_limits[16] = { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5422 PCLRSV, PCLRSV 5423 }; 5424 5425 int slv_pkg_cstate_limits[16] = { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5426 PCL__6, PCL__7 5427 }; 5428 5429 int amt_pkg_cstate_limits[16] = { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5430 PCLRSV, PCLRSV 5431 }; 5432 5433 int phi_pkg_cstate_limits[16] = { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5434 PCLRSV, PCLRSV 5435 }; 5436 5437 int glm_pkg_cstate_limits[16] = { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5438 PCLRSV, PCLRSV 5439 }; 5440 5441 int skx_pkg_cstate_limits[16] = { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5442 PCLRSV, PCLRSV 5443 }; 5444 5445 int icx_pkg_cstate_limits[16] = { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 5446 PCLRSV, PCLRSV 5447 }; 5448 5449 void probe_cst_limit(void) 5450 { 5451 unsigned long long msr; 5452 int *pkg_cstate_limits; 5453 5454 if (!platform->has_nhm_msrs || no_msr) 5455 return; 5456 5457 switch (platform->cst_limit) { 5458 case CST_LIMIT_NHM: 5459 pkg_cstate_limits = nhm_pkg_cstate_limits; 5460 break; 5461 case CST_LIMIT_SNB: 5462 pkg_cstate_limits = snb_pkg_cstate_limits; 5463 break; 5464 case CST_LIMIT_HSW: 5465 pkg_cstate_limits = hsw_pkg_cstate_limits; 5466 break; 5467 case CST_LIMIT_SKX: 5468 pkg_cstate_limits = skx_pkg_cstate_limits; 5469 break; 5470 case CST_LIMIT_ICX: 5471 pkg_cstate_limits = icx_pkg_cstate_limits; 5472 break; 5473 case CST_LIMIT_SLV: 5474 pkg_cstate_limits = slv_pkg_cstate_limits; 5475 break; 5476 case CST_LIMIT_AMT: 5477 pkg_cstate_limits = amt_pkg_cstate_limits; 5478 break; 5479 case CST_LIMIT_KNL: 5480 pkg_cstate_limits = phi_pkg_cstate_limits; 5481 break; 5482 case CST_LIMIT_GMT: 5483 pkg_cstate_limits = glm_pkg_cstate_limits; 5484 break; 5485 default: 5486 return; 5487 } 5488 5489 get_msr(master_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 5490 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 5491 } 5492 5493 static void dump_platform_info(void) 5494 { 5495 unsigned long long msr; 5496 unsigned int ratio; 5497 5498 if (!platform->has_nhm_msrs || no_msr) 5499 return; 5500 5501 get_msr(master_cpu, MSR_PLATFORM_INFO, &msr); 5502 5503 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", master_cpu, msr); 5504 5505 ratio = (msr >> 40) & 0xFF; 5506 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 5507 5508 ratio = (msr >> 8) & 0xFF; 5509 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 5510 } 5511 5512 static void dump_power_ctl(void) 5513 { 5514 unsigned long long msr; 5515 5516 if (!platform->has_nhm_msrs || no_msr) 5517 return; 5518 5519 get_msr(master_cpu, MSR_IA32_POWER_CTL, &msr); 5520 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", master_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 5521 5522 /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 5523 if (platform->has_cst_prewake_bit) 5524 fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 5525 5526 return; 5527 } 5528 5529 static void dump_turbo_ratio_limit2(void) 5530 { 5531 unsigned long long msr; 5532 unsigned int ratio; 5533 5534 get_msr(master_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 5535 5536 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", master_cpu, msr); 5537 5538 ratio = (msr >> 8) & 0xFF; 5539 if (ratio) 5540 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 5541 5542 ratio = (msr >> 0) & 0xFF; 5543 if (ratio) 5544 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 5545 return; 5546 } 5547 5548 static void dump_turbo_ratio_limit1(void) 5549 { 5550 unsigned long long msr; 5551 unsigned int ratio; 5552 5553 get_msr(master_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 5554 5555 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", master_cpu, msr); 5556 5557 ratio = (msr >> 56) & 0xFF; 5558 if (ratio) 5559 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 5560 5561 ratio = (msr >> 48) & 0xFF; 5562 if (ratio) 5563 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 5564 5565 ratio = (msr >> 40) & 0xFF; 5566 if (ratio) 5567 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 5568 5569 ratio = (msr >> 32) & 0xFF; 5570 if (ratio) 5571 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 5572 5573 ratio = (msr >> 24) & 0xFF; 5574 if (ratio) 5575 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 5576 5577 ratio = (msr >> 16) & 0xFF; 5578 if (ratio) 5579 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 5580 5581 ratio = (msr >> 8) & 0xFF; 5582 if (ratio) 5583 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 5584 5585 ratio = (msr >> 0) & 0xFF; 5586 if (ratio) 5587 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 5588 return; 5589 } 5590 5591 static void dump_turbo_ratio_limits(int trl_msr_offset) 5592 { 5593 unsigned long long msr, core_counts; 5594 int shift; 5595 5596 get_msr(master_cpu, trl_msr_offset, &msr); 5597 fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n", master_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY_" : "", msr); 5598 5599 if (platform->trl_msrs & TRL_CORECOUNT) { 5600 get_msr(master_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 5601 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", master_cpu, core_counts); 5602 } else { 5603 core_counts = 0x0807060504030201; 5604 } 5605 5606 for (shift = 56; shift >= 0; shift -= 8) { 5607 unsigned int ratio, group_size; 5608 5609 ratio = (msr >> shift) & 0xFF; 5610 group_size = (core_counts >> shift) & 0xFF; 5611 if (ratio) 5612 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", ratio, bclk, ratio * bclk, group_size); 5613 } 5614 5615 return; 5616 } 5617 5618 static void dump_atom_turbo_ratio_limits(void) 5619 { 5620 unsigned long long msr; 5621 unsigned int ratio; 5622 5623 get_msr(master_cpu, MSR_ATOM_CORE_RATIOS, &msr); 5624 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", master_cpu, msr & 0xFFFFFFFF); 5625 5626 ratio = (msr >> 0) & 0x3F; 5627 if (ratio) 5628 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 5629 5630 ratio = (msr >> 8) & 0x3F; 5631 if (ratio) 5632 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 5633 5634 ratio = (msr >> 16) & 0x3F; 5635 if (ratio) 5636 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 5637 5638 get_msr(master_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 5639 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", master_cpu, msr & 0xFFFFFFFF); 5640 5641 ratio = (msr >> 24) & 0x3F; 5642 if (ratio) 5643 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 5644 5645 ratio = (msr >> 16) & 0x3F; 5646 if (ratio) 5647 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 5648 5649 ratio = (msr >> 8) & 0x3F; 5650 if (ratio) 5651 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 5652 5653 ratio = (msr >> 0) & 0x3F; 5654 if (ratio) 5655 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 5656 } 5657 5658 static void dump_knl_turbo_ratio_limits(void) 5659 { 5660 const unsigned int buckets_no = 7; 5661 5662 unsigned long long msr; 5663 int delta_cores, delta_ratio; 5664 int i, b_nr; 5665 unsigned int cores[buckets_no]; 5666 unsigned int ratio[buckets_no]; 5667 5668 get_msr(master_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 5669 5670 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", master_cpu, msr); 5671 5672 /* 5673 * Turbo encoding in KNL is as follows: 5674 * [0] -- Reserved 5675 * [7:1] -- Base value of number of active cores of bucket 1. 5676 * [15:8] -- Base value of freq ratio of bucket 1. 5677 * [20:16] -- +ve delta of number of active cores of bucket 2. 5678 * i.e. active cores of bucket 2 = 5679 * active cores of bucket 1 + delta 5680 * [23:21] -- Negative delta of freq ratio of bucket 2. 5681 * i.e. freq ratio of bucket 2 = 5682 * freq ratio of bucket 1 - delta 5683 * [28:24]-- +ve delta of number of active cores of bucket 3. 5684 * [31:29]-- -ve delta of freq ratio of bucket 3. 5685 * [36:32]-- +ve delta of number of active cores of bucket 4. 5686 * [39:37]-- -ve delta of freq ratio of bucket 4. 5687 * [44:40]-- +ve delta of number of active cores of bucket 5. 5688 * [47:45]-- -ve delta of freq ratio of bucket 5. 5689 * [52:48]-- +ve delta of number of active cores of bucket 6. 5690 * [55:53]-- -ve delta of freq ratio of bucket 6. 5691 * [60:56]-- +ve delta of number of active cores of bucket 7. 5692 * [63:61]-- -ve delta of freq ratio of bucket 7. 5693 */ 5694 5695 b_nr = 0; 5696 cores[b_nr] = (msr & 0xFF) >> 1; 5697 ratio[b_nr] = (msr >> 8) & 0xFF; 5698 5699 for (i = 16; i < 64; i += 8) { 5700 delta_cores = (msr >> i) & 0x1F; 5701 delta_ratio = (msr >> (i + 5)) & 0x7; 5702 5703 cores[b_nr + 1] = cores[b_nr] + delta_cores; 5704 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 5705 b_nr++; 5706 } 5707 5708 for (i = buckets_no - 1; i >= 0; i--) 5709 if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 5710 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", ratio[i], bclk, ratio[i] * bclk, cores[i]); 5711 } 5712 5713 static void dump_cst_cfg(void) 5714 { 5715 unsigned long long msr; 5716 5717 if (!platform->has_nhm_msrs || no_msr) 5718 return; 5719 5720 get_msr(master_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 5721 5722 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", master_cpu, msr); 5723 5724 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 5725 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 5726 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 5727 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 5728 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 5729 5730 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 5731 if (platform->has_cst_auto_convension) { 5732 fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 5733 } 5734 5735 fprintf(outf, ")\n"); 5736 5737 return; 5738 } 5739 5740 static void dump_config_tdp(void) 5741 { 5742 unsigned long long msr; 5743 5744 get_msr(master_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 5745 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", master_cpu, msr); 5746 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 5747 5748 get_msr(master_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 5749 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", master_cpu, msr); 5750 if (msr) { 5751 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 5752 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 5753 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 5754 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 5755 } 5756 fprintf(outf, ")\n"); 5757 5758 get_msr(master_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 5759 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", master_cpu, msr); 5760 if (msr) { 5761 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 5762 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 5763 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 5764 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 5765 } 5766 fprintf(outf, ")\n"); 5767 5768 get_msr(master_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 5769 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", master_cpu, msr); 5770 if ((msr) & 0x3) 5771 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 5772 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 5773 fprintf(outf, ")\n"); 5774 5775 get_msr(master_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 5776 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", master_cpu, msr); 5777 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 5778 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 5779 fprintf(outf, ")\n"); 5780 } 5781 5782 unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 5783 5784 void print_irtl(void) 5785 { 5786 unsigned long long msr; 5787 5788 if (!platform->has_irtl_msrs || no_msr) 5789 return; 5790 5791 if (platform->supported_cstates & PC3) { 5792 get_msr(master_cpu, MSR_PKGC3_IRTL, &msr); 5793 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", master_cpu, msr); 5794 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5795 } 5796 5797 if (platform->supported_cstates & PC6) { 5798 get_msr(master_cpu, MSR_PKGC6_IRTL, &msr); 5799 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", master_cpu, msr); 5800 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5801 } 5802 5803 if (platform->supported_cstates & PC7) { 5804 get_msr(master_cpu, MSR_PKGC7_IRTL, &msr); 5805 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", master_cpu, msr); 5806 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5807 } 5808 5809 if (platform->supported_cstates & PC8) { 5810 get_msr(master_cpu, MSR_PKGC8_IRTL, &msr); 5811 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", master_cpu, msr); 5812 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5813 } 5814 5815 if (platform->supported_cstates & PC9) { 5816 get_msr(master_cpu, MSR_PKGC9_IRTL, &msr); 5817 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", master_cpu, msr); 5818 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5819 } 5820 5821 if (platform->supported_cstates & PC10) { 5822 get_msr(master_cpu, MSR_PKGC10_IRTL, &msr); 5823 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", master_cpu, msr); 5824 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 5825 } 5826 } 5827 5828 void free_fd_percpu(void) 5829 { 5830 int i; 5831 5832 if (!fd_percpu) 5833 return; 5834 5835 for (i = 0; i <= topo.max_cpu_num; ++i) { 5836 if (fd_percpu[i] != 0) 5837 close(fd_percpu[i]); 5838 } 5839 5840 free(fd_percpu); 5841 fd_percpu = NULL; 5842 } 5843 5844 void free_fd_instr_count_percpu(void) 5845 { 5846 if (!fd_instr_count_percpu) 5847 return; 5848 5849 for (int i = 0; i <= topo.max_cpu_num; ++i) { 5850 if (fd_instr_count_percpu[i] != 0) 5851 close(fd_instr_count_percpu[i]); 5852 } 5853 5854 free(fd_instr_count_percpu); 5855 fd_instr_count_percpu = NULL; 5856 } 5857 5858 void free_fd_llc_percpu(void) 5859 { 5860 if (!fd_llc_percpu) 5861 return; 5862 5863 for (int i = 0; i <= topo.max_cpu_num; ++i) { 5864 if (fd_llc_percpu[i] != 0) 5865 close(fd_llc_percpu[i]); 5866 } 5867 5868 free(fd_llc_percpu); 5869 fd_llc_percpu = NULL; 5870 5871 BIC_NOT_PRESENT(BIC_LLC_MRPS); 5872 BIC_NOT_PRESENT(BIC_LLC_HIT); 5873 } 5874 5875 void free_fd_l2_percpu(void) 5876 { 5877 if (!fd_l2_percpu) 5878 return; 5879 5880 for (int i = 0; i <= topo.max_cpu_num; ++i) { 5881 if (fd_l2_percpu[i] != 0) 5882 close(fd_l2_percpu[i]); 5883 } 5884 5885 free(fd_l2_percpu); 5886 fd_l2_percpu = NULL; 5887 5888 BIC_NOT_PRESENT(BIC_L2_MRPS); 5889 BIC_NOT_PRESENT(BIC_L2_HIT); 5890 } 5891 5892 void free_fd_cstate(void) 5893 { 5894 if (!ccstate_counter_info) 5895 return; 5896 5897 const int counter_info_num = ccstate_counter_info_size; 5898 5899 for (int counter_id = 0; counter_id < counter_info_num; ++counter_id) { 5900 if (ccstate_counter_info[counter_id].fd_perf_core != -1) 5901 close(ccstate_counter_info[counter_id].fd_perf_core); 5902 5903 if (ccstate_counter_info[counter_id].fd_perf_pkg != -1) 5904 close(ccstate_counter_info[counter_id].fd_perf_pkg); 5905 } 5906 5907 free(ccstate_counter_info); 5908 ccstate_counter_info = NULL; 5909 ccstate_counter_info_size = 0; 5910 } 5911 5912 void free_fd_msr(void) 5913 { 5914 if (!msr_counter_info) 5915 return; 5916 5917 for (int cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 5918 if (msr_counter_info[cpu].fd_perf != -1) 5919 close(msr_counter_info[cpu].fd_perf); 5920 } 5921 5922 free(msr_counter_info); 5923 msr_counter_info = NULL; 5924 msr_counter_info_size = 0; 5925 } 5926 5927 void free_fd_rapl_percpu(void) 5928 { 5929 if (!rapl_counter_info_perdomain) 5930 return; 5931 5932 const int num_domains = rapl_counter_info_perdomain_size; 5933 5934 for (int domain_id = 0; domain_id < num_domains; ++domain_id) { 5935 if (rapl_counter_info_perdomain[domain_id].fd_perf != -1) 5936 close(rapl_counter_info_perdomain[domain_id].fd_perf); 5937 } 5938 5939 free(rapl_counter_info_perdomain); 5940 rapl_counter_info_perdomain = NULL; 5941 rapl_counter_info_perdomain_size = 0; 5942 } 5943 5944 void free_fd_added_perf_counters_(struct perf_counter_info *pp) 5945 { 5946 if (!pp) 5947 return; 5948 5949 if (!pp->fd_perf_per_domain) 5950 return; 5951 5952 while (pp) { 5953 for (size_t domain = 0; domain < pp->num_domains; ++domain) { 5954 if (pp->fd_perf_per_domain[domain] != -1) { 5955 close(pp->fd_perf_per_domain[domain]); 5956 pp->fd_perf_per_domain[domain] = -1; 5957 } 5958 } 5959 5960 free(pp->fd_perf_per_domain); 5961 pp->fd_perf_per_domain = NULL; 5962 5963 pp = pp->next; 5964 } 5965 } 5966 5967 void free_fd_added_perf_counters(void) 5968 { 5969 free_fd_added_perf_counters_(sys.perf_tp); 5970 free_fd_added_perf_counters_(sys.perf_cp); 5971 free_fd_added_perf_counters_(sys.perf_pp); 5972 } 5973 5974 void free_all_buffers(void) 5975 { 5976 int i; 5977 5978 CPU_FREE(cpu_present_set); 5979 cpu_present_set = NULL; 5980 5981 CPU_FREE(cpu_effective_set); 5982 cpu_effective_set = NULL; 5983 5984 CPU_FREE(cpu_allowed_set); 5985 cpu_allowed_set = NULL; 5986 5987 CPU_FREE(cpu_affinity_set); 5988 cpu_affinity_set = NULL; 5989 5990 if (perf_pcore_set) { 5991 CPU_FREE(perf_pcore_set); 5992 perf_pcore_set = NULL; 5993 } 5994 5995 if (perf_ecore_set) { 5996 CPU_FREE(perf_ecore_set); 5997 perf_ecore_set = NULL; 5998 } 5999 6000 if (perf_lcore_set) { 6001 CPU_FREE(perf_lcore_set); 6002 perf_lcore_set = NULL; 6003 } 6004 6005 free(even.threads); 6006 free(even.cores); 6007 free(even.packages); 6008 6009 even.threads = NULL; 6010 even.cores = NULL; 6011 even.packages = NULL; 6012 6013 free(odd.threads); 6014 free(odd.cores); 6015 free(odd.packages); 6016 6017 odd.threads = NULL; 6018 odd.cores = NULL; 6019 odd.packages = NULL; 6020 6021 free(output_buffer); 6022 output_buffer = NULL; 6023 outp = NULL; 6024 6025 free_fd_percpu(); 6026 free_fd_instr_count_percpu(); 6027 free_fd_llc_percpu(); 6028 free_fd_l2_percpu(); 6029 free_fd_msr(); 6030 free_fd_rapl_percpu(); 6031 free_fd_cstate(); 6032 free_fd_added_perf_counters(); 6033 6034 free(irq_column_2_cpu); 6035 free(irqs_per_cpu); 6036 free(nmi_per_cpu); 6037 6038 for (i = 0; i <= topo.max_cpu_num; ++i) { 6039 if (cpus[i].put_ids) 6040 CPU_FREE(cpus[i].put_ids); 6041 } 6042 free(cpus); 6043 } 6044 6045 /* 6046 * Parse a file containing a single int. 6047 * Return 0 if file can not be opened 6048 * Exit if file can be opened, but can not be parsed 6049 */ 6050 int parse_int_file(const char *fmt, ...) 6051 { 6052 va_list args; 6053 char path[PATH_MAX]; 6054 FILE *filep; 6055 int value; 6056 6057 va_start(args, fmt); 6058 vsnprintf(path, sizeof(path), fmt, args); 6059 va_end(args); 6060 filep = fopen(path, "r"); 6061 if (!filep) 6062 return 0; 6063 if (fscanf(filep, "%d", &value) != 1) 6064 err(1, "%s: failed to parse number from file", path); 6065 fclose(filep); 6066 return value; 6067 } 6068 6069 /* 6070 * cpu_is_first_core_in_package(cpu) 6071 * return 1 if given CPU is 1st core in package 6072 */ 6073 int cpu_is_first_core_in_package(int cpu) 6074 { 6075 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 6076 } 6077 6078 int get_package_id(int cpu) 6079 { 6080 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 6081 } 6082 6083 int get_die_id(int cpu) 6084 { 6085 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 6086 } 6087 6088 int get_l3_id(int cpu) 6089 { 6090 return parse_int_file("/sys/devices/system/cpu/cpu%d/cache/index3/id", cpu); 6091 } 6092 6093 int get_module_id(int cpu) 6094 { 6095 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/cluster_id", cpu); 6096 } 6097 6098 int get_core_id(int cpu) 6099 { 6100 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 6101 } 6102 6103 void set_node_data(void) 6104 { 6105 int pkg, node, lnode, cpu, cpux; 6106 int cpu_count; 6107 6108 /* initialize logical_node_id */ 6109 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 6110 cpus[cpu].logical_node_id = -1; 6111 6112 cpu_count = 0; 6113 for (pkg = 0; pkg < topo.num_packages; pkg++) { 6114 lnode = 0; 6115 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 6116 if (cpus[cpu].package_id != pkg) 6117 continue; 6118 /* find a cpu with an unset logical_node_id */ 6119 if (cpus[cpu].logical_node_id != -1) 6120 continue; 6121 cpus[cpu].logical_node_id = lnode; 6122 node = cpus[cpu].physical_node_id; 6123 cpu_count++; 6124 /* 6125 * find all matching cpus on this pkg and set 6126 * the logical_node_id 6127 */ 6128 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 6129 if ((cpus[cpux].package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 6130 cpus[cpux].logical_node_id = lnode; 6131 cpu_count++; 6132 } 6133 } 6134 lnode++; 6135 if (lnode > topo.nodes_per_pkg) 6136 topo.nodes_per_pkg = lnode; 6137 } 6138 if (cpu_count >= topo.max_cpu_num) 6139 break; 6140 } 6141 } 6142 6143 int get_physical_node_id(struct cpu_topology *thiscpu) 6144 { 6145 char path[80]; 6146 FILE *filep; 6147 int i; 6148 int cpu = thiscpu->cpu_id; 6149 6150 for (i = 0; i <= topo.max_cpu_num; i++) { 6151 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 6152 filep = fopen(path, "r"); 6153 if (!filep) 6154 continue; 6155 fclose(filep); 6156 return i; 6157 } 6158 return -1; 6159 } 6160 6161 static int parse_cpu_str(char *cpu_str, cpu_set_t *cpu_set, int cpu_set_size) 6162 { 6163 int start, end; 6164 char *next = cpu_str; 6165 6166 while (next && *next) { 6167 6168 if (*next == '-') /* no negative cpu numbers */ 6169 return 1; 6170 6171 if (*next == '\0' || *next == '\n') 6172 break; 6173 6174 start = strtoul(next, &next, 10); 6175 6176 if (start > topo.max_cpu_num) 6177 return 1; 6178 CPU_SET_S(start, cpu_set_size, cpu_set); 6179 6180 if (*next == '\0' || *next == '\n') 6181 break; 6182 6183 if (*next == ',') { 6184 next += 1; 6185 continue; 6186 } 6187 6188 if (*next == '-') { 6189 next += 1; /* start range */ 6190 } else if (*next == '.') { 6191 next += 1; 6192 if (*next == '.') 6193 next += 1; /* start range */ 6194 else 6195 return 1; 6196 } 6197 6198 end = strtoul(next, &next, 10); 6199 if (end <= start) 6200 return 1; 6201 6202 while (++start <= end) { 6203 if (start > topo.max_cpu_num) 6204 return 1; 6205 CPU_SET_S(start, cpu_set_size, cpu_set); 6206 } 6207 6208 if (*next == ',') 6209 next += 1; 6210 else if (*next != '\0' && *next != '\n') 6211 return 1; 6212 } 6213 6214 return 0; 6215 } 6216 6217 /* 6218 * run func(thread, core, package) in topology order 6219 * skip non-present cpus 6220 */ 6221 6222 int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 6223 struct pkg_data *, struct thread_data *, struct core_data *, 6224 struct pkg_data *), struct thread_data *thread_base, 6225 struct core_data *core_base, struct pkg_data *pkg_base, struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 6226 { 6227 int cpu, retval; 6228 6229 retval = 0; 6230 6231 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 6232 struct thread_data *t, *t2; 6233 struct core_data *c, *c2; 6234 struct pkg_data *p, *p2; 6235 6236 if (cpu_is_not_allowed(cpu)) 6237 continue; 6238 6239 if (has_allowed_lower_ht_sibling(cpu)) /* skip HT sibling */ 6240 continue; 6241 6242 t = &thread_base[cpu]; 6243 t2 = &thread_base2[cpu]; 6244 c = &core_base[GLOBAL_CORE_ID(cpus[cpu].core_id, cpus[cpu].package_id)]; 6245 c2 = &core_base2[GLOBAL_CORE_ID(cpus[cpu].core_id, cpus[cpu].package_id)]; 6246 p = &pkg_base[cpus[cpu].package_id]; 6247 p2 = &pkg_base2[cpus[cpu].package_id]; 6248 6249 retval |= func(t, c, p, t2, c2, p2); 6250 6251 /* Handle HT sibling now */ 6252 int i; 6253 6254 for (i = 0; i <= MAX_HT_ID; ++i) { 6255 int sibling_cpu_id = cpus[cpu].ht_sibling_cpu_id[i]; 6256 6257 if (sibling_cpu_id < 0) 6258 break; 6259 6260 if (sibling_cpu_id == cpu) 6261 continue; 6262 6263 if (cpu_is_not_allowed(sibling_cpu_id)) 6264 continue; 6265 6266 t = &thread_base[sibling_cpu_id]; 6267 t2 = &thread_base2[sibling_cpu_id]; 6268 6269 retval |= func(t, c, p, t2, c2, p2); 6270 } 6271 } 6272 return retval; 6273 } 6274 6275 /* 6276 * run func(cpu) on every cpu in /proc/stat 6277 * return max_cpu number 6278 */ 6279 int for_all_proc_cpus(int (func) (int)) 6280 { 6281 FILE *fp; 6282 int cpu_num; 6283 int retval; 6284 6285 fp = fopen_or_die(proc_stat, "r"); 6286 6287 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 6288 if (retval != 0) 6289 err(1, "%s: failed to parse format", proc_stat); 6290 6291 while (1) { 6292 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 6293 if (retval != 1) 6294 break; 6295 6296 retval = func(cpu_num); 6297 if (retval) { 6298 fclose(fp); 6299 return (retval); 6300 } 6301 } 6302 fclose(fp); 6303 return 0; 6304 } 6305 6306 #define PATH_EFFECTIVE_CPUS "/sys/fs/cgroup/cpuset.cpus.effective" 6307 6308 static char *cpu_effective_str; 6309 6310 static int update_effective_str(bool startup) 6311 { 6312 FILE *fp; 6313 char *buf = NULL; 6314 size_t size = 0; 6315 int ret; 6316 6317 if (!cpu_effective_str && !startup) 6318 return 0; 6319 6320 fp = fopen(PATH_EFFECTIVE_CPUS, "r"); 6321 if (!fp) 6322 return 0; 6323 6324 if (getline(&buf, &size, fp) < 0) 6325 err(1, "%s: file read failed", PATH_EFFECTIVE_CPUS); 6326 6327 fclose(fp); 6328 6329 ret = cpu_effective_str ? strcmp(cpu_effective_str, buf) : 1; 6330 if (!ret) { 6331 free(buf); 6332 return 0; 6333 } 6334 6335 free(cpu_effective_str); 6336 cpu_effective_str = buf; 6337 return 1; 6338 } 6339 6340 static void update_effective_set(bool startup) 6341 { 6342 update_effective_str(startup); 6343 6344 if (parse_cpu_str(cpu_effective_str, cpu_effective_set, cpu_setsize)) 6345 err(1, "%s: cpu str malformat %s", PATH_EFFECTIVE_CPUS, cpu_effective_str); 6346 } 6347 6348 void linux_perf_init(void); 6349 void msr_perf_init(void); 6350 void rapl_perf_init(void); 6351 void cstate_perf_init(void); 6352 void perf_llc_init(void); 6353 void perf_l2_init(void); 6354 void added_perf_counters_init(void); 6355 void pmt_init(void); 6356 6357 void re_initialize(void) 6358 { 6359 free_all_buffers(); 6360 setup_all_buffers(false); 6361 linux_perf_init(); 6362 msr_perf_init(); 6363 rapl_perf_init(); 6364 cstate_perf_init(); 6365 perf_llc_init(); 6366 perf_l2_init(); 6367 added_perf_counters_init(); 6368 pmt_init(); 6369 fprintf(outf, "turbostat: re-initialized with num_cpus %d, allowed_cpus %d\n", topo.num_cpus, topo.allowed_cpus); 6370 } 6371 6372 /* 6373 * count_cpus() 6374 * remember the last one seen, it will be the max 6375 */ 6376 int count_cpus(int cpu) 6377 { 6378 UNUSED(cpu); 6379 6380 topo.num_cpus++; 6381 return 0; 6382 } 6383 6384 int mark_cpu_present(int cpu) 6385 { 6386 CPU_SET_S(cpu, cpu_setsize, cpu_present_set); 6387 return 0; 6388 } 6389 6390 int clear_ht_id(int cpu) 6391 { 6392 int i; 6393 6394 cpus[cpu].ht_id = -1; 6395 for (i = 0; i <= MAX_HT_ID; ++i) 6396 cpus[cpu].ht_sibling_cpu_id[i] = -1; 6397 return 0; 6398 } 6399 6400 int set_my_cpu_type(void) 6401 { 6402 unsigned int eax, ebx, ecx, edx; 6403 unsigned int max_level; 6404 6405 __cpuid(0, max_level, ebx, ecx, edx); 6406 6407 if (max_level < CPUID_LEAF_MODEL_ID) 6408 return 0; 6409 6410 __cpuid(CPUID_LEAF_MODEL_ID, eax, ebx, ecx, edx); 6411 6412 return (eax >> CPUID_LEAF_MODEL_ID_CORE_TYPE_SHIFT); 6413 } 6414 6415 int set_cpu_hybrid_type(int cpu) 6416 { 6417 if (cpu_migrate(cpu)) 6418 return -1; 6419 6420 int type = set_my_cpu_type(); 6421 6422 cpus[cpu].type = type; 6423 return 0; 6424 } 6425 6426 /* 6427 * snapshot_proc_interrupts() 6428 * 6429 * read and record summary of /proc/interrupts 6430 * 6431 * return 1 if config change requires a restart, else return 0 6432 */ 6433 int snapshot_proc_interrupts(void) 6434 { 6435 static FILE *fp; 6436 int column, retval; 6437 6438 if (fp == NULL) 6439 fp = fopen_or_die("/proc/interrupts", "r"); 6440 else 6441 rewind(fp); 6442 6443 /* read 1st line of /proc/interrupts to get cpu* name for each column */ 6444 for (column = 0; column < topo.num_cpus; ++column) { 6445 int cpu_number; 6446 6447 retval = fscanf(fp, " CPU%d", &cpu_number); 6448 if (retval != 1) 6449 break; 6450 6451 if (cpu_number > topo.max_cpu_num) { 6452 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 6453 return 1; 6454 } 6455 6456 irq_column_2_cpu[column] = cpu_number; 6457 irqs_per_cpu[cpu_number] = 0; 6458 nmi_per_cpu[cpu_number] = 0; 6459 } 6460 6461 /* read /proc/interrupt count lines and sum up irqs per cpu */ 6462 while (1) { 6463 int column; 6464 char buf[64]; 6465 int this_row_is_nmi = 0; 6466 6467 retval = fscanf(fp, " %s:", buf); /* irq# "N:" */ 6468 if (retval != 1) 6469 break; 6470 6471 if (strncmp(buf, "NMI", strlen("NMI")) == 0) 6472 this_row_is_nmi = 1; 6473 6474 /* read the count per cpu */ 6475 for (column = 0; column < topo.num_cpus; ++column) { 6476 6477 int cpu_number, irq_count; 6478 6479 retval = fscanf(fp, " %d", &irq_count); 6480 6481 if (retval != 1) 6482 break; 6483 6484 cpu_number = irq_column_2_cpu[column]; 6485 irqs_per_cpu[cpu_number] += irq_count; 6486 if (this_row_is_nmi) 6487 nmi_per_cpu[cpu_number] += irq_count; 6488 } 6489 while (getc(fp) != '\n') ; /* flush interrupt description */ 6490 6491 } 6492 return 0; 6493 } 6494 6495 /* 6496 * snapshot_graphics() 6497 * 6498 * record snapshot of specified graphics sysfs knob 6499 * 6500 * return 1 if config change requires a restart, else return 0 6501 */ 6502 int snapshot_graphics(int idx) 6503 { 6504 int retval; 6505 6506 rewind(gfx_info[idx].fp); 6507 fflush(gfx_info[idx].fp); 6508 6509 switch (idx) { 6510 case GFX_rc6: 6511 case SAM_mc6: 6512 retval = fscanf(gfx_info[idx].fp, "%lld", &gfx_info[idx].val_ull); 6513 if (retval != 1) 6514 err(1, "rc6"); 6515 return 0; 6516 case GFX_MHz: 6517 case GFX_ACTMHz: 6518 case SAM_MHz: 6519 case SAM_ACTMHz: 6520 retval = fscanf(gfx_info[idx].fp, "%d", &gfx_info[idx].val); 6521 if (retval != 1) 6522 err(1, "MHz"); 6523 return 0; 6524 default: 6525 return -EINVAL; 6526 } 6527 } 6528 6529 /* 6530 * snapshot_cpu_lpi() 6531 * 6532 * record snapshot of 6533 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 6534 */ 6535 int snapshot_cpu_lpi_us(void) 6536 { 6537 FILE *fp; 6538 int retval; 6539 6540 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 6541 6542 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 6543 if (retval != 1) { 6544 fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 6545 BIC_NOT_PRESENT(BIC_CPU_LPI); 6546 fclose(fp); 6547 return -1; 6548 } 6549 6550 fclose(fp); 6551 6552 return 0; 6553 } 6554 6555 /* 6556 * snapshot_sys_lpi() 6557 * 6558 * record snapshot of sys_lpi_file 6559 */ 6560 int snapshot_sys_lpi_us(void) 6561 { 6562 FILE *fp; 6563 int retval; 6564 6565 fp = fopen_or_die(sys_lpi_file, "r"); 6566 6567 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 6568 if (retval != 1) { 6569 fprintf(stderr, "Disabling Low Power Idle System output\n"); 6570 BIC_NOT_PRESENT(BIC_SYS_LPI); 6571 fclose(fp); 6572 return -1; 6573 } 6574 fclose(fp); 6575 6576 return 0; 6577 } 6578 6579 /* 6580 * snapshot /proc and /sys files 6581 * 6582 * return 1 if configuration restart needed, else return 0 6583 */ 6584 int snapshot_proc_sysfs_files(void) 6585 { 6586 gettimeofday(&procsysfs_tv_begin, (struct timezone *)NULL); 6587 6588 if (DO_BIC(BIC_IRQ) || DO_BIC(BIC_NMI)) 6589 if (snapshot_proc_interrupts()) 6590 return 1; 6591 6592 if (DO_BIC(BIC_GFX_rc6)) 6593 snapshot_graphics(GFX_rc6); 6594 6595 if (DO_BIC(BIC_GFXMHz)) 6596 snapshot_graphics(GFX_MHz); 6597 6598 if (DO_BIC(BIC_GFXACTMHz)) 6599 snapshot_graphics(GFX_ACTMHz); 6600 6601 if (DO_BIC(BIC_SAM_mc6)) 6602 snapshot_graphics(SAM_mc6); 6603 6604 if (DO_BIC(BIC_SAMMHz)) 6605 snapshot_graphics(SAM_MHz); 6606 6607 if (DO_BIC(BIC_SAMACTMHz)) 6608 snapshot_graphics(SAM_ACTMHz); 6609 6610 if (DO_BIC(BIC_CPU_LPI)) 6611 snapshot_cpu_lpi_us(); 6612 6613 if (DO_BIC(BIC_SYS_LPI)) 6614 snapshot_sys_lpi_us(); 6615 6616 return 0; 6617 } 6618 6619 int exit_requested; 6620 6621 static void signal_handler(int signal) 6622 { 6623 switch (signal) { 6624 case SIGINT: 6625 exit_requested = 1; 6626 if (debug) 6627 fprintf(stderr, " SIGINT\n"); 6628 break; 6629 case SIGUSR1: 6630 if (debug > 1) 6631 fprintf(stderr, "SIGUSR1\n"); 6632 break; 6633 } 6634 } 6635 6636 void setup_signal_handler(void) 6637 { 6638 struct sigaction sa; 6639 6640 memset(&sa, 0, sizeof(sa)); 6641 6642 sa.sa_handler = &signal_handler; 6643 6644 if (sigaction(SIGINT, &sa, NULL) < 0) 6645 err(1, "sigaction SIGINT"); 6646 if (sigaction(SIGUSR1, &sa, NULL) < 0) 6647 err(1, "sigaction SIGUSR1"); 6648 } 6649 6650 void do_sleep(void) 6651 { 6652 struct timeval tout; 6653 struct timespec rest; 6654 fd_set readfds; 6655 int retval; 6656 6657 FD_ZERO(&readfds); 6658 FD_SET(0, &readfds); 6659 6660 if (ignore_stdin) { 6661 nanosleep(&interval_ts, NULL); 6662 return; 6663 } 6664 6665 tout = interval_tv; 6666 retval = select(1, &readfds, NULL, NULL, &tout); 6667 6668 if (retval == 1) { 6669 switch (getc(stdin)) { 6670 case 'q': 6671 exit_requested = 1; 6672 break; 6673 case EOF: 6674 /* 6675 * 'stdin' is a pipe closed on the other end. There 6676 * won't be any further input. 6677 */ 6678 ignore_stdin = 1; 6679 /* Sleep the rest of the time */ 6680 rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 6681 rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 6682 nanosleep(&rest, NULL); 6683 } 6684 } 6685 } 6686 6687 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 6688 { 6689 int idx; 6690 unsigned long long msr_cur, msr_last; 6691 6692 assert(!no_msr); 6693 6694 if (!per_cpu_msr_sum) 6695 return 1; 6696 6697 idx = offset_to_idx(offset); 6698 if (idx < 0) 6699 return idx; 6700 /* get_msr_sum() = sum + (get_msr() - last) */ 6701 get_msr(cpu, offset, &msr_cur); 6702 msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 6703 DELTA_WRAP32(msr_cur, msr_last); 6704 *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 6705 6706 return 0; 6707 } 6708 6709 timer_t timerid; 6710 6711 /* Timer callback, update the sum of MSRs periodically. */ 6712 static int update_msr_sum(PER_THREAD_PARAMS) 6713 { 6714 int i; 6715 int cpu = t->cpu_id; 6716 6717 UNUSED(c); 6718 UNUSED(p); 6719 6720 assert(!no_msr); 6721 6722 for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 6723 unsigned long long msr_cur, msr_last; 6724 off_t offset; 6725 6726 if (!idx_valid(i)) 6727 continue; 6728 offset = idx_to_offset(i); 6729 if (offset < 0) 6730 continue; 6731 get_msr(cpu, offset, &msr_cur); 6732 6733 msr_last = per_cpu_msr_sum[cpu].entries[i].last; 6734 per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 6735 6736 DELTA_WRAP32(msr_cur, msr_last); 6737 per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 6738 } 6739 return 0; 6740 } 6741 6742 static void msr_record_handler(union sigval v) 6743 { 6744 UNUSED(v); 6745 6746 for_all_cpus(update_msr_sum, EVEN_COUNTERS); 6747 } 6748 6749 void msr_sum_record(void) 6750 { 6751 struct itimerspec its; 6752 struct sigevent sev; 6753 6754 per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 6755 if (!per_cpu_msr_sum) { 6756 fprintf(outf, "Can not allocate memory for long time MSR.\n"); 6757 return; 6758 } 6759 /* 6760 * Signal handler might be restricted, so use thread notifier instead. 6761 */ 6762 memset(&sev, 0, sizeof(struct sigevent)); 6763 sev.sigev_notify = SIGEV_THREAD; 6764 sev.sigev_notify_function = msr_record_handler; 6765 6766 sev.sigev_value.sival_ptr = &timerid; 6767 if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 6768 fprintf(outf, "Can not create timer.\n"); 6769 goto release_msr; 6770 } 6771 6772 its.it_value.tv_sec = 0; 6773 its.it_value.tv_nsec = 1; 6774 /* 6775 * A wraparound time has been calculated early. 6776 * Some sources state that the peak power for a 6777 * microprocessor is usually 1.5 times the TDP rating, 6778 * use 2 * TDP for safety. 6779 */ 6780 its.it_interval.tv_sec = rapl_joule_counter_range / 2; 6781 its.it_interval.tv_nsec = 0; 6782 6783 if (timer_settime(timerid, 0, &its, NULL) == -1) { 6784 fprintf(outf, "Can not set timer.\n"); 6785 goto release_timer; 6786 } 6787 return; 6788 6789 release_timer: 6790 timer_delete(timerid); 6791 release_msr: 6792 free(per_cpu_msr_sum); 6793 per_cpu_msr_sum = NULL; 6794 } 6795 6796 /* 6797 * set_my_sched_priority(pri) 6798 * return previous priority on success 6799 * return value < -20 on failure 6800 */ 6801 int set_my_sched_priority(int priority) 6802 { 6803 int retval; 6804 int original_priority; 6805 6806 errno = 0; 6807 original_priority = getpriority(PRIO_PROCESS, 0); 6808 if (errno && (original_priority == -1)) 6809 return -21; 6810 6811 retval = setpriority(PRIO_PROCESS, 0, priority); 6812 if (retval) 6813 return -21; 6814 6815 errno = 0; 6816 retval = getpriority(PRIO_PROCESS, 0); 6817 if (retval != priority) 6818 return -21; 6819 6820 return original_priority; 6821 } 6822 6823 void turbostat_loop() 6824 { 6825 int retval; 6826 int restarted = 0; 6827 unsigned int done_iters = 0; 6828 6829 setup_signal_handler(); 6830 6831 /* 6832 * elevate own priority for interval mode 6833 * 6834 * ignore on error - we probably don't have permission to set it, but 6835 * it's not a big deal 6836 */ 6837 set_my_sched_priority(-20); 6838 6839 restart: 6840 restarted++; 6841 6842 snapshot_proc_sysfs_files(); 6843 retval = for_all_cpus(get_counters, EVEN_COUNTERS); 6844 first_counter_read = 0; 6845 if (retval < -1) { 6846 exit(retval); 6847 } else if (retval == -1) { 6848 if (restarted > 10) { 6849 exit(retval); 6850 } 6851 re_initialize(); 6852 goto restart; 6853 } 6854 restarted = 0; 6855 done_iters = 0; 6856 gettimeofday(&tv_even, (struct timezone *)NULL); 6857 6858 while (1) { 6859 if (for_all_proc_cpus(cpu_is_not_present)) { 6860 re_initialize(); 6861 goto restart; 6862 } 6863 if (update_effective_str(false)) { 6864 re_initialize(); 6865 goto restart; 6866 } 6867 do_sleep(); 6868 if (snapshot_proc_sysfs_files()) 6869 goto restart; 6870 retval = for_all_cpus(get_counters, ODD_COUNTERS); 6871 if (retval < -1) { 6872 exit(retval); 6873 } else if (retval == -1) { 6874 re_initialize(); 6875 goto restart; 6876 } 6877 gettimeofday(&tv_odd, (struct timezone *)NULL); 6878 timersub(&tv_odd, &tv_even, &tv_delta); 6879 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 6880 re_initialize(); 6881 goto restart; 6882 } 6883 delta_platform(&platform_counters_odd, &platform_counters_even); 6884 compute_average(EVEN_COUNTERS); 6885 format_all_counters(EVEN_COUNTERS); 6886 flush_output_stdout(); 6887 if (exit_requested) 6888 break; 6889 if (num_iterations && ++done_iters >= num_iterations) 6890 break; 6891 do_sleep(); 6892 if (snapshot_proc_sysfs_files()) 6893 goto restart; 6894 retval = for_all_cpus(get_counters, EVEN_COUNTERS); 6895 if (retval < -1) { 6896 exit(retval); 6897 } else if (retval == -1) { 6898 re_initialize(); 6899 goto restart; 6900 } 6901 gettimeofday(&tv_even, (struct timezone *)NULL); 6902 timersub(&tv_even, &tv_odd, &tv_delta); 6903 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 6904 re_initialize(); 6905 goto restart; 6906 } 6907 delta_platform(&platform_counters_even, &platform_counters_odd); 6908 compute_average(ODD_COUNTERS); 6909 format_all_counters(ODD_COUNTERS); 6910 flush_output_stdout(); 6911 if (exit_requested) 6912 break; 6913 if (num_iterations && ++done_iters >= num_iterations) 6914 break; 6915 } 6916 } 6917 6918 int probe_dev_msr(void) 6919 { 6920 struct stat sb; 6921 char pathname[32]; 6922 6923 sprintf(pathname, "/dev/msr%d", master_cpu); 6924 return !stat(pathname, &sb); 6925 } 6926 6927 int probe_dev_cpu_msr(void) 6928 { 6929 struct stat sb; 6930 char pathname[32]; 6931 6932 sprintf(pathname, "/dev/cpu/%d/msr", master_cpu); 6933 return !stat(pathname, &sb); 6934 } 6935 6936 int probe_msr_driver(void) 6937 { 6938 if (probe_dev_msr()) { 6939 use_android_msr_path = 1; 6940 return 1; 6941 } 6942 return probe_dev_cpu_msr(); 6943 } 6944 6945 void check_msr_driver(void) 6946 { 6947 if (probe_msr_driver()) 6948 return; 6949 6950 if (system("/sbin/modprobe msr > /dev/null 2>&1")) 6951 no_msr = 1; 6952 6953 if (!probe_msr_driver()) 6954 no_msr = 1; 6955 } 6956 6957 /* 6958 * check for CAP_SYS_RAWIO 6959 * return 0 on success 6960 * return 1 on fail 6961 */ 6962 int check_for_cap_sys_rawio(void) 6963 { 6964 cap_t caps; 6965 cap_flag_value_t cap_flag_value; 6966 int ret = 0; 6967 6968 caps = cap_get_proc(); 6969 if (caps == NULL) { 6970 /* 6971 * CONFIG_MULTIUSER=n kernels have no cap_get_proc() 6972 * Allow them to continue and attempt to access MSRs 6973 */ 6974 if (errno == ENOSYS) 6975 return 0; 6976 6977 return 1; 6978 } 6979 6980 if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) { 6981 ret = 1; 6982 goto free_and_exit; 6983 } 6984 6985 if (cap_flag_value != CAP_SET) { 6986 ret = 1; 6987 goto free_and_exit; 6988 } 6989 6990 free_and_exit: 6991 if (cap_free(caps) == -1) 6992 err(-6, "cap_free"); 6993 6994 return ret; 6995 } 6996 6997 void check_msr_permission(void) 6998 { 6999 int failed = 0; 7000 char pathname[32]; 7001 7002 if (no_msr) 7003 return; 7004 7005 /* check for CAP_SYS_RAWIO */ 7006 failed += check_for_cap_sys_rawio(); 7007 7008 /* test file permissions */ 7009 sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", master_cpu); 7010 if (euidaccess(pathname, R_OK)) { 7011 failed++; 7012 } 7013 7014 if (failed) { 7015 warnx("Failed to access %s. Some of the counters may not be available\n" "\tRun as root to enable them or use %s to disable the access explicitly", pathname, "--no-msr"); 7016 no_msr = 1; 7017 } 7018 } 7019 7020 void probe_bclk(void) 7021 { 7022 unsigned long long msr; 7023 unsigned int base_ratio; 7024 7025 if (!platform->has_nhm_msrs || no_msr) 7026 return; 7027 7028 if (platform->bclk_freq == BCLK_100MHZ) 7029 bclk = 100.00; 7030 else if (platform->bclk_freq == BCLK_133MHZ) 7031 bclk = 133.33; 7032 else if (platform->bclk_freq == BCLK_SLV) 7033 bclk = slm_bclk(); 7034 else 7035 return; 7036 7037 get_msr(master_cpu, MSR_PLATFORM_INFO, &msr); 7038 base_ratio = (msr >> 8) & 0xFF; 7039 7040 base_hz = base_ratio * bclk * 1000000; 7041 has_base_hz = 1; 7042 7043 if (platform->enable_tsc_tweak) 7044 tsc_tweak = base_hz / tsc_hz; 7045 } 7046 7047 static void remove_underbar(char *s) 7048 { 7049 char *to = s; 7050 7051 while (*s) { 7052 if (*s != '_') 7053 *to++ = *s; 7054 s++; 7055 } 7056 7057 *to = 0; 7058 } 7059 7060 static void dump_turbo_ratio_info(void) 7061 { 7062 if (!has_turbo) 7063 return; 7064 7065 if (!platform->has_nhm_msrs || no_msr) 7066 return; 7067 7068 if (platform->trl_msrs & TRL_LIMIT2) 7069 dump_turbo_ratio_limit2(); 7070 7071 if (platform->trl_msrs & TRL_LIMIT1) 7072 dump_turbo_ratio_limit1(); 7073 7074 if (platform->trl_msrs & TRL_BASE) { 7075 dump_turbo_ratio_limits(MSR_TURBO_RATIO_LIMIT); 7076 7077 if (is_hybrid) 7078 dump_turbo_ratio_limits(MSR_SECONDARY_TURBO_RATIO_LIMIT); 7079 } 7080 7081 if (platform->trl_msrs & TRL_ATOM) 7082 dump_atom_turbo_ratio_limits(); 7083 7084 if (platform->trl_msrs & TRL_KNL) 7085 dump_knl_turbo_ratio_limits(); 7086 7087 if (platform->has_config_tdp) 7088 dump_config_tdp(); 7089 } 7090 7091 static int read_sysfs_int(char *path) 7092 { 7093 FILE *input; 7094 int retval = -1; 7095 7096 input = fopen(path, "r"); 7097 if (input == NULL) { 7098 if (debug) 7099 fprintf(outf, "NSFOD %s\n", path); 7100 return (-1); 7101 } 7102 if (fscanf(input, "%d", &retval) != 1) 7103 err(1, "%s: failed to read int from file", path); 7104 fclose(input); 7105 7106 return (retval); 7107 } 7108 7109 /* 7110 * read_sysfs_line(char *path, char **buf) 7111 * 7112 * fail silently if open fails 7113 * err() if open works, but read fails 7114 * Replace \n with \0 in buffer and return byte count accordingly. 7115 * 7116 * n.b. caller owns freeing buf 7117 */ 7118 static int read_sysfs_line(char *path, char **buf) 7119 { 7120 FILE *fp; 7121 size_t size = 0; 7122 int bytes_read; 7123 7124 fp = fopen(path, "r"); 7125 if (!fp) 7126 return 0; 7127 7128 bytes_read = getline(buf, &size, fp); 7129 if (bytes_read < 0) 7130 err(1, "%s: file read failed", path); 7131 7132 fclose(fp); 7133 7134 assert((*buf)[bytes_read - 1] == '\n'); 7135 (*buf)[bytes_read - 1] = '\0'; 7136 return bytes_read - 1; 7137 } 7138 7139 static void dump_sysfs_file(char *path) 7140 { 7141 FILE *input; 7142 char cpuidle_buf[64]; 7143 7144 input = fopen(path, "r"); 7145 if (input == NULL) { 7146 if (debug) 7147 fprintf(outf, "NSFOD %s\n", path); 7148 return; 7149 } 7150 if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 7151 err(1, "%s: failed to read file", path); 7152 fclose(input); 7153 7154 fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 7155 } 7156 7157 static void probe_intel_uncore_frequency_legacy(void) 7158 { 7159 int i, j; 7160 char path[256]; 7161 7162 for (i = 0; i < topo.num_packages; ++i) { 7163 for (j = 0; j <= topo.max_die_id; ++j) { 7164 int k, l; 7165 char path_base[128]; 7166 7167 sprintf(path_base, "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d", i, j); 7168 7169 sprintf(path, "%s/current_freq_khz", path_base); 7170 if (access(path, R_OK)) 7171 continue; 7172 7173 BIC_PRESENT(BIC_UNCORE_MHZ); 7174 7175 if (quiet) 7176 return; 7177 7178 sprintf(path, "%s/min_freq_khz", path_base); 7179 k = read_sysfs_int(path); 7180 sprintf(path, "%s/max_freq_khz", path_base); 7181 l = read_sysfs_int(path); 7182 fprintf(outf, "Uncore Frequency package%d die%d: %d - %d MHz ", i, j, k / 1000, l / 1000); 7183 7184 sprintf(path, "%s/initial_min_freq_khz", path_base); 7185 k = read_sysfs_int(path); 7186 sprintf(path, "%s/initial_max_freq_khz", path_base); 7187 l = read_sysfs_int(path); 7188 fprintf(outf, "(%d - %d MHz)", k / 1000, l / 1000); 7189 7190 sprintf(path, "%s/current_freq_khz", path_base); 7191 k = read_sysfs_int(path); 7192 fprintf(outf, " %d MHz\n", k / 1000); 7193 } 7194 } 7195 } 7196 7197 static void probe_intel_uncore_frequency_cluster(void) 7198 { 7199 int i, uncore_max_id; 7200 char path[256]; 7201 char path_base[128]; 7202 7203 if (access("/sys/devices/system/cpu/intel_uncore_frequency/uncore00/current_freq_khz", R_OK)) 7204 return; 7205 7206 for (uncore_max_id = 0;; ++uncore_max_id) { 7207 7208 sprintf(path_base, "/sys/devices/system/cpu/intel_uncore_frequency/uncore%02d", uncore_max_id); 7209 7210 /* uncore## start at 00 and skips no numbers, so stop upon first missing */ 7211 if (access(path_base, R_OK)) { 7212 uncore_max_id -= 1; 7213 break; 7214 } 7215 } 7216 for (i = uncore_max_id; i >= 0; --i) { 7217 int k, l; 7218 int unc_pkg_id, domain_id, cluster_id; 7219 char name_buf[16]; 7220 7221 sprintf(path_base, "/sys/devices/system/cpu/intel_uncore_frequency/uncore%02d", i); 7222 7223 if (access(path_base, R_OK)) 7224 err(1, "%s: %s", __func__, path_base); 7225 7226 sprintf(path, "%s/package_id", path_base); 7227 unc_pkg_id = read_sysfs_int(path); 7228 7229 sprintf(path, "%s/domain_id", path_base); 7230 domain_id = read_sysfs_int(path); 7231 7232 sprintf(path, "%s/fabric_cluster_id", path_base); 7233 cluster_id = read_sysfs_int(path); 7234 7235 sprintf(path, "%s/current_freq_khz", path_base); 7236 sprintf(name_buf, "UMHz%d.%d", domain_id, cluster_id); 7237 7238 /* 7239 * Once add_couter() is called, that counter is always read 7240 * and reported -- So it is effectively (enabled & present). 7241 * Only call add_counter() here if legacy BIC_UNCORE_MHZ (UncMHz) 7242 * is (enabled). Since we are in this routine, we 7243 * know we will not probe and set (present) the legacy counter. 7244 * 7245 * This allows "--show/--hide UncMHz" to be effective for 7246 * the clustered MHz counters, as a group. 7247 */ 7248 if BIC_IS_ENABLED 7249 (BIC_UNCORE_MHZ) 7250 add_counter(0, path, name_buf, 0, SCOPE_PACKAGE, COUNTER_K2M, FORMAT_AVERAGE, 0, unc_pkg_id); 7251 7252 if (quiet) 7253 continue; 7254 7255 char *agent_types = NULL; 7256 int agent_bytes; 7257 7258 sprintf(path, "%s/agent_types", path_base); 7259 agent_bytes = read_sysfs_line(path, &agent_types); 7260 7261 if (agent_bytes > 0) 7262 fprintf(outf, "pkg%d: %s(%s):", unc_pkg_id, name_buf, agent_types); 7263 else 7264 fprintf(outf, "pkg%d: %s:", unc_pkg_id, name_buf); 7265 free(agent_types); 7266 7267 sprintf(path, "%s/min_freq_khz", path_base); 7268 k = read_sysfs_int(path); 7269 sprintf(path, "%s/max_freq_khz", path_base); 7270 l = read_sysfs_int(path); 7271 fprintf(outf, " %d - %d MHz ", k / 1000, l / 1000); 7272 7273 sprintf(path, "%s/initial_min_freq_khz", path_base); 7274 k = read_sysfs_int(path); 7275 sprintf(path, "%s/initial_max_freq_khz", path_base); 7276 l = read_sysfs_int(path); 7277 fprintf(outf, "(%d - %d MHz)", k / 1000, l / 1000); 7278 7279 sprintf(path, "%s/current_freq_khz", path_base); 7280 k = read_sysfs_int(path); 7281 fprintf(outf, " %d MHz\n", k / 1000); 7282 } 7283 } 7284 7285 static void probe_intel_uncore_frequency(void) 7286 { 7287 if (!genuine_intel) 7288 return; 7289 7290 if (access("/sys/devices/system/cpu/intel_uncore_frequency/uncore00", R_OK) == 0) 7291 probe_intel_uncore_frequency_cluster(); 7292 else 7293 probe_intel_uncore_frequency_legacy(); 7294 } 7295 7296 static void set_graphics_fp(char *path, int idx) 7297 { 7298 if (!access(path, R_OK)) 7299 gfx_info[idx].fp = fopen_or_die(path, "r"); 7300 } 7301 7302 /* Enlarge this if there are /sys/class/drm/card2 ... */ 7303 #define GFX_MAX_CARDS 2 7304 7305 static void probe_graphics(void) 7306 { 7307 char path[PATH_MAX]; 7308 int i; 7309 7310 /* Xe graphics sysfs knobs */ 7311 if (!access("/sys/class/drm/card0/device/tile0/gt0/gtidle/idle_residency_ms", R_OK)) { 7312 FILE *fp; 7313 char buf[8]; 7314 bool gt0_is_gt; 7315 7316 fp = fopen("/sys/class/drm/card0/device/tile0/gt0/gtidle/name", "r"); 7317 if (!fp) 7318 goto next; 7319 7320 if (!fread(buf, sizeof(char), 7, fp)) { 7321 fclose(fp); 7322 goto next; 7323 } 7324 fclose(fp); 7325 7326 if (!strncmp(buf, "gt0-rc", strlen("gt0-rc"))) 7327 gt0_is_gt = true; 7328 else if (!strncmp(buf, "gt0-mc", strlen("gt0-mc"))) 7329 gt0_is_gt = false; 7330 else 7331 goto next; 7332 7333 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt0/gtidle/idle_residency_ms", gt0_is_gt ? GFX_rc6 : SAM_mc6); 7334 7335 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt0/freq0/cur_freq", gt0_is_gt ? GFX_MHz : SAM_MHz); 7336 7337 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt0/freq0/act_freq", gt0_is_gt ? GFX_ACTMHz : SAM_ACTMHz); 7338 7339 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt1/gtidle/idle_residency_ms", gt0_is_gt ? SAM_mc6 : GFX_rc6); 7340 7341 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt1/freq0/cur_freq", gt0_is_gt ? SAM_MHz : GFX_MHz); 7342 7343 set_graphics_fp("/sys/class/drm/card0/device/tile0/gt1/freq0/act_freq", gt0_is_gt ? SAM_ACTMHz : GFX_ACTMHz); 7344 7345 goto end; 7346 } 7347 7348 next: 7349 /* New i915 graphics sysfs knobs */ 7350 for (i = 0; i < GFX_MAX_CARDS; i++) { 7351 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt0/rc6_residency_ms", i); 7352 if (!access(path, R_OK)) 7353 break; 7354 } 7355 7356 if (i == GFX_MAX_CARDS) 7357 goto legacy_i915; 7358 7359 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt0/rc6_residency_ms", i); 7360 set_graphics_fp(path, GFX_rc6); 7361 7362 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt0/rps_cur_freq_mhz", i); 7363 set_graphics_fp(path, GFX_MHz); 7364 7365 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt0/rps_act_freq_mhz", i); 7366 set_graphics_fp(path, GFX_ACTMHz); 7367 7368 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt1/rc6_residency_ms", i); 7369 set_graphics_fp(path, SAM_mc6); 7370 7371 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt1/rps_cur_freq_mhz", i); 7372 set_graphics_fp(path, SAM_MHz); 7373 7374 snprintf(path, PATH_MAX, "/sys/class/drm/card%d/gt/gt1/rps_act_freq_mhz", i); 7375 set_graphics_fp(path, SAM_ACTMHz); 7376 7377 goto end; 7378 7379 legacy_i915: 7380 /* Fall back to traditional i915 graphics sysfs knobs */ 7381 set_graphics_fp("/sys/class/drm/card0/power/rc6_residency_ms", GFX_rc6); 7382 7383 set_graphics_fp("/sys/class/drm/card0/gt_cur_freq_mhz", GFX_MHz); 7384 if (!gfx_info[GFX_MHz].fp) 7385 set_graphics_fp("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", GFX_MHz); 7386 7387 set_graphics_fp("/sys/class/drm/card0/gt_act_freq_mhz", GFX_ACTMHz); 7388 if (!gfx_info[GFX_ACTMHz].fp) 7389 set_graphics_fp("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", GFX_ACTMHz); 7390 7391 end: 7392 if (gfx_info[GFX_rc6].fp) 7393 BIC_PRESENT(BIC_GFX_rc6); 7394 if (gfx_info[GFX_MHz].fp) 7395 BIC_PRESENT(BIC_GFXMHz); 7396 if (gfx_info[GFX_ACTMHz].fp) 7397 BIC_PRESENT(BIC_GFXACTMHz); 7398 if (gfx_info[SAM_mc6].fp) 7399 BIC_PRESENT(BIC_SAM_mc6); 7400 if (gfx_info[SAM_MHz].fp) 7401 BIC_PRESENT(BIC_SAMMHz); 7402 if (gfx_info[SAM_ACTMHz].fp) 7403 BIC_PRESENT(BIC_SAMACTMHz); 7404 } 7405 7406 static void dump_sysfs_cstate_config(void) 7407 { 7408 char path[64]; 7409 char name_buf[16]; 7410 char desc[64]; 7411 FILE *input; 7412 int state; 7413 char *sp; 7414 7415 if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 7416 fprintf(outf, "cpuidle not loaded\n"); 7417 return; 7418 } 7419 7420 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 7421 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 7422 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 7423 7424 for (state = 0; state < 10; ++state) { 7425 7426 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", master_cpu, state); 7427 input = fopen(path, "r"); 7428 if (input == NULL) 7429 continue; 7430 if (!fgets(name_buf, sizeof(name_buf), input)) 7431 err(1, "%s: failed to read file", path); 7432 7433 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 7434 sp = strchr(name_buf, '-'); 7435 if (!sp) 7436 sp = strchrnul(name_buf, '\n'); 7437 *sp = '\0'; 7438 fclose(input); 7439 7440 remove_underbar(name_buf); 7441 7442 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", master_cpu, state); 7443 input = fopen(path, "r"); 7444 if (input == NULL) 7445 continue; 7446 if (!fgets(desc, sizeof(desc), input)) 7447 err(1, "%s: failed to read file", path); 7448 7449 fprintf(outf, "cpu%d: %s: %s", master_cpu, name_buf, desc); 7450 fclose(input); 7451 } 7452 } 7453 7454 static void dump_sysfs_pstate_config(void) 7455 { 7456 char path[64]; 7457 char driver_buf[64]; 7458 char governor_buf[64]; 7459 FILE *input; 7460 int turbo; 7461 7462 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", master_cpu); 7463 input = fopen(path, "r"); 7464 if (input == NULL) { 7465 fprintf(outf, "NSFOD %s\n", path); 7466 return; 7467 } 7468 if (!fgets(driver_buf, sizeof(driver_buf), input)) 7469 err(1, "%s: failed to read file", path); 7470 fclose(input); 7471 7472 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", master_cpu); 7473 input = fopen(path, "r"); 7474 if (input == NULL) { 7475 fprintf(outf, "NSFOD %s\n", path); 7476 return; 7477 } 7478 if (!fgets(governor_buf, sizeof(governor_buf), input)) 7479 err(1, "%s: failed to read file", path); 7480 fclose(input); 7481 7482 fprintf(outf, "cpu%d: cpufreq driver: %s", master_cpu, driver_buf); 7483 fprintf(outf, "cpu%d: cpufreq governor: %s", master_cpu, governor_buf); 7484 7485 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 7486 input = fopen(path, "r"); 7487 if (input != NULL) { 7488 if (fscanf(input, "%d", &turbo) != 1) 7489 err(1, "%s: failed to parse number from file", path); 7490 fprintf(outf, "cpufreq boost: %d\n", turbo); 7491 fclose(input); 7492 } 7493 7494 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 7495 input = fopen(path, "r"); 7496 if (input != NULL) { 7497 if (fscanf(input, "%d", &turbo) != 1) 7498 err(1, "%s: failed to parse number from file", path); 7499 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 7500 fclose(input); 7501 } 7502 } 7503 7504 /* 7505 * print_epb() 7506 * Decode the ENERGY_PERF_BIAS MSR 7507 */ 7508 int print_epb(PER_THREAD_PARAMS) 7509 { 7510 char *epb_string; 7511 int cpu, epb; 7512 7513 UNUSED(c); 7514 UNUSED(p); 7515 7516 if (!has_epb) 7517 return 0; 7518 7519 cpu = t->cpu_id; 7520 7521 /* EPB is per-package */ 7522 if (!is_cpu_first_thread_in_package(t, c, p)) 7523 return 0; 7524 7525 if (cpu_migrate(cpu)) { 7526 fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 7527 return -1; 7528 } 7529 7530 epb = get_epb(cpu); 7531 if (epb < 0) 7532 return 0; 7533 7534 switch (epb) { 7535 case ENERGY_PERF_BIAS_PERFORMANCE: 7536 epb_string = "performance"; 7537 break; 7538 case ENERGY_PERF_BIAS_NORMAL: 7539 epb_string = "balanced"; 7540 break; 7541 case ENERGY_PERF_BIAS_POWERSAVE: 7542 epb_string = "powersave"; 7543 break; 7544 default: 7545 epb_string = "custom"; 7546 break; 7547 } 7548 fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 7549 7550 return 0; 7551 } 7552 7553 /* 7554 * print_hwp() 7555 * Decode the MSR_HWP_CAPABILITIES 7556 */ 7557 int print_hwp(PER_THREAD_PARAMS) 7558 { 7559 unsigned long long msr; 7560 int cpu; 7561 7562 UNUSED(c); 7563 UNUSED(p); 7564 7565 if (no_msr) 7566 return 0; 7567 7568 if (!has_hwp) 7569 return 0; 7570 7571 cpu = t->cpu_id; 7572 7573 /* MSR_HWP_CAPABILITIES is per-package */ 7574 if (!is_cpu_first_thread_in_package(t, c, p)) 7575 return 0; 7576 7577 if (cpu_migrate(cpu)) { 7578 fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 7579 return -1; 7580 } 7581 7582 get_msr(cpu, MSR_PM_ENABLE, &msr); 7583 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 7584 7585 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 7586 if ((msr & (1 << 0)) == 0) 7587 return 0; 7588 7589 get_msr(cpu, MSR_HWP_CAPABILITIES, &msr); 7590 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 7591 "(high %d guar %d eff %d low %d)\n", 7592 cpu, msr, (unsigned int)HWP_HIGHEST_PERF(msr), (unsigned int)HWP_GUARANTEED_PERF(msr), (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 7593 7594 get_msr(cpu, MSR_HWP_REQUEST, &msr); 7595 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 7596 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 7597 cpu, msr, 7598 (unsigned int)(((msr) >> 0) & 0xff), 7599 (unsigned int)(((msr) >> 8) & 0xff), 7600 (unsigned int)(((msr) >> 16) & 0xff), (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 7601 7602 if (has_hwp_pkg) { 7603 get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr); 7604 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 7605 "(min %d max %d des %d epp 0x%x window 0x%x)\n", 7606 cpu, msr, 7607 (unsigned int)(((msr) >> 0) & 0xff), 7608 (unsigned int)(((msr) >> 8) & 0xff), (unsigned int)(((msr) >> 16) & 0xff), (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 7609 } 7610 if (has_hwp_notify) { 7611 get_msr(cpu, MSR_HWP_INTERRUPT, &msr); 7612 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 7613 } 7614 get_msr(cpu, MSR_HWP_STATUS, &msr); 7615 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x4) ? "" : "No-"); 7616 7617 return 0; 7618 } 7619 7620 /* 7621 * print_perf_limit() 7622 */ 7623 int print_perf_limit(PER_THREAD_PARAMS) 7624 { 7625 unsigned long long msr; 7626 int cpu; 7627 7628 UNUSED(c); 7629 UNUSED(p); 7630 7631 if (no_msr) 7632 return 0; 7633 7634 cpu = t->cpu_id; 7635 7636 /* per-package */ 7637 if (!is_cpu_first_thread_in_package(t, c, p)) 7638 return 0; 7639 7640 if (cpu_migrate(cpu)) { 7641 fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 7642 return -1; 7643 } 7644 7645 if (platform->plr_msrs & PLR_CORE) { 7646 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 7647 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 7648 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 7649 (msr & 1 << 15) ? "bit15, " : "", 7650 (msr & 1 << 14) ? "bit14, " : "", 7651 (msr & 1 << 13) ? "Transitions, " : "", 7652 (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 7653 (msr & 1 << 11) ? "PkgPwrL2, " : "", 7654 (msr & 1 << 10) ? "PkgPwrL1, " : "", 7655 (msr & 1 << 9) ? "CorePwr, " : "", 7656 (msr & 1 << 8) ? "Amps, " : "", 7657 (msr & 1 << 6) ? "VR-Therm, " : "", 7658 (msr & 1 << 5) ? "Auto-HWP, " : "", 7659 (msr & 1 << 4) ? "Graphics, " : "", (msr & 1 << 2) ? "bit2, " : "", (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 7660 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 7661 (msr & 1 << 31) ? "bit31, " : "", 7662 (msr & 1 << 30) ? "bit30, " : "", 7663 (msr & 1 << 29) ? "Transitions, " : "", 7664 (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 7665 (msr & 1 << 27) ? "PkgPwrL2, " : "", 7666 (msr & 1 << 26) ? "PkgPwrL1, " : "", 7667 (msr & 1 << 25) ? "CorePwr, " : "", 7668 (msr & 1 << 24) ? "Amps, " : "", 7669 (msr & 1 << 22) ? "VR-Therm, " : "", 7670 (msr & 1 << 21) ? "Auto-HWP, " : "", 7671 (msr & 1 << 20) ? "Graphics, " : "", (msr & 1 << 18) ? "bit18, " : "", (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 7672 7673 } 7674 if (platform->plr_msrs & PLR_GFX) { 7675 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 7676 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 7677 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 7678 (msr & 1 << 0) ? "PROCHOT, " : "", 7679 (msr & 1 << 1) ? "ThermStatus, " : "", 7680 (msr & 1 << 4) ? "Graphics, " : "", 7681 (msr & 1 << 6) ? "VR-Therm, " : "", 7682 (msr & 1 << 8) ? "Amps, " : "", (msr & 1 << 9) ? "GFXPwr, " : "", (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 7683 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 7684 (msr & 1 << 16) ? "PROCHOT, " : "", 7685 (msr & 1 << 17) ? "ThermStatus, " : "", 7686 (msr & 1 << 20) ? "Graphics, " : "", 7687 (msr & 1 << 22) ? "VR-Therm, " : "", 7688 (msr & 1 << 24) ? "Amps, " : "", (msr & 1 << 25) ? "GFXPwr, " : "", (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 7689 } 7690 if (platform->plr_msrs & PLR_RING) { 7691 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 7692 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 7693 fprintf(outf, " (Active: %s%s%s%s%s%s)", 7694 (msr & 1 << 0) ? "PROCHOT, " : "", 7695 (msr & 1 << 1) ? "ThermStatus, " : "", 7696 (msr & 1 << 6) ? "VR-Therm, " : "", (msr & 1 << 8) ? "Amps, " : "", (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 7697 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 7698 (msr & 1 << 16) ? "PROCHOT, " : "", 7699 (msr & 1 << 17) ? "ThermStatus, " : "", 7700 (msr & 1 << 22) ? "VR-Therm, " : "", (msr & 1 << 24) ? "Amps, " : "", (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 7701 } 7702 return 0; 7703 } 7704 7705 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 7706 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 7707 7708 double get_quirk_tdp(void) 7709 { 7710 if (platform->rapl_quirk_tdp) 7711 return platform->rapl_quirk_tdp; 7712 7713 return 135.0; 7714 } 7715 7716 double get_tdp_intel(void) 7717 { 7718 unsigned long long msr; 7719 7720 if (valid_rapl_msrs & RAPL_PKG_POWER_INFO) { 7721 get_msr(master_cpu, MSR_PKG_POWER_INFO, &msr); 7722 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 7723 } 7724 return get_quirk_tdp(); 7725 } 7726 7727 double get_tdp_amd(void) 7728 { 7729 return get_quirk_tdp(); 7730 } 7731 7732 void rapl_probe_intel(void) 7733 { 7734 unsigned long long msr; 7735 unsigned int time_unit; 7736 double tdp; 7737 7738 if (rapl_joules) { 7739 CLR_BIC(BIC_SysWatt, &bic_enabled); 7740 CLR_BIC(BIC_PkgWatt, &bic_enabled); 7741 CLR_BIC(BIC_CorWatt, &bic_enabled); 7742 CLR_BIC(BIC_RAMWatt, &bic_enabled); 7743 CLR_BIC(BIC_GFXWatt, &bic_enabled); 7744 } else { 7745 CLR_BIC(BIC_Sys_J, &bic_enabled); 7746 CLR_BIC(BIC_Pkg_J, &bic_enabled); 7747 CLR_BIC(BIC_Cor_J, &bic_enabled); 7748 CLR_BIC(BIC_RAM_J, &bic_enabled); 7749 CLR_BIC(BIC_GFX_J, &bic_enabled); 7750 } 7751 7752 if (!valid_rapl_msrs || no_msr) 7753 return; 7754 7755 if (!(valid_rapl_msrs & RAPL_PKG_PERF_STATUS)) 7756 CLR_BIC(BIC_PKG__, &bic_enabled); 7757 if (!(valid_rapl_msrs & RAPL_DRAM_PERF_STATUS)) 7758 CLR_BIC(BIC_RAM__, &bic_enabled); 7759 7760 /* units on package 0, verify later other packages match */ 7761 get_msr(master_cpu, MSR_RAPL_POWER_UNIT, &msr); 7762 7763 rapl_power_units = 1.0 / (1 << (msr & 0xF)); 7764 if (platform->has_rapl_divisor) 7765 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 7766 else 7767 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 7768 7769 if (platform->has_fixed_rapl_unit) 7770 rapl_dram_energy_units = (15.3 / 1000000); 7771 else 7772 rapl_dram_energy_units = rapl_energy_units; 7773 7774 if (platform->has_fixed_rapl_psys_unit) 7775 rapl_psys_energy_units = 1.0; 7776 else 7777 rapl_psys_energy_units = rapl_energy_units; 7778 7779 time_unit = msr >> 16 & 0xF; 7780 if (time_unit == 0) 7781 time_unit = 0xA; 7782 7783 rapl_time_units = 1.0 / (1 << (time_unit)); 7784 7785 tdp = get_tdp_intel(); 7786 7787 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 7788 if (!quiet) 7789 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 7790 } 7791 7792 void rapl_probe_amd(void) 7793 { 7794 unsigned long long msr; 7795 double tdp; 7796 7797 if (rapl_joules) { 7798 CLR_BIC(BIC_SysWatt, &bic_enabled); 7799 CLR_BIC(BIC_CorWatt, &bic_enabled); 7800 } else { 7801 CLR_BIC(BIC_Pkg_J, &bic_enabled); 7802 CLR_BIC(BIC_Cor_J, &bic_enabled); 7803 } 7804 7805 if (!valid_rapl_msrs || no_msr) 7806 return; 7807 7808 get_msr(master_cpu, MSR_RAPL_PWR_UNIT, &msr); 7809 rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 7810 rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 7811 rapl_power_units = ldexp(1.0, -(msr & 0xf)); 7812 7813 tdp = get_tdp_amd(); 7814 7815 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 7816 if (!quiet) 7817 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 7818 } 7819 7820 void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 7821 { 7822 fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n", 7823 cpu, label, 7824 ((msr >> 15) & 1) ? "EN" : "DIS", 7825 ((msr >> 0) & 0x7FFF) * rapl_power_units, (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, (((msr >> 16) & 1) ? "EN" : "DIS")); 7826 7827 return; 7828 } 7829 7830 static int fread_int(char *path, int *val) 7831 { 7832 FILE *filep; 7833 int ret; 7834 7835 filep = fopen(path, "r"); 7836 if (!filep) 7837 return -1; 7838 7839 ret = fscanf(filep, "%d", val); 7840 fclose(filep); 7841 return ret; 7842 } 7843 7844 static int fread_ull(char *path, unsigned long long *val) 7845 { 7846 FILE *filep; 7847 int ret; 7848 7849 filep = fopen(path, "r"); 7850 if (!filep) 7851 return -1; 7852 7853 ret = fscanf(filep, "%llu", val); 7854 fclose(filep); 7855 return ret; 7856 } 7857 7858 static int fread_str(char *path, char *buf, int size) 7859 { 7860 FILE *filep; 7861 int ret; 7862 char *cp; 7863 7864 filep = fopen(path, "r"); 7865 if (!filep) 7866 return -1; 7867 7868 ret = fread(buf, 1, size, filep); 7869 fclose(filep); 7870 7871 /* replace '\n' with '\0' */ 7872 cp = strchr(buf, '\n'); 7873 if (cp != NULL) 7874 *cp = '\0'; 7875 7876 return ret; 7877 } 7878 7879 #define PATH_RAPL_SYSFS "/sys/class/powercap" 7880 7881 static int dump_one_domain(char *domain_path) 7882 { 7883 char path[PATH_MAX]; 7884 char str[PATH_MAX]; 7885 unsigned long long val; 7886 int constraint; 7887 int enable; 7888 int ret; 7889 7890 snprintf(path, PATH_MAX, "%s/name", domain_path); 7891 ret = fread_str(path, str, PATH_MAX); 7892 if (ret <= 0) 7893 return -1; 7894 7895 fprintf(outf, "%s: %s", domain_path + strlen(PATH_RAPL_SYSFS) + 1, str); 7896 7897 snprintf(path, PATH_MAX, "%s/enabled", domain_path); 7898 ret = fread_int(path, &enable); 7899 if (ret <= 0) 7900 return -1; 7901 7902 if (!enable) { 7903 fputs(" disabled\n", outf); 7904 return 0; 7905 } 7906 7907 for (constraint = 0;; constraint++) { 7908 snprintf(path, PATH_MAX, "%s/constraint_%d_time_window_us", domain_path, constraint); 7909 ret = fread_ull(path, &val); 7910 if (ret <= 0) 7911 break; 7912 7913 if (val > 1000000) 7914 fprintf(outf, " %0.1fs", (double)val / 1000000); 7915 else if (val > 1000) 7916 fprintf(outf, " %0.1fms", (double)val / 1000); 7917 else 7918 fprintf(outf, " %0.1fus", (double)val); 7919 7920 snprintf(path, PATH_MAX, "%s/constraint_%d_power_limit_uw", domain_path, constraint); 7921 ret = fread_ull(path, &val); 7922 if (ret > 0 && val) 7923 fprintf(outf, ":%lluW", val / 1000000); 7924 7925 snprintf(path, PATH_MAX, "%s/constraint_%d_max_power_uw", domain_path, constraint); 7926 ret = fread_ull(path, &val); 7927 if (ret > 0 && val) 7928 fprintf(outf, ",max:%lluW", val / 1000000); 7929 } 7930 fputc('\n', outf); 7931 7932 return 0; 7933 } 7934 7935 static int print_rapl_sysfs(void) 7936 { 7937 DIR *dir, *cdir; 7938 struct dirent *entry, *centry; 7939 char path[PATH_MAX]; 7940 char str[PATH_MAX]; 7941 7942 if ((dir = opendir(PATH_RAPL_SYSFS)) == NULL) { 7943 warn("open %s failed", PATH_RAPL_SYSFS); 7944 return 1; 7945 } 7946 7947 while ((entry = readdir(dir)) != NULL) { 7948 if (strlen(entry->d_name) > 100) 7949 continue; 7950 7951 if (strncmp(entry->d_name, "intel-rapl", strlen("intel-rapl"))) 7952 continue; 7953 7954 snprintf(path, PATH_MAX, "%s/%s/name", PATH_RAPL_SYSFS, entry->d_name); 7955 7956 /* Parse top level domains first, including package and psys */ 7957 fread_str(path, str, PATH_MAX); 7958 if (strncmp(str, "package", strlen("package")) && strncmp(str, "psys", strlen("psys"))) 7959 continue; 7960 7961 snprintf(path, PATH_MAX, "%s/%s", PATH_RAPL_SYSFS, entry->d_name); 7962 if ((cdir = opendir(path)) == NULL) { 7963 warn("%s", path); 7964 return 1; 7965 } 7966 7967 dump_one_domain(path); 7968 7969 while ((centry = readdir(cdir)) != NULL) { 7970 if (strncmp(centry->d_name, "intel-rapl", strlen("intel-rapl"))) 7971 continue; 7972 snprintf(path, PATH_MAX, "%s/%s/%s", PATH_RAPL_SYSFS, entry->d_name, centry->d_name); 7973 dump_one_domain(path); 7974 } 7975 closedir(cdir); 7976 } 7977 7978 closedir(dir); 7979 return 0; 7980 } 7981 7982 int print_rapl(PER_THREAD_PARAMS) 7983 { 7984 unsigned long long msr; 7985 const char *msr_name; 7986 int cpu; 7987 7988 UNUSED(c); 7989 UNUSED(p); 7990 7991 if (!valid_rapl_msrs) 7992 return 0; 7993 7994 /* RAPL counters are per package, so print only for 1st thread/package */ 7995 if (!is_cpu_first_thread_in_package(t, c, p)) 7996 return 0; 7997 7998 cpu = t->cpu_id; 7999 if (cpu_migrate(cpu)) { 8000 fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 8001 return -1; 8002 } 8003 8004 if (valid_rapl_msrs & RAPL_AMD_F17H) { 8005 msr_name = "MSR_RAPL_PWR_UNIT"; 8006 get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr); 8007 } else { 8008 msr_name = "MSR_RAPL_POWER_UNIT"; 8009 get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr); 8010 } 8011 8012 fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, rapl_power_units, rapl_energy_units, rapl_time_units); 8013 8014 if (valid_rapl_msrs & RAPL_PKG_POWER_INFO) { 8015 8016 get_msr(cpu, MSR_PKG_POWER_INFO, &msr); 8017 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 8018 cpu, msr, 8019 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 8020 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 8021 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 8022 8023 } 8024 if (valid_rapl_msrs & RAPL_PKG) { 8025 8026 get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr); 8027 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 8028 8029 print_power_limit_msr(cpu, msr, "PKG Limit #1"); 8030 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n", 8031 cpu, 8032 ((msr >> 47) & 1) ? "EN" : "DIS", 8033 ((msr >> 32) & 0x7FFF) * rapl_power_units, (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, ((msr >> 48) & 1) ? "EN" : "DIS"); 8034 8035 get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr); 8036 fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr); 8037 fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n", cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN"); 8038 } 8039 8040 if (valid_rapl_msrs & RAPL_DRAM_POWER_INFO) { 8041 get_msr(cpu, MSR_DRAM_POWER_INFO, &msr); 8042 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 8043 cpu, msr, 8044 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 8045 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 8046 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 8047 } 8048 if (valid_rapl_msrs & RAPL_DRAM) { 8049 get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr); 8050 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 8051 8052 print_power_limit_msr(cpu, msr, "DRAM Limit"); 8053 } 8054 if (valid_rapl_msrs & RAPL_CORE_POLICY) { 8055 get_msr(cpu, MSR_PP0_POLICY, &msr); 8056 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 8057 } 8058 if (valid_rapl_msrs & RAPL_CORE_POWER_LIMIT) { 8059 get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr); 8060 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 8061 print_power_limit_msr(cpu, msr, "Cores Limit"); 8062 } 8063 if (valid_rapl_msrs & RAPL_GFX) { 8064 get_msr(cpu, MSR_PP1_POLICY, &msr); 8065 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 8066 8067 get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr); 8068 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 8069 print_power_limit_msr(cpu, msr, "GFX Limit"); 8070 } 8071 return 0; 8072 } 8073 8074 /* 8075 * probe_rapl_msrs 8076 * 8077 * initialize global valid_rapl_msrs to platform->plat_rapl_msrs 8078 * only if PKG_ENERGY counter is enumerated and reads non-zero 8079 */ 8080 void probe_rapl_msrs(void) 8081 { 8082 off_t offset; 8083 unsigned long long msr_value; 8084 8085 if (no_msr) 8086 return; 8087 8088 if ((platform->plat_rapl_msrs & (RAPL_PKG | RAPL_AMD_F17H)) == 0) 8089 return; 8090 8091 offset = idx_to_offset(IDX_PKG_ENERGY); 8092 if (offset < 0) 8093 return; 8094 8095 get_msr(master_cpu, offset, &msr_value); 8096 if (msr_value == 0) { 8097 if (debug) 8098 fprintf(outf, "RAPL_PKG_ENERGY MSR(0x%llx) == ZERO: disabling all RAPL MSRs\n", (unsigned long long)offset); 8099 return; 8100 } 8101 8102 valid_rapl_msrs = platform->plat_rapl_msrs; /* success */ 8103 } 8104 8105 /* 8106 * probe_rapl() 8107 * 8108 * sets rapl_power_units, rapl_energy_units, rapl_time_units 8109 */ 8110 void probe_rapl(void) 8111 { 8112 probe_rapl_msrs(); 8113 8114 if (genuine_intel) 8115 rapl_probe_intel(); 8116 if (authentic_amd || hygon_genuine) 8117 rapl_probe_amd(); 8118 8119 if (quiet) 8120 return; 8121 8122 print_rapl_sysfs(); 8123 8124 if (!valid_rapl_msrs || no_msr) 8125 return; 8126 8127 for_all_cpus(print_rapl, ODD_COUNTERS); 8128 } 8129 8130 /* 8131 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 8132 * the Thermal Control Circuit (TCC) activates. 8133 * This is usually equal to tjMax. 8134 * 8135 * Older processors do not have this MSR, so there we guess, 8136 * but also allow cmdline over-ride with -T. 8137 * 8138 * Several MSR temperature values are in units of degrees-C 8139 * below this value, including the Digital Thermal Sensor (DTS), 8140 * Package Thermal Management Sensor (PTM), and thermal event thresholds. 8141 */ 8142 int set_temperature_target(PER_THREAD_PARAMS) 8143 { 8144 unsigned long long msr; 8145 unsigned int tcc_default, tcc_offset; 8146 int cpu; 8147 8148 UNUSED(c); 8149 UNUSED(p); 8150 8151 /* tj_max is used only for dts or ptm */ 8152 if (!(do_dts || do_ptm)) 8153 return 0; 8154 8155 /* this is a per-package concept */ 8156 if (!is_cpu_first_thread_in_package(t, c, p)) 8157 return 0; 8158 8159 cpu = t->cpu_id; 8160 if (cpu_migrate(cpu)) { 8161 fprintf(outf, "Could not migrate to CPU %d\n", cpu); 8162 return -1; 8163 } 8164 8165 if (tj_max_override != 0) { 8166 tj_max = tj_max_override; 8167 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 8168 return 0; 8169 } 8170 8171 /* Temperature Target MSR is Nehalem and newer only */ 8172 if (!platform->has_nhm_msrs || no_msr) 8173 goto guess; 8174 8175 get_msr(master_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr); 8176 tcc_default = (msr >> 16) & 0xFF; 8177 8178 if (!quiet) { 8179 int bits = platform->tcc_offset_bits; 8180 unsigned long long enabled = 0; 8181 8182 if (bits) { 8183 get_msr(master_cpu, MSR_PLATFORM_INFO, &enabled); 8184 enabled = (enabled >> 30) & 1; 8185 } 8186 8187 if (bits && enabled) { 8188 tcc_offset = (msr >> 24) & GENMASK(bits - 1, 0); 8189 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 8190 } else { 8191 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 8192 } 8193 } 8194 8195 if (!tcc_default) 8196 goto guess; 8197 8198 tj_max = tcc_default; 8199 8200 return 0; 8201 8202 guess: 8203 tj_max = TJMAX_DEFAULT; 8204 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 8205 8206 return 0; 8207 } 8208 8209 int print_thermal(PER_THREAD_PARAMS) 8210 { 8211 unsigned long long msr; 8212 unsigned int dts, dts2; 8213 int cpu; 8214 8215 UNUSED(c); 8216 UNUSED(p); 8217 8218 if (no_msr) 8219 return 0; 8220 8221 if (!(do_dts || do_ptm)) 8222 return 0; 8223 8224 cpu = t->cpu_id; 8225 8226 /* DTS is per-core, no need to print for each thread */ 8227 if (!is_cpu_first_thread_in_core(t, c)) 8228 return 0; 8229 8230 if (cpu_migrate(cpu)) { 8231 fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 8232 return -1; 8233 } 8234 8235 if (do_ptm && is_cpu_first_core_in_package(t, p)) { 8236 get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr); 8237 dts = (msr >> 16) & 0x7F; 8238 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 8239 8240 get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr); 8241 dts = (msr >> 16) & 0x7F; 8242 dts2 = (msr >> 8) & 0x7F; 8243 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", cpu, msr, tj_max - dts, tj_max - dts2); 8244 } 8245 8246 if (do_dts && debug) { 8247 unsigned int resolution; 8248 8249 get_msr(cpu, MSR_IA32_THERM_STATUS, &msr); 8250 dts = (msr >> 16) & 0x7F; 8251 resolution = (msr >> 27) & 0xF; 8252 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", cpu, msr, tj_max - dts, resolution); 8253 8254 get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr); 8255 dts = (msr >> 16) & 0x7F; 8256 dts2 = (msr >> 8) & 0x7F; 8257 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", cpu, msr, tj_max - dts, tj_max - dts2); 8258 } 8259 8260 return 0; 8261 } 8262 8263 void probe_thermal(void) 8264 { 8265 if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK)) 8266 BIC_PRESENT(BIC_CORE_THROT_CNT); 8267 else 8268 BIC_NOT_PRESENT(BIC_CORE_THROT_CNT); 8269 8270 for_all_cpus(set_temperature_target, ODD_COUNTERS); 8271 8272 if (quiet) 8273 return; 8274 8275 for_all_cpus(print_thermal, ODD_COUNTERS); 8276 } 8277 8278 int get_cpu_type(PER_THREAD_PARAMS) 8279 { 8280 unsigned int eax, ebx, ecx, edx; 8281 8282 UNUSED(c); 8283 UNUSED(p); 8284 8285 if (!genuine_intel) 8286 return 0; 8287 8288 if (cpu_migrate(t->cpu_id)) { 8289 fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 8290 return -1; 8291 } 8292 8293 if (max_level < 0x1a) 8294 return 0; 8295 8296 __cpuid(0x1a, eax, ebx, ecx, edx); 8297 eax = (eax >> 24) & 0xFF; 8298 if (eax == 0x20) 8299 t->is_atom = true; 8300 return 0; 8301 } 8302 8303 void decode_feature_control_msr(void) 8304 { 8305 unsigned long long msr; 8306 8307 if (no_msr) 8308 return; 8309 8310 get_msr(master_cpu, MSR_IA32_FEAT_CTL, &msr); 8311 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", master_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 8312 } 8313 8314 void decode_misc_enable_msr(void) 8315 { 8316 unsigned long long msr; 8317 8318 if (no_msr) 8319 return; 8320 8321 if (!genuine_intel) 8322 return; 8323 8324 get_msr(master_cpu, MSR_IA32_MISC_ENABLE, &msr); 8325 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 8326 master_cpu, msr, 8327 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 8328 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 8329 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 8330 } 8331 8332 void decode_misc_feature_control(void) 8333 { 8334 unsigned long long msr; 8335 8336 if (no_msr) 8337 return; 8338 8339 if (!platform->has_msr_misc_feature_control) 8340 return; 8341 8342 get_msr(master_cpu, MSR_MISC_FEATURE_CONTROL, &msr); 8343 fprintf(outf, 8344 "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 8345 master_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 8346 } 8347 8348 /* 8349 * Decode MSR_MISC_PWR_MGMT 8350 * 8351 * Decode the bits according to the Nehalem documentation 8352 * bit[0] seems to continue to have same meaning going forward 8353 * bit[1] less so... 8354 */ 8355 void decode_misc_pwr_mgmt_msr(void) 8356 { 8357 unsigned long long msr; 8358 8359 if (no_msr) 8360 return; 8361 8362 if (!platform->has_msr_misc_pwr_mgmt) 8363 return; 8364 8365 get_msr(master_cpu, MSR_MISC_PWR_MGMT, &msr); 8366 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 8367 master_cpu, msr, msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 8368 } 8369 8370 /* 8371 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 8372 * 8373 * This MSRs are present on Silvermont processors, 8374 * Intel Atom processor E3000 series (Baytrail), and friends. 8375 */ 8376 void decode_c6_demotion_policy_msr(void) 8377 { 8378 unsigned long long msr; 8379 8380 if (no_msr) 8381 return; 8382 8383 if (!platform->has_msr_c6_demotion_policy_config) 8384 return; 8385 8386 get_msr(master_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr); 8387 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", master_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 8388 8389 get_msr(master_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr); 8390 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", master_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 8391 } 8392 8393 void print_dev_latency(void) 8394 { 8395 char *path = "/dev/cpu_dma_latency"; 8396 int fd; 8397 int value; 8398 int retval; 8399 8400 fd = open(path, O_RDONLY); 8401 if (fd < 0) { 8402 if (debug) 8403 warnx("Read %s failed", path); 8404 return; 8405 } 8406 8407 retval = read(fd, (void *)&value, sizeof(int)); 8408 if (retval != sizeof(int)) { 8409 warn("read failed %s", path); 8410 close(fd); 8411 return; 8412 } 8413 fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 8414 8415 close(fd); 8416 } 8417 8418 static int has_perf_instr_count_access(void) 8419 { 8420 int fd; 8421 8422 if (no_perf) 8423 return 0; 8424 8425 fd = open_perf_counter(master_cpu, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS, -1, 0); 8426 if (fd != -1) 8427 close(fd); 8428 8429 if (fd == -1) 8430 warnx("Failed to access %s. Some of the counters may not be available\n" 8431 "\tRun as root to enable them or use %s to disable the access explicitly", "perf instructions retired counter", "'--hide IPC' or '--no-perf'"); 8432 8433 return (fd != -1); 8434 } 8435 8436 int add_rapl_perf_counter(int cpu, struct rapl_counter_info_t *rci, const struct rapl_counter_arch_info *cai, double *scale_, enum rapl_unit *unit_) 8437 { 8438 int ret = -1; 8439 8440 if (no_perf) 8441 return -1; 8442 8443 if (!cai->perf_name) 8444 return -1; 8445 8446 const double scale = read_perf_scale(cai->perf_subsys, cai->perf_name); 8447 8448 if (scale == 0.0) 8449 goto end; 8450 8451 const enum rapl_unit unit = read_perf_rapl_unit(cai->perf_subsys, cai->perf_name); 8452 8453 if (unit == RAPL_UNIT_INVALID) 8454 goto end; 8455 8456 const unsigned int rapl_type = read_perf_type(cai->perf_subsys); 8457 const unsigned int rapl_energy_pkg_config = read_perf_config(cai->perf_subsys, cai->perf_name); 8458 8459 ret = open_perf_counter(cpu, rapl_type, rapl_energy_pkg_config, rci->fd_perf, PERF_FORMAT_GROUP); 8460 if (ret == -1) 8461 goto end; 8462 8463 /* If it's the first counter opened, make it a group descriptor */ 8464 if (rci->fd_perf == -1) 8465 rci->fd_perf = ret; 8466 8467 *scale_ = scale; 8468 *unit_ = unit; 8469 8470 end: 8471 if (debug >= 2) 8472 fprintf(stderr, "%s: %d (cpu: %d)\n", __func__, ret, cpu); 8473 8474 return ret; 8475 } 8476 8477 int initialize_cpu_set_from_sysfs(cpu_set_t *cpu_set, const char *sysfs_path, const char *sysfs_file) 8478 { 8479 FILE *fp; 8480 char *cpuset_buf = NULL; 8481 size_t size = 0; 8482 char path[128]; 8483 int ret = -1; 8484 8485 if (snprintf(path, sizeof(path), "%s/%s", sysfs_path, sysfs_file) >= (int)sizeof(path)) 8486 err(-1, "%s %s", sysfs_path, sysfs_file); 8487 8488 fp = fopen(path, "r"); 8489 if (!fp) { 8490 warn("open %s", path); 8491 return -1; 8492 } 8493 if (getline(&cpuset_buf, &size, fp) < 0) { 8494 warn("read %s", sysfs_path); 8495 goto out; 8496 } 8497 if (parse_cpu_str(cpuset_buf, cpu_set, cpu_setsize)) { 8498 warnx("%s: cpu str malformat %s\n", sysfs_path, cpuset_buf); 8499 goto out; 8500 } 8501 ret = 0; 8502 8503 out: 8504 fclose(fp); 8505 free(cpuset_buf); 8506 return ret; 8507 } 8508 8509 void print_cpu_set(char *s, cpu_set_t *set) 8510 { 8511 int i; 8512 8513 assert(MAX_BIC < CPU_SETSIZE); 8514 8515 printf("%s:", s); 8516 8517 for (i = 0; i <= topo.max_cpu_num; ++i) 8518 if (CPU_ISSET(i, set)) 8519 printf(" %d", i); 8520 putchar('\n'); 8521 } 8522 8523 void linux_perf_init_hybrid_cpus(void) 8524 { 8525 char *perf_cpu_pcore_path = "/sys/devices/cpu_core"; 8526 char *perf_cpu_ecore_path = "/sys/devices/cpu_atom"; 8527 char *perf_cpu_lcore_path = "/sys/devices/cpu_lowpower"; 8528 char path[128]; 8529 8530 if (!access(perf_cpu_pcore_path, F_OK)) { 8531 perf_pcore_set = CPU_ALLOC((topo.max_cpu_num + 1)); 8532 if (perf_pcore_set == NULL) 8533 err(3, "CPU_ALLOC"); 8534 CPU_ZERO_S(cpu_setsize, perf_pcore_set); 8535 initialize_cpu_set_from_sysfs(perf_pcore_set, perf_cpu_pcore_path, "cpus"); 8536 if (debug) 8537 print_cpu_set("perf pcores", perf_pcore_set); 8538 sprintf(path, "%s/%s", perf_cpu_pcore_path, "type"); 8539 perf_pmu_types.pcore = snapshot_sysfs_counter(path); 8540 } 8541 8542 if (!access(perf_cpu_ecore_path, F_OK)) { 8543 perf_ecore_set = CPU_ALLOC((topo.max_cpu_num + 1)); 8544 if (perf_ecore_set == NULL) 8545 err(3, "CPU_ALLOC"); 8546 CPU_ZERO_S(cpu_setsize, perf_ecore_set); 8547 initialize_cpu_set_from_sysfs(perf_ecore_set, perf_cpu_ecore_path, "cpus"); 8548 if (debug) 8549 print_cpu_set("perf ecores", perf_ecore_set); 8550 sprintf(path, "%s/%s", perf_cpu_ecore_path, "type"); 8551 perf_pmu_types.ecore = snapshot_sysfs_counter(path); 8552 } 8553 8554 if (!access(perf_cpu_lcore_path, F_OK)) { 8555 perf_lcore_set = CPU_ALLOC((topo.max_cpu_num + 1)); 8556 if (perf_lcore_set == NULL) 8557 err(3, "CPU_ALLOC"); 8558 CPU_ZERO_S(cpu_setsize, perf_lcore_set); 8559 initialize_cpu_set_from_sysfs(perf_lcore_set, perf_cpu_lcore_path, "cpus"); 8560 if (debug) 8561 print_cpu_set("perf lcores", perf_lcore_set); 8562 sprintf(path, "%s/%s", perf_cpu_lcore_path, "type"); 8563 perf_pmu_types.lcore = snapshot_sysfs_counter(path); 8564 } 8565 } 8566 8567 /* 8568 * Linux-perf related initialization 8569 */ 8570 void linux_perf_init(void) 8571 { 8572 char path[128]; 8573 char *perf_cpu_path = "/sys/devices/cpu"; 8574 8575 if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 8576 return; 8577 8578 if (!access(perf_cpu_path, F_OK)) { 8579 sprintf(path, "%s/%s", perf_cpu_path, "type"); 8580 perf_pmu_types.uniform = snapshot_sysfs_counter(path); 8581 } else { 8582 linux_perf_init_hybrid_cpus(); 8583 } 8584 8585 if (BIC_IS_ENABLED(BIC_IPC) && cpuid_has_aperf_mperf) { 8586 fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 8587 if (fd_instr_count_percpu == NULL) 8588 err(-1, "calloc fd_instr_count_percpu"); 8589 } 8590 if (BIC_IS_ENABLED(BIC_LLC_MRPS) || BIC_IS_ENABLED(BIC_LLC_HIT)) { 8591 fd_llc_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 8592 if (fd_llc_percpu == NULL) 8593 err(-1, "calloc fd_llc_percpu"); 8594 } 8595 if (BIC_IS_ENABLED(BIC_L2_MRPS) || BIC_IS_ENABLED(BIC_L2_HIT)) { 8596 fd_l2_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 8597 if (fd_l2_percpu == NULL) 8598 err(-1, "calloc fd_l2_percpu"); 8599 } 8600 } 8601 8602 void rapl_perf_init(void) 8603 { 8604 const unsigned int num_domains = get_rapl_num_domains(); 8605 bool *domain_visited = calloc(num_domains, sizeof(bool)); 8606 if (!domain_visited) 8607 err(-1, "calloc domain_visited"); 8608 8609 rapl_counter_info_perdomain = calloc(num_domains, sizeof(*rapl_counter_info_perdomain)); 8610 if (rapl_counter_info_perdomain == NULL) 8611 err(-1, "calloc rapl_counter_info_percpu"); 8612 rapl_counter_info_perdomain_size = num_domains; 8613 8614 /* 8615 * Initialize rapl_counter_info_percpu 8616 */ 8617 for (unsigned int domain_id = 0; domain_id < num_domains; ++domain_id) { 8618 struct rapl_counter_info_t *rci = &rapl_counter_info_perdomain[domain_id]; 8619 8620 rci->fd_perf = -1; 8621 for (size_t i = 0; i < NUM_RAPL_COUNTERS; ++i) { 8622 rci->data[i] = 0; 8623 rci->source[i] = COUNTER_SOURCE_NONE; 8624 } 8625 } 8626 8627 /* 8628 * Open/probe the counters 8629 * If can't get it via perf, fallback to MSR 8630 */ 8631 for (size_t i = 0; i < ARRAY_SIZE(rapl_counter_arch_infos); ++i) { 8632 8633 const struct rapl_counter_arch_info *const cai = &rapl_counter_arch_infos[i]; 8634 bool has_counter = 0; 8635 double scale; 8636 enum rapl_unit unit; 8637 unsigned int next_domain; 8638 8639 if (!BIC_IS_ENABLED(cai->bic_number)) 8640 continue; 8641 8642 memset(domain_visited, 0, num_domains * sizeof(*domain_visited)); 8643 8644 for (int cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 8645 8646 if (cpu_is_not_allowed(cpu)) 8647 continue; 8648 8649 /* Skip already seen and handled RAPL domains */ 8650 next_domain = get_rapl_domain_id(cpu); 8651 8652 assert(next_domain < num_domains); 8653 8654 if (domain_visited[next_domain]) 8655 continue; 8656 8657 domain_visited[next_domain] = 1; 8658 8659 if ((cai->flags & RAPL_COUNTER_FLAG_PLATFORM_COUNTER) && (cpu != master_cpu)) 8660 continue; 8661 8662 struct rapl_counter_info_t *rci = &rapl_counter_info_perdomain[next_domain]; 8663 8664 /* 8665 * rapl_counter_arch_infos[] can have multiple entries describing the same 8666 * counter, due to the difference from different platforms/Vendors. 8667 * E.g. rapl_counter_arch_infos[0] and rapl_counter_arch_infos[1] share the 8668 * same perf_subsys and perf_name, but with different MSR address. 8669 * rapl_counter_arch_infos[0] is for Intel and rapl_counter_arch_infos[1] 8670 * is for AMD. 8671 * In this case, it is possible that multiple rapl_counter_arch_infos[] 8672 * entries are probed just because their perf/msr is duplicate and valid. 8673 * 8674 * Thus need a check to avoid re-probe the same counters. 8675 */ 8676 if (rci->source[cai->rci_index] != COUNTER_SOURCE_NONE) 8677 break; 8678 8679 /* Use perf API for this counter */ 8680 if (add_rapl_perf_counter(cpu, rci, cai, &scale, &unit) != -1) { 8681 rci->source[cai->rci_index] = COUNTER_SOURCE_PERF; 8682 rci->scale[cai->rci_index] = scale * cai->compat_scale; 8683 rci->unit[cai->rci_index] = unit; 8684 rci->flags[cai->rci_index] = cai->flags; 8685 8686 /* Use MSR for this counter */ 8687 } else if (add_rapl_msr_counter(cpu, cai) >= 0) { 8688 rci->source[cai->rci_index] = COUNTER_SOURCE_MSR; 8689 rci->msr[cai->rci_index] = cai->msr; 8690 rci->msr_mask[cai->rci_index] = cai->msr_mask; 8691 rci->msr_shift[cai->rci_index] = cai->msr_shift; 8692 rci->unit[cai->rci_index] = RAPL_UNIT_JOULES; 8693 rci->scale[cai->rci_index] = *cai->platform_rapl_msr_scale * cai->compat_scale; 8694 rci->flags[cai->rci_index] = cai->flags; 8695 } 8696 8697 if (rci->source[cai->rci_index] != COUNTER_SOURCE_NONE) 8698 has_counter = 1; 8699 } 8700 8701 /* If any CPU has access to the counter, make it present */ 8702 if (has_counter) 8703 BIC_PRESENT(cai->bic_number); 8704 } 8705 8706 free(domain_visited); 8707 } 8708 8709 /* Assumes msr_counter_info is populated */ 8710 static int has_amperf_access(void) 8711 { 8712 return cpuid_has_aperf_mperf && msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present && msr_counter_arch_infos[MSR_ARCH_INFO_MPERF_INDEX].present; 8713 } 8714 8715 int *get_cstate_perf_group_fd(struct cstate_counter_info_t *cci, const char *group_name) 8716 { 8717 if (strcmp(group_name, "cstate_core") == 0) 8718 return &cci->fd_perf_core; 8719 8720 if (strcmp(group_name, "cstate_pkg") == 0) 8721 return &cci->fd_perf_pkg; 8722 8723 return NULL; 8724 } 8725 8726 int add_cstate_perf_counter(int cpu, struct cstate_counter_info_t *cci, const struct cstate_counter_arch_info *cai) 8727 { 8728 int ret = -1; 8729 8730 if (no_perf) 8731 return -1; 8732 8733 if (!cai->perf_name) 8734 return -1; 8735 8736 int *pfd_group = get_cstate_perf_group_fd(cci, cai->perf_subsys); 8737 8738 if (pfd_group == NULL) 8739 goto end; 8740 8741 const unsigned int type = read_perf_type(cai->perf_subsys); 8742 const unsigned int config = read_perf_config(cai->perf_subsys, cai->perf_name); 8743 8744 ret = open_perf_counter(cpu, type, config, *pfd_group, PERF_FORMAT_GROUP); 8745 8746 if (ret == -1) 8747 goto end; 8748 8749 /* If it's the first counter opened, make it a group descriptor */ 8750 if (*pfd_group == -1) 8751 *pfd_group = ret; 8752 8753 end: 8754 if (debug >= 2) 8755 fprintf(stderr, "%s: %d (cpu: %d)\n", __func__, ret, cpu); 8756 8757 return ret; 8758 } 8759 8760 int add_msr_perf_counter(int cpu, struct msr_counter_info_t *cci, const struct msr_counter_arch_info *cai) 8761 { 8762 int ret = -1; 8763 8764 if (no_perf) 8765 return -1; 8766 8767 if (!cai->perf_name) 8768 return -1; 8769 8770 const unsigned int type = read_perf_type(cai->perf_subsys); 8771 const unsigned int config = read_perf_config(cai->perf_subsys, cai->perf_name); 8772 8773 ret = open_perf_counter(cpu, type, config, cci->fd_perf, PERF_FORMAT_GROUP); 8774 8775 if (ret == -1) 8776 goto end; 8777 8778 /* If it's the first counter opened, make it a group descriptor */ 8779 if (cci->fd_perf == -1) 8780 cci->fd_perf = ret; 8781 8782 end: 8783 if (debug) 8784 fprintf(stderr, "%s: %s/%s: %d (cpu: %d)\n", __func__, cai->perf_subsys, cai->perf_name, ret, cpu); 8785 8786 return ret; 8787 } 8788 8789 void msr_perf_init_(void) 8790 { 8791 const int mci_num = topo.max_cpu_num + 1; 8792 8793 msr_counter_info = calloc(mci_num, sizeof(*msr_counter_info)); 8794 if (!msr_counter_info) 8795 err(1, "calloc msr_counter_info"); 8796 msr_counter_info_size = mci_num; 8797 8798 for (int cpu = 0; cpu < mci_num; ++cpu) 8799 msr_counter_info[cpu].fd_perf = -1; 8800 8801 for (int cidx = 0; cidx < NUM_MSR_COUNTERS; ++cidx) { 8802 8803 struct msr_counter_arch_info *cai = &msr_counter_arch_infos[cidx]; 8804 8805 cai->present = false; 8806 8807 for (int cpu = 0; cpu < mci_num; ++cpu) { 8808 8809 struct msr_counter_info_t *const cci = &msr_counter_info[cpu]; 8810 8811 if (cpu_is_not_allowed(cpu)) 8812 continue; 8813 8814 if (cai->needed) { 8815 /* Use perf API for this counter */ 8816 if (add_msr_perf_counter(cpu, cci, cai) != -1) { 8817 cci->source[cai->rci_index] = COUNTER_SOURCE_PERF; 8818 cai->present = true; 8819 8820 /* User MSR for this counter */ 8821 } else if (add_msr_counter(cpu, cai->msr) >= 0) { 8822 cci->source[cai->rci_index] = COUNTER_SOURCE_MSR; 8823 cci->msr[cai->rci_index] = cai->msr; 8824 cci->msr_mask[cai->rci_index] = cai->msr_mask; 8825 cai->present = true; 8826 } 8827 } 8828 } 8829 } 8830 } 8831 8832 /* Initialize data for reading perf counters from the MSR group. */ 8833 void msr_perf_init(void) 8834 { 8835 bool need_amperf = false, need_smi = false; 8836 const bool need_soft_c1 = (!platform->has_msr_core_c1_res) && (platform->supported_cstates & CC1); 8837 8838 need_amperf = BIC_IS_ENABLED(BIC_Avg_MHz) || BIC_IS_ENABLED(BIC_Busy) || BIC_IS_ENABLED(BIC_Bzy_MHz) 8839 || BIC_IS_ENABLED(BIC_IPC) || need_soft_c1; 8840 8841 if (BIC_IS_ENABLED(BIC_SMI)) 8842 need_smi = true; 8843 8844 /* Enable needed counters */ 8845 msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].needed = need_amperf; 8846 msr_counter_arch_infos[MSR_ARCH_INFO_MPERF_INDEX].needed = need_amperf; 8847 msr_counter_arch_infos[MSR_ARCH_INFO_SMI_INDEX].needed = need_smi; 8848 8849 msr_perf_init_(); 8850 8851 const bool has_amperf = has_amperf_access(); 8852 const bool has_smi = msr_counter_arch_infos[MSR_ARCH_INFO_SMI_INDEX].present; 8853 8854 has_aperf_access = has_amperf; 8855 8856 if (has_amperf) { 8857 BIC_PRESENT(BIC_Avg_MHz); 8858 BIC_PRESENT(BIC_Busy); 8859 BIC_PRESENT(BIC_Bzy_MHz); 8860 BIC_PRESENT(BIC_SMI); 8861 } 8862 8863 if (has_smi) 8864 BIC_PRESENT(BIC_SMI); 8865 } 8866 8867 void cstate_perf_init_(bool soft_c1) 8868 { 8869 bool has_counter; 8870 bool *cores_visited = NULL, *pkg_visited = NULL; 8871 const int cores_visited_elems = topo.max_core_id + 1; 8872 const int pkg_visited_elems = topo.max_package_id + 1; 8873 const int cci_num = topo.max_cpu_num + 1; 8874 8875 ccstate_counter_info = calloc(cci_num, sizeof(*ccstate_counter_info)); 8876 if (!ccstate_counter_info) 8877 err(1, "calloc ccstate_counter_arch_info"); 8878 ccstate_counter_info_size = cci_num; 8879 8880 cores_visited = calloc(cores_visited_elems, sizeof(*cores_visited)); 8881 if (!cores_visited) 8882 err(1, "calloc cores_visited"); 8883 8884 pkg_visited = calloc(pkg_visited_elems, sizeof(*pkg_visited)); 8885 if (!pkg_visited) 8886 err(1, "calloc pkg_visited"); 8887 8888 /* Initialize cstate_counter_info_percpu */ 8889 for (int cpu = 0; cpu < cci_num; ++cpu) { 8890 ccstate_counter_info[cpu].fd_perf_core = -1; 8891 ccstate_counter_info[cpu].fd_perf_pkg = -1; 8892 } 8893 8894 for (int cidx = 0; cidx < NUM_CSTATE_COUNTERS; ++cidx) { 8895 has_counter = false; 8896 memset(cores_visited, 0, cores_visited_elems * sizeof(*cores_visited)); 8897 memset(pkg_visited, 0, pkg_visited_elems * sizeof(*pkg_visited)); 8898 8899 const struct cstate_counter_arch_info *cai = &ccstate_counter_arch_infos[cidx]; 8900 8901 for (int cpu = 0; cpu < cci_num; ++cpu) { 8902 8903 struct cstate_counter_info_t *const cci = &ccstate_counter_info[cpu]; 8904 8905 if (cpu_is_not_allowed(cpu)) 8906 continue; 8907 8908 const int core_id = cpus[cpu].core_id; 8909 const int pkg_id = cpus[cpu].package_id; 8910 8911 assert(core_id < cores_visited_elems); 8912 assert(pkg_id < pkg_visited_elems); 8913 8914 const bool per_thread = cai->flags & CSTATE_COUNTER_FLAG_COLLECT_PER_THREAD; 8915 const bool per_core = cai->flags & CSTATE_COUNTER_FLAG_COLLECT_PER_CORE; 8916 8917 if (!per_thread && cores_visited[core_id]) 8918 continue; 8919 8920 if (!per_core && pkg_visited[pkg_id]) 8921 continue; 8922 8923 const bool counter_needed = BIC_IS_ENABLED(cai->bic_number) || (soft_c1 && (cai->flags & CSTATE_COUNTER_FLAG_SOFT_C1_DEPENDENCY)); 8924 const bool counter_supported = (platform->supported_cstates & cai->feature_mask); 8925 8926 if (counter_needed && counter_supported) { 8927 /* Use perf API for this counter */ 8928 if (add_cstate_perf_counter(cpu, cci, cai) != -1) { 8929 8930 cci->source[cai->rci_index] = COUNTER_SOURCE_PERF; 8931 8932 /* User MSR for this counter */ 8933 } else if (pkg_cstate_limit >= cai->pkg_cstate_limit && add_msr_counter(cpu, cai->msr) >= 0) { 8934 cci->source[cai->rci_index] = COUNTER_SOURCE_MSR; 8935 cci->msr[cai->rci_index] = cai->msr; 8936 } 8937 } 8938 8939 if (cci->source[cai->rci_index] != COUNTER_SOURCE_NONE) { 8940 has_counter = true; 8941 cores_visited[core_id] = true; 8942 pkg_visited[pkg_id] = true; 8943 } 8944 } 8945 8946 /* If any CPU has access to the counter, make it present */ 8947 if (has_counter) 8948 BIC_PRESENT(cai->bic_number); 8949 } 8950 8951 free(cores_visited); 8952 free(pkg_visited); 8953 } 8954 8955 void cstate_perf_init(void) 8956 { 8957 /* 8958 * If we don't have a C1 residency MSR, we calculate it "in software", 8959 * but we need APERF, MPERF too. 8960 */ 8961 const bool soft_c1 = !platform->has_msr_core_c1_res && has_amperf_access() 8962 && platform->supported_cstates & CC1; 8963 8964 if (soft_c1) 8965 BIC_PRESENT(BIC_CPU_c1); 8966 8967 cstate_perf_init_(soft_c1); 8968 } 8969 8970 void probe_cstates(void) 8971 { 8972 probe_cst_limit(); 8973 8974 if (platform->has_msr_module_c6_res_ms) 8975 BIC_PRESENT(BIC_Mod_c6); 8976 8977 if (platform->has_ext_cst_msrs && !no_msr) { 8978 BIC_PRESENT(BIC_Totl_c0); 8979 BIC_PRESENT(BIC_Any_c0); 8980 BIC_PRESENT(BIC_GFX_c0); 8981 BIC_PRESENT(BIC_CPUGFX); 8982 } 8983 8984 if (quiet) 8985 return; 8986 8987 dump_power_ctl(); 8988 dump_cst_cfg(); 8989 decode_c6_demotion_policy_msr(); 8990 print_dev_latency(); 8991 dump_sysfs_cstate_config(); 8992 print_irtl(); 8993 } 8994 8995 void probe_lpi(void) 8996 { 8997 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 8998 BIC_PRESENT(BIC_CPU_LPI); 8999 else 9000 BIC_NOT_PRESENT(BIC_CPU_LPI); 9001 9002 if (!access(sys_lpi_file_sysfs, R_OK)) { 9003 sys_lpi_file = sys_lpi_file_sysfs; 9004 BIC_PRESENT(BIC_SYS_LPI); 9005 } else if (!access(sys_lpi_file_debugfs, R_OK)) { 9006 sys_lpi_file = sys_lpi_file_debugfs; 9007 BIC_PRESENT(BIC_SYS_LPI); 9008 } else { 9009 sys_lpi_file_sysfs = NULL; 9010 BIC_NOT_PRESENT(BIC_SYS_LPI); 9011 } 9012 9013 } 9014 9015 void probe_pstates(void) 9016 { 9017 probe_bclk(); 9018 9019 if (quiet) 9020 return; 9021 9022 dump_platform_info(); 9023 dump_turbo_ratio_info(); 9024 dump_sysfs_pstate_config(); 9025 decode_misc_pwr_mgmt_msr(); 9026 9027 for_all_cpus(print_hwp, ODD_COUNTERS); 9028 for_all_cpus(print_epb, ODD_COUNTERS); 9029 for_all_cpus(print_perf_limit, ODD_COUNTERS); 9030 } 9031 9032 void dump_word_chars(unsigned int word) 9033 { 9034 int i; 9035 9036 for (i = 0; i < 4; ++i) 9037 fprintf(outf, "%c", (word >> (i * 8)) & 0xFF); 9038 } 9039 9040 void dump_cpuid_hypervisor(void) 9041 { 9042 unsigned int ebx = 0; 9043 unsigned int ecx = 0; 9044 unsigned int edx = 0; 9045 9046 __cpuid(0x40000000, max_extended_level, ebx, ecx, edx); 9047 9048 fprintf(outf, "Hypervisor: "); 9049 dump_word_chars(ebx); 9050 dump_word_chars(ecx); 9051 dump_word_chars(edx); 9052 fprintf(outf, "\n"); 9053 } 9054 9055 void dump_rdt_m_leaf(int leaf) 9056 { 9057 unsigned int eax, ebx, ecx, edx; 9058 int cpu = sched_getcpu(); 9059 9060 __cpuid_count(leaf, 0, eax, ebx, ecx, edx); 9061 9062 fprintf(outf, "cpu%d: CPUID(0x%x).0: MAX_RMID:%d %sL3_MON\n", cpu, leaf, ebx, edx & 0x1 ? "" : "No-"); 9063 } 9064 9065 #define RDT_CAT_L3_INDEX 1 9066 #define RDT_CAT_L2_INDEX 2 9067 #define RDT_MBA_INDEX 3 9068 #define RDT_CBA_INDEX 5 9069 #define RDT_RP_INDEX 6 9070 9071 #ifndef MSR_IA32_RESOURCE_PRIORITY 9072 #define MSR_IA32_RESOURCE_PRIORITY 0xc88 9073 #endif 9074 9075 #ifndef MSR_IA32_RESOURCE_PRIORITY_PKG 9076 #define MSR_IA32_RESOURCE_PRIORITY_PKG 0xc89 9077 #endif 9078 9079 void dump_rdt_a_leaf(int leaf) 9080 { 9081 unsigned int eax, ebx, ecx, edx; 9082 int cpu = sched_getcpu(); 9083 unsigned int has_cat_l3, has_cat_l2, has_mba, has_cba, has_rp; 9084 9085 __cpuid_count(leaf, 0, eax, ebx, ecx, edx); 9086 9087 has_cat_l3 = ebx & (1 << RDT_CAT_L3_INDEX); 9088 has_cat_l2 = ebx & (1 << RDT_CAT_L2_INDEX); 9089 has_mba = ebx & (1 << RDT_MBA_INDEX); 9090 has_cba = ebx & (1 << RDT_CBA_INDEX); 9091 has_rp = ebx & (1 << RDT_RP_INDEX); 9092 9093 fprintf(outf, "cpu%d: CPUID(0x%x).0: %sCATL3, %sCATL2, %sMBA, %sCBA, %sRP\n", cpu, leaf, 9094 has_cat_l3 ? "" : "No-", has_cat_l2 ? "" : "No-", has_mba ? "" : "No-", has_cba ? "" : "No-", has_rp ? "" : "No-"); 9095 if (has_rp) { 9096 unsigned int has_rp_thread, has_rp_package; 9097 9098 __cpuid_count(leaf, RDT_RP_INDEX, eax, ebx, ecx, edx); 9099 9100 has_rp_thread = eax & (1 << 0); 9101 has_rp_package = eax & (1 << 1); 9102 9103 fprintf(outf, "cpu%d: CPUID(0x%x).%d: %sRP_THREAD, %sRP_PKG", cpu, leaf, RDT_RP_INDEX, has_rp_thread ? "" : "No-", has_rp_package ? "" : "No-"); 9104 if (has_rp_thread && !no_msr) { 9105 unsigned long long msr; 9106 9107 get_msr(cpu, MSR_IA32_RESOURCE_PRIORITY, &msr); 9108 fprintf(outf, " MSR_RP_THREAD_EN:%llu", msr); 9109 } 9110 if (has_rp_package && !no_msr) { 9111 unsigned long long msr; 9112 9113 get_msr(cpu, MSR_IA32_RESOURCE_PRIORITY_PKG, &msr); 9114 fprintf(outf, " MSR_RP_PKG_EN:%llu", msr); 9115 } 9116 fprintf(outf, "\n"); 9117 } 9118 } 9119 9120 int dump_ardt_m(int cpu) 9121 { 9122 cpu_migrate(cpu); 9123 dump_rdt_m_leaf(0x27); 9124 return 0; 9125 } 9126 9127 int dump_ardt_a(int cpu) 9128 { 9129 cpu_migrate(cpu); 9130 dump_rdt_a_leaf(0x28); 9131 return 0; 9132 } 9133 9134 void process_cpuid() 9135 { 9136 unsigned int eax, ebx, ecx, edx; 9137 unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 9138 unsigned long long ucode_patch = 0; 9139 bool ucode_patch_valid = false; 9140 9141 eax = ebx = ecx = edx = 0; 9142 9143 __cpuid(0, max_level, ebx, ecx, edx); 9144 9145 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 9146 genuine_intel = 1; 9147 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 9148 authentic_amd = 1; 9149 else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 9150 hygon_genuine = 1; 9151 9152 if (!quiet) 9153 fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 9154 9155 __cpuid(1, fms, ebx, ecx, edx); 9156 family = (fms >> 8) & 0xf; 9157 model = (fms >> 4) & 0xf; 9158 stepping = fms & 0xf; 9159 if (family == 0xf) 9160 family += (fms >> 20) & 0xff; 9161 if (family >= 6) 9162 model += ((fms >> 16) & 0xf) << 4; 9163 ecx_flags = ecx; 9164 edx_flags = edx; 9165 cpuid_has_hv = ecx_flags & (1 << 31); 9166 9167 if (!no_msr) { 9168 get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch); 9169 ucode_patch_valid = true; 9170 if (!authentic_amd && !hygon_genuine) 9171 ucode_patch >>= 32; 9172 } 9173 9174 /* 9175 * check max extended function levels of CPUID. 9176 * This is needed to check for invariant TSC. 9177 * This check is valid for both Intel and AMD. 9178 */ 9179 ebx = ecx = edx = 0; 9180 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 9181 9182 if (!quiet) { 9183 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d)", family, model, stepping, family, model, stepping); 9184 if (ucode_patch_valid) 9185 fprintf(outf, " microcode 0x%x", (unsigned int)ucode_patch); 9186 fputc('\n', outf); 9187 9188 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 9189 fprintf(outf, "CPUID(1): %sSSE3 %sMONITOR %sSMX %sEIST %sTM2 %sHV %sTSC %sMSR %sACPI-TM %sHT %sTM\n", 9190 ecx_flags & (1 << 0) ? "" : "No-", 9191 ecx_flags & (1 << 3) ? "" : "No-", 9192 ecx_flags & (1 << 6) ? "" : "No-", 9193 ecx_flags & (1 << 7) ? "" : "No-", 9194 ecx_flags & (1 << 8) ? "" : "No-", 9195 cpuid_has_hv ? "" : "No-", 9196 edx_flags & (1 << 4) ? "" : "No-", 9197 edx_flags & (1 << 5) ? "" : "No-", edx_flags & (1 << 22) ? "" : "No-", edx_flags & (1 << 28) ? "" : "No-", edx_flags & (1 << 29) ? "" : "No-"); 9198 } 9199 if (!quiet && cpuid_has_hv) 9200 dump_cpuid_hypervisor(); 9201 9202 probe_platform_features(family, model); 9203 init_perf_model_support(family, model); 9204 9205 if (!(edx_flags & (1 << 5))) 9206 errx(1, "CPUID: no MSR"); 9207 9208 if (max_extended_level >= 0x80000007) { 9209 9210 /* 9211 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 9212 * this check is valid for both Intel and AMD 9213 */ 9214 __cpuid(0x80000007, eax, ebx, ecx, edx); 9215 has_invariant_tsc = edx & (1 << 8); 9216 } 9217 9218 /* 9219 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 9220 * this check is valid for both Intel and AMD 9221 */ 9222 9223 __cpuid(0x6, eax, ebx, ecx, edx); 9224 cpuid_has_aperf_mperf = ecx & (1 << 0); 9225 do_dts = eax & (1 << 0); 9226 if (do_dts) 9227 BIC_PRESENT(BIC_CoreTmp); 9228 has_turbo = eax & (1 << 1); 9229 do_ptm = eax & (1 << 6); 9230 if (do_ptm) 9231 BIC_PRESENT(BIC_PkgTmp); 9232 has_hwp = eax & (1 << 7); 9233 has_hwp_notify = eax & (1 << 8); 9234 has_hwp_activity_window = eax & (1 << 9); 9235 has_hwp_epp = eax & (1 << 10); 9236 has_hwp_pkg = eax & (1 << 11); 9237 has_epb = ecx & (1 << 3); 9238 9239 if (!quiet) 9240 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 9241 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 9242 cpuid_has_aperf_mperf ? "" : "No-", 9243 has_turbo ? "" : "No-", 9244 do_dts ? "" : "No-", 9245 do_ptm ? "" : "No-", 9246 has_hwp ? "" : "No-", has_hwp_notify ? "" : "No-", has_hwp_activity_window ? "" : "No-", has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 9247 9248 if (!quiet) 9249 decode_misc_enable_msr(); 9250 9251 if (max_level >= 0x7) { 9252 eax = ebx = ecx = edx = 0; 9253 __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 9254 9255 is_hybrid = !!(edx & (1 << 15)); 9256 9257 if (!quiet) { 9258 int has_sgx, has_rdt_m, has_rdt_a, has_ardt_m, has_ardt_a; 9259 9260 has_sgx = ebx & (1 << 2); 9261 has_rdt_m = ebx & (1 << 12); 9262 has_rdt_a = ebx & (1 << 15); 9263 9264 eax = ebx = ecx = edx = 0; 9265 __cpuid_count(0x7, 1, eax, ebx, ecx, edx); 9266 9267 has_ardt_m = ecx & (1 << 0); 9268 has_ardt_a = ecx & (1 << 1); 9269 9270 fprintf(outf, "CPUID(7): %sSGX %sRDT_M %sRDT_A %sARDT_M %sARDT_A %sHybrid\n", 9271 has_sgx ? "" : "No-", has_rdt_m ? "" : "No-", has_rdt_a ? "" : "No-", has_ardt_m ? "" : "No-", has_ardt_a ? "" : "No-", is_hybrid ? "" : "No-"); 9272 9273 if (has_sgx) 9274 decode_feature_control_msr(); 9275 9276 if (has_rdt_m && max_level >= 0xF) 9277 dump_rdt_m_leaf(0xF); 9278 if (has_rdt_a && max_level >= 0x10) 9279 dump_rdt_a_leaf(0x10); 9280 if (has_ardt_m && max_level >= 0x27) 9281 for_all_proc_cpus(dump_ardt_m); 9282 if (has_ardt_a && max_level >= 0x28) 9283 for_all_proc_cpus(dump_ardt_a); 9284 } 9285 } 9286 9287 if (max_level >= 0x15) { 9288 unsigned int eax_crystal; 9289 unsigned int ebx_tsc; 9290 9291 /* 9292 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 9293 */ 9294 eax_crystal = ebx_tsc = crystal_hz = edx = 0; 9295 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 9296 9297 if (ebx_tsc != 0) { 9298 if (!quiet && (ebx != 0)) 9299 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", eax_crystal, ebx_tsc, crystal_hz); 9300 9301 if (crystal_hz == 0) 9302 crystal_hz = platform->crystal_freq; 9303 9304 if (crystal_hz) { 9305 tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 9306 if (!quiet) 9307 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 9308 } 9309 } 9310 } 9311 if (max_level >= 0x16) { 9312 unsigned int base_mhz, max_mhz, bus_mhz, edx; 9313 9314 /* 9315 * CPUID 16H Base MHz, Max MHz, Bus MHz 9316 */ 9317 base_mhz = max_mhz = bus_mhz = edx = 0; 9318 9319 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 9320 9321 bclk = bus_mhz; 9322 9323 base_hz = base_mhz * 1000000; 9324 has_base_hz = 1; 9325 9326 if (platform->enable_tsc_tweak) 9327 tsc_tweak = base_hz / tsc_hz; 9328 9329 if (!quiet) 9330 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", base_mhz, max_mhz, bus_mhz); 9331 } 9332 9333 if (cpuid_has_aperf_mperf) 9334 aperf_mperf_multiplier = platform->need_perf_multiplier ? 1024 : 1; 9335 9336 BIC_PRESENT(BIC_IRQ); 9337 BIC_PRESENT(BIC_NMI); 9338 BIC_PRESENT(BIC_TSC_MHz); 9339 } 9340 9341 static void counter_info_init(void) 9342 { 9343 for (int i = 0; i < NUM_CSTATE_COUNTERS; ++i) { 9344 struct cstate_counter_arch_info *const cai = &ccstate_counter_arch_infos[i]; 9345 9346 if (platform->has_msr_knl_core_c6_residency && cai->msr == MSR_CORE_C6_RESIDENCY) 9347 cai->msr = MSR_KNL_CORE_C6_RESIDENCY; 9348 9349 if (!platform->has_msr_core_c1_res && cai->msr == MSR_CORE_C1_RES) 9350 cai->msr = 0; 9351 9352 if (platform->has_msr_atom_pkg_c6_residency && cai->msr == MSR_PKG_C6_RESIDENCY) 9353 cai->msr = MSR_ATOM_PKG_C6_RESIDENCY; 9354 } 9355 9356 for (int i = 0; i < NUM_MSR_COUNTERS; ++i) { 9357 msr_counter_arch_infos[i].present = false; 9358 msr_counter_arch_infos[i].needed = false; 9359 } 9360 } 9361 9362 void probe_pm_features(void) 9363 { 9364 probe_pstates(); 9365 9366 probe_cstates(); 9367 9368 probe_lpi(); 9369 9370 probe_intel_uncore_frequency(); 9371 9372 probe_graphics(); 9373 9374 probe_rapl(); 9375 9376 probe_thermal(); 9377 9378 if (platform->has_nhm_msrs && !no_msr) 9379 BIC_PRESENT(BIC_SMI); 9380 9381 if (!quiet) 9382 decode_misc_feature_control(); 9383 } 9384 9385 /* 9386 * has_perf_llc_access() 9387 * 9388 * return 1 on success, else 0 9389 */ 9390 int has_perf_llc_access(void) 9391 { 9392 int fd; 9393 9394 if (no_perf) 9395 return 0; 9396 9397 fd = open_perf_counter(master_cpu, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES, -1, PERF_FORMAT_GROUP); 9398 if (fd != -1) 9399 close(fd); 9400 9401 if (fd == -1) 9402 warnx("Failed to access %s. Some of the counters may not be available\n" 9403 "\tRun as root to enable them or use %s to disable the access explicitly", "perf LLC counters", "'--hide LLC' or '--no-perf'"); 9404 9405 return (fd != -1); 9406 } 9407 9408 void perf_llc_init(void) 9409 { 9410 int cpu; 9411 int retval; 9412 9413 if (no_perf) 9414 return; 9415 if (!(BIC_IS_ENABLED(BIC_LLC_MRPS) || BIC_IS_ENABLED(BIC_LLC_HIT))) 9416 return; 9417 9418 assert(fd_llc_percpu != 0); 9419 9420 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 9421 9422 if (cpu_is_not_allowed(cpu)) 9423 continue; 9424 9425 fd_llc_percpu[cpu] = open_perf_counter(cpu, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES, -1, PERF_FORMAT_GROUP); 9426 if (fd_llc_percpu[cpu] == -1) { 9427 warnx("%s: perf REFS: failed to open counter on cpu%d", __func__, cpu); 9428 free_fd_llc_percpu(); 9429 return; 9430 } 9431 retval = open_perf_counter(cpu, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES, fd_llc_percpu[cpu], PERF_FORMAT_GROUP); 9432 if (retval == -1) { 9433 warnx("%s: perf MISS: failed to open counter on cpu%d", __func__, cpu); 9434 free_fd_llc_percpu(); 9435 return; 9436 } 9437 } 9438 BIC_PRESENT(BIC_LLC_MRPS); 9439 BIC_PRESENT(BIC_LLC_HIT); 9440 } 9441 9442 void perf_l2_init(void) 9443 { 9444 int cpu; 9445 int retval; 9446 9447 if (no_perf) 9448 return; 9449 if (!(BIC_IS_ENABLED(BIC_L2_MRPS) || BIC_IS_ENABLED(BIC_L2_HIT))) 9450 return; 9451 if (perf_model_support == NULL) 9452 return; 9453 9454 assert(fd_l2_percpu != 0); 9455 9456 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 9457 9458 if (cpu_is_not_allowed(cpu)) 9459 continue; 9460 9461 if (!is_hybrid) { 9462 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.uniform, perf_model_support->first.refs, -1, PERF_FORMAT_GROUP); 9463 if (fd_l2_percpu[cpu] == -1) { 9464 warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.refs); 9465 free_fd_l2_percpu(); 9466 return; 9467 } 9468 retval = open_perf_counter(cpu, perf_pmu_types.uniform, perf_model_support->first.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP); 9469 if (retval == -1) { 9470 warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.hits); 9471 free_fd_l2_percpu(); 9472 return; 9473 } 9474 continue; 9475 } 9476 if (perf_pcore_set && CPU_ISSET_S(cpu, cpu_setsize, perf_pcore_set)) { 9477 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.pcore, perf_model_support->first.refs, -1, PERF_FORMAT_GROUP); 9478 if (fd_l2_percpu[cpu] == -1) { 9479 warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.refs); 9480 free_fd_l2_percpu(); 9481 return; 9482 } 9483 retval = open_perf_counter(cpu, perf_pmu_types.pcore, perf_model_support->first.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP); 9484 if (retval == -1) { 9485 warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.hits); 9486 free_fd_l2_percpu(); 9487 return; 9488 } 9489 } else if (perf_ecore_set && CPU_ISSET_S(cpu, cpu_setsize, perf_ecore_set)) { 9490 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.ecore, perf_model_support->second.refs, -1, PERF_FORMAT_GROUP); 9491 if (fd_l2_percpu[cpu] == -1) { 9492 warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.refs); 9493 free_fd_l2_percpu(); 9494 return; 9495 } 9496 retval = open_perf_counter(cpu, perf_pmu_types.ecore, perf_model_support->second.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP); 9497 if (retval == -1) { 9498 warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.hits); 9499 free_fd_l2_percpu(); 9500 return; 9501 } 9502 } else if (perf_lcore_set && CPU_ISSET_S(cpu, cpu_setsize, perf_lcore_set)) { 9503 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.lcore, perf_model_support->third.refs, -1, PERF_FORMAT_GROUP); 9504 if (fd_l2_percpu[cpu] == -1) { 9505 warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.refs); 9506 free_fd_l2_percpu(); 9507 return; 9508 } 9509 retval = open_perf_counter(cpu, perf_pmu_types.lcore, perf_model_support->third.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP); 9510 if (retval == -1) { 9511 warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.hits); 9512 free_fd_l2_percpu(); 9513 return; 9514 } 9515 } else { 9516 warn("%s: cpu%d: type %d: Update kernel perf support or use \"--hide L2MRPS,L2%%hit\" or \"--hide cache\" or \"--no-perf\"", __func__, cpu, cpus[cpu].type); 9517 return; 9518 } 9519 } 9520 BIC_PRESENT(BIC_L2_MRPS); 9521 BIC_PRESENT(BIC_L2_HIT); 9522 } 9523 9524 /* 9525 * in /dev/cpu/ return success for names that are numbers 9526 * ie. filter out ".", "..", "microcode". 9527 */ 9528 int dir_filter(const struct dirent *dirp) 9529 { 9530 if (isdigit(dirp->d_name[0])) 9531 return 1; 9532 else 9533 return 0; 9534 } 9535 9536 int set_thread_siblings(struct cpu_topology *thiscpu) 9537 { 9538 char path[80]; 9539 int cpu = thiscpu->cpu_id; 9540 size_t size; 9541 int ht_id = 0; 9542 int i; 9543 9544 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 9545 if (thiscpu->ht_id < 0) 9546 thiscpu->ht_id = 0; /* first CPU in core */ 9547 if (!thiscpu->put_ids) 9548 return -1; 9549 9550 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 9551 CPU_ZERO_S(size, thiscpu->put_ids); 9552 9553 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology", cpu); 9554 9555 initialize_cpu_set_from_sysfs(thiscpu->put_ids, path, "thread_siblings_list"); 9556 9557 for (i = 0; i <= topo.max_cpu_num; ++i) 9558 if (CPU_ISSET_S(i, size, thiscpu->put_ids)) { 9559 cpus[i].ht_id = ht_id; 9560 cpus[cpu].ht_sibling_cpu_id[ht_id] = i; 9561 ht_id += 1; 9562 } 9563 9564 return (ht_id - 1); 9565 } 9566 9567 /* 9568 * get_max_cpu_num(path) 9569 * return the last number of the cpu list in this file 9570 */ 9571 static int get_max_cpu_num(char *path) 9572 { 9573 FILE *filep; 9574 int retval, num; 9575 char c; 9576 9577 filep = fopen(path, "r"); 9578 if (filep == NULL) { 9579 warn("%s", path); 9580 return 255; 9581 } 9582 9583 while ((retval = fscanf(filep, "%d%c", &num, &c)) == 2) { 9584 if (c == '\n') 9585 break; 9586 if (c != ',' && c != '-') 9587 errx(1, "Bad Format '%s'", path); 9588 } 9589 if (retval != 2) 9590 errx(1, "Bad format '%s'", path); 9591 9592 fclose(filep); 9593 return num; 9594 } 9595 9596 void topology_probe(bool startup) 9597 { 9598 int i; 9599 int max_core_id = 0; 9600 int max_package_id = 0; 9601 int max_siblings = 0; 9602 9603 /* Initialize num_cpus */ 9604 topo.num_cpus = 0; 9605 for_all_proc_cpus(count_cpus); 9606 if (!summary_only) 9607 BIC_PRESENT(BIC_CPU); 9608 9609 if (debug > 1) 9610 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 9611 9612 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 9613 if (cpus == NULL) 9614 err(1, "calloc cpus"); 9615 9616 /* 9617 * Allocate and initialize cpu_present_set 9618 */ 9619 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 9620 if (cpu_present_set == NULL) 9621 err(3, "CPU_ALLOC"); 9622 CPU_ZERO_S(cpu_setsize, cpu_present_set); 9623 for_all_proc_cpus(mark_cpu_present); 9624 if (debug) 9625 print_cpu_set("present set", cpu_present_set); 9626 9627 /* 9628 * Allocate and initialize cpu_possible_set 9629 */ 9630 cpu_possible_set = CPU_ALLOC((topo.max_cpu_num + 1)); 9631 if (cpu_possible_set == NULL) 9632 err(3, "CPU_ALLOC"); 9633 CPU_ZERO_S(cpu_setsize, cpu_possible_set); 9634 initialize_cpu_set_from_sysfs(cpu_possible_set, "/sys/devices/system/cpu", "possible"); 9635 if (debug) 9636 print_cpu_set("possible set", cpu_possible_set); 9637 9638 /* 9639 * Allocate and initialize cpu_effective_set 9640 */ 9641 cpu_effective_set = CPU_ALLOC((topo.max_cpu_num + 1)); 9642 if (cpu_effective_set == NULL) 9643 err(3, "CPU_ALLOC"); 9644 CPU_ZERO_S(cpu_setsize, cpu_effective_set); 9645 update_effective_set(startup); 9646 if (debug) 9647 print_cpu_set("effective set", cpu_effective_set); 9648 9649 /* 9650 * Allocate and initialize cpu_allowed_set 9651 */ 9652 cpu_allowed_set = CPU_ALLOC((topo.max_cpu_num + 1)); 9653 if (cpu_allowed_set == NULL) 9654 err(3, "CPU_ALLOC"); 9655 CPU_ZERO_S(cpu_setsize, cpu_allowed_set); 9656 9657 /* 9658 * Validate and update cpu_allowed_set. 9659 * 9660 * Make sure all cpus in cpuset_cmdline are also in cpu_present_set during startup. 9661 * Give a warning when cpus in cpuset_cmdline become unavailable at runtime. 9662 * Give a warning when cpus are not effective because of cgroup setting. 9663 * 9664 * cpu_allowed_set is the intersection of cpu_present_set/cpu_effective_set/cpuset_cmdline. 9665 */ 9666 for (i = 0; i <= topo.max_cpu_num; ++i) { 9667 if (cpuset_cmdline && !CPU_ISSET_S(i, cpu_setsize, cpuset_cmdline)) 9668 continue; 9669 9670 if (!CPU_ISSET_S(i, cpu_setsize, cpu_present_set)) { 9671 if (cpuset_cmdline) { 9672 /* cpus in cpuset_cmdline must be in cpu_present_set during startup */ 9673 if (startup) 9674 errx(1, "cpu%d not present", i); 9675 else 9676 fprintf(stderr, "cpu%d not present\n", i); 9677 } 9678 continue; 9679 } 9680 9681 if (CPU_COUNT_S(cpu_setsize, cpu_effective_set)) { 9682 if (!CPU_ISSET_S(i, cpu_setsize, cpu_effective_set)) { 9683 fprintf(stderr, "cpu%d not effective\n", i); 9684 continue; 9685 } 9686 } 9687 9688 CPU_SET_S(i, cpu_setsize, cpu_allowed_set); 9689 } 9690 if (debug) 9691 print_cpu_set("allowed set", cpu_allowed_set); 9692 9693 if (!CPU_COUNT_S(cpu_setsize, cpu_allowed_set)) 9694 errx(-ENODEV, "No valid cpus found"); 9695 sched_setaffinity(0, cpu_setsize, cpu_allowed_set); 9696 9697 /* 9698 * Allocate and initialize cpu_affinity_set 9699 */ 9700 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 9701 if (cpu_affinity_set == NULL) 9702 err(3, "CPU_ALLOC"); 9703 CPU_ZERO_S(cpu_setsize, cpu_affinity_set); 9704 9705 for_all_proc_cpus(clear_ht_id); 9706 9707 for_all_proc_cpus(set_cpu_hybrid_type); 9708 9709 /* 9710 * For online cpus 9711 * find max_core_id, max_package_id, num_cores (per system) 9712 */ 9713 topo.min_module_id = 0x7FFFFFFF; 9714 for (i = 0; i <= topo.max_cpu_num; ++i) { 9715 int siblings; 9716 9717 if (cpu_is_not_present(i)) { 9718 if (debug > 1) 9719 fprintf(outf, "cpu%d NOT PRESENT\n", i); 9720 continue; 9721 } 9722 9723 cpus[i].cpu_id = i; 9724 9725 /* get package information */ 9726 cpus[i].package_id = get_package_id(i); 9727 if (cpus[i].package_id > max_package_id) 9728 max_package_id = cpus[i].package_id; 9729 9730 /* get die information */ 9731 cpus[i].die_id = get_die_id(i); 9732 if (cpus[i].die_id > topo.max_die_id) 9733 topo.max_die_id = cpus[i].die_id; 9734 9735 /* get l3 information */ 9736 cpus[i].l3_id = get_l3_id(i); 9737 if (cpus[i].l3_id > topo.max_l3_id) 9738 topo.max_l3_id = cpus[i].l3_id; 9739 9740 /* get numa node information */ 9741 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 9742 if (cpus[i].physical_node_id > topo.max_node_num) 9743 topo.max_node_num = cpus[i].physical_node_id; 9744 9745 /* get module information */ 9746 cpus[i].module_id = get_module_id(i); 9747 if (cpus[i].module_id > topo.max_module_id) 9748 topo.max_module_id = cpus[i].module_id; 9749 if (cpus[i].module_id < topo.min_module_id) 9750 topo.min_module_id = cpus[i].module_id; 9751 9752 /* get core information */ 9753 cpus[i].core_id = get_core_id(i); 9754 if (cpus[i].core_id > max_core_id) 9755 max_core_id = cpus[i].core_id; 9756 9757 /* get thread information */ 9758 siblings = set_thread_siblings(&cpus[i]); 9759 if (siblings > max_siblings) 9760 max_siblings = siblings; 9761 if (cpus[i].ht_id == 0) 9762 topo.num_cores++; 9763 } 9764 topo.max_core_id = max_core_id; /* within a package */ 9765 topo.max_package_id = max_package_id; 9766 9767 topo.cores_per_pkg = max_core_id + 1; 9768 if (debug > 1) 9769 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_pkg); 9770 if (!summary_only) 9771 BIC_PRESENT(BIC_Core); 9772 9773 if (debug > 1) 9774 fprintf(outf, "min_module_id %d max_module_id %d\n", topo.min_module_id, topo.max_module_id); 9775 if (!summary_only && (topo.min_module_id != topo.max_module_id)) 9776 BIC_PRESENT(BIC_Module); 9777 9778 topo.num_die = topo.max_die_id + 1; 9779 if (debug > 1) 9780 fprintf(outf, "max_die_id %d, sizing for %d die\n", topo.max_die_id, topo.num_die); 9781 if (!summary_only && topo.num_die > 1) 9782 BIC_PRESENT(BIC_Die); 9783 9784 if (!summary_only && topo.max_l3_id > 0) 9785 BIC_PRESENT(BIC_L3); 9786 9787 topo.num_packages = max_package_id + 1; 9788 if (debug > 1) 9789 fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 9790 if (!summary_only && topo.num_packages > 1) 9791 BIC_PRESENT(BIC_Package); 9792 9793 set_node_data(); 9794 if (debug > 1) 9795 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 9796 if (!summary_only && topo.nodes_per_pkg > 1) 9797 BIC_PRESENT(BIC_Node); 9798 9799 topo.threads_per_core = max_siblings; 9800 if (debug > 1) 9801 fprintf(outf, "max_siblings %d\n", max_siblings); 9802 9803 if (debug < 1) 9804 return; 9805 9806 for (i = 0; i <= topo.max_cpu_num; ++i) { 9807 int ht_id; 9808 9809 if (cpu_is_not_present(i)) 9810 continue; 9811 fprintf(outf, 9812 "cpu %d pkg %d die %d l3 %d node %d lnode %d module 0x%x core %d ht_id %d", 9813 i, cpus[i].package_id, cpus[i].die_id, cpus[i].l3_id, cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].module_id, cpus[i].core_id, cpus[i].ht_id); 9814 fprintf(outf, " siblings"); 9815 for (ht_id = 0; ht_id <= MAX_HT_ID; ++ht_id) 9816 fprintf(outf, " %d", cpus[i].ht_sibling_cpu_id[ht_id]); 9817 fprintf(outf, "\n"); 9818 } 9819 9820 } 9821 9822 void allocate_counters_1(struct counters *counters) 9823 { 9824 counters->threads = calloc(1, sizeof(struct thread_data)); 9825 if (counters->threads == NULL) 9826 goto error; 9827 9828 counters->cores = calloc(1, sizeof(struct core_data)); 9829 if (counters->cores == NULL) 9830 goto error; 9831 9832 counters->packages = calloc(1, sizeof(struct pkg_data)); 9833 if (counters->packages == NULL) 9834 goto error; 9835 9836 return; 9837 error: 9838 err(1, "calloc counters_1"); 9839 } 9840 9841 void allocate_counters(struct counters *counters) 9842 { 9843 int i; 9844 int num_cores = topo.cores_per_pkg * topo.num_packages; 9845 9846 counters->threads = calloc(topo.max_cpu_num + 1, sizeof(struct thread_data)); 9847 if (counters->threads == NULL) 9848 goto error; 9849 9850 for (i = 0; i <= topo.max_cpu_num; i++) 9851 (counters->threads)[i].cpu_id = -1; 9852 9853 counters->cores = calloc(num_cores, sizeof(struct core_data)); 9854 if (counters->cores == NULL) 9855 goto error; 9856 9857 for (i = 0; i < num_cores; i++) 9858 (counters->cores)[i].first_cpu = -1; 9859 9860 counters->packages = calloc(topo.num_packages, sizeof(struct pkg_data)); 9861 if (counters->packages == NULL) 9862 goto error; 9863 9864 for (i = 0; i < topo.num_packages; i++) 9865 (counters->packages)[i].first_cpu = -1; 9866 9867 return; 9868 error: 9869 err(1, "calloc counters"); 9870 } 9871 9872 /* 9873 * init_counter() 9874 * 9875 * set t->cpu_id, FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 9876 */ 9877 void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 9878 { 9879 int pkg_id = cpus[cpu_id].package_id; 9880 int node_id = cpus[cpu_id].logical_node_id; 9881 int core_id = cpus[cpu_id].core_id; 9882 struct thread_data *t; 9883 struct core_data *c; 9884 9885 /* Workaround for systems where physical_node_id==-1 9886 * and logical_node_id==(-1 - topo.num_cpus) 9887 */ 9888 if (node_id < 0) 9889 node_id = 0; 9890 9891 t = &thread_base[cpu_id]; 9892 c = &core_base[GLOBAL_CORE_ID(core_id, pkg_id)]; 9893 9894 t->cpu_id = cpu_id; 9895 if (!cpu_is_not_allowed(cpu_id)) { 9896 9897 if (c->first_cpu < 0) 9898 c->first_cpu = t->cpu_id; 9899 if (pkg_base[pkg_id].first_cpu < 0) 9900 pkg_base[pkg_id].first_cpu = t->cpu_id; 9901 } 9902 } 9903 9904 int initialize_counters(int cpu_id) 9905 { 9906 init_counter(EVEN_COUNTERS, cpu_id); 9907 init_counter(ODD_COUNTERS, cpu_id); 9908 return 0; 9909 } 9910 9911 void allocate_output_buffer() 9912 { 9913 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 9914 outp = output_buffer; 9915 if (outp == NULL) 9916 err(-1, "calloc output buffer"); 9917 } 9918 9919 void allocate_fd_percpu(void) 9920 { 9921 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 9922 if (fd_percpu == NULL) 9923 err(-1, "calloc fd_percpu"); 9924 } 9925 9926 void allocate_irq_buffers(void) 9927 { 9928 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 9929 if (irq_column_2_cpu == NULL) 9930 err(-1, "calloc %d", topo.num_cpus); 9931 9932 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 9933 if (irqs_per_cpu == NULL) 9934 err(-1, "calloc %d IRQ", topo.max_cpu_num + 1); 9935 9936 nmi_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 9937 if (nmi_per_cpu == NULL) 9938 err(-1, "calloc %d NMI", topo.max_cpu_num + 1); 9939 } 9940 9941 int update_topo(PER_THREAD_PARAMS) 9942 { 9943 topo.allowed_cpus++; 9944 if ((int)t->cpu_id == c->first_cpu) 9945 topo.allowed_cores++; 9946 if ((int)t->cpu_id == p->first_cpu) 9947 topo.allowed_packages++; 9948 9949 return 0; 9950 } 9951 9952 void topology_update(void) 9953 { 9954 topo.allowed_cpus = 0; 9955 topo.allowed_cores = 0; 9956 topo.allowed_packages = 0; 9957 for_all_cpus(update_topo, ODD_COUNTERS); 9958 if (debug) 9959 fprintf(stderr, "allowed_cpus %d allowed_cores %d allowed_packages %d\n", topo.allowed_cpus, topo.allowed_cores, topo.allowed_packages); 9960 } 9961 9962 void setup_all_buffers(bool startup) 9963 { 9964 topology_probe(startup); 9965 allocate_irq_buffers(); 9966 allocate_fd_percpu(); 9967 allocate_counters_1(&average); 9968 allocate_counters(&even); 9969 allocate_counters(&odd); 9970 allocate_output_buffer(); 9971 for_all_proc_cpus(initialize_counters); 9972 topology_update(); 9973 } 9974 9975 void set_master_cpu(void) 9976 { 9977 int i; 9978 9979 for (i = 0; i <= topo.max_cpu_num; ++i) { 9980 if (cpu_is_not_allowed(i)) 9981 continue; 9982 master_cpu = i; 9983 if (debug > 1) 9984 fprintf(outf, "master_cpu = %d\n", master_cpu); 9985 return; 9986 } 9987 err(-ENODEV, "No valid cpus found"); 9988 } 9989 9990 bool has_added_counters(void) 9991 { 9992 /* 9993 * It only makes sense to call this after the command line is parsed, 9994 * otherwise sys structure is not populated. 9995 */ 9996 9997 return sys.added_core_counters | sys.added_thread_counters | sys.added_package_counters; 9998 } 9999 10000 void check_msr_access(void) 10001 { 10002 check_msr_driver(); 10003 check_msr_permission(); 10004 10005 if (no_msr) 10006 bic_disable_msr_access(); 10007 } 10008 10009 void check_perf_access(void) 10010 { 10011 if (BIC_IS_ENABLED(BIC_IPC)) 10012 if (!has_perf_instr_count_access()) 10013 no_perf = 1; 10014 10015 if (BIC_IS_ENABLED(BIC_LLC_MRPS) || BIC_IS_ENABLED(BIC_LLC_HIT)) 10016 if (!has_perf_llc_access()) 10017 no_perf = 1; 10018 10019 if (no_perf) 10020 bic_disable_perf_access(); 10021 } 10022 10023 bool perf_has_hybrid_devices(void) 10024 { 10025 /* 10026 * 0: unknown 10027 * 1: has separate perf device for p and e core 10028 * -1: doesn't have separate perf device for p and e core 10029 */ 10030 static int cached; 10031 10032 if (cached > 0) 10033 return true; 10034 10035 if (cached < 0) 10036 return false; 10037 10038 if (access("/sys/bus/event_source/devices/cpu_core", F_OK)) { 10039 cached = -1; 10040 return false; 10041 } 10042 10043 if (access("/sys/bus/event_source/devices/cpu_atom", F_OK)) { 10044 cached = -1; 10045 return false; 10046 } 10047 10048 cached = 1; 10049 return true; 10050 } 10051 10052 int added_perf_counters_init_(struct perf_counter_info *pinfo) 10053 { 10054 size_t num_domains = 0; 10055 unsigned int next_domain; 10056 bool *domain_visited; 10057 unsigned int perf_type, perf_config; 10058 double perf_scale; 10059 int fd_perf; 10060 10061 if (!pinfo) 10062 return 0; 10063 10064 const size_t max_num_domains = MAX(topo.max_cpu_num + 1, MAX(topo.max_core_id + 1, topo.max_package_id + 1)); 10065 10066 domain_visited = calloc(max_num_domains, sizeof(*domain_visited)); 10067 if (!domain_visited) 10068 errx(1, "%s: alloc %s", __func__, "domain_visited"); 10069 10070 while (pinfo) { 10071 switch (pinfo->scope) { 10072 case SCOPE_CPU: 10073 num_domains = topo.max_cpu_num + 1; 10074 break; 10075 10076 case SCOPE_CORE: 10077 num_domains = topo.max_core_id + 1; 10078 break; 10079 10080 case SCOPE_PACKAGE: 10081 num_domains = topo.max_package_id + 1; 10082 break; 10083 } 10084 10085 /* Allocate buffer for file descriptor for each domain. */ 10086 pinfo->fd_perf_per_domain = calloc(num_domains, sizeof(*pinfo->fd_perf_per_domain)); 10087 if (!pinfo->fd_perf_per_domain) 10088 errx(1, "%s: alloc %s", __func__, "fd_perf_per_domain"); 10089 10090 for (size_t i = 0; i < num_domains; ++i) 10091 pinfo->fd_perf_per_domain[i] = -1; 10092 10093 pinfo->num_domains = num_domains; 10094 pinfo->scale = 1.0; 10095 10096 memset(domain_visited, 0, max_num_domains * sizeof(*domain_visited)); 10097 10098 for (int cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 10099 10100 next_domain = cpu_to_domain(pinfo, cpu); 10101 10102 assert(next_domain < num_domains); 10103 10104 if (cpu_is_not_allowed(cpu)) 10105 continue; 10106 10107 if (domain_visited[next_domain]) 10108 continue; 10109 10110 /* 10111 * Intel hybrid platforms expose different perf devices for P and E cores. 10112 * Instead of one, "/sys/bus/event_source/devices/cpu" device, there are 10113 * "/sys/bus/event_source/devices/{cpu_core,cpu_atom}". 10114 * 10115 * This makes it more complicated to the user, because most of the counters 10116 * are available on both and have to be handled manually, otherwise. 10117 * 10118 * Code below, allow user to use the old "cpu" name, which is translated accordingly. 10119 */ 10120 const char *perf_device = pinfo->device; 10121 10122 if (strcmp(perf_device, "cpu") == 0 && perf_has_hybrid_devices()) { 10123 switch (cpus[cpu].type) { 10124 case INTEL_PCORE_TYPE: 10125 perf_device = "cpu_core"; 10126 break; 10127 10128 case INTEL_ECORE_TYPE: 10129 perf_device = "cpu_atom"; 10130 break; 10131 10132 default: /* Don't change, we will probably fail and report a problem soon. */ 10133 break; 10134 } 10135 } 10136 10137 perf_type = read_perf_type(perf_device); 10138 if (perf_type == (unsigned int)-1) { 10139 warnx("%s: perf/%s/%s: failed to read %s", __func__, perf_device, pinfo->event, "type"); 10140 continue; 10141 } 10142 10143 perf_config = read_perf_config(perf_device, pinfo->event); 10144 if (perf_config == (unsigned int)-1) { 10145 warnx("%s: perf/%s/%s: failed to read %s", __func__, perf_device, pinfo->event, "config"); 10146 continue; 10147 } 10148 10149 /* Scale is not required, some counters just don't have it. */ 10150 perf_scale = read_perf_scale(perf_device, pinfo->event); 10151 if (perf_scale == 0.0) 10152 perf_scale = 1.0; 10153 10154 fd_perf = open_perf_counter(cpu, perf_type, perf_config, -1, 0); 10155 if (fd_perf == -1) { 10156 warnx("%s: perf/%s/%s: failed to open counter on cpu%d", __func__, perf_device, pinfo->event, cpu); 10157 continue; 10158 } 10159 10160 domain_visited[next_domain] = 1; 10161 pinfo->fd_perf_per_domain[next_domain] = fd_perf; 10162 pinfo->scale = perf_scale; 10163 10164 if (debug) 10165 fprintf(stderr, "Add perf/%s/%s cpu%d: %d\n", perf_device, pinfo->event, cpu, pinfo->fd_perf_per_domain[next_domain]); 10166 } 10167 10168 pinfo = pinfo->next; 10169 } 10170 10171 free(domain_visited); 10172 10173 return 0; 10174 } 10175 10176 void added_perf_counters_init(void) 10177 { 10178 if (added_perf_counters_init_(sys.perf_tp)) 10179 errx(1, "%s: %s", __func__, "thread"); 10180 10181 if (added_perf_counters_init_(sys.perf_cp)) 10182 errx(1, "%s: %s", __func__, "core"); 10183 10184 if (added_perf_counters_init_(sys.perf_pp)) 10185 errx(1, "%s: %s", __func__, "package"); 10186 } 10187 10188 int parse_telem_info_file(int fd_dir, const char *info_filename, const char *format, unsigned long *output) 10189 { 10190 int fd_telem_info; 10191 FILE *file_telem_info; 10192 unsigned long value; 10193 10194 fd_telem_info = openat(fd_dir, info_filename, O_RDONLY); 10195 if (fd_telem_info == -1) 10196 return -1; 10197 10198 file_telem_info = fdopen(fd_telem_info, "r"); 10199 if (file_telem_info == NULL) { 10200 close(fd_telem_info); 10201 return -1; 10202 } 10203 10204 if (fscanf(file_telem_info, format, &value) != 1) { 10205 fclose(file_telem_info); 10206 return -1; 10207 } 10208 10209 fclose(file_telem_info); 10210 10211 *output = value; 10212 10213 return 0; 10214 } 10215 10216 struct pmt_mmio *pmt_mmio_open(unsigned int target_guid) 10217 { 10218 struct pmt_diriter_t pmt_iter; 10219 const struct dirent *entry; 10220 struct stat st; 10221 int fd_telem_dir, fd_pmt; 10222 unsigned long guid, size, offset; 10223 size_t mmap_size; 10224 void *mmio; 10225 struct pmt_mmio *head = NULL, *last = NULL; 10226 struct pmt_mmio *new_pmt = NULL; 10227 10228 if (stat(SYSFS_TELEM_PATH, &st) == -1) 10229 return NULL; 10230 10231 pmt_diriter_init(&pmt_iter); 10232 entry = pmt_diriter_begin(&pmt_iter, SYSFS_TELEM_PATH); 10233 if (!entry) { 10234 pmt_diriter_remove(&pmt_iter); 10235 return NULL; 10236 } 10237 10238 for (; entry != NULL; entry = pmt_diriter_next(&pmt_iter)) { 10239 if (fstatat(dirfd(pmt_iter.dir), entry->d_name, &st, 0) == -1) 10240 break; 10241 10242 if (!S_ISDIR(st.st_mode)) 10243 continue; 10244 10245 fd_telem_dir = openat(dirfd(pmt_iter.dir), entry->d_name, O_RDONLY); 10246 if (fd_telem_dir == -1) 10247 break; 10248 10249 if (parse_telem_info_file(fd_telem_dir, "guid", "%lx", &guid)) { 10250 close(fd_telem_dir); 10251 break; 10252 } 10253 10254 if (parse_telem_info_file(fd_telem_dir, "size", "%lu", &size)) { 10255 close(fd_telem_dir); 10256 break; 10257 } 10258 10259 if (guid != target_guid) { 10260 close(fd_telem_dir); 10261 continue; 10262 } 10263 10264 if (parse_telem_info_file(fd_telem_dir, "offset", "%lu", &offset)) { 10265 close(fd_telem_dir); 10266 break; 10267 } 10268 10269 fd_pmt = openat(fd_telem_dir, "telem", O_RDONLY); 10270 if (fd_pmt == -1) 10271 goto loop_cleanup_and_break; 10272 10273 mmap_size = ROUND_UP_TO_PAGE_SIZE(offset + size); 10274 mmio = mmap(0, mmap_size, PROT_READ, MAP_SHARED, fd_pmt, 0); 10275 if (mmio != MAP_FAILED) { 10276 if (debug) 10277 fprintf(stderr, "%s: 0x%lx mmaped at: %p\n", __func__, guid, mmio); 10278 10279 new_pmt = calloc(1, sizeof(*new_pmt)); 10280 10281 if (!new_pmt) { 10282 fprintf(stderr, "%s: Failed to allocate pmt_mmio\n", __func__); 10283 exit(1); 10284 } 10285 10286 /* 10287 * Create linked list of mmaped regions, 10288 * but preserve the ordering from sysfs. 10289 * Ordering is important for the user to 10290 * use the seq=%u parameter when adding a counter. 10291 */ 10292 new_pmt->guid = guid; 10293 new_pmt->mmio_base = mmio; 10294 new_pmt->pmt_offset = offset; 10295 new_pmt->size = size; 10296 new_pmt->next = pmt_mmios; 10297 10298 if (last) 10299 last->next = new_pmt; 10300 else 10301 head = new_pmt; 10302 10303 last = new_pmt; 10304 } 10305 10306 loop_cleanup_and_break: 10307 close(fd_pmt); 10308 close(fd_telem_dir); 10309 } 10310 10311 pmt_diriter_remove(&pmt_iter); 10312 10313 /* 10314 * If we found something, stick just 10315 * created linked list to the front. 10316 */ 10317 if (head) 10318 pmt_mmios = head; 10319 10320 return head; 10321 } 10322 10323 struct pmt_mmio *pmt_mmio_find(unsigned int guid) 10324 { 10325 struct pmt_mmio *pmmio = pmt_mmios; 10326 10327 while (pmmio) { 10328 if (pmmio->guid == guid) 10329 return pmmio; 10330 10331 pmmio = pmmio->next; 10332 } 10333 10334 return NULL; 10335 } 10336 10337 void *pmt_get_counter_pointer(struct pmt_mmio *pmmio, unsigned long counter_offset) 10338 { 10339 char *ret; 10340 10341 /* Get base of mmaped PMT file. */ 10342 ret = (char *)pmmio->mmio_base; 10343 10344 /* 10345 * Apply PMT MMIO offset to obtain beginning of the mmaped telemetry data. 10346 * It's not guaranteed that the mmaped memory begins with the telemetry data 10347 * - we might have to apply the offset first. 10348 */ 10349 ret += pmmio->pmt_offset; 10350 10351 /* Apply the counter offset to get the address to the mmaped counter. */ 10352 ret += counter_offset; 10353 10354 return ret; 10355 } 10356 10357 struct pmt_mmio *pmt_add_guid(unsigned int guid, unsigned int seq) 10358 { 10359 struct pmt_mmio *ret; 10360 10361 ret = pmt_mmio_find(guid); 10362 if (!ret) 10363 ret = pmt_mmio_open(guid); 10364 10365 while (ret && seq) { 10366 ret = ret->next; 10367 --seq; 10368 } 10369 10370 return ret; 10371 } 10372 10373 enum pmt_open_mode { 10374 PMT_OPEN_TRY, /* Open failure is not an error. */ 10375 PMT_OPEN_REQUIRED, /* Open failure is a fatal error. */ 10376 }; 10377 10378 struct pmt_counter *pmt_find_counter(struct pmt_counter *pcounter, const char *name) 10379 { 10380 while (pcounter) { 10381 if (strcmp(pcounter->name, name) == 0) 10382 break; 10383 10384 pcounter = pcounter->next; 10385 } 10386 10387 return pcounter; 10388 } 10389 10390 struct pmt_counter **pmt_get_scope_root(enum counter_scope scope) 10391 { 10392 switch (scope) { 10393 case SCOPE_CPU: 10394 return &sys.pmt_tp; 10395 case SCOPE_CORE: 10396 return &sys.pmt_cp; 10397 case SCOPE_PACKAGE: 10398 return &sys.pmt_pp; 10399 } 10400 10401 __builtin_unreachable(); 10402 } 10403 10404 void pmt_counter_add_domain(struct pmt_counter *pcounter, unsigned long *pmmio, unsigned int domain_id) 10405 { 10406 /* Make sure the new domain fits. */ 10407 if (domain_id >= pcounter->num_domains) 10408 pmt_counter_resize(pcounter, domain_id + 1); 10409 10410 assert(pcounter->domains); 10411 assert(domain_id < pcounter->num_domains); 10412 10413 pcounter->domains[domain_id].pcounter = pmmio; 10414 } 10415 10416 int pmt_add_counter(unsigned int guid, unsigned int seq, const char *name, enum pmt_datatype type, 10417 unsigned int lsb, unsigned int msb, unsigned int offset, enum counter_scope scope, enum counter_format format, unsigned int domain_id, enum pmt_open_mode mode) 10418 { 10419 struct pmt_mmio *mmio; 10420 struct pmt_counter *pcounter; 10421 struct pmt_counter **const pmt_root = pmt_get_scope_root(scope); 10422 bool new_counter = false; 10423 int conflict = 0; 10424 10425 if (lsb > msb) { 10426 fprintf(stderr, "%s: %s: `%s` must be satisfied\n", __func__, "lsb <= msb", name); 10427 exit(1); 10428 } 10429 10430 if (msb >= 64) { 10431 fprintf(stderr, "%s: %s: `%s` must be satisfied\n", __func__, "msb < 64", name); 10432 exit(1); 10433 } 10434 10435 mmio = pmt_add_guid(guid, seq); 10436 if (!mmio) { 10437 if (mode != PMT_OPEN_TRY) { 10438 fprintf(stderr, "%s: failed to map PMT MMIO for guid %x, seq %u\n", __func__, guid, seq); 10439 exit(1); 10440 } 10441 10442 return 1; 10443 } 10444 10445 if (offset > mmio->size || mmio->size - offset < 8) { 10446 if (mode != PMT_OPEN_TRY) { 10447 fprintf(stderr, "%s: offset %u outside of PMT MMIO size %u\n", __func__, offset, mmio->size); 10448 exit(1); 10449 } 10450 10451 return 1; 10452 } 10453 10454 pcounter = pmt_find_counter(*pmt_root, name); 10455 if (!pcounter) { 10456 pcounter = calloc(1, sizeof(*pcounter)); 10457 if (!pcounter) 10458 return 1; 10459 new_counter = true; 10460 } 10461 10462 if (new_counter) { 10463 strncpy(pcounter->name, name, ARRAY_SIZE(pcounter->name) - 1); 10464 pcounter->type = type; 10465 pcounter->scope = scope; 10466 pcounter->lsb = lsb; 10467 pcounter->msb = msb; 10468 pcounter->format = format; 10469 } else { 10470 conflict += pcounter->type != type; 10471 conflict += pcounter->scope != scope; 10472 conflict += pcounter->lsb != lsb; 10473 conflict += pcounter->msb != msb; 10474 conflict += pcounter->format != format; 10475 } 10476 10477 if (conflict) { 10478 fprintf(stderr, "%s: conflicting parameters for the PMT counter with the same name %s\n", __func__, name); 10479 exit(1); 10480 } 10481 10482 pmt_counter_add_domain(pcounter, pmt_get_counter_pointer(mmio, offset), domain_id); 10483 10484 if (new_counter) { 10485 pcounter->next = *pmt_root; 10486 *pmt_root = pcounter; 10487 } 10488 10489 return 0; 10490 } 10491 10492 void pmt_init(void) 10493 { 10494 int cpu_num; 10495 unsigned long seq, offset, mod_num; 10496 10497 if (BIC_IS_ENABLED(BIC_Diec6)) { 10498 pmt_add_counter(PMT_MTL_DC6_GUID, PMT_MTL_DC6_SEQ, "Die%c6", PMT_TYPE_XTAL_TIME, 10499 PMT_COUNTER_MTL_DC6_LSB, PMT_COUNTER_MTL_DC6_MSB, PMT_COUNTER_MTL_DC6_OFFSET, SCOPE_PACKAGE, FORMAT_DELTA, 0, PMT_OPEN_TRY); 10500 } 10501 10502 if (BIC_IS_ENABLED(BIC_CPU_c1e)) { 10503 seq = 0; 10504 offset = PMT_COUNTER_CWF_MC1E_OFFSET_BASE; 10505 mod_num = 0; /* Relative module number for current PMT file. */ 10506 10507 /* Open the counter for each CPU. */ 10508 for (cpu_num = 0; cpu_num <= topo.max_cpu_num;) { 10509 10510 if (cpu_is_not_allowed(cpu_num)) 10511 goto next_loop_iter; 10512 10513 /* 10514 * Set the scope to CPU, even though CWF report the counter per module. 10515 * CPUs inside the same module will read from the same location, instead of reporting zeros. 10516 * 10517 * CWF with newer firmware might require a PMT_TYPE_XTAL_TIME instead of PMT_TYPE_TCORE_CLOCK. 10518 */ 10519 pmt_add_counter(PMT_CWF_MC1E_GUID, seq, "CPU%c1e", PMT_TYPE_TCORE_CLOCK, 10520 PMT_COUNTER_CWF_MC1E_LSB, PMT_COUNTER_CWF_MC1E_MSB, offset, SCOPE_CPU, FORMAT_DELTA, cpu_num, PMT_OPEN_TRY); 10521 10522 /* 10523 * Rather complex logic for each time we go to the next loop iteration, 10524 * so keep it as a label. 10525 */ 10526 next_loop_iter: 10527 /* 10528 * Advance the cpu number and check if we should also advance offset to 10529 * the next counter inside the PMT file. 10530 * 10531 * On Clearwater Forest platform, the counter is reported per module, 10532 * so open the same counter for all of the CPUs inside the module. 10533 * That way, reported table show the correct value for all of the CPUs inside the module, 10534 * instead of zeros. 10535 */ 10536 ++cpu_num; 10537 if (cpu_num % PMT_COUNTER_CWF_CPUS_PER_MODULE == 0) { 10538 offset += PMT_COUNTER_CWF_MC1E_OFFSET_INCREMENT; 10539 ++mod_num; 10540 } 10541 10542 /* 10543 * There are PMT_COUNTER_CWF_MC1E_NUM_MODULES_PER_FILE in each PMT file. 10544 * 10545 * If that number is reached, seq must be incremented to advance to the next file in a sequence. 10546 * Offset inside that file and a module counter has to be reset. 10547 */ 10548 if (mod_num == PMT_COUNTER_CWF_MC1E_NUM_MODULES_PER_FILE) { 10549 ++seq; 10550 offset = PMT_COUNTER_CWF_MC1E_OFFSET_BASE; 10551 mod_num = 0; 10552 } 10553 } 10554 } 10555 } 10556 10557 void turbostat_init() 10558 { 10559 setup_all_buffers(true); 10560 set_master_cpu(); 10561 check_msr_access(); 10562 check_perf_access(); 10563 process_cpuid(); 10564 counter_info_init(); 10565 probe_pm_features(); 10566 msr_perf_init(); 10567 linux_perf_init(); 10568 rapl_perf_init(); 10569 cstate_perf_init(); 10570 perf_llc_init(); 10571 perf_l2_init(); 10572 added_perf_counters_init(); 10573 pmt_init(); 10574 10575 for_all_cpus(get_cpu_type, ODD_COUNTERS); 10576 for_all_cpus(get_cpu_type, EVEN_COUNTERS); 10577 10578 if (BIC_IS_ENABLED(BIC_IPC) && has_aperf_access && get_instr_count_fd(master_cpu) != -1) 10579 BIC_PRESENT(BIC_IPC); 10580 10581 /* 10582 * If TSC tweak is needed, but couldn't get it, 10583 * disable more BICs, since it can't be reported accurately. 10584 */ 10585 if (platform->enable_tsc_tweak && !has_base_hz) { 10586 CLR_BIC(BIC_Busy, &bic_enabled); 10587 CLR_BIC(BIC_Bzy_MHz, &bic_enabled); 10588 } 10589 } 10590 10591 void affinitize_child(void) 10592 { 10593 /* Prefer cpu_possible_set, if available */ 10594 if (sched_setaffinity(0, cpu_setsize, cpu_possible_set)) { 10595 warn("sched_setaffinity cpu_possible_set"); 10596 10597 /* Otherwise, allow child to run on same cpu set as turbostat */ 10598 if (sched_setaffinity(0, cpu_setsize, cpu_allowed_set)) 10599 warn("sched_setaffinity cpu_allowed_set"); 10600 } 10601 } 10602 10603 int fork_it(char **argv) 10604 { 10605 pid_t child_pid; 10606 int status; 10607 10608 snapshot_proc_sysfs_files(); 10609 status = for_all_cpus(get_counters, EVEN_COUNTERS); 10610 first_counter_read = 0; 10611 if (status) 10612 exit(status); 10613 gettimeofday(&tv_even, (struct timezone *)NULL); 10614 10615 child_pid = fork(); 10616 if (!child_pid) { 10617 /* child */ 10618 affinitize_child(); 10619 execvp(argv[0], argv); 10620 err(errno, "exec %s", argv[0]); 10621 } else { 10622 10623 /* parent */ 10624 if (child_pid == -1) 10625 err(1, "fork"); 10626 10627 signal(SIGINT, SIG_IGN); 10628 signal(SIGQUIT, SIG_IGN); 10629 if (waitpid(child_pid, &status, 0) == -1) 10630 err(status, "waitpid"); 10631 10632 if (WIFEXITED(status)) 10633 status = WEXITSTATUS(status); 10634 } 10635 /* 10636 * n.b. fork_it() does not check for errors from for_all_cpus() 10637 * because re-starting is problematic when forking 10638 */ 10639 snapshot_proc_sysfs_files(); 10640 for_all_cpus(get_counters, ODD_COUNTERS); 10641 gettimeofday(&tv_odd, (struct timezone *)NULL); 10642 timersub(&tv_odd, &tv_even, &tv_delta); 10643 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 10644 fprintf(outf, "%s: Counter reset detected\n", progname); 10645 delta_platform(&platform_counters_odd, &platform_counters_even); 10646 10647 compute_average(EVEN_COUNTERS); 10648 format_all_counters(EVEN_COUNTERS); 10649 10650 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 10651 10652 flush_output_stderr(); 10653 10654 return status; 10655 } 10656 10657 int get_and_dump_counters(void) 10658 { 10659 int status; 10660 10661 snapshot_proc_sysfs_files(); 10662 status = for_all_cpus(get_counters, ODD_COUNTERS); 10663 if (status) 10664 return status; 10665 10666 status = for_all_cpus(dump_counters, ODD_COUNTERS); 10667 if (status) 10668 return status; 10669 10670 flush_output_stdout(); 10671 10672 return status; 10673 } 10674 10675 void print_version() 10676 { 10677 fprintf(outf, "turbostat version 2026.10.31 - Len Brown <lenb@kernel.org>\n"); 10678 } 10679 10680 #define COMMAND_LINE_SIZE 2048 10681 10682 void print_bootcmd(void) 10683 { 10684 char bootcmd[COMMAND_LINE_SIZE]; 10685 FILE *fp; 10686 int ret; 10687 10688 memset(bootcmd, 0, COMMAND_LINE_SIZE); 10689 fp = fopen("/proc/cmdline", "r"); 10690 if (!fp) 10691 return; 10692 10693 ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp); 10694 if (ret) { 10695 bootcmd[ret] = '\0'; 10696 /* the last character is already '\n' */ 10697 fprintf(outf, "Kernel command line: %s", bootcmd); 10698 } 10699 10700 fclose(fp); 10701 } 10702 10703 struct msr_counter *find_msrp_by_name(struct msr_counter *head, const char *name) 10704 { 10705 struct msr_counter *mp; 10706 10707 for (mp = head; mp; mp = mp->next) { 10708 if (debug) 10709 fprintf(stderr, "%s: %s %s\n", __func__, name, mp->name); 10710 if (!strcmp(name, mp->name)) 10711 return mp; 10712 } 10713 return NULL; 10714 } 10715 10716 int add_counter(unsigned int msr_num, const char *path, const char *name, unsigned int width, enum counter_scope scope, enum counter_type type, enum counter_format format, int flags, int id) 10717 { 10718 struct msr_counter *msrp; 10719 10720 if (no_msr && msr_num) 10721 errx(1, "Requested MSR counter 0x%x, but in --no-msr mode", msr_num); 10722 10723 if (debug) 10724 fprintf(stderr, "%s(msr%d, %s, %s, width%d, scope%d, type%d, format%d, flags%x, id%d)\n", __func__, msr_num, path, name, width, scope, type, format, flags, id); 10725 10726 switch (scope) { 10727 10728 case SCOPE_CPU: 10729 msrp = find_msrp_by_name(sys.tp, name); 10730 if (msrp) { 10731 if (debug) 10732 fprintf(stderr, "%s: %s FOUND\n", __func__, name); 10733 break; 10734 } 10735 if (sys.added_thread_counters >= MAX_ADDED_THREAD_COUNTERS) { 10736 warnx("ignoring thread counter %s", name); 10737 return -1; 10738 } 10739 sys.added_thread_counters++; 10740 break; 10741 case SCOPE_CORE: 10742 msrp = find_msrp_by_name(sys.cp, name); 10743 if (msrp) { 10744 if (debug) 10745 fprintf(stderr, "%s: %s FOUND\n", __func__, name); 10746 break; 10747 } 10748 if (sys.added_core_counters >= MAX_ADDED_CORE_COUNTERS) { 10749 warnx("ignoring core counter %s", name); 10750 return -1; 10751 } 10752 sys.added_core_counters++; 10753 break; 10754 case SCOPE_PACKAGE: 10755 msrp = find_msrp_by_name(sys.pp, name); 10756 if (msrp) { 10757 if (debug) 10758 fprintf(stderr, "%s: %s FOUND\n", __func__, name); 10759 break; 10760 } 10761 if (sys.added_package_counters >= MAX_ADDED_PACKAGE_COUNTERS) { 10762 warnx("ignoring package counter %s", name); 10763 return -1; 10764 } 10765 sys.added_package_counters++; 10766 break; 10767 default: 10768 warnx("ignoring counter %s with unknown scope", name); 10769 return -1; 10770 } 10771 10772 if (msrp == NULL) { 10773 msrp = calloc(1, sizeof(struct msr_counter)); 10774 if (msrp == NULL) 10775 err(-1, "calloc msr_counter"); 10776 10777 msrp->msr_num = msr_num; 10778 strncpy(msrp->name, name, NAME_BYTES - 1); 10779 msrp->width = width; 10780 msrp->type = type; 10781 msrp->format = format; 10782 msrp->flags = flags; 10783 10784 switch (scope) { 10785 case SCOPE_CPU: 10786 msrp->next = sys.tp; 10787 sys.tp = msrp; 10788 break; 10789 case SCOPE_CORE: 10790 msrp->next = sys.cp; 10791 sys.cp = msrp; 10792 break; 10793 case SCOPE_PACKAGE: 10794 msrp->next = sys.pp; 10795 sys.pp = msrp; 10796 break; 10797 } 10798 } 10799 10800 if (path) { 10801 struct sysfs_path *sp; 10802 10803 sp = calloc(1, sizeof(struct sysfs_path)); 10804 if (sp == NULL) 10805 err(-1, "%s", path); 10806 strncpy(sp->path, path, PATH_BYTES - 1); 10807 sp->id = id; 10808 sp->next = msrp->sp; 10809 msrp->sp = sp; 10810 } 10811 10812 return 0; 10813 } 10814 10815 /* 10816 * Initialize the fields used for identifying and opening the counter. 10817 * 10818 * Defer the initialization of any runtime buffers for actually reading 10819 * the counters for when we initialize all perf counters, so we can later 10820 * easily call re_initialize(). 10821 */ 10822 struct perf_counter_info *make_perf_counter_info(const char *perf_device, 10823 const char *perf_event, const char *name, unsigned int width, enum counter_scope scope, enum counter_type type, enum counter_format format) 10824 { 10825 struct perf_counter_info *pinfo; 10826 10827 pinfo = calloc(1, sizeof(*pinfo)); 10828 if (!pinfo) 10829 errx(1, "%s: Failed to allocate %s/%s\n", __func__, perf_device, perf_event); 10830 10831 strncpy(pinfo->device, perf_device, ARRAY_SIZE(pinfo->device) - 1); 10832 strncpy(pinfo->event, perf_event, ARRAY_SIZE(pinfo->event) - 1); 10833 10834 strncpy(pinfo->name, name, ARRAY_SIZE(pinfo->name) - 1); 10835 pinfo->width = width; 10836 pinfo->scope = scope; 10837 pinfo->type = type; 10838 pinfo->format = format; 10839 10840 return pinfo; 10841 } 10842 10843 int add_perf_counter(const char *perf_device, const char *perf_event, const char *name_buffer, unsigned int width, 10844 enum counter_scope scope, enum counter_type type, enum counter_format format) 10845 { 10846 struct perf_counter_info *pinfo; 10847 10848 switch (scope) { 10849 case SCOPE_CPU: 10850 if (sys.added_thread_perf_counters >= MAX_ADDED_THREAD_COUNTERS) { 10851 warnx("ignoring thread counter perf/%s/%s", perf_device, perf_event); 10852 return -1; 10853 } 10854 break; 10855 10856 case SCOPE_CORE: 10857 if (sys.added_core_perf_counters >= MAX_ADDED_CORE_COUNTERS) { 10858 warnx("ignoring core counter perf/%s/%s", perf_device, perf_event); 10859 return -1; 10860 } 10861 break; 10862 10863 case SCOPE_PACKAGE: 10864 if (sys.added_package_perf_counters >= MAX_ADDED_PACKAGE_COUNTERS) { 10865 warnx("ignoring package counter perf/%s/%s", perf_device, perf_event); 10866 return -1; 10867 } 10868 break; 10869 } 10870 10871 pinfo = make_perf_counter_info(perf_device, perf_event, name_buffer, width, scope, type, format); 10872 10873 if (!pinfo) 10874 return -1; 10875 10876 switch (scope) { 10877 case SCOPE_CPU: 10878 pinfo->next = sys.perf_tp; 10879 sys.perf_tp = pinfo; 10880 ++sys.added_thread_perf_counters; 10881 break; 10882 10883 case SCOPE_CORE: 10884 pinfo->next = sys.perf_cp; 10885 sys.perf_cp = pinfo; 10886 ++sys.added_core_perf_counters; 10887 break; 10888 10889 case SCOPE_PACKAGE: 10890 pinfo->next = sys.perf_pp; 10891 sys.perf_pp = pinfo; 10892 ++sys.added_package_perf_counters; 10893 break; 10894 } 10895 10896 // FIXME: we might not have debug here yet 10897 if (debug) 10898 fprintf(stderr, "%s: %s/%s, name: %s, scope%d\n", __func__, pinfo->device, pinfo->event, pinfo->name, pinfo->scope); 10899 10900 return 0; 10901 } 10902 10903 void parse_add_command_msr(char *add_command) 10904 { 10905 int msr_num = 0; 10906 char *path = NULL; 10907 char perf_device[PERF_DEV_NAME_BYTES] = ""; 10908 char perf_event[PERF_EVT_NAME_BYTES] = ""; 10909 char name_buffer[PERF_NAME_BYTES] = ""; 10910 int width = 64; 10911 int fail = 0; 10912 enum counter_scope scope = SCOPE_CPU; 10913 enum counter_type type = COUNTER_CYCLES; 10914 enum counter_format format = FORMAT_DELTA; 10915 10916 while (add_command) { 10917 10918 if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 10919 goto next; 10920 10921 if (sscanf(add_command, "msr%d", &msr_num) == 1) 10922 goto next; 10923 10924 BUILD_BUG_ON(ARRAY_SIZE(perf_device) <= 31); 10925 BUILD_BUG_ON(ARRAY_SIZE(perf_event) <= 31); 10926 if (sscanf(add_command, "perf/%31[^/]/%31[^,]", &perf_device[0], &perf_event[0]) == 2) 10927 goto next; 10928 10929 if (*add_command == '/') { 10930 path = add_command; 10931 goto next; 10932 } 10933 10934 if (sscanf(add_command, "u%d", &width) == 1) { 10935 if ((width == 32) || (width == 64)) 10936 goto next; 10937 width = 64; 10938 } 10939 if (!strncmp(add_command, "cpu", strlen("cpu"))) { 10940 scope = SCOPE_CPU; 10941 goto next; 10942 } 10943 if (!strncmp(add_command, "core", strlen("core"))) { 10944 scope = SCOPE_CORE; 10945 goto next; 10946 } 10947 if (!strncmp(add_command, "package", strlen("package"))) { 10948 scope = SCOPE_PACKAGE; 10949 goto next; 10950 } 10951 if (!strncmp(add_command, "cycles", strlen("cycles"))) { 10952 type = COUNTER_CYCLES; 10953 goto next; 10954 } 10955 if (!strncmp(add_command, "seconds", strlen("seconds"))) { 10956 type = COUNTER_SECONDS; 10957 goto next; 10958 } 10959 if (!strncmp(add_command, "usec", strlen("usec"))) { 10960 type = COUNTER_USEC; 10961 goto next; 10962 } 10963 if (!strncmp(add_command, "raw", strlen("raw"))) { 10964 format = FORMAT_RAW; 10965 goto next; 10966 } 10967 if (!strncmp(add_command, "average", strlen("average"))) { 10968 format = FORMAT_AVERAGE; 10969 goto next; 10970 } 10971 if (!strncmp(add_command, "delta", strlen("delta"))) { 10972 format = FORMAT_DELTA; 10973 goto next; 10974 } 10975 if (!strncmp(add_command, "percent", strlen("percent"))) { 10976 format = FORMAT_PERCENT; 10977 goto next; 10978 } 10979 10980 BUILD_BUG_ON(ARRAY_SIZE(name_buffer) <= 18); 10981 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { 10982 char *eos; 10983 10984 eos = strchr(name_buffer, ','); 10985 if (eos) 10986 *eos = '\0'; 10987 goto next; 10988 } 10989 10990 next: 10991 add_command = strchr(add_command, ','); 10992 if (add_command) { 10993 *add_command = '\0'; 10994 add_command++; 10995 } 10996 10997 } 10998 if ((msr_num == 0) && (path == NULL) && (perf_device[0] == '\0' || perf_event[0] == '\0')) { 10999 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter | perf/device/event) required\n"); 11000 fail++; 11001 } 11002 11003 /* Test for non-empty perf_device and perf_event */ 11004 const bool is_perf_counter = perf_device[0] && perf_event[0]; 11005 11006 /* generate default column header */ 11007 if (*name_buffer == '\0') { 11008 if (is_perf_counter) { 11009 snprintf(name_buffer, ARRAY_SIZE(name_buffer), "perf/%s", perf_event); 11010 } else { 11011 if (width == 32) 11012 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 11013 else 11014 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 11015 } 11016 } 11017 11018 if (is_perf_counter) { 11019 if (add_perf_counter(perf_device, perf_event, name_buffer, width, scope, type, format)) 11020 fail++; 11021 } else { 11022 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0, 0)) 11023 fail++; 11024 } 11025 11026 if (fail) { 11027 help(); 11028 exit(1); 11029 } 11030 } 11031 11032 bool starts_with(const char *str, const char *prefix) 11033 { 11034 return strncmp(prefix, str, strlen(prefix)) == 0; 11035 } 11036 11037 int pmt_parse_from_path(const char *target_path, unsigned int *out_guid, unsigned int *out_seq) 11038 { 11039 struct pmt_diriter_t pmt_iter; 11040 const struct dirent *dirname; 11041 struct stat stat, target_stat; 11042 int fd_telem_dir = -1; 11043 int fd_target_dir; 11044 unsigned int seq = 0; 11045 unsigned long guid, target_guid; 11046 int ret = -1; 11047 11048 fd_target_dir = open(target_path, O_RDONLY | O_DIRECTORY); 11049 if (fd_target_dir == -1) { 11050 return -1; 11051 } 11052 11053 if (fstat(fd_target_dir, &target_stat) == -1) { 11054 fprintf(stderr, "%s: Failed to stat the target: %s", __func__, strerror(errno)); 11055 exit(1); 11056 } 11057 11058 if (parse_telem_info_file(fd_target_dir, "guid", "%lx", &target_guid)) { 11059 fprintf(stderr, "%s: Failed to parse the target guid file: %s", __func__, strerror(errno)); 11060 exit(1); 11061 } 11062 11063 close(fd_target_dir); 11064 11065 pmt_diriter_init(&pmt_iter); 11066 11067 for (dirname = pmt_diriter_begin(&pmt_iter, SYSFS_TELEM_PATH); dirname != NULL; dirname = pmt_diriter_next(&pmt_iter)) { 11068 11069 fd_telem_dir = openat(dirfd(pmt_iter.dir), dirname->d_name, O_RDONLY | O_DIRECTORY); 11070 if (fd_telem_dir == -1) 11071 continue; 11072 11073 if (parse_telem_info_file(fd_telem_dir, "guid", "%lx", &guid)) { 11074 fprintf(stderr, "%s: Failed to parse the guid file: %s", __func__, strerror(errno)); 11075 continue; 11076 } 11077 11078 if (fstat(fd_telem_dir, &stat) == -1) { 11079 fprintf(stderr, "%s: Failed to stat %s directory: %s", __func__, dirname->d_name, strerror(errno)); 11080 continue; 11081 } 11082 11083 /* 11084 * If reached the same directory as target, exit the loop. 11085 * Seq has the correct value now. 11086 */ 11087 if (stat.st_dev == target_stat.st_dev && stat.st_ino == target_stat.st_ino) { 11088 ret = 0; 11089 break; 11090 } 11091 11092 /* 11093 * If reached directory with the same guid, 11094 * but it's not the target directory yet, 11095 * increment seq and continue the search. 11096 */ 11097 if (guid == target_guid) 11098 ++seq; 11099 11100 close(fd_telem_dir); 11101 fd_telem_dir = -1; 11102 } 11103 11104 pmt_diriter_remove(&pmt_iter); 11105 11106 if (fd_telem_dir != -1) 11107 close(fd_telem_dir); 11108 11109 if (!ret) { 11110 *out_guid = target_guid; 11111 *out_seq = seq; 11112 } 11113 11114 return ret; 11115 } 11116 11117 void parse_add_command_pmt(char *add_command) 11118 { 11119 char *name = NULL; 11120 char *type_name = NULL; 11121 char *format_name = NULL; 11122 char *direct_path = NULL; 11123 static const char direct_path_prefix[] = "path="; 11124 unsigned int offset; 11125 unsigned int lsb; 11126 unsigned int msb; 11127 unsigned int guid; 11128 unsigned int seq = 0; /* By default, pick first file in a sequence with a given GUID. */ 11129 unsigned int domain_id; 11130 enum counter_scope scope = 0; 11131 enum pmt_datatype type = PMT_TYPE_RAW; 11132 enum counter_format format = FORMAT_RAW; 11133 bool has_offset = false; 11134 bool has_lsb = false; 11135 bool has_msb = false; 11136 bool has_format = true; /* Format has a default value. */ 11137 bool has_guid = false; 11138 bool has_scope = false; 11139 bool has_type = true; /* Type has a default value. */ 11140 11141 /* Consume the "pmt," prefix. */ 11142 add_command = strchr(add_command, ','); 11143 if (!add_command) { 11144 help(); 11145 exit(1); 11146 } 11147 ++add_command; 11148 11149 while (add_command) { 11150 if (starts_with(add_command, "name=")) { 11151 name = add_command + strlen("name="); 11152 goto next; 11153 } 11154 11155 if (starts_with(add_command, "type=")) { 11156 type_name = add_command + strlen("type="); 11157 goto next; 11158 } 11159 11160 if (starts_with(add_command, "domain=")) { 11161 const size_t prefix_len = strlen("domain="); 11162 11163 if (sscanf(add_command + prefix_len, "cpu%u", &domain_id) == 1) { 11164 scope = SCOPE_CPU; 11165 has_scope = true; 11166 } else if (sscanf(add_command + prefix_len, "core%u", &domain_id) == 1) { 11167 scope = SCOPE_CORE; 11168 has_scope = true; 11169 } else if (sscanf(add_command + prefix_len, "package%u", &domain_id) == 1) { 11170 scope = SCOPE_PACKAGE; 11171 has_scope = true; 11172 } 11173 11174 if (!has_scope) { 11175 printf("%s: invalid value for scope. Expected cpu%%u, core%%u or package%%u.\n", __func__); 11176 exit(1); 11177 } 11178 11179 goto next; 11180 } 11181 11182 if (starts_with(add_command, "format=")) { 11183 format_name = add_command + strlen("format="); 11184 goto next; 11185 } 11186 11187 if (sscanf(add_command, "offset=%u", &offset) == 1) { 11188 has_offset = true; 11189 goto next; 11190 } 11191 11192 if (sscanf(add_command, "lsb=%u", &lsb) == 1) { 11193 has_lsb = true; 11194 goto next; 11195 } 11196 11197 if (sscanf(add_command, "msb=%u", &msb) == 1) { 11198 has_msb = true; 11199 goto next; 11200 } 11201 11202 if (sscanf(add_command, "guid=%x", &guid) == 1) { 11203 has_guid = true; 11204 goto next; 11205 } 11206 11207 if (sscanf(add_command, "seq=%x", &seq) == 1) 11208 goto next; 11209 11210 if (strncmp(add_command, direct_path_prefix, strlen(direct_path_prefix)) == 0) { 11211 direct_path = add_command + strlen(direct_path_prefix); 11212 goto next; 11213 } 11214 next: 11215 add_command = strchr(add_command, ','); 11216 if (add_command) { 11217 *add_command = '\0'; 11218 add_command++; 11219 } 11220 } 11221 11222 if (!name) { 11223 printf("%s: missing %s\n", __func__, "name"); 11224 exit(1); 11225 } 11226 11227 if (strlen(name) >= PMT_COUNTER_NAME_SIZE_BYTES) { 11228 printf("%s: name has to be at most %d characters long\n", __func__, PMT_COUNTER_NAME_SIZE_BYTES); 11229 exit(1); 11230 } 11231 11232 if (format_name) { 11233 has_format = false; 11234 11235 if (strcmp("raw", format_name) == 0) { 11236 format = FORMAT_RAW; 11237 has_format = true; 11238 } 11239 11240 if (strcmp("average", format_name) == 0) { 11241 format = FORMAT_AVERAGE; 11242 has_format = true; 11243 } 11244 11245 if (strcmp("delta", format_name) == 0) { 11246 format = FORMAT_DELTA; 11247 has_format = true; 11248 } 11249 11250 if (!has_format) { 11251 fprintf(stderr, "%s: Invalid format %s. Expected raw, average or delta\n", __func__, format_name); 11252 exit(1); 11253 } 11254 } 11255 11256 if (type_name) { 11257 has_type = false; 11258 11259 if (strcmp("raw", type_name) == 0) { 11260 type = PMT_TYPE_RAW; 11261 has_type = true; 11262 } 11263 11264 if (strcmp("txtal_time", type_name) == 0) { 11265 type = PMT_TYPE_XTAL_TIME; 11266 has_type = true; 11267 } 11268 11269 if (strcmp("tcore_clock", type_name) == 0) { 11270 type = PMT_TYPE_TCORE_CLOCK; 11271 has_type = true; 11272 } 11273 11274 if (!has_type) { 11275 printf("%s: invalid %s: %s\n", __func__, "type", type_name); 11276 exit(1); 11277 } 11278 } 11279 11280 if (!has_offset) { 11281 printf("%s : missing %s\n", __func__, "offset"); 11282 exit(1); 11283 } 11284 11285 if (!has_lsb) { 11286 printf("%s: missing %s\n", __func__, "lsb"); 11287 exit(1); 11288 } 11289 11290 if (!has_msb) { 11291 printf("%s: missing %s\n", __func__, "msb"); 11292 exit(1); 11293 } 11294 11295 if (direct_path && has_guid) { 11296 printf("%s: path and guid+seq parameters are mutually exclusive\nnotice: passed guid=0x%x and path=%s\n", __func__, guid, direct_path); 11297 exit(1); 11298 } 11299 11300 if (direct_path) { 11301 if (pmt_parse_from_path(direct_path, &guid, &seq)) { 11302 printf("%s: failed to parse PMT file from %s\n", __func__, direct_path); 11303 exit(1); 11304 } 11305 11306 /* GUID was just infered from the direct path. */ 11307 has_guid = true; 11308 } 11309 11310 if (!has_guid) { 11311 printf("%s: missing %s\n", __func__, "guid or path"); 11312 exit(1); 11313 } 11314 11315 if (!has_scope) { 11316 printf("%s: missing %s\n", __func__, "scope"); 11317 exit(1); 11318 } 11319 11320 if (lsb > msb) { 11321 printf("%s: lsb > msb doesn't make sense\n", __func__); 11322 exit(1); 11323 } 11324 11325 pmt_add_counter(guid, seq, name, type, lsb, msb, offset, scope, format, domain_id, PMT_OPEN_REQUIRED); 11326 } 11327 11328 void parse_add_command(char *add_command) 11329 { 11330 if (strncmp(add_command, "pmt", strlen("pmt")) == 0) 11331 return parse_add_command_pmt(add_command); 11332 return parse_add_command_msr(add_command); 11333 } 11334 11335 int is_deferred_add(char *name) 11336 { 11337 int i; 11338 11339 for (i = 0; i < deferred_add_index; ++i) 11340 if (!strcmp(name, deferred_add_names[i])) { 11341 deferred_add_consumed |= (1 << i); 11342 return 1; 11343 } 11344 return 0; 11345 } 11346 11347 int is_deferred_skip(char *name) 11348 { 11349 int i; 11350 11351 for (i = 0; i < deferred_skip_index; ++i) 11352 if (!strcmp(name, deferred_skip_names[i])) { 11353 deferred_skip_consumed |= (1 << i); 11354 return 1; 11355 } 11356 return 0; 11357 } 11358 11359 void verify_deferred_consumed(void) 11360 { 11361 int i; 11362 int fail = 0; 11363 11364 for (i = 0; i < deferred_add_index; ++i) { 11365 if (!(deferred_add_consumed & (1 << i))) { 11366 warnx("Counter '%s' can not be added.", deferred_add_names[i]); 11367 fail++; 11368 } 11369 } 11370 for (i = 0; i < deferred_skip_index; ++i) { 11371 if (!(deferred_skip_consumed & (1 << i))) { 11372 warnx("Counter '%s' can not be skipped.", deferred_skip_names[i]); 11373 fail++; 11374 } 11375 } 11376 if (fail) 11377 exit(-EINVAL); 11378 } 11379 11380 void probe_cpuidle_residency(void) 11381 { 11382 char path[64]; 11383 char name_buf[16]; 11384 FILE *input; 11385 int state; 11386 int min_state = 1024, max_state = 0; 11387 char *sp; 11388 11389 for (state = 10; state >= 0; --state) { 11390 11391 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", master_cpu, state); 11392 input = fopen(path, "r"); 11393 if (input == NULL) 11394 continue; 11395 if (!fgets(name_buf, sizeof(name_buf), input)) 11396 err(1, "%s: failed to read file", path); 11397 11398 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 11399 sp = strchr(name_buf, '-'); 11400 if (!sp) 11401 sp = strchrnul(name_buf, '\n'); 11402 *sp = '%'; 11403 *(sp + 1) = '\0'; 11404 11405 remove_underbar(name_buf); 11406 11407 fclose(input); 11408 11409 sprintf(path, "cpuidle/state%d/time", state); 11410 11411 if (!DO_BIC(BIC_pct_idle) && !is_deferred_add(name_buf)) 11412 continue; 11413 11414 if (is_deferred_skip(name_buf)) 11415 continue; 11416 11417 add_counter(0, path, name_buf, 32, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU, 0); 11418 11419 if (state > max_state) 11420 max_state = state; 11421 if (state < min_state) 11422 min_state = state; 11423 } 11424 } 11425 11426 static bool cpuidle_counter_wanted(char *name) 11427 { 11428 if (is_deferred_skip(name)) 11429 return false; 11430 11431 return DO_BIC(BIC_cpuidle) || is_deferred_add(name); 11432 } 11433 11434 void probe_cpuidle_counts(void) 11435 { 11436 char path[64]; 11437 char name_buf[16]; 11438 FILE *input; 11439 int state; 11440 int min_state = 1024, max_state = 0; 11441 char *sp; 11442 11443 if (!DO_BIC(BIC_cpuidle) && !deferred_add_index) 11444 return; 11445 11446 for (state = 10; state >= 0; --state) { 11447 11448 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", master_cpu, state); 11449 input = fopen(path, "r"); 11450 if (input == NULL) 11451 continue; 11452 if (!fgets(name_buf, sizeof(name_buf), input)) 11453 err(1, "%s: failed to read file", path); 11454 fclose(input); 11455 11456 remove_underbar(name_buf); 11457 11458 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 11459 sp = strchr(name_buf, '-'); 11460 if (!sp) 11461 sp = strchrnul(name_buf, '\n'); 11462 11463 /* 11464 * The 'below' sysfs file always contains 0 for the deepest state (largest index), 11465 * do not add it. 11466 */ 11467 if (state != max_state) { 11468 /* 11469 * Add 'C1+' for C1, and so on. The 'below' sysfs file always contains 0 for 11470 * the last state, so do not add it. 11471 */ 11472 *sp = '+'; 11473 *(sp + 1) = '\0'; 11474 if (cpuidle_counter_wanted(name_buf)) { 11475 sprintf(path, "cpuidle/state%d/below", state); 11476 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0); 11477 } 11478 } 11479 11480 *sp = '\0'; 11481 if (cpuidle_counter_wanted(name_buf)) { 11482 sprintf(path, "cpuidle/state%d/usage", state); 11483 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0); 11484 } 11485 11486 /* 11487 * The 'above' sysfs file always contains 0 for the shallowest state (smallest 11488 * index), do not add it. 11489 */ 11490 if (state != min_state) { 11491 *sp = '-'; 11492 *(sp + 1) = '\0'; 11493 if (cpuidle_counter_wanted(name_buf)) { 11494 sprintf(path, "cpuidle/state%d/above", state); 11495 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0); 11496 } 11497 } 11498 } 11499 } 11500 11501 /* 11502 * parse cpuset with following syntax 11503 * 1,2,4..6,8-10 and set bits in cpuset_cmdline 11504 */ 11505 void parse_cpu_command(char *optarg) 11506 { 11507 if (!strcmp(optarg, "core")) { 11508 if (cpuset_cmdline) 11509 goto error; 11510 show_core_only++; 11511 return; 11512 } 11513 if (!strcmp(optarg, "package")) { 11514 if (cpuset_cmdline) 11515 goto error; 11516 show_pkg_only++; 11517 return; 11518 } 11519 if (show_core_only || show_pkg_only) 11520 goto error; 11521 11522 /* 11523 * cpuset_cmdline is allocated on the first invocation of --cpu 11524 * It is re-used for subsequent invocations. 11525 * It is never freed. 11526 */ 11527 if (!cpuset_cmdline) { 11528 cpuset_cmdline = CPU_ALLOC(topo.max_cpu_num + 1); 11529 if (cpuset_cmdline == NULL) 11530 err(3, "CPU_ALLOC"); 11531 11532 CPU_ZERO_S(cpu_setsize, cpuset_cmdline); 11533 } 11534 11535 if (parse_cpu_str(optarg, cpuset_cmdline, cpu_setsize)) 11536 goto error; 11537 if (debug) 11538 print_cpu_set("--cpu", cpuset_cmdline); 11539 11540 return; 11541 11542 error: 11543 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 11544 help(); 11545 exit(-1); 11546 } 11547 11548 void cmdline(int argc, char **argv) 11549 { 11550 int opt; 11551 int option_index = 0; 11552 static struct option long_options[] = { 11553 { "add", required_argument, 0, 'a' }, 11554 { "cpu", required_argument, 0, 'c' }, 11555 { "Dump", no_argument, 0, 'D' }, 11556 { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 11557 { "enable", required_argument, 0, 'e' }, 11558 { "force", no_argument, 0, 'f' }, 11559 { "interval", required_argument, 0, 'i' }, 11560 { "IPC", no_argument, 0, 'I' }, 11561 { "num_iterations", required_argument, 0, 'n' }, 11562 { "header_iterations", required_argument, 0, 'N' }, 11563 { "help", no_argument, 0, 'h' }, 11564 { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 11565 { "Joules", no_argument, 0, 'J' }, 11566 { "list", no_argument, 0, 'l' }, 11567 { "out", required_argument, 0, 'o' }, 11568 { "quiet", no_argument, 0, 'q' }, 11569 { "no-msr", no_argument, 0, 'M' }, 11570 { "no-perf", no_argument, 0, 'P' }, 11571 { "show", required_argument, 0, 's' }, 11572 { "Summary", no_argument, 0, 'S' }, 11573 { "TCC", required_argument, 0, 'T' }, 11574 { "version", no_argument, 0, 'v' }, 11575 { 0, 0, 0, 0 } 11576 }; 11577 11578 progname = argv[0]; 11579 11580 /* 11581 * Parse some options early, because they may make other options invalid, 11582 * like adding the MSR counter with --add and at the same time using --no-msr. 11583 */ 11584 while ((opt = getopt_long_only(argc, argv, "+:MP", long_options, &option_index)) != -1) { 11585 switch (opt) { 11586 case 'M': 11587 no_msr = 1; 11588 break; 11589 case 'P': 11590 no_perf = 1; 11591 break; 11592 default: 11593 break; 11594 } 11595 } 11596 optind = 0; 11597 11598 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:N:o:qMPST:v", long_options, &option_index)) != -1) { 11599 switch (opt) { 11600 case 'a': 11601 parse_add_command(optarg); 11602 break; 11603 case 'c': 11604 parse_cpu_command(optarg); 11605 break; 11606 case 'D': 11607 dump_only++; 11608 /* 11609 * Force the no_perf early to prevent using it as a source. 11610 * User asks for raw values, but perf returns them relative 11611 * to the opening of the file descriptor. 11612 */ 11613 no_perf = 1; 11614 break; 11615 case 'e': 11616 /* --enable specified counter, without clearning existing list */ 11617 bic_lookup(&bic_enabled, optarg, SHOW_LIST); 11618 break; 11619 case 'f': 11620 force_load = true; 11621 break; 11622 case 'd': 11623 debug++; 11624 bic_set_all(&bic_enabled); 11625 break; 11626 case 'H': 11627 /* 11628 * --hide: do not show those specified 11629 * multiple invocations simply clear more bits in enabled mask 11630 */ 11631 { 11632 bic_set_t bic_group_hide; 11633 11634 BIC_INIT(&bic_group_hide); 11635 11636 bic_lookup(&bic_group_hide, optarg, HIDE_LIST); 11637 bic_clear_bits(&bic_enabled, &bic_group_hide); 11638 } 11639 break; 11640 case 'h': 11641 help(); 11642 exit(1); 11643 case 'i': 11644 { 11645 double interval = strtod(optarg, NULL); 11646 11647 if (interval < 0.001) { 11648 fprintf(outf, "interval %f seconds is too small\n", interval); 11649 exit(2); 11650 } 11651 11652 interval_tv.tv_sec = interval_ts.tv_sec = interval; 11653 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 11654 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 11655 } 11656 break; 11657 case 'J': 11658 rapl_joules++; 11659 break; 11660 case 'l': 11661 bic_set_all(&bic_enabled); 11662 list_header_only++; 11663 quiet++; 11664 break; 11665 case 'o': 11666 outf = fopen_or_die(optarg, "w"); 11667 break; 11668 case 'q': 11669 quiet = 1; 11670 break; 11671 case 'M': 11672 case 'P': 11673 /* Parsed earlier */ 11674 break; 11675 case 'n': 11676 num_iterations = strtoul(optarg, NULL, 0); 11677 errno = 0; 11678 11679 if (errno || num_iterations == 0) 11680 errx(-1, "invalid iteration count: %s", optarg); 11681 break; 11682 case 'N': 11683 header_iterations = strtoul(optarg, NULL, 0); 11684 errno = 0; 11685 11686 if (errno || header_iterations == 0) 11687 errx(-1, "invalid header iteration count: %s", optarg); 11688 break; 11689 case 's': 11690 /* 11691 * --show: show only those specified 11692 * The 1st invocation will clear and replace the enabled mask 11693 * subsequent invocations can add to it. 11694 */ 11695 if (shown == 0) 11696 BIC_INIT(&bic_enabled); 11697 bic_lookup(&bic_enabled, optarg, SHOW_LIST); 11698 shown = 1; 11699 break; 11700 case 'S': 11701 summary_only++; 11702 break; 11703 case 'T': 11704 tj_max_override = atoi(optarg); 11705 break; 11706 case 'v': 11707 print_version(); 11708 exit(0); 11709 break; 11710 default: 11711 help(); 11712 exit(1); 11713 } 11714 } 11715 } 11716 11717 void set_rlimit(void) 11718 { 11719 struct rlimit limit; 11720 11721 if (getrlimit(RLIMIT_NOFILE, &limit) < 0) 11722 err(1, "Failed to get rlimit"); 11723 11724 if (limit.rlim_max < MAX_NOFILE) 11725 limit.rlim_max = MAX_NOFILE; 11726 if (limit.rlim_cur < MAX_NOFILE) 11727 limit.rlim_cur = MAX_NOFILE; 11728 11729 if (setrlimit(RLIMIT_NOFILE, &limit) < 0) 11730 err(1, "Failed to set rlimit"); 11731 } 11732 11733 int main(int argc, char **argv) 11734 { 11735 int fd, ret; 11736 11737 bic_groups_init(); 11738 11739 fd = open("/sys/fs/cgroup/cgroup.procs", O_WRONLY); 11740 if (fd < 0) 11741 goto skip_cgroup_setting; 11742 11743 ret = write(fd, "0\n", 2); 11744 if (ret == -1) 11745 warn("/sys/fs/cgroup/cgroup.procs"); 11746 11747 close(fd); 11748 11749 skip_cgroup_setting: 11750 11751 /* probe max_cpu_num and set cpu_setsize early for use by cmdline() */ 11752 topo.max_cpu_num = get_max_cpu_num("/sys/devices/system/cpu/possible"); 11753 cpu_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 11754 11755 outf = stderr; 11756 cmdline(argc, argv); 11757 11758 if (!quiet) { 11759 print_version(); 11760 print_bootcmd(); 11761 } 11762 11763 probe_cpuidle_residency(); 11764 probe_cpuidle_counts(); 11765 11766 verify_deferred_consumed(); 11767 11768 if (!getuid()) 11769 set_rlimit(); 11770 11771 turbostat_init(); 11772 11773 if (!no_msr) 11774 msr_sum_record(); 11775 11776 /* dump counters and exit */ 11777 if (dump_only) { 11778 dump_bic_sets(); 11779 return get_and_dump_counters(); 11780 } 11781 11782 /* list header and exit */ 11783 if (list_header_only) { 11784 print_header(","); 11785 flush_output_stdout(); 11786 return 0; 11787 } 11788 11789 /* 11790 * if any params left, it must be a command to fork 11791 */ 11792 if (argc - optind) 11793 return fork_it(argv + optind); 11794 else 11795 turbostat_loop(); 11796 11797 return 0; 11798 } 11799