kubelet: fix the bug that the number of cpu cannot be obtained normally under Windows
This commit is contained in:
		@@ -26,6 +26,7 @@ import (
 | 
				
			|||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
						"syscall"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
	"unsafe"
 | 
						"unsafe"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -52,8 +53,11 @@ type MemoryStatusEx struct {
 | 
				
			|||||||
var (
 | 
					var (
 | 
				
			||||||
	modkernel32                 = windows.NewLazySystemDLL("kernel32.dll")
 | 
						modkernel32                 = windows.NewLazySystemDLL("kernel32.dll")
 | 
				
			||||||
	procGlobalMemoryStatusEx    = modkernel32.NewProc("GlobalMemoryStatusEx")
 | 
						procGlobalMemoryStatusEx    = modkernel32.NewProc("GlobalMemoryStatusEx")
 | 
				
			||||||
 | 
						procGetActiveProcessorCount = modkernel32.NewProc("GetActiveProcessorCount")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const allProcessorGroups = 0xFFFF
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewPerfCounterClient creates a client using perf counters
 | 
					// NewPerfCounterClient creates a client using perf counters
 | 
				
			||||||
func NewPerfCounterClient() (Client, error) {
 | 
					func NewPerfCounterClient() (Client, error) {
 | 
				
			||||||
	// Initialize the cache
 | 
						// Initialize the cache
 | 
				
			||||||
@@ -140,13 +144,33 @@ func (p *perfCounterNodeStatsClient) getMachineInfo() (*cadvisorapi.MachineInfo,
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &cadvisorapi.MachineInfo{
 | 
						return &cadvisorapi.MachineInfo{
 | 
				
			||||||
		NumCores:       runtime.NumCPU(),
 | 
							NumCores:       processorCount(),
 | 
				
			||||||
		MemoryCapacity: p.nodeInfo.memoryPhysicalCapacityBytes,
 | 
							MemoryCapacity: p.nodeInfo.memoryPhysicalCapacityBytes,
 | 
				
			||||||
		MachineID:      hostname,
 | 
							MachineID:      hostname,
 | 
				
			||||||
		SystemUUID:     systemUUID,
 | 
							SystemUUID:     systemUUID,
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// runtime.NumCPU() will only return the information for a single Processor Group.
 | 
				
			||||||
 | 
					// Since a single group can only hold 64 logical processors, this
 | 
				
			||||||
 | 
					// means when there are more they will be divided into multiple groups.
 | 
				
			||||||
 | 
					// For the above reason, procGetActiveProcessorCount is used to get the
 | 
				
			||||||
 | 
					// cpu count for all processor groups of the windows node.
 | 
				
			||||||
 | 
					// more notes for this issue:
 | 
				
			||||||
 | 
					// same issue in moby: https://github.com/moby/moby/issues/38935#issuecomment-744638345
 | 
				
			||||||
 | 
					// solution in hcsshim: https://github.com/microsoft/hcsshim/blob/master/internal/processorinfo/processor_count.go
 | 
				
			||||||
 | 
					func processorCount() int {
 | 
				
			||||||
 | 
						if amount := getActiveProcessorCount(allProcessorGroups); amount != 0 {
 | 
				
			||||||
 | 
							return int(amount)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return runtime.NumCPU()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getActiveProcessorCount(groupNumber uint16) int {
 | 
				
			||||||
 | 
						r0, _, _ := syscall.Syscall(procGetActiveProcessorCount.Addr(), 1, uintptr(groupNumber), 0, 0)
 | 
				
			||||||
 | 
						return int(r0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *perfCounterNodeStatsClient) getVersionInfo() (*cadvisorapi.VersionInfo, error) {
 | 
					func (p *perfCounterNodeStatsClient) getVersionInfo() (*cadvisorapi.VersionInfo, error) {
 | 
				
			||||||
	return &cadvisorapi.VersionInfo{
 | 
						return &cadvisorapi.VersionInfo{
 | 
				
			||||||
		KernelVersion:      p.nodeInfo.kernelVersion,
 | 
							KernelVersion:      p.nodeInfo.kernelVersion,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user