1 /* Detect the number of processors.
3 Copyright (C) 2009 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. */
26 #include <sys/types.h>
29 # include <sys/pstat.h>
33 # include <sys/param.h>
37 # include <sys/sysctl.h>
40 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
42 /* Return the total number of processors. The result is guaranteed to
47 #if defined _SC_NPROCESSORS_ONLN
48 { /* This works on glibc, MacOS X 10.5, FreeBSD, AIX, OSF/1, Solaris, Cygwin,
50 long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
56 #if HAVE_PSTAT_GETDYNAMIC
57 { /* This works on HP-UX. */
58 struct pst_dynamic psd;
59 if (0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)
60 && 0 < psd.psd_proc_cnt)
61 return psd.psd_proc_cnt;
65 #if HAVE_SYSCTL && defined HW_NCPU
66 { /* This works on MacOS X, FreeBSD, NetBSD, OpenBSD. */
68 size_t len = sizeof (nprocs);
69 static int mib[2] = { CTL_HW, HW_NCPU };
71 if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0
72 && len == sizeof (nprocs)