nproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Detect the number of processors.
  2. Copyright (C) 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Glen Lenker and Bruno Haible. */
  14. #include <config.h>
  15. #include "nproc.h"
  16. #include <limits.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #if HAVE_PTHREAD_GETAFFINITY_NP && 0
  20. # include <pthread.h>
  21. # include <sched.h>
  22. #endif
  23. #if HAVE_SCHED_GETAFFINITY_LIKE_GLIBC || HAVE_SCHED_GETAFFINITY_NP
  24. # include <sched.h>
  25. #endif
  26. #include <sys/types.h>
  27. #if HAVE_SYS_PSTAT_H
  28. # include <sys/pstat.h>
  29. #endif
  30. #if HAVE_SYS_SYSMP_H
  31. # include <sys/sysmp.h>
  32. #endif
  33. #if HAVE_SYS_PARAM_H
  34. # include <sys/param.h>
  35. #endif
  36. #if HAVE_SYS_SYSCTL_H && ! defined __GLIBC__
  37. # include <sys/sysctl.h>
  38. #endif
  39. #if defined _WIN32 && ! defined __CYGWIN__
  40. # define WIN32_LEAN_AND_MEAN
  41. # include <windows.h>
  42. #endif
  43. #include "c-ctype.h"
  44. #include "minmax.h"
  45. #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
  46. /* Return the number of processors available to the current process, based
  47. on a modern system call that returns the "affinity" between the current
  48. process and each CPU. Return 0 if unknown or if such a system call does
  49. not exist. */
  50. static unsigned long
  51. num_processors_via_affinity_mask (void)
  52. {
  53. /* glibc >= 2.3.3 with NPTL and NetBSD 5 have pthread_getaffinity_np,
  54. but with different APIs. Also it requires linking with -lpthread.
  55. Therefore this code is not enabled.
  56. glibc >= 2.3.4 has sched_getaffinity whereas NetBSD 5 has
  57. sched_getaffinity_np. */
  58. #if HAVE_PTHREAD_GETAFFINITY_NP && defined __GLIBC__ && 0
  59. {
  60. cpu_set_t set;
  61. if (pthread_getaffinity_np (pthread_self (), sizeof (set), &set) == 0)
  62. {
  63. unsigned long count;
  64. # ifdef CPU_COUNT
  65. /* glibc >= 2.6 has the CPU_COUNT macro. */
  66. count = CPU_COUNT (&set);
  67. # else
  68. size_t i;
  69. count = 0;
  70. for (i = 0; i < CPU_SETSIZE; i++)
  71. if (CPU_ISSET (i, &set))
  72. count++;
  73. # endif
  74. if (count > 0)
  75. return count;
  76. }
  77. }
  78. #elif HAVE_PTHREAD_GETAFFINITY_NP && defined __NetBSD__ && 0
  79. {
  80. cpuset_t *set;
  81. set = cpuset_create ();
  82. if (set != NULL)
  83. {
  84. unsigned long count = 0;
  85. if (pthread_getaffinity_np (pthread_self (), cpuset_size (set), set)
  86. == 0)
  87. {
  88. cpuid_t i;
  89. for (i = 0;; i++)
  90. {
  91. int ret = cpuset_isset (i, set);
  92. if (ret < 0)
  93. break;
  94. if (ret > 0)
  95. count++;
  96. }
  97. }
  98. cpuset_destroy (set);
  99. if (count > 0)
  100. return count;
  101. }
  102. }
  103. #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */
  104. {
  105. cpu_set_t set;
  106. if (sched_getaffinity (0, sizeof (set), &set) == 0)
  107. {
  108. unsigned long count;
  109. # ifdef CPU_COUNT
  110. /* glibc >= 2.6 has the CPU_COUNT macro. */
  111. count = CPU_COUNT (&set);
  112. # else
  113. size_t i;
  114. count = 0;
  115. for (i = 0; i < CPU_SETSIZE; i++)
  116. if (CPU_ISSET (i, &set))
  117. count++;
  118. # endif
  119. if (count > 0)
  120. return count;
  121. }
  122. }
  123. #elif HAVE_SCHED_GETAFFINITY_NP /* NetBSD >= 5 */
  124. {
  125. cpuset_t *set;
  126. set = cpuset_create ();
  127. if (set != NULL)
  128. {
  129. unsigned long count = 0;
  130. if (sched_getaffinity_np (getpid (), cpuset_size (set), set) == 0)
  131. {
  132. cpuid_t i;
  133. for (i = 0;; i++)
  134. {
  135. int ret = cpuset_isset (i, set);
  136. if (ret < 0)
  137. break;
  138. if (ret > 0)
  139. count++;
  140. }
  141. }
  142. cpuset_destroy (set);
  143. if (count > 0)
  144. return count;
  145. }
  146. }
  147. #endif
  148. #if defined _WIN32 && ! defined __CYGWIN__
  149. { /* This works on native Windows platforms. */
  150. DWORD_PTR process_mask;
  151. DWORD_PTR system_mask;
  152. if (GetProcessAffinityMask (GetCurrentProcess (),
  153. &process_mask, &system_mask))
  154. {
  155. DWORD_PTR mask = process_mask;
  156. unsigned long count = 0;
  157. for (; mask != 0; mask = mask >> 1)
  158. if (mask & 1)
  159. count++;
  160. if (count > 0)
  161. return count;
  162. }
  163. }
  164. #endif
  165. return 0;
  166. }
  167. /* Return the total number of processors. Here QUERY must be one of
  168. NPROC_ALL, NPROC_CURRENT. The result is guaranteed to be at least 1. */
  169. static unsigned long int
  170. num_processors_ignoring_omp (enum nproc_query query)
  171. {
  172. /* On systems with a modern affinity mask system call, we have
  173. sysconf (_SC_NPROCESSORS_CONF)
  174. >= sysconf (_SC_NPROCESSORS_ONLN)
  175. >= num_processors_via_affinity_mask ()
  176. The first number is the number of CPUs configured in the system.
  177. The second number is the number of CPUs available to the scheduler.
  178. The third number is the number of CPUs available to the current process.
  179. Note! On Linux systems with glibc, the first and second number come from
  180. the /sys and /proc file systems (see
  181. glibc/sysdeps/unix/sysv/linux/getsysstats.c).
  182. In some situations these file systems are not mounted, and the sysconf call
  183. returns 1 or 2 (<https://sourceware.org/bugzilla/show_bug.cgi?id=21542>),
  184. which does not reflect the reality. */
  185. if (query == NPROC_CURRENT)
  186. {
  187. /* Try the modern affinity mask system call. */
  188. {
  189. unsigned long nprocs = num_processors_via_affinity_mask ();
  190. if (nprocs > 0)
  191. return nprocs;
  192. }
  193. #if defined _SC_NPROCESSORS_ONLN
  194. { /* This works on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
  195. Cygwin, Haiku. */
  196. long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
  197. if (nprocs > 0)
  198. return nprocs;
  199. }
  200. #endif
  201. }
  202. else /* query == NPROC_ALL */
  203. {
  204. #if defined _SC_NPROCESSORS_CONF
  205. { /* This works on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
  206. Cygwin, Haiku. */
  207. long int nprocs = sysconf (_SC_NPROCESSORS_CONF);
  208. # if __GLIBC__ >= 2 && defined __linux__
  209. /* On Linux systems with glibc, this information comes from the /sys and
  210. /proc file systems (see glibc/sysdeps/unix/sysv/linux/getsysstats.c).
  211. In some situations these file systems are not mounted, and the
  212. sysconf call returns 1 or 2. But we wish to guarantee that
  213. num_processors (NPROC_ALL) >= num_processors (NPROC_CURRENT). */
  214. if (nprocs == 1 || nprocs == 2)
  215. {
  216. unsigned long nprocs_current = num_processors_via_affinity_mask ();
  217. if (/* nprocs_current > 0 && */ nprocs_current > nprocs)
  218. nprocs = nprocs_current;
  219. }
  220. # endif
  221. if (nprocs > 0)
  222. return nprocs;
  223. }
  224. #endif
  225. }
  226. #if HAVE_PSTAT_GETDYNAMIC
  227. { /* This works on HP-UX. */
  228. struct pst_dynamic psd;
  229. if (pstat_getdynamic (&psd, sizeof psd, 1, 0) >= 0)
  230. {
  231. /* The field psd_proc_cnt contains the number of active processors.
  232. In newer releases of HP-UX 11, the field psd_max_proc_cnt includes
  233. deactivated processors. */
  234. if (query == NPROC_CURRENT)
  235. {
  236. if (psd.psd_proc_cnt > 0)
  237. return psd.psd_proc_cnt;
  238. }
  239. else
  240. {
  241. if (psd.psd_max_proc_cnt > 0)
  242. return psd.psd_max_proc_cnt;
  243. }
  244. }
  245. }
  246. #endif
  247. #if HAVE_SYSMP && defined MP_NAPROCS && defined MP_NPROCS
  248. { /* This works on IRIX. */
  249. /* MP_NPROCS yields the number of installed processors.
  250. MP_NAPROCS yields the number of processors available to unprivileged
  251. processes. */
  252. int nprocs =
  253. sysmp (query == NPROC_CURRENT && getuid () != 0
  254. ? MP_NAPROCS
  255. : MP_NPROCS);
  256. if (nprocs > 0)
  257. return nprocs;
  258. }
  259. #endif
  260. /* Finally, as fallback, use the APIs that don't distinguish between
  261. NPROC_CURRENT and NPROC_ALL. */
  262. #if HAVE_SYSCTL && ! defined __GLIBC__ && defined HW_NCPU
  263. { /* This works on macOS, FreeBSD, NetBSD, OpenBSD.
  264. macOS 10.14 does not allow mib to be const. */
  265. int nprocs;
  266. size_t len = sizeof (nprocs);
  267. static int mib[][2] = {
  268. # ifdef HW_NCPUONLINE
  269. { CTL_HW, HW_NCPUONLINE },
  270. # endif
  271. { CTL_HW, HW_NCPU }
  272. };
  273. for (int i = 0; i < ARRAY_SIZE (mib); i++)
  274. {
  275. if (sysctl (mib[i], ARRAY_SIZE (mib[i]), &nprocs, &len, NULL, 0) == 0
  276. && len == sizeof (nprocs)
  277. && 0 < nprocs)
  278. return nprocs;
  279. }
  280. }
  281. #endif
  282. #if defined _WIN32 && ! defined __CYGWIN__
  283. { /* This works on native Windows platforms. */
  284. SYSTEM_INFO system_info;
  285. GetSystemInfo (&system_info);
  286. if (0 < system_info.dwNumberOfProcessors)
  287. return system_info.dwNumberOfProcessors;
  288. }
  289. #endif
  290. return 1;
  291. }
  292. /* Parse OMP environment variables without dependence on OMP.
  293. Return 0 for invalid values. */
  294. static unsigned long int
  295. parse_omp_threads (char const* threads)
  296. {
  297. unsigned long int ret = 0;
  298. if (threads == NULL)
  299. return ret;
  300. /* The OpenMP spec says that the value assigned to the environment variables
  301. "may have leading and trailing white space". */
  302. while (*threads != '\0' && c_isspace (*threads))
  303. threads++;
  304. /* Convert it from positive decimal to 'unsigned long'. */
  305. if (c_isdigit (*threads))
  306. {
  307. char *endptr = NULL;
  308. unsigned long int value = strtoul (threads, &endptr, 10);
  309. if (endptr != NULL)
  310. {
  311. while (*endptr != '\0' && c_isspace (*endptr))
  312. endptr++;
  313. if (*endptr == '\0')
  314. return value;
  315. /* Also accept the first value in a nesting level,
  316. since we can't determine the nesting level from env vars. */
  317. else if (*endptr == ',')
  318. return value;
  319. }
  320. }
  321. return ret;
  322. }
  323. unsigned long int
  324. num_processors (enum nproc_query query)
  325. {
  326. unsigned long int omp_env_limit = ULONG_MAX;
  327. if (query == NPROC_CURRENT_OVERRIDABLE)
  328. {
  329. unsigned long int omp_env_threads;
  330. /* Honor the OpenMP environment variables, recognized also by all
  331. programs that are based on OpenMP. */
  332. omp_env_threads = parse_omp_threads (getenv ("OMP_NUM_THREADS"));
  333. omp_env_limit = parse_omp_threads (getenv ("OMP_THREAD_LIMIT"));
  334. if (! omp_env_limit)
  335. omp_env_limit = ULONG_MAX;
  336. if (omp_env_threads)
  337. return MIN (omp_env_threads, omp_env_limit);
  338. query = NPROC_CURRENT;
  339. }
  340. /* Here query is one of NPROC_ALL, NPROC_CURRENT. */
  341. {
  342. unsigned long nprocs = num_processors_ignoring_omp (query);
  343. return MIN (nprocs, omp_env_limit);
  344. }
  345. }