Skip to content

Commit

Permalink
pkg/sysinfo: Deprecate NumCPU
Browse files Browse the repository at this point in the history
Deprecate in favor of `runtime.NumCPU` as the behavior is the same now.

Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Jan 9, 2025
1 parent bda51fe commit 3db72b2
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 80 deletions.
2 changes: 1 addition & 1 deletion daemon/daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions daemon/oci_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/Microsoft/hcsshim"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libcontainerd/local/local_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"syscall"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions pkg/sysinfo/numcpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
17 changes: 0 additions & 17 deletions pkg/sysinfo/numcpu_linux.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/sysinfo/numcpu_other.go

This file was deleted.

36 changes: 0 additions & 36 deletions pkg/sysinfo/numcpu_windows.go

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/sysinfo/sysinfo_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3db72b2

Please sign in to comment.