nproc.c 9.8 KB

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