[load][android] implement on top of sysinfo syscall

This commit is contained in:
Aman Gupta 2020-05-19 13:53:39 -07:00
parent c89193f22d
commit f42052bee7
1 changed files with 24 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
@ -16,6 +17,29 @@ func Avg() (*AvgStat, error) {
} }
func AvgWithContext(ctx context.Context) (*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() values, err := readLoadAvgFromFile()
if err != nil { if err != nil {
return nil, err return nil, err