getncpu-linux.c 737 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include <features.h>
  5. #include <sched.h>
  6. // CPU_COUNT is only provided by glibc 2.6 or higher
  7. #ifndef CPU_COUNT
  8. #define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int))
  9. static int _CPU_COUNT(unsigned int *set, size_t len) {
  10. int cnt;
  11. cnt = 0;
  12. while (len--)
  13. cnt += __builtin_popcount(*set++);
  14. return cnt;
  15. }
  16. #endif
  17. #include "runtime.h"
  18. #include "defs.h"
  19. int32
  20. getproccount(void)
  21. {
  22. cpu_set_t set;
  23. int32 r, cnt;
  24. cnt = 0;
  25. r = sched_getaffinity(0, sizeof(set), &set);
  26. if(r == 0)
  27. cnt += CPU_COUNT(&set);
  28. return cnt ? cnt : 1;
  29. }