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 <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
diff --git a/unix/syscall_openbsd.go b/unix/syscall_openbsd.go
index 6879995..c8648ec 100644
--- a/unix/syscall_openbsd.go
+++ b/unix/syscall_openbsd.go
@@ -43,6 +43,23 @@
 	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 b95f334..7bf75ee 100644
--- a/unix/syscall_openbsd_test.go
+++ b/unix/syscall_openbsd_test.go
@@ -40,6 +40,15 @@
 	}
 }
 
+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 4e5e57f..8aafbe4 100644
--- a/unix/types_openbsd.go
+++ b/unix/types_openbsd.go
@@ -274,3 +274,9 @@
 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 8b37d83..900fb44 100644
--- a/unix/ztypes_openbsd_386.go
+++ b/unix/ztypes_openbsd_386.go
@@ -558,3 +558,13 @@
 	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 6efea46..028fa78 100644
--- a/unix/ztypes_openbsd_amd64.go
+++ b/unix/ztypes_openbsd_amd64.go
@@ -558,3 +558,13 @@
 	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 510efc3..b45d5ee 100644
--- a/unix/ztypes_openbsd_arm.go
+++ b/unix/ztypes_openbsd_arm.go
@@ -559,3 +559,13 @@
 	Fpswtch            int32
 	Kmapent            int32
 }
+
+const SizeofClockinfo = 0x14
+
+type Clockinfo struct {
+	Hz      int32
+	Tick    int32
+	Tickadj int32
+	Stathz  int32
+	Profhz  int32
+}