Skip to content

Commit

Permalink
time: record monotonic clock reading in time.Now, for more accurate c…
Browse files Browse the repository at this point in the history
…omparisons

See https://golang.org/design/12914-monotonic for details.

Fixes golang#12914.

Change-Id: I80edc2e6c012b4ace7161c84cf067d444381a009
Reviewed-on: https://go-review.googlesource.com/36255
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Caleb Spare <cespare@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Feb 3, 2017
1 parent 8179b9b commit 0e33559
Show file tree
Hide file tree
Showing 43 changed files with 629 additions and 129 deletions.
15 changes: 9 additions & 6 deletions src/archive/tar/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ func TestRoundTrip(t *testing.T) {
var b bytes.Buffer
tw := NewWriter(&b)
hdr := &Header{
Name: "file.txt",
Uid: 1 << 21, // too big for 8 octal digits
Size: int64(len(data)),
ModTime: time.Now(),
Name: "file.txt",
Uid: 1 << 21, // too big for 8 octal digits
Size: int64(len(data)),
// AddDate to strip monotonic clock reading,
// and Round to discard sub-second precision,
// both of which are not included in the tar header
// and would otherwise break the round-trip check
// below.
ModTime: time.Now().AddDate(0, 0, 0).Round(1 * time.Second),
}
// tar only supports second precision.
hdr.ModTime = hdr.ModTime.Add(-time.Duration(hdr.ModTime.Nanosecond()) * time.Nanosecond)
if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("tw.WriteHeader: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/gob/gobencdec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func (i *isZeroBugInterface) GobDecode(data []byte) error {
}

func TestGobEncodeIsZero(t *testing.T) {
x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
x := isZeroBug{time.Unix(1e9, 0), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
b := new(bytes.Buffer)
enc := NewEncoder(b)
err := enc.Encode(x)
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/testdata/expr3.src
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func struct_literals() {
_ = time.Time{}
_ = time.Time{sec /* ERROR "unknown field" */ : 0}
_ = time.Time{
0 /* ERROR implicit assignment to unexported field sec in time.Time literal */,
0 /* ERROR implicit assignment to unexported field wall in time.Time literal */,
0 /* ERROR implicit assignment */ ,
nil /* ERROR implicit assignment */ ,
}
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ func stackBarrier()
// in asm_*.s
func return0()

//go:linkname time_now time.now
func time_now() (sec int64, nsec int32)
func walltime() (sec int64, nsec int32)

// in asm_*.s
// not called directly; definitions here supply type information for traceback.
Expand Down Expand Up @@ -281,7 +280,7 @@ func prefetcht2(addr uintptr)
func prefetchnta(addr uintptr)

func unixnanotime() int64 {
sec, nsec := time_now()
sec, nsec := walltime()
return sec*1e9 + int64(nsec)
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_darwin_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ inreg:
ADCL $0, DX
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$0
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$0
CALL runtime·now(SB)
MOVL $1000000000, CX
DIVL CX
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ TEXT runtime·nanotime(SB),NOSPLIT,$0-8
MOVQ AX, ret+0(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$0-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$0-12
CALL nanotime<>(SB)

// generated code for
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sys_darwin_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$0
MOVW R0, ret+12(FP)
RET

TEXT time·now(SB), 7, $32
TEXT runtime·walltime(SB), 7, $32
MOVW $8(R13), R0 // timeval
MOVW $0, R1 // zone
MOVW $0, R2 // see issue 16570
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sys_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ TEXT runtime·setitimer(SB),NOSPLIT,$0
SVC $0x80
RET

TEXT time·now(SB),NOSPLIT,$40-12
TEXT runtime·walltime(SB),NOSPLIT,$40-12
MOVD RSP, R0 // timeval
MOVD R0, R9 // this is how dyld calls gettimeofday
MOVW $0, R1 // zone
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_dragonfly_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SYSCALL
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
MOVQ $0, DI // CLOCK_REALTIME
LEAQ 8(SP), SI
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_freebsd_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-4
INT $0x80
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
LEAL 12(SP), BX
MOVL $0, 4(SP) // CLOCK_REALTIME
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_freebsd_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SYSCALL
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
MOVQ $0, DI // CLOCK_REALTIME
LEAQ 8(SP), SI
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_freebsd_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SWI $0
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1
MOVW $SYS_clock_gettime, R7
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-16
MOVL AX, ret+12(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $265, AX // syscall - clock_gettime
MOVL $0, BX // CLOCK_REALTIME
LEAL 8(SP), CX
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-28
MOVL AX, ret+24(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
// Be careful. We're calling a function with gcc calling convention here.
// We're guaranteed 128 bytes on entry, and we've taken 16, and the
// call uses another 8.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sys_linux_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$0
MOVW R0, ret+12(FP)
RET

TEXT time·now(SB), NOSPLIT, $32
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1 // timespec
MOVW $SYS_clock_gettime, R7
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$-8-28
MOVW R0, ret+24(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$24-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$24-12
MOVW $0, R0 // CLOCK_REALTIME
MOVD RSP, R1
MOVD $SYS_clock_gettime, R8
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_mips64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$-8-28
MOVW R2, ret+24(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R4 // CLOCK_REALTIME
MOVV $0(R29), R5
MOVV $SYS_clock_gettime, R2
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_mipsx.s
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-16
MOVW R2, ret+12(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
MOVW $0, R4 // CLOCK_REALTIME
MOVW $4(R29), R5
MOVW $SYS_clock_gettime, R2
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_ppc64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
MOVW R3, ret+24(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVD $0, R3 // CLOCK_REALTIME
MOVD $0(R1), R4
SYSCALL $SYS_clock_gettime
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_linux_s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
MOVW R2, ret+24(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R2 // CLOCK_REALTIME
MOVD $tp-16(SP), R3
MOVW $SYS_clock_gettime, R1
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_nacl_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$32
MOVL AX, ret+24(FP)
RET

TEXT time·now(SB),NOSPLIT,$20
TEXT runtime·walltime(SB),NOSPLIT,$20
MOVL $0, 0(SP) // real time clock
LEAL 8(SP), AX
MOVL AX, 4(SP) // timespec
Expand All @@ -249,7 +249,7 @@ TEXT time·now(SB),NOSPLIT,$20
RET

TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
JMP runtime·walltime(SB)

TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$8
MOVL arg1+0(FP), AX
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_nacl_amd64p32.s
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$8
MOVL AX, ret+24(FP)
RET

TEXT time·now(SB),NOSPLIT,$16
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVQ runtime·faketime(SB), AX
CMPQ AX, $0
JEQ realtime
Expand All @@ -268,7 +268,7 @@ realtime:
RET

TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
JMP runtime·walltime(SB)

TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
MOVL arg1+0(FP), DI
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_nacl_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$8
MOVW R0, ret+24(FP)
RET

TEXT time·now(SB),NOSPLIT,$16
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R0 // real time clock
MOVW $4(R13), R1
NACL_SYSCALL(SYS_clock_gettime)
Expand All @@ -209,7 +209,7 @@ TEXT time·now(SB),NOSPLIT,$16
RET

TEXT syscall·now(SB),NOSPLIT,$0
B time·now(SB)
B runtime·walltime(SB)

TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
MOVW arg1+0(FP), R0
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_netbsd_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
INT $0x80
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
LEAL 12(SP), BX
MOVL $0, 4(SP) // arg 1 - clock_id
MOVL BX, 8(SP) // arg 2 - tp
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_netbsd_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-8
SYSCALL
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVQ $0, DI // arg 1 - clock_id
LEAQ 8(SP), SI // arg 2 - tp
MOVL $427, AX // sys_clock_gettime
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_netbsd_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
SWI $0xa001a9 // sys_setitimer
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1
SWI $0xa001ab // clock_gettime
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_openbsd_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
INT $0x80
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
LEAL 12(SP), BX
MOVL $0, 4(SP) // arg 1 - clock_id
MOVL BX, 8(SP) // arg 2 - tp
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_openbsd_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-8
SYSCALL
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVQ $0, DI // arg 1 - clock_id
LEAQ 8(SP), SI // arg 2 - tp
MOVL $87, AX // sys_clock_gettime
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_openbsd_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$0
SWI $0
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW CLOCK_REALTIME, R0 // arg 1 - clock_id
MOVW $8(R13), R1 // arg 2 - tp
MOVW $87, R12 // sys_clock_gettime
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_plan9_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ TEXT runtime·nsec(SB),NOSPLIT,$8
MOVL $-1, ret_hi+8(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVL 0(SP), AX
MOVL 4(SP), DX
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_plan9_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ TEXT runtime·nsec(SB),NOSPLIT,$0
MOVQ AX, ret+8(FP)
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVQ 0(SP), AX

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sys_plan9_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ TEXT runtime·nsec(SB),NOSPLIT,$-4-12
RET

// time.now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$12-12
TEXT runtime·walltime(SB),NOSPLIT,$12-12
// use nsec system call to get current time in nanoseconds
MOVW $sysnsec_lo-8(SP), R0 // destination addr
MOVW R0,res-12(SP)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/sys_solaris_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ TEXT runtime·osyield1(SB),NOSPLIT,$0
CALL AX
RET

// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVQ 0(SP), AX

Expand Down
Loading

0 comments on commit 0e33559

Please sign in to comment.