xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_utility.cpp (revision 47ef2a131091508e049ab10cad7f91a3c1342cd9)
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 static kmp_uint64 __kmp_parse_frequency( // R: Frequency in Hz.
32     char const *frequency // I: Float number and unit: MHz, GHz, or TGz.
33 ) {
34 
35   double value = 0.0;
36   char *unit = NULL;
37   kmp_uint64 result = 0; /* Zero is a better unknown value than all ones. */
38 
39   if (frequency == NULL) {
40     return result;
41   }
42   value = strtod(frequency, &unit);
43   if (0 < value &&
44       value <= DBL_MAX) { // Good value (not overflow, underflow, etc).
45     if (strcmp(unit, "MHz") == 0) {
46       value = value * 1.0E+6;
47     } else if (strcmp(unit, "GHz") == 0) {
48       value = value * 1.0E+9;
49     } else if (strcmp(unit, "THz") == 0) {
50       value = value * 1.0E+12;
51     } else { // Wrong unit.
52       return result;
53     }
54     result = (kmp_uint64)value; // rounds down
55   }
56   return result;
57 
58 } // func __kmp_parse_cpu_frequency
59 
60 void __kmp_query_cpuid(kmp_cpuinfo_t *p) {
61   struct kmp_cpuid buf;
62   int max_arg;
63 #ifdef KMP_DEBUG
64   int cflush_size;
65 #endif
66 
67   p->initialized = 1;
68 
69   p->flags.sse2 = 1; // Assume SSE2 by default.
70 
71   __kmp_x86_cpuid(0, 0, &buf);
72 
73   KA_TRACE(trace_level,
74            ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n", 0,
75             buf.eax, buf.ebx, buf.ecx, buf.edx));
76 
77   max_arg = buf.eax;
78 
79   p->apic_id = -1;
80 
81   if (max_arg >= 1) {
82     int i;
83     kmp_uint32 t, data[4];
84 
85     __kmp_x86_cpuid(1, 0, &buf);
86     KA_TRACE(trace_level,
87              ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n",
88               1, buf.eax, buf.ebx, buf.ecx, buf.edx));
89 
90     {
91 #define get_value(reg, lo, mask) (((reg) >> (lo)) & (mask))
92 
93       p->signature = buf.eax;
94       p->family = get_value(buf.eax, 20, 0xff) + get_value(buf.eax, 8, 0x0f);
95       p->model =
96           (get_value(buf.eax, 16, 0x0f) << 4) + get_value(buf.eax, 4, 0x0f);
97       p->stepping = get_value(buf.eax, 0, 0x0f);
98 
99 #undef get_value
100 
101       KA_TRACE(trace_level, (" family = %d, model = %d, stepping = %d\n",
102                              p->family, p->model, p->stepping));
103     }
104 
105     for (t = buf.ebx, i = 0; i < 4; t >>= 8, ++i) {
106       data[i] = (t & 0xff);
107     }
108 
109     p->flags.sse2 = (buf.edx >> 26) & 1;
110 
111 #ifdef KMP_DEBUG
112 
113     if ((buf.edx >> 4) & 1) {
114       /* TSC - Timestamp Counter Available */
115       KA_TRACE(trace_level, (" TSC"));
116     }
117     if ((buf.edx >> 8) & 1) {
118       /* CX8 - CMPXCHG8B Instruction Available */
119       KA_TRACE(trace_level, (" CX8"));
120     }
121     if ((buf.edx >> 9) & 1) {
122       /* APIC - Local APIC Present (multi-processor operation support */
123       KA_TRACE(trace_level, (" APIC"));
124     }
125     if ((buf.edx >> 15) & 1) {
126       /* CMOV - Conditional MOVe Instruction Available */
127       KA_TRACE(trace_level, (" CMOV"));
128     }
129     if ((buf.edx >> 18) & 1) {
130       /* PSN - Processor Serial Number Available */
131       KA_TRACE(trace_level, (" PSN"));
132     }
133     if ((buf.edx >> 19) & 1) {
134       /* CLFLUSH - Cache Flush Instruction Available */
135       cflush_size =
136           data[1] * 8; /* Bits 15-08: CLFLUSH line size = 8 (64 bytes) */
137       KA_TRACE(trace_level, (" CLFLUSH(%db)", cflush_size));
138     }
139     if ((buf.edx >> 21) & 1) {
140       /* DTES - Debug Trace & EMON Store */
141       KA_TRACE(trace_level, (" DTES"));
142     }
143     if ((buf.edx >> 22) & 1) {
144       /* ACPI - ACPI Support Available */
145       KA_TRACE(trace_level, (" ACPI"));
146     }
147     if ((buf.edx >> 23) & 1) {
148       /* MMX - Multimedia Extensions */
149       KA_TRACE(trace_level, (" MMX"));
150     }
151     if ((buf.edx >> 25) & 1) {
152       /* SSE - SSE Instructions */
153       KA_TRACE(trace_level, (" SSE"));
154     }
155     if ((buf.edx >> 26) & 1) {
156       /* SSE2 - SSE2 Instructions */
157       KA_TRACE(trace_level, (" SSE2"));
158     }
159     if ((buf.edx >> 27) & 1) {
160       /* SLFSNP - Self-Snooping Cache */
161       KA_TRACE(trace_level, (" SLFSNP"));
162     }
163 #endif /* KMP_DEBUG */
164 
165     if ((buf.edx >> 28) & 1) {
166       /* Bits 23-16: Logical Processors per Physical Processor (1 for P4) */
167       p->apic_id = data[3]; /* Bits 31-24: Processor Initial APIC ID (X) */
168       KA_TRACE(trace_level, (" HT(%d TPUs)", data[2]));
169     }
170 #ifdef KMP_DEBUG
171     if ((buf.edx >> 29) & 1) {
172       /* ATHROTL - Automatic Throttle Control */
173       KA_TRACE(trace_level, (" ATHROTL"));
174     }
175     KA_TRACE(trace_level, (" ]\n"));
176 
177     for (i = 2; i <= max_arg; ++i) {
178       __kmp_x86_cpuid(i, 0, &buf);
179       KA_TRACE(trace_level,
180                ("INFO: CPUID %d: EAX=0x%08X EBX=0x%08X ECX=0x%08X EDX=0x%08X\n",
181                 i, buf.eax, buf.ebx, buf.ecx, buf.edx));
182     }
183 #endif
184     p->flags.rtm = 0;
185     p->flags.hybrid = 0;
186     if (max_arg > 7) {
187       /* RTM bit CPUID.07:EBX, bit 11 */
188       /* HYRBID bit CPUID.07:EDX, bit 15 */
189       __kmp_x86_cpuid(7, 0, &buf);
190       p->flags.rtm = (buf.ebx >> 11) & 1;
191       p->flags.hybrid = (buf.edx >> 15) & 1;
192       if (p->flags.rtm) {
193         KA_TRACE(trace_level, (" RTM"));
194       }
195       if (p->flags.hybrid) {
196         KA_TRACE(trace_level, (" HYBRID"));
197       }
198     }
199   }
200 
201   { // Parse CPU brand string for frequency, saving the string for later.
202     int i;
203     kmp_cpuid_t *base = (kmp_cpuid_t *)&p->name[0];
204 
205     // Get CPU brand string.
206     for (i = 0; i < 3; ++i) {
207       __kmp_x86_cpuid(0x80000002 + i, 0, base + i);
208     }
209     p->name[sizeof(p->name) - 1] = 0; // Just in case. ;-)
210     KA_TRACE(trace_level, ("cpu brand string: \"%s\"\n", &p->name[0]));
211 
212     // Parse frequency.
213     p->frequency = __kmp_parse_frequency(strrchr(&p->name[0], ' '));
214     KA_TRACE(trace_level,
215              ("cpu frequency from brand string: %" KMP_UINT64_SPEC "\n",
216               p->frequency));
217   }
218 }
219 
220 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
221 
222 void __kmp_expand_host_name(char *buffer, size_t size) {
223   KMP_DEBUG_ASSERT(size >= sizeof(unknown));
224 #if KMP_OS_WINDOWS
225   {
226     DWORD s = size;
227 
228     if (!GetComputerNameA(buffer, &s))
229       KMP_STRCPY_S(buffer, size, unknown);
230   }
231 #elif KMP_OS_WASI
232   KMP_STRCPY_S(buffer, size, unknown);
233 #else
234   buffer[size - 2] = 0;
235   if (gethostname(buffer, size) || buffer[size - 2] != 0)
236     KMP_STRCPY_S(buffer, size, unknown);
237 #endif
238 }
239 
240 /* Expand the meta characters in the filename:
241  * Currently defined characters are:
242  * %H the hostname
243  * %P the number of threads used.
244  * %I the unique identifier for this run.
245  */
246 
247 void __kmp_expand_file_name(char *result, size_t rlen, char *pattern) {
248   char *pos = result, *end = result + rlen - 1;
249   char buffer[256];
250   int default_cpu_width = 1;
251   int snp_result;
252 
253   KMP_DEBUG_ASSERT(rlen > 0);
254   *end = 0;
255   {
256     int i;
257     for (i = __kmp_xproc; i >= 10; i /= 10, ++default_cpu_width)
258       ;
259   }
260 
261   if (pattern != NULL) {
262     while (*pattern != '\0' && pos < end) {
263       if (*pattern != '%') {
264         *pos++ = *pattern++;
265       } else {
266         char *old_pattern = pattern;
267         int width = 1;
268         int cpu_width = default_cpu_width;
269 
270         ++pattern;
271 
272         if (*pattern >= '0' && *pattern <= '9') {
273           width = 0;
274           do {
275             width = (width * 10) + *pattern++ - '0';
276           } while (*pattern >= '0' && *pattern <= '9');
277           if (width < 0 || width > 1024)
278             width = 1;
279 
280           cpu_width = width;
281         }
282 
283         switch (*pattern) {
284         case 'H':
285         case 'h': {
286           __kmp_expand_host_name(buffer, sizeof(buffer));
287           KMP_STRNCPY(pos, buffer, end - pos + 1);
288           if (*end == 0) {
289             while (*pos)
290               ++pos;
291             ++pattern;
292           } else
293             pos = end;
294         } break;
295         case 'P':
296         case 'p': {
297           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", cpu_width,
298                                     __kmp_dflt_team_nth);
299           if (snp_result >= 0 && snp_result <= end - pos) {
300             while (*pos)
301               ++pos;
302             ++pattern;
303           } else
304             pos = end;
305         } break;
306         case 'I':
307         case 'i': {
308           pid_t id = getpid();
309 #if (KMP_ARCH_X86_64 || KMP_ARCH_AARCH64) && defined(__MINGW32__)
310           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*lld", width, id);
311 #else
312           snp_result = KMP_SNPRINTF(pos, end - pos + 1, "%0*d", width, id);
313 #endif
314           if (snp_result >= 0 && snp_result <= end - pos) {
315             while (*pos)
316               ++pos;
317             ++pattern;
318           } else
319             pos = end;
320           break;
321         }
322         case '%': {
323           *pos++ = '%';
324           ++pattern;
325           break;
326         }
327         default: {
328           *pos++ = '%';
329           pattern = old_pattern + 1;
330           break;
331         }
332         }
333       }
334     }
335     /* TODO: How do we get rid of this? */
336     if (*pattern != '\0')
337       KMP_FATAL(FileNameTooLong);
338   }
339 
340   *pos = '\0';
341 }
342 
343 #if !OMPT_SUPPORT
344 extern "C" {
345 typedef struct ompt_start_tool_result_t ompt_start_tool_result_t;
346 // Define symbols expected by VERSION script
347 ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version,
348                                           const char *runtime_version) {
349   return nullptr;
350 }
351 
352 void ompt_libomp_connect(ompt_start_tool_result_t *result) { result = nullptr; }
353 }
354 #endif
355