From 6c81ef8f67ca3f42fc9cd71dfbd5f35b0c4b5771 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 18 Mar 2019 07:51:36 +0000 Subject: [PATCH] unix: add SysctlClockinfo on OpenBSD OpenBSD (like NetBSD) uses sysctl with struct clockinfo to get clock rate information from the kernel. Add type Clockinfo and the SysctlClockinfo function to query this information. Change-Id: I35070a82b8de23dcd7592e8654dcc5eeee143b5b Reviewed-on: https://go-review.googlesource.com/c/sys/+/168057 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Matt Layher --- unix/syscall_openbsd.go | 17 +++++++++++++++++ unix/syscall_openbsd_test.go | 9 +++++++++ unix/types_openbsd.go | 6 ++++++ unix/ztypes_openbsd_386.go | 10 ++++++++++ unix/ztypes_openbsd_amd64.go | 10 ++++++++++ unix/ztypes_openbsd_arm.go | 10 ++++++++++ 6 files changed, 62 insertions(+) diff --git a/unix/syscall_openbsd.go b/unix/syscall_openbsd.go index 68799954..c8648ec0 100644 --- a/unix/syscall_openbsd.go +++ b/unix/syscall_openbsd.go @@ -43,6 +43,23 @@ func nametomib(name string) (mib []_C_int, err error) { return nil, EINVAL } +func SysctlClockinfo(name string) (*Clockinfo, error) { + mib, err := sysctlmib(name) + if err != nil { + return nil, err + } + + n := uintptr(SizeofClockinfo) + var ci Clockinfo + if err := sysctl(mib, (*byte)(unsafe.Pointer(&ci)), &n, nil, 0); err != nil { + return nil, err + } + if n != SizeofClockinfo { + return nil, EIO + } + return &ci, nil +} + func SysctlUvmexp(name string) (*Uvmexp, error) { mib, err := sysctlmib(name) if err != nil { diff --git a/unix/syscall_openbsd_test.go b/unix/syscall_openbsd_test.go index b95f334e..7bf75eed 100644 --- a/unix/syscall_openbsd_test.go +++ b/unix/syscall_openbsd_test.go @@ -40,6 +40,15 @@ func TestPpoll(t *testing.T) { } } +func TestSysctlClockinfo(t *testing.T) { + ci, err := unix.SysctlClockinfo("kern.clockrate") + if err != nil { + t.Fatal(err) + } + t.Logf("tick = %v, tickadj = %v, hz = %v, profhz = %v, stathz = %v", + ci.Tick, ci.Tickadj, ci.Hz, ci.Profhz, ci.Stathz) +} + func TestSysctlUvmexp(t *testing.T) { uvm, err := unix.SysctlUvmexp("vm.uvmexp") if err != nil { diff --git a/unix/types_openbsd.go b/unix/types_openbsd.go index 4e5e57f9..8aafbe44 100644 --- a/unix/types_openbsd.go +++ b/unix/types_openbsd.go @@ -274,3 +274,9 @@ type Utsname C.struct_utsname const SizeofUvmexp = C.sizeof_struct_uvmexp type Uvmexp C.struct_uvmexp + +// Clockinfo + +const SizeofClockinfo = C.sizeof_struct_clockinfo + +type Clockinfo C.struct_clockinfo diff --git a/unix/ztypes_openbsd_386.go b/unix/ztypes_openbsd_386.go index 8b37d839..900fb446 100644 --- a/unix/ztypes_openbsd_386.go +++ b/unix/ztypes_openbsd_386.go @@ -558,3 +558,13 @@ type Uvmexp struct { Fpswtch int32 Kmapent int32 } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +} diff --git a/unix/ztypes_openbsd_amd64.go b/unix/ztypes_openbsd_amd64.go index 6efea463..028fa78d 100644 --- a/unix/ztypes_openbsd_amd64.go +++ b/unix/ztypes_openbsd_amd64.go @@ -558,3 +558,13 @@ type Uvmexp struct { Fpswtch int32 Kmapent int32 } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +} diff --git a/unix/ztypes_openbsd_arm.go b/unix/ztypes_openbsd_arm.go index 510efc3e..b45d5eed 100644 --- a/unix/ztypes_openbsd_arm.go +++ b/unix/ztypes_openbsd_arm.go @@ -559,3 +559,13 @@ type Uvmexp struct { Fpswtch int32 Kmapent int32 } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +}