xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_utility.cpp (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
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->flags.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->flags.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     p->flags.rtm = 0;
251     p->flags.hybrid = 0;
252     if (max_arg > 7) {
253       /* RTM bit CPUID.07:EBX, bit 11 */
254       /* HYRBID bit CPUID.07:EDX, bit 15 */
255       __kmp_x86_cpuid(7, 0, &buf);
256       p->flags.rtm = (buf.ebx >> 11) & 1;
257       p->flags.hybrid = (buf.edx >> 15) & 1;
258       if (p->flags.rtm) {
259         KA_TRACE(trace_level, (" RTM"));
260       }
261       if (p->flags.hybrid) {
262         KA_TRACE(trace_level, (" HYBRID"));
263       }
264     }
265   }
266 
267   { // Parse CPU brand string for frequency, saving the string for later.
268     int i;
269     kmp_cpuid_t *base = (kmp_cpuid_t *)&p->name[0];
270 
271     // Get CPU brand string.
272     for (i = 0; i < 3; ++i) {
273       __kmp_x86_cpuid(0x80000002 + i, 0, base + i);
274     }
275     p->name[sizeof(p->name) - 1] = 0; // Just in case. ;-)
276     KA_TRACE(trace_level, ("cpu brand string: \"%s\"\n", &p->name[0]));
277 
278     // Parse frequency.
279     p->frequency = __kmp_parse_frequency(strrchr(&p->name[0], ' '));
280     KA_TRACE(trace_level,
281              ("cpu frequency from brand string: %" KMP_UINT64_SPEC "\n",
282               p->frequency));
283   }
284 }
285 
286 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
287 
288 void __kmp_expand_host_name(char *buffer, size_t size) {
289   KMP_DEBUG_ASSERT(size >= sizeof(unknown));
290 #if KMP_OS_WINDOWS
291   {
292     DWORD s = size;
293 
294     if (!GetComputerNameA(buffer, &s))
295       KMP_STRCPY_S(buffer, size, unknown);
296   }
297 #else
298   buffer[size - 2] = 0;
299   if (gethostname(buffer, size) || buffer[size - 2] != 0)
300     KMP_STRCPY_S(buffer, size, unknown);
301 #endif
302 }
303 
304 /* Expand the meta characters in the filename:
305  * Currently defined characters are:
306  * %H the hostname
307  * %P the number of threads used.
308  * %I the unique identifier for this run.
309  */
310 
311 void __kmp_expand_file_name(char *result, size_t rlen, char *pattern) {
312   char *pos = result, *end = result + rlen - 1;
313   char buffer[256];
314   int default_cpu_width = 1;
315   int snp_result;
316 
317   KMP_DEBUG_ASSERT(rlen > 0);
318   *end = 0;
319   {
320     int i;
321     for (i = __kmp_xproc; i >= 10; i /= 10, ++default_cpu_width)
322       ;
323   }
324 
325   if (pattern != NULL) {
326     while (*pattern != '\0' && pos < end) {
327       if (*pattern != '%') {
328         *pos++ = *pattern++;
329       } else {
330         char *old_pattern = pattern;
331         int width = 1;
332         int cpu_width = default_cpu_width;
333 
334         ++pattern;
335 
336         if (*pattern >= '0' && *pattern <= '9') {
337           width = 0;
338           do {
339             width = (width * 10) + *pattern++ - '0';
340           } while (*pattern >= '0' && *pattern <= '9');
341           if (width < 0 || width > 1024)
342             width = 1;
343 
344           cpu_width = width;
345         }
346 
347         switch (*pattern) {
348         case 'H':
349         case 'h': {
350           __kmp_expand_host_name(buffer, sizeof(buffer));
351           KMP_STRNCPY(pos, buffer, end - pos + 1);
352           if (*end == 0) {
353             while (*pos)
354               ++pos;
355             ++pattern;
356           } else
357             pos = end;
358         } break;
359         case 'P':
360         case 'p': {
361           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", cpu_width,
362                                     __kmp_dflt_team_nth);
363           if (snp_result >= 0 && snp_result <= end - pos) {
364             while (*pos)
365               ++pos;
366             ++pattern;
367           } else
368             pos = end;
369         } break;
370         case 'I':
371         case 'i': {
372           pid_t id = getpid();
373 #if (KMP_ARCH_X86_64 || KMP_ARCH_AARCH64) && defined(__MINGW32__)
374           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*lld", width, id);
375 #else
376           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", width, id);
377 #endif
378           if (snp_result >= 0 && snp_result <= end - pos) {
379             while (*pos)
380               ++pos;
381             ++pattern;
382           } else
383             pos = end;
384           break;
385         }
386         case '%': {
387           *pos++ = '%';
388           ++pattern;
389           break;
390         }
391         default: {
392           *pos++ = '%';
393           pattern = old_pattern + 1;
394           break;
395         }
396         }
397       }
398     }
399     /* TODO: How do we get rid of this? */
400     if (*pattern != '\0')
401       KMP_FATAL(FileNameTooLong);
402   }
403 
404   *pos = '\0';
405 }
406 
407 #if !OMPT_SUPPORT
408 extern "C" {
409 typedef struct ompt_start_tool_result_t ompt_start_tool_result_t;
410 // Define symbols expected by VERSION script
411 ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version,
412                                           const char *runtime_version) {
413   return nullptr;
414 }
415 
416 void ompt_libomp_connect(ompt_start_tool_result_t *result) { result = nullptr; }
417 }
418 #endif
419