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
15207753Smm#if defined(TUKLIB_CPUCORES_SYSCTL)
16207753Smm#	ifdef HAVE_SYS_PARAM_H
17207753Smm#		include <sys/param.h>
18207753Smm#	endif
19207753Smm#	include <sys/sysctl.h>
20207753Smm
21207753Smm#elif defined(TUKLIB_CPUCORES_SYSCONF)
22207753Smm#	include <unistd.h>
23213700Smm
24213700Smm// HP-UX
25213700Smm#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
26213700Smm#	include <sys/param.h>
27213700Smm#	include <sys/pstat.h>
28207753Smm#endif
29207753Smm
30207753Smm
31207753Smmextern uint32_t
32207753Smmtuklib_cpucores(void)
33207753Smm{
34207753Smm	uint32_t ret = 0;
35207753Smm
36207753Smm#if defined(TUKLIB_CPUCORES_SYSCTL)
37207753Smm	int name[2] = { CTL_HW, HW_NCPU };
38207753Smm	int cpus;
39207753Smm	size_t cpus_size = sizeof(cpus);
40207753Smm	if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
41207753Smm			&& cpus_size == sizeof(cpus) && cpus > 0)
42213700Smm		ret = cpus;
43207753Smm
44207753Smm#elif defined(TUKLIB_CPUCORES_SYSCONF)
45207753Smm#	ifdef _SC_NPROCESSORS_ONLN
46207753Smm	// Most systems
47207753Smm	const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
48207753Smm#	else
49207753Smm	// IRIX
50207753Smm	const long cpus = sysconf(_SC_NPROC_ONLN);
51207753Smm#	endif
52207753Smm	if (cpus > 0)
53213700Smm		ret = cpus;
54213700Smm
55213700Smm#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
56213700Smm	struct pst_dynamic pst;
57213700Smm	if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1)
58213700Smm		ret = pst.psd_proc_cnt;
59207753Smm#endif
60207753Smm
61207753Smm	return ret;
62207753Smm}
63