blob: aa0302c0fc37d38abb409e73cfaddb80cf0cddc4 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * z_Linux_util.cpp -- platform specific routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014#include "kmp.h"
Jonathan Peyton1cdd87a2016-11-14 21:08:35 +000015#include "kmp_affinity.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000016#include "kmp_i18n.h"
17#include "kmp_io.h"
18#include "kmp_itt.h"
19#include "kmp_lock.h"
20#include "kmp_stats.h"
21#include "kmp_str.h"
22#include "kmp_wait_release.h"
23#include "kmp_wrapper_getpid.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000024
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +000025#if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000026#include <alloca.h>
Alp Toker763b9392014-02-28 09:42:41 +000027#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000028#include <math.h> // HUGE_VAL.
Jim Cownie5e8470a2013-09-27 10:38:44 +000029#include <sys/resource.h>
30#include <sys/syscall.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000031#include <sys/time.h>
32#include <sys/times.h>
33#include <unistd.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000034
Jim Cownie3051f972014-08-07 10:12:54 +000035#if KMP_OS_LINUX && !KMP_OS_CNK
Jonathan Peyton30419822017-05-12 18:01:32 +000036#include <sys/sysinfo.h>
37#if KMP_USE_FUTEX
38// We should really include <futex.h>, but that causes compatibility problems on
39// different Linux* OS distributions that either require that you include (or
40// break when you try to include) <pci/types.h>. Since all we need is the two
41// macros below (which are part of the kernel ABI, so can't change) we just
42// define the constants here and don't include <futex.h>
43#ifndef FUTEX_WAIT
44#define FUTEX_WAIT 0
45#endif
46#ifndef FUTEX_WAKE
47#define FUTEX_WAKE 1
48#endif
49#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000050#elif KMP_OS_DARWIN
Jonathan Peyton30419822017-05-12 18:01:32 +000051#include <mach/mach.h>
52#include <sys/sysctl.h>
Kamil Rytarowskia56ac942018-12-09 16:40:33 +000053#elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000054#include <pthread_np.h>
Kamil Rytarowski316f4232018-12-11 18:35:07 +000055#elif KMP_OS_NETBSD
56#include <sys/types.h>
57#include <sys/sysctl.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000058#endif
59
Jim Cownie5e8470a2013-09-27 10:38:44 +000060#include <ctype.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000061#include <dirent.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000062#include <fcntl.h>
63
Jonas Hahnfeld50fed042016-11-07 15:58:36 +000064#include "tsan_annotations.h"
65
Jim Cownie5e8470a2013-09-27 10:38:44 +000066struct kmp_sys_timer {
Jonathan Peyton30419822017-05-12 18:01:32 +000067 struct timespec start;
Jim Cownie5e8470a2013-09-27 10:38:44 +000068};
69
70// Convert timespec to nanoseconds.
71#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
72
73static struct kmp_sys_timer __kmp_sys_timer_data;
74
75#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +000076typedef void (*sig_func_t)(int);
77STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG];
78static sigset_t __kmp_sigset;
Jim Cownie5e8470a2013-09-27 10:38:44 +000079#endif
80
Jonathan Peyton30419822017-05-12 18:01:32 +000081static int __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +000082
83static int __kmp_fork_count = 0;
84
Jonathan Peyton30419822017-05-12 18:01:32 +000085static pthread_condattr_t __kmp_suspend_cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +000086static pthread_mutexattr_t __kmp_suspend_mutex_attr;
87
Jonathan Peyton30419822017-05-12 18:01:32 +000088static kmp_cond_align_t __kmp_wait_cv;
89static kmp_mutex_align_t __kmp_wait_mx;
Jim Cownie5e8470a2013-09-27 10:38:44 +000090
Jonathan Peyton35d75ae2017-03-20 22:11:31 +000091kmp_uint64 __kmp_ticks_per_msec = 1000000;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +000092
Jim Cownie5e8470a2013-09-27 10:38:44 +000093#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +000094static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
95 KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
96 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
97 cond->c_cond.__c_waiting);
Jim Cownie5e8470a2013-09-27 10:38:44 +000098}
99#endif
100
Jonathan Peyton30419822017-05-12 18:01:32 +0000101#if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000102
Jonathan Peyton30419822017-05-12 18:01:32 +0000103/* Affinity support */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000104
Jonathan Peyton30419822017-05-12 18:01:32 +0000105void __kmp_affinity_bind_thread(int which) {
106 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
107 "Illegal set affinity operation when not capable");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000108
Jonathan Peyton30419822017-05-12 18:01:32 +0000109 kmp_affin_mask_t *mask;
110 KMP_CPU_ALLOC_ON_STACK(mask);
111 KMP_CPU_ZERO(mask);
112 KMP_CPU_SET(which, mask);
113 __kmp_set_system_affinity(mask, TRUE);
114 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000115}
116
Jonathan Peyton30419822017-05-12 18:01:32 +0000117/* Determine if we can access affinity functionality on this version of
Jim Cownie5e8470a2013-09-27 10:38:44 +0000118 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
Jonathan Peyton30419822017-05-12 18:01:32 +0000119 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
120void __kmp_affinity_determine_capable(const char *env_var) {
121// Check and see if the OS supports thread affinity.
122
123#define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
124
125 int gCode;
126 int sCode;
127 unsigned char *buf;
128 buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
129
130 // If Linux* OS:
131 // If the syscall fails or returns a suggestion for the size,
132 // then we don't have to search for an appropriate size.
133 gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf);
134 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
135 "initial getaffinity call returned %d errno = %d\n",
136 gCode, errno));
137
138 // if ((gCode < 0) && (errno == ENOSYS))
139 if (gCode < 0) {
140 // System call not supported
141 if (__kmp_affinity_verbose ||
142 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
143 (__kmp_affinity_type != affinity_default) &&
144 (__kmp_affinity_type != affinity_disabled))) {
145 int error = errno;
146 kmp_msg_t err_code = KMP_ERR(error);
147 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
148 err_code, __kmp_msg_null);
149 if (__kmp_generate_warnings == kmp_warnings_off) {
150 __kmp_str_free(&err_code.str);
151 }
152 }
153 KMP_AFFINITY_DISABLE();
154 KMP_INTERNAL_FREE(buf);
155 return;
156 }
157 if (gCode > 0) { // Linux* OS only
158 // The optimal situation: the OS returns the size of the buffer it expects.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000159 //
Jonathan Peyton30419822017-05-12 18:01:32 +0000160 // A verification of correct behavior is that Isetaffinity on a NULL
161 // buffer with the same size fails with errno set to EFAULT.
162 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
163 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
164 "setaffinity for mask size %d returned %d errno = %d\n",
165 gCode, sCode, errno));
166 if (sCode < 0) {
167 if (errno == ENOSYS) {
168 if (__kmp_affinity_verbose ||
169 (__kmp_affinity_warnings &&
170 (__kmp_affinity_type != affinity_none) &&
171 (__kmp_affinity_type != affinity_default) &&
172 (__kmp_affinity_type != affinity_disabled))) {
173 int error = errno;
174 kmp_msg_t err_code = KMP_ERR(error);
175 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
176 err_code, __kmp_msg_null);
177 if (__kmp_generate_warnings == kmp_warnings_off) {
178 __kmp_str_free(&err_code.str);
179 }
180 }
181 KMP_AFFINITY_DISABLE();
182 KMP_INTERNAL_FREE(buf);
183 }
184 if (errno == EFAULT) {
185 KMP_AFFINITY_ENABLE(gCode);
186 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
187 "affinity supported (mask size %d)\n",
188 (int)__kmp_affin_mask_size));
189 KMP_INTERNAL_FREE(buf);
190 return;
191 }
192 }
193 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000194
Jonathan Peyton30419822017-05-12 18:01:32 +0000195 // Call the getaffinity system call repeatedly with increasing set sizes
196 // until we succeed, or reach an upper bound on the search.
197 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
198 "searching for proper set size\n"));
199 int size;
200 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
201 gCode = syscall(__NR_sched_getaffinity, 0, size, buf);
202 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
203 "getaffinity for mask size %d returned %d errno = %d\n",
204 size, gCode, errno));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000205
Jim Cownie5e8470a2013-09-27 10:38:44 +0000206 if (gCode < 0) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000207 if (errno == ENOSYS) {
208 // We shouldn't get here
209 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
210 "inconsistent OS call behavior: errno == ENOSYS for mask "
211 "size %d\n",
212 size));
213 if (__kmp_affinity_verbose ||
214 (__kmp_affinity_warnings &&
215 (__kmp_affinity_type != affinity_none) &&
216 (__kmp_affinity_type != affinity_default) &&
217 (__kmp_affinity_type != affinity_disabled))) {
218 int error = errno;
219 kmp_msg_t err_code = KMP_ERR(error);
220 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
221 err_code, __kmp_msg_null);
222 if (__kmp_generate_warnings == kmp_warnings_off) {
223 __kmp_str_free(&err_code.str);
224 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000225 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000226 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000227 KMP_INTERNAL_FREE(buf);
228 return;
Jonathan Peyton30419822017-05-12 18:01:32 +0000229 }
230 continue;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000231 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000232
233 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
234 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
235 "setaffinity for mask size %d returned %d errno = %d\n",
236 gCode, sCode, errno));
237 if (sCode < 0) {
238 if (errno == ENOSYS) { // Linux* OS only
239 // We shouldn't get here
240 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
241 "inconsistent OS call behavior: errno == ENOSYS for mask "
242 "size %d\n",
243 size));
244 if (__kmp_affinity_verbose ||
245 (__kmp_affinity_warnings &&
246 (__kmp_affinity_type != affinity_none) &&
247 (__kmp_affinity_type != affinity_default) &&
248 (__kmp_affinity_type != affinity_disabled))) {
249 int error = errno;
250 kmp_msg_t err_code = KMP_ERR(error);
251 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
252 err_code, __kmp_msg_null);
253 if (__kmp_generate_warnings == kmp_warnings_off) {
254 __kmp_str_free(&err_code.str);
255 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000256 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000257 KMP_AFFINITY_DISABLE();
258 KMP_INTERNAL_FREE(buf);
259 return;
260 }
261 if (errno == EFAULT) {
262 KMP_AFFINITY_ENABLE(gCode);
263 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
264 "affinity supported (mask size %d)\n",
265 (int)__kmp_affin_mask_size));
266 KMP_INTERNAL_FREE(buf);
267 return;
268 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000269 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000270 }
271 // save uncaught error code
272 // int error = errno;
273 KMP_INTERNAL_FREE(buf);
274 // restore uncaught error code, will be printed at the next KMP_WARNING below
275 // errno = error;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000276
Jonathan Peyton30419822017-05-12 18:01:32 +0000277 // Affinity is not supported
278 KMP_AFFINITY_DISABLE();
279 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
280 "cannot determine mask size - affinity not supported\n"));
281 if (__kmp_affinity_verbose ||
282 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
283 (__kmp_affinity_type != affinity_default) &&
284 (__kmp_affinity_type != affinity_disabled))) {
285 KMP_WARNING(AffCantGetMaskSize, env_var);
286 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000287}
288
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000289#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000290
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000291#if KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000292
Jonathan Peyton30419822017-05-12 18:01:32 +0000293int __kmp_futex_determine_capable() {
294 int loc = 0;
295 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
296 int retval = (rc == 0) || (errno != ENOSYS);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000297
Jonathan Peyton30419822017-05-12 18:01:32 +0000298 KA_TRACE(10,
299 ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno));
300 KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n",
301 retval ? "" : " not"));
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000302
Jonathan Peyton30419822017-05-12 18:01:32 +0000303 return retval;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000304}
305
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000306#endif // KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000307
Jonathan Peyton30419822017-05-12 18:01:32 +0000308#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS)
309/* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
310 use compare_and_store for these routines */
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000311
Jonathan Peyton30419822017-05-12 18:01:32 +0000312kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
313 kmp_int8 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000314
Jonathan Peyton30419822017-05-12 18:01:32 +0000315 old_value = TCR_1(*p);
316 new_value = old_value | d;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000317
Jonathan Peyton30419822017-05-12 18:01:32 +0000318 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
319 KMP_CPU_PAUSE();
320 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000321 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000322 }
323 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000324}
325
Jonathan Peyton30419822017-05-12 18:01:32 +0000326kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
327 kmp_int8 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000328
Jonathan Peyton30419822017-05-12 18:01:32 +0000329 old_value = TCR_1(*p);
330 new_value = old_value & d;
331
332 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
333 KMP_CPU_PAUSE();
334 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000335 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000336 }
337 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000338}
339
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000340kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) {
341 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000342
Jonathan Peyton30419822017-05-12 18:01:32 +0000343 old_value = TCR_4(*p);
344 new_value = old_value | d;
345
346 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
347 KMP_CPU_PAUSE();
348 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000349 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000350 }
351 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000352}
353
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000354kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) {
355 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000356
Jonathan Peyton30419822017-05-12 18:01:32 +0000357 old_value = TCR_4(*p);
358 new_value = old_value & d;
359
360 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
361 KMP_CPU_PAUSE();
362 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000363 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000364 }
365 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000366}
367
Jonathan Peyton30419822017-05-12 18:01:32 +0000368#if KMP_ARCH_X86
369kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
370 kmp_int8 old_value, new_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000371
Jonathan Peyton30419822017-05-12 18:01:32 +0000372 old_value = TCR_1(*p);
373 new_value = old_value + d;
374
375 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
376 KMP_CPU_PAUSE();
377 old_value = TCR_1(*p);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000378 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000379 }
380 return old_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000381}
382
Jonathan Peyton30419822017-05-12 18:01:32 +0000383kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
384 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000385
Jonathan Peyton30419822017-05-12 18:01:32 +0000386 old_value = TCR_8(*p);
387 new_value = old_value + d;
388
389 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
390 KMP_CPU_PAUSE();
391 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000392 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000393 }
394 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000395}
Jonathan Peyton30419822017-05-12 18:01:32 +0000396#endif /* KMP_ARCH_X86 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000397
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000398kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) {
399 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000400
Jonathan Peyton30419822017-05-12 18:01:32 +0000401 old_value = TCR_8(*p);
402 new_value = old_value | d;
403 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
404 KMP_CPU_PAUSE();
405 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000406 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000407 }
408 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000409}
410
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000411kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) {
412 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000413
Jonathan Peyton30419822017-05-12 18:01:32 +0000414 old_value = TCR_8(*p);
415 new_value = old_value & d;
416 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
417 KMP_CPU_PAUSE();
418 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000419 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000420 }
421 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000422}
423
424#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
425
Jonathan Peyton30419822017-05-12 18:01:32 +0000426void __kmp_terminate_thread(int gtid) {
427 int status;
428 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000429
Jonathan Peyton30419822017-05-12 18:01:32 +0000430 if (!th)
431 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000432
Jonathan Peyton30419822017-05-12 18:01:32 +0000433#ifdef KMP_CANCEL_THREADS
434 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
435 status = pthread_cancel(th->th.th_info.ds.ds_thread);
436 if (status != 0 && status != ESRCH) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000437 __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
438 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000439 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000440#endif
441 __kmp_yield(TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000442} //
443
Jonathan Peyton30419822017-05-12 18:01:32 +0000444/* Set thread stack info according to values returned by pthread_getattr_np().
445 If values are unreasonable, assume call failed and use incremental stack
446 refinement method instead. Returns TRUE if the stack parameters could be
447 determined exactly, FALSE if incremental refinement is necessary. */
448static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
449 int stack_data;
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000450#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
451 KMP_OS_HURD
Jonathan Peyton30419822017-05-12 18:01:32 +0000452 pthread_attr_t attr;
453 int status;
454 size_t size = 0;
455 void *addr = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000456
Jonathan Peyton30419822017-05-12 18:01:32 +0000457 /* Always do incremental stack refinement for ubermaster threads since the
458 initial thread stack range can be reduced by sibling thread creation so
459 pthread_attr_getstack may cause thread gtid aliasing */
460 if (!KMP_UBER_GTID(gtid)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000461
Jonathan Peyton30419822017-05-12 18:01:32 +0000462 /* Fetch the real thread attributes */
463 status = pthread_attr_init(&attr);
464 KMP_CHECK_SYSFAIL("pthread_attr_init", status);
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000465#if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000466 status = pthread_attr_get_np(pthread_self(), &attr);
467 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000468#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000469 status = pthread_getattr_np(pthread_self(), &attr);
470 KMP_CHECK_SYSFAIL("pthread_getattr_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000471#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000472 status = pthread_attr_getstack(&attr, &addr, &size);
473 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status);
474 KA_TRACE(60,
475 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
476 " %lu, low addr: %p\n",
477 gtid, size, addr));
478 status = pthread_attr_destroy(&attr);
479 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
480 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000481
Jonathan Peyton30419822017-05-12 18:01:32 +0000482 if (size != 0 && addr != 0) { // was stack parameter determination successful?
483 /* Store the correct base and size */
484 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
485 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
486 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
487 return TRUE;
488 }
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000489#endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD ||
490 KMP_OS_HURD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000491 /* Use incremental refinement starting from initial conservative estimate */
492 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
493 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
494 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
495 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000496}
497
Jonathan Peyton30419822017-05-12 18:01:32 +0000498static void *__kmp_launch_worker(void *thr) {
499 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000500#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000501 sigset_t new_set, old_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000502#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000503 void *exit_val;
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000504#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +0000505 KMP_OS_OPENBSD || KMP_OS_HURD
Jonathan Peyton30419822017-05-12 18:01:32 +0000506 void *volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000507#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000508 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509
Jonathan Peyton30419822017-05-12 18:01:32 +0000510 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
511 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000512#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000513 __kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000514#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000515#if KMP_STATS_ENABLED
Jonathan Peyton40039ac2017-11-07 23:32:13 +0000516 // set thread local index to point to thread-specific stats
Jonathan Peyton30419822017-05-12 18:01:32 +0000517 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000518 __kmp_stats_thread_ptr->startLife();
Jonathan Peyton30419822017-05-12 18:01:32 +0000519 KMP_SET_THREAD_STATE(IDLE);
520 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000521#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000522
523#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000524 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000525#endif /* USE_ITT_BUILD */
526
Alp Toker763b9392014-02-28 09:42:41 +0000527#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +0000528 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000529#endif
530
531#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000532 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
533 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
534 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
535 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
536 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000537#endif
538
539#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000540 // Set FP control regs to be a copy of the parallel initialization thread's.
541 __kmp_clear_x87_fpu_status_word();
542 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
543 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000544#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
545
546#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000547 status = sigfillset(&new_set);
548 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
549 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
550 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000551#endif /* KMP_BLOCK_SIGNALS */
552
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +0000553#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
554 KMP_OS_OPENBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000555 if (__kmp_stkoffset > 0 && gtid > 0) {
556 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
557 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000558#endif
559
Jonathan Peyton30419822017-05-12 18:01:32 +0000560 KMP_MB();
561 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000562
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000564
Jonathan Peyton30419822017-05-12 18:01:32 +0000565 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000566
567#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000568 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
569 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000570#endif /* KMP_BLOCK_SIGNALS */
571
Jonathan Peyton30419822017-05-12 18:01:32 +0000572 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000573}
574
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000575#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000576/* The monitor thread controls all of the threads in the complex */
577
Jonathan Peyton30419822017-05-12 18:01:32 +0000578static void *__kmp_launch_monitor(void *thr) {
579 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000580#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000581 sigset_t new_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000582#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000583 struct timespec interval;
584 int yield_count;
585 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000586
Jonathan Peyton30419822017-05-12 18:01:32 +0000587 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000588
Jonathan Peyton30419822017-05-12 18:01:32 +0000589 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 /* register us as the monitor thread */
592 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000593#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 __kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000595#endif
596
Jonathan Peyton30419822017-05-12 18:01:32 +0000597 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000598
599#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000600 // Instruct Intel(R) Threading Tools to ignore monitor thread.
601 __kmp_itt_thread_ignore();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000602#endif /* USE_ITT_BUILD */
603
Jonathan Peyton30419822017-05-12 18:01:32 +0000604 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
605 (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000606
Jonathan Peyton30419822017-05-12 18:01:32 +0000607 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000608
609#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000610 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
611 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
612 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
613 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
614 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000615#endif
616
Jonathan Peyton30419822017-05-12 18:01:32 +0000617#if KMP_REAL_TIME_FIX
618 // This is a potential fix which allows application with real-time scheduling
619 // policy work. However, decision about the fix is not made yet, so it is
620 // disabled by default.
621 { // Are program started with real-time scheduling policy?
622 int sched = sched_getscheduler(0);
623 if (sched == SCHED_FIFO || sched == SCHED_RR) {
624 // Yes, we are a part of real-time application. Try to increase the
625 // priority of the monitor.
626 struct sched_param param;
627 int max_priority = sched_get_priority_max(sched);
628 int rc;
629 KMP_WARNING(RealTimeSchedNotSupported);
630 sched_getparam(0, &param);
631 if (param.sched_priority < max_priority) {
632 param.sched_priority += 1;
633 rc = sched_setscheduler(0, sched, &param);
634 if (rc != 0) {
635 int error = errno;
636 kmp_msg_t err_code = KMP_ERR(error);
637 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
638 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
639 if (__kmp_generate_warnings == kmp_warnings_off) {
640 __kmp_str_free(&err_code.str);
641 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000642 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000643 } else {
644 // We cannot abort here, because number of CPUs may be enough for all
645 // the threads, including the monitor thread, so application could
646 // potentially work...
647 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
648 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
649 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000650 }
651 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000652 // AC: free thread that waits for monitor started
653 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
654 }
655#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +0000656
Jonathan Peyton30419822017-05-12 18:01:32 +0000657 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000658
Jonathan Peyton30419822017-05-12 18:01:32 +0000659 if (__kmp_monitor_wakeups == 1) {
660 interval.tv_sec = 1;
661 interval.tv_nsec = 0;
662 } else {
663 interval.tv_sec = 0;
664 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
665 }
666
667 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
668
669 if (__kmp_yield_cycle) {
670 __kmp_yielding_on = 0; /* Start out with yielding shut off */
671 yield_count = __kmp_yield_off_count;
672 } else {
673 __kmp_yielding_on = 1; /* Yielding is on permanently */
674 }
675
676 while (!TCR_4(__kmp_global.g.g_done)) {
677 struct timespec now;
678 struct timeval tval;
679
680 /* This thread monitors the state of the system */
681
682 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
683
684 status = gettimeofday(&tval, NULL);
685 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
686 TIMEVAL_TO_TIMESPEC(&tval, &now);
687
688 now.tv_sec += interval.tv_sec;
689 now.tv_nsec += interval.tv_nsec;
690
691 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
692 now.tv_sec += 1;
693 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000694 }
695
Jonathan Peyton30419822017-05-12 18:01:32 +0000696 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
697 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
698 // AC: the monitor should not fall asleep if g_done has been set
699 if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex
700 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
701 &__kmp_wait_mx.m_mutex, &now);
702 if (status != 0) {
703 if (status != ETIMEDOUT && status != EINTR) {
704 KMP_SYSFAIL("pthread_cond_timedwait", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000705 }
706 }
707 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000708 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
709 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000710
711 if (__kmp_yield_cycle) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000712 yield_cycles++;
713 if ((yield_cycles % yield_count) == 0) {
714 if (__kmp_yielding_on) {
715 __kmp_yielding_on = 0; /* Turn it off now */
716 yield_count = __kmp_yield_off_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000717 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000718 __kmp_yielding_on = 1; /* Turn it on now */
719 yield_count = __kmp_yield_on_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000720 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000721 yield_cycles = 0;
722 }
723 } else {
724 __kmp_yielding_on = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000725 }
726
Jonathan Peyton30419822017-05-12 18:01:32 +0000727 TCW_4(__kmp_global.g.g_time.dt.t_value,
728 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
729
730 KMP_MB(); /* Flush all pending memory write invalidates. */
731 }
732
733 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000734
735#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000736 status = sigfillset(&new_set);
737 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
738 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
739 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000740#endif /* KMP_BLOCK_SIGNALS */
741
Jonathan Peyton30419822017-05-12 18:01:32 +0000742 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000743
Jonathan Peyton30419822017-05-12 18:01:32 +0000744 if (__kmp_global.g.g_abort != 0) {
745 /* now we need to terminate the worker threads */
746 /* the value of t_abort is the signal we caught */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000747
Jonathan Peyton30419822017-05-12 18:01:32 +0000748 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000749
Jonathan Peyton30419822017-05-12 18:01:32 +0000750 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
751 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000752
Jonathan Peyton30419822017-05-12 18:01:32 +0000753 /* terminate the OpenMP worker threads */
754 /* TODO this is not valid for sibling threads!!
755 * the uber master might not be 0 anymore.. */
756 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
757 __kmp_terminate_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000758
Jonathan Peyton30419822017-05-12 18:01:32 +0000759 __kmp_cleanup();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000760
Jonathan Peyton30419822017-05-12 18:01:32 +0000761 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
762 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000763
Jonathan Peyton30419822017-05-12 18:01:32 +0000764 if (__kmp_global.g.g_abort > 0)
765 raise(__kmp_global.g.g_abort);
766 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000767
Jonathan Peyton30419822017-05-12 18:01:32 +0000768 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000769
Jonathan Peyton30419822017-05-12 18:01:32 +0000770 return thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000771}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000772#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000773
Jonathan Peyton30419822017-05-12 18:01:32 +0000774void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
775 pthread_t handle;
776 pthread_attr_t thread_attr;
777 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000778
Jonathan Peyton30419822017-05-12 18:01:32 +0000779 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000780
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000781#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000782 // sets up worker thread stats
783 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000784
Jonathan Peyton30419822017-05-12 18:01:32 +0000785 // th->th.th_stats is used to transfer thread-specific stats-pointer to
786 // __kmp_launch_worker. So when thread is created (goes into
Jonathan Peyton40039ac2017-11-07 23:32:13 +0000787 // __kmp_launch_worker) it will set its thread local pointer to
Jonathan Peyton30419822017-05-12 18:01:32 +0000788 // th->th.th_stats
789 if (!KMP_UBER_GTID(gtid)) {
790 th->th.th_stats = __kmp_stats_list->push_back(gtid);
791 } else {
792 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
793 // so set the th->th.th_stats field to it.
794 th->th.th_stats = __kmp_stats_thread_ptr;
795 }
796 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000797
798#endif // KMP_STATS_ENABLED
799
Jonathan Peyton30419822017-05-12 18:01:32 +0000800 if (KMP_UBER_GTID(gtid)) {
801 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid));
802 th->th.th_info.ds.ds_thread = pthread_self();
803 __kmp_set_stack_info(gtid, th);
804 __kmp_check_stack_overlap(th);
805 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000806 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000807
Jonathan Peyton30419822017-05-12 18:01:32 +0000808 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000809
Jonathan Peyton30419822017-05-12 18:01:32 +0000810 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000811
812#ifdef KMP_THREAD_ATTR
Jonathan Peyton30419822017-05-12 18:01:32 +0000813 status = pthread_attr_init(&thread_attr);
814 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000815 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000816 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000817 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
818 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000819 __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000820 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000821
Jonathan Peyton30419822017-05-12 18:01:32 +0000822 /* Set stack size for this thread now.
823 The multiple of 2 is there because on some machines, requesting an unusual
824 stacksize causes the thread to have an offset before the dummy alloca()
825 takes place to create the offset. Since we want the user to have a
826 sufficient stacksize AND support a stack offset, we alloca() twice the
827 offset so that the upcoming alloca() does not eliminate any premade offset,
828 and also gives the user the stack space they requested for all threads */
829 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000830
Jonathan Peyton30419822017-05-12 18:01:32 +0000831 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
832 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
833 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000834
Jim Cownie5e8470a2013-09-27 10:38:44 +0000835#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton30419822017-05-12 18:01:32 +0000836 status = pthread_attr_setstacksize(&thread_attr, stack_size);
837#ifdef KMP_BACKUP_STKSIZE
838 if (status != 0) {
839 if (!__kmp_env_stksize) {
840 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
841 __kmp_stksize = KMP_BACKUP_STKSIZE;
842 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
843 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
844 "bytes\n",
845 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
846 status = pthread_attr_setstacksize(&thread_attr, stack_size);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000847 }
848 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000849#endif /* KMP_BACKUP_STKSIZE */
850 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000851 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
852 KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000853 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000854#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000855
Jim Cownie5e8470a2013-09-27 10:38:44 +0000856#endif /* KMP_THREAD_ATTR */
857
Jonathan Peyton30419822017-05-12 18:01:32 +0000858 status =
859 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th);
860 if (status != 0 || !handle) { // ??? Why do we check handle??
861#ifdef _POSIX_THREAD_ATTR_STACKSIZE
862 if (status == EINVAL) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000863 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
864 KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000865 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000866 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000867 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
868 KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000869 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000870#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
871 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000872 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status),
873 KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000874 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000875 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000876 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000877
Jonathan Peyton30419822017-05-12 18:01:32 +0000878 th->th.th_info.ds.ds_thread = handle;
879
880#ifdef KMP_THREAD_ATTR
881 status = pthread_attr_destroy(&thread_attr);
882 if (status) {
883 kmp_msg_t err_code = KMP_ERR(status);
884 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
885 __kmp_msg_null);
886 if (__kmp_generate_warnings == kmp_warnings_off) {
887 __kmp_str_free(&err_code.str);
888 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000889 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000890#endif /* KMP_THREAD_ATTR */
891
892 KMP_MB(); /* Flush all pending memory write invalidates. */
893
894 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000895
896} // __kmp_create_worker
897
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000898#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000899void __kmp_create_monitor(kmp_info_t *th) {
900 pthread_t handle;
901 pthread_attr_t thread_attr;
902 size_t size;
903 int status;
904 int auto_adj_size = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000905
Jonathan Peyton30419822017-05-12 18:01:32 +0000906 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
907 // We don't need monitor thread in case of MAX_BLOCKTIME
908 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
909 "MAX blocktime\n"));
910 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
911 th->th.th_info.ds.ds_gtid = 0;
912 return;
913 }
914 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
915
916 KMP_MB(); /* Flush all pending memory write invalidates. */
917
918 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
919 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
920#if KMP_REAL_TIME_FIX
921 TCW_4(__kmp_global.g.g_time.dt.t_value,
922 -1); // Will use it for synchronization a bit later.
923#else
924 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
925#endif // KMP_REAL_TIME_FIX
926
927#ifdef KMP_THREAD_ATTR
928 if (__kmp_monitor_stksize == 0) {
929 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
930 auto_adj_size = TRUE;
931 }
932 status = pthread_attr_init(&thread_attr);
933 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000934 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000935 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000936 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
937 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000938 __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000939 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000940
941#ifdef _POSIX_THREAD_ATTR_STACKSIZE
942 status = pthread_attr_getstacksize(&thread_attr, &size);
943 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
944#else
945 size = __kmp_sys_min_stksize;
946#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
947#endif /* KMP_THREAD_ATTR */
948
949 if (__kmp_monitor_stksize == 0) {
950 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
951 }
952 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
953 __kmp_monitor_stksize = __kmp_sys_min_stksize;
954 }
955
956 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
957 "requested stacksize = %lu bytes\n",
958 size, __kmp_monitor_stksize));
959
960retry:
961
962/* Set stack size for this thread now. */
963#ifdef _POSIX_THREAD_ATTR_STACKSIZE
964 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
965 __kmp_monitor_stksize));
966 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
967 if (status != 0) {
968 if (auto_adj_size) {
969 __kmp_monitor_stksize *= 2;
970 goto retry;
Jonathan Peyton4fee5f62015-12-18 23:20:36 +0000971 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000972 kmp_msg_t err_code = KMP_ERR(status);
973 __kmp_msg(kmp_ms_warning, // should this be fatal? BB
974 KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize),
975 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
976 if (__kmp_generate_warnings == kmp_warnings_off) {
977 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000978 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000979 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000980#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000981
Jonathan Peyton30419822017-05-12 18:01:32 +0000982 status =
983 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000984
Jonathan Peyton30419822017-05-12 18:01:32 +0000985 if (status != 0) {
986#ifdef _POSIX_THREAD_ATTR_STACKSIZE
987 if (status == EINVAL) {
988 if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) {
989 __kmp_monitor_stksize *= 2;
990 goto retry;
991 }
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000992 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
993 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize),
994 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000995 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000996 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000997 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
998 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize),
999 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001000 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001001#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1002 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001003 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status),
1004 KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001005 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001006 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001007 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001008
Jonathan Peyton30419822017-05-12 18:01:32 +00001009 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001010
Jonathan Peyton30419822017-05-12 18:01:32 +00001011#if KMP_REAL_TIME_FIX
1012 // Wait for the monitor thread is really started and set its *priority*.
1013 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) ==
1014 sizeof(__kmp_global.g.g_time.dt.t_value));
1015 __kmp_wait_yield_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value,
1016 -1, &__kmp_neq_4, NULL);
1017#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +00001018
Jonathan Peyton30419822017-05-12 18:01:32 +00001019#ifdef KMP_THREAD_ATTR
1020 status = pthread_attr_destroy(&thread_attr);
1021 if (status != 0) {
1022 kmp_msg_t err_code = KMP_ERR(status);
1023 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1024 __kmp_msg_null);
1025 if (__kmp_generate_warnings == kmp_warnings_off) {
1026 __kmp_str_free(&err_code.str);
1027 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001028 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001029#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001030
Jonathan Peyton30419822017-05-12 18:01:32 +00001031 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001032
Jonathan Peyton30419822017-05-12 18:01:32 +00001033 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
1034 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001035
1036} // __kmp_create_monitor
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001037#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001038
Jonathan Peyton30419822017-05-12 18:01:32 +00001039void __kmp_exit_thread(int exit_status) {
1040 pthread_exit((void *)(intptr_t)exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001041} // __kmp_exit_thread
1042
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001043#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001044void __kmp_resume_monitor();
1045
Jonathan Peyton30419822017-05-12 18:01:32 +00001046void __kmp_reap_monitor(kmp_info_t *th) {
1047 int status;
1048 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001049
Jonathan Peyton30419822017-05-12 18:01:32 +00001050 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1051 " %#.8lx\n",
1052 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001053
Jonathan Peyton30419822017-05-12 18:01:32 +00001054 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1055 // If both tid and gtid are 0, it means the monitor did not ever start.
1056 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1057 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1058 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1059 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1060 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001061 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001062
Jonathan Peyton30419822017-05-12 18:01:32 +00001063 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001064
Jonathan Peyton30419822017-05-12 18:01:32 +00001065 /* First, check to see whether the monitor thread exists to wake it up. This
1066 is to avoid performance problem when the monitor sleeps during
1067 blocktime-size interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001068
Jonathan Peyton30419822017-05-12 18:01:32 +00001069 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1070 if (status != ESRCH) {
1071 __kmp_resume_monitor(); // Wake up the monitor thread
1072 }
1073 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
1074 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1075 if (exit_val != th) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001076 __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001077 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001078
Jonathan Peyton30419822017-05-12 18:01:32 +00001079 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1080 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001081
Jonathan Peyton30419822017-05-12 18:01:32 +00001082 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1083 " %#.8lx\n",
1084 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001085
Jonathan Peyton30419822017-05-12 18:01:32 +00001086 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001087}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001088#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001089
Jonathan Peyton30419822017-05-12 18:01:32 +00001090void __kmp_reap_worker(kmp_info_t *th) {
1091 int status;
1092 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001093
Jonathan Peyton30419822017-05-12 18:01:32 +00001094 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001095
Jonathan Peyton30419822017-05-12 18:01:32 +00001096 KA_TRACE(
1097 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001098
Jonathan Peyton30419822017-05-12 18:01:32 +00001099 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001100#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001101 /* Don't expose these to the user until we understand when they trigger */
1102 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001103 __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001104 }
1105 if (exit_val != th) {
1106 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1107 "exit_val = %p\n",
1108 th->th.th_info.ds.ds_gtid, exit_val));
1109 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001110#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001111
Jonathan Peyton30419822017-05-12 18:01:32 +00001112 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1113 th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001114
Jonathan Peyton30419822017-05-12 18:01:32 +00001115 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001116}
1117
Jim Cownie5e8470a2013-09-27 10:38:44 +00001118#if KMP_HANDLE_SIGNALS
1119
Jonathan Peyton30419822017-05-12 18:01:32 +00001120static void __kmp_null_handler(int signo) {
1121 // Do nothing, for doing SIG_IGN-type actions.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001122} // __kmp_null_handler
1123
Jonathan Peyton30419822017-05-12 18:01:32 +00001124static void __kmp_team_handler(int signo) {
1125 if (__kmp_global.g.g_abort == 0) {
1126/* Stage 1 signal handler, let's shut down all of the threads */
1127#ifdef KMP_DEBUG
1128 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
1129#endif
1130 switch (signo) {
1131 case SIGHUP:
1132 case SIGINT:
1133 case SIGQUIT:
1134 case SIGILL:
1135 case SIGABRT:
1136 case SIGFPE:
1137 case SIGBUS:
1138 case SIGSEGV:
1139#ifdef SIGSYS
1140 case SIGSYS:
1141#endif
1142 case SIGTERM:
1143 if (__kmp_debug_buf) {
1144 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001145 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001146 KMP_MB(); // Flush all pending memory write invalidates.
1147 TCW_4(__kmp_global.g.g_abort, signo);
1148 KMP_MB(); // Flush all pending memory write invalidates.
1149 TCW_4(__kmp_global.g.g_done, TRUE);
1150 KMP_MB(); // Flush all pending memory write invalidates.
1151 break;
1152 default:
1153#ifdef KMP_DEBUG
1154 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1155#endif
1156 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001157 }
1158 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001159} // __kmp_team_handler
1160
Jonathan Peyton30419822017-05-12 18:01:32 +00001161static void __kmp_sigaction(int signum, const struct sigaction *act,
1162 struct sigaction *oldact) {
1163 int rc = sigaction(signum, act, oldact);
1164 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001165}
1166
Jonathan Peyton30419822017-05-12 18:01:32 +00001167static void __kmp_install_one_handler(int sig, sig_func_t handler_func,
1168 int parallel_init) {
1169 KMP_MB(); // Flush all pending memory write invalidates.
1170 KB_TRACE(60,
1171 ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
1172 if (parallel_init) {
1173 struct sigaction new_action;
1174 struct sigaction old_action;
1175 new_action.sa_handler = handler_func;
1176 new_action.sa_flags = 0;
1177 sigfillset(&new_action.sa_mask);
1178 __kmp_sigaction(sig, &new_action, &old_action);
1179 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
1180 sigaddset(&__kmp_sigset, sig);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001181 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001182 // Restore/keep user's handler if one previously installed.
1183 __kmp_sigaction(sig, &old_action, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001184 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001185 } else {
1186 // Save initial/system signal handlers to see if user handlers installed.
1187 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001188 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001189 KMP_MB(); // Flush all pending memory write invalidates.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001190} // __kmp_install_one_handler
1191
Jonathan Peyton30419822017-05-12 18:01:32 +00001192static void __kmp_remove_one_handler(int sig) {
1193 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig));
1194 if (sigismember(&__kmp_sigset, sig)) {
1195 struct sigaction old;
1196 KMP_MB(); // Flush all pending memory write invalidates.
1197 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
1198 if ((old.sa_handler != __kmp_team_handler) &&
1199 (old.sa_handler != __kmp_null_handler)) {
1200 // Restore the users signal handler.
1201 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1202 "restoring: sig=%d\n",
1203 sig));
1204 __kmp_sigaction(sig, &old, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001205 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001206 sigdelset(&__kmp_sigset, sig);
1207 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001208 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001209} // __kmp_remove_one_handler
1210
Jonathan Peyton30419822017-05-12 18:01:32 +00001211void __kmp_install_signals(int parallel_init) {
1212 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init));
1213 if (__kmp_handle_signals || !parallel_init) {
1214 // If ! parallel_init, we do not install handlers, just save original
1215 // handlers. Let us do it even __handle_signals is 0.
1216 sigemptyset(&__kmp_sigset);
1217 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
1218 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1219 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
1220 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1221 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1222 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1223 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
1224 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1225#ifdef SIGSYS
1226 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1227#endif // SIGSYS
1228 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1229#ifdef SIGPIPE
1230 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1231#endif // SIGPIPE
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001232 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001233} // __kmp_install_signals
1234
Jonathan Peyton30419822017-05-12 18:01:32 +00001235void __kmp_remove_signals(void) {
1236 int sig;
1237 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1238 for (sig = 1; sig < NSIG; ++sig) {
1239 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001240 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001241} // __kmp_remove_signals
1242
Jim Cownie5e8470a2013-09-27 10:38:44 +00001243#endif // KMP_HANDLE_SIGNALS
1244
Jonathan Peyton30419822017-05-12 18:01:32 +00001245void __kmp_enable(int new_state) {
1246#ifdef KMP_CANCEL_THREADS
1247 int status, old_state;
1248 status = pthread_setcancelstate(new_state, &old_state);
1249 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1250 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
1251#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001252}
1253
Jonathan Peyton30419822017-05-12 18:01:32 +00001254void __kmp_disable(int *old_state) {
1255#ifdef KMP_CANCEL_THREADS
1256 int status;
1257 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1258 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1259#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001260}
1261
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001262static void __kmp_atfork_prepare(void) {
1263 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
1264 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001265}
1266
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001267static void __kmp_atfork_parent(void) {
1268 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
1269 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001270}
1271
Jonathan Peyton30419822017-05-12 18:01:32 +00001272/* Reset the library so execution in the child starts "all over again" with
1273 clean data structures in initial states. Don't worry about freeing memory
1274 allocated by parent, just abandon it to be safe. */
1275static void __kmp_atfork_child(void) {
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001276 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jonathan Peyton30419822017-05-12 18:01:32 +00001277 /* TODO make sure this is done right for nested/sibling */
1278 // ATT: Memory leaks are here? TODO: Check it and fix.
1279 /* KMP_ASSERT( 0 ); */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001280
Jonathan Peyton30419822017-05-12 18:01:32 +00001281 ++__kmp_fork_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001282
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001283#if KMP_AFFINITY_SUPPORTED
1284#if KMP_OS_LINUX
Jonathan Peytond330e632017-06-13 17:16:12 +00001285 // reset the affinity in the child to the initial thread
1286 // affinity in the parent
1287 kmp_set_thread_affinity_mask_initial();
1288#endif
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001289 // Set default not to bind threads tightly in the child (we’re expecting
1290 // over-subscription after the fork and this can improve things for
1291 // scripting languages that use OpenMP inside process-parallel code).
1292 __kmp_affinity_type = affinity_none;
1293#if OMP_40_ENABLED
1294 if (__kmp_nested_proc_bind.bind_types != NULL) {
1295 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
1296 }
1297#endif // OMP_40_ENABLED
1298#endif // KMP_AFFINITY_SUPPORTED
Jonathan Peytond330e632017-06-13 17:16:12 +00001299
Jonathan Peyton30419822017-05-12 18:01:32 +00001300 __kmp_init_runtime = FALSE;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001301#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001302 __kmp_init_monitor = 0;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001303#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001304 __kmp_init_parallel = FALSE;
1305 __kmp_init_middle = FALSE;
1306 __kmp_init_serial = FALSE;
1307 TCW_4(__kmp_init_gtid, FALSE);
1308 __kmp_init_common = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001309
Jonathan Peyton30419822017-05-12 18:01:32 +00001310 TCW_4(__kmp_init_user_locks, FALSE);
1311#if !KMP_USE_DYNAMIC_LOCK
1312 __kmp_user_lock_table.used = 1;
1313 __kmp_user_lock_table.allocated = 0;
1314 __kmp_user_lock_table.table = NULL;
1315 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001316#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001317
Jonathan Peyton30419822017-05-12 18:01:32 +00001318 __kmp_all_nth = 0;
1319 TCW_4(__kmp_nth, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001320
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001321 __kmp_thread_pool = NULL;
1322 __kmp_thread_pool_insert_pt = NULL;
1323 __kmp_team_pool = NULL;
1324
Jonathan Peyton30419822017-05-12 18:01:32 +00001325 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate
1326 here so threadprivate doesn't use stale data */
1327 KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n",
1328 __kmp_threadpriv_cache_list));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001329
Jonathan Peyton30419822017-05-12 18:01:32 +00001330 while (__kmp_threadpriv_cache_list != NULL) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001331
Jonathan Peyton30419822017-05-12 18:01:32 +00001332 if (*__kmp_threadpriv_cache_list->addr != NULL) {
1333 KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n",
1334 &(*__kmp_threadpriv_cache_list->addr)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001335
Jonathan Peyton30419822017-05-12 18:01:32 +00001336 *__kmp_threadpriv_cache_list->addr = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001337 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001338 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
1339 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001340
Jonathan Peyton30419822017-05-12 18:01:32 +00001341 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001342
Jonathan Peyton30419822017-05-12 18:01:32 +00001343 /* reset statically initialized locks */
1344 __kmp_init_bootstrap_lock(&__kmp_initz_lock);
1345 __kmp_init_bootstrap_lock(&__kmp_stdio_lock);
1346 __kmp_init_bootstrap_lock(&__kmp_console_lock);
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001347 __kmp_init_bootstrap_lock(&__kmp_task_team_lock);
1348
Andrey Churbanov5388acd2018-01-11 15:09:49 +00001349#if USE_ITT_BUILD
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001350 __kmp_itt_reset(); // reset ITT's global state
Andrey Churbanov5388acd2018-01-11 15:09:49 +00001351#endif /* USE_ITT_BUILD */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001352
Jonathan Peyton30419822017-05-12 18:01:32 +00001353 /* This is necessary to make sure no stale data is left around */
1354 /* AC: customers complain that we use unsafe routines in the atfork
1355 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1356 in dynamic_link when check the presence of shared tbbmalloc library.
1357 Suggestion is to make the library initialization lazier, similar
1358 to what done for __kmpc_begin(). */
1359 // TODO: synchronize all static initializations with regular library
1360 // startup; look at kmp_global.cpp and etc.
1361 //__kmp_internal_begin ();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001362}
1363
Jonathan Peyton30419822017-05-12 18:01:32 +00001364void __kmp_register_atfork(void) {
1365 if (__kmp_need_register_atfork) {
1366 int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent,
1367 __kmp_atfork_child);
1368 KMP_CHECK_SYSFAIL("pthread_atfork", status);
1369 __kmp_need_register_atfork = FALSE;
1370 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001371}
1372
Jonathan Peyton30419822017-05-12 18:01:32 +00001373void __kmp_suspend_initialize(void) {
1374 int status;
1375 status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr);
1376 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1377 status = pthread_condattr_init(&__kmp_suspend_cond_attr);
1378 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1379}
1380
1381static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
1382 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count);
1383 if (th->th.th_suspend_init_count <= __kmp_fork_count) {
1384 /* this means we haven't initialized the suspension pthread objects for this
1385 thread in this instance of the process */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001386 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001387 status = pthread_cond_init(&th->th.th_suspend_cv.c_cond,
1388 &__kmp_suspend_cond_attr);
1389 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
1390 status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex,
1391 &__kmp_suspend_mutex_attr);
1392 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1393 *(volatile int *)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1394 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001395 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001396}
1397
Jonathan Peyton30419822017-05-12 18:01:32 +00001398void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
1399 if (th->th.th_suspend_init_count > __kmp_fork_count) {
1400 /* this means we have initialize the suspension pthread objects for this
1401 thread in this instance of the process */
1402 int status;
1403
1404 status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond);
1405 if (status != 0 && status != EBUSY) {
1406 KMP_SYSFAIL("pthread_cond_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001407 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001408 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
1409 if (status != 0 && status != EBUSY) {
1410 KMP_SYSFAIL("pthread_mutex_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001411 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001412 --th->th.th_suspend_init_count;
1413 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1414 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001415}
1416
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001417/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +00001418 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001419template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001420static inline void __kmp_suspend_template(int th_gtid, C *flag) {
1421 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
1422 kmp_info_t *th = __kmp_threads[th_gtid];
1423 int status;
1424 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001425
Jonathan Peyton30419822017-05-12 18:01:32 +00001426 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1427 flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001428
Jonathan Peyton30419822017-05-12 18:01:32 +00001429 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001430
Jonathan Peyton30419822017-05-12 18:01:32 +00001431 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1432 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433
Jonathan Peyton30419822017-05-12 18:01:32 +00001434 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1435 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001436
Jonathan Peyton30419822017-05-12 18:01:32 +00001437 /* TODO: shouldn't this use release semantics to ensure that
1438 __kmp_suspend_initialize_thread gets called first? */
1439 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001440
Jonathan Peyton30419822017-05-12 18:01:32 +00001441 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1442 " was %x\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001443 th_gtid, flag->get(), flag->load(), old_spin));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001444
Jonathan Peyton30419822017-05-12 18:01:32 +00001445 if (flag->done_check_val(old_spin)) {
1446 old_spin = flag->unset_sleeping();
1447 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
1448 "for spin(%p)\n",
1449 th_gtid, flag->get()));
1450 } else {
1451 /* Encapsulate in a loop as the documentation states that this may
1452 "with low probability" return when the condition variable has
1453 not been signaled or broadcast */
1454 int deactivated = FALSE;
1455 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1456
1457 while (flag->is_sleeping()) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001458#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001459 char buffer[128];
1460 __kmp_suspend_count++;
1461 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1462 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
1463 buffer);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001464#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001465 // Mark the thread as no longer active (only in the first iteration of the
1466 // loop).
1467 if (!deactivated) {
1468 th->th.th_active = FALSE;
1469 if (th->th.th_active_in_pool) {
1470 th->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001471 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001472 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1473 }
1474 deactivated = TRUE;
1475 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001476
1477#if USE_SUSPEND_TIMEOUT
Jonathan Peyton30419822017-05-12 18:01:32 +00001478 struct timespec now;
1479 struct timeval tval;
1480 int msecs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001481
Jonathan Peyton30419822017-05-12 18:01:32 +00001482 status = gettimeofday(&tval, NULL);
1483 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1484 TIMEVAL_TO_TIMESPEC(&tval, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001485
Jonathan Peyton30419822017-05-12 18:01:32 +00001486 msecs = (4 * __kmp_dflt_blocktime) + 200;
1487 now.tv_sec += msecs / 1000;
1488 now.tv_nsec += (msecs % 1000) * 1000;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001489
Jonathan Peyton30419822017-05-12 18:01:32 +00001490 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1491 "pthread_cond_timedwait\n",
1492 th_gtid));
1493 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1494 &th->th.th_suspend_mx.m_mutex, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001495#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001496 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1497 " pthread_cond_wait\n",
1498 th_gtid));
1499 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1500 &th->th.th_suspend_mx.m_mutex);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001501#endif
1502
Jonathan Peyton30419822017-05-12 18:01:32 +00001503 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1504 KMP_SYSFAIL("pthread_cond_wait", status);
1505 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001506#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001507 if (status == ETIMEDOUT) {
1508 if (flag->is_sleeping()) {
1509 KF_TRACE(100,
1510 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1511 } else {
1512 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1513 "not set!\n",
1514 th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001515 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001516 } else if (flag->is_sleeping()) {
1517 KF_TRACE(100,
1518 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1519 }
1520#endif
1521 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +00001522
Jonathan Peyton30419822017-05-12 18:01:32 +00001523 // Mark the thread as active again (if it was previous marked as inactive)
1524 if (deactivated) {
1525 th->th.th_active = TRUE;
1526 if (TCR_4(th->th.th_in_pool)) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001527 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001528 th->th.th_active_in_pool = TRUE;
1529 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001530 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001531 }
1532#ifdef DEBUG_SUSPEND
1533 {
1534 char buffer[128];
1535 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1536 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1537 buffer);
1538 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001539#endif
1540
Jonathan Peyton30419822017-05-12 18:01:32 +00001541 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1542 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1543 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001544}
1545
1546void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001547 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001548}
1549void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001550 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001551}
1552void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001553 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001554}
1555
Jim Cownie5e8470a2013-09-27 10:38:44 +00001556/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +00001557 after setting the sleep bit indicated by the flag argument to FALSE.
1558 The target thread must already have called __kmp_suspend_template() */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001559template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001560static inline void __kmp_resume_template(int target_gtid, C *flag) {
1561 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1562 kmp_info_t *th = __kmp_threads[target_gtid];
1563 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001564
1565#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001566 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001567#endif
1568
Jonathan Peyton30419822017-05-12 18:01:32 +00001569 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1570 gtid, target_gtid));
1571 KMP_DEBUG_ASSERT(gtid != target_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001572
Jonathan Peyton30419822017-05-12 18:01:32 +00001573 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001574
Jonathan Peyton30419822017-05-12 18:01:32 +00001575 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1576 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001577
Jonathan Peyton30419822017-05-12 18:01:32 +00001578 if (!flag) { // coming from __kmp_null_resume_wrapper
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001579 flag = (C *)CCAST(void *, th->th.th_sleep_loc);
Jonathan Peyton30419822017-05-12 18:01:32 +00001580 }
1581
1582 // First, check if the flag is null or its type has changed. If so, someone
1583 // else woke it up.
1584 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
1585 // simply shows what
1586 // flag was cast to
1587 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1588 "awake: flag(%p)\n",
1589 gtid, target_gtid, NULL));
1590 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1591 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1592 return;
1593 } else { // if multiple threads are sleeping, flag should be internally
1594 // referring to a specific thread here
1595 typename C::flag_t old_spin = flag->unset_sleeping();
1596 if (!flag->is_sleeping_val(old_spin)) {
1597 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1598 "awake: flag(%p): "
1599 "%u => %u\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001600 gtid, target_gtid, flag->get(), old_spin, flag->load()));
Jonathan Peyton30419822017-05-12 18:01:32 +00001601 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1602 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1603 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001604 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001605 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1606 "sleep bit for flag's loc(%p): "
1607 "%u => %u\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001608 gtid, target_gtid, flag->get(), old_spin, flag->load()));
Jonathan Peyton30419822017-05-12 18:01:32 +00001609 }
1610 TCW_PTR(th->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001611
1612#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001613 {
1614 char buffer[128];
1615 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1616 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
1617 target_gtid, buffer);
1618 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001619#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001620 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
1621 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1622 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1623 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1624 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
1625 " for T#%d\n",
1626 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001627}
1628
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001629void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001630 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001631}
1632void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001633 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001634}
1635void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001636 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001637}
1638
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001639#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001640void __kmp_resume_monitor() {
1641 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1642 int status;
Jim Cownie07ea89f2014-09-03 11:10:54 +00001643#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001644 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1645 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
1646 KMP_GTID_MONITOR));
1647 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001648#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001649 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1650 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001651#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001652 {
1653 char buffer[128];
1654 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
1655 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
1656 KMP_GTID_MONITOR, buffer);
1657 }
Jim Cownie07ea89f2014-09-03 11:10:54 +00001658#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001659 status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
1660 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1661 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
1662 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1663 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
1664 " for T#%d\n",
1665 gtid, KMP_GTID_MONITOR));
Jim Cownie07ea89f2014-09-03 11:10:54 +00001666}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001667#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001668
Jonathan Peyton30419822017-05-12 18:01:32 +00001669void __kmp_yield(int cond) {
1670 if (!cond)
1671 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001672#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001673 if (!__kmp_yielding_on)
1674 return;
Jonathan Peyton581fdba2017-02-15 17:19:21 +00001675#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001676 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1677 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001678#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001679 sched_yield();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001680}
1681
Jonathan Peyton30419822017-05-12 18:01:32 +00001682void __kmp_gtid_set_specific(int gtid) {
1683 if (__kmp_init_gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001684 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001685 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1686 (void *)(intptr_t)(gtid + 1));
1687 KMP_CHECK_SYSFAIL("pthread_setspecific", status);
1688 } else {
1689 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1690 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001691}
1692
Jonathan Peyton30419822017-05-12 18:01:32 +00001693int __kmp_gtid_get_specific() {
1694 int gtid;
1695 if (!__kmp_init_gtid) {
1696 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
1697 "KMP_GTID_SHUTDOWN\n"));
1698 return KMP_GTID_SHUTDOWN;
1699 }
1700 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1701 if (gtid == 0) {
1702 gtid = KMP_GTID_DNE;
1703 } else {
1704 gtid--;
1705 }
1706 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1707 __kmp_gtid_threadprivate_key, gtid));
1708 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001709}
1710
Jonathan Peyton30419822017-05-12 18:01:32 +00001711double __kmp_read_cpu_time(void) {
1712 /*clock_t t;*/
1713 struct tms buffer;
1714
1715 /*t =*/times(&buffer);
1716
1717 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001718}
1719
Jonathan Peyton30419822017-05-12 18:01:32 +00001720int __kmp_read_system_info(struct kmp_sys_info *info) {
1721 int status;
1722 struct rusage r_usage;
1723
1724 memset(info, 0, sizeof(*info));
1725
1726 status = getrusage(RUSAGE_SELF, &r_usage);
1727 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
1728
1729 // The maximum resident set size utilized (in kilobytes)
1730 info->maxrss = r_usage.ru_maxrss;
1731 // The number of page faults serviced without any I/O
1732 info->minflt = r_usage.ru_minflt;
1733 // The number of page faults serviced that required I/O
1734 info->majflt = r_usage.ru_majflt;
1735 // The number of times a process was "swapped" out of memory
1736 info->nswap = r_usage.ru_nswap;
1737 // The number of times the file system had to perform input
1738 info->inblock = r_usage.ru_inblock;
1739 // The number of times the file system had to perform output
1740 info->oublock = r_usage.ru_oublock;
1741 // The number of times a context switch was voluntarily
1742 info->nvcsw = r_usage.ru_nvcsw;
1743 // The number of times a context switch was forced
1744 info->nivcsw = r_usage.ru_nivcsw;
1745
1746 return (status != 0);
1747}
1748
1749void __kmp_read_system_time(double *delta) {
1750 double t_ns;
1751 struct timeval tval;
1752 struct timespec stop;
1753 int status;
1754
1755 status = gettimeofday(&tval, NULL);
1756 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1757 TIMEVAL_TO_TIMESPEC(&tval, &stop);
1758 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1759 *delta = (t_ns * 1e-9);
1760}
1761
1762void __kmp_clear_system_time(void) {
1763 struct timeval tval;
1764 int status;
1765 status = gettimeofday(&tval, NULL);
1766 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1767 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1768}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001769
Jonathan Peyton30419822017-05-12 18:01:32 +00001770static int __kmp_get_xproc(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001771
Jonathan Peyton30419822017-05-12 18:01:32 +00001772 int r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001773
Kamil Rytarowskia56ac942018-12-09 16:40:33 +00001774#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +00001775 KMP_OS_OPENBSD || KMP_OS_HURD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001776
Jonathan Peyton30419822017-05-12 18:01:32 +00001777 r = sysconf(_SC_NPROCESSORS_ONLN);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001778
Jonathan Peyton30419822017-05-12 18:01:32 +00001779#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00001780
Jonathan Peyton30419822017-05-12 18:01:32 +00001781 // Bug C77011 High "OpenMP Threads and number of active cores".
Jim Cownie5e8470a2013-09-27 10:38:44 +00001782
Jonathan Peyton30419822017-05-12 18:01:32 +00001783 // Find the number of available CPUs.
1784 kern_return_t rc;
1785 host_basic_info_data_t info;
1786 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1787 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
1788 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001789 // Cannot use KA_TRACE() here because this code works before trace support
1790 // is initialized.
Jonathan Peyton30419822017-05-12 18:01:32 +00001791 r = info.avail_cpus;
1792 } else {
1793 KMP_WARNING(CantGetNumAvailCPU);
1794 KMP_INFORM(AssumedNumCPU);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001795 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001796
Jonathan Peyton30419822017-05-12 18:01:32 +00001797#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001798
Jonathan Peyton30419822017-05-12 18:01:32 +00001799#error "Unknown or unsupported OS."
Jim Cownie5e8470a2013-09-27 10:38:44 +00001800
Jonathan Peyton30419822017-05-12 18:01:32 +00001801#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001802
Jonathan Peyton30419822017-05-12 18:01:32 +00001803 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001804
1805} // __kmp_get_xproc
1806
Jonathan Peyton30419822017-05-12 18:01:32 +00001807int __kmp_read_from_file(char const *path, char const *format, ...) {
1808 int result;
1809 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001810
Jonathan Peyton30419822017-05-12 18:01:32 +00001811 va_start(args, format);
1812 FILE *f = fopen(path, "rb");
1813 if (f == NULL)
1814 return 0;
1815 result = vfscanf(f, format, args);
1816 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001817
Jonathan Peyton30419822017-05-12 18:01:32 +00001818 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001819}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001820
Jonathan Peyton30419822017-05-12 18:01:32 +00001821void __kmp_runtime_initialize(void) {
1822 int status;
1823 pthread_mutexattr_t mutex_attr;
1824 pthread_condattr_t cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001825
Jonathan Peyton30419822017-05-12 18:01:32 +00001826 if (__kmp_init_runtime) {
1827 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001828 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001829
Jonathan Peyton30419822017-05-12 18:01:32 +00001830#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1831 if (!__kmp_cpuinfo.initialized) {
1832 __kmp_query_cpuid(&__kmp_cpuinfo);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001833 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001834#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001835
Jonathan Peyton30419822017-05-12 18:01:32 +00001836 __kmp_xproc = __kmp_get_xproc();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001837
Jonathan Peyton30419822017-05-12 18:01:32 +00001838 if (sysconf(_SC_THREADS)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001839
Jonathan Peyton30419822017-05-12 18:01:32 +00001840 /* Query the maximum number of threads */
1841 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX);
1842 if (__kmp_sys_max_nth == -1) {
1843 /* Unlimited threads for NPTL */
1844 __kmp_sys_max_nth = INT_MAX;
1845 } else if (__kmp_sys_max_nth <= 1) {
1846 /* Can't tell, just use PTHREAD_THREADS_MAX */
1847 __kmp_sys_max_nth = KMP_MAX_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001848 }
1849
Jonathan Peyton30419822017-05-12 18:01:32 +00001850 /* Query the minimum stack size */
1851 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
1852 if (__kmp_sys_min_stksize <= 1) {
1853 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
1854 }
1855 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001856
Jonathan Peyton30419822017-05-12 18:01:32 +00001857 /* Set up minimum number of threads to switch to TLS gtid */
1858 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001859
Jonathan Peyton30419822017-05-12 18:01:32 +00001860 status = pthread_key_create(&__kmp_gtid_threadprivate_key,
1861 __kmp_internal_end_dest);
1862 KMP_CHECK_SYSFAIL("pthread_key_create", status);
1863 status = pthread_mutexattr_init(&mutex_attr);
1864 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1865 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
1866 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1867 status = pthread_condattr_init(&cond_attr);
1868 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1869 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
1870 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001871#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001872 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001873#endif /* USE_ITT_BUILD */
1874
Jonathan Peyton30419822017-05-12 18:01:32 +00001875 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001876}
1877
Jonathan Peyton30419822017-05-12 18:01:32 +00001878void __kmp_runtime_destroy(void) {
1879 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001880
Jonathan Peyton30419822017-05-12 18:01:32 +00001881 if (!__kmp_init_runtime) {
1882 return; // Nothing to do.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001883 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001884
1885#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001886 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001887#endif /* USE_ITT_BUILD */
1888
Jonathan Peyton30419822017-05-12 18:01:32 +00001889 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1890 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001891
Jonathan Peyton30419822017-05-12 18:01:32 +00001892 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1893 if (status != 0 && status != EBUSY) {
1894 KMP_SYSFAIL("pthread_mutex_destroy", status);
1895 }
1896 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1897 if (status != 0 && status != EBUSY) {
1898 KMP_SYSFAIL("pthread_cond_destroy", status);
1899 }
1900#if KMP_AFFINITY_SUPPORTED
1901 __kmp_affinity_uninitialize();
1902#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001903
Jonathan Peyton30419822017-05-12 18:01:32 +00001904 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001905}
1906
Jim Cownie5e8470a2013-09-27 10:38:44 +00001907/* Put the thread to sleep for a time period */
1908/* NOTE: not currently used anywhere */
Jonathan Peyton30419822017-05-12 18:01:32 +00001909void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001910
1911/* Calculate the elapsed wall clock time for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001912void __kmp_elapsed(double *t) {
1913 int status;
1914#ifdef FIX_SGI_CLOCK
1915 struct timespec ts;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001916
Jonathan Peyton30419822017-05-12 18:01:32 +00001917 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1918 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
1919 *t =
1920 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
1921#else
1922 struct timeval tv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001923
Jonathan Peyton30419822017-05-12 18:01:32 +00001924 status = gettimeofday(&tv, NULL);
1925 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1926 *t =
1927 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
1928#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001929}
1930
1931/* Calculate the elapsed wall clock tick for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001932void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001933
Jonathan Peyton377aa402016-04-14 16:00:37 +00001934/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +00001935kmp_uint64 __kmp_now_nsec() {
1936 struct timeval t;
1937 gettimeofday(&t, NULL);
Jonathan Peytonbdb0a2f2018-12-13 23:18:55 +00001938 kmp_uint64 nsec = (kmp_uint64)KMP_NSEC_PER_SEC * (kmp_uint64)t.tv_sec +
1939 (kmp_uint64)1000 * (kmp_uint64)t.tv_usec;
1940 return nsec;
Jonathan Peyton377aa402016-04-14 16:00:37 +00001941}
1942
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001943#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton35d75ae2017-03-20 22:11:31 +00001944/* Measure clock ticks per millisecond */
Jonathan Peyton30419822017-05-12 18:01:32 +00001945void __kmp_initialize_system_tick() {
Jonathan Peytonbdb0a2f2018-12-13 23:18:55 +00001946 kmp_uint64 now, nsec2, diff;
Jonathan Peyton30419822017-05-12 18:01:32 +00001947 kmp_uint64 delay = 100000; // 50~100 usec on most machines.
1948 kmp_uint64 nsec = __kmp_now_nsec();
1949 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
Jonathan Peyton30419822017-05-12 18:01:32 +00001950 while ((now = __kmp_hardware_timestamp()) < goal)
1951 ;
Jonathan Peytonbdb0a2f2018-12-13 23:18:55 +00001952 nsec2 = __kmp_now_nsec();
1953 diff = nsec2 - nsec;
1954 if (diff > 0) {
1955 kmp_uint64 tpms = (kmp_uint64)(1e6 * (delay + (now - goal)) / diff);
1956 if (tpms > 0)
1957 __kmp_ticks_per_msec = tpms;
1958 }
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001959}
1960#endif
1961
Jonathan Peyton30419822017-05-12 18:01:32 +00001962/* Determine whether the given address is mapped into the current address
1963 space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001964
Jonathan Peyton30419822017-05-12 18:01:32 +00001965int __kmp_is_address_mapped(void *addr) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001966
Jonathan Peyton30419822017-05-12 18:01:32 +00001967 int found = 0;
1968 int rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001969
Andrey Churbanov855d0982018-11-07 12:27:38 +00001970#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001971
Andrey Churbanov855d0982018-11-07 12:27:38 +00001972 /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address
Jonathan Peyton30419822017-05-12 18:01:32 +00001973 ranges mapped into the address space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001974
Jonathan Peyton30419822017-05-12 18:01:32 +00001975 char *name = __kmp_str_format("/proc/%d/maps", getpid());
1976 FILE *file = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001977
Jonathan Peyton30419822017-05-12 18:01:32 +00001978 file = fopen(name, "r");
1979 KMP_ASSERT(file != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001980
Jonathan Peyton30419822017-05-12 18:01:32 +00001981 for (;;) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001982
Jonathan Peyton30419822017-05-12 18:01:32 +00001983 void *beginning = NULL;
1984 void *ending = NULL;
1985 char perms[5];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001986
Jonathan Peyton30419822017-05-12 18:01:32 +00001987 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
1988 if (rc == EOF) {
1989 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001990 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001991 KMP_ASSERT(rc == 3 &&
1992 KMP_STRLEN(perms) == 4); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001993
Jonathan Peyton30419822017-05-12 18:01:32 +00001994 // Ending address is not included in the region, but beginning is.
1995 if ((addr >= beginning) && (addr < ending)) {
1996 perms[2] = 0; // 3th and 4th character does not matter.
1997 if (strcmp(perms, "rw") == 0) {
1998 // Memory we are looking for should be readable and writable.
Alp Toker763b9392014-02-28 09:42:41 +00001999 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002000 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002001 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002002 }
2003 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002004
Jonathan Peyton30419822017-05-12 18:01:32 +00002005 // Free resources.
2006 fclose(file);
2007 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002008
Jonathan Peyton30419822017-05-12 18:01:32 +00002009#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002010
Jonathan Peyton30419822017-05-12 18:01:32 +00002011 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
2012 using vm interface. */
2013
2014 int buffer;
2015 vm_size_t count;
2016 rc = vm_read_overwrite(
2017 mach_task_self(), // Task to read memory of.
2018 (vm_address_t)(addr), // Address to read from.
2019 1, // Number of bytes to be read.
2020 (vm_address_t)(&buffer), // Address of buffer to save read bytes in.
2021 &count // Address of var to save number of read bytes in.
2022 );
2023 if (rc == 0) {
2024 // Memory successfully read.
2025 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002026 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002027
Kamil Rytarowski316f4232018-12-11 18:35:07 +00002028#elif KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +00002029
Kamil Rytarowski316f4232018-12-11 18:35:07 +00002030 int mib[5];
2031 mib[0] = CTL_VM;
2032 mib[1] = VM_PROC;
2033 mib[2] = VM_PROC_MAP;
2034 mib[3] = getpid();
2035 mib[4] = sizeof(struct kinfo_vmentry);
2036
2037 size_t size;
2038 rc = sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0);
2039 KMP_ASSERT(!rc);
2040 KMP_ASSERT(size);
2041
2042 size = size * 4 / 3;
2043 struct kinfo_vmentry *kiv = (struct kinfo_vmentry *)KMP_INTERNAL_MALLOC(size);
2044 KMP_ASSERT(kiv);
2045
2046 rc = sysctl(mib, __arraycount(mib), kiv, &size, NULL, 0);
2047 KMP_ASSERT(!rc);
2048 KMP_ASSERT(size);
2049
2050 for (size_t i = 0; i < size; i++) {
2051 if (kiv[i].kve_start >= (uint64_t)addr &&
2052 kiv[i].kve_end <= (uint64_t)addr) {
2053 found = 1;
2054 break;
2055 }
2056 }
2057 KMP_INTERNAL_FREE(kiv);
2058#elif KMP_OS_DRAGONFLY || KMP_OS_OPENBSD
2059
2060 // FIXME(DragonFly, OpenBSD): Implement this
Jonathan Peyton30419822017-05-12 18:01:32 +00002061 found = 1;
2062
2063#else
2064
2065#error "Unknown or unsupported OS"
2066
2067#endif
2068
2069 return found;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002070
2071} // __kmp_is_address_mapped
2072
2073#ifdef USE_LOAD_BALANCE
2074
Michal Gorny70cdd832018-12-11 19:02:09 +00002075#if KMP_OS_DARWIN || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002076
2077// The function returns the rounded value of the system load average
2078// during given time interval which depends on the value of
2079// __kmp_load_balance_interval variable (default is 60 sec, other values
2080// may be 300 sec or 900 sec).
2081// It returns -1 in case of error.
Jonathan Peyton30419822017-05-12 18:01:32 +00002082int __kmp_get_load_balance(int max) {
2083 double averages[3];
2084 int ret_avg = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002085
Jonathan Peyton30419822017-05-12 18:01:32 +00002086 int res = getloadavg(averages, 3);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002087
Jonathan Peyton30419822017-05-12 18:01:32 +00002088 // Check __kmp_load_balance_interval to determine which of averages to use.
2089 // getloadavg() may return the number of samples less than requested that is
2090 // less than 3.
2091 if (__kmp_load_balance_interval < 180 && (res >= 1)) {
2092 ret_avg = averages[0]; // 1 min
2093 } else if ((__kmp_load_balance_interval >= 180 &&
2094 __kmp_load_balance_interval < 600) &&
2095 (res >= 2)) {
2096 ret_avg = averages[1]; // 5 min
2097 } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
2098 ret_avg = averages[2]; // 15 min
2099 } else { // Error occurred
2100 return -1;
2101 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002102
Jonathan Peyton30419822017-05-12 18:01:32 +00002103 return ret_avg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002104}
2105
Jonathan Peyton30419822017-05-12 18:01:32 +00002106#else // Linux* OS
Jim Cownie5e8470a2013-09-27 10:38:44 +00002107
Jonathan Peyton30419822017-05-12 18:01:32 +00002108// The fuction returns number of running (not sleeping) threads, or -1 in case
2109// of error. Error could be reported if Linux* OS kernel too old (without
2110// "/proc" support). Counting running threads stops if max running threads
2111// encountered.
2112int __kmp_get_load_balance(int max) {
2113 static int permanent_error = 0;
2114 static int glb_running_threads = 0; // Saved count of the running threads for
2115 // the thread balance algortihm
2116 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002117
Jonathan Peyton30419822017-05-12 18:01:32 +00002118 int running_threads = 0; // Number of running threads in the system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002119
Jonathan Peyton30419822017-05-12 18:01:32 +00002120 DIR *proc_dir = NULL; // Handle of "/proc/" directory.
2121 struct dirent *proc_entry = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002122
Jonathan Peyton30419822017-05-12 18:01:32 +00002123 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2124 DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2125 struct dirent *task_entry = NULL;
2126 int task_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002127
Jonathan Peyton30419822017-05-12 18:01:32 +00002128 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2129 int stat_file = -1;
2130 int stat_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002131
Jonathan Peyton30419822017-05-12 18:01:32 +00002132 int total_processes = 0; // Total number of processes in system.
2133 int total_threads = 0; // Total number of threads in system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002134
Jonathan Peyton30419822017-05-12 18:01:32 +00002135 double call_time = 0.0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002136
Jonathan Peyton30419822017-05-12 18:01:32 +00002137 __kmp_str_buf_init(&task_path);
2138 __kmp_str_buf_init(&stat_path);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002139
Jonathan Peyton30419822017-05-12 18:01:32 +00002140 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002141
Jonathan Peyton30419822017-05-12 18:01:32 +00002142 if (glb_call_time &&
2143 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2144 running_threads = glb_running_threads;
2145 goto finish;
2146 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002147
Jonathan Peyton30419822017-05-12 18:01:32 +00002148 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002149
Jonathan Peyton30419822017-05-12 18:01:32 +00002150 // Do not spend time on scanning "/proc/" if we have a permanent error.
2151 if (permanent_error) {
2152 running_threads = -1;
2153 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002154 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002155
Jonathan Peyton30419822017-05-12 18:01:32 +00002156 if (max <= 0) {
2157 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002158 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002159
Jonathan Peyton30419822017-05-12 18:01:32 +00002160 // Open "/proc/" directory.
2161 proc_dir = opendir("/proc");
2162 if (proc_dir == NULL) {
2163 // Cannot open "/prroc/". Probably the kernel does not support it. Return an
2164 // error now and in subsequent calls.
2165 running_threads = -1;
2166 permanent_error = 1;
2167 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002168 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002169
Jonathan Peyton30419822017-05-12 18:01:32 +00002170 // Initialize fixed part of task_path. This part will not change.
2171 __kmp_str_buf_cat(&task_path, "/proc/", 6);
2172 task_path_fixed_len = task_path.used; // Remember number of used characters.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002173
Jonathan Peyton30419822017-05-12 18:01:32 +00002174 proc_entry = readdir(proc_dir);
2175 while (proc_entry != NULL) {
2176 // Proc entry is a directory and name starts with a digit. Assume it is a
2177 // process' directory.
2178 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002179
Jonathan Peyton30419822017-05-12 18:01:32 +00002180 ++total_processes;
2181 // Make sure init process is the very first in "/proc", so we can replace
2182 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
2183 // 1. We are going to check that total_processes == 1 => d_name == "1" is
2184 // true (where "=>" is implication). Since C++ does not have => operator,
2185 // let us replace it with its equivalent: a => b == ! a || b.
2186 KMP_DEBUG_ASSERT(total_processes != 1 ||
2187 strcmp(proc_entry->d_name, "1") == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002188
Jonathan Peyton30419822017-05-12 18:01:32 +00002189 // Construct task_path.
2190 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2191 __kmp_str_buf_cat(&task_path, proc_entry->d_name,
2192 KMP_STRLEN(proc_entry->d_name));
2193 __kmp_str_buf_cat(&task_path, "/task", 5);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002194
Jonathan Peyton30419822017-05-12 18:01:32 +00002195 task_dir = opendir(task_path.str);
2196 if (task_dir == NULL) {
2197 // Process can finish between reading "/proc/" directory entry and
2198 // opening process' "task/" directory. So, in general case we should not
2199 // complain, but have to skip this process and read the next one. But on
2200 // systems with no "task/" support we will spend lot of time to scan
2201 // "/proc/" tree again and again without any benefit. "init" process
2202 // (its pid is 1) should exist always, so, if we cannot open
2203 // "/proc/1/task/" directory, it means "task/" is not supported by
2204 // kernel. Report an error now and in the future.
2205 if (strcmp(proc_entry->d_name, "1") == 0) {
2206 running_threads = -1;
2207 permanent_error = 1;
2208 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002209 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002210 } else {
2211 // Construct fixed part of stat file path.
2212 __kmp_str_buf_clear(&stat_path);
2213 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
2214 __kmp_str_buf_cat(&stat_path, "/", 1);
2215 stat_path_fixed_len = stat_path.used;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002216
Jonathan Peyton30419822017-05-12 18:01:32 +00002217 task_entry = readdir(task_dir);
2218 while (task_entry != NULL) {
2219 // It is a directory and name starts with a digit.
2220 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
2221 ++total_threads;
2222
2223 // Consruct complete stat file path. Easiest way would be:
2224 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
2225 // task_entry->d_name );
2226 // but seriae of __kmp_str_buf_cat works a bit faster.
2227 stat_path.used =
2228 stat_path_fixed_len; // Reset stat path to its fixed part.
2229 __kmp_str_buf_cat(&stat_path, task_entry->d_name,
2230 KMP_STRLEN(task_entry->d_name));
2231 __kmp_str_buf_cat(&stat_path, "/stat", 5);
2232
2233 // Note: Low-level API (open/read/close) is used. High-level API
2234 // (fopen/fclose) works ~ 30 % slower.
2235 stat_file = open(stat_path.str, O_RDONLY);
2236 if (stat_file == -1) {
2237 // We cannot report an error because task (thread) can terminate
2238 // just before reading this file.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002239 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002240 /* Content of "stat" file looks like:
2241 24285 (program) S ...
Jim Cownie5e8470a2013-09-27 10:38:44 +00002242
Jonathan Peyton30419822017-05-12 18:01:32 +00002243 It is a single line (if program name does not include funny
2244 symbols). First number is a thread id, then name of executable
2245 file name in paretheses, then state of the thread. We need just
2246 thread state.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002247
Jonathan Peyton30419822017-05-12 18:01:32 +00002248 Good news: Length of program name is 15 characters max. Longer
2249 names are truncated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002250
Jonathan Peyton30419822017-05-12 18:01:32 +00002251 Thus, we need rather short buffer: 15 chars for program name +
2252 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002253
Jonathan Peyton30419822017-05-12 18:01:32 +00002254 Bad news: Program name may contain special symbols like space,
2255 closing parenthesis, or even new line. This makes parsing
2256 "stat" file not 100 % reliable. In case of fanny program names
2257 parsing may fail (report incorrect thread state).
Jim Cownie5e8470a2013-09-27 10:38:44 +00002258
Jonathan Peyton30419822017-05-12 18:01:32 +00002259 Parsing "status" file looks more promissing (due to different
2260 file structure and escaping special symbols) but reading and
2261 parsing of "status" file works slower.
2262 -- ln
2263 */
2264 char buffer[65];
2265 int len;
2266 len = read(stat_file, buffer, sizeof(buffer) - 1);
2267 if (len >= 0) {
2268 buffer[len] = 0;
2269 // Using scanf:
2270 // sscanf( buffer, "%*d (%*s) %c ", & state );
2271 // looks very nice, but searching for a closing parenthesis
2272 // works a bit faster.
2273 char *close_parent = strstr(buffer, ") ");
2274 if (close_parent != NULL) {
2275 char state = *(close_parent + 2);
2276 if (state == 'R') {
2277 ++running_threads;
2278 if (running_threads >= max) {
2279 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002280 }
2281 }
2282 }
2283 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002284 close(stat_file);
2285 stat_file = -1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002286 }
2287 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002288 task_entry = readdir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002289 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002290 closedir(task_dir);
2291 task_dir = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002292 }
2293 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002294 proc_entry = readdir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002295 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002296
Jonathan Peyton30419822017-05-12 18:01:32 +00002297 // There _might_ be a timing hole where the thread executing this
2298 // code get skipped in the load balance, and running_threads is 0.
2299 // Assert in the debug builds only!!!
2300 KMP_DEBUG_ASSERT(running_threads > 0);
2301 if (running_threads <= 0) {
2302 running_threads = 1;
2303 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002304
Jonathan Peyton30419822017-05-12 18:01:32 +00002305finish: // Clean up and exit.
2306 if (proc_dir != NULL) {
2307 closedir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002308 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002309 __kmp_str_buf_free(&task_path);
2310 if (task_dir != NULL) {
2311 closedir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002312 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002313 __kmp_str_buf_free(&stat_path);
2314 if (stat_file != -1) {
2315 close(stat_file);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002316 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002317
Jonathan Peyton30419822017-05-12 18:01:32 +00002318 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002319
Jonathan Peyton30419822017-05-12 18:01:32 +00002320 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002321
2322} // __kmp_get_load_balance
2323
Jonathan Peyton30419822017-05-12 18:01:32 +00002324#endif // KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002325
2326#endif // USE_LOAD_BALANCE
2327
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00002328#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
2329 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002330
2331// we really only need the case with 1 argument, because CLANG always build
2332// a struct of pointers to shared variables referenced in the outlined function
Jonathan Peyton30419822017-05-12 18:01:32 +00002333int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
2334 void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002335#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002336 ,
2337 void **exit_frame_ptr
Jonathan Peyton122dd762015-07-13 18:55:45 +00002338#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002339 ) {
Jonathan Peyton122dd762015-07-13 18:55:45 +00002340#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002341 *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0);
Jonathan Peyton122dd762015-07-13 18:55:45 +00002342#endif
2343
Jim Cownie3051f972014-08-07 10:12:54 +00002344 switch (argc) {
2345 default:
2346 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2347 fflush(stderr);
2348 exit(-1);
2349 case 0:
2350 (*pkfn)(&gtid, &tid);
2351 break;
2352 case 1:
2353 (*pkfn)(&gtid, &tid, p_argv[0]);
2354 break;
2355 case 2:
2356 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2357 break;
2358 case 3:
2359 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2360 break;
2361 case 4:
2362 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2363 break;
2364 case 5:
2365 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2366 break;
2367 case 6:
2368 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2369 p_argv[5]);
2370 break;
2371 case 7:
2372 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2373 p_argv[5], p_argv[6]);
2374 break;
2375 case 8:
2376 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2377 p_argv[5], p_argv[6], p_argv[7]);
2378 break;
2379 case 9:
2380 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2381 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2382 break;
2383 case 10:
2384 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2385 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2386 break;
2387 case 11:
2388 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2389 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2390 break;
2391 case 12:
2392 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2393 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2394 p_argv[11]);
2395 break;
2396 case 13:
2397 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2398 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2399 p_argv[11], p_argv[12]);
2400 break;
2401 case 14:
2402 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2403 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2404 p_argv[11], p_argv[12], p_argv[13]);
2405 break;
2406 case 15:
2407 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2408 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2409 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2410 break;
2411 }
2412
Jonathan Peyton122dd762015-07-13 18:55:45 +00002413#if OMPT_SUPPORT
2414 *exit_frame_ptr = 0;
2415#endif
2416
Jim Cownie3051f972014-08-07 10:12:54 +00002417 return 1;
2418}
2419
2420#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002421
Jim Cownie5e8470a2013-09-27 10:38:44 +00002422// end of file //