2009-02-13 23:45:29 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2009-09-19 08:47:30 +02:00
|
|
|
/// \file tuklib_cpucores.c
|
|
|
|
/// \brief Get the number of CPU cores online
|
2009-02-13 23:45:29 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2009-02-13 23:45:29 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2009-02-13 23:45:29 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#include "tuklib_cpucores.h"
|
2009-02-13 23:45:29 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#if defined(TUKLIB_CPUCORES_SYSCTL)
|
2009-02-13 23:45:29 +01:00
|
|
|
# ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
# endif
|
2009-09-19 08:47:30 +02:00
|
|
|
# include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
|
|
|
# include <unistd.h>
|
2009-02-13 23:45:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
extern uint32_t
|
|
|
|
tuklib_cpucores(void)
|
2009-02-13 23:45:29 +01:00
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#if defined(TUKLIB_CPUCORES_SYSCTL)
|
2009-02-13 23:45:29 +01:00
|
|
|
int name[2] = { CTL_HW, HW_NCPU };
|
|
|
|
int cpus;
|
|
|
|
size_t cpus_size = sizeof(cpus);
|
2009-09-05 00:20:29 +02:00
|
|
|
if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
|
2009-02-13 23:45:29 +01:00
|
|
|
&& cpus_size == sizeof(cpus) && cpus > 0)
|
2009-09-19 08:47:30 +02:00
|
|
|
ret = (uint32_t)cpus;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
|
|
|
const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (cpus > 0)
|
|
|
|
ret = (uint32_t)cpus;
|
2009-02-13 23:45:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|