Merge pull request #1380 from shirou/feature/readable_error_on_load_windows

[load][windows]: add error detail and context handling.
feature/fix_type_alias_warning
shirou 2 years ago committed by GitHub
commit 30aa851d39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,7 +26,7 @@ var (
// TODO instead of this goroutine, we can register a Win32 counter just as psutil does // TODO instead of this goroutine, we can register a Win32 counter just as psutil does
// see https://psutil.readthedocs.io/en/latest/#psutil.getloadavg // see https://psutil.readthedocs.io/en/latest/#psutil.getloadavg
// code https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/arch/windows/wmi.c // code https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/arch/windows/wmi.c
func loadAvgGoroutine() { func loadAvgGoroutine(ctx context.Context) {
var ( var (
samplingFrequency time.Duration = 5 * time.Second samplingFrequency time.Duration = 5 * time.Second
loadAvgFactor1M float64 = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds()) loadAvgFactor1M float64 = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds())
@ -37,20 +37,30 @@ func loadAvgGoroutine() {
counter, err := common.ProcessorQueueLengthCounter() counter, err := common.ProcessorQueueLengthCounter()
if err != nil || counter == nil { if err != nil || counter == nil {
log.Println("gopsutil: unexpected processor queue length counter error, please file an issue on github: err") log.Printf("unexpected processor queue length counter error, %v\n", err)
return return
} }
tick := time.NewTicker(samplingFrequency).C tick := time.NewTicker(samplingFrequency).C
for {
f := func() {
currentLoad, err = counter.GetValue() currentLoad, err = counter.GetValue()
loadAvgMutex.Lock()
loadErr = err loadErr = err
loadAvgMutex.Lock()
loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M) loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M)
loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M) loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M)
loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M) loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M)
loadAvgMutex.Unlock() loadAvgMutex.Unlock()
<-tick }
f() // run first time
for {
select {
case <-ctx.Done():
return
case <-tick:
f()
}
} }
} }
@ -61,7 +71,7 @@ func Avg() (*AvgStat, error) {
func AvgWithContext(ctx context.Context) (*AvgStat, error) { func AvgWithContext(ctx context.Context) (*AvgStat, error) {
loadAvgGoroutineOnce.Do(func() { loadAvgGoroutineOnce.Do(func() {
go loadAvgGoroutine() go loadAvgGoroutine(ctx)
}) })
loadAvgMutex.RLock() loadAvgMutex.RLock()
defer loadAvgMutex.RUnlock() defer loadAvgMutex.RUnlock()

Loading…
Cancel
Save