2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgo / runtime / getncpu-linux.c
blob0122b77c9ffcd75302e62e8eb33354060e40f436
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.
5 #include <features.h>
6 #include <sched.h>
8 // CPU_COUNT is only provided by glibc 2.6 or higher
9 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 6)
10 #define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int))
11 static int _CPU_COUNT(unsigned int *set, size_t len) {
12 int cnt;
14 cnt = 0;
15 while (len--)
16 cnt += __builtin_popcount(*set++);
17 return cnt;
19 #endif
21 #include "runtime.h"
22 #include "defs.h"
24 int32
25 getproccount(void)
27 cpu_set_t set;
28 int32 r, cnt;
30 cnt = 0;
31 r = sched_getaffinity(0, sizeof(set), &set);
32 if(r == 0)
33 cnt += CPU_COUNT(&set);
35 return cnt ? cnt : 1;