Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
collector/cpu_netbsd: fix 32-bit host support and plug memory leak
Signed-off-by: Tobias Nygren <[email protected]>
  • Loading branch information
tnn2 committed Aug 4, 2024
commit 3eff1036595348cfce31e0a75343e053481a2ed0
39 changes: 21 additions & 18 deletions collector/cpu_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ import (
"howett.net/plist"
)

const (
_IOC_OUT = uint(0x40000000)
_IOC_IN = uint(0x80000000)
_IOC_INOUT = (_IOC_IN | _IOC_OUT)
_IOCPARM_MASK = uint(0x1fff)
_IOCPARM_SHIFT = uint(16)
_IOCGROUP_SHIFT = uint(8)
)

type clockinfo struct {
hz int32 // clock frequency
tick int32 // micro-seconds per hz tick
Expand All @@ -51,7 +60,7 @@ type cputime struct {

type plistref struct {
pref_plist unsafe.Pointer
pref_len uint64
pref_len uint
}

type sysmonValues struct {
Expand All @@ -65,25 +74,19 @@ type sysmonProperty []sysmonValues

type sysmonProperties map[string]sysmonProperty

func readBytes(ptr unsafe.Pointer, length uint64) []byte {
buf := make([]byte, length-1)
var i uint64
for ; i < length-1; i++ {
buf[i] = *(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(i)))
}
return buf
func _IOC(inout uint, group byte, num uint, len uintptr) uint {
return ((inout) | ((uint(len) & _IOCPARM_MASK) << _IOCPARM_SHIFT) | (uint(group) << _IOCGROUP_SHIFT) | (num))
}

func ioctl(fd int, nr int64, typ byte, size uintptr, retptr unsafe.Pointer) error {
func _IOWR(group byte, num uint, len uintptr) uint {
return _IOC(_IOC_INOUT, group, num, len)
}

func ioctl(fd int, nr uint, typ byte, size uintptr, retptr unsafe.Pointer) error {
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
// Some magicks derived from sys/ioccom.h.
uintptr((0x40000000|0x80000000)|
((int64(size)&(1<<13-1))<<16)|
(int64(typ)<<8)|
nr,
),
uintptr(_IOWR(typ, nr, size)),
uintptr(retptr),
)
if errno != 0 {
Expand All @@ -93,7 +96,7 @@ func ioctl(fd int, nr int64, typ byte, size uintptr, retptr unsafe.Pointer) erro
}

func readSysmonProperties() (sysmonProperties, error) {
fd, err := unix.Open(rootfsFilePath("/dev/sysmon"), unix.O_RDONLY, 0777)
fd, err := unix.Open(rootfsFilePath("/dev/sysmon"), unix.O_RDONLY, 0)
if err != nil {
return nil, err
}
Expand All @@ -104,8 +107,8 @@ func readSysmonProperties() (sysmonProperties, error) {
if err = ioctl(fd, 0, 'E', unsafe.Sizeof(retptr), unsafe.Pointer(&retptr)); err != nil {
return nil, err
}

bytes := readBytes(retptr.pref_plist, retptr.pref_len)
defer unix.Syscall(unix.SYS_MUNMAP, uintptr(retptr.pref_plist), uintptr(retptr.pref_len), uintptr(0))
bytes := unsafe.Slice((*byte)(unsafe.Pointer(retptr.pref_plist)), retptr.pref_len-1)

var props sysmonProperties
if _, err = plist.Unmarshal(bytes, &props); err != nil {
Expand Down