From 8ccfc680374f2d48b5d05f16bd7f6a3cef1bdb99 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 27 Sep 2018 09:25:26 +0000 Subject: [PATCH] unix: add SysctlClockinfo on NetBSD 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. This will be used in github.com/tklauser/go-sysconf to get _SC_CLK_TCK on NetBSD. Change-Id: I9e67d766f491ec3b460f26cb243b3595f0ba4d69 Reviewed-on: https://go-review.googlesource.com/138035 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- unix/syscall_netbsd.go | 17 +++++++++++++++++ unix/syscall_netbsd_test.go | 15 +++++++++++++++ unix/types_netbsd.go | 6 ++++++ unix/ztypes_netbsd_386.go | 10 ++++++++++ unix/ztypes_netbsd_amd64.go | 10 ++++++++++ unix/ztypes_netbsd_arm.go | 10 ++++++++++ 6 files changed, 68 insertions(+) diff --git a/unix/syscall_netbsd.go b/unix/syscall_netbsd.go index 6f8ebde3..639bcdef 100644 --- a/unix/syscall_netbsd.go +++ b/unix/syscall_netbsd.go @@ -93,6 +93,23 @@ func nametomib(name string) (mib []_C_int, err error) { return mib, nil } +func SysctlClockinfo(name string) (*Clockinfo, error) { + mib, err := sysctlmib(name) + if err != nil { + return nil, err + } + + n := uintptr(SizeofClockinfo) + buf := make([]byte, SizeofClockinfo) + if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil { + return nil, err + } + if n != SizeofClockinfo { + return nil, EIO + } + return (*Clockinfo)(unsafe.Pointer(&buf[0])), nil +} + //sysnb pipe() (fd1 int, fd2 int, err error) func Pipe(p []int) (err error) { if len(p) != 2 { diff --git a/unix/syscall_netbsd_test.go b/unix/syscall_netbsd_test.go index de8c0797..3573a133 100644 --- a/unix/syscall_netbsd_test.go +++ b/unix/syscall_netbsd_test.go @@ -4,6 +4,12 @@ package unix_test +import ( + "testing" + + "golang.org/x/sys/unix" +) + // stringsFromByteSlice converts a sequence of attributes to a []string. // On NetBSD, each entry consists of a single byte containing the length // of the attribute name, followed by the attribute name. @@ -18,3 +24,12 @@ func stringsFromByteSlice(buf []byte) []string { } return result } + +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) +} diff --git a/unix/types_netbsd.go b/unix/types_netbsd.go index 1494aafc..c49621ce 100644 --- a/unix/types_netbsd.go +++ b/unix/types_netbsd.go @@ -279,3 +279,9 @@ type Sysctlnode C.struct_sysctlnode // Uname type Utsname C.struct_utsname + +// Clockinfo + +const SizeofClockinfo = C.sizeof_struct_clockinfo + +type Clockinfo C.struct_clockinfo diff --git a/unix/ztypes_netbsd_386.go b/unix/ztypes_netbsd_386.go index 4b86fb2b..9e9088de 100644 --- a/unix/ztypes_netbsd_386.go +++ b/unix/ztypes_netbsd_386.go @@ -446,3 +446,13 @@ type Utsname struct { Version [256]byte Machine [256]byte } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +} diff --git a/unix/ztypes_netbsd_amd64.go b/unix/ztypes_netbsd_amd64.go index 9048a509..ed3f1736 100644 --- a/unix/ztypes_netbsd_amd64.go +++ b/unix/ztypes_netbsd_amd64.go @@ -453,3 +453,13 @@ type Utsname struct { Version [256]byte Machine [256]byte } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +} diff --git a/unix/ztypes_netbsd_arm.go b/unix/ztypes_netbsd_arm.go index 00525e7b..d263b614 100644 --- a/unix/ztypes_netbsd_arm.go +++ b/unix/ztypes_netbsd_arm.go @@ -451,3 +451,13 @@ type Utsname struct { Version [256]byte Machine [256]byte } + +const SizeofClockinfo = 0x14 + +type Clockinfo struct { + Hz int32 + Tick int32 + Tickadj int32 + Stathz int32 + Profhz int32 +}