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