1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_cpucores.c
4 /// \brief Get the number of CPU cores online
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_cpucores.h"
15 #if defined(TUKLIB_CPUCORES_SYSCTL)
16 # ifdef HAVE_SYS_PARAM_H
17 # include <sys/param.h>
19 # include <sys/sysctl.h>
21 #elif defined(TUKLIB_CPUCORES_SYSCONF)
25 #elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
26 # include <sys/param.h>
27 # include <sys/pstat.h>
36 #if defined(TUKLIB_CPUCORES_SYSCTL)
37 int name[2] = { CTL_HW, HW_NCPU };
39 size_t cpus_size = sizeof(cpus);
40 if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
41 && cpus_size == sizeof(cpus) && cpus > 0)
44 #elif defined(TUKLIB_CPUCORES_SYSCONF)
45 # ifdef _SC_NPROCESSORS_ONLN
47 const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
50 const long cpus = sysconf(_SC_NPROC_ONLN);
55 #elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
56 struct pst_dynamic pst;
57 if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1)
58 ret = pst.psd_proc_cnt;