mirror of
https://github.com/upa/mscp.git
synced 2026-02-10 14:54:43 +08:00
39 lines
603 B
C
39 lines
603 B
C
#ifdef __APPLE__
|
|
#include <sys/types.h>
|
|
#include <sys/sysctl.h>
|
|
#elif linux
|
|
#define _GNU_SOURCE
|
|
#include <sched.h>
|
|
#else
|
|
#error unsupported platform
|
|
#endif
|
|
|
|
#include <util.h>
|
|
#include <platform.h>
|
|
|
|
#ifdef __APPLE__
|
|
int nr_cpus()
|
|
{
|
|
int n;
|
|
size_t size = sizeof(n);
|
|
|
|
if (sysctlbyname("machdep.cpu.core_count", &n, &size, NULL, 0) != 0) {
|
|
pr_err("failed to get number of cpu cores: %s\n", strerrno());
|
|
return -1;
|
|
}
|
|
|
|
return n;
|
|
}
|
|
#endif
|
|
|
|
#ifdef linux
|
|
int nr_cpus()
|
|
{
|
|
cpu_set_t cpu_set;
|
|
if (sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
|
|
return CPU_COUNT(&cpu_set);
|
|
return -1;
|
|
}
|
|
#endif
|
|
|