1 /* 2 * z_Linux_util.cpp -- platform specific routines. 3 */ 4 5 //===----------------------------------------------------------------------===// 6 // 7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 8 // See https://llvm.org/LICENSE.txt for license information. 9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "kmp.h" 14 #include "kmp_affinity.h" 15 #include "kmp_i18n.h" 16 #include "kmp_io.h" 17 #include "kmp_itt.h" 18 #include "kmp_lock.h" 19 #include "kmp_stats.h" 20 #include "kmp_str.h" 21 #include "kmp_wait_release.h" 22 #include "kmp_wrapper_getpid.h" 23 24 #if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD 25 #include <alloca.h> 26 #endif 27 #include <math.h> // HUGE_VAL. 28 #include <sys/resource.h> 29 #include <sys/syscall.h> 30 #include <sys/time.h> 31 #include <sys/times.h> 32 #include <unistd.h> 33 34 #if KMP_OS_LINUX && !KMP_OS_CNK 35 #include <sys/sysinfo.h> 36 #if KMP_USE_FUTEX 37 // We should really include <futex.h>, but that causes compatibility problems on 38 // different Linux* OS distributions that either require that you include (or 39 // break when you try to include) <pci/types.h>. Since all we need is the two 40 // macros below (which are part of the kernel ABI, so can't change) we just 41 // define the constants here and don't include <futex.h> 42 #ifndef FUTEX_WAIT 43 #define FUTEX_WAIT 0 44 #endif 45 #ifndef FUTEX_WAKE 46 #define FUTEX_WAKE 1 47 #endif 48 #endif 49 #elif KMP_OS_DARWIN 50 #include <mach/mach.h> 51 #include <sys/sysctl.h> 52 #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD 53 #include <pthread_np.h> 54 #elif KMP_OS_NETBSD 55 #include <sys/types.h> 56 #include <sys/sysctl.h> 57 #endif 58 59 #include <ctype.h> 60 #include <dirent.h> 61 #include <fcntl.h> 62 63 #include "tsan_annotations.h" 64 65 struct kmp_sys_timer { 66 struct timespec start; 67 }; 68 69 // Convert timespec to nanoseconds. 70 #define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec) 71 72 static struct kmp_sys_timer __kmp_sys_timer_data; 73 74 #if KMP_HANDLE_SIGNALS 75 typedef void (*sig_func_t)(int); 76 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG]; 77 static sigset_t __kmp_sigset; 78 #endif 79 80 static int __kmp_init_runtime = FALSE; 81 82 static int __kmp_fork_count = 0; 83 84 static pthread_condattr_t __kmp_suspend_cond_attr; 85 static pthread_mutexattr_t __kmp_suspend_mutex_attr; 86 87 static kmp_cond_align_t __kmp_wait_cv; 88 static kmp_mutex_align_t __kmp_wait_mx; 89 90 kmp_uint64 __kmp_ticks_per_msec = 1000000; 91 92 #ifdef DEBUG_SUSPEND 93 static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) { 94 KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))", 95 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock, 96 cond->c_cond.__c_waiting); 97 } 98 #endif 99 100 #if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED) 101 102 /* Affinity support */ 103 104 void __kmp_affinity_bind_thread(int which) { 105 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(), 106 "Illegal set affinity operation when not capable"); 107 108 kmp_affin_mask_t *mask; 109 KMP_CPU_ALLOC_ON_STACK(mask); 110 KMP_CPU_ZERO(mask); 111 KMP_CPU_SET(which, mask); 112 __kmp_set_system_affinity(mask, TRUE); 113 KMP_CPU_FREE_FROM_STACK(mask); 114 } 115 116 /* Determine if we can access affinity functionality on this version of 117 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set 118 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */ 119 void __kmp_affinity_determine_capable(const char *env_var) { 120 // Check and see if the OS supports thread affinity. 121 122 #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024) 123 124 int gCode; 125 int sCode; 126 unsigned char *buf; 127 buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT); 128 129 // If Linux* OS: 130 // If the syscall fails or returns a suggestion for the size, 131 // then we don't have to search for an appropriate size. 132 gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf); 133 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 134 "initial getaffinity call returned %d errno = %d\n", 135 gCode, errno)); 136 137 // if ((gCode < 0) && (errno == ENOSYS)) 138 if (gCode < 0) { 139 // System call not supported 140 if (__kmp_affinity_verbose || 141 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) && 142 (__kmp_affinity_type != affinity_default) && 143 (__kmp_affinity_type != affinity_disabled))) { 144 int error = errno; 145 kmp_msg_t err_code = KMP_ERR(error); 146 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var), 147 err_code, __kmp_msg_null); 148 if (__kmp_generate_warnings == kmp_warnings_off) { 149 __kmp_str_free(&err_code.str); 150 } 151 } 152 KMP_AFFINITY_DISABLE(); 153 KMP_INTERNAL_FREE(buf); 154 return; 155 } 156 if (gCode > 0) { // Linux* OS only 157 // The optimal situation: the OS returns the size of the buffer it expects. 158 // 159 // A verification of correct behavior is that Isetaffinity on a NULL 160 // buffer with the same size fails with errno set to EFAULT. 161 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL); 162 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 163 "setaffinity for mask size %d returned %d errno = %d\n", 164 gCode, sCode, errno)); 165 if (sCode < 0) { 166 if (errno == ENOSYS) { 167 if (__kmp_affinity_verbose || 168 (__kmp_affinity_warnings && 169 (__kmp_affinity_type != affinity_none) && 170 (__kmp_affinity_type != affinity_default) && 171 (__kmp_affinity_type != affinity_disabled))) { 172 int error = errno; 173 kmp_msg_t err_code = KMP_ERR(error); 174 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var), 175 err_code, __kmp_msg_null); 176 if (__kmp_generate_warnings == kmp_warnings_off) { 177 __kmp_str_free(&err_code.str); 178 } 179 } 180 KMP_AFFINITY_DISABLE(); 181 KMP_INTERNAL_FREE(buf); 182 } 183 if (errno == EFAULT) { 184 KMP_AFFINITY_ENABLE(gCode); 185 KA_TRACE(10, ("__kmp_affinity_determine_capable: " 186 "affinity supported (mask size %d)\n", 187 (int)__kmp_affin_mask_size)); 188 KMP_INTERNAL_FREE(buf); 189 return; 190 } 191 } 192 } 193 194 // Call the getaffinity system call repeatedly with increasing set sizes 195 // until we succeed, or reach an upper bound on the search. 196 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 197 "searching for proper set size\n")); 198 int size; 199 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) { 200 gCode = syscall(__NR_sched_getaffinity, 0, size, buf); 201 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 202 "getaffinity for mask size %d returned %d errno = %d\n", 203 size, gCode, errno)); 204 205 if (gCode < 0) { 206 if (errno == ENOSYS) { 207 // We shouldn't get here 208 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 209 "inconsistent OS call behavior: errno == ENOSYS for mask " 210 "size %d\n", 211 size)); 212 if (__kmp_affinity_verbose || 213 (__kmp_affinity_warnings && 214 (__kmp_affinity_type != affinity_none) && 215 (__kmp_affinity_type != affinity_default) && 216 (__kmp_affinity_type != affinity_disabled))) { 217 int error = errno; 218 kmp_msg_t err_code = KMP_ERR(error); 219 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var), 220 err_code, __kmp_msg_null); 221 if (__kmp_generate_warnings == kmp_warnings_off) { 222 __kmp_str_free(&err_code.str); 223 } 224 } 225 KMP_AFFINITY_DISABLE(); 226 KMP_INTERNAL_FREE(buf); 227 return; 228 } 229 continue; 230 } 231 232 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL); 233 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 234 "setaffinity for mask size %d returned %d errno = %d\n", 235 gCode, sCode, errno)); 236 if (sCode < 0) { 237 if (errno == ENOSYS) { // Linux* OS only 238 // We shouldn't get here 239 KA_TRACE(30, ("__kmp_affinity_determine_capable: " 240 "inconsistent OS call behavior: errno == ENOSYS for mask " 241 "size %d\n", 242 size)); 243 if (__kmp_affinity_verbose || 244 (__kmp_affinity_warnings && 245 (__kmp_affinity_type != affinity_none) && 246 (__kmp_affinity_type != affinity_default) && 247 (__kmp_affinity_type != affinity_disabled))) { 248 int error = errno; 249 kmp_msg_t err_code = KMP_ERR(error); 250 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var), 251 err_code, __kmp_msg_null); 252 if (__kmp_generate_warnings == kmp_warnings_off) { 253 __kmp_str_free(&err_code.str); 254 } 255 } 256 KMP_AFFINITY_DISABLE(); 257 KMP_INTERNAL_FREE(buf); 258 return; 259 } 260 if (errno == EFAULT) { 261 KMP_AFFINITY_ENABLE(gCode); 262 KA_TRACE(10, ("__kmp_affinity_determine_capable: " 263 "affinity supported (mask size %d)\n", 264 (int)__kmp_affin_mask_size)); 265 KMP_INTERNAL_FREE(buf); 266 return; 267 } 268 } 269 } 270 // save uncaught error code 271 // int error = errno; 272 KMP_INTERNAL_FREE(buf); 273 // restore uncaught error code, will be printed at the next KMP_WARNING below 274 // errno = error; 275 276 // Affinity is not supported 277 KMP_AFFINITY_DISABLE(); 278 KA_TRACE(10, ("__kmp_affinity_determine_capable: " 279 "cannot determine mask size - affinity not supported\n")); 280 if (__kmp_affinity_verbose || 281 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) && 282 (__kmp_affinity_type != affinity_default) && 283 (__kmp_affinity_type != affinity_disabled))) { 284 KMP_WARNING(AffCantGetMaskSize, env_var); 285 } 286 } 287 288 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED 289 290 #if KMP_USE_FUTEX 291 292 int __kmp_futex_determine_capable() { 293 int loc = 0; 294 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0); 295 int retval = (rc == 0) || (errno != ENOSYS); 296 297 KA_TRACE(10, 298 ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno)); 299 KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n", 300 retval ? "" : " not")); 301 302 return retval; 303 } 304 305 #endif // KMP_USE_FUTEX 306 307 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS) 308 /* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to 309 use compare_and_store for these routines */ 310 311 kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) { 312 kmp_int8 old_value, new_value; 313 314 old_value = TCR_1(*p); 315 new_value = old_value | d; 316 317 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { 318 KMP_CPU_PAUSE(); 319 old_value = TCR_1(*p); 320 new_value = old_value | d; 321 } 322 return old_value; 323 } 324 325 kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) { 326 kmp_int8 old_value, new_value; 327 328 old_value = TCR_1(*p); 329 new_value = old_value & d; 330 331 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { 332 KMP_CPU_PAUSE(); 333 old_value = TCR_1(*p); 334 new_value = old_value & d; 335 } 336 return old_value; 337 } 338 339 kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) { 340 kmp_uint32 old_value, new_value; 341 342 old_value = TCR_4(*p); 343 new_value = old_value | d; 344 345 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) { 346 KMP_CPU_PAUSE(); 347 old_value = TCR_4(*p); 348 new_value = old_value | d; 349 } 350 return old_value; 351 } 352 353 kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) { 354 kmp_uint32 old_value, new_value; 355 356 old_value = TCR_4(*p); 357 new_value = old_value & d; 358 359 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) { 360 KMP_CPU_PAUSE(); 361 old_value = TCR_4(*p); 362 new_value = old_value & d; 363 } 364 return old_value; 365 } 366 367 #if KMP_ARCH_X86 368 kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) { 369 kmp_int8 old_value, new_value; 370 371 old_value = TCR_1(*p); 372 new_value = old_value + d; 373 374 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { 375 KMP_CPU_PAUSE(); 376 old_value = TCR_1(*p); 377 new_value = old_value + d; 378 } 379 return old_value; 380 } 381 382 kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) { 383 kmp_int64 old_value, new_value; 384 385 old_value = TCR_8(*p); 386 new_value = old_value + d; 387 388 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { 389 KMP_CPU_PAUSE(); 390 old_value = TCR_8(*p); 391 new_value = old_value + d; 392 } 393 return old_value; 394 } 395 #endif /* KMP_ARCH_X86 */ 396 397 kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) { 398 kmp_uint64 old_value, new_value; 399 400 old_value = TCR_8(*p); 401 new_value = old_value | d; 402 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { 403 KMP_CPU_PAUSE(); 404 old_value = TCR_8(*p); 405 new_value = old_value | d; 406 } 407 return old_value; 408 } 409 410 kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) { 411 kmp_uint64 old_value, new_value; 412 413 old_value = TCR_8(*p); 414 new_value = old_value & d; 415 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { 416 KMP_CPU_PAUSE(); 417 old_value = TCR_8(*p); 418 new_value = old_value & d; 419 } 420 return old_value; 421 } 422 423 #endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */ 424 425 void __kmp_terminate_thread(int gtid) { 426 int status; 427 kmp_info_t *th = __kmp_threads[gtid]; 428 429 if (!th) 430 return; 431 432 #ifdef KMP_CANCEL_THREADS 433 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid)); 434 status = pthread_cancel(th->th.th_info.ds.ds_thread); 435 if (status != 0 && status != ESRCH) { 436 __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status), 437 __kmp_msg_null); 438 } 439 #endif 440 KMP_YIELD(TRUE); 441 } // 442 443 /* Set thread stack info according to values returned by pthread_getattr_np(). 444 If values are unreasonable, assume call failed and use incremental stack 445 refinement method instead. Returns TRUE if the stack parameters could be 446 determined exactly, FALSE if incremental refinement is necessary. */ 447 static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) { 448 int stack_data; 449 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ 450 KMP_OS_HURD 451 pthread_attr_t attr; 452 int status; 453 size_t size = 0; 454 void *addr = 0; 455 456 /* Always do incremental stack refinement for ubermaster threads since the 457 initial thread stack range can be reduced by sibling thread creation so 458 pthread_attr_getstack may cause thread gtid aliasing */ 459 if (!KMP_UBER_GTID(gtid)) { 460 461 /* Fetch the real thread attributes */ 462 status = pthread_attr_init(&attr); 463 KMP_CHECK_SYSFAIL("pthread_attr_init", status); 464 #if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD 465 status = pthread_attr_get_np(pthread_self(), &attr); 466 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status); 467 #else 468 status = pthread_getattr_np(pthread_self(), &attr); 469 KMP_CHECK_SYSFAIL("pthread_getattr_np", status); 470 #endif 471 status = pthread_attr_getstack(&attr, &addr, &size); 472 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status); 473 KA_TRACE(60, 474 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:" 475 " %lu, low addr: %p\n", 476 gtid, size, addr)); 477 status = pthread_attr_destroy(&attr); 478 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status); 479 } 480 481 if (size != 0 && addr != 0) { // was stack parameter determination successful? 482 /* Store the correct base and size */ 483 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size)); 484 TCW_PTR(th->th.th_info.ds.ds_stacksize, size); 485 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE); 486 return TRUE; 487 } 488 #endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || 489 KMP_OS_HURD */ 490 /* Use incremental refinement starting from initial conservative estimate */ 491 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0); 492 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data); 493 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE); 494 return FALSE; 495 } 496 497 static void *__kmp_launch_worker(void *thr) { 498 int status, old_type, old_state; 499 #ifdef KMP_BLOCK_SIGNALS 500 sigset_t new_set, old_set; 501 #endif /* KMP_BLOCK_SIGNALS */ 502 void *exit_val; 503 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ 504 KMP_OS_OPENBSD || KMP_OS_HURD 505 void *volatile padding = 0; 506 #endif 507 int gtid; 508 509 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid; 510 __kmp_gtid_set_specific(gtid); 511 #ifdef KMP_TDATA_GTID 512 __kmp_gtid = gtid; 513 #endif 514 #if KMP_STATS_ENABLED 515 // set thread local index to point to thread-specific stats 516 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats; 517 __kmp_stats_thread_ptr->startLife(); 518 KMP_SET_THREAD_STATE(IDLE); 519 KMP_INIT_PARTITIONED_TIMERS(OMP_idle); 520 #endif 521 522 #if USE_ITT_BUILD 523 __kmp_itt_thread_name(gtid); 524 #endif /* USE_ITT_BUILD */ 525 526 #if KMP_AFFINITY_SUPPORTED 527 __kmp_affinity_set_init_mask(gtid, FALSE); 528 #endif 529 530 #ifdef KMP_CANCEL_THREADS 531 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type); 532 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status); 533 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? 534 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state); 535 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); 536 #endif 537 538 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 539 // Set FP control regs to be a copy of the parallel initialization thread's. 540 __kmp_clear_x87_fpu_status_word(); 541 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word); 542 __kmp_load_mxcsr(&__kmp_init_mxcsr); 543 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ 544 545 #ifdef KMP_BLOCK_SIGNALS 546 status = sigfillset(&new_set); 547 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status); 548 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set); 549 KMP_CHECK_SYSFAIL("pthread_sigmask", status); 550 #endif /* KMP_BLOCK_SIGNALS */ 551 552 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ 553 KMP_OS_OPENBSD 554 if (__kmp_stkoffset > 0 && gtid > 0) { 555 padding = KMP_ALLOCA(gtid * __kmp_stkoffset); 556 } 557 #endif 558 559 KMP_MB(); 560 __kmp_set_stack_info(gtid, (kmp_info_t *)thr); 561 562 __kmp_check_stack_overlap((kmp_info_t *)thr); 563 564 exit_val = __kmp_launch_thread((kmp_info_t *)thr); 565 566 #ifdef KMP_BLOCK_SIGNALS 567 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL); 568 KMP_CHECK_SYSFAIL("pthread_sigmask", status); 569 #endif /* KMP_BLOCK_SIGNALS */ 570 571 return exit_val; 572 } 573 574 #if KMP_USE_MONITOR 575 /* The monitor thread controls all of the threads in the complex */ 576 577 static void *__kmp_launch_monitor(void *thr) { 578 int status, old_type, old_state; 579 #ifdef KMP_BLOCK_SIGNALS 580 sigset_t new_set; 581 #endif /* KMP_BLOCK_SIGNALS */ 582 struct timespec interval; 583 584 KMP_MB(); /* Flush all pending memory write invalidates. */ 585 586 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n")); 587 588 /* register us as the monitor thread */ 589 __kmp_gtid_set_specific(KMP_GTID_MONITOR); 590 #ifdef KMP_TDATA_GTID 591 __kmp_gtid = KMP_GTID_MONITOR; 592 #endif 593 594 KMP_MB(); 595 596 #if USE_ITT_BUILD 597 // Instruct Intel(R) Threading Tools to ignore monitor thread. 598 __kmp_itt_thread_ignore(); 599 #endif /* USE_ITT_BUILD */ 600 601 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid, 602 (kmp_info_t *)thr); 603 604 __kmp_check_stack_overlap((kmp_info_t *)thr); 605 606 #ifdef KMP_CANCEL_THREADS 607 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type); 608 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status); 609 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? 610 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state); 611 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); 612 #endif 613 614 #if KMP_REAL_TIME_FIX 615 // This is a potential fix which allows application with real-time scheduling 616 // policy work. However, decision about the fix is not made yet, so it is 617 // disabled by default. 618 { // Are program started with real-time scheduling policy? 619 int sched = sched_getscheduler(0); 620 if (sched == SCHED_FIFO || sched == SCHED_RR) { 621 // Yes, we are a part of real-time application. Try to increase the 622 // priority of the monitor. 623 struct sched_param param; 624 int max_priority = sched_get_priority_max(sched); 625 int rc; 626 KMP_WARNING(RealTimeSchedNotSupported); 627 sched_getparam(0, ¶m); 628 if (param.sched_priority < max_priority) { 629 param.sched_priority += 1; 630 rc = sched_setscheduler(0, sched, ¶m); 631 if (rc != 0) { 632 int error = errno; 633 kmp_msg_t err_code = KMP_ERR(error); 634 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority), 635 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null); 636 if (__kmp_generate_warnings == kmp_warnings_off) { 637 __kmp_str_free(&err_code.str); 638 } 639 } 640 } else { 641 // We cannot abort here, because number of CPUs may be enough for all 642 // the threads, including the monitor thread, so application could 643 // potentially work... 644 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority), 645 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority), 646 __kmp_msg_null); 647 } 648 } 649 // AC: free thread that waits for monitor started 650 TCW_4(__kmp_global.g.g_time.dt.t_value, 0); 651 } 652 #endif // KMP_REAL_TIME_FIX 653 654 KMP_MB(); /* Flush all pending memory write invalidates. */ 655 656 if (__kmp_monitor_wakeups == 1) { 657 interval.tv_sec = 1; 658 interval.tv_nsec = 0; 659 } else { 660 interval.tv_sec = 0; 661 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups); 662 } 663 664 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n")); 665 666 while (!TCR_4(__kmp_global.g.g_done)) { 667 struct timespec now; 668 struct timeval tval; 669 670 /* This thread monitors the state of the system */ 671 672 KA_TRACE(15, ("__kmp_launch_monitor: update\n")); 673 674 status = gettimeofday(&tval, NULL); 675 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); 676 TIMEVAL_TO_TIMESPEC(&tval, &now); 677 678 now.tv_sec += interval.tv_sec; 679 now.tv_nsec += interval.tv_nsec; 680 681 if (now.tv_nsec >= KMP_NSEC_PER_SEC) { 682 now.tv_sec += 1; 683 now.tv_nsec -= KMP_NSEC_PER_SEC; 684 } 685 686 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex); 687 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); 688 // AC: the monitor should not fall asleep if g_done has been set 689 if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex 690 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond, 691 &__kmp_wait_mx.m_mutex, &now); 692 if (status != 0) { 693 if (status != ETIMEDOUT && status != EINTR) { 694 KMP_SYSFAIL("pthread_cond_timedwait", status); 695 } 696 } 697 } 698 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex); 699 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 700 701 TCW_4(__kmp_global.g.g_time.dt.t_value, 702 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1); 703 704 KMP_MB(); /* Flush all pending memory write invalidates. */ 705 } 706 707 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n")); 708 709 #ifdef KMP_BLOCK_SIGNALS 710 status = sigfillset(&new_set); 711 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status); 712 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL); 713 KMP_CHECK_SYSFAIL("pthread_sigmask", status); 714 #endif /* KMP_BLOCK_SIGNALS */ 715 716 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n")); 717 718 if (__kmp_global.g.g_abort != 0) { 719 /* now we need to terminate the worker threads */ 720 /* the value of t_abort is the signal we caught */ 721 722 int gtid; 723 724 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", 725 __kmp_global.g.g_abort)); 726 727 /* terminate the OpenMP worker threads */ 728 /* TODO this is not valid for sibling threads!! 729 * the uber master might not be 0 anymore.. */ 730 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid) 731 __kmp_terminate_thread(gtid); 732 733 __kmp_cleanup(); 734 735 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n", 736 __kmp_global.g.g_abort)); 737 738 if (__kmp_global.g.g_abort > 0) 739 raise(__kmp_global.g.g_abort); 740 } 741 742 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n")); 743 744 return thr; 745 } 746 #endif // KMP_USE_MONITOR 747 748 void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) { 749 pthread_t handle; 750 pthread_attr_t thread_attr; 751 int status; 752 753 th->th.th_info.ds.ds_gtid = gtid; 754 755 #if KMP_STATS_ENABLED 756 // sets up worker thread stats 757 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid); 758 759 // th->th.th_stats is used to transfer thread-specific stats-pointer to 760 // __kmp_launch_worker. So when thread is created (goes into 761 // __kmp_launch_worker) it will set its thread local pointer to 762 // th->th.th_stats 763 if (!KMP_UBER_GTID(gtid)) { 764 th->th.th_stats = __kmp_stats_list->push_back(gtid); 765 } else { 766 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(), 767 // so set the th->th.th_stats field to it. 768 th->th.th_stats = __kmp_stats_thread_ptr; 769 } 770 __kmp_release_tas_lock(&__kmp_stats_lock, gtid); 771 772 #endif // KMP_STATS_ENABLED 773 774 if (KMP_UBER_GTID(gtid)) { 775 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid)); 776 th->th.th_info.ds.ds_thread = pthread_self(); 777 __kmp_set_stack_info(gtid, th); 778 __kmp_check_stack_overlap(th); 779 return; 780 } 781 782 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid)); 783 784 KMP_MB(); /* Flush all pending memory write invalidates. */ 785 786 #ifdef KMP_THREAD_ATTR 787 status = pthread_attr_init(&thread_attr); 788 if (status != 0) { 789 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null); 790 } 791 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); 792 if (status != 0) { 793 __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null); 794 } 795 796 /* Set stack size for this thread now. 797 The multiple of 2 is there because on some machines, requesting an unusual 798 stacksize causes the thread to have an offset before the dummy alloca() 799 takes place to create the offset. Since we want the user to have a 800 sufficient stacksize AND support a stack offset, we alloca() twice the 801 offset so that the upcoming alloca() does not eliminate any premade offset, 802 and also gives the user the stack space they requested for all threads */ 803 stack_size += gtid * __kmp_stkoffset * 2; 804 805 #if defined(__ANDROID__) && __ANDROID_API__ < 19 806 // Round the stack size to a multiple of the page size. Older versions of 807 // Android (until KitKat) would fail pthread_attr_setstacksize with EINVAL 808 // if the stack size was not a multiple of the page size. 809 stack_size = (stack_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 810 #endif 811 812 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 813 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n", 814 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size)); 815 816 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 817 status = pthread_attr_setstacksize(&thread_attr, stack_size); 818 #ifdef KMP_BACKUP_STKSIZE 819 if (status != 0) { 820 if (!__kmp_env_stksize) { 821 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset; 822 __kmp_stksize = KMP_BACKUP_STKSIZE; 823 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 824 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu " 825 "bytes\n", 826 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size)); 827 status = pthread_attr_setstacksize(&thread_attr, stack_size); 828 } 829 } 830 #endif /* KMP_BACKUP_STKSIZE */ 831 if (status != 0) { 832 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), 833 KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null); 834 } 835 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ 836 837 #endif /* KMP_THREAD_ATTR */ 838 839 status = 840 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th); 841 if (status != 0 || !handle) { // ??? Why do we check handle?? 842 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 843 if (status == EINVAL) { 844 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), 845 KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null); 846 } 847 if (status == ENOMEM) { 848 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), 849 KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null); 850 } 851 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ 852 if (status == EAGAIN) { 853 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status), 854 KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null); 855 } 856 KMP_SYSFAIL("pthread_create", status); 857 } 858 859 th->th.th_info.ds.ds_thread = handle; 860 861 #ifdef KMP_THREAD_ATTR 862 status = pthread_attr_destroy(&thread_attr); 863 if (status) { 864 kmp_msg_t err_code = KMP_ERR(status); 865 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code, 866 __kmp_msg_null); 867 if (__kmp_generate_warnings == kmp_warnings_off) { 868 __kmp_str_free(&err_code.str); 869 } 870 } 871 #endif /* KMP_THREAD_ATTR */ 872 873 KMP_MB(); /* Flush all pending memory write invalidates. */ 874 875 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid)); 876 877 } // __kmp_create_worker 878 879 #if KMP_USE_MONITOR 880 void __kmp_create_monitor(kmp_info_t *th) { 881 pthread_t handle; 882 pthread_attr_t thread_attr; 883 size_t size; 884 int status; 885 int auto_adj_size = FALSE; 886 887 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) { 888 // We don't need monitor thread in case of MAX_BLOCKTIME 889 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of " 890 "MAX blocktime\n")); 891 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op 892 th->th.th_info.ds.ds_gtid = 0; 893 return; 894 } 895 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n")); 896 897 KMP_MB(); /* Flush all pending memory write invalidates. */ 898 899 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR; 900 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR; 901 #if KMP_REAL_TIME_FIX 902 TCW_4(__kmp_global.g.g_time.dt.t_value, 903 -1); // Will use it for synchronization a bit later. 904 #else 905 TCW_4(__kmp_global.g.g_time.dt.t_value, 0); 906 #endif // KMP_REAL_TIME_FIX 907 908 #ifdef KMP_THREAD_ATTR 909 if (__kmp_monitor_stksize == 0) { 910 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE; 911 auto_adj_size = TRUE; 912 } 913 status = pthread_attr_init(&thread_attr); 914 if (status != 0) { 915 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null); 916 } 917 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); 918 if (status != 0) { 919 __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null); 920 } 921 922 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 923 status = pthread_attr_getstacksize(&thread_attr, &size); 924 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status); 925 #else 926 size = __kmp_sys_min_stksize; 927 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ 928 #endif /* KMP_THREAD_ATTR */ 929 930 if (__kmp_monitor_stksize == 0) { 931 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE; 932 } 933 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) { 934 __kmp_monitor_stksize = __kmp_sys_min_stksize; 935 } 936 937 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes," 938 "requested stacksize = %lu bytes\n", 939 size, __kmp_monitor_stksize)); 940 941 retry: 942 943 /* Set stack size for this thread now. */ 944 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 945 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,", 946 __kmp_monitor_stksize)); 947 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize); 948 if (status != 0) { 949 if (auto_adj_size) { 950 __kmp_monitor_stksize *= 2; 951 goto retry; 952 } 953 kmp_msg_t err_code = KMP_ERR(status); 954 __kmp_msg(kmp_ms_warning, // should this be fatal? BB 955 KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize), 956 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null); 957 if (__kmp_generate_warnings == kmp_warnings_off) { 958 __kmp_str_free(&err_code.str); 959 } 960 } 961 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ 962 963 status = 964 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th); 965 966 if (status != 0) { 967 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 968 if (status == EINVAL) { 969 if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) { 970 __kmp_monitor_stksize *= 2; 971 goto retry; 972 } 973 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize), 974 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize), 975 __kmp_msg_null); 976 } 977 if (status == ENOMEM) { 978 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize), 979 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize), 980 __kmp_msg_null); 981 } 982 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ 983 if (status == EAGAIN) { 984 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status), 985 KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null); 986 } 987 KMP_SYSFAIL("pthread_create", status); 988 } 989 990 th->th.th_info.ds.ds_thread = handle; 991 992 #if KMP_REAL_TIME_FIX 993 // Wait for the monitor thread is really started and set its *priority*. 994 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == 995 sizeof(__kmp_global.g.g_time.dt.t_value)); 996 __kmp_wait_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value, -1, 997 &__kmp_neq_4, NULL); 998 #endif // KMP_REAL_TIME_FIX 999 1000 #ifdef KMP_THREAD_ATTR 1001 status = pthread_attr_destroy(&thread_attr); 1002 if (status != 0) { 1003 kmp_msg_t err_code = KMP_ERR(status); 1004 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code, 1005 __kmp_msg_null); 1006 if (__kmp_generate_warnings == kmp_warnings_off) { 1007 __kmp_str_free(&err_code.str); 1008 } 1009 } 1010 #endif 1011 1012 KMP_MB(); /* Flush all pending memory write invalidates. */ 1013 1014 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n", 1015 th->th.th_info.ds.ds_thread)); 1016 1017 } // __kmp_create_monitor 1018 #endif // KMP_USE_MONITOR 1019 1020 void __kmp_exit_thread(int exit_status) { 1021 pthread_exit((void *)(intptr_t)exit_status); 1022 } // __kmp_exit_thread 1023 1024 #if KMP_USE_MONITOR 1025 void __kmp_resume_monitor(); 1026 1027 void __kmp_reap_monitor(kmp_info_t *th) { 1028 int status; 1029 void *exit_val; 1030 1031 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle" 1032 " %#.8lx\n", 1033 th->th.th_info.ds.ds_thread)); 1034 1035 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR. 1036 // If both tid and gtid are 0, it means the monitor did not ever start. 1037 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down. 1038 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid); 1039 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) { 1040 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n")); 1041 return; 1042 } 1043 1044 KMP_MB(); /* Flush all pending memory write invalidates. */ 1045 1046 /* First, check to see whether the monitor thread exists to wake it up. This 1047 is to avoid performance problem when the monitor sleeps during 1048 blocktime-size interval */ 1049 1050 status = pthread_kill(th->th.th_info.ds.ds_thread, 0); 1051 if (status != ESRCH) { 1052 __kmp_resume_monitor(); // Wake up the monitor thread 1053 } 1054 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n")); 1055 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val); 1056 if (exit_val != th) { 1057 __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null); 1058 } 1059 1060 th->th.th_info.ds.ds_tid = KMP_GTID_DNE; 1061 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE; 1062 1063 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle" 1064 " %#.8lx\n", 1065 th->th.th_info.ds.ds_thread)); 1066 1067 KMP_MB(); /* Flush all pending memory write invalidates. */ 1068 } 1069 #endif // KMP_USE_MONITOR 1070 1071 void __kmp_reap_worker(kmp_info_t *th) { 1072 int status; 1073 void *exit_val; 1074 1075 KMP_MB(); /* Flush all pending memory write invalidates. */ 1076 1077 KA_TRACE( 1078 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid)); 1079 1080 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val); 1081 #ifdef KMP_DEBUG 1082 /* Don't expose these to the user until we understand when they trigger */ 1083 if (status != 0) { 1084 __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null); 1085 } 1086 if (exit_val != th) { 1087 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, " 1088 "exit_val = %p\n", 1089 th->th.th_info.ds.ds_gtid, exit_val)); 1090 } 1091 #endif /* KMP_DEBUG */ 1092 1093 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n", 1094 th->th.th_info.ds.ds_gtid)); 1095 1096 KMP_MB(); /* Flush all pending memory write invalidates. */ 1097 } 1098 1099 #if KMP_HANDLE_SIGNALS 1100 1101 static void __kmp_null_handler(int signo) { 1102 // Do nothing, for doing SIG_IGN-type actions. 1103 } // __kmp_null_handler 1104 1105 static void __kmp_team_handler(int signo) { 1106 if (__kmp_global.g.g_abort == 0) { 1107 /* Stage 1 signal handler, let's shut down all of the threads */ 1108 #ifdef KMP_DEBUG 1109 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo); 1110 #endif 1111 switch (signo) { 1112 case SIGHUP: 1113 case SIGINT: 1114 case SIGQUIT: 1115 case SIGILL: 1116 case SIGABRT: 1117 case SIGFPE: 1118 case SIGBUS: 1119 case SIGSEGV: 1120 #ifdef SIGSYS 1121 case SIGSYS: 1122 #endif 1123 case SIGTERM: 1124 if (__kmp_debug_buf) { 1125 __kmp_dump_debug_buffer(); 1126 } 1127 KMP_MB(); // Flush all pending memory write invalidates. 1128 TCW_4(__kmp_global.g.g_abort, signo); 1129 KMP_MB(); // Flush all pending memory write invalidates. 1130 TCW_4(__kmp_global.g.g_done, TRUE); 1131 KMP_MB(); // Flush all pending memory write invalidates. 1132 break; 1133 default: 1134 #ifdef KMP_DEBUG 1135 __kmp_debug_printf("__kmp_team_handler: unknown signal type"); 1136 #endif 1137 break; 1138 } 1139 } 1140 } // __kmp_team_handler 1141 1142 static void __kmp_sigaction(int signum, const struct sigaction *act, 1143 struct sigaction *oldact) { 1144 int rc = sigaction(signum, act, oldact); 1145 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc); 1146 } 1147 1148 static void __kmp_install_one_handler(int sig, sig_func_t handler_func, 1149 int parallel_init) { 1150 KMP_MB(); // Flush all pending memory write invalidates. 1151 KB_TRACE(60, 1152 ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init)); 1153 if (parallel_init) { 1154 struct sigaction new_action; 1155 struct sigaction old_action; 1156 new_action.sa_handler = handler_func; 1157 new_action.sa_flags = 0; 1158 sigfillset(&new_action.sa_mask); 1159 __kmp_sigaction(sig, &new_action, &old_action); 1160 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) { 1161 sigaddset(&__kmp_sigset, sig); 1162 } else { 1163 // Restore/keep user's handler if one previously installed. 1164 __kmp_sigaction(sig, &old_action, NULL); 1165 } 1166 } else { 1167 // Save initial/system signal handlers to see if user handlers installed. 1168 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]); 1169 } 1170 KMP_MB(); // Flush all pending memory write invalidates. 1171 } // __kmp_install_one_handler 1172 1173 static void __kmp_remove_one_handler(int sig) { 1174 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig)); 1175 if (sigismember(&__kmp_sigset, sig)) { 1176 struct sigaction old; 1177 KMP_MB(); // Flush all pending memory write invalidates. 1178 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old); 1179 if ((old.sa_handler != __kmp_team_handler) && 1180 (old.sa_handler != __kmp_null_handler)) { 1181 // Restore the users signal handler. 1182 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, " 1183 "restoring: sig=%d\n", 1184 sig)); 1185 __kmp_sigaction(sig, &old, NULL); 1186 } 1187 sigdelset(&__kmp_sigset, sig); 1188 KMP_MB(); // Flush all pending memory write invalidates. 1189 } 1190 } // __kmp_remove_one_handler 1191 1192 void __kmp_install_signals(int parallel_init) { 1193 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init)); 1194 if (__kmp_handle_signals || !parallel_init) { 1195 // If ! parallel_init, we do not install handlers, just save original 1196 // handlers. Let us do it even __handle_signals is 0. 1197 sigemptyset(&__kmp_sigset); 1198 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init); 1199 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init); 1200 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init); 1201 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init); 1202 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init); 1203 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init); 1204 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init); 1205 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init); 1206 #ifdef SIGSYS 1207 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init); 1208 #endif // SIGSYS 1209 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init); 1210 #ifdef SIGPIPE 1211 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init); 1212 #endif // SIGPIPE 1213 } 1214 } // __kmp_install_signals 1215 1216 void __kmp_remove_signals(void) { 1217 int sig; 1218 KB_TRACE(10, ("__kmp_remove_signals()\n")); 1219 for (sig = 1; sig < NSIG; ++sig) { 1220 __kmp_remove_one_handler(sig); 1221 } 1222 } // __kmp_remove_signals 1223 1224 #endif // KMP_HANDLE_SIGNALS 1225 1226 void __kmp_enable(int new_state) { 1227 #ifdef KMP_CANCEL_THREADS 1228 int status, old_state; 1229 status = pthread_setcancelstate(new_state, &old_state); 1230 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); 1231 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE); 1232 #endif 1233 } 1234 1235 void __kmp_disable(int *old_state) { 1236 #ifdef KMP_CANCEL_THREADS 1237 int status; 1238 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state); 1239 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); 1240 #endif 1241 } 1242 1243 static void __kmp_atfork_prepare(void) { 1244 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); 1245 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); 1246 } 1247 1248 static void __kmp_atfork_parent(void) { 1249 __kmp_release_bootstrap_lock(&__kmp_initz_lock); 1250 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); 1251 } 1252 1253 /* Reset the library so execution in the child starts "all over again" with 1254 clean data structures in initial states. Don't worry about freeing memory 1255 allocated by parent, just abandon it to be safe. */ 1256 static void __kmp_atfork_child(void) { 1257 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); 1258 /* TODO make sure this is done right for nested/sibling */ 1259 // ATT: Memory leaks are here? TODO: Check it and fix. 1260 /* KMP_ASSERT( 0 ); */ 1261 1262 ++__kmp_fork_count; 1263 1264 #if KMP_AFFINITY_SUPPORTED 1265 #if KMP_OS_LINUX 1266 // reset the affinity in the child to the initial thread 1267 // affinity in the parent 1268 kmp_set_thread_affinity_mask_initial(); 1269 #endif 1270 // Set default not to bind threads tightly in the child (we’re expecting 1271 // over-subscription after the fork and this can improve things for 1272 // scripting languages that use OpenMP inside process-parallel code). 1273 __kmp_affinity_type = affinity_none; 1274 if (__kmp_nested_proc_bind.bind_types != NULL) { 1275 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false; 1276 } 1277 #endif // KMP_AFFINITY_SUPPORTED 1278 1279 __kmp_init_runtime = FALSE; 1280 #if KMP_USE_MONITOR 1281 __kmp_init_monitor = 0; 1282 #endif 1283 __kmp_init_parallel = FALSE; 1284 __kmp_init_middle = FALSE; 1285 __kmp_init_serial = FALSE; 1286 TCW_4(__kmp_init_gtid, FALSE); 1287 __kmp_init_common = FALSE; 1288 1289 TCW_4(__kmp_init_user_locks, FALSE); 1290 #if !KMP_USE_DYNAMIC_LOCK 1291 __kmp_user_lock_table.used = 1; 1292 __kmp_user_lock_table.allocated = 0; 1293 __kmp_user_lock_table.table = NULL; 1294 __kmp_lock_blocks = NULL; 1295 #endif 1296 1297 __kmp_all_nth = 0; 1298 TCW_4(__kmp_nth, 0); 1299 1300 __kmp_thread_pool = NULL; 1301 __kmp_thread_pool_insert_pt = NULL; 1302 __kmp_team_pool = NULL; 1303 1304 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate 1305 here so threadprivate doesn't use stale data */ 1306 KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n", 1307 __kmp_threadpriv_cache_list)); 1308 1309 while (__kmp_threadpriv_cache_list != NULL) { 1310 1311 if (*__kmp_threadpriv_cache_list->addr != NULL) { 1312 KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n", 1313 &(*__kmp_threadpriv_cache_list->addr))); 1314 1315 *__kmp_threadpriv_cache_list->addr = NULL; 1316 } 1317 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next; 1318 } 1319 1320 __kmp_init_runtime = FALSE; 1321 1322 /* reset statically initialized locks */ 1323 __kmp_init_bootstrap_lock(&__kmp_initz_lock); 1324 __kmp_init_bootstrap_lock(&__kmp_stdio_lock); 1325 __kmp_init_bootstrap_lock(&__kmp_console_lock); 1326 __kmp_init_bootstrap_lock(&__kmp_task_team_lock); 1327 1328 #if USE_ITT_BUILD 1329 __kmp_itt_reset(); // reset ITT's global state 1330 #endif /* USE_ITT_BUILD */ 1331 1332 /* This is necessary to make sure no stale data is left around */ 1333 /* AC: customers complain that we use unsafe routines in the atfork 1334 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen 1335 in dynamic_link when check the presence of shared tbbmalloc library. 1336 Suggestion is to make the library initialization lazier, similar 1337 to what done for __kmpc_begin(). */ 1338 // TODO: synchronize all static initializations with regular library 1339 // startup; look at kmp_global.cpp and etc. 1340 //__kmp_internal_begin (); 1341 } 1342 1343 void __kmp_register_atfork(void) { 1344 if (__kmp_need_register_atfork) { 1345 int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent, 1346 __kmp_atfork_child); 1347 KMP_CHECK_SYSFAIL("pthread_atfork", status); 1348 __kmp_need_register_atfork = FALSE; 1349 } 1350 } 1351 1352 void __kmp_suspend_initialize(void) { 1353 int status; 1354 status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr); 1355 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status); 1356 status = pthread_condattr_init(&__kmp_suspend_cond_attr); 1357 KMP_CHECK_SYSFAIL("pthread_condattr_init", status); 1358 } 1359 1360 void __kmp_suspend_initialize_thread(kmp_info_t *th) { 1361 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count); 1362 int old_value = KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init_count); 1363 int new_value = __kmp_fork_count + 1; 1364 // Return if already initialized 1365 if (old_value == new_value) 1366 return; 1367 // Wait, then return if being initialized 1368 if (old_value == -1 || 1369 !__kmp_atomic_compare_store(&th->th.th_suspend_init_count, old_value, 1370 -1)) { 1371 while (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init_count) != new_value) { 1372 KMP_CPU_PAUSE(); 1373 } 1374 } else { 1375 // Claim to be the initializer and do initializations 1376 int status; 1377 status = pthread_cond_init(&th->th.th_suspend_cv.c_cond, 1378 &__kmp_suspend_cond_attr); 1379 KMP_CHECK_SYSFAIL("pthread_cond_init", status); 1380 status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex, 1381 &__kmp_suspend_mutex_attr); 1382 KMP_CHECK_SYSFAIL("pthread_mutex_init", status); 1383 KMP_ATOMIC_ST_REL(&th->th.th_suspend_init_count, new_value); 1384 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count); 1385 } 1386 } 1387 1388 void __kmp_suspend_uninitialize_thread(kmp_info_t *th) { 1389 if (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init_count) > __kmp_fork_count) { 1390 /* this means we have initialize the suspension pthread objects for this 1391 thread in this instance of the process */ 1392 int status; 1393 1394 status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond); 1395 if (status != 0 && status != EBUSY) { 1396 KMP_SYSFAIL("pthread_cond_destroy", status); 1397 } 1398 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex); 1399 if (status != 0 && status != EBUSY) { 1400 KMP_SYSFAIL("pthread_mutex_destroy", status); 1401 } 1402 --th->th.th_suspend_init_count; 1403 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init_count) == 1404 __kmp_fork_count); 1405 } 1406 } 1407 1408 // return true if lock obtained, false otherwise 1409 int __kmp_try_suspend_mx(kmp_info_t *th) { 1410 return (pthread_mutex_trylock(&th->th.th_suspend_mx.m_mutex) == 0); 1411 } 1412 1413 void __kmp_lock_suspend_mx(kmp_info_t *th) { 1414 int status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); 1415 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); 1416 } 1417 1418 void __kmp_unlock_suspend_mx(kmp_info_t *th) { 1419 int status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1420 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1421 } 1422 1423 /* This routine puts the calling thread to sleep after setting the 1424 sleep bit for the indicated flag variable to true. */ 1425 template <class C> 1426 static inline void __kmp_suspend_template(int th_gtid, C *flag) { 1427 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend); 1428 kmp_info_t *th = __kmp_threads[th_gtid]; 1429 int status; 1430 typename C::flag_t old_spin; 1431 1432 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, 1433 flag->get())); 1434 1435 __kmp_suspend_initialize_thread(th); 1436 1437 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); 1438 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); 1439 1440 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n", 1441 th_gtid, flag->get())); 1442 1443 /* TODO: shouldn't this use release semantics to ensure that 1444 __kmp_suspend_initialize_thread gets called first? */ 1445 old_spin = flag->set_sleeping(); 1446 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME && 1447 __kmp_pause_status != kmp_soft_paused) { 1448 flag->unset_sleeping(); 1449 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1450 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1451 return; 1452 } 1453 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x," 1454 " was %x\n", 1455 th_gtid, flag->get(), flag->load(), old_spin)); 1456 1457 if (flag->done_check_val(old_spin)) { 1458 old_spin = flag->unset_sleeping(); 1459 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit " 1460 "for spin(%p)\n", 1461 th_gtid, flag->get())); 1462 } else { 1463 /* Encapsulate in a loop as the documentation states that this may 1464 "with low probability" return when the condition variable has 1465 not been signaled or broadcast */ 1466 int deactivated = FALSE; 1467 TCW_PTR(th->th.th_sleep_loc, (void *)flag); 1468 1469 while (flag->is_sleeping()) { 1470 #ifdef DEBUG_SUSPEND 1471 char buffer[128]; 1472 __kmp_suspend_count++; 1473 __kmp_print_cond(buffer, &th->th.th_suspend_cv); 1474 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, 1475 buffer); 1476 #endif 1477 // Mark the thread as no longer active (only in the first iteration of the 1478 // loop). 1479 if (!deactivated) { 1480 th->th.th_active = FALSE; 1481 if (th->th.th_active_in_pool) { 1482 th->th.th_active_in_pool = FALSE; 1483 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth); 1484 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0); 1485 } 1486 deactivated = TRUE; 1487 } 1488 1489 #if USE_SUSPEND_TIMEOUT 1490 struct timespec now; 1491 struct timeval tval; 1492 int msecs; 1493 1494 status = gettimeofday(&tval, NULL); 1495 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); 1496 TIMEVAL_TO_TIMESPEC(&tval, &now); 1497 1498 msecs = (4 * __kmp_dflt_blocktime) + 200; 1499 now.tv_sec += msecs / 1000; 1500 now.tv_nsec += (msecs % 1000) * 1000; 1501 1502 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform " 1503 "pthread_cond_timedwait\n", 1504 th_gtid)); 1505 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond, 1506 &th->th.th_suspend_mx.m_mutex, &now); 1507 #else 1508 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform" 1509 " pthread_cond_wait\n", 1510 th_gtid)); 1511 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond, 1512 &th->th.th_suspend_mx.m_mutex); 1513 #endif 1514 1515 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) { 1516 KMP_SYSFAIL("pthread_cond_wait", status); 1517 } 1518 #ifdef KMP_DEBUG 1519 if (status == ETIMEDOUT) { 1520 if (flag->is_sleeping()) { 1521 KF_TRACE(100, 1522 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid)); 1523 } else { 1524 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit " 1525 "not set!\n", 1526 th_gtid)); 1527 } 1528 } else if (flag->is_sleeping()) { 1529 KF_TRACE(100, 1530 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid)); 1531 } 1532 #endif 1533 } // while 1534 1535 // Mark the thread as active again (if it was previous marked as inactive) 1536 if (deactivated) { 1537 th->th.th_active = TRUE; 1538 if (TCR_4(th->th.th_in_pool)) { 1539 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth); 1540 th->th.th_active_in_pool = TRUE; 1541 } 1542 } 1543 } 1544 #ifdef DEBUG_SUSPEND 1545 { 1546 char buffer[128]; 1547 __kmp_print_cond(buffer, &th->th.th_suspend_cv); 1548 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, 1549 buffer); 1550 } 1551 #endif 1552 1553 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1554 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1555 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid)); 1556 } 1557 1558 void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) { 1559 __kmp_suspend_template(th_gtid, flag); 1560 } 1561 void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) { 1562 __kmp_suspend_template(th_gtid, flag); 1563 } 1564 void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) { 1565 __kmp_suspend_template(th_gtid, flag); 1566 } 1567 1568 /* This routine signals the thread specified by target_gtid to wake up 1569 after setting the sleep bit indicated by the flag argument to FALSE. 1570 The target thread must already have called __kmp_suspend_template() */ 1571 template <class C> 1572 static inline void __kmp_resume_template(int target_gtid, C *flag) { 1573 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume); 1574 kmp_info_t *th = __kmp_threads[target_gtid]; 1575 int status; 1576 1577 #ifdef KMP_DEBUG 1578 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1; 1579 #endif 1580 1581 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", 1582 gtid, target_gtid)); 1583 KMP_DEBUG_ASSERT(gtid != target_gtid); 1584 1585 __kmp_suspend_initialize_thread(th); 1586 1587 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); 1588 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); 1589 1590 if (!flag) { // coming from __kmp_null_resume_wrapper 1591 flag = (C *)CCAST(void *, th->th.th_sleep_loc); 1592 } 1593 1594 // First, check if the flag is null or its type has changed. If so, someone 1595 // else woke it up. 1596 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type 1597 // simply shows what 1598 // flag was cast to 1599 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " 1600 "awake: flag(%p)\n", 1601 gtid, target_gtid, NULL)); 1602 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1603 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1604 return; 1605 } else { // if multiple threads are sleeping, flag should be internally 1606 // referring to a specific thread here 1607 typename C::flag_t old_spin = flag->unset_sleeping(); 1608 if (!flag->is_sleeping_val(old_spin)) { 1609 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " 1610 "awake: flag(%p): " 1611 "%u => %u\n", 1612 gtid, target_gtid, flag->get(), old_spin, flag->load())); 1613 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1614 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1615 return; 1616 } 1617 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset " 1618 "sleep bit for flag's loc(%p): " 1619 "%u => %u\n", 1620 gtid, target_gtid, flag->get(), old_spin, flag->load())); 1621 } 1622 TCW_PTR(th->th.th_sleep_loc, NULL); 1623 1624 #ifdef DEBUG_SUSPEND 1625 { 1626 char buffer[128]; 1627 __kmp_print_cond(buffer, &th->th.th_suspend_cv); 1628 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, 1629 target_gtid, buffer); 1630 } 1631 #endif 1632 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond); 1633 KMP_CHECK_SYSFAIL("pthread_cond_signal", status); 1634 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); 1635 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1636 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up" 1637 " for T#%d\n", 1638 gtid, target_gtid)); 1639 } 1640 1641 void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) { 1642 __kmp_resume_template(target_gtid, flag); 1643 } 1644 void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) { 1645 __kmp_resume_template(target_gtid, flag); 1646 } 1647 void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) { 1648 __kmp_resume_template(target_gtid, flag); 1649 } 1650 1651 #if KMP_USE_MONITOR 1652 void __kmp_resume_monitor() { 1653 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume); 1654 int status; 1655 #ifdef KMP_DEBUG 1656 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1; 1657 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid, 1658 KMP_GTID_MONITOR)); 1659 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR); 1660 #endif 1661 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex); 1662 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); 1663 #ifdef DEBUG_SUSPEND 1664 { 1665 char buffer[128]; 1666 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond); 1667 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, 1668 KMP_GTID_MONITOR, buffer); 1669 } 1670 #endif 1671 status = pthread_cond_signal(&__kmp_wait_cv.c_cond); 1672 KMP_CHECK_SYSFAIL("pthread_cond_signal", status); 1673 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex); 1674 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); 1675 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up" 1676 " for T#%d\n", 1677 gtid, KMP_GTID_MONITOR)); 1678 } 1679 #endif // KMP_USE_MONITOR 1680 1681 void __kmp_yield() { sched_yield(); } 1682 1683 void __kmp_gtid_set_specific(int gtid) { 1684 if (__kmp_init_gtid) { 1685 int status; 1686 status = pthread_setspecific(__kmp_gtid_threadprivate_key, 1687 (void *)(intptr_t)(gtid + 1)); 1688 KMP_CHECK_SYSFAIL("pthread_setspecific", status); 1689 } else { 1690 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n")); 1691 } 1692 } 1693 1694 int __kmp_gtid_get_specific() { 1695 int gtid; 1696 if (!__kmp_init_gtid) { 1697 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning " 1698 "KMP_GTID_SHUTDOWN\n")); 1699 return KMP_GTID_SHUTDOWN; 1700 } 1701 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key); 1702 if (gtid == 0) { 1703 gtid = KMP_GTID_DNE; 1704 } else { 1705 gtid--; 1706 } 1707 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n", 1708 __kmp_gtid_threadprivate_key, gtid)); 1709 return gtid; 1710 } 1711 1712 double __kmp_read_cpu_time(void) { 1713 /*clock_t t;*/ 1714 struct tms buffer; 1715 1716 /*t =*/times(&buffer); 1717 1718 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC; 1719 } 1720 1721 int __kmp_read_system_info(struct kmp_sys_info *info) { 1722 int status; 1723 struct rusage r_usage; 1724 1725 memset(info, 0, sizeof(*info)); 1726 1727 status = getrusage(RUSAGE_SELF, &r_usage); 1728 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status); 1729 1730 // The maximum resident set size utilized (in kilobytes) 1731 info->maxrss = r_usage.ru_maxrss; 1732 // The number of page faults serviced without any I/O 1733 info->minflt = r_usage.ru_minflt; 1734 // The number of page faults serviced that required I/O 1735 info->majflt = r_usage.ru_majflt; 1736 // The number of times a process was "swapped" out of memory 1737 info->nswap = r_usage.ru_nswap; 1738 // The number of times the file system had to perform input 1739 info->inblock = r_usage.ru_inblock; 1740 // The number of times the file system had to perform output 1741 info->oublock = r_usage.ru_oublock; 1742 // The number of times a context switch was voluntarily 1743 info->nvcsw = r_usage.ru_nvcsw; 1744 // The number of times a context switch was forced 1745 info->nivcsw = r_usage.ru_nivcsw; 1746 1747 return (status != 0); 1748 } 1749 1750 void __kmp_read_system_time(double *delta) { 1751 double t_ns; 1752 struct timeval tval; 1753 struct timespec stop; 1754 int status; 1755 1756 status = gettimeofday(&tval, NULL); 1757 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); 1758 TIMEVAL_TO_TIMESPEC(&tval, &stop); 1759 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start); 1760 *delta = (t_ns * 1e-9); 1761 } 1762 1763 void __kmp_clear_system_time(void) { 1764 struct timeval tval; 1765 int status; 1766 status = gettimeofday(&tval, NULL); 1767 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); 1768 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start); 1769 } 1770 1771 static int __kmp_get_xproc(void) { 1772 1773 int r = 0; 1774 1775 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ 1776 KMP_OS_OPENBSD || KMP_OS_HURD 1777 1778 r = sysconf(_SC_NPROCESSORS_ONLN); 1779 1780 #elif KMP_OS_DARWIN 1781 1782 // Bug C77011 High "OpenMP Threads and number of active cores". 1783 1784 // Find the number of available CPUs. 1785 kern_return_t rc; 1786 host_basic_info_data_t info; 1787 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT; 1788 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num); 1789 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) { 1790 // Cannot use KA_TRACE() here because this code works before trace support 1791 // is initialized. 1792 r = info.avail_cpus; 1793 } else { 1794 KMP_WARNING(CantGetNumAvailCPU); 1795 KMP_INFORM(AssumedNumCPU); 1796 } 1797 1798 #else 1799 1800 #error "Unknown or unsupported OS." 1801 1802 #endif 1803 1804 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */ 1805 1806 } // __kmp_get_xproc 1807 1808 int __kmp_read_from_file(char const *path, char const *format, ...) { 1809 int result; 1810 va_list args; 1811 1812 va_start(args, format); 1813 FILE *f = fopen(path, "rb"); 1814 if (f == NULL) 1815 return 0; 1816 result = vfscanf(f, format, args); 1817 fclose(f); 1818 1819 return result; 1820 } 1821 1822 void __kmp_runtime_initialize(void) { 1823 int status; 1824 pthread_mutexattr_t mutex_attr; 1825 pthread_condattr_t cond_attr; 1826 1827 if (__kmp_init_runtime) { 1828 return; 1829 } 1830 1831 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) 1832 if (!__kmp_cpuinfo.initialized) { 1833 __kmp_query_cpuid(&__kmp_cpuinfo); 1834 } 1835 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ 1836 1837 __kmp_xproc = __kmp_get_xproc(); 1838 1839 #if ! KMP_32_BIT_ARCH 1840 struct rlimit rlim; 1841 // read stack size of calling thread, save it as default for worker threads; 1842 // this should be done before reading environment variables 1843 status = getrlimit(RLIMIT_STACK, &rlim); 1844 if (status == 0) { // success? 1845 __kmp_stksize = rlim.rlim_cur; 1846 __kmp_check_stksize(&__kmp_stksize); // check value and adjust if needed 1847 } 1848 #endif /* KMP_32_BIT_ARCH */ 1849 1850 if (sysconf(_SC_THREADS)) { 1851 1852 /* Query the maximum number of threads */ 1853 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX); 1854 if (__kmp_sys_max_nth == -1) { 1855 /* Unlimited threads for NPTL */ 1856 __kmp_sys_max_nth = INT_MAX; 1857 } else if (__kmp_sys_max_nth <= 1) { 1858 /* Can't tell, just use PTHREAD_THREADS_MAX */ 1859 __kmp_sys_max_nth = KMP_MAX_NTH; 1860 } 1861 1862 /* Query the minimum stack size */ 1863 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN); 1864 if (__kmp_sys_min_stksize <= 1) { 1865 __kmp_sys_min_stksize = KMP_MIN_STKSIZE; 1866 } 1867 } 1868 1869 /* Set up minimum number of threads to switch to TLS gtid */ 1870 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN; 1871 1872 status = pthread_key_create(&__kmp_gtid_threadprivate_key, 1873 __kmp_internal_end_dest); 1874 KMP_CHECK_SYSFAIL("pthread_key_create", status); 1875 status = pthread_mutexattr_init(&mutex_attr); 1876 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status); 1877 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr); 1878 KMP_CHECK_SYSFAIL("pthread_mutex_init", status); 1879 status = pthread_condattr_init(&cond_attr); 1880 KMP_CHECK_SYSFAIL("pthread_condattr_init", status); 1881 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr); 1882 KMP_CHECK_SYSFAIL("pthread_cond_init", status); 1883 #if USE_ITT_BUILD 1884 __kmp_itt_initialize(); 1885 #endif /* USE_ITT_BUILD */ 1886 1887 __kmp_init_runtime = TRUE; 1888 } 1889 1890 void __kmp_runtime_destroy(void) { 1891 int status; 1892 1893 if (!__kmp_init_runtime) { 1894 return; // Nothing to do. 1895 } 1896 1897 #if USE_ITT_BUILD 1898 __kmp_itt_destroy(); 1899 #endif /* USE_ITT_BUILD */ 1900 1901 status = pthread_key_delete(__kmp_gtid_threadprivate_key); 1902 KMP_CHECK_SYSFAIL("pthread_key_delete", status); 1903 1904 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex); 1905 if (status != 0 && status != EBUSY) { 1906 KMP_SYSFAIL("pthread_mutex_destroy", status); 1907 } 1908 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond); 1909 if (status != 0 && status != EBUSY) { 1910 KMP_SYSFAIL("pthread_cond_destroy", status); 1911 } 1912 #if KMP_AFFINITY_SUPPORTED 1913 __kmp_affinity_uninitialize(); 1914 #endif 1915 1916 __kmp_init_runtime = FALSE; 1917 } 1918 1919 /* Put the thread to sleep for a time period */ 1920 /* NOTE: not currently used anywhere */ 1921 void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); } 1922 1923 /* Calculate the elapsed wall clock time for the user */ 1924 void __kmp_elapsed(double *t) { 1925 int status; 1926 #ifdef FIX_SGI_CLOCK 1927 struct timespec ts; 1928 1929 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); 1930 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status); 1931 *t = 1932 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec; 1933 #else 1934 struct timeval tv; 1935 1936 status = gettimeofday(&tv, NULL); 1937 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); 1938 *t = 1939 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec; 1940 #endif 1941 } 1942 1943 /* Calculate the elapsed wall clock tick for the user */ 1944 void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; } 1945 1946 /* Return the current time stamp in nsec */ 1947 kmp_uint64 __kmp_now_nsec() { 1948 struct timeval t; 1949 gettimeofday(&t, NULL); 1950 kmp_uint64 nsec = (kmp_uint64)KMP_NSEC_PER_SEC * (kmp_uint64)t.tv_sec + 1951 (kmp_uint64)1000 * (kmp_uint64)t.tv_usec; 1952 return nsec; 1953 } 1954 1955 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 1956 /* Measure clock ticks per millisecond */ 1957 void __kmp_initialize_system_tick() { 1958 kmp_uint64 now, nsec2, diff; 1959 kmp_uint64 delay = 100000; // 50~100 usec on most machines. 1960 kmp_uint64 nsec = __kmp_now_nsec(); 1961 kmp_uint64 goal = __kmp_hardware_timestamp() + delay; 1962 while ((now = __kmp_hardware_timestamp()) < goal) 1963 ; 1964 nsec2 = __kmp_now_nsec(); 1965 diff = nsec2 - nsec; 1966 if (diff > 0) { 1967 kmp_uint64 tpms = (kmp_uint64)(1e6 * (delay + (now - goal)) / diff); 1968 if (tpms > 0) 1969 __kmp_ticks_per_msec = tpms; 1970 } 1971 } 1972 #endif 1973 1974 /* Determine whether the given address is mapped into the current address 1975 space. */ 1976 1977 int __kmp_is_address_mapped(void *addr) { 1978 1979 int found = 0; 1980 int rc; 1981 1982 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD 1983 1984 /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address 1985 ranges mapped into the address space. */ 1986 1987 char *name = __kmp_str_format("/proc/%d/maps", getpid()); 1988 FILE *file = NULL; 1989 1990 file = fopen(name, "r"); 1991 KMP_ASSERT(file != NULL); 1992 1993 for (;;) { 1994 1995 void *beginning = NULL; 1996 void *ending = NULL; 1997 char perms[5]; 1998 1999 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms); 2000 if (rc == EOF) { 2001 break; 2002 } 2003 KMP_ASSERT(rc == 3 && 2004 KMP_STRLEN(perms) == 4); // Make sure all fields are read. 2005 2006 // Ending address is not included in the region, but beginning is. 2007 if ((addr >= beginning) && (addr < ending)) { 2008 perms[2] = 0; // 3th and 4th character does not matter. 2009 if (strcmp(perms, "rw") == 0) { 2010 // Memory we are looking for should be readable and writable. 2011 found = 1; 2012 } 2013 break; 2014 } 2015 } 2016 2017 // Free resources. 2018 fclose(file); 2019 KMP_INTERNAL_FREE(name); 2020 2021 #elif KMP_OS_DARWIN 2022 2023 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory 2024 using vm interface. */ 2025 2026 int buffer; 2027 vm_size_t count; 2028 rc = vm_read_overwrite( 2029 mach_task_self(), // Task to read memory of. 2030 (vm_address_t)(addr), // Address to read from. 2031 1, // Number of bytes to be read. 2032 (vm_address_t)(&buffer), // Address of buffer to save read bytes in. 2033 &count // Address of var to save number of read bytes in. 2034 ); 2035 if (rc == 0) { 2036 // Memory successfully read. 2037 found = 1; 2038 } 2039 2040 #elif KMP_OS_NETBSD 2041 2042 int mib[5]; 2043 mib[0] = CTL_VM; 2044 mib[1] = VM_PROC; 2045 mib[2] = VM_PROC_MAP; 2046 mib[3] = getpid(); 2047 mib[4] = sizeof(struct kinfo_vmentry); 2048 2049 size_t size; 2050 rc = sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0); 2051 KMP_ASSERT(!rc); 2052 KMP_ASSERT(size); 2053 2054 size = size * 4 / 3; 2055 struct kinfo_vmentry *kiv = (struct kinfo_vmentry *)KMP_INTERNAL_MALLOC(size); 2056 KMP_ASSERT(kiv); 2057 2058 rc = sysctl(mib, __arraycount(mib), kiv, &size, NULL, 0); 2059 KMP_ASSERT(!rc); 2060 KMP_ASSERT(size); 2061 2062 for (size_t i = 0; i < size; i++) { 2063 if (kiv[i].kve_start >= (uint64_t)addr && 2064 kiv[i].kve_end <= (uint64_t)addr) { 2065 found = 1; 2066 break; 2067 } 2068 } 2069 KMP_INTERNAL_FREE(kiv); 2070 #elif KMP_OS_DRAGONFLY || KMP_OS_OPENBSD 2071 2072 // FIXME(DragonFly, OpenBSD): Implement this 2073 found = 1; 2074 2075 #else 2076 2077 #error "Unknown or unsupported OS" 2078 2079 #endif 2080 2081 return found; 2082 2083 } // __kmp_is_address_mapped 2084 2085 #ifdef USE_LOAD_BALANCE 2086 2087 #if KMP_OS_DARWIN || KMP_OS_NETBSD 2088 2089 // The function returns the rounded value of the system load average 2090 // during given time interval which depends on the value of 2091 // __kmp_load_balance_interval variable (default is 60 sec, other values 2092 // may be 300 sec or 900 sec). 2093 // It returns -1 in case of error. 2094 int __kmp_get_load_balance(int max) { 2095 double averages[3]; 2096 int ret_avg = 0; 2097 2098 int res = getloadavg(averages, 3); 2099 2100 // Check __kmp_load_balance_interval to determine which of averages to use. 2101 // getloadavg() may return the number of samples less than requested that is 2102 // less than 3. 2103 if (__kmp_load_balance_interval < 180 && (res >= 1)) { 2104 ret_avg = averages[0]; // 1 min 2105 } else if ((__kmp_load_balance_interval >= 180 && 2106 __kmp_load_balance_interval < 600) && 2107 (res >= 2)) { 2108 ret_avg = averages[1]; // 5 min 2109 } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) { 2110 ret_avg = averages[2]; // 15 min 2111 } else { // Error occurred 2112 return -1; 2113 } 2114 2115 return ret_avg; 2116 } 2117 2118 #else // Linux* OS 2119 2120 // The fuction returns number of running (not sleeping) threads, or -1 in case 2121 // of error. Error could be reported if Linux* OS kernel too old (without 2122 // "/proc" support). Counting running threads stops if max running threads 2123 // encountered. 2124 int __kmp_get_load_balance(int max) { 2125 static int permanent_error = 0; 2126 static int glb_running_threads = 0; // Saved count of the running threads for 2127 // the thread balance algortihm 2128 static double glb_call_time = 0; /* Thread balance algorithm call time */ 2129 2130 int running_threads = 0; // Number of running threads in the system. 2131 2132 DIR *proc_dir = NULL; // Handle of "/proc/" directory. 2133 struct dirent *proc_entry = NULL; 2134 2135 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path. 2136 DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory. 2137 struct dirent *task_entry = NULL; 2138 int task_path_fixed_len; 2139 2140 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path. 2141 int stat_file = -1; 2142 int stat_path_fixed_len; 2143 2144 int total_processes = 0; // Total number of processes in system. 2145 int total_threads = 0; // Total number of threads in system. 2146 2147 double call_time = 0.0; 2148 2149 __kmp_str_buf_init(&task_path); 2150 __kmp_str_buf_init(&stat_path); 2151 2152 __kmp_elapsed(&call_time); 2153 2154 if (glb_call_time && 2155 (call_time - glb_call_time < __kmp_load_balance_interval)) { 2156 running_threads = glb_running_threads; 2157 goto finish; 2158 } 2159 2160 glb_call_time = call_time; 2161 2162 // Do not spend time on scanning "/proc/" if we have a permanent error. 2163 if (permanent_error) { 2164 running_threads = -1; 2165 goto finish; 2166 } 2167 2168 if (max <= 0) { 2169 max = INT_MAX; 2170 } 2171 2172 // Open "/proc/" directory. 2173 proc_dir = opendir("/proc"); 2174 if (proc_dir == NULL) { 2175 // Cannot open "/prroc/". Probably the kernel does not support it. Return an 2176 // error now and in subsequent calls. 2177 running_threads = -1; 2178 permanent_error = 1; 2179 goto finish; 2180 } 2181 2182 // Initialize fixed part of task_path. This part will not change. 2183 __kmp_str_buf_cat(&task_path, "/proc/", 6); 2184 task_path_fixed_len = task_path.used; // Remember number of used characters. 2185 2186 proc_entry = readdir(proc_dir); 2187 while (proc_entry != NULL) { 2188 // Proc entry is a directory and name starts with a digit. Assume it is a 2189 // process' directory. 2190 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) { 2191 2192 ++total_processes; 2193 // Make sure init process is the very first in "/proc", so we can replace 2194 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 2195 // 1. We are going to check that total_processes == 1 => d_name == "1" is 2196 // true (where "=>" is implication). Since C++ does not have => operator, 2197 // let us replace it with its equivalent: a => b == ! a || b. 2198 KMP_DEBUG_ASSERT(total_processes != 1 || 2199 strcmp(proc_entry->d_name, "1") == 0); 2200 2201 // Construct task_path. 2202 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/". 2203 __kmp_str_buf_cat(&task_path, proc_entry->d_name, 2204 KMP_STRLEN(proc_entry->d_name)); 2205 __kmp_str_buf_cat(&task_path, "/task", 5); 2206 2207 task_dir = opendir(task_path.str); 2208 if (task_dir == NULL) { 2209 // Process can finish between reading "/proc/" directory entry and 2210 // opening process' "task/" directory. So, in general case we should not 2211 // complain, but have to skip this process and read the next one. But on 2212 // systems with no "task/" support we will spend lot of time to scan 2213 // "/proc/" tree again and again without any benefit. "init" process 2214 // (its pid is 1) should exist always, so, if we cannot open 2215 // "/proc/1/task/" directory, it means "task/" is not supported by 2216 // kernel. Report an error now and in the future. 2217 if (strcmp(proc_entry->d_name, "1") == 0) { 2218 running_threads = -1; 2219 permanent_error = 1; 2220 goto finish; 2221 } 2222 } else { 2223 // Construct fixed part of stat file path. 2224 __kmp_str_buf_clear(&stat_path); 2225 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used); 2226 __kmp_str_buf_cat(&stat_path, "/", 1); 2227 stat_path_fixed_len = stat_path.used; 2228 2229 task_entry = readdir(task_dir); 2230 while (task_entry != NULL) { 2231 // It is a directory and name starts with a digit. 2232 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) { 2233 ++total_threads; 2234 2235 // Consruct complete stat file path. Easiest way would be: 2236 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, 2237 // task_entry->d_name ); 2238 // but seriae of __kmp_str_buf_cat works a bit faster. 2239 stat_path.used = 2240 stat_path_fixed_len; // Reset stat path to its fixed part. 2241 __kmp_str_buf_cat(&stat_path, task_entry->d_name, 2242 KMP_STRLEN(task_entry->d_name)); 2243 __kmp_str_buf_cat(&stat_path, "/stat", 5); 2244 2245 // Note: Low-level API (open/read/close) is used. High-level API 2246 // (fopen/fclose) works ~ 30 % slower. 2247 stat_file = open(stat_path.str, O_RDONLY); 2248 if (stat_file == -1) { 2249 // We cannot report an error because task (thread) can terminate 2250 // just before reading this file. 2251 } else { 2252 /* Content of "stat" file looks like: 2253 24285 (program) S ... 2254 2255 It is a single line (if program name does not include funny 2256 symbols). First number is a thread id, then name of executable 2257 file name in paretheses, then state of the thread. We need just 2258 thread state. 2259 2260 Good news: Length of program name is 15 characters max. Longer 2261 names are truncated. 2262 2263 Thus, we need rather short buffer: 15 chars for program name + 2264 2 parenthesis, + 3 spaces + ~7 digits of pid = 37. 2265 2266 Bad news: Program name may contain special symbols like space, 2267 closing parenthesis, or even new line. This makes parsing 2268 "stat" file not 100 % reliable. In case of fanny program names 2269 parsing may fail (report incorrect thread state). 2270 2271 Parsing "status" file looks more promissing (due to different 2272 file structure and escaping special symbols) but reading and 2273 parsing of "status" file works slower. 2274 -- ln 2275 */ 2276 char buffer[65]; 2277 int len; 2278 len = read(stat_file, buffer, sizeof(buffer) - 1); 2279 if (len >= 0) { 2280 buffer[len] = 0; 2281 // Using scanf: 2282 // sscanf( buffer, "%*d (%*s) %c ", & state ); 2283 // looks very nice, but searching for a closing parenthesis 2284 // works a bit faster. 2285 char *close_parent = strstr(buffer, ") "); 2286 if (close_parent != NULL) { 2287 char state = *(close_parent + 2); 2288 if (state == 'R') { 2289 ++running_threads; 2290 if (running_threads >= max) { 2291 goto finish; 2292 } 2293 } 2294 } 2295 } 2296 close(stat_file); 2297 stat_file = -1; 2298 } 2299 } 2300 task_entry = readdir(task_dir); 2301 } 2302 closedir(task_dir); 2303 task_dir = NULL; 2304 } 2305 } 2306 proc_entry = readdir(proc_dir); 2307 } 2308 2309 // There _might_ be a timing hole where the thread executing this 2310 // code get skipped in the load balance, and running_threads is 0. 2311 // Assert in the debug builds only!!! 2312 KMP_DEBUG_ASSERT(running_threads > 0); 2313 if (running_threads <= 0) { 2314 running_threads = 1; 2315 } 2316 2317 finish: // Clean up and exit. 2318 if (proc_dir != NULL) { 2319 closedir(proc_dir); 2320 } 2321 __kmp_str_buf_free(&task_path); 2322 if (task_dir != NULL) { 2323 closedir(task_dir); 2324 } 2325 __kmp_str_buf_free(&stat_path); 2326 if (stat_file != -1) { 2327 close(stat_file); 2328 } 2329 2330 glb_running_threads = running_threads; 2331 2332 return running_threads; 2333 2334 } // __kmp_get_load_balance 2335 2336 #endif // KMP_OS_DARWIN 2337 2338 #endif // USE_LOAD_BALANCE 2339 2340 #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \ 2341 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || \ 2342 KMP_ARCH_PPC64 || KMP_ARCH_RISCV64) 2343 2344 // we really only need the case with 1 argument, because CLANG always build 2345 // a struct of pointers to shared variables referenced in the outlined function 2346 int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc, 2347 void *p_argv[] 2348 #if OMPT_SUPPORT 2349 , 2350 void **exit_frame_ptr 2351 #endif 2352 ) { 2353 #if OMPT_SUPPORT 2354 *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0); 2355 #endif 2356 2357 switch (argc) { 2358 default: 2359 fprintf(stderr, "Too many args to microtask: %d!\n", argc); 2360 fflush(stderr); 2361 exit(-1); 2362 case 0: 2363 (*pkfn)(>id, &tid); 2364 break; 2365 case 1: 2366 (*pkfn)(>id, &tid, p_argv[0]); 2367 break; 2368 case 2: 2369 (*pkfn)(>id, &tid, p_argv[0], p_argv[1]); 2370 break; 2371 case 3: 2372 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]); 2373 break; 2374 case 4: 2375 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]); 2376 break; 2377 case 5: 2378 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]); 2379 break; 2380 case 6: 2381 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2382 p_argv[5]); 2383 break; 2384 case 7: 2385 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2386 p_argv[5], p_argv[6]); 2387 break; 2388 case 8: 2389 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2390 p_argv[5], p_argv[6], p_argv[7]); 2391 break; 2392 case 9: 2393 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2394 p_argv[5], p_argv[6], p_argv[7], p_argv[8]); 2395 break; 2396 case 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]); 2399 break; 2400 case 11: 2401 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2402 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]); 2403 break; 2404 case 12: 2405 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2406 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], 2407 p_argv[11]); 2408 break; 2409 case 13: 2410 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2411 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], 2412 p_argv[11], p_argv[12]); 2413 break; 2414 case 14: 2415 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2416 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], 2417 p_argv[11], p_argv[12], p_argv[13]); 2418 break; 2419 case 15: 2420 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], 2421 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], 2422 p_argv[11], p_argv[12], p_argv[13], p_argv[14]); 2423 break; 2424 } 2425 2426 return 1; 2427 } 2428 2429 #endif 2430 2431 // end of file // 2432