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

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

Loading…
Cancel
Save