Created
February 5, 2014 09:49
-
-
Save vicendominguez/8820295 to your computer and use it in GitHub Desktop.
Discovering the real number of CPUs in the host server from inside of a VZ container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* numcpu.c code is a part of FFMPEG source (libavutil/) ;) | |
You can play with the definitions in compilation time. Very interesting. | |
[root@host:~# grep -c processor /proc/cpuinfo | |
24 | |
[root@build /]# grep -c processor /proc/cpuinfo | |
1 | |
We have one openvz container with 1 CPU in one server with 24 cpus (cores: 2x cpu 6xcore 2x ht): | |
[root@build /]# gcc -D_GNU_SOURCE -DHAVE_SYSCTL -DHAVE_SYSCONF -Wall numcpu.c -o numcpu | |
[root@build /]# ./numcpu | |
_SC_NPROCESSORS_ONLN CPU num: 1 | |
[root@build /]# | |
And pay attention: | |
[root@build /]# gcc -D_GNU_SOURCE -DHAVE_SCHED_GETAFFINITY -Wall numcpu.c -o numcpu | |
[root@build /]# ./numcpu | |
HAVE_SCHED_GETAFFINITY CPU num: 24 | |
So it looks like the auto-discovering of CPUs inside of openvz container using the HAVE_SCHED_GETAFFINITY macros (and the sched_setaffinity function) is not ok (or it is very very ok ;)). Be careful. If you are using this method to get resources it could be a bad idea. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <features.h> | |
#include <sched.h> | |
#if HAVE_SCHED_GETAFFINITY | |
#ifndef _GNU_SOURCE | |
# define _GNU_SOURCE | |
#endif | |
#include <sched.h> | |
#endif | |
#if HAVE_GETPROCESSAFFINITYMASK | |
#include <windows.h> | |
#endif | |
#if HAVE_SYSCTL | |
#if HAVE_SYS_PARAM_H | |
#include <sys/param.h> | |
#endif | |
#include <sys/types.h> | |
#include <sys/param.h> | |
#include <sys/sysctl.h> | |
#endif | |
#if HAVE_SYSCONF | |
#include <unistd.h> | |
#endif | |
int av_cpu_count(void) | |
{ | |
int nb_cpus = 0; | |
#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) | |
cpu_set_t cpuset; | |
CPU_ZERO(&cpuset); | |
if (!sched_getaffinity(0, sizeof(cpuset), &cpuset)) | |
nb_cpus = CPU_COUNT(&cpuset); | |
printf ("HAVE_SCHED_GETAFFINITY "); | |
#elif HAVE_GETPROCESSAFFINITYMASK | |
DWORD_PTR proc_aff, sys_aff; | |
if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff)) | |
nb_cpus = av_popcount64(proc_aff); | |
printf ("HAVE_GETPROCESSAFFINITYMASK "); | |
#elif HAVE_SYSCTL && defined(HW_NCPU) | |
int mib[2] = { CTL_HW, HW_NCPU }; | |
size_t len = sizeof(nb_cpus); | |
if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1) | |
nb_cpus = 0; | |
printf ("HW_NCPU "); | |
#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) | |
nb_cpus = sysconf(_SC_NPROC_ONLN); | |
printf ("_SC_NPROC_ONLN "); | |
#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) | |
nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
printf ("_SC_NPROCESSORS_ONLN "); | |
#endif | |
return nb_cpus; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int numcpu=av_cpu_count(); | |
printf ("CPU num: %d \n", numcpu); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment