Skip to content

Commit 2df5cdb

Browse files
jerrinsgianlancetaylor
authored andcommitted
runtime: make nanotime use monotonic clock in Solaris
nanotime() currently uses the REALTIME clock to get the elapsed time in Solaris. This commit changes it to use the MONOTONIC clock instead, similar to how it's done in Linux and other OSs. Also changed nanotime() and walltime() to call clock_gettime() library function directly from Go code rather than from assembly. Fixes #33674 Change-Id: Ie4a687b17d2140998ecd97af6ce048c86cf5fc02 Reviewed-on: https://go-review.googlesource.com/c/go/+/199502 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Aram Hăvărneanu <[email protected]>
1 parent c1ccae4 commit 2df5cdb

File tree

3 files changed

+14
-42
lines changed

3 files changed

+14
-42
lines changed

src/runtime/os3_solaris.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,16 @@ func munmap(addr unsafe.Pointer, n uintptr) {
393393
sysvicall2(&libc_munmap, uintptr(addr), uintptr(n))
394394
}
395395

396-
func nanotime2()
396+
const (
397+
_CLOCK_REALTIME = 3
398+
_CLOCK_MONOTONIC = 4
399+
)
397400

398401
//go:nosplit
399402
func nanotime1() int64 {
400-
return int64(sysvicall0((*libcFunc)(unsafe.Pointer(funcPC(nanotime2)))))
403+
var ts mts
404+
sysvicall2(&libc_clock_gettime, _CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)))
405+
return ts.tv_sec*1e9 + ts.tv_nsec
401406
}
402407

403408
//go:nosplit
@@ -498,6 +503,12 @@ func usleep(µs uint32) {
498503
usleep1(µs)
499504
}
500505

506+
func walltime1() (sec int64, nsec int32) {
507+
var ts mts
508+
sysvicall2(&libc_clock_gettime, _CLOCK_REALTIME, uintptr(unsafe.Pointer(&ts)))
509+
return ts.tv_sec, int32(ts.tv_nsec)
510+
}
511+
501512
//go:nosplit
502513
func write1(fd uintptr, buf unsafe.Pointer, nbyte int32) int32 {
503514
return int32(sysvicall3(&libc_write, uintptr(fd), uintptr(buf), uintptr(nbyte)))

src/runtime/sys_solaris_amd64.s

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,6 @@ TEXT runtime·miniterrno(SB),NOSPLIT,$0
2929
MOVQ AX, (m_mOS+mOS_perrno)(BX)
3030
RET
3131

32-
// int64 runtime·nanotime2(void);
33-
//
34-
// clock_gettime(3c) wrapper because Timespec is too large for
35-
// runtime·nanotime stack.
36-
//
37-
// Called using runtime·sysvicall6 from os_solaris.c:/nanotime.
38-
// NOT USING GO CALLING CONVENTION.
39-
TEXT runtime·nanotime2(SB),NOSPLIT,$0
40-
// need space for the timespec argument.
41-
SUBQ $64, SP // 16 bytes will do, but who knows in the future?
42-
MOVQ $3, DI // CLOCK_REALTIME from <sys/time_impl.h>
43-
MOVQ SP, SI
44-
LEAQ libc_clock_gettime(SB), AX
45-
CALL AX
46-
MOVQ (SP), AX // tv_sec from struct timespec
47-
IMULQ $1000000000, AX // multiply into nanoseconds
48-
ADDQ 8(SP), AX // tv_nsec, offset should be stable.
49-
ADDQ $64, SP
50-
RET
51-
5232
// pipe(3c) wrapper that returns fds in AX, DX.
5333
// NOT USING GO CALLING CONVENTION.
5434
TEXT runtime·pipe1(SB),NOSPLIT,$0
@@ -338,23 +318,3 @@ TEXT runtime·osyield1(SB),NOSPLIT,$0
338318
LEAQ libc_sched_yield(SB), AX
339319
CALL AX
340320
RET
341-
342-
// func walltime1() (sec int64, nsec int32)
343-
TEXT runtime·walltime1(SB),NOSPLIT,$8-12
344-
CALL runtime·nanotime1(SB)
345-
MOVQ 0(SP), AX
346-
347-
// generated code for
348-
// func f(x uint64) (uint64, uint64) { return x/1000000000, x%100000000 }
349-
// adapted to reduce duplication
350-
MOVQ AX, CX
351-
MOVQ $1360296554856532783, AX
352-
MULQ CX
353-
ADDQ CX, DX
354-
RCRQ $1, DX
355-
SHRQ $29, DX
356-
MOVQ DX, sec+0(FP)
357-
IMULQ $1000000000, DX
358-
SUBQ DX, CX
359-
MOVL CX, nsec+8(FP)
360-
RET

src/runtime/timestub2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// +build !windows
77
// +build !freebsd
88
// +build !aix
9+
// +build !solaris
910

1011
package runtime
1112

0 commit comments

Comments
 (0)