@ -5,10 +5,10 @@ package load
import (
import (
"context"
"context"
"os/exec"
"os/exec"
"strconv"
"strings"
"strings"
"unsafe"
"g ithub.com/shirou/gopsutil/internal/common "
"g olang.org/x/sys/unix "
)
)
func Avg ( ) ( * AvgStat , error ) {
func Avg ( ) ( * AvgStat , error ) {
@ -16,28 +16,23 @@ func Avg() (*AvgStat, error) {
}
}
func AvgWithContext ( ctx context . Context ) ( * AvgStat , error ) {
func AvgWithContext ( ctx context . Context ) ( * AvgStat , error ) {
values , err := common . DoSysctrlWithContext ( ctx , "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
// this implementation is common with BSDs
}
type loadavg struct {
load [ 3 ] uint32
load1 , err := strconv . ParseFloat ( values [ 0 ] , 64 )
scale int
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 ( load 1) ,
Load1 : float64 ( load . load [ 0 ] ) / scale ,
Load5 : float64 ( load 5) ,
Load5 : float64 ( load . load [ 1 ] ) / scale ,
Load15 : float64 ( load 15) ,
Load15 : float64 ( load . load [ 2 ] ) / scale ,
}
}
return ret , nil
return ret , nil