Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[load][windows]: add error detail and context handling. #1380

Merged
merged 1 commit into from Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions load/load_windows.go
Expand Up @@ -26,7 +26,7 @@ var (
// TODO instead of this goroutine, we can register a Win32 counter just as psutil does
// see https://psutil.readthedocs.io/en/latest/#psutil.getloadavg
// code https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/arch/windows/wmi.c
func loadAvgGoroutine() {
func loadAvgGoroutine(ctx context.Context) {
var (
samplingFrequency time.Duration = 5 * time.Second
loadAvgFactor1M float64 = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds())
Expand All @@ -37,20 +37,30 @@ func loadAvgGoroutine() {

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")
log.Printf("unexpected processor queue length counter error, %v\n", err)
return
}

tick := time.NewTicker(samplingFrequency).C
for {

f := func() {
currentLoad, err = counter.GetValue()
loadAvgMutex.Lock()
loadErr = err
loadAvgMutex.Lock()
loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M)
loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M)
loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M)
loadAvgMutex.Unlock()
<-tick
}

f() // run first time
shirou marked this conversation as resolved.
Show resolved Hide resolved
for {
select {
case <-ctx.Done():
return
case <-tick:
f()
}
}
}

Expand All @@ -61,7 +71,7 @@ func Avg() (*AvgStat, error) {

func AvgWithContext(ctx context.Context) (*AvgStat, error) {
loadAvgGoroutineOnce.Do(func() {
go loadAvgGoroutine()
go loadAvgGoroutine(ctx)
})
loadAvgMutex.RLock()
defer loadAvgMutex.RUnlock()
Expand Down