Merge pull request #49241 from vvoland/runtime-numcpu

pkg/sysinfo: Deprecate NumCPU
This commit is contained in:
Sebastiaan van Stijn
2025-01-09 15:15:41 +01:00
committed by GitHub
10 changed files with 10 additions and 80 deletions

View File

@@ -494,7 +494,7 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, sysIn
// Here we don't set the lower limit and it is up to the underlying platform (e.g., Linux) to return an error.
// The error message is 0.01 so that this is consistent with Windows
if resources.NanoCPUs != 0 {
nc := sysinfo.NumCPU()
nc := runtime.NumCPU()
if resources.NanoCPUs < 0 || resources.NanoCPUs > int64(nc)*1e9 {
return warnings, fmt.Errorf("range of CPUs is from 0.01 to %[1]d.00, as there are only %[1]d CPUs available", nc)
}

View File

@@ -109,7 +109,7 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, isHyp
// The precision we could get is 0.01, because on Windows we have to convert to CPUPercent.
// We don't set the lower limit here and it is up to the underlying platform (e.g., Windows) to return an error.
if resources.NanoCPUs != 0 {
nc := sysinfo.NumCPU()
nc := runtime.NumCPU()
if resources.NanoCPUs < 0 || resources.NanoCPUs > int64(nc)*1e9 {
return warnings, fmt.Errorf("range of CPUs is from 0.01 to %[1]d.00, as there are only %[1]d CPUs available", nc)
}

View File

@@ -64,7 +64,7 @@ func (daemon *Daemon) SystemInfo(ctx context.Context) (*system.Info, error) {
OSType: runtime.GOOS,
Architecture: platform.Architecture(),
RegistryConfig: doWithTrace(ctx, "registry.ServiceConfig", daemon.registryService.ServiceConfig),
NCPU: doWithTrace(ctx, "sysinfo.NumCPU", sysinfo.NumCPU),
NCPU: doWithTrace(ctx, "runtime.NumCPU", runtime.NumCPU),
MemTotal: memInfo(ctx).MemTotal,
GenericResources: daemon.genericResources,
DockerRootDir: cfg.Root,

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/Microsoft/hcsshim"
@@ -18,7 +19,6 @@ import (
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
"github.com/docker/docker/oci"
"github.com/docker/docker/pkg/sysinfo"
"github.com/docker/docker/pkg/system"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
@@ -428,7 +428,7 @@ func setResourcesInSpec(c *container.Container, s *specs.Spec, isHyperV bool) {
}
}
} else {
cpuMaximum = uint16(c.HostConfig.NanoCPUs / int64(sysinfo.NumCPU()) / (1e9 / 10000))
cpuMaximum = uint16(c.HostConfig.NanoCPUs / int64(runtime.NumCPU()) / (1e9 / 10000))
if cpuMaximum < 1 {
// The requested NanoCPUs is so small that we rounded to 0, use 1 instead
cpuMaximum = 1

View File

@@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"syscall"
@@ -23,7 +24,6 @@ import (
"github.com/docker/docker/errdefs"
"github.com/docker/docker/libcontainerd/queue"
libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
"github.com/docker/docker/pkg/sysinfo"
"github.com/docker/docker/pkg/system"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
@@ -366,7 +366,7 @@ func (c *client) extractResourcesFromSpec(spec *specs.Spec, configuration *hcssh
// because we don't want to update the HostConfig in case this container
// is moved to a host with more CPUs than this one.
cpuCount := *spec.Windows.Resources.CPU.Count
hostCPUCount := uint64(sysinfo.NumCPU())
hostCPUCount := uint64(runtime.NumCPU())
if cpuCount > hostCPUCount {
c.logger.Warnf("Changing requested CPUCount of %d to current number of processors, %d", cpuCount, hostCPUCount)
cpuCount = hostCPUCount

View File

@@ -4,12 +4,9 @@ import (
"runtime"
)
// NumCPU returns the number of CPUs. On Linux and Windows, it returns
// the number of CPUs which are currently online. On other platforms,
// it's the equivalent of [runtime.NumCPU].
// NumCPU returns the number of CPUs. It's the equivalent of [runtime.NumCPU].
//
// Deprecated: Use [runtime.NumCPU] instead. It will be removed in the next release.
func NumCPU() int {
if ncpu := numCPU(); ncpu > 0 {
return ncpu
}
return runtime.NumCPU()
}

View File

@@ -1,17 +0,0 @@
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
import "golang.org/x/sys/unix"
// numCPU queries the system for the count of threads available
// for use to this process.
//
// Returns 0 on errors. Use |runtime.NumCPU| in that case.
func numCPU() int {
// Gets the affinity mask for a process: The very one invoking this function.
var mask unix.CPUSet
err := unix.SchedGetaffinity(0, &mask)
if err != nil {
return 0
}
return mask.Count()
}

View File

@@ -1,8 +0,0 @@
//go:build !linux && !windows
package sysinfo
func numCPU() int {
// not implemented
return 0
}

View File

@@ -1,36 +0,0 @@
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
import (
"unsafe"
"golang.org/x/sys/windows"
)
var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
getCurrentProcess = kernel32.NewProc("GetCurrentProcess")
getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask")
)
// Returns bit count of 1, used by NumCPU
func popcnt(x uint64) (n byte) {
x -= (x >> 1) & 0x5555555555555555
x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
x += x >> 4
x &= 0x0f0f0f0f0f0f0f0f
x *= 0x0101010101010101
return byte(x >> 56)
}
func numCPU() int {
// Gets the affinity mask for a process
var mask, sysmask uintptr
currentProcess, _, _ := getCurrentProcess.Call()
ret, _, _ := getProcessAffinityMask.Call(currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask)))
if ret == 0 {
return 0
}
// For every available thread a bit is set in the mask.
ncpu := int(popcnt(uint64(mask)))
return ncpu
}

View File

@@ -66,12 +66,6 @@ func TestNew(t *testing.T) {
}
}
func TestNumCPU(t *testing.T) {
if cpuNumbers := NumCPU(); cpuNumbers <= 0 {
t.Fatal("CPU returned must be greater than zero")
}
}
func TestIsCpusetListAvailable(t *testing.T) {
cases := []struct {
provided string