Skip to content

Commit cfd016d

Browse files
committed
runtime: on windows, read nanotime with one instruction or issue barrier
On 64-bit, this is more efficient, and on ARM64, this prevents the time from moving backwards due to the weaker memory model. On ARM32 due to the weaker memory model, we issue a memory barrier. Updates #48072. Change-Id: If4695716c3039d8af14e14808af217f5c99fc93a Reviewed-on: https://go-review.googlesource.com/c/go/+/361057 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
1 parent d4e0e8e commit cfd016d

7 files changed

+17
-54
lines changed

src/runtime/sys_windows_amd64.s

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,9 @@ TEXT runtime·nanotime1(SB),NOSPLIT,$0-8
344344
CMPB runtime·useQPCTime(SB), $0
345345
JNE useQPC
346346
MOVQ $_INTERRUPT_TIME, DI
347-
loop:
348-
MOVL time_hi1(DI), AX
349-
MOVL time_lo(DI), BX
350-
MOVL time_hi2(DI), CX
351-
CMPL AX, CX
352-
JNE loop
353-
SHLQ $32, CX
354-
ORQ BX, CX
355-
IMULQ $100, CX
356-
MOVQ CX, ret+0(FP)
347+
MOVQ time_lo(DI), AX
348+
IMULQ $100, AX
349+
MOVQ AX, ret+0(FP)
357350
RET
358351
useQPC:
359352
JMP runtime·nanotimeQPC(SB)

src/runtime/sys_windows_arm.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ TEXT runtime·nanotime1(SB),NOSPLIT|NOFRAME,$0-8
350350
MOVW $_INTERRUPT_TIME, R3
351351
loop:
352352
MOVW time_hi1(R3), R1
353+
DMB MB_ISH
353354
MOVW time_lo(R3), R0
355+
DMB MB_ISH
354356
MOVW time_hi2(R3), R2
355357
CMP R1, R2
356358
BNE loop

src/runtime/sys_windows_arm64.s

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,7 @@ TEXT runtime·nanotime1(SB),NOSPLIT|NOFRAME,$0-8
415415
CMP $0, R0
416416
BNE useQPC
417417
MOVD $_INTERRUPT_TIME, R3
418-
loop:
419-
MOVWU time_hi1(R3), R1
420-
MOVWU time_lo(R3), R0
421-
MOVWU time_hi2(R3), R2
422-
CMP R1, R2
423-
BNE loop
424-
425-
// wintime = R1:R0, multiply by 100
426-
ORR R1<<32, R0
418+
MOVD time_lo(R3), R0
427419
MOVD $100, R1
428420
MUL R1, R0
429421
MOVD R0, ret+0(FP)

src/runtime/time_windows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// http://web.archive.org/web/20210411000829/https://wrkhpi.wordpress.com/2007/08/09/getting-os-information-the-kuser_shared_data-structure/
1010

1111
// Must read hi1, then lo, then hi2. The snapshot is valid if hi1 == hi2.
12+
// Or, on 64-bit, just read lo:hi1 all at once atomically.
1213
#define _INTERRUPT_TIME 0x7ffe0008
1314
#define _SYSTEM_TIME 0x7ffe0014
1415
#define time_lo 0

src/runtime/time_windows_amd64.s

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,14 @@
1212
TEXT time·now(SB),NOSPLIT,$0-24
1313
CMPB runtime·useQPCTime(SB), $0
1414
JNE useQPC
15+
1516
MOVQ $_INTERRUPT_TIME, DI
16-
loop:
17-
MOVL time_hi1(DI), AX
18-
MOVL time_lo(DI), BX
19-
MOVL time_hi2(DI), CX
20-
CMPL AX, CX
21-
JNE loop
22-
SHLQ $32, AX
23-
ORQ BX, AX
17+
MOVQ time_lo(DI), AX
2418
IMULQ $100, AX
2519
MOVQ AX, mono+16(FP)
2620

2721
MOVQ $_SYSTEM_TIME, DI
28-
wall:
29-
MOVL time_hi1(DI), AX
30-
MOVL time_lo(DI), BX
31-
MOVL time_hi2(DI), CX
32-
CMPL AX, CX
33-
JNE wall
34-
SHLQ $32, AX
35-
ORQ BX, AX
22+
MOVQ time_lo(DI), AX
3623
MOVQ $116444736000000000, DI
3724
SUBQ DI, AX
3825
IMULQ $100, AX

src/runtime/time_windows_arm.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ TEXT time·now(SB),NOSPLIT|NOFRAME,$0-20
1717
MOVW $_INTERRUPT_TIME, R3
1818
loop:
1919
MOVW time_hi1(R3), R1
20+
DMB MB_ISH
2021
MOVW time_lo(R3), R0
22+
DMB MB_ISH
2123
MOVW time_hi2(R3), R2
2224
CMP R1, R2
2325
BNE loop
@@ -34,7 +36,9 @@ loop:
3436
MOVW $_SYSTEM_TIME, R3
3537
wall:
3638
MOVW time_hi1(R3), R1
39+
DMB MB_ISH
3740
MOVW time_lo(R3), R0
41+
DMB MB_ISH
3842
MOVW time_hi2(R3), R2
3943
CMP R1, R2
4044
BNE wall

src/runtime/time_windows_arm64.s

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,18 @@ TEXT time·now(SB),NOSPLIT|NOFRAME,$0-24
1313
MOVB runtime·useQPCTime(SB), R0
1414
CMP $0, R0
1515
BNE useQPC
16-
MOVD $_INTERRUPT_TIME, R3
17-
loop:
18-
MOVWU time_hi1(R3), R1
19-
MOVWU time_lo(R3), R0
20-
MOVWU time_hi2(R3), R2
21-
CMP R1, R2
22-
BNE loop
2316

24-
// wintime = R1:R0, multiply by 100
25-
ORR R1<<32, R0
17+
MOVD $_INTERRUPT_TIME, R3
18+
MOVD time_lo(R3), R0
2619
MOVD $100, R1
2720
MUL R1, R0
2821
MOVD R0, mono+16(FP)
2922

3023
MOVD $_SYSTEM_TIME, R3
31-
wall:
32-
MOVWU time_hi1(R3), R1
33-
MOVWU time_lo(R3), R0
34-
MOVWU time_hi2(R3), R2
35-
CMP R1, R2
36-
BNE wall
37-
38-
// w = R1:R0 in 100ns units
24+
MOVD time_lo(R3), R0
3925
// convert to Unix epoch (but still 100ns units)
4026
#define delta 116444736000000000
41-
ORR R1<<32, R0
4227
SUB $delta, R0
43-
4428
// Convert to nSec
4529
MOVD $100, R1
4630
MUL R1, R0

0 commit comments

Comments
 (0)