shirou_gopsutil/load/load_windows.go

86 lines
2.3 KiB
Go
Raw Normal View History

2021-12-23 05:54:41 +08:00
//go:build windows
2014-04-18 20:28:00 +08:00
// +build windows
2014-12-30 21:09:05 +08:00
package load
2014-04-18 20:28:00 +08:00
import (
2017-12-31 14:25:49 +08:00
"context"
"log"
2020-11-09 15:00:22 +08:00
"math"
2020-10-22 22:13:43 +08:00
"sync"
"time"
2017-12-31 14:25:49 +08:00
"github.com/shirou/gopsutil/v3/internal/common"
)
2020-10-22 22:13:43 +08:00
var (
loadErr error
loadAvg1M float64 = 0.0
loadAvg5M float64 = 0.0
loadAvg15M float64 = 0.0
loadAvgMutex sync.RWMutex
loadAvgGoroutineOnce sync.Once
)
// loadAvgGoroutine updates avg data by fetching current load by interval
2020-11-06 16:07:02 +08:00
// TODO instead of this goroutine, we can register a Win32 counter just as psutil does
2020-10-22 22:13:43 +08:00
// see https://psutil.readthedocs.io/en/latest/#psutil.getloadavg
2020-11-06 16:07:02 +08:00
// code https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/arch/windows/wmi.c
2020-10-22 22:13:43 +08:00
func loadAvgGoroutine() {
var (
2020-11-09 15:00:22 +08:00
samplingFrequency time.Duration = 5 * time.Second
loadAvgFactor1M float64 = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds())
loadAvgFactor5M float64 = 1 / math.Exp(samplingFrequency.Seconds()/(5*time.Minute).Seconds())
loadAvgFactor15M float64 = 1 / math.Exp(samplingFrequency.Seconds()/(15*time.Minute).Seconds())
currentLoad float64
2020-10-22 22:13:43 +08:00
)
counter, err := common.ProcessorQueueLengthCounter()
if err != nil || counter == nil {
log.Println("gopsutil: unexpected processor queue length counter error, please file an issue on github: err")
2020-10-22 22:13:43 +08:00
return
}
2020-11-09 15:00:22 +08:00
tick := time.NewTicker(samplingFrequency).C
2020-10-22 22:13:43 +08:00
for {
2020-11-09 15:50:09 +08:00
currentLoad, err = counter.GetValue()
loadAvgMutex.Lock()
loadErr = err
2020-11-09 15:00:22 +08:00
loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M)
loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M)
loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M)
2020-10-22 22:13:43 +08:00
loadAvgMutex.Unlock()
2020-11-09 15:00:22 +08:00
<-tick
2020-10-22 22:13:43 +08:00
}
}
2020-11-09 15:37:31 +08:00
// Avg for Windows may return 0 values for the first few 5 second intervals
func Avg() (*AvgStat, error) {
2017-12-31 14:25:49 +08:00
return AvgWithContext(context.Background())
}
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
loadAvgGoroutineOnce.Do(func() {
go loadAvgGoroutine()
})
2020-10-22 22:13:43 +08:00
loadAvgMutex.RLock()
defer loadAvgMutex.RUnlock()
ret := AvgStat{
Load1: loadAvg1M,
Load5: loadAvg5M,
Load15: loadAvg15M,
}
2014-04-18 20:28:00 +08:00
2020-10-22 22:13:43 +08:00
return &ret, loadErr
2014-04-18 20:28:00 +08:00
}
func Misc() (*MiscStat, error) {
2017-12-31 14:25:49 +08:00
return MiscWithContext(context.Background())
}
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
ret := MiscStat{}
2016-04-01 20:34:39 +08:00
return &ret, common.ErrNotImplementedError
}