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