2 * z_Linux_util.cpp -- platform specific routines.
5 //===----------------------------------------------------------------------===//
7 // The LLVM Compiler Infrastructure
9 // This file is dual licensed under the MIT and the University of Illinois Open
10 // Source Licenses. See LICENSE.txt for details.
12 //===----------------------------------------------------------------------===//
15 #include "kmp_affinity.h"
20 #include "kmp_stats.h"
22 #include "kmp_wait_release.h"
23 #include "kmp_wrapper_getpid.h"
25 #if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD
28 #include <math.h> // HUGE_VAL.
29 #include <sys/resource.h>
30 #include <sys/syscall.h>
32 #include <sys/times.h>
35 #if KMP_OS_LINUX && !KMP_OS_CNK
36 #include <sys/sysinfo.h>
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>
51 #include <mach/mach.h>
52 #include <sys/sysctl.h>
53 #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
54 #include <pthread_np.h>
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
64 #include "tsan_annotations.h"
66 struct kmp_sys_timer {
67 struct timespec start;
70 // Convert timespec to nanoseconds.
71 #define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
73 static struct kmp_sys_timer __kmp_sys_timer_data;
75 #if KMP_HANDLE_SIGNALS
76 typedef void (*sig_func_t)(int);
77 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG];
78 static sigset_t __kmp_sigset;
81 static int __kmp_init_runtime = FALSE;
83 static int __kmp_fork_count = 0;
85 static pthread_condattr_t __kmp_suspend_cond_attr;
86 static pthread_mutexattr_t __kmp_suspend_mutex_attr;
88 static kmp_cond_align_t __kmp_wait_cv;
89 static kmp_mutex_align_t __kmp_wait_mx;
91 kmp_uint64 __kmp_ticks_per_msec = 1000000;
94 static 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);
101 #if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
103 /* Affinity support */
105 void __kmp_affinity_bind_thread(int which) {
106 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
107 "Illegal set affinity operation when not capable");
109 kmp_affin_mask_t *mask;
110 KMP_CPU_ALLOC_ON_STACK(mask);
112 KMP_CPU_SET(which, mask);
113 __kmp_set_system_affinity(mask, TRUE);
114 KMP_CPU_FREE_FROM_STACK(mask);
117 /* Determine if we can access affinity functionality on this version of
118 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
119 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
120 void __kmp_affinity_determine_capable(const char *env_var) {
121 // Check and see if the OS supports thread affinity.
123 #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
128 buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
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",
138 // if ((gCode < 0) && (errno == ENOSYS))
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))) {
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);
153 KMP_AFFINITY_DISABLE();
154 KMP_INTERNAL_FREE(buf);
157 if (gCode > 0) { // Linux* OS only
158 // The optimal situation: the OS returns the size of the buffer it expects.
160 // 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));
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))) {
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);
181 KMP_AFFINITY_DISABLE();
182 KMP_INTERNAL_FREE(buf);
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);
195 // 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"));
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));
207 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 "
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))) {
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);
226 KMP_AFFINITY_DISABLE();
227 KMP_INTERNAL_FREE(buf);
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));
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 "
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))) {
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);
257 KMP_AFFINITY_DISABLE();
258 KMP_INTERNAL_FREE(buf);
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);
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
277 // 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);
289 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
293 int __kmp_futex_determine_capable() {
295 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
296 int retval = (rc == 0) || (errno != ENOSYS);
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"));
306 #endif // KMP_USE_FUTEX
308 #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 */
312 kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
313 kmp_int8 old_value, new_value;
315 old_value = TCR_1(*p);
316 new_value = old_value | d;
318 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
320 old_value = TCR_1(*p);
321 new_value = old_value | d;
326 kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
327 kmp_int8 old_value, new_value;
329 old_value = TCR_1(*p);
330 new_value = old_value & d;
332 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
334 old_value = TCR_1(*p);
335 new_value = old_value & d;
340 kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) {
341 kmp_uint32 old_value, new_value;
343 old_value = TCR_4(*p);
344 new_value = old_value | d;
346 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
348 old_value = TCR_4(*p);
349 new_value = old_value | d;
354 kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) {
355 kmp_uint32 old_value, new_value;
357 old_value = TCR_4(*p);
358 new_value = old_value & d;
360 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
362 old_value = TCR_4(*p);
363 new_value = old_value & d;
369 kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
370 kmp_int8 old_value, new_value;
372 old_value = TCR_1(*p);
373 new_value = old_value + d;
375 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
377 old_value = TCR_1(*p);
378 new_value = old_value + d;
383 kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
384 kmp_int64 old_value, new_value;
386 old_value = TCR_8(*p);
387 new_value = old_value + d;
389 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
391 old_value = TCR_8(*p);
392 new_value = old_value + d;
396 #endif /* KMP_ARCH_X86 */
398 kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) {
399 kmp_uint64 old_value, new_value;
401 old_value = TCR_8(*p);
402 new_value = old_value | d;
403 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
405 old_value = TCR_8(*p);
406 new_value = old_value | d;
411 kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) {
412 kmp_uint64 old_value, new_value;
414 old_value = TCR_8(*p);
415 new_value = old_value & d;
416 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
418 old_value = TCR_8(*p);
419 new_value = old_value & d;
424 #endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
426 void __kmp_terminate_thread(int gtid) {
428 kmp_info_t *th = __kmp_threads[gtid];
433 #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) {
437 __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
444 /* 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. */
448 static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
450 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
457 /* 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)) {
462 /* Fetch the real thread attributes */
463 status = pthread_attr_init(&attr);
464 KMP_CHECK_SYSFAIL("pthread_attr_init", status);
465 #if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD
466 status = pthread_attr_get_np(pthread_self(), &attr);
467 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status);
469 status = pthread_getattr_np(pthread_self(), &attr);
470 KMP_CHECK_SYSFAIL("pthread_getattr_np", status);
472 status = pthread_attr_getstack(&attr, &addr, &size);
473 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status);
475 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
476 " %lu, low addr: %p\n",
478 status = pthread_attr_destroy(&attr);
479 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
482 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);
489 #endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD ||
491 /* 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);
498 static void *__kmp_launch_worker(void *thr) {
499 int status, old_type, old_state;
500 #ifdef KMP_BLOCK_SIGNALS
501 sigset_t new_set, old_set;
502 #endif /* KMP_BLOCK_SIGNALS */
504 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
505 KMP_OS_OPENBSD || KMP_OS_HURD
506 void *volatile padding = 0;
510 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
511 __kmp_gtid_set_specific(gtid);
512 #ifdef KMP_TDATA_GTID
515 #if KMP_STATS_ENABLED
516 // set thread local index to point to thread-specific stats
517 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
518 __kmp_stats_thread_ptr->startLife();
519 KMP_SET_THREAD_STATE(IDLE);
520 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
524 __kmp_itt_thread_name(gtid);
525 #endif /* USE_ITT_BUILD */
527 #if KMP_AFFINITY_SUPPORTED
528 __kmp_affinity_set_init_mask(gtid, FALSE);
531 #ifdef KMP_CANCEL_THREADS
532 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);
539 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
540 // 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);
544 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
546 #ifdef KMP_BLOCK_SIGNALS
547 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);
551 #endif /* KMP_BLOCK_SIGNALS */
553 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
555 if (__kmp_stkoffset > 0 && gtid > 0) {
556 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
561 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
563 __kmp_check_stack_overlap((kmp_info_t *)thr);
565 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
567 #ifdef KMP_BLOCK_SIGNALS
568 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
569 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
570 #endif /* KMP_BLOCK_SIGNALS */
576 /* The monitor thread controls all of the threads in the complex */
578 static void *__kmp_launch_monitor(void *thr) {
579 int status, old_type, old_state;
580 #ifdef KMP_BLOCK_SIGNALS
582 #endif /* KMP_BLOCK_SIGNALS */
583 struct timespec interval;
585 int yield_cycles = 0;
587 KMP_MB(); /* Flush all pending memory write invalidates. */
589 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
591 /* register us as the monitor thread */
592 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
593 #ifdef KMP_TDATA_GTID
594 __kmp_gtid = KMP_GTID_MONITOR;
600 // Instruct Intel(R) Threading Tools to ignore monitor thread.
601 __kmp_itt_thread_ignore();
602 #endif /* USE_ITT_BUILD */
604 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
607 __kmp_check_stack_overlap((kmp_info_t *)thr);
609 #ifdef KMP_CANCEL_THREADS
610 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);
617 #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);
629 KMP_WARNING(RealTimeSchedNotSupported);
630 sched_getparam(0, ¶m);
631 if (param.sched_priority < max_priority) {
632 param.sched_priority += 1;
633 rc = sched_setscheduler(0, sched, ¶m);
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);
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),
652 // AC: free thread that waits for monitor started
653 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
655 #endif // KMP_REAL_TIME_FIX
657 KMP_MB(); /* Flush all pending memory write invalidates. */
659 if (__kmp_monitor_wakeups == 1) {
661 interval.tv_nsec = 0;
664 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
667 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
669 if (__kmp_yield_cycle) {
670 __kmp_yielding_on = 0; /* Start out with yielding shut off */
671 yield_count = __kmp_yield_off_count;
673 __kmp_yielding_on = 1; /* Yielding is on permanently */
676 while (!TCR_4(__kmp_global.g.g_done)) {
680 /* This thread monitors the state of the system */
682 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
684 status = gettimeofday(&tval, NULL);
685 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
686 TIMEVAL_TO_TIMESPEC(&tval, &now);
688 now.tv_sec += interval.tv_sec;
689 now.tv_nsec += interval.tv_nsec;
691 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
693 now.tv_nsec -= KMP_NSEC_PER_SEC;
696 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);
703 if (status != ETIMEDOUT && status != EINTR) {
704 KMP_SYSFAIL("pthread_cond_timedwait", status);
708 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
709 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
711 if (__kmp_yield_cycle) {
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;
718 __kmp_yielding_on = 1; /* Turn it on now */
719 yield_count = __kmp_yield_on_count;
724 __kmp_yielding_on = 1;
727 TCW_4(__kmp_global.g.g_time.dt.t_value,
728 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
730 KMP_MB(); /* Flush all pending memory write invalidates. */
733 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
735 #ifdef KMP_BLOCK_SIGNALS
736 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);
740 #endif /* KMP_BLOCK_SIGNALS */
742 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
744 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 */
750 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
751 __kmp_global.g.g_abort));
753 /* 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);
761 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
762 __kmp_global.g.g_abort));
764 if (__kmp_global.g.g_abort > 0)
765 raise(__kmp_global.g.g_abort);
768 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
772 #endif // KMP_USE_MONITOR
774 void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
776 pthread_attr_t thread_attr;
779 th->th.th_info.ds.ds_gtid = gtid;
781 #if KMP_STATS_ENABLED
782 // sets up worker thread stats
783 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
785 // th->th.th_stats is used to transfer thread-specific stats-pointer to
786 // __kmp_launch_worker. So when thread is created (goes into
787 // __kmp_launch_worker) it will set its thread local pointer to
789 if (!KMP_UBER_GTID(gtid)) {
790 th->th.th_stats = __kmp_stats_list->push_back(gtid);
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;
796 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
798 #endif // KMP_STATS_ENABLED
800 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);
808 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
810 KMP_MB(); /* Flush all pending memory write invalidates. */
812 #ifdef KMP_THREAD_ATTR
813 status = pthread_attr_init(&thread_attr);
815 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
817 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
819 __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null);
822 /* 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;
831 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));
835 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
836 status = pthread_attr_setstacksize(&thread_attr, stack_size);
837 #ifdef KMP_BACKUP_STKSIZE
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 "
845 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
846 status = pthread_attr_setstacksize(&thread_attr, stack_size);
849 #endif /* KMP_BACKUP_STKSIZE */
851 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
852 KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
854 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
856 #endif /* KMP_THREAD_ATTR */
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) {
863 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
864 KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null);
866 if (status == ENOMEM) {
867 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
868 KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null);
870 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
871 if (status == EAGAIN) {
872 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status),
873 KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
875 KMP_SYSFAIL("pthread_create", status);
878 th->th.th_info.ds.ds_thread = handle;
880 #ifdef KMP_THREAD_ATTR
881 status = pthread_attr_destroy(&thread_attr);
883 kmp_msg_t err_code = KMP_ERR(status);
884 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
886 if (__kmp_generate_warnings == kmp_warnings_off) {
887 __kmp_str_free(&err_code.str);
890 #endif /* KMP_THREAD_ATTR */
892 KMP_MB(); /* Flush all pending memory write invalidates. */
894 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
896 } // __kmp_create_worker
899 void __kmp_create_monitor(kmp_info_t *th) {
901 pthread_attr_t thread_attr;
904 int auto_adj_size = FALSE;
906 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 "
910 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
911 th->th.th_info.ds.ds_gtid = 0;
914 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
916 KMP_MB(); /* Flush all pending memory write invalidates. */
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.
924 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
925 #endif // KMP_REAL_TIME_FIX
927 #ifdef KMP_THREAD_ATTR
928 if (__kmp_monitor_stksize == 0) {
929 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
930 auto_adj_size = TRUE;
932 status = pthread_attr_init(&thread_attr);
934 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
936 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
938 __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null);
941 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
942 status = pthread_attr_getstacksize(&thread_attr, &size);
943 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
945 size = __kmp_sys_min_stksize;
946 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
947 #endif /* KMP_THREAD_ATTR */
949 if (__kmp_monitor_stksize == 0) {
950 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
952 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
953 __kmp_monitor_stksize = __kmp_sys_min_stksize;
956 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
957 "requested stacksize = %lu bytes\n",
958 size, __kmp_monitor_stksize));
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);
969 __kmp_monitor_stksize *= 2;
972 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);
980 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
983 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
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;
992 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
993 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize),
996 if (status == ENOMEM) {
997 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
998 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize),
1001 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1002 if (status == EAGAIN) {
1003 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status),
1004 KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null);
1006 KMP_SYSFAIL("pthread_create", status);
1009 th->th.th_info.ds.ds_thread = handle;
1011 #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
1019 #ifdef KMP_THREAD_ATTR
1020 status = pthread_attr_destroy(&thread_attr);
1022 kmp_msg_t err_code = KMP_ERR(status);
1023 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1025 if (__kmp_generate_warnings == kmp_warnings_off) {
1026 __kmp_str_free(&err_code.str);
1031 KMP_MB(); /* Flush all pending memory write invalidates. */
1033 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
1034 th->th.th_info.ds.ds_thread));
1036 } // __kmp_create_monitor
1037 #endif // KMP_USE_MONITOR
1039 void __kmp_exit_thread(int exit_status) {
1040 pthread_exit((void *)(intptr_t)exit_status);
1041 } // __kmp_exit_thread
1044 void __kmp_resume_monitor();
1046 void __kmp_reap_monitor(kmp_info_t *th) {
1050 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1052 th->th.th_info.ds.ds_thread));
1054 // 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"));
1063 KMP_MB(); /* Flush all pending memory write invalidates. */
1065 /* 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 */
1069 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1070 if (status != ESRCH) {
1071 __kmp_resume_monitor(); // Wake up the monitor thread
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) {
1076 __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null);
1079 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1080 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1082 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1084 th->th.th_info.ds.ds_thread));
1086 KMP_MB(); /* Flush all pending memory write invalidates. */
1088 #endif // KMP_USE_MONITOR
1090 void __kmp_reap_worker(kmp_info_t *th) {
1094 KMP_MB(); /* Flush all pending memory write invalidates. */
1097 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
1099 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1101 /* Don't expose these to the user until we understand when they trigger */
1103 __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null);
1105 if (exit_val != th) {
1106 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1108 th->th.th_info.ds.ds_gtid, exit_val));
1110 #endif /* KMP_DEBUG */
1112 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1113 th->th.th_info.ds.ds_gtid));
1115 KMP_MB(); /* Flush all pending memory write invalidates. */
1118 #if KMP_HANDLE_SIGNALS
1120 static void __kmp_null_handler(int signo) {
1121 // Do nothing, for doing SIG_IGN-type actions.
1122 } // __kmp_null_handler
1124 static 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 */
1128 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
1143 if (__kmp_debug_buf) {
1144 __kmp_dump_debug_buffer();
1146 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.
1154 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1159 } // __kmp_team_handler
1161 static 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);
1167 static 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.
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);
1182 // Restore/keep user's handler if one previously installed.
1183 __kmp_sigaction(sig, &old_action, NULL);
1186 // Save initial/system signal handlers to see if user handlers installed.
1187 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
1189 KMP_MB(); // Flush all pending memory write invalidates.
1190 } // __kmp_install_one_handler
1192 static 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",
1204 __kmp_sigaction(sig, &old, NULL);
1206 sigdelset(&__kmp_sigset, sig);
1207 KMP_MB(); // Flush all pending memory write invalidates.
1209 } // __kmp_remove_one_handler
1211 void __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);
1226 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1228 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1230 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1233 } // __kmp_install_signals
1235 void __kmp_remove_signals(void) {
1237 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1238 for (sig = 1; sig < NSIG; ++sig) {
1239 __kmp_remove_one_handler(sig);
1241 } // __kmp_remove_signals
1243 #endif // KMP_HANDLE_SIGNALS
1245 void __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);
1254 void __kmp_disable(int *old_state) {
1255 #ifdef KMP_CANCEL_THREADS
1257 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1258 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1262 static void __kmp_atfork_prepare(void) {
1263 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
1264 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
1267 static void __kmp_atfork_parent(void) {
1268 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
1269 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1272 /* 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. */
1275 static void __kmp_atfork_child(void) {
1276 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1277 /* 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 ); */
1283 #if KMP_AFFINITY_SUPPORTED
1285 // reset the affinity in the child to the initial thread
1286 // affinity in the parent
1287 kmp_set_thread_affinity_mask_initial();
1289 // 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;
1294 if (__kmp_nested_proc_bind.bind_types != NULL) {
1295 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
1297 #endif // OMP_40_ENABLED
1298 #endif // KMP_AFFINITY_SUPPORTED
1300 __kmp_init_runtime = FALSE;
1302 __kmp_init_monitor = 0;
1304 __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;
1310 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;
1319 TCW_4(__kmp_nth, 0);
1321 __kmp_thread_pool = NULL;
1322 __kmp_thread_pool_insert_pt = NULL;
1323 __kmp_team_pool = NULL;
1325 /* 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));
1330 while (__kmp_threadpriv_cache_list != NULL) {
1332 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)));
1336 *__kmp_threadpriv_cache_list->addr = NULL;
1338 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
1341 __kmp_init_runtime = FALSE;
1343 /* 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);
1347 __kmp_init_bootstrap_lock(&__kmp_task_team_lock);
1350 __kmp_itt_reset(); // reset ITT's global state
1351 #endif /* USE_ITT_BUILD */
1353 /* 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 ();
1364 void __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;
1373 void __kmp_suspend_initialize(void) {
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);
1381 static 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 */
1387 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);
1398 void __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 */
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);
1408 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
1409 if (status != 0 && status != EBUSY) {
1410 KMP_SYSFAIL("pthread_mutex_destroy", status);
1412 --th->th.th_suspend_init_count;
1413 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1417 /* This routine puts the calling thread to sleep after setting the
1418 sleep bit for the indicated flag variable to true. */
1420 static 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];
1424 typename C::flag_t old_spin;
1426 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1429 __kmp_suspend_initialize_thread(th);
1431 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1432 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
1434 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1435 th_gtid, flag->get()));
1437 /* TODO: shouldn't this use release semantics to ensure that
1438 __kmp_suspend_initialize_thread gets called first? */
1439 old_spin = flag->set_sleeping();
1441 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1443 th_gtid, flag->get(), flag->load(), old_spin));
1445 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 "
1449 th_gtid, flag->get()));
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);
1457 while (flag->is_sleeping()) {
1458 #ifdef DEBUG_SUSPEND
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,
1465 // Mark the thread as no longer active (only in the first iteration of the
1468 th->th.th_active = FALSE;
1469 if (th->th.th_active_in_pool) {
1470 th->th.th_active_in_pool = FALSE;
1471 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
1472 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1477 #if USE_SUSPEND_TIMEOUT
1478 struct timespec now;
1479 struct timeval tval;
1482 status = gettimeofday(&tval, NULL);
1483 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1484 TIMEVAL_TO_TIMESPEC(&tval, &now);
1486 msecs = (4 * __kmp_dflt_blocktime) + 200;
1487 now.tv_sec += msecs / 1000;
1488 now.tv_nsec += (msecs % 1000) * 1000;
1490 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1491 "pthread_cond_timedwait\n",
1493 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1494 &th->th.th_suspend_mx.m_mutex, &now);
1496 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1497 " pthread_cond_wait\n",
1499 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1500 &th->th.th_suspend_mx.m_mutex);
1503 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1504 KMP_SYSFAIL("pthread_cond_wait", status);
1507 if (status == ETIMEDOUT) {
1508 if (flag->is_sleeping()) {
1510 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1512 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1516 } else if (flag->is_sleeping()) {
1518 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1523 // Mark the thread as active again (if it was previous marked as inactive)
1525 th->th.th_active = TRUE;
1526 if (TCR_4(th->th.th_in_pool)) {
1527 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
1528 th->th.th_active_in_pool = TRUE;
1532 #ifdef DEBUG_SUSPEND
1535 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1536 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1541 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));
1546 void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1547 __kmp_suspend_template(th_gtid, flag);
1549 void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1550 __kmp_suspend_template(th_gtid, flag);
1552 void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1553 __kmp_suspend_template(th_gtid, flag);
1556 /* This routine signals the thread specified by target_gtid to wake up
1557 after setting the sleep bit indicated by the flag argument to FALSE.
1558 The target thread must already have called __kmp_suspend_template() */
1560 static 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];
1566 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1569 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);
1573 __kmp_suspend_initialize_thread(th);
1575 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1576 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
1578 if (!flag) { // coming from __kmp_null_resume_wrapper
1579 flag = (C *)CCAST(void *, th->th.th_sleep_loc);
1582 // First, check if the flag is null or its type has changed. If so, someone
1584 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
1585 // simply shows what
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);
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 "
1600 gtid, target_gtid, flag->get(), old_spin, flag->load()));
1601 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1602 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1605 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1606 "sleep bit for flag's loc(%p): "
1608 gtid, target_gtid, flag->get(), old_spin, flag->load()));
1610 TCW_PTR(th->th.th_sleep_loc, NULL);
1612 #ifdef DEBUG_SUSPEND
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);
1620 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"
1626 gtid, target_gtid));
1629 void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1630 __kmp_resume_template(target_gtid, flag);
1632 void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1633 __kmp_resume_template(target_gtid, flag);
1635 void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1636 __kmp_resume_template(target_gtid, flag);
1640 void __kmp_resume_monitor() {
1641 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1644 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,
1647 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
1649 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1650 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
1651 #ifdef DEBUG_SUSPEND
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);
1659 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"
1665 gtid, KMP_GTID_MONITOR));
1667 #endif // KMP_USE_MONITOR
1669 void __kmp_yield(int cond) {
1673 if (!__kmp_yielding_on)
1676 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1682 void __kmp_gtid_set_specific(int gtid) {
1683 if (__kmp_init_gtid) {
1685 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1686 (void *)(intptr_t)(gtid + 1));
1687 KMP_CHECK_SYSFAIL("pthread_setspecific", status);
1689 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1693 int __kmp_gtid_get_specific() {
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;
1700 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1702 gtid = KMP_GTID_DNE;
1706 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1707 __kmp_gtid_threadprivate_key, gtid));
1711 double __kmp_read_cpu_time(void) {
1715 /*t =*/times(&buffer);
1717 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
1720 int __kmp_read_system_info(struct kmp_sys_info *info) {
1722 struct rusage r_usage;
1724 memset(info, 0, sizeof(*info));
1726 status = getrusage(RUSAGE_SELF, &r_usage);
1727 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
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;
1746 return (status != 0);
1749 void __kmp_read_system_time(double *delta) {
1751 struct timeval tval;
1752 struct timespec stop;
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);
1762 void __kmp_clear_system_time(void) {
1763 struct timeval tval;
1765 status = gettimeofday(&tval, NULL);
1766 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1767 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1770 static int __kmp_get_xproc(void) {
1774 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
1775 KMP_OS_OPENBSD || KMP_OS_HURD
1777 r = sysconf(_SC_NPROCESSORS_ONLN);
1781 // Bug C77011 High "OpenMP Threads and number of active cores".
1783 // Find the number of available CPUs.
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) {
1789 // Cannot use KA_TRACE() here because this code works before trace support
1791 r = info.avail_cpus;
1793 KMP_WARNING(CantGetNumAvailCPU);
1794 KMP_INFORM(AssumedNumCPU);
1799 #error "Unknown or unsupported OS."
1803 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
1805 } // __kmp_get_xproc
1807 int __kmp_read_from_file(char const *path, char const *format, ...) {
1811 va_start(args, format);
1812 FILE *f = fopen(path, "rb");
1815 result = vfscanf(f, format, args);
1821 void __kmp_runtime_initialize(void) {
1823 pthread_mutexattr_t mutex_attr;
1824 pthread_condattr_t cond_attr;
1826 if (__kmp_init_runtime) {
1830 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1831 if (!__kmp_cpuinfo.initialized) {
1832 __kmp_query_cpuid(&__kmp_cpuinfo);
1834 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1836 __kmp_xproc = __kmp_get_xproc();
1838 if (sysconf(_SC_THREADS)) {
1840 /* 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;
1850 /* 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;
1857 /* Set up minimum number of threads to switch to TLS gtid */
1858 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
1860 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);
1872 __kmp_itt_initialize();
1873 #endif /* USE_ITT_BUILD */
1875 __kmp_init_runtime = TRUE;
1878 void __kmp_runtime_destroy(void) {
1881 if (!__kmp_init_runtime) {
1882 return; // Nothing to do.
1886 __kmp_itt_destroy();
1887 #endif /* USE_ITT_BUILD */
1889 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1890 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
1892 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1893 if (status != 0 && status != EBUSY) {
1894 KMP_SYSFAIL("pthread_mutex_destroy", status);
1896 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1897 if (status != 0 && status != EBUSY) {
1898 KMP_SYSFAIL("pthread_cond_destroy", status);
1900 #if KMP_AFFINITY_SUPPORTED
1901 __kmp_affinity_uninitialize();
1904 __kmp_init_runtime = FALSE;
1907 /* Put the thread to sleep for a time period */
1908 /* NOTE: not currently used anywhere */
1909 void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
1911 /* Calculate the elapsed wall clock time for the user */
1912 void __kmp_elapsed(double *t) {
1914 #ifdef FIX_SGI_CLOCK
1917 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1918 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
1920 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
1924 status = gettimeofday(&tv, NULL);
1925 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1927 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
1931 /* Calculate the elapsed wall clock tick for the user */
1932 void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
1934 /* Return the current time stamp in nsec */
1935 kmp_uint64 __kmp_now_nsec() {
1937 gettimeofday(&t, NULL);
1938 kmp_uint64 nsec = (kmp_uint64)KMP_NSEC_PER_SEC * (kmp_uint64)t.tv_sec +
1939 (kmp_uint64)1000 * (kmp_uint64)t.tv_usec;
1943 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1944 /* Measure clock ticks per millisecond */
1945 void __kmp_initialize_system_tick() {
1946 kmp_uint64 now, nsec2, diff;
1947 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;
1950 while ((now = __kmp_hardware_timestamp()) < goal)
1952 nsec2 = __kmp_now_nsec();
1953 diff = nsec2 - nsec;
1955 kmp_uint64 tpms = (kmp_uint64)(1e6 * (delay + (now - goal)) / diff);
1957 __kmp_ticks_per_msec = tpms;
1962 /* Determine whether the given address is mapped into the current address
1965 int __kmp_is_address_mapped(void *addr) {
1970 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD
1972 /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address
1973 ranges mapped into the address space. */
1975 char *name = __kmp_str_format("/proc/%d/maps", getpid());
1978 file = fopen(name, "r");
1979 KMP_ASSERT(file != NULL);
1983 void *beginning = NULL;
1984 void *ending = NULL;
1987 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
1991 KMP_ASSERT(rc == 3 &&
1992 KMP_STRLEN(perms) == 4); // Make sure all fields are read.
1994 // 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.
2007 KMP_INTERNAL_FREE(name);
2011 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
2012 using vm interface. */
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.
2024 // Memory successfully read.
2033 mib[2] = VM_PROC_MAP;
2035 mib[4] = sizeof(struct kinfo_vmentry);
2038 rc = sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0);
2042 size = size * 4 / 3;
2043 struct kinfo_vmentry *kiv = (struct kinfo_vmentry *)KMP_INTERNAL_MALLOC(size);
2046 rc = sysctl(mib, __arraycount(mib), kiv, &size, NULL, 0);
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) {
2057 KMP_INTERNAL_FREE(kiv);
2058 #elif KMP_OS_DRAGONFLY || KMP_OS_OPENBSD
2060 // FIXME(DragonFly, OpenBSD): Implement this
2065 #error "Unknown or unsupported OS"
2071 } // __kmp_is_address_mapped
2073 #ifdef USE_LOAD_BALANCE
2075 #if KMP_OS_DARWIN || KMP_OS_NETBSD
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.
2082 int __kmp_get_load_balance(int max) {
2086 int res = getloadavg(averages, 3);
2088 // 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
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) &&
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
2108 // 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
2112 int __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 */
2118 int running_threads = 0; // Number of running threads in the system.
2120 DIR *proc_dir = NULL; // Handle of "/proc/" directory.
2121 struct dirent *proc_entry = NULL;
2123 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;
2128 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2130 int stat_path_fixed_len;
2132 int total_processes = 0; // Total number of processes in system.
2133 int total_threads = 0; // Total number of threads in system.
2135 double call_time = 0.0;
2137 __kmp_str_buf_init(&task_path);
2138 __kmp_str_buf_init(&stat_path);
2140 __kmp_elapsed(&call_time);
2142 if (glb_call_time &&
2143 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2144 running_threads = glb_running_threads;
2148 glb_call_time = call_time;
2150 // Do not spend time on scanning "/proc/" if we have a permanent error.
2151 if (permanent_error) {
2152 running_threads = -1;
2160 // 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;
2170 // 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.
2174 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])) {
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);
2189 // 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);
2195 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;
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;
2217 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])) {
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.
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);
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.
2240 /* Content of "stat" file looks like:
2241 24285 (program) S ...
2243 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
2248 Good news: Length of program name is 15 characters max. Longer
2249 names are truncated.
2251 Thus, we need rather short buffer: 15 chars for program name +
2252 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2254 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).
2259 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.
2266 len = read(stat_file, buffer, sizeof(buffer) - 1);
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);
2278 if (running_threads >= max) {
2288 task_entry = readdir(task_dir);
2294 proc_entry = readdir(proc_dir);
2297 // 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;
2305 finish: // Clean up and exit.
2306 if (proc_dir != NULL) {
2309 __kmp_str_buf_free(&task_path);
2310 if (task_dir != NULL) {
2313 __kmp_str_buf_free(&stat_path);
2314 if (stat_file != -1) {
2318 glb_running_threads = running_threads;
2320 return running_threads;
2322 } // __kmp_get_load_balance
2324 #endif // KMP_OS_DARWIN
2326 #endif // USE_LOAD_BALANCE
2328 #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
2329 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
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
2333 int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
2337 void **exit_frame_ptr
2341 *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0);
2346 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2350 (*pkfn)(>id, &tid);
2353 (*pkfn)(>id, &tid, p_argv[0]);
2356 (*pkfn)(>id, &tid, p_argv[0], p_argv[1]);
2359 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]);
2362 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2365 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2368 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2372 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2373 p_argv[5], p_argv[6]);
2376 (*pkfn)(>id, &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]);
2380 (*pkfn)(>id, &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]);
2384 (*pkfn)(>id, &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]);
2388 (*pkfn)(>id, &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]);
2392 (*pkfn)(>id, &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],
2397 (*pkfn)(>id, &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]);
2402 (*pkfn)(>id, &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]);
2407 (*pkfn)(>id, &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]);
2414 *exit_frame_ptr = 0;