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