deprecate pkg/platform and move internal

Functions in this package are only used internally in the daemon for
the `/info` endpoint (Architecture), and as part of `stats` (NumProcs).

I was not able to find external consumers, but deprecating the package
first, so that we can remove / dismantle the package in a follow-up.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-11-13 15:05:56 +01:00
parent a95a6788b5
commit b034dc41a2
6 changed files with 56 additions and 15 deletions

View File

@@ -0,0 +1,31 @@
package platform
import (
"context"
"sync"
"github.com/containerd/log"
)
var (
arch string
onceArch sync.Once
)
// Architecture returns the runtime architecture of the process.
//
// Unlike [runtime.GOARCH] (which refers to the compiler platform),
// Architecture refers to the running platform.
//
// For example, Architecture reports "x86_64" as architecture, even
// when running a "linux/386" compiled binary on "linux/amd64" hardware.
func Architecture() string {
onceArch.Do(func() {
var err error
arch, err = runtimeArchitecture()
if err != nil {
log.G(context.TODO()).WithError(err).Error("Could not read system architecture info")
}
})
return arch
}

View File

@@ -0,0 +1,25 @@
//go:build !windows
package platform
import (
"golang.org/x/sys/unix"
)
// runtimeArchitecture gets the name of the current architecture (x86, x86_64, i86pc, sun4v, ...)
func runtimeArchitecture() (string, error) {
utsname := &unix.Utsname{}
if err := unix.Uname(utsname); err != nil {
return "", err
}
return unix.ByteSliceToString(utsname.Machine[:]), nil
}
// NumProcs returns the number of processors on the system
//
// Deprecated: temporary stub for non-Windows to provide an alias for the deprecated github.com/docker/docker/pkg/platform package.
//
// FIXME(thaJeztah): remove once we remove github.com/docker/docker/pkg/platform
func NumProcs() uint32 {
return 0
}

View File

@@ -0,0 +1,71 @@
package platform
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
)
// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
type systeminfo struct {
wProcessorArchitecture uint16
wReserved uint16
dwPageSize uint32
lpMinimumApplicationAddress uintptr
lpMaximumApplicationAddress uintptr
dwActiveProcessorMask uintptr
dwNumberOfProcessors uint32
dwProcessorType uint32
dwAllocationGranularity uint32
wProcessorLevel uint16
wProcessorRevision uint16
}
// Windows processor architectures.
//
// see https://github.com/microsoft/go-winio/blob/v0.6.0/wim/wim.go#L48-L65
// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
const (
processorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64
processorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
processorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL
processorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM
processorArchitectureArm64 = 12 // PROCESSOR_ARCHITECTURE_ARM64
)
// runtimeArchitecture gets the name of the current architecture (x86, x86_64, …)
func runtimeArchitecture() (string, error) {
// TODO(thaJeztah): rewrite this to use "GetNativeSystemInfo" instead.
// See: https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
// See: https://github.com/shirou/gopsutil/blob/v3.23.3/host/host_windows.go#L267-L297
// > To retrieve accurate information for an application running on WOW64,
// > call the GetNativeSystemInfo function.
var sysinfo systeminfo
_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
switch sysinfo.wProcessorArchitecture {
case processorArchitecture64, processorArchitectureIA64:
return "x86_64", nil
case processorArchitecture32:
return "i686", nil
case processorArchitectureArm:
return "arm", nil
case processorArchitectureArm64:
return "arm64", nil
default:
return "", fmt.Errorf("unknown processor architecture %+v", sysinfo.wProcessorArchitecture)
}
}
// NumProcs returns the number of processors on the system
func NumProcs() uint32 {
var sysinfo systeminfo
_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
return sysinfo.dwNumberOfProcessors
}