pkg/system: move memory-info types to pkg/systeminfo

These types and functions are more closely related to the functionality
provided by pkg/systeminfo, and used in conjunction with the other functions
in that package, so moving them there.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-16 15:09:40 +01:00
parent 6919b9879b
commit 6a516acb2e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
9 changed files with 47 additions and 30 deletions

View file

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

View file

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

View file

@ -1,7 +1,7 @@
package system // import "github.com/docker/docker/pkg/system" package sysinfo
// MemInfo contains memory statistics of the host system. // Memory contains memory statistics of the host system.
type MemInfo struct { type Memory struct {
// Total usable RAM (i.e. physical RAM minus a few reserved bits and the // Total usable RAM (i.e. physical RAM minus a few reserved bits and the
// kernel binary code). // kernel binary code).
MemTotal int64 MemTotal int64

View file

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

View file

@ -1,7 +1,7 @@
//go:build linux || freebsd //go:build linux || freebsd
// +build linux freebsd // +build linux freebsd
package system // import "github.com/docker/docker/pkg/system" package sysinfo
import ( import (
"strings" "strings"

View file

@ -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")
}

View file

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

View file

@ -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()
}

View file

@ -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
}