2009-02-13 17:45:29 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2009-09-19 02:47:30 -04:00
|
|
|
/// \file tuklib_cpucores.c
|
|
|
|
/// \brief Get the number of CPU cores online
|
2009-02-13 17:45:29 -05:00
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// Author: Lasse Collin
|
2009-02-13 17:45:29 -05:00
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2009-02-13 17:45:29 -05:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-09-19 02:47:30 -04:00
|
|
|
#include "tuklib_cpucores.h"
|
2009-02-13 17:45:29 -05:00
|
|
|
|
2013-08-03 06:52:58 -04:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
# ifndef _WIN32_WINNT
|
|
|
|
# define _WIN32_WINNT 0x0500
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
|
|
|
|
2013-08-04 08:24:09 -04:00
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCTL)
|
2009-02-13 17:45:29 -05:00
|
|
|
# ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
# endif
|
2009-09-19 02:47:30 -04:00
|
|
|
# include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
|
|
|
# include <unistd.h>
|
2010-05-10 12:54:15 -04:00
|
|
|
|
|
|
|
// HP-UX
|
|
|
|
#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
|
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/pstat.h>
|
2009-02-13 17:45:29 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-09-19 02:47:30 -04:00
|
|
|
extern uint32_t
|
|
|
|
tuklib_cpucores(void)
|
2009-02-13 17:45:29 -05:00
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
2013-08-03 06:52:58 -04:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
ret = sysinfo.dwNumberOfProcessors;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCTL)
|
2009-02-13 17:45:29 -05:00
|
|
|
int name[2] = { CTL_HW, HW_NCPU };
|
|
|
|
int cpus;
|
|
|
|
size_t cpus_size = sizeof(cpus);
|
2009-09-04 18:20:29 -04:00
|
|
|
if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
|
2009-02-13 17:45:29 -05:00
|
|
|
&& cpus_size == sizeof(cpus) && cpus > 0)
|
2010-05-10 12:54:15 -04:00
|
|
|
ret = cpus;
|
2009-09-19 02:47:30 -04:00
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
2010-01-12 09:18:14 -05:00
|
|
|
# ifdef _SC_NPROCESSORS_ONLN
|
|
|
|
// Most systems
|
2009-09-19 02:47:30 -04:00
|
|
|
const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
2010-01-12 09:18:14 -05:00
|
|
|
# else
|
|
|
|
// IRIX
|
|
|
|
const long cpus = sysconf(_SC_NPROC_ONLN);
|
|
|
|
# endif
|
2009-09-19 02:47:30 -04:00
|
|
|
if (cpus > 0)
|
2010-05-10 12:54:15 -04:00
|
|
|
ret = cpus;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
|
|
|
|
struct pst_dynamic pst;
|
|
|
|
if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1)
|
|
|
|
ret = pst.psd_proc_cnt;
|
2009-02-13 17:45:29 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|