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