annotate libgo/runtime/getncpu-linux.c @ 136:4627f235cf2a

fix c-next example
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:11:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2012 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 #include <features.h>
kono
parents:
diff changeset
6 #include <sched.h>
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 // CPU_COUNT is only provided by glibc 2.6 or higher
kono
parents:
diff changeset
9 #ifndef CPU_COUNT
kono
parents:
diff changeset
10 #define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int))
kono
parents:
diff changeset
11 static int _CPU_COUNT(unsigned int *set, size_t len) {
kono
parents:
diff changeset
12 int cnt;
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 cnt = 0;
kono
parents:
diff changeset
15 while (len--)
kono
parents:
diff changeset
16 cnt += __builtin_popcount(*set++);
kono
parents:
diff changeset
17 return cnt;
kono
parents:
diff changeset
18 }
kono
parents:
diff changeset
19 #endif
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #include "runtime.h"
kono
parents:
diff changeset
22 #include "defs.h"
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 int32
kono
parents:
diff changeset
25 getproccount(void)
kono
parents:
diff changeset
26 {
kono
parents:
diff changeset
27 cpu_set_t set;
kono
parents:
diff changeset
28 int32 r, cnt;
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 cnt = 0;
kono
parents:
diff changeset
31 r = sched_getaffinity(0, sizeof(set), &set);
kono
parents:
diff changeset
32 if(r == 0)
kono
parents:
diff changeset
33 cnt += CPU_COUNT(&set);
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 return cnt ? cnt : 1;
kono
parents:
diff changeset
36 }