aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/kmp_utility.cpp
blob: 063b2a141ef6aa87c3e471783ba7044e77ddcd63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * kmp_utility.cpp -- Utility routines for the OpenMP support library.
 */


//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//


#include "kmp.h"
#include "kmp_i18n.h"
#include "kmp_str.h"
#include "kmp_wrapper_getpid.h"
#include <float.h>

static const char *unknown = "unknown";

#if KMP_ARCH_X86 || KMP_ARCH_X86_64

/* NOTE: If called before serial_initialize (i.e. from runtime_initialize), then
   the debugging package has not been initialized yet, and only "0" will print
   debugging output since the environment variables have not been read. */

#ifdef KMP_DEBUG
static int trace_level = 5;
#endif

/* LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * PHY_ID       = APIC_ID >> LOG_ID_BITS
 */
int __kmp_get_physical_id(int log_per_phy, int apic_id) {
  int index_lsb, index_msb, temp;

  if (log_per_phy > 1) {
    index_lsb = 0;
    index_msb = 31;

    temp = log_per_phy;
    while ((temp & 1) == 0) {
      temp >>= 1;
      index_lsb++;
    }

    temp = log_per_phy;
    while ((temp & 0x80000000) == 0) {
      temp <<= 1;
      index_msb--;
    }

    /* If >1 bits were set in log_per_phy, choose next higher power of 2 */
    if (index_lsb != index_msb)
      index_msb++;

    return ((int)(apic_id >> index_msb));
  }

  return apic_id;
}

/*
 * LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * LOG_ID       = APIC_ID & (( 1 << LOG_ID_BITS ) - 1 )
 */
int __kmp_get_logical_id(int log_per_phy, int apic_id) {
  unsigned current_bit;
  int bits_seen;

  if (log_per_phy <= 1)
    return (0);

  bits_seen = 0;

  for (current_bit = 1; log_per_phy != 0; current_bit <<= 1) {
    if (log_per_phy & current_bit) {
      log_per_phy &= ~current_bit;
      bits_seen++;
    }
  }

  /* If exactly 1 bit was set in log_per_phy, choose next lower power of 2 */
  if (bits_seen == 1) {
    current_bit >>= 1;
  }

  return ((int)((current_bit - 1) & apic_id));
}

static kmp_uint64 __kmp_parse_frequency( // R: Frequency in Hz.
    char const *frequency // I: Float number and unit: MHz, GHz, or TGz.
    ) {

  double value = 0.0;
  char const *unit = NULL;
  kmp_uint64 result = 0; /* Zero is a better unknown value than all ones. */

  if (frequency == NULL) {
    return result;
  }
  value = strtod(frequency,
                 CCAST(char **, &unit)); // strtod() does not like "const"
  if (0 < value &&
      value <= DBL_MAX) { // Good value (not overflow, underflow, etc).
    if (strcmp(unit, "MHz") == 0) {
      value = value * 1.0E+6;
    } else if (strcmp(unit, "GHz") == 0) {
      value = value * 1.0E+9;
    } else if (strcmp(unit, "THz") == 0) {
      value = value * 1.0E+12;
    } else { // Wrong unit.
      return result;
    }
    result = value;
  }
  return result;

} // func __kmp_parse_cpu_frequency

void __kmp_query_cpuid(kmp_cpuinfo_t *p) {
  struct kmp_cpuid buf;
  int max_arg;
  int log_per_phy;
#ifdef KMP_DEBUG
  int cflush_size;
#endif

  p->initialized = 1;

  p->sse2 = 1; // Assume SSE2 by default.

  __kmp_x86_cpuid(0, 0, &buf);

  KA_TRACE(trace_level,
           ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n", 0,
            buf.eax, buf.ebx, buf.ecx, buf.edx));

  max_arg = buf.eax;

  p->apic_id = -1;

  if (max_arg >= 1) {
    int i;
    kmp_uint32 t, data[4];

    __kmp_x86_cpuid(1, 0, &buf);
    KA_TRACE(trace_level,
             ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n",
              1, buf.eax, buf.ebx, buf.ecx, buf.edx));

    {
#define get_value(reg, lo, mask) (((reg) >> (lo)) & (mask))

      p->signature = buf.eax;
      p->family = get_value(buf.eax, 20, 0xff) + get_value(buf.eax, 8, 0x0f);
      p->model =
          (get_value(buf.eax, 16, 0x0f) << 4) + get_value(buf.eax, 4, 0x0f);
      p->stepping = get_value(buf.eax, 0, 0x0f);

#undef get_value

      KA_TRACE(trace_level, (" family = %d, model = %d, stepping = %d\n",
                             p->family, p->model, p->stepping));
    }

    for (t = buf.ebx, i = 0; i < 4; t >>= 8, ++i) {
      data[i] = (t & 0xff);
    }

    p->sse2 = (buf.edx >> 26) & 1;

#ifdef KMP_DEBUG

    if ((buf.edx >> 4) & 1) {
      /* TSC - Timestamp Counter Available */
      KA_TRACE(trace_level, (" TSC"));
    }
    if ((buf.edx >> 8) & 1) {
      /* CX8 - CMPXCHG8B Instruction Available */
      KA_TRACE(trace_level, (" CX8"));
    }
    if ((buf.edx >> 9) & 1) {
      /* APIC - Local APIC Present (multi-processor operation support */
      KA_TRACE(trace_level, (" APIC"));
    }
    if ((buf.edx >> 15) & 1) {
      /* CMOV - Conditional MOVe Instruction Available */
      KA_TRACE(trace_level, (" CMOV"));
    }
    if ((buf.edx >> 18) & 1) {
      /* PSN - Processor Serial Number Available */
      KA_TRACE(trace_level, (" PSN"));
    }
    if ((buf.edx >> 19) & 1) {
      /* CLFULSH - Cache Flush Instruction Available */
      cflush_size =
          data[1] * 8; /* Bits 15-08: CLFLUSH line size = 8 (64 bytes) */
      KA_TRACE(trace_level, (" CLFLUSH(%db)", cflush_size));
    }
    if ((buf.edx >> 21) & 1) {
      /* DTES - Debug Trace & EMON Store */
      KA_TRACE(trace_level, (" DTES"));
    }
    if ((buf.edx >> 22) & 1) {
      /* ACPI - ACPI Support Available */
      KA_TRACE(trace_level, (" ACPI"));
    }
    if ((buf.edx >> 23) & 1) {
      /* MMX - Multimedia Extensions */
      KA_TRACE(trace_level, (" MMX"));
    }
    if ((buf.edx >> 25) & 1) {
      /* SSE - SSE Instructions */
      KA_TRACE(trace_level, (" SSE"));
    }
    if ((buf.edx >> 26) & 1) {
      /* SSE2 - SSE2 Instructions */
      KA_TRACE(trace_level, (" SSE2"));
    }
    if ((buf.edx >> 27) & 1) {
      /* SLFSNP - Self-Snooping Cache */
      KA_TRACE(trace_level, (" SLFSNP"));
    }
#endif /* KMP_DEBUG */

    if ((buf.edx >> 28) & 1) {
      /* Bits 23-16: Logical Processors per Physical Processor (1 for P4) */
      log_per_phy = data[2];
      p->apic_id = data[3]; /* Bits 31-24: Processor Initial APIC ID (X) */
      KA_TRACE(trace_level, (" HT(%d TPUs)", log_per_phy));

      if (log_per_phy > 1) {
/* default to 1k FOR JT-enabled processors (4k on OS X*) */
#if KMP_OS_DARWIN
        p->cpu_stackoffset = 4 * 1024;
#else
        p->cpu_stackoffset = 1 * 1024;
#endif
      }

      p->physical_id = __kmp_get_physical_id(log_per_phy, p->apic_id);
      p->logical_id = __kmp_get_logical_id(log_per_phy, p->apic_id);
    }
#ifdef KMP_DEBUG
    if ((buf.edx >> 29) & 1) {
      /* ATHROTL - Automatic Throttle Control */
      KA_TRACE(trace_level, (" ATHROTL"));
    }
    KA_TRACE(trace_level, (" ]\n"));

    for (i = 2; i <= max_arg; ++i) {
      __kmp_x86_cpuid(i, 0, &buf);
      KA_TRACE(trace_level,
               ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n",
                i, buf.eax, buf.ebx, buf.ecx, buf.edx));
    }
#endif
#if KMP_USE_ADAPTIVE_LOCKS
    p->rtm = 0;
    if (max_arg > 7) {
      /* RTM bit CPUID.07:EBX, bit 11 */
      __kmp_x86_cpuid(7, 0, &buf);
      p->rtm = (buf.ebx >> 11) & 1;
      KA_TRACE(trace_level, (" RTM"));
    }
#endif
  }

  { // Parse CPU brand string for frequency, saving the string for later.
    int i;
    kmp_cpuid_t *base = (kmp_cpuid_t *)&p->name[0];

    // Get CPU brand string.
    for (i = 0; i < 3; ++i) {
      __kmp_x86_cpuid(0x80000002 + i, 0, base + i);
    }
    p->name[sizeof(p->name) - 1] = 0; // Just in case. ;-)
    KA_TRACE(trace_level, ("cpu brand string: \"%s\"\n", &p->name[0]));

    // Parse frequency.
    p->frequency = __kmp_parse_frequency(strrchr(&p->name[0], ' '));
    KA_TRACE(trace_level,
             ("cpu frequency from brand string: %" KMP_UINT64_SPEC "\n",
              p->frequency));
  }
}

#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */

void __kmp_expand_host_name(char *buffer, size_t size) {
  KMP_DEBUG_ASSERT(size >= sizeof(unknown));
#if KMP_OS_WINDOWS
  {
    DWORD s = size;

    if (!GetComputerNameA(buffer, &s))
      KMP_STRCPY_S(buffer, size, unknown);
  }
#else
  buffer[size - 2] = 0;
  if (gethostname(buffer, size) || buffer[size - 2] != 0)
    KMP_STRCPY_S(buffer, size, unknown);
#endif
}

/* Expand the meta characters in the filename:
 * Currently defined characters are:
 * %H the hostname
 * %P the number of threads used.
 * %I the unique identifier for this run.
 */

void __kmp_expand_file_name(char *result, size_t rlen, char *pattern) {
  char *pos = result, *end = result + rlen - 1;
  char buffer[256];
  int default_cpu_width = 1;
  int snp_result;

  KMP_DEBUG_ASSERT(rlen > 0);
  *end = 0;
  {
    int i;
    for (i = __kmp_xproc; i >= 10; i /= 10, ++default_cpu_width)
      ;
  }

  if (pattern != NULL) {
    while (*pattern != '\0' && pos < end) {
      if (*pattern != '%') {
        *pos++ = *pattern++;
      } else {
        char *old_pattern = pattern;
        int width = 1;
        int cpu_width = default_cpu_width;

        ++pattern;

        if (*pattern >= '0' && *pattern <= '9') {
          width = 0;
          do {
            width = (width * 10) + *pattern++ - '0';
          } while (*pattern >= '0' && *pattern <= '9');
          if (width < 0 || width > 1024)
            width = 1;

          cpu_width = width;
        }

        switch (*pattern) {
        case 'H':
        case 'h': {
          __kmp_expand_host_name(buffer, sizeof(buffer));
          KMP_STRNCPY(pos, buffer, end - pos + 1);
          if (*end == 0) {
            while (*pos)
              ++pos;
            ++pattern;
          } else
            pos = end;
        } break;
        case 'P':
        case 'p': {
          snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", cpu_width,
                                    __kmp_dflt_team_nth);
          if (snp_result >= 0 && snp_result <= end - pos) {
            while (*pos)
              ++pos;
            ++pattern;
          } else
            pos = end;
        } break;
        case 'I':
        case 'i': {
          pid_t id = getpid();
          snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", width, id);
          if (snp_result >= 0 && snp_result <= end - pos) {
            while (*pos)
              ++pos;
            ++pattern;
          } else
            pos = end;
          break;
        }
        case '%': {
          *pos++ = '%';
          ++pattern;
          break;
        }
        default: {
          *pos++ = '%';
          pattern = old_pattern + 1;
          break;
        }
        }
      }
    }
    /* TODO: How do we get rid of this? */
    if (*pattern != '\0')
      KMP_FATAL(FileNameTooLong);
  }

  *pos = '\0';
}