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
|
|
|
|
2014-11-27 09:18:15 +08:00
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2020-11-02 21:51:13 +08:00
|
|
|
"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
|
|
|
|
2020-11-02 21:51:13 +08:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-11-27 09:18:15 +08:00
|
|
|
)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-11-02 21:51:13 +08:00
|
|
|
counter, err := common.ProcessorQueueLengthCounter()
|
|
|
|
loadErr = err
|
|
|
|
if err != nil || counter == nil {
|
2020-10-22 22:13:43 +08:00
|
|
|
loadAvgMutex.Unlock()
|
2020-11-02 21:51:13 +08:00
|
|
|
log.Println("unexpected processor queue length counter error, please file an issue on github")
|
2020-10-22 22:13:43 +08:00
|
|
|
return
|
|
|
|
}
|
2020-11-02 21:51:13 +08:00
|
|
|
|
2020-11-09 15:00:22 +08:00
|
|
|
tick := time.NewTicker(samplingFrequency).C
|
2020-10-22 22:13:43 +08:00
|
|
|
for {
|
2020-11-02 21:51:13 +08:00
|
|
|
currentLoad, loadErr = counter.GetValue()
|
|
|
|
if loadErr != nil {
|
|
|
|
goto SKIP
|
2020-10-22 22:13:43 +08:00
|
|
|
}
|
2020-11-02 21:51:13 +08:00
|
|
|
// comment following block if you want load to be 0 as long as process queue is zero
|
|
|
|
{
|
|
|
|
if currentLoad == 0.0 {
|
|
|
|
percent, err := cpu.Percent(0, false)
|
|
|
|
if err == nil {
|
|
|
|
currentLoad = percent[0] / 100
|
|
|
|
// load averages are also given some amount of the currentLoad
|
|
|
|
// maybe they shouldnt?
|
|
|
|
if loadAvg1M == 0 {
|
|
|
|
loadAvg1M = currentLoad
|
|
|
|
}
|
|
|
|
if loadAvg5M == 0 {
|
|
|
|
loadAvg5M = currentLoad / 2
|
|
|
|
}
|
|
|
|
if loadAvg15M == 0 {
|
|
|
|
loadAvg15M = currentLoad / 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 22:13:43 +08:00
|
|
|
}
|
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-11-02 21:51:13 +08:00
|
|
|
|
|
|
|
SKIP:
|
2020-10-22 22:13:43 +08:00
|
|
|
loadAvgMutex.Unlock()
|
2020-11-09 15:00:22 +08:00
|
|
|
<-tick
|
2020-11-02 21:51:13 +08:00
|
|
|
loadAvgMutex.Lock()
|
2020-10-22 22:13:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Avg() (*AvgStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return AvgWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
2020-11-02 21:51:13 +08:00
|
|
|
loadAvgGoroutineOnce.Do(func() {
|
|
|
|
loadAvgMutex.Lock()
|
|
|
|
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
|
|
|
}
|
2016-02-20 21:52:16 +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) {
|
2016-02-20 21:52:16 +08:00
|
|
|
ret := MiscStat{}
|
|
|
|
|
2016-04-01 20:34:39 +08:00
|
|
|
return &ret, common.ErrNotImplementedError
|
2016-02-20 21:52:16 +08:00
|
|
|
}
|