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 <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/unix/syscall_netbsd.go b/unix/syscall_netbsd.go
index 6f8ebde..639bcde 100644
--- a/unix/syscall_netbsd.go
+++ b/unix/syscall_netbsd.go
@@ -93,6 +93,23 @@
 	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 de8c079..3573a13 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 @@
 	}
 	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 1494aaf..c49621c 100644
--- a/unix/types_netbsd.go
+++ b/unix/types_netbsd.go
@@ -279,3 +279,9 @@
 // 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 4b86fb2..9e9088d 100644
--- a/unix/ztypes_netbsd_386.go
+++ b/unix/ztypes_netbsd_386.go
@@ -446,3 +446,13 @@
 	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 9048a50..ed3f173 100644
--- a/unix/ztypes_netbsd_amd64.go
+++ b/unix/ztypes_netbsd_amd64.go
@@ -453,3 +453,13 @@
 	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 00525e7..d263b61 100644
--- a/unix/ztypes_netbsd_arm.go
+++ b/unix/ztypes_netbsd_arm.go
@@ -451,3 +451,13 @@
 	Version  [256]byte
 	Machine  [256]byte
 }
+
+const SizeofClockinfo = 0x14
+
+type Clockinfo struct {
+	Hz      int32
+	Tick    int32
+	Tickadj int32
+	Stathz  int32
+	Profhz  int32
+}