Skip to content

Commit

Permalink
Merge pull request #1088 from fredwangwang/fix-slow-cpuinfo-win
Browse files Browse the repository at this point in the history
processor, v3/processor (Win): fix slow cpuinfo on multisocket config
  • Loading branch information
shirou authored Jun 19, 2021
2 parents b60e8d1 + 6a57295 commit fcbe555
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 13 additions & 3 deletions cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cpu
import (
"context"
"fmt"
"strings"
"unsafe"

"github.com/StackExchange/wmi"
Expand All @@ -18,7 +19,14 @@ var (
)

type Win32_Processor struct {
LoadPercentage *uint16
Win32_ProcessorWithoutLoadPct
LoadPercentage *uint16
}

// LoadPercentage takes a linearly more time as the number of sockets increases.
// For vSphere by default corespersocket = 1, meaning for a 40 vCPU VM Get Processor Info
// could take more than half a minute.
type Win32_ProcessorWithoutLoadPct struct {
Family uint16
Manufacturer string
Name string
Expand Down Expand Up @@ -104,8 +112,9 @@ func Info() ([]InfoStat, error) {

func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
var ret []InfoStat
var dst []Win32_Processor
var dst []Win32_ProcessorWithoutLoadPct
q := wmi.CreateQuery(&dst, "")
q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor")
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
return ret, err
}
Expand Down Expand Up @@ -242,8 +251,9 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
}
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L499
// for the time being, try with unreliable and slow WMI call…
var dst []Win32_Processor
var dst []Win32_ProcessorWithoutLoadPct
q := wmi.CreateQuery(&dst, "")
q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor")
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
return 0, err
}
Expand Down
1 change: 0 additions & 1 deletion v3/cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
)

type win32_Processor struct {
LoadPercentage *uint16
Family uint16
Manufacturer string
Name string
Expand Down

0 comments on commit fcbe555

Please sign in to comment.