Commit 4f308edc authored by Shenghou Ma's avatar Shenghou Ma

runtime: use sched_getaffinity for runtime.NumCPU() on Linux

        Fixes #3921.

R=iant
CC=golang-dev
https://golang.org/cl/6448132
parent c5038c85
...@@ -423,3 +423,11 @@ TEXT runtime·osyield(SB),7,$0 ...@@ -423,3 +423,11 @@ TEXT runtime·osyield(SB),7,$0
MOVL $158, AX MOVL $158, AX
CALL *runtime·_vdso(SB) CALL *runtime·_vdso(SB)
RET RET
TEXT runtime·sched_getaffinity(SB),7,$0
MOVL $242, AX // syscall - sched_getaffinity
MOVL 4(SP), BX
MOVL 8(SP), CX
MOVL 12(SP), DX
CALL *runtime·_vdso(SB)
RET
...@@ -310,3 +310,11 @@ TEXT runtime·osyield(SB),7,$0 ...@@ -310,3 +310,11 @@ TEXT runtime·osyield(SB),7,$0
MOVL $24, AX MOVL $24, AX
SYSCALL SYSCALL
RET RET
TEXT runtime·sched_getaffinity(SB),7,$0
MOVQ 8(SP), DI
MOVL 16(SP), SI
MOVQ 24(SP), DX
MOVL $204, AX // syscall entry
SYSCALL
RET
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#define SYS_sched_yield (SYS_BASE + 158) #define SYS_sched_yield (SYS_BASE + 158)
#define SYS_select (SYS_BASE + 142) // newselect #define SYS_select (SYS_BASE + 142) // newselect
#define SYS_ugetrlimit (SYS_BASE + 191) #define SYS_ugetrlimit (SYS_BASE + 191)
#define SYS_sched_getaffinity (SYS_BASE + 242)
#define ARM_BASE (SYS_BASE + 0x0f0000) #define ARM_BASE (SYS_BASE + 0x0f0000)
#define SYS_ARM_cacheflush (ARM_BASE + 2) #define SYS_ARM_cacheflush (ARM_BASE + 2)
...@@ -393,3 +394,11 @@ TEXT runtime·osyield(SB),7,$0 ...@@ -393,3 +394,11 @@ TEXT runtime·osyield(SB),7,$0
MOVW $SYS_sched_yield, R7 MOVW $SYS_sched_yield, R7
SWI $0 SWI $0
RET RET
TEXT runtime·sched_getaffinity(SB),7,$0
MOVW 0(FP), R0
MOVW 4(FP), R1
MOVW 8(FP), R2
MOVW $SYS_sched_getaffinity, R7
SWI $0
RET
...@@ -80,33 +80,23 @@ runtime·futexwakeup(uint32 *addr, uint32 cnt) ...@@ -80,33 +80,23 @@ runtime·futexwakeup(uint32 *addr, uint32 cnt)
*(int32*)0x1006 = 0x1006; *(int32*)0x1006 = 0x1006;
} }
extern runtime·sched_getaffinity(uintptr pid, uintptr len, uintptr *buf);
static int32 static int32
getproccount(void) getproccount(void)
{ {
int32 fd, rd, cnt, cpustrlen; uintptr buf[16], t;
byte *cpustr, *pos, *bufpos; int32 r, cnt, i;
byte buf[256];
fd = runtime·open((byte*)"/proc/stat", O_RDONLY|O_CLOEXEC, 0);
if(fd == -1)
return 1;
cnt = 0; cnt = 0;
bufpos = buf; r = runtime·sched_getaffinity(0, sizeof(buf), buf);
cpustr = (byte*)"\ncpu"; if(r > 0)
cpustrlen = runtime·findnull(cpustr); for(i = 0; i < r/sizeof(buf[0]); i++) {
for(;;) { t = buf[i];
rd = runtime·read(fd, bufpos, sizeof(buf)-cpustrlen); t = t - ((t >> 1) & 0x5555555555555555ULL);
if(rd == -1) t = (t & 0x3333333333333333ULL) + ((t >> 2) & 0x3333333333333333ULL);
break; cnt += (int32)((((t + (t >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
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;
} }
runtime·close(fd);
return cnt ? cnt : 1; return cnt ? cnt : 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment