Skip to content

Commit

Permalink
Merge pull request #878 from fancybits/android-loadavg
Browse files Browse the repository at this point in the history
[load][linux] implement Avg() on top of sysinfo syscall when /proc/loadavg is not readable
  • Loading branch information
Lomanic authored May 21, 2020
2 parents c89193f + f42052b commit 42aec72
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions load/load_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"strconv"
"strings"
"syscall"

"github.com/shirou/gopsutil/internal/common"
)
Expand All @@ -16,6 +17,29 @@ func Avg() (*AvgStat, error) {
}

func AvgWithContext(ctx context.Context) (*AvgStat, error) {
stat, err := fileAvgWithContext(ctx)
if err != nil {
stat, err = sysinfoAvgWithContext(ctx)
}
return stat, err
}

func sysinfoAvgWithContext(ctx context.Context) (*AvgStat, error) {
var info syscall.Sysinfo_t
err := syscall.Sysinfo(&info)
if err != nil {
return nil, err
}

const si_load_shift = 16
return &AvgStat{
Load1: float64(info.Loads[0]) / float64(1<<si_load_shift),
Load5: float64(info.Loads[1]) / float64(1<<si_load_shift),
Load15: float64(info.Loads[2]) / float64(1<<si_load_shift),
}, nil
}

func fileAvgWithContext(ctx context.Context) (*AvgStat, error) {
values, err := readLoadAvgFromFile()
if err != nil {
return nil, err
Expand Down

0 comments on commit 42aec72

Please sign in to comment.