Pārlūkot izejas kodu

Merge pull request #44663 from thaJeztah/move_meminfo

pkg/system: move memory-info types to pkg/systeminfo, and minor refactor
Sebastiaan van Stijn 2 gadi atpakaļ
vecāks
revīzija
c4ed09ad4e

+ 3 - 4
daemon/info.go

@@ -18,7 +18,6 @@ import (
 	"github.com/docker/docker/pkg/parsers/operatingsystem"
 	"github.com/docker/docker/pkg/platform"
 	"github.com/docker/docker/pkg/sysinfo"
-	"github.com/docker/docker/pkg/system"
 	"github.com/docker/docker/registry"
 	metrics "github.com/docker/go-metrics"
 	"github.com/opencontainers/selinux/go-selinux"
@@ -250,11 +249,11 @@ func kernelVersion() string {
 	return kernelVersion
 }
 
-func memInfo() *system.MemInfo {
-	memInfo, err := system.ReadMemInfo()
+func memInfo() *sysinfo.Memory {
+	memInfo, err := sysinfo.ReadMemInfo()
 	if err != nil {
 		logrus.Errorf("Could not read system memory info: %v", err)
-		memInfo = &system.MemInfo{}
+		memInfo = &sysinfo.Memory{}
 	}
 	return memInfo
 }

+ 2 - 2
daemon/stats_collector.go

@@ -5,7 +5,7 @@ import (
 	"time"
 
 	"github.com/docker/docker/daemon/stats"
-	"github.com/docker/docker/pkg/system"
+	"github.com/docker/docker/pkg/sysinfo"
 )
 
 // newStatsCollector returns a new statsCollector that collections
@@ -15,7 +15,7 @@ import (
 func (daemon *Daemon) newStatsCollector(interval time.Duration) *stats.Collector {
 	// FIXME(vdemeester) move this elsewhere
 	if runtime.GOOS == "linux" {
-		meminfo, err := system.ReadMemInfo()
+		meminfo, err := sysinfo.ReadMemInfo()
 		if err == nil && meminfo.MemTotal > 0 {
 			daemon.machineMemory = uint64(meminfo.MemTotal)
 		}

+ 24 - 0
pkg/sysinfo/meminfo.go

@@ -0,0 +1,24 @@
+package sysinfo
+
+// ReadMemInfo retrieves memory statistics of the host system and returns a
+// Memory type. It is only supported on Linux and Windows, and returns an
+// error on other platforms.
+func ReadMemInfo() (*Memory, error) {
+	return readMemInfo()
+}
+
+// Memory contains memory statistics of the host system.
+type Memory struct {
+	// Total usable RAM (i.e. physical RAM minus a few reserved bits and the
+	// kernel binary code).
+	MemTotal int64
+
+	// Amount of free memory.
+	MemFree int64
+
+	// Total amount of swap space available.
+	SwapTotal int64
+
+	// Amount of swap space that is currently unused.
+	SwapFree int64
+}

+ 7 - 7
pkg/system/meminfo_linux.go → pkg/sysinfo/meminfo_linux.go

@@ -1,4 +1,4 @@
-package system // import "github.com/docker/docker/pkg/system"
+package sysinfo
 
 import (
 	"bufio"
@@ -8,9 +8,9 @@ import (
 	"strings"
 )
 
-// ReadMemInfo retrieves memory statistics of the host system and returns a
-// MemInfo type.
-func ReadMemInfo() (*MemInfo, error) {
+// readMemInfo retrieves memory statistics of the host system and returns a
+// Memory type.
+func readMemInfo() (*Memory, error) {
 	file, err := os.Open("/proc/meminfo")
 	if err != nil {
 		return nil, err
@@ -20,10 +20,10 @@ func ReadMemInfo() (*MemInfo, error) {
 }
 
 // parseMemInfo parses the /proc/meminfo file into
-// a MemInfo object given an io.Reader to the file.
+// a Memory object given an io.Reader to the file.
 // Throws error if there are problems reading from the file
-func parseMemInfo(reader io.Reader) (*MemInfo, error) {
-	meminfo := &MemInfo{}
+func parseMemInfo(reader io.Reader) (*Memory, error) {
+	meminfo := &Memory{}
 	scanner := bufio.NewScanner(reader)
 	memAvailable := int64(-1)
 	for scanner.Scan() {

+ 8 - 7
pkg/system/meminfo_unix_test.go → pkg/sysinfo/meminfo_unix_test.go

@@ -1,13 +1,11 @@
 //go:build linux || freebsd
 // +build linux freebsd
 
-package system // import "github.com/docker/docker/pkg/system"
+package sysinfo
 
 import (
 	"strings"
 	"testing"
-
-	units "github.com/docker/go-units"
 )
 
 // TestMemInfo tests parseMemInfo with a static meminfo string
@@ -23,20 +21,23 @@ func TestMemInfo(t *testing.T) {
 	Malformed3:    2 MB
 	Malformed4:    X kB
 	`
+
+	const KiB = 1024
+
 	meminfo, err := parseMemInfo(strings.NewReader(input))
 	if err != nil {
 		t.Fatal(err)
 	}
-	if meminfo.MemTotal != 1*units.KiB {
+	if meminfo.MemTotal != 1*KiB {
 		t.Fatalf("Unexpected MemTotal: %d", meminfo.MemTotal)
 	}
-	if meminfo.MemFree != 3*units.KiB {
+	if meminfo.MemFree != 3*KiB {
 		t.Fatalf("Unexpected MemFree: %d", meminfo.MemFree)
 	}
-	if meminfo.SwapTotal != 4*units.KiB {
+	if meminfo.SwapTotal != 4*KiB {
 		t.Fatalf("Unexpected SwapTotal: %d", meminfo.SwapTotal)
 	}
-	if meminfo.SwapFree != 5*units.KiB {
+	if meminfo.SwapFree != 5*KiB {
 		t.Fatalf("Unexpected SwapFree: %d", meminfo.SwapFree)
 	}
 }

+ 11 - 0
pkg/sysinfo/meminfo_unsupported.go

@@ -0,0 +1,11 @@
+//go:build !linux && !windows
+// +build !linux,!windows
+
+package sysinfo
+
+import "errors"
+
+// readMemInfo is not supported on platforms other than linux and windows.
+func readMemInfo() (*Memory, error) {
+	return nil, errors.New("platform and architecture is not supported")
+}

+ 5 - 5
pkg/system/meminfo_windows.go → pkg/sysinfo/meminfo_windows.go

@@ -1,4 +1,4 @@
-package system // import "github.com/docker/docker/pkg/system"
+package sysinfo
 
 import (
 	"unsafe"
@@ -27,16 +27,16 @@ type memorystatusex struct {
 }
 
 // ReadMemInfo retrieves memory statistics of the host system and returns a
-// MemInfo type.
-func ReadMemInfo() (*MemInfo, error) {
+// Memory type.
+func readMemInfo() (*Memory, error) {
 	msi := &memorystatusex{
 		dwLength: 64,
 	}
 	r1, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msi)))
 	if r1 == 0 {
-		return &MemInfo{}, nil
+		return &Memory{}, nil
 	}
-	return &MemInfo{
+	return &Memory{
 		MemTotal:  int64(msi.ullTotalPhys),
 		MemFree:   int64(msi.ullAvailPhys),
 		SwapTotal: int64(msi.ullTotalPageFile),

+ 6 - 4
pkg/sysinfo/numcpu.go

@@ -1,13 +1,15 @@
-//go:build !linux && !windows
-// +build !linux,!windows
-
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
 	"runtime"
 )
 
-// NumCPU returns the number of CPUs
+// 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].
 func NumCPU() int {
+	if ncpu := numCPU(); ncpu > 0 {
+		return ncpu
+	}
 	return runtime.NumCPU()
 }

+ 0 - 10
pkg/sysinfo/numcpu_linux.go

@@ -1,8 +1,6 @@
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
-	"runtime"
-
 	"golang.org/x/sys/unix"
 )
 
@@ -23,11 +21,3 @@ func numCPU() int {
 
 	return mask.Count()
 }
-
-// NumCPU returns the number of CPUs which are currently online
-func NumCPU() int {
-	if ncpu := numCPU(); ncpu > 0 {
-		return ncpu
-	}
-	return runtime.NumCPU()
-}

+ 9 - 0
pkg/sysinfo/numcpu_other.go

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

+ 0 - 9
pkg/sysinfo/numcpu_windows.go

@@ -1,7 +1,6 @@
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
-	"runtime"
 	"unsafe"
 
 	"golang.org/x/sys/windows"
@@ -35,11 +34,3 @@ func numCPU() int {
 	ncpu := int(popcnt(uint64(mask)))
 	return ncpu
 }
-
-// NumCPU returns the number of CPUs which are currently online
-func NumCPU() int {
-	if ncpu := numCPU(); ncpu > 0 {
-		return ncpu
-	}
-	return runtime.NumCPU()
-}

+ 0 - 17
pkg/system/meminfo.go

@@ -1,17 +0,0 @@
-package system // import "github.com/docker/docker/pkg/system"
-
-// MemInfo contains memory statistics of the host system.
-type MemInfo struct {
-	// Total usable RAM (i.e. physical RAM minus a few reserved bits and the
-	// kernel binary code).
-	MemTotal int64
-
-	// Amount of free memory.
-	MemFree int64
-
-	// Total amount of swap space available.
-	SwapTotal int64
-
-	// Amount of swap space that is currently unused.
-	SwapFree int64
-}

+ 16 - 0
pkg/system/meminfo_deprecated.go

@@ -0,0 +1,16 @@
+package system
+
+import "github.com/docker/docker/pkg/sysinfo"
+
+// MemInfo contains memory statistics of the host system.
+//
+// Deprecated: use [sysinfo.Memory].
+type MemInfo = sysinfo.Memory
+
+// ReadMemInfo retrieves memory statistics of the host system and returns a
+// MemInfo type.
+//
+// Deprecated: use [sysinfo.ReadMemInfo].
+func ReadMemInfo() (*sysinfo.Memory, error) {
+	return sysinfo.ReadMemInfo()
+}

+ 0 - 9
pkg/system/meminfo_unsupported.go

@@ -1,9 +0,0 @@
-//go:build !linux && !windows
-// +build !linux,!windows
-
-package system // import "github.com/docker/docker/pkg/system"
-
-// ReadMemInfo is not supported on platforms other than linux and windows.
-func ReadMemInfo() (*MemInfo, error) {
-	return nil, ErrNotSupportedPlatform
-}