Skip to content

Commit 23fd10b

Browse files
pmurmknyszek
authored andcommitted
[release-branch.go1.19] runtime: fix usleep on linux/PPC64
The existing implementation fails to convert the remainder microseconds to nanoseconds. This causes sysmon to consume much more cpu, and generate lots of context switches. We can also do a little better here to avoid division by a constant. I used go to determine the magic numbers. Fixes #56397 Change-Id: I2e37ec218b9027efab6db4634eed1504c0c1b3c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/444735 Reviewed-by: Lynn Boger <[email protected]> Run-TryBot: Paul Murphy <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/445157
1 parent 6109c07 commit 23fd10b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/runtime/sys_linux_ppc64x.s

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,23 @@ TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
111111
MOVW R3, errno+16(FP)
112112
RET
113113

114+
// func usleep(usec uint32)
114115
TEXT runtime·usleep(SB),NOSPLIT,$16-4
115116
MOVW usec+0(FP), R3
116-
MOVD R3, R5
117-
MOVW $1000000, R4
118-
DIVD R4, R3
119-
MOVD R3, 8(R1)
120-
MOVW $1000, R4
121-
MULLD R3, R4
122-
SUB R4, R5
123-
MOVD R5, 16(R1)
117+
118+
// Use magic constant 0x8637bd06 and shift right 51
119+
// to perform usec/1000000.
120+
ORIS $0x8637, R0, R4 // Note, R0 always contains 0 here.
121+
OR $0xbd06, R4, R4
122+
MULLD R3, R4, R4 // Convert usec to S.
123+
SRD $51, R4, R4
124+
MOVD R4, 8(R1) // Store to tv_sec
125+
126+
MOVD $1000000, R5
127+
MULLW R4, R5, R5 // Convert tv_sec back into uS
128+
SUB R5, R3, R5 // Compute remainder uS.
129+
MULLD $1000, R5, R5 // Convert to nsec
130+
MOVD R5, 16(R1) // Store to tv_nsec
124131

125132
// nanosleep(&ts, 0)
126133
ADD $8, R1, R3

0 commit comments

Comments
 (0)