1 /* Detect the number of processors.
3 Copyright (C) 2009-2010 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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Glen Lenker and Bruno Haible. */
27 #if HAVE_PTHREAD_AFFINITY_NP && 0
31 #if HAVE_SCHED_GETAFFINITY_LIKE_GLIBC || HAVE_SCHED_GETAFFINITY_NP
35 #include <sys/types.h>
38 # include <sys/pstat.h>
42 # include <sys/sysmp.h>
46 # include <sys/param.h>
50 # include <sys/sysctl.h>
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
54 # define WIN32_LEAN_AND_MEAN
60 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
62 /* Return the number of processors available to the current process, based
63 on a modern system call that returns the "affinity" between the current
64 process and each CPU. Return 0 if unknown or if such a system call does
67 num_processors_via_affinity_mask (void)
69 /* glibc >= 2.3.3 with NPTL and NetBSD 5 have pthread_getaffinity_np,
70 but with different APIs. Also it requires linking with -lpthread.
71 Therefore this code is not enabled.
72 glibc >= 2.3.4 has sched_getaffinity whereas NetBSD 5 has
73 sched_getaffinity_np. */
74 #if HAVE_PTHREAD_AFFINITY_NP && defined __GLIBC__ && 0
78 if (pthread_getaffinity_np (pthread_self (), sizeof (set), &set) == 0)
83 /* glibc >= 2.6 has the CPU_COUNT macro. */
84 count = CPU_COUNT (&set);
89 for (i = 0; i < CPU_SETSIZE; i++)
90 if (CPU_ISSET (i, &set))
97 #elif HAVE_PTHREAD_AFFINITY_NP && defined __NetBSD__ && 0
101 set = cpuset_create ();
104 unsigned long count = 0;
106 if (pthread_getaffinity_np (pthread_self (), cpuset_size (set), set)
113 int ret = cpuset_isset (i, set);
120 cpuset_destroy (set);
125 #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */
129 if (sched_getaffinity (0, sizeof (set), &set) == 0)
134 /* glibc >= 2.6 has the CPU_COUNT macro. */
135 count = CPU_COUNT (&set);
140 for (i = 0; i < CPU_SETSIZE; i++)
141 if (CPU_ISSET (i, &set))
148 #elif HAVE_SCHED_GETAFFINITY_NP /* NetBSD >= 5 */
152 set = cpuset_create ();
155 unsigned long count = 0;
157 if (sched_getaffinity_np (getpid (), cpuset_size (set), set) == 0)
163 int ret = cpuset_isset (i, set);
170 cpuset_destroy (set);
177 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
178 { /* This works on native Windows platforms. */
179 DWORD_PTR process_mask;
180 DWORD_PTR system_mask;
182 if (GetProcessAffinityMask (GetCurrentProcess (),
183 &process_mask, &system_mask))
185 DWORD_PTR mask = process_mask;
186 unsigned long count = 0;
188 for (; mask != 0; mask = mask >> 1)
201 num_processors (enum nproc_query query)
203 if (query == NPROC_CURRENT_OVERRIDABLE)
205 /* Test the environment variable OMP_NUM_THREADS, recognized also by all
206 programs that are based on OpenMP. The OpenMP spec says that the
207 value assigned to the environment variable "may have leading and
208 trailing white space". */
209 const char *envvalue = getenv ("OMP_NUM_THREADS");
211 if (envvalue != NULL)
213 while (*envvalue != '\0' && c_isspace (*envvalue))
215 /* Convert it from decimal to 'unsigned long'. */
216 if (c_isdigit (*envvalue))
219 unsigned long int value = strtoul (envvalue, &endptr, 10);
223 while (*endptr != '\0' && c_isspace (*endptr))
226 return (value > 0 ? value : 1);
231 query = NPROC_CURRENT;
233 /* Here query is one of NPROC_ALL, NPROC_CURRENT. */
235 /* On systems with a modern affinity mask system call, we have
236 sysconf (_SC_NPROCESSORS_CONF)
237 >= sysconf (_SC_NPROCESSORS_ONLN)
238 >= num_processors_via_affinity_mask ()
239 The first number is the number of CPUs configured in the system.
240 The second number is the number of CPUs available to the scheduler.
241 The third number is the number of CPUs available to the current process.
243 Note! On Linux systems with glibc, the first and second number come from
244 the /sys and /proc file systems (see
245 glibc/sysdeps/unix/sysv/linux/getsysstats.c).
246 In some situations these file systems are not mounted, and the sysconf
247 call returns 1, which does not reflect the reality. */
249 if (query == NPROC_CURRENT)
251 /* Try the modern affinity mask system call. */
253 unsigned long nprocs = num_processors_via_affinity_mask ();
259 #if defined _SC_NPROCESSORS_ONLN
260 { /* This works on glibc, MacOS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
262 long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
268 else /* query == NPROC_ALL */
270 #if defined _SC_NPROCESSORS_CONF
271 { /* This works on glibc, MacOS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
273 long int nprocs = sysconf (_SC_NPROCESSORS_CONF);
275 # if __GLIBC__ >= 2 && defined __linux__
276 /* On Linux systems with glibc, this information comes from the /sys and
277 /proc file systems (see glibc/sysdeps/unix/sysv/linux/getsysstats.c).
278 In some situations these file systems are not mounted, and the
279 sysconf call returns 1. But we wish to guarantee that
280 num_processors (NPROC_ALL) >= num_processors (NPROC_CURRENT). */
283 unsigned long nprocs_current = num_processors_via_affinity_mask ();
285 if (nprocs_current > 0)
286 nprocs = nprocs_current;
296 #if HAVE_PSTAT_GETDYNAMIC
297 { /* This works on HP-UX. */
298 struct pst_dynamic psd;
299 if (pstat_getdynamic (&psd, sizeof psd, 1, 0) >= 0)
301 /* The field psd_proc_cnt contains the number of active processors.
302 In newer releases of HP-UX 11, the field psd_max_proc_cnt includes
303 deactivated processors. */
304 if (query == NPROC_CURRENT)
306 if (psd.psd_proc_cnt > 0)
307 return psd.psd_proc_cnt;
311 if (psd.psd_max_proc_cnt > 0)
312 return psd.psd_max_proc_cnt;
318 #if HAVE_SYSMP && defined MP_NAPROCS && defined MP_NPROCS
319 { /* This works on IRIX. */
320 /* MP_NPROCS yields the number of installed processors.
321 MP_NAPROCS yields the number of processors available to unprivileged
324 sysmp (query == NPROC_CURRENT && getpid () != 0
332 /* Finally, as fallback, use the APIs that don't distinguish between
333 NPROC_CURRENT and NPROC_ALL. */
335 #if HAVE_SYSCTL && defined HW_NCPU
336 { /* This works on MacOS X, FreeBSD, NetBSD, OpenBSD. */
338 size_t len = sizeof (nprocs);
339 static int mib[2] = { CTL_HW, HW_NCPU };
341 if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0
342 && len == sizeof (nprocs)
348 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
349 { /* This works on native Windows platforms. */
350 SYSTEM_INFO system_info;
351 GetSystemInfo (&system_info);
352 if (0 < system_info.dwNumberOfProcessors)
353 return system_info.dwNumberOfProcessors;