1 /* Detect the number of processors.
3 Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Glen Lenker and Bruno Haible. */
26 #if HAVE_PTHREAD_GETAFFINITY_NP && 0
30 #if HAVE_SCHED_GETAFFINITY_LIKE_GLIBC || HAVE_SCHED_GETAFFINITY_NP
34 #include <sys/types.h>
37 # include <sys/pstat.h>
41 # include <sys/sysmp.h>
45 # include <sys/param.h>
49 # include <sys/sysctl.h>
52 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
53 # define WIN32_LEAN_AND_MEAN
59 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
61 /* Return the number of processors available to the current process, based
62 on a modern system call that returns the "affinity" between the current
63 process and each CPU. Return 0 if unknown or if such a system call does
66 num_processors_via_affinity_mask (void)
68 /* glibc >= 2.3.3 with NPTL and NetBSD 5 have pthread_getaffinity_np,
69 but with different APIs. Also it requires linking with -lpthread.
70 Therefore this code is not enabled.
71 glibc >= 2.3.4 has sched_getaffinity whereas NetBSD 5 has
72 sched_getaffinity_np. */
73 #if HAVE_PTHREAD_GETAFFINITY_NP && defined __GLIBC__ && 0
77 if (pthread_getaffinity_np (pthread_self (), sizeof (set), &set) == 0)
82 /* glibc >= 2.6 has the CPU_COUNT macro. */
83 count = CPU_COUNT (&set);
88 for (i = 0; i < CPU_SETSIZE; i++)
89 if (CPU_ISSET (i, &set))
96 #elif HAVE_PTHREAD_GETAFFINITY_NP && defined __NetBSD__ && 0
100 set = cpuset_create ();
103 unsigned long count = 0;
105 if (pthread_getaffinity_np (pthread_self (), cpuset_size (set), set)
112 int ret = cpuset_isset (i, set);
119 cpuset_destroy (set);
124 #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */
128 if (sched_getaffinity (0, sizeof (set), &set) == 0)
133 /* glibc >= 2.6 has the CPU_COUNT macro. */
134 count = CPU_COUNT (&set);
139 for (i = 0; i < CPU_SETSIZE; i++)
140 if (CPU_ISSET (i, &set))
147 #elif HAVE_SCHED_GETAFFINITY_NP /* NetBSD >= 5 */
151 set = cpuset_create ();
154 unsigned long count = 0;
156 if (sched_getaffinity_np (getpid (), cpuset_size (set), set) == 0)
162 int ret = cpuset_isset (i, set);
169 cpuset_destroy (set);
176 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
177 { /* This works on native Windows platforms. */
178 DWORD_PTR process_mask;
179 DWORD_PTR system_mask;
181 if (GetProcessAffinityMask (GetCurrentProcess (),
182 &process_mask, &system_mask))
184 DWORD_PTR mask = process_mask;
185 unsigned long count = 0;
187 for (; mask != 0; mask = mask >> 1)
200 num_processors (enum nproc_query query)
202 if (query == NPROC_CURRENT_OVERRIDABLE)
204 /* Test the environment variable OMP_NUM_THREADS, recognized also by all
205 programs that are based on OpenMP. The OpenMP spec says that the
206 value assigned to the environment variable "may have leading and
207 trailing white space". */
208 const char *envvalue = getenv ("OMP_NUM_THREADS");
210 if (envvalue != NULL)
212 while (*envvalue != '\0' && c_isspace (*envvalue))
214 /* Convert it from decimal to 'unsigned long'. */
215 if (c_isdigit (*envvalue))
218 unsigned long int value = strtoul (envvalue, &endptr, 10);
222 while (*endptr != '\0' && c_isspace (*endptr))
225 return (value > 0 ? value : 1);
230 query = NPROC_CURRENT;
232 /* Here query is one of NPROC_ALL, NPROC_CURRENT. */
234 /* On systems with a modern affinity mask system call, we have
235 sysconf (_SC_NPROCESSORS_CONF)
236 >= sysconf (_SC_NPROCESSORS_ONLN)
237 >= num_processors_via_affinity_mask ()
238 The first number is the number of CPUs configured in the system.
239 The second number is the number of CPUs available to the scheduler.
240 The third number is the number of CPUs available to the current process.
242 Note! On Linux systems with glibc, the first and second number come from
243 the /sys and /proc file systems (see
244 glibc/sysdeps/unix/sysv/linux/getsysstats.c).
245 In some situations these file systems are not mounted, and the sysconf
246 call returns 1, which does not reflect the reality. */
248 if (query == NPROC_CURRENT)
250 /* Try the modern affinity mask system call. */
252 unsigned long nprocs = num_processors_via_affinity_mask ();
258 #if defined _SC_NPROCESSORS_ONLN
259 { /* This works on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
261 long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
267 else /* query == NPROC_ALL */
269 #if defined _SC_NPROCESSORS_CONF
270 { /* This works on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
272 long int nprocs = sysconf (_SC_NPROCESSORS_CONF);
274 # if __GLIBC__ >= 2 && defined __linux__
275 /* On Linux systems with glibc, this information comes from the /sys and
276 /proc file systems (see glibc/sysdeps/unix/sysv/linux/getsysstats.c).
277 In some situations these file systems are not mounted, and the
278 sysconf call returns 1. But we wish to guarantee that
279 num_processors (NPROC_ALL) >= num_processors (NPROC_CURRENT). */
282 unsigned long nprocs_current = num_processors_via_affinity_mask ();
284 if (nprocs_current > 0)
285 nprocs = nprocs_current;
295 #if HAVE_PSTAT_GETDYNAMIC
296 { /* This works on HP-UX. */
297 struct pst_dynamic psd;
298 if (pstat_getdynamic (&psd, sizeof psd, 1, 0) >= 0)
300 /* The field psd_proc_cnt contains the number of active processors.
301 In newer releases of HP-UX 11, the field psd_max_proc_cnt includes
302 deactivated processors. */
303 if (query == NPROC_CURRENT)
305 if (psd.psd_proc_cnt > 0)
306 return psd.psd_proc_cnt;
310 if (psd.psd_max_proc_cnt > 0)
311 return psd.psd_max_proc_cnt;
317 #if HAVE_SYSMP && defined MP_NAPROCS && defined MP_NPROCS
318 { /* This works on IRIX. */
319 /* MP_NPROCS yields the number of installed processors.
320 MP_NAPROCS yields the number of processors available to unprivileged
323 sysmp (query == NPROC_CURRENT && getpid () != 0
331 /* Finally, as fallback, use the APIs that don't distinguish between
332 NPROC_CURRENT and NPROC_ALL. */
334 #if HAVE_SYSCTL && defined HW_NCPU
335 { /* This works on Mac OS X, FreeBSD, NetBSD, OpenBSD. */
337 size_t len = sizeof (nprocs);
338 static int mib[2] = { CTL_HW, HW_NCPU };
340 if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0
341 && len == sizeof (nprocs)
347 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
348 { /* This works on native Windows platforms. */
349 SYSTEM_INFO system_info;
350 GetSystemInfo (&system_info);
351 if (0 < system_info.dwNumberOfProcessors)
352 return system_info.dwNumberOfProcessors;