nproc.c 9.8 KB

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