[load] freebsd: change to use x/sys/unix.SysctlRaw.

This implementation is borrowed from NodeExporter.
https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
pull/431/head
WAKAYAMA shirou 8 years ago
parent fcba942e03
commit 1ba77cdb3d

@ -4,35 +4,29 @@ package load
import (
"os/exec"
"strconv"
"strings"
"unsafe"
"github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
)
func Avg() (*AvgStat, error) {
values, err := common.DoSysctrl("vm.loadavg")
if err != nil {
return nil, err
}
load1, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return nil, err
// This SysctlRaw method borrowed from
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
type loadavg struct {
load [3]uint32
scale int
}
load5, err := strconv.ParseFloat(values[1], 64)
b, err := unix.SysctlRaw("vm.loadavg")
if err != nil {
return nil, err
}
load15, err := strconv.ParseFloat(values[2], 64)
if err != nil {
return nil, err
}
load := *(*loadavg)(unsafe.Pointer((&b[0])))
scale := float64(load.scale)
ret := &AvgStat{
Load1: float64(load1),
Load5: float64(load5),
Load15: float64(load15),
Load1: float64(load.load[0]) / scale,
Load5: float64(load.load[1]) / scale,
Load15: float64(load.load[2]) / scale,
}
return ret, nil

@ -15,6 +15,7 @@ func TestLoad(t *testing.T) {
if v == empty {
t.Errorf("error load: %v", v)
}
t.Log(v)
}
func TestLoadAvgStat_String(t *testing.T) {
@ -27,6 +28,7 @@ func TestLoadAvgStat_String(t *testing.T) {
if e != fmt.Sprintf("%v", v) {
t.Errorf("LoadAvgStat string is invalid: %v", v)
}
t.Log(e)
}
func TestMisc(t *testing.T) {
@ -39,6 +41,7 @@ func TestMisc(t *testing.T) {
if v == empty {
t.Errorf("error load: %v", v)
}
t.Log(v)
}
func TestMiscStatString(t *testing.T) {
@ -51,4 +54,5 @@ func TestMiscStatString(t *testing.T) {
if e != fmt.Sprintf("%v", v) {
t.Errorf("TestMiscString string is invalid: %v", v)
}
t.Log(e)
}

Loading…
Cancel
Save