Skip to content

Commit a1ee0a2

Browse files
committed
runtime, time: refactor startNano handling
Move startNano from runtime to time package. In preparation for a subsequent change that speeds up Since and Until. This also makes code simpler as we have less assembly as the result, monotonic time handling is better localized in time package. This changes values returned from nanotime on windows (it does not account for startNano anymore), current comments state that it's important, but it's unclear how it can be important since no other OS does this. Update #25729 Change-Id: I2275d57b7b5ed8fd0d53eb0f19d55a86136cc555 Reviewed-on: https://go-review.googlesource.com/c/146340 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a86f549 commit a1ee0a2

File tree

9 files changed

+17
-34
lines changed

9 files changed

+17
-34
lines changed

src/runtime/proc.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ func main() {
157157
}
158158
}()
159159

160-
// Record when the world started. Must be after runtime_init
161-
// because nanotime on some platforms depends on startNano.
160+
// Record when the world started.
162161
runtimeInitTime = nanotime()
163162

164163
gcenable()

src/runtime/sys_windows_386.s

+1-6
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,7 @@ loop:
455455
MULL CX
456456
IMULL $100, DI
457457
ADDL DI, DX
458-
// wintime*100 = DX:AX, subtract startNano and return
459-
SUBL runtime·startNano+0(SB), AX
460-
SBBL runtime·startNano+4(SB), DX
458+
// wintime*100 = DX:AX
461459
MOVL AX, ret_lo+0(FP)
462460
MOVL DX, ret_hi+4(FP)
463461
RET
@@ -482,9 +480,6 @@ loop:
482480
IMULL $100, DI
483481
ADDL DI, DX
484482
// w*100 = DX:AX
485-
// subtract startNano and save for return
486-
SUBL runtime·startNano+0(SB), AX
487-
SBBL runtime·startNano+4(SB), DX
488483
MOVL AX, mono+12(FP)
489484
MOVL DX, mono+16(FP)
490485

src/runtime/sys_windows_amd64.s

-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ loop:
486486
SHLQ $32, CX
487487
ORQ BX, CX
488488
IMULQ $100, CX
489-
SUBQ runtime·startNano(SB), CX
490489
MOVQ CX, ret+0(FP)
491490
RET
492491
useQPC:
@@ -506,7 +505,6 @@ loop:
506505
SHLQ $32, AX
507506
ORQ BX, AX
508507
IMULQ $100, AX
509-
SUBQ runtime·startNano(SB), AX
510508
MOVQ AX, mono+16(FP)
511509

512510
MOVQ $_SYSTEM_TIME, DI

src/runtime/sys_windows_arm.s

+2-10
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,7 @@ loop:
510510
MULLU R0, R2, (R4, R3) // R4:R3 = R1:R0 * R2
511511
MULA R1, R2, R4, R4
512512

513-
// wintime*100 = R4:R3, subtract startNano and return
514-
MOVW runtime·startNano+0(SB), R0
515-
MOVW runtime·startNano+4(SB), R1
516-
SUB.S R0, R3
517-
SBC R1, R4
513+
// wintime*100 = R4:R3
518514
MOVW R3, ret_lo+0(FP)
519515
MOVW R4, ret_hi+4(FP)
520516
RET
@@ -540,11 +536,7 @@ loop:
540536
MULLU R0, R2, (R4, R3) // R4:R3 = R1:R0 * R2
541537
MULA R1, R2, R4, R4
542538

543-
// wintime*100 = R4:R3, subtract startNano and return
544-
MOVW runtime·startNano+0(SB), R0
545-
MOVW runtime·startNano+4(SB), R1
546-
SUB.S R0, R3
547-
SBC R1, R4
539+
// wintime*100 = R4:R3
548540
MOVW R3, mono+12(FP)
549541
MOVW R4, mono+16(FP)
550542

src/runtime/time.go

-8
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,3 @@ func poll_runtimeNano() int64 {
470470
func time_runtimeNano() int64 {
471471
return nanotime()
472472
}
473-
474-
// Monotonic times are reported as offsets from startNano.
475-
// We initialize startNano to nanotime() - 1 so that on systems where
476-
// monotonic time resolution is fairly low (e.g. Windows 2008
477-
// which appears to have a default resolution of 15ms),
478-
// we avoid ever reporting a nanotime of 0.
479-
// (Callers may want to use 0 as "time not set".)
480-
var startNano int64 = nanotime() - 1

src/runtime/timeasm.go

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// license that can be found in the LICENSE file.
44

55
// Declarations for operating systems implementing time.now directly in assembly.
6-
// Those systems are also expected to have nanotime subtract startNano,
7-
// so that time.now and nanotime return the same monotonic clock readings.
86

97
// +build windows
108

src/runtime/timestub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ import _ "unsafe" // for go:linkname
1414
//go:linkname time_now time.now
1515
func time_now() (sec int64, nsec int32, mono int64) {
1616
sec, nsec = walltime()
17-
return sec, nsec, nanotime() - startNano
17+
return sec, nsec, nanotime()
1818
}

src/time/sleep.go

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ package time
88
// A negative or zero duration causes Sleep to return immediately.
99
func Sleep(d Duration)
1010

11-
// runtimeNano returns the current value of the runtime clock in nanoseconds.
12-
func runtimeNano() int64
13-
1411
// Interface to timers implemented in package runtime.
1512
// Must be in sync with ../runtime/time.go:/^type timer
1613
type runtimeTimer struct {

src/time/time.go

+12
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,21 @@ func daysIn(m Month, year int) int {
10501050
// Provided by package runtime.
10511051
func now() (sec int64, nsec int32, mono int64)
10521052

1053+
// runtimeNano returns the current value of the runtime clock in nanoseconds.
1054+
func runtimeNano() int64
1055+
1056+
// Monotonic times are reported as offsets from startNano.
1057+
// We initialize startNano to runtimeNano() - 1 so that on systems where
1058+
// monotonic time resolution is fairly low (e.g. Windows 2008
1059+
// which appears to have a default resolution of 15ms),
1060+
// we avoid ever reporting a monotonic time of 0.
1061+
// (Callers may want to use 0 as "time not set".)
1062+
var startNano int64 = runtimeNano() - 1
1063+
10531064
// Now returns the current local time.
10541065
func Now() Time {
10551066
sec, nsec, mono := now()
1067+
mono -= startNano
10561068
sec += unixToInternal - minWall
10571069
if uint64(sec)>>33 != 0 {
10581070
return Time{uint64(nsec), sec + minWall, Local}

0 commit comments

Comments
 (0)