mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-03-05 21:00:12 -08:00
collector/cpu_netbsd: fix 32-bit host support and plug memory leak (#3083)
Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
parent
f252c4616a
commit
cab75a8011
|
@ -32,6 +32,15 @@ import (
|
||||||
"howett.net/plist"
|
"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 {
|
type clockinfo struct {
|
||||||
hz int32 // clock frequency
|
hz int32 // clock frequency
|
||||||
tick int32 // micro-seconds per hz tick
|
tick int32 // micro-seconds per hz tick
|
||||||
|
@ -50,7 +59,7 @@ type cputime struct {
|
||||||
|
|
||||||
type plistref struct {
|
type plistref struct {
|
||||||
pref_plist unsafe.Pointer
|
pref_plist unsafe.Pointer
|
||||||
pref_len uint64
|
pref_len uint
|
||||||
}
|
}
|
||||||
|
|
||||||
type sysmonValues struct {
|
type sysmonValues struct {
|
||||||
|
@ -64,25 +73,19 @@ type sysmonProperty []sysmonValues
|
||||||
|
|
||||||
type sysmonProperties map[string]sysmonProperty
|
type sysmonProperties map[string]sysmonProperty
|
||||||
|
|
||||||
func readBytes(ptr unsafe.Pointer, length uint64) []byte {
|
func _IOC(inout uint, group byte, num uint, len uintptr) uint {
|
||||||
buf := make([]byte, length-1)
|
return ((inout) | ((uint(len) & _IOCPARM_MASK) << _IOCPARM_SHIFT) | (uint(group) << _IOCGROUP_SHIFT) | (num))
|
||||||
var i uint64
|
|
||||||
for ; i < length-1; i++ {
|
|
||||||
buf[i] = *(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(i)))
|
|
||||||
}
|
|
||||||
return buf
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
_, _, errno := unix.Syscall(
|
||||||
unix.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
uintptr(fd),
|
uintptr(fd),
|
||||||
// Some magicks derived from sys/ioccom.h.
|
uintptr(_IOWR(typ, nr, size)),
|
||||||
uintptr((0x40000000|0x80000000)|
|
|
||||||
((int64(size)&(1<<13-1))<<16)|
|
|
||||||
(int64(typ)<<8)|
|
|
||||||
nr,
|
|
||||||
),
|
|
||||||
uintptr(retptr),
|
uintptr(retptr),
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
|
@ -92,7 +95,7 @@ func ioctl(fd int, nr int64, typ byte, size uintptr, retptr unsafe.Pointer) erro
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSysmonProperties() (sysmonProperties, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -103,8 +106,8 @@ func readSysmonProperties() (sysmonProperties, error) {
|
||||||
if err = ioctl(fd, 0, 'E', unsafe.Sizeof(retptr), unsafe.Pointer(&retptr)); err != nil {
|
if err = ioctl(fd, 0, 'E', unsafe.Sizeof(retptr), unsafe.Pointer(&retptr)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer unix.Syscall(unix.SYS_MUNMAP, uintptr(retptr.pref_plist), uintptr(retptr.pref_len), uintptr(0))
|
||||||
bytes := readBytes(retptr.pref_plist, retptr.pref_len)
|
bytes := unsafe.Slice((*byte)(unsafe.Pointer(retptr.pref_plist)), retptr.pref_len-1)
|
||||||
|
|
||||||
var props sysmonProperties
|
var props sysmonProperties
|
||||||
if _, err = plist.Unmarshal(bytes, &props); err != nil {
|
if _, err = plist.Unmarshal(bytes, &props); err != nil {
|
||||||
|
@ -179,7 +182,7 @@ func getCPUTimes() ([]cputime, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ncpus := *(*int)(unsafe.Pointer(&ncpusb[0]))
|
ncpus := int(*(*uint32)(unsafe.Pointer(&ncpusb[0])))
|
||||||
|
|
||||||
if ncpus < 1 {
|
if ncpus < 1 {
|
||||||
return nil, errors.New("Invalid cpu number")
|
return nil, errors.New("Invalid cpu number")
|
||||||
|
@ -191,10 +194,10 @@ func getCPUTimes() ([]cputime, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for len(cpb) >= int(unsafe.Sizeof(int(0))) {
|
for len(cpb) >= int(unsafe.Sizeof(uint64(0))) {
|
||||||
t := *(*int)(unsafe.Pointer(&cpb[0]))
|
t := *(*uint64)(unsafe.Pointer(&cpb[0]))
|
||||||
times = append(times, float64(t)/cpufreq)
|
times = append(times, float64(t)/cpufreq)
|
||||||
cpb = cpb[unsafe.Sizeof(int(0)):]
|
cpb = cpb[unsafe.Sizeof(uint64(0)):]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue