Advertisement
vicendominguez

Discovering real CPU number inside a openvz container in C

Jul 19th, 2013
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /* numcpu.c code is a part of FFMPEG source (libavutil/) ;)
  2.  
  3. You can play with the definitions in compilation time. Very interesting.
  4.  
  5. [root@host:~# grep -c processor /proc/cpuinfo
  6. 24
  7. [root@build /]# grep -c processor /proc/cpuinfo
  8. 1
  9.  
  10. We have one openvz container with 1 CPU in one server with 24 cpus (cores: 2x cpu 6xcore 2x ht):
  11.  
  12. [root@build /]# gcc -D_GNU_SOURCE -DHAVE_SYSCTL -DHAVE_SYSCONF -Wall numcpu.c -o numcpu
  13. [root@build /]# ./numcpu
  14. _SC_NPROCESSORS_ONLN CPU num: 1
  15. [root@build /]#
  16.  
  17. And pay attention:
  18.  
  19. [root@build /]# gcc -D_GNU_SOURCE -DHAVE_SCHED_GETAFFINITY -Wall numcpu.c -o numcpu
  20. [root@build /]# ./numcpu
  21. HAVE_SCHED_GETAFFINITY CPU num: 24
  22.  
  23. 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.
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <features.h>
  30. #include <sched.h>
  31.  
  32. #if HAVE_SCHED_GETAFFINITY
  33. #ifndef _GNU_SOURCE
  34. # define _GNU_SOURCE
  35. #endif
  36. #include <sched.h>
  37. #endif
  38. #if HAVE_GETPROCESSAFFINITYMASK
  39. #include <windows.h>
  40. #endif
  41. #if HAVE_SYSCTL
  42. #if HAVE_SYS_PARAM_H
  43. #include <sys/param.h>
  44. #endif
  45. #include <sys/types.h>
  46. #include <sys/param.h>
  47. #include <sys/sysctl.h>
  48. #endif
  49. #if HAVE_SYSCONF
  50. #include <unistd.h>
  51. #endif
  52.  
  53. int av_cpu_count(void)
  54. {
  55.     int nb_cpus = 0;
  56. #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
  57.     cpu_set_t cpuset;
  58.  
  59.     CPU_ZERO(&cpuset);
  60.  
  61.     if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
  62.         nb_cpus = CPU_COUNT(&cpuset);
  63.     printf ("HAVE_SCHED_GETAFFINITY ");
  64.  
  65. #elif HAVE_GETPROCESSAFFINITYMASK
  66.     DWORD_PTR proc_aff, sys_aff;
  67.     if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
  68.         nb_cpus = av_popcount64(proc_aff);
  69.         printf ("HAVE_GETPROCESSAFFINITYMASK ");
  70.  
  71. #elif HAVE_SYSCTL && defined(HW_NCPU)
  72.     int mib[2] = { CTL_HW, HW_NCPU };
  73.     size_t len = sizeof(nb_cpus);
  74.  
  75.     if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
  76.         nb_cpus = 0;
  77.     printf ("HW_NCPU ");
  78. #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
  79.     nb_cpus = sysconf(_SC_NPROC_ONLN);
  80.     printf ("_SC_NPROC_ONLN ");
  81.  
  82. #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
  83.     nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  84.     printf ("_SC_NPROCESSORS_ONLN ");
  85. #endif
  86.  
  87.     return nb_cpus;
  88. }
  89.  
  90. int main(int argc, char *argv[])
  91. {
  92.     int numcpu=av_cpu_count();
  93.     printf ("CPU num: %d \n", numcpu);
  94.     exit(EXIT_SUCCESS);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement