From 3db72b255d68a972a8240c6114460442ca52ce59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Thu, 9 Jan 2025 12:39:46 +0100 Subject: [PATCH] pkg/sysinfo: Deprecate NumCPU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deprecate in favor of `runtime.NumCPU` as the behavior is the same now. Signed-off-by: Paweł Gronowski --- daemon/daemon_unix.go | 2 +- daemon/daemon_windows.go | 2 +- daemon/info.go | 2 +- daemon/oci_windows.go | 4 ++-- libcontainerd/local/local_windows.go | 4 ++-- pkg/sysinfo/numcpu.go | 9 +++---- pkg/sysinfo/numcpu_linux.go | 17 ------------- pkg/sysinfo/numcpu_other.go | 8 ------- pkg/sysinfo/numcpu_windows.go | 36 ---------------------------- pkg/sysinfo/sysinfo_linux_test.go | 6 ----- 10 files changed, 10 insertions(+), 80 deletions(-) delete mode 100644 pkg/sysinfo/numcpu_linux.go delete mode 100644 pkg/sysinfo/numcpu_other.go delete mode 100644 pkg/sysinfo/numcpu_windows.go diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index a8fdb62859b39..84da60122f284 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -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) } diff --git a/daemon/daemon_windows.go b/daemon/daemon_windows.go index b0379fcf0fb65..768b31eeedd56 100644 --- a/daemon/daemon_windows.go +++ b/daemon/daemon_windows.go @@ -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) } diff --git a/daemon/info.go b/daemon/info.go index 7c3e9dd521d98..3d46eedc3fb8e 100644 --- a/daemon/info.go +++ b/daemon/info.go @@ -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, diff --git a/daemon/oci_windows.go b/daemon/oci_windows.go index 31107465888e6..e1801d2aa10fd 100644 --- a/daemon/oci_windows.go +++ b/daemon/oci_windows.go @@ -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 diff --git a/libcontainerd/local/local_windows.go b/libcontainerd/local/local_windows.go index e767faa993c3c..20d909538903d 100644 --- a/libcontainerd/local/local_windows.go +++ b/libcontainerd/local/local_windows.go @@ -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 diff --git a/pkg/sysinfo/numcpu.go b/pkg/sysinfo/numcpu.go index 26fe99cfcceec..660f323b11d4e 100644 --- a/pkg/sysinfo/numcpu.go +++ b/pkg/sysinfo/numcpu.go @@ -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() } diff --git a/pkg/sysinfo/numcpu_linux.go b/pkg/sysinfo/numcpu_linux.go deleted file mode 100644 index 0f47c081d1c63..0000000000000 --- a/pkg/sysinfo/numcpu_linux.go +++ /dev/null @@ -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() -} diff --git a/pkg/sysinfo/numcpu_other.go b/pkg/sysinfo/numcpu_other.go deleted file mode 100644 index fcafd56ae39b1..0000000000000 --- a/pkg/sysinfo/numcpu_other.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build !linux && !windows - -package sysinfo - -func numCPU() int { - // not implemented - return 0 -} diff --git a/pkg/sysinfo/numcpu_windows.go b/pkg/sysinfo/numcpu_windows.go deleted file mode 100644 index ff4753445a3b2..0000000000000 --- a/pkg/sysinfo/numcpu_windows.go +++ /dev/null @@ -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 -} diff --git a/pkg/sysinfo/sysinfo_linux_test.go b/pkg/sysinfo/sysinfo_linux_test.go index f847120741da7..c88f1b0f5dcd8 100644 --- a/pkg/sysinfo/sysinfo_linux_test.go +++ b/pkg/sysinfo/sysinfo_linux_test.go @@ -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