1 /* 2 * kmp_utility.cpp -- Utility routines for the OpenMP support library. 3 */ 4 5 //===----------------------------------------------------------------------===// 6 // 7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 8 // See https://llvm.org/LICENSE.txt for license information. 9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "kmp.h" 14 #include "kmp_i18n.h" 15 #include "kmp_str.h" 16 #include "kmp_wrapper_getpid.h" 17 #include <float.h> 18 19 static const char *unknown = "unknown"; 20 21 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 22 23 /* NOTE: If called before serial_initialize (i.e. from runtime_initialize), then 24 the debugging package has not been initialized yet, and only "0" will print 25 debugging output since the environment variables have not been read. */ 26 27 #ifdef KMP_DEBUG 28 static int trace_level = 5; 29 #endif 30 31 /* LOG_ID_BITS = ( 1 + floor( log_2( max( log_per_phy - 1, 1 )))) 32 * APIC_ID = (PHY_ID << LOG_ID_BITS) | LOG_ID 33 * PHY_ID = APIC_ID >> LOG_ID_BITS 34 */ 35 int __kmp_get_physical_id(int log_per_phy, int apic_id) { 36 int index_lsb, index_msb, temp; 37 38 if (log_per_phy > 1) { 39 index_lsb = 0; 40 index_msb = 31; 41 42 temp = log_per_phy; 43 while ((temp & 1) == 0) { 44 temp >>= 1; 45 index_lsb++; 46 } 47 48 temp = log_per_phy; 49 while ((temp & 0x80000000) == 0) { 50 temp <<= 1; 51 index_msb--; 52 } 53 54 /* If >1 bits were set in log_per_phy, choose next higher power of 2 */ 55 if (index_lsb != index_msb) 56 index_msb++; 57 58 return ((int)(apic_id >> index_msb)); 59 } 60 61 return apic_id; 62 } 63 64 /* 65 * LOG_ID_BITS = ( 1 + floor( log_2( max( log_per_phy - 1, 1 )))) 66 * APIC_ID = (PHY_ID << LOG_ID_BITS) | LOG_ID 67 * LOG_ID = APIC_ID & (( 1 << LOG_ID_BITS ) - 1 ) 68 */ 69 int __kmp_get_logical_id(int log_per_phy, int apic_id) { 70 unsigned current_bit; 71 int bits_seen; 72 73 if (log_per_phy <= 1) 74 return (0); 75 76 bits_seen = 0; 77 78 for (current_bit = 1; log_per_phy != 0; current_bit <<= 1) { 79 if (log_per_phy & current_bit) { 80 log_per_phy &= ~current_bit; 81 bits_seen++; 82 } 83 } 84 85 /* If exactly 1 bit was set in log_per_phy, choose next lower power of 2 */ 86 if (bits_seen == 1) { 87 current_bit >>= 1; 88 } 89 90 return ((int)((current_bit - 1) & apic_id)); 91 } 92 93 static kmp_uint64 __kmp_parse_frequency( // R: Frequency in Hz. 94 char const *frequency // I: Float number and unit: MHz, GHz, or TGz. 95 ) { 96 97 double value = 0.0; 98 char *unit = NULL; 99 kmp_uint64 result = 0; /* Zero is a better unknown value than all ones. */ 100 101 if (frequency == NULL) { 102 return result; 103 } 104 value = strtod(frequency, &unit); 105 if (0 < value && 106 value <= DBL_MAX) { // Good value (not overflow, underflow, etc). 107 if (strcmp(unit, "MHz") == 0) { 108 value = value * 1.0E+6; 109 } else if (strcmp(unit, "GHz") == 0) { 110 value = value * 1.0E+9; 111 } else if (strcmp(unit, "THz") == 0) { 112 value = value * 1.0E+12; 113 } else { // Wrong unit. 114 return result; 115 } 116 result = (kmp_uint64)value; // rounds down 117 } 118 return result; 119 120 } // func __kmp_parse_cpu_frequency 121 122 void __kmp_query_cpuid(kmp_cpuinfo_t *p) { 123 struct kmp_cpuid buf; 124 int max_arg; 125 int log_per_phy; 126 #ifdef KMP_DEBUG 127 int cflush_size; 128 #endif 129 130 p->initialized = 1; 131 132 p->sse2 = 1; // Assume SSE2 by default. 133 134 __kmp_x86_cpuid(0, 0, &buf); 135 136 KA_TRACE(trace_level, 137 ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n", 0, 138 buf.eax, buf.ebx, buf.ecx, buf.edx)); 139 140 max_arg = buf.eax; 141 142 p->apic_id = -1; 143 144 if (max_arg >= 1) { 145 int i; 146 kmp_uint32 t, data[4]; 147 148 __kmp_x86_cpuid(1, 0, &buf); 149 KA_TRACE(trace_level, 150 ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n", 151 1, buf.eax, buf.ebx, buf.ecx, buf.edx)); 152 153 { 154 #define get_value(reg, lo, mask) (((reg) >> (lo)) & (mask)) 155 156 p->signature = buf.eax; 157 p->family = get_value(buf.eax, 20, 0xff) + get_value(buf.eax, 8, 0x0f); 158 p->model = 159 (get_value(buf.eax, 16, 0x0f) << 4) + get_value(buf.eax, 4, 0x0f); 160 p->stepping = get_value(buf.eax, 0, 0x0f); 161 162 #undef get_value 163 164 KA_TRACE(trace_level, (" family = %d, model = %d, stepping = %d\n", 165 p->family, p->model, p->stepping)); 166 } 167 168 for (t = buf.ebx, i = 0; i < 4; t >>= 8, ++i) { 169 data[i] = (t & 0xff); 170 } 171 172 p->sse2 = (buf.edx >> 26) & 1; 173 174 #ifdef KMP_DEBUG 175 176 if ((buf.edx >> 4) & 1) { 177 /* TSC - Timestamp Counter Available */ 178 KA_TRACE(trace_level, (" TSC")); 179 } 180 if ((buf.edx >> 8) & 1) { 181 /* CX8 - CMPXCHG8B Instruction Available */ 182 KA_TRACE(trace_level, (" CX8")); 183 } 184 if ((buf.edx >> 9) & 1) { 185 /* APIC - Local APIC Present (multi-processor operation support */ 186 KA_TRACE(trace_level, (" APIC")); 187 } 188 if ((buf.edx >> 15) & 1) { 189 /* CMOV - Conditional MOVe Instruction Available */ 190 KA_TRACE(trace_level, (" CMOV")); 191 } 192 if ((buf.edx >> 18) & 1) { 193 /* PSN - Processor Serial Number Available */ 194 KA_TRACE(trace_level, (" PSN")); 195 } 196 if ((buf.edx >> 19) & 1) { 197 /* CLFLUSH - Cache Flush Instruction Available */ 198 cflush_size = 199 data[1] * 8; /* Bits 15-08: CLFLUSH line size = 8 (64 bytes) */ 200 KA_TRACE(trace_level, (" CLFLUSH(%db)", cflush_size)); 201 } 202 if ((buf.edx >> 21) & 1) { 203 /* DTES - Debug Trace & EMON Store */ 204 KA_TRACE(trace_level, (" DTES")); 205 } 206 if ((buf.edx >> 22) & 1) { 207 /* ACPI - ACPI Support Available */ 208 KA_TRACE(trace_level, (" ACPI")); 209 } 210 if ((buf.edx >> 23) & 1) { 211 /* MMX - Multimedia Extensions */ 212 KA_TRACE(trace_level, (" MMX")); 213 } 214 if ((buf.edx >> 25) & 1) { 215 /* SSE - SSE Instructions */ 216 KA_TRACE(trace_level, (" SSE")); 217 } 218 if ((buf.edx >> 26) & 1) { 219 /* SSE2 - SSE2 Instructions */ 220 KA_TRACE(trace_level, (" SSE2")); 221 } 222 if ((buf.edx >> 27) & 1) { 223 /* SLFSNP - Self-Snooping Cache */ 224 KA_TRACE(trace_level, (" SLFSNP")); 225 } 226 #endif /* KMP_DEBUG */ 227 228 if ((buf.edx >> 28) & 1) { 229 /* Bits 23-16: Logical Processors per Physical Processor (1 for P4) */ 230 log_per_phy = data[2]; 231 p->apic_id = data[3]; /* Bits 31-24: Processor Initial APIC ID (X) */ 232 KA_TRACE(trace_level, (" HT(%d TPUs)", log_per_phy)); 233 p->physical_id = __kmp_get_physical_id(log_per_phy, p->apic_id); 234 p->logical_id = __kmp_get_logical_id(log_per_phy, p->apic_id); 235 } 236 #ifdef KMP_DEBUG 237 if ((buf.edx >> 29) & 1) { 238 /* ATHROTL - Automatic Throttle Control */ 239 KA_TRACE(trace_level, (" ATHROTL")); 240 } 241 KA_TRACE(trace_level, (" ]\n")); 242 243 for (i = 2; i <= max_arg; ++i) { 244 __kmp_x86_cpuid(i, 0, &buf); 245 KA_TRACE(trace_level, 246 ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n", 247 i, buf.eax, buf.ebx, buf.ecx, buf.edx)); 248 } 249 #endif 250 #if KMP_USE_ADAPTIVE_LOCKS 251 p->rtm = 0; 252 if (max_arg > 7) { 253 /* RTM bit CPUID.07:EBX, bit 11 */ 254 __kmp_x86_cpuid(7, 0, &buf); 255 p->rtm = (buf.ebx >> 11) & 1; 256 KA_TRACE(trace_level, (" RTM")); 257 } 258 #endif 259 } 260 261 { // Parse CPU brand string for frequency, saving the string for later. 262 int i; 263 kmp_cpuid_t *base = (kmp_cpuid_t *)&p->name[0]; 264 265 // Get CPU brand string. 266 for (i = 0; i < 3; ++i) { 267 __kmp_x86_cpuid(0x80000002 + i, 0, base + i); 268 } 269 p->name[sizeof(p->name) - 1] = 0; // Just in case. ;-) 270 KA_TRACE(trace_level, ("cpu brand string: \"%s\"\n", &p->name[0])); 271 272 // Parse frequency. 273 p->frequency = __kmp_parse_frequency(strrchr(&p->name[0], ' ')); 274 KA_TRACE(trace_level, 275 ("cpu frequency from brand string: %" KMP_UINT64_SPEC "\n", 276 p->frequency)); 277 } 278 } 279 280 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ 281 282 void __kmp_expand_host_name(char *buffer, size_t size) { 283 KMP_DEBUG_ASSERT(size >= sizeof(unknown)); 284 #if KMP_OS_WINDOWS 285 { 286 DWORD s = size; 287 288 if (!GetComputerNameA(buffer, &s)) 289 KMP_STRCPY_S(buffer, size, unknown); 290 } 291 #else 292 buffer[size - 2] = 0; 293 if (gethostname(buffer, size) || buffer[size - 2] != 0) 294 KMP_STRCPY_S(buffer, size, unknown); 295 #endif 296 } 297 298 /* Expand the meta characters in the filename: 299 * Currently defined characters are: 300 * %H the hostname 301 * %P the number of threads used. 302 * %I the unique identifier for this run. 303 */ 304 305 void __kmp_expand_file_name(char *result, size_t rlen, char *pattern) { 306 char *pos = result, *end = result + rlen - 1; 307 char buffer[256]; 308 int default_cpu_width = 1; 309 int snp_result; 310 311 KMP_DEBUG_ASSERT(rlen > 0); 312 *end = 0; 313 { 314 int i; 315 for (i = __kmp_xproc; i >= 10; i /= 10, ++default_cpu_width) 316 ; 317 } 318 319 if (pattern != NULL) { 320 while (*pattern != '\0' && pos < end) { 321 if (*pattern != '%') { 322 *pos++ = *pattern++; 323 } else { 324 char *old_pattern = pattern; 325 int width = 1; 326 int cpu_width = default_cpu_width; 327 328 ++pattern; 329 330 if (*pattern >= '0' && *pattern <= '9') { 331 width = 0; 332 do { 333 width = (width * 10) + *pattern++ - '0'; 334 } while (*pattern >= '0' && *pattern <= '9'); 335 if (width < 0 || width > 1024) 336 width = 1; 337 338 cpu_width = width; 339 } 340 341 switch (*pattern) { 342 case 'H': 343 case 'h': { 344 __kmp_expand_host_name(buffer, sizeof(buffer)); 345 KMP_STRNCPY(pos, buffer, end - pos + 1); 346 if (*end == 0) { 347 while (*pos) 348 ++pos; 349 ++pattern; 350 } else 351 pos = end; 352 } break; 353 case 'P': 354 case 'p': { 355 snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", cpu_width, 356 __kmp_dflt_team_nth); 357 if (snp_result >= 0 && snp_result <= end - pos) { 358 while (*pos) 359 ++pos; 360 ++pattern; 361 } else 362 pos = end; 363 } break; 364 case 'I': 365 case 'i': { 366 pid_t id = getpid(); 367 #if KMP_ARCH_X86_64 && defined(__MINGW32__) 368 snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*lld", width, id); 369 #else 370 snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", width, id); 371 #endif 372 if (snp_result >= 0 && snp_result <= end - pos) { 373 while (*pos) 374 ++pos; 375 ++pattern; 376 } else 377 pos = end; 378 break; 379 } 380 case '%': { 381 *pos++ = '%'; 382 ++pattern; 383 break; 384 } 385 default: { 386 *pos++ = '%'; 387 pattern = old_pattern + 1; 388 break; 389 } 390 } 391 } 392 } 393 /* TODO: How do we get rid of this? */ 394 if (*pattern != '\0') 395 KMP_FATAL(FileNameTooLong); 396 } 397 398 *pos = '\0'; 399 } 400