tuklib_cpucores.c revision 292588
1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       tuklib_cpucores.c
4207753Smm/// \brief      Get the number of CPU cores online
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#include "tuklib_cpucores.h"
14207753Smm
15292588Sdelphij#if defined(_WIN32) || defined(__CYGWIN__)
16292588Sdelphij#	ifndef _WIN32_WINNT
17292588Sdelphij#		define _WIN32_WINNT 0x0500
18292588Sdelphij#	endif
19292588Sdelphij#	include <windows.h>
20292588Sdelphij
21292588Sdelphij// FreeBSD
22292588Sdelphij#elif defined(TUKLIB_CPUCORES_CPUSET)
23292588Sdelphij#	include <sys/param.h>
24292588Sdelphij#	include <sys/cpuset.h>
25292588Sdelphij
26292588Sdelphij#elif defined(TUKLIB_CPUCORES_SYSCTL)
27207753Smm#	ifdef HAVE_SYS_PARAM_H
28207753Smm#		include <sys/param.h>
29207753Smm#	endif
30207753Smm#	include <sys/sysctl.h>
31207753Smm
32207753Smm#elif defined(TUKLIB_CPUCORES_SYSCONF)
33207753Smm#	include <unistd.h>
34213700Smm
35213700Smm// HP-UX
36213700Smm#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
37213700Smm#	include <sys/param.h>
38213700Smm#	include <sys/pstat.h>
39207753Smm#endif
40207753Smm
41207753Smm
42207753Smmextern uint32_t
43207753Smmtuklib_cpucores(void)
44207753Smm{
45207753Smm	uint32_t ret = 0;
46207753Smm
47292588Sdelphij#if defined(_WIN32) || defined(__CYGWIN__)
48292588Sdelphij	SYSTEM_INFO sysinfo;
49292588Sdelphij	GetSystemInfo(&sysinfo);
50292588Sdelphij	ret = sysinfo.dwNumberOfProcessors;
51292588Sdelphij
52292588Sdelphij#elif defined(TUKLIB_CPUCORES_CPUSET)
53292588Sdelphij	cpuset_t set;
54292588Sdelphij	if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
55292588Sdelphij			sizeof(set), &set) == 0) {
56292588Sdelphij#	ifdef CPU_COUNT
57292588Sdelphij		ret = CPU_COUNT(&set);
58292588Sdelphij#	else
59292588Sdelphij		for (unsigned i = 0; i < CPU_SETSIZE; ++i)
60292588Sdelphij			if (CPU_ISSET(i, &set))
61292588Sdelphij				++ret;
62292588Sdelphij#	endif
63292588Sdelphij	}
64292588Sdelphij
65292588Sdelphij#elif defined(TUKLIB_CPUCORES_SYSCTL)
66207753Smm	int name[2] = { CTL_HW, HW_NCPU };
67207753Smm	int cpus;
68207753Smm	size_t cpus_size = sizeof(cpus);
69207753Smm	if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
70207753Smm			&& cpus_size == sizeof(cpus) && cpus > 0)
71213700Smm		ret = cpus;
72207753Smm
73207753Smm#elif defined(TUKLIB_CPUCORES_SYSCONF)
74207753Smm#	ifdef _SC_NPROCESSORS_ONLN
75207753Smm	// Most systems
76207753Smm	const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
77207753Smm#	else
78207753Smm	// IRIX
79207753Smm	const long cpus = sysconf(_SC_NPROC_ONLN);
80207753Smm#	endif
81207753Smm	if (cpus > 0)
82213700Smm		ret = cpus;
83213700Smm
84213700Smm#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
85213700Smm	struct pst_dynamic pst;
86213700Smm	if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1)
87213700Smm		ret = pst.psd_proc_cnt;
88207753Smm#endif
89207753Smm
90207753Smm	return ret;
91207753Smm}
92