Add pkg/parsers/architecture and pkg/platform

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2015-11-14 23:03:02 +01:00
parent 4e8fcd4002
commit 49779b674a
8 changed files with 124 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/parsers/kernel"
"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"
@ -75,8 +76,8 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
KernelVersion: kernelVersion,
OperatingSystem: operatingSystem,
IndexServerAddress: registry.IndexServer,
OSType: runtime.GOOS,
Architecture: runtime.GOARCH,
OSType: platform.OSType,
Architecture: platform.Architecture,
RegistryConfig: daemon.RegistryService.Config,
InitSha1: dockerversion.InitSHA1,
InitPath: initPath,

View File

@ -1884,7 +1884,7 @@ Display system-wide information
Content-Type: application/json
{
"Architecture": "amd64",
"Architecture": "x86_64",
"Containers": 11,
"CpuCfsPeriod": true,
"CpuCfsQuota": true,

View File

@ -32,7 +32,7 @@ For example:
Logging Driver: json-file
Kernel Version: 3.19.0-22-generic
OSType: linux
Architecture: amd64
Architecture: x86_64
Operating System: Ubuntu 15.04
CPUs: 24
Total Memory: 62.86 GiB

View File

@ -42,7 +42,7 @@ Here is a sample output:
Kernel Version: 3.13.0-24-generic
Operating System: Ubuntu 14.04 LTS
OSType: linux
Architecture: amd64
Architecture: x86_64
CPUs: 1
Total Memory: 2 GiB

View File

@ -0,0 +1,15 @@
package platform
import (
"os/exec"
)
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
func GetRuntimeArchitecture() (string, error) {
cmd := exec.Command("uname", "-m")
machine, err := cmd.Output()
if err != nil {
return "", err
}
return string(machine), nil
}

View File

@ -0,0 +1,28 @@
// Package platform provides helper function to get the runtime architecture
// for different platforms.
package platform
import (
"syscall"
)
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
func GetRuntimeArchitecture() (string, error) {
utsname := &syscall.Utsname{}
if err := syscall.Uname(utsname); err != nil {
return "", err
}
return charsToString(utsname.Machine), nil
}
func charsToString(ca [65]int8) string {
s := make([]byte, len(ca))
var lens int
for ; lens < len(ca); lens++ {
if ca[lens] == 0 {
break
}
s[lens] = uint8(ca[lens])
}
return string(s[0:lens])
}

View File

@ -0,0 +1,52 @@
package platform
import (
"fmt"
"syscall"
"unsafe"
)
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
)
// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
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
}
// Constants
const (
ProcessorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64
ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
ProcessorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL
ProcessorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM
)
var sysinfo systeminfo
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
func GetRuntimeArchitecture() (string, error) {
syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
switch sysinfo.wProcessorArchitecture {
case ProcessorArchitecture64, ProcessorArchitectureIA64:
return "x86_64", nil
case ProcessorArchitecture32:
return "i686", nil
case ProcessorArchitectureArm:
return "arm", nil
default:
return "", fmt.Errorf("Unknown processor architecture")
}
}

23
pkg/platform/platform.go Normal file
View File

@ -0,0 +1,23 @@
package platform
import (
"runtime"
"github.com/Sirupsen/logrus"
)
var (
// Architecture holds the runtime architecture of the process.
Architecture string
// OSType holds the runtime operating system type (Linux, …) of the process.
OSType string
)
func init() {
var err error
Architecture, err = GetRuntimeArchitecture()
if err != nil {
logrus.Errorf("Could no read system architecture info: %v", err)
}
OSType = runtime.GOOS
}