proc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (C) 2015-2022 Free Software Foundation, Inc.
  2. Contributed by Sebastian Huber <sebastian.huber@embedded-brains.de>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file contains RTEMS specific routines related to counting
  21. online processors and dynamic load balancing. */
  22. #include "libgomp.h"
  23. #include "pool.h"
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. struct gomp_thread_pool_reservoir **gomp_thread_pool_reservoirs;
  29. __thread struct gomp_tls_rtems_data gomp_tls_rtems_data;
  30. static void
  31. allocate_thread_pool_reservoirs (void)
  32. {
  33. struct gomp_thread_pool_reservoir **reservoirs;
  34. size_t size = _Sched_Count () * sizeof (*reservoirs);
  35. reservoirs = gomp_malloc (size);
  36. gomp_thread_pool_reservoirs = reservoirs;
  37. memset (reservoirs, 0, size);
  38. }
  39. static void
  40. allocate_thread_pool_reservoir (unsigned long count, unsigned long priority,
  41. unsigned long scheduler)
  42. {
  43. struct gomp_thread_pool_reservoir *res;
  44. struct gomp_thread_pool *pools;
  45. unsigned long i;
  46. size_t size;
  47. res = gomp_thread_pool_reservoirs[scheduler];
  48. if (res != NULL)
  49. gomp_fatal ("Multiple thread pool reservoir initialization");
  50. size = sizeof (*res) + count * (sizeof(pools) + sizeof(*pools));
  51. pools = gomp_malloc (size);
  52. memset (pools, 0, size);
  53. res = (struct gomp_thread_pool_reservoir *) (pools + count);
  54. res->index = count;
  55. res->priority = priority;
  56. gomp_sem_init (&res->available, count);
  57. pthread_spin_init (&res->lock, PTHREAD_PROCESS_PRIVATE);
  58. for (i = 0; i < count; ++i)
  59. res->pools[i] = &pools[i];
  60. gomp_thread_pool_reservoirs[scheduler] = res;
  61. }
  62. static char *
  63. parse_thread_pools (char *env, unsigned long *count, unsigned long *priority,
  64. unsigned long *scheduler)
  65. {
  66. size_t len;
  67. int i;
  68. char *end;
  69. if (*env == ':')
  70. ++env;
  71. errno = 0;
  72. *count = strtoul (env, &end, 10);
  73. if (errno != 0 || end == env)
  74. gomp_fatal ("Invalid thread pool count");
  75. env = end;
  76. if (*env == '$')
  77. {
  78. ++env;
  79. errno = 0;
  80. *priority = strtoul (env, &end, 10);
  81. if (errno != 0 || end == env)
  82. gomp_fatal ("Invalid thread pool priority");
  83. env = end;
  84. }
  85. else
  86. *priority = -1;
  87. if (*env != '@')
  88. gomp_fatal ("Invalid thread pool scheduler prefix");
  89. ++env;
  90. len = 0;
  91. while (env[len] != '\0' && env[len] != ':')
  92. ++len;
  93. i = _Sched_Name_to_index (env, len);
  94. if (i < 0)
  95. gomp_fatal ("Invalid thread pool scheduler");
  96. *scheduler = i;
  97. env += len;
  98. return env;
  99. }
  100. static void
  101. init_thread_pool_reservoirs (void)
  102. {
  103. char *env = getenv ("GOMP_RTEMS_THREAD_POOLS");
  104. if (env != NULL)
  105. {
  106. allocate_thread_pool_reservoirs ();
  107. while (*env != '\0')
  108. {
  109. unsigned long count;
  110. unsigned long priority;
  111. unsigned long scheduler;
  112. env = parse_thread_pools (env, &count, &priority, &scheduler);
  113. allocate_thread_pool_reservoir (count, priority, scheduler);
  114. }
  115. }
  116. }
  117. void
  118. gomp_init_num_threads (void)
  119. {
  120. gomp_global_icv.nthreads_var = omp_get_num_procs();
  121. init_thread_pool_reservoirs ();
  122. }
  123. unsigned
  124. gomp_dynamic_max_threads (void)
  125. {
  126. unsigned n_onln = (unsigned) omp_get_num_procs();
  127. unsigned nthreads_var = gomp_icv (false)->nthreads_var;
  128. if (n_onln > nthreads_var)
  129. return nthreads_var;
  130. else
  131. return n_onln;
  132. }
  133. int
  134. omp_get_num_procs (void)
  135. {
  136. return sysconf (_SC_NPROCESSORS_ONLN);
  137. }
  138. ialias (omp_get_num_procs)