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