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