diff options
| author | Shenghou Ma <minux.ma@gmail.com> | 2012-08-10 10:05:26 +0800 |
|---|---|---|
| committer | Shenghou Ma <minux.ma@gmail.com> | 2012-08-10 10:05:26 +0800 |
| commit | 4f308edc864a87ff4f6f9d6c531733cb1bf100f1 (patch) | |
| tree | 33667df64d6c50b4e1b266a3585a2f8b5d6acac9 /src/pkg/runtime/thread_linux.c | |
| parent | c5038c85933a69a1ddeae812d601eb11e71cdc58 (diff) | |
| download | go-4f308edc864a87ff4f6f9d6c531733cb1bf100f1.tar.xz | |
runtime: use sched_getaffinity for runtime.NumCPU() on Linux
Fixes #3921.
R=iant
CC=golang-dev
https://golang.org/cl/6448132
Diffstat (limited to 'src/pkg/runtime/thread_linux.c')
| -rw-r--r-- | src/pkg/runtime/thread_linux.c | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/src/pkg/runtime/thread_linux.c b/src/pkg/runtime/thread_linux.c index 5db074175c..f66d2dd4d2 100644 --- a/src/pkg/runtime/thread_linux.c +++ b/src/pkg/runtime/thread_linux.c @@ -80,33 +80,23 @@ runtime·futexwakeup(uint32 *addr, uint32 cnt) *(int32*)0x1006 = 0x1006; } +extern runtime·sched_getaffinity(uintptr pid, uintptr len, uintptr *buf); static int32 getproccount(void) { - int32 fd, rd, cnt, cpustrlen; - byte *cpustr, *pos, *bufpos; - byte buf[256]; + uintptr buf[16], t; + int32 r, cnt, i; - fd = runtime·open((byte*)"/proc/stat", O_RDONLY|O_CLOEXEC, 0); - if(fd == -1) - return 1; cnt = 0; - bufpos = buf; - cpustr = (byte*)"\ncpu"; - cpustrlen = runtime·findnull(cpustr); - for(;;) { - rd = runtime·read(fd, bufpos, sizeof(buf)-cpustrlen); - if(rd == -1) - break; - bufpos[rd] = 0; - for(pos=buf; pos=runtime·strstr(pos, cpustr); cnt++, pos++) { - } - if(rd < cpustrlen) - break; - runtime·memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1); - bufpos = buf+cpustrlen-1; + r = runtime·sched_getaffinity(0, sizeof(buf), buf); + if(r > 0) + for(i = 0; i < r/sizeof(buf[0]); i++) { + t = buf[i]; + t = t - ((t >> 1) & 0x5555555555555555ULL); + t = (t & 0x3333333333333333ULL) + ((t >> 2) & 0x3333333333333333ULL); + cnt += (int32)((((t + (t >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56); } - runtime·close(fd); + return cnt ? cnt : 1; } |
