Skip to content

Commit fcbe555

Browse files
authored
Merge pull request #1088 from fredwangwang/fix-slow-cpuinfo-win
processor, v3/processor (Win): fix slow cpuinfo on multisocket config
2 parents b60e8d1 + 6a57295 commit fcbe555

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

cpu/cpu_windows.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cpu
55
import (
66
"context"
77
"fmt"
8+
"strings"
89
"unsafe"
910

1011
"github.com/StackExchange/wmi"
@@ -18,7 +19,14 @@ var (
1819
)
1920

2021
type Win32_Processor struct {
21-
LoadPercentage *uint16
22+
Win32_ProcessorWithoutLoadPct
23+
LoadPercentage *uint16
24+
}
25+
26+
// LoadPercentage takes a linearly more time as the number of sockets increases.
27+
// For vSphere by default corespersocket = 1, meaning for a 40 vCPU VM Get Processor Info
28+
// could take more than half a minute.
29+
type Win32_ProcessorWithoutLoadPct struct {
2230
Family uint16
2331
Manufacturer string
2432
Name string
@@ -104,8 +112,9 @@ func Info() ([]InfoStat, error) {
104112

105113
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
106114
var ret []InfoStat
107-
var dst []Win32_Processor
115+
var dst []Win32_ProcessorWithoutLoadPct
108116
q := wmi.CreateQuery(&dst, "")
117+
q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor")
109118
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
110119
return ret, err
111120
}
@@ -242,8 +251,9 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
242251
}
243252
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L499
244253
// for the time being, try with unreliable and slow WMI call…
245-
var dst []Win32_Processor
254+
var dst []Win32_ProcessorWithoutLoadPct
246255
q := wmi.CreateQuery(&dst, "")
256+
q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor")
247257
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
248258
return 0, err
249259
}

v3/cpu/cpu_windows.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var (
1818
)
1919

2020
type win32_Processor struct {
21-
LoadPercentage *uint16
2221
Family uint16
2322
Manufacturer string
2423
Name string

0 commit comments

Comments
 (0)