Skip to content

Commit cab75a8

Browse files
authored
collector/cpu_netbsd: fix 32-bit host support and plug memory leak (#3083)
Signed-off-by: Ben Kochie <[email protected]>
1 parent f252c46 commit cab75a8

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

collector/cpu_netbsd.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ import (
3232
"howett.net/plist"
3333
)
3434

35+
const (
36+
_IOC_OUT = uint(0x40000000)
37+
_IOC_IN = uint(0x80000000)
38+
_IOC_INOUT = (_IOC_IN | _IOC_OUT)
39+
_IOCPARM_MASK = uint(0x1fff)
40+
_IOCPARM_SHIFT = uint(16)
41+
_IOCGROUP_SHIFT = uint(8)
42+
)
43+
3544
type clockinfo struct {
3645
hz int32 // clock frequency
3746
tick int32 // micro-seconds per hz tick
@@ -50,7 +59,7 @@ type cputime struct {
5059

5160
type plistref struct {
5261
pref_plist unsafe.Pointer
53-
pref_len uint64
62+
pref_len uint
5463
}
5564

5665
type sysmonValues struct {
@@ -64,25 +73,19 @@ type sysmonProperty []sysmonValues
6473

6574
type sysmonProperties map[string]sysmonProperty
6675

67-
func readBytes(ptr unsafe.Pointer, length uint64) []byte {
68-
buf := make([]byte, length-1)
69-
var i uint64
70-
for ; i < length-1; i++ {
71-
buf[i] = *(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(i)))
72-
}
73-
return buf
76+
func _IOC(inout uint, group byte, num uint, len uintptr) uint {
77+
return ((inout) | ((uint(len) & _IOCPARM_MASK) << _IOCPARM_SHIFT) | (uint(group) << _IOCGROUP_SHIFT) | (num))
7478
}
7579

76-
func ioctl(fd int, nr int64, typ byte, size uintptr, retptr unsafe.Pointer) error {
80+
func _IOWR(group byte, num uint, len uintptr) uint {
81+
return _IOC(_IOC_INOUT, group, num, len)
82+
}
83+
84+
func ioctl(fd int, nr uint, typ byte, size uintptr, retptr unsafe.Pointer) error {
7785
_, _, errno := unix.Syscall(
7886
unix.SYS_IOCTL,
7987
uintptr(fd),
80-
// Some magicks derived from sys/ioccom.h.
81-
uintptr((0x40000000|0x80000000)|
82-
((int64(size)&(1<<13-1))<<16)|
83-
(int64(typ)<<8)|
84-
nr,
85-
),
88+
uintptr(_IOWR(typ, nr, size)),
8689
uintptr(retptr),
8790
)
8891
if errno != 0 {
@@ -92,7 +95,7 @@ func ioctl(fd int, nr int64, typ byte, size uintptr, retptr unsafe.Pointer) erro
9295
}
9396

9497
func readSysmonProperties() (sysmonProperties, error) {
95-
fd, err := unix.Open(rootfsFilePath("/dev/sysmon"), unix.O_RDONLY, 0777)
98+
fd, err := unix.Open(rootfsFilePath("/dev/sysmon"), unix.O_RDONLY, 0)
9699
if err != nil {
97100
return nil, err
98101
}
@@ -103,8 +106,8 @@ func readSysmonProperties() (sysmonProperties, error) {
103106
if err = ioctl(fd, 0, 'E', unsafe.Sizeof(retptr), unsafe.Pointer(&retptr)); err != nil {
104107
return nil, err
105108
}
106-
107-
bytes := readBytes(retptr.pref_plist, retptr.pref_len)
109+
defer unix.Syscall(unix.SYS_MUNMAP, uintptr(retptr.pref_plist), uintptr(retptr.pref_len), uintptr(0))
110+
bytes := unsafe.Slice((*byte)(unsafe.Pointer(retptr.pref_plist)), retptr.pref_len-1)
108111

109112
var props sysmonProperties
110113
if _, err = plist.Unmarshal(bytes, &props); err != nil {
@@ -179,7 +182,7 @@ func getCPUTimes() ([]cputime, error) {
179182
if err != nil {
180183
return nil, err
181184
}
182-
ncpus := *(*int)(unsafe.Pointer(&ncpusb[0]))
185+
ncpus := int(*(*uint32)(unsafe.Pointer(&ncpusb[0])))
183186

184187
if ncpus < 1 {
185188
return nil, errors.New("Invalid cpu number")
@@ -191,10 +194,10 @@ func getCPUTimes() ([]cputime, error) {
191194
if err != nil {
192195
return nil, err
193196
}
194-
for len(cpb) >= int(unsafe.Sizeof(int(0))) {
195-
t := *(*int)(unsafe.Pointer(&cpb[0]))
197+
for len(cpb) >= int(unsafe.Sizeof(uint64(0))) {
198+
t := *(*uint64)(unsafe.Pointer(&cpb[0]))
196199
times = append(times, float64(t)/cpufreq)
197-
cpb = cpb[unsafe.Sizeof(int(0)):]
200+
cpb = cpb[unsafe.Sizeof(uint64(0)):]
198201
}
199202
}
200203

0 commit comments

Comments
 (0)