kmp_utility.cpp 11 KB

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