1
0
mirror of https://github.com/golang/go synced 2024-11-23 07:20:06 -07:00

runtime: FreeBSD fast clock_gettime HPET timecounter support

This is a followup for CL 93156.

Fixes #22942.

Change-Id: Ic6e2de44011d041b91454353a6f2e3b0cf590060
Reviewed-on: https://go-review.googlesource.com/108095
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Yuval Pavel Zholkover 2018-04-19 17:52:14 +03:00 committed by Ian Lance Taylor
parent 279b530d68
commit 58c231f244
6 changed files with 89 additions and 21 deletions

View File

@ -53,6 +53,7 @@ const (
PROT_EXEC = C.PROT_EXEC PROT_EXEC = C.PROT_EXEC
MAP_ANON = C.MAP_ANON MAP_ANON = C.MAP_ANON
MAP_SHARED = C.MAP_SHARED
MAP_PRIVATE = C.MAP_PRIVATE MAP_PRIVATE = C.MAP_PRIVATE
MAP_FIXED = C.MAP_FIXED MAP_FIXED = C.MAP_FIXED

View File

@ -22,6 +22,7 @@ const (
_PROT_EXEC = 0x4 _PROT_EXEC = 0x4
_MAP_ANON = 0x1000 _MAP_ANON = 0x1000
_MAP_SHARED = 0x1
_MAP_PRIVATE = 0x2 _MAP_PRIVATE = 0x2
_MAP_FIXED = 0x10 _MAP_FIXED = 0x10

View File

@ -22,6 +22,7 @@ const (
_PROT_EXEC = 0x4 _PROT_EXEC = 0x4
_MAP_ANON = 0x1000 _MAP_ANON = 0x1000
_MAP_SHARED = 0x1
_MAP_PRIVATE = 0x2 _MAP_PRIVATE = 0x2
_MAP_FIXED = 0x10 _MAP_FIXED = 0x10

View File

@ -22,6 +22,7 @@ const (
_PROT_EXEC = 0x4 _PROT_EXEC = 0x4
_MAP_ANON = 0x1000 _MAP_ANON = 0x1000
_MAP_SHARED = 0x1
_MAP_PRIVATE = 0x2 _MAP_PRIVATE = 0x2
_MAP_FIXED = 0x10 _MAP_FIXED = 0x10

View File

@ -16,37 +16,41 @@ const _VDSO_TH_NUM = 4 // defined in <sys/vdso.h> #ifdef _KERNEL
var timekeepSharedPage *vdsoTimekeep var timekeepSharedPage *vdsoTimekeep
//go:nosplit //go:nosplit
func (bt bintime) Add(bt2 bintime) bintime { func (bt *bintime) Add(bt2 *bintime) {
u := bt.frac u := bt.frac
bt.frac += bt2.frac bt.frac += bt2.frac
if u > bt.frac { if u > bt.frac {
bt.sec++ bt.sec++
} }
bt.sec += bt2.sec bt.sec += bt2.sec
return bt
} }
//go:nosplit //go:nosplit
func (bt bintime) AddX(x uint64) bintime { func (bt *bintime) AddX(x uint64) {
u := bt.frac u := bt.frac
bt.frac += x bt.frac += x
if u > bt.frac { if u > bt.frac {
bt.sec++ bt.sec++
} }
return bt
} }
var binuptimeDummy uint32 var (
// binuptimeDummy is used in binuptime as the address of an atomic.Load, to simulate
// an atomic_thread_fence_acq() call which behaves as an instruction reordering and
// memory barrier.
binuptimeDummy uint32
zeroBintime bintime
)
// based on /usr/src/lib/libc/sys/__vdso_gettimeofday.c // based on /usr/src/lib/libc/sys/__vdso_gettimeofday.c
// //
//go:nosplit //go:nosplit
func binuptime(abs bool) (bintime, bool) { func binuptime(abs bool) (bt bintime) {
var bt bintime
timehands := (*[_VDSO_TH_NUM]vdsoTimehands)(add(unsafe.Pointer(timekeepSharedPage), vdsoTimekeepSize)) timehands := (*[_VDSO_TH_NUM]vdsoTimehands)(add(unsafe.Pointer(timekeepSharedPage), vdsoTimekeepSize))
for { for {
if timekeepSharedPage.enabled == 0 { if timekeepSharedPage.enabled == 0 {
return bt, false return zeroBintime
} }
curr := atomic.Load(&timekeepSharedPage.current) // atomic_load_acq_32 curr := atomic.Load(&timekeepSharedPage.current) // atomic_load_acq_32
@ -55,13 +59,13 @@ func binuptime(abs bool) (bintime, bool) {
bt = th.offset bt = th.offset
if tc, ok := th.getTimecounter(); !ok { if tc, ok := th.getTimecounter(); !ok {
return bt, false return zeroBintime
} else { } else {
delta := (tc - th.offset_count) & th.counter_mask delta := (tc - th.offset_count) & th.counter_mask
bt = bt.AddX(th.scale * uint64(delta)) bt.AddX(th.scale * uint64(delta))
} }
if abs { if abs {
bt = bt.Add(th.boottime) bt.Add(&th.boottime)
} }
atomic.Load(&binuptimeDummy) // atomic_thread_fence_acq() atomic.Load(&binuptimeDummy) // atomic_thread_fence_acq()
@ -69,13 +73,13 @@ func binuptime(abs bool) (bintime, bool) {
break break
} }
} }
return bt, true return bt
} }
//go:nosplit //go:nosplit
func vdsoClockGettime(clockID int32) (bintime, bool) { func vdsoClockGettime(clockID int32) bintime {
if timekeepSharedPage == nil || timekeepSharedPage.ver != _VDSO_TK_VER_CURR { if timekeepSharedPage == nil || timekeepSharedPage.ver != _VDSO_TK_VER_CURR {
return bintime{}, false return zeroBintime
} }
abs := false abs := false
switch clockID { switch clockID {
@ -84,9 +88,8 @@ func vdsoClockGettime(clockID int32) (bintime, bool) {
case _CLOCK_REALTIME: case _CLOCK_REALTIME:
abs = true abs = true
default: default:
return bintime{}, false return zeroBintime
} }
return binuptime(abs) return binuptime(abs)
} }
@ -95,16 +98,16 @@ func fallback_walltime() (sec int64, nsec int32)
//go:nosplit //go:nosplit
func nanotime() int64 { func nanotime() int64 {
bt, ok := vdsoClockGettime(_CLOCK_MONOTONIC) bt := vdsoClockGettime(_CLOCK_MONOTONIC)
if !ok { if bt == zeroBintime {
return fallback_nanotime() return fallback_nanotime()
} }
return int64((1e9 * uint64(bt.sec)) + ((1e9 * uint64(bt.frac>>32)) >> 32)) return int64((1e9 * uint64(bt.sec)) + ((1e9 * uint64(bt.frac>>32)) >> 32))
} }
func walltime() (sec int64, nsec int32) { func walltime() (sec int64, nsec int32) {
bt, ok := vdsoClockGettime(_CLOCK_REALTIME) bt := vdsoClockGettime(_CLOCK_REALTIME)
if !ok { if bt == zeroBintime {
return fallback_walltime() return fallback_walltime()
} }
return int64(bt.sec), int32((1e9 * uint64(bt.frac>>32)) >> 32) return int64(bt.sec), int32((1e9 * uint64(bt.frac>>32)) >> 32)

View File

@ -7,8 +7,35 @@
package runtime package runtime
import (
"runtime/internal/atomic"
"unsafe"
)
const ( const (
_VDSO_TH_ALGO_X86_TSC = 1 _VDSO_TH_ALGO_X86_TSC = 1
_VDSO_TH_ALGO_X86_HPET = 2
)
const (
_HPET_DEV_MAP_MAX = 10
_HPET_MAIN_COUNTER = 0xf0 /* Main counter register */
)
var (
hpetDevMap [_HPET_DEV_MAP_MAX]uintptr
hpetDevPath = [_HPET_DEV_MAP_MAX][11]byte{
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '0', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '1', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '2', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '3', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '4', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '5', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '6', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '7', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '8', 0},
{'/', 'd', 'e', 'v', '/', 'h', 'p', 'e', 't', '9', 0},
}
) )
//go:nosplit //go:nosplit
@ -20,11 +47,45 @@ func (th *vdsoTimehands) getTSCTimecounter() uint32 {
return uint32(tsc) return uint32(tsc)
} }
//go:nosplit
func (th *vdsoTimehands) getHPETTimecounter() (uint32, bool) {
idx := int(th.x86_hpet_idx)
if idx >= len(hpetDevMap) {
return 0, false
}
p := atomic.Loaduintptr(&hpetDevMap[idx])
if p == 0 {
fd := open(&hpetDevPath[idx][0], 0 /* O_RDONLY */, 0)
if fd < 0 {
atomic.Casuintptr(&hpetDevMap[idx], 0, ^uintptr(0))
return 0, false
}
addr, mmapErr := mmap(nil, physPageSize, _PROT_READ, _MAP_SHARED, fd, 0)
closefd(fd)
newP := uintptr(addr)
if mmapErr != 0 {
newP = ^uintptr(0)
}
if !atomic.Casuintptr(&hpetDevMap[idx], 0, newP) && mmapErr == 0 {
munmap(addr, physPageSize)
}
p = atomic.Loaduintptr(&hpetDevMap[idx])
}
if p == ^uintptr(0) {
return 0, false
}
return *(*uint32)(unsafe.Pointer(p + _HPET_MAIN_COUNTER)), true
}
//go:nosplit //go:nosplit
func (th *vdsoTimehands) getTimecounter() (uint32, bool) { func (th *vdsoTimehands) getTimecounter() (uint32, bool) {
switch th.algo { switch th.algo {
case _VDSO_TH_ALGO_X86_TSC: case _VDSO_TH_ALGO_X86_TSC:
return th.getTSCTimecounter(), true return th.getTSCTimecounter(), true
case _VDSO_TH_ALGO_X86_HPET:
return th.getHPETTimecounter()
default: default:
return 0, false return 0, false
} }