浏览代码

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>
Tobias Klauser 6 年之前
父节点
当前提交
6c81ef8f67

+ 17 - 0
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 {

+ 9 - 0
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 {

+ 6 - 0
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

+ 10 - 0
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
+}

+ 10 - 0
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
+}

+ 10 - 0
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
+}