From e4093980e83e70784fc393a93d5620e2654097f0 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 1 Apr 2019 09:06:21 +0200 Subject: [PATCH] unix: add SysctlClockinfo on darwin Add type Clockinfo and the SysctlClockinfo function to query clock rate information from the kernel. Change-Id: Ic29edcacee3b3f6125bb218b82c2a2496c39213a Reviewed-on: https://go-review.googlesource.com/c/sys/+/170297 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- unix/syscall_darwin.go | 17 +++++++++++++++++ unix/syscall_darwin_test.go | 9 +++++++++ unix/types_darwin.go | 6 ++++++ unix/zsyscall_darwin_amd64.1_11.go | 10 ++++++++++ unix/ztypes_darwin_386.go | 10 ++++++++++ unix/ztypes_darwin_amd64.go | 10 ++++++++++ unix/ztypes_darwin_arm.go | 10 ++++++++++ unix/ztypes_darwin_arm64.go | 10 ++++++++++ 8 files changed, 82 insertions(+) diff --git a/unix/syscall_darwin.go b/unix/syscall_darwin.go index a2e36888..21200918 100644 --- a/unix/syscall_darwin.go +++ b/unix/syscall_darwin.go @@ -144,6 +144,23 @@ func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) ( //sys getattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error) +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 +} + //sysnb pipe() (r int, w int, err error) func Pipe(p []int) (err error) { diff --git a/unix/syscall_darwin_test.go b/unix/syscall_darwin_test.go index 7faa295f..a8a5d4e1 100644 --- a/unix/syscall_darwin_test.go +++ b/unix/syscall_darwin_test.go @@ -61,3 +61,12 @@ func TestUtimesNanoAt(t *testing.T) { t.Errorf("UtimesNanoAt: wrong mtime: got %v, expected %v", st.Mtimespec, expected) } } + +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_darwin.go b/unix/types_darwin.go index 9fd2aaa6..155c2e69 100644 --- a/unix/types_darwin.go +++ b/unix/types_darwin.go @@ -275,3 +275,9 @@ const ( // uname type Utsname C.struct_utsname + +// Clockinfo + +const SizeofClockinfo = C.sizeof_struct_clockinfo + +type Clockinfo C.struct_clockinfo diff --git a/unix/zsyscall_darwin_amd64.1_11.go b/unix/zsyscall_darwin_amd64.1_11.go index 2581e896..8c88d583 100644 --- a/unix/zsyscall_darwin_amd64.1_11.go +++ b/unix/zsyscall_darwin_amd64.1_11.go @@ -683,6 +683,16 @@ func Chroot(path string) (err error) { // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT +func ClockGettime(clockid int32, time *Timespec) (err error) { + _, _, e1 := Syscall(SYS_CLOCK_GETTIME, uintptr(clockid), uintptr(unsafe.Pointer(time)), 0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + func Close(fd int) (err error) { _, _, e1 := Syscall(SYS_CLOSE, uintptr(fd), 0, 0) if e1 != 0 { diff --git a/unix/ztypes_darwin_386.go b/unix/ztypes_darwin_386.go index 2aeb52a8..cefe2f8e 100644 --- a/unix/ztypes_darwin_386.go +++ b/unix/ztypes_darwin_386.go @@ -487,3 +487,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_darwin_amd64.go b/unix/ztypes_darwin_amd64.go index 0d0d9f2c..c6bb883c 100644 --- a/unix/ztypes_darwin_amd64.go +++ b/unix/ztypes_darwin_amd64.go @@ -497,3 +497,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_darwin_arm.go b/unix/ztypes_darwin_arm.go index 04e344b7..94c23bc2 100644 --- a/unix/ztypes_darwin_arm.go +++ b/unix/ztypes_darwin_arm.go @@ -488,3 +488,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_darwin_arm64.go b/unix/ztypes_darwin_arm64.go index 9fec185c..c82a930c 100644 --- a/unix/ztypes_darwin_arm64.go +++ b/unix/ztypes_darwin_arm64.go @@ -497,3 +497,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 +}