瀏覽代碼

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>
Tobias Klauser 7 年之前
父節點
當前提交
8ccfc68037
共有 6 個文件被更改,包括 68 次插入0 次删除
  1. 17 0
      unix/syscall_netbsd.go
  2. 15 0
      unix/syscall_netbsd_test.go
  3. 6 0
      unix/types_netbsd.go
  4. 10 0
      unix/ztypes_netbsd_386.go
  5. 10 0
      unix/ztypes_netbsd_amd64.go
  6. 10 0
      unix/ztypes_netbsd_arm.go

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

+ 15 - 0
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)
+}

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

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

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

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