From a355cc17e5862739b0b964a51908c745dadbcfec Mon Sep 17 00:00:00 2001 From: Hui Zhu Date: Wed, 15 Aug 2018 17:38:09 +0800 Subject: [PATCH] AARCH64 support use host memory size and cpu number as maxmem and maxcpus Signed-off-by: Hui Zhu --- hypervisor/qemu/qemu_amd64.go | 17 ++--------------- hypervisor/qemu/qemu_arm64.go | 15 ++++++++++----- hypervisor/qemu/qemu_ppc64le.go | 2 +- hypervisor/qemu/qemu_process.go | 17 +++++++++++++++-- hypervisor/qemu/qemu_s390x.go | 2 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/hypervisor/qemu/qemu_amd64.go b/hypervisor/qemu/qemu_amd64.go index 1b7495d7..c8ad720e 100644 --- a/hypervisor/qemu/qemu_amd64.go +++ b/hypervisor/qemu/qemu_amd64.go @@ -4,13 +4,9 @@ package qemu import ( "fmt" - "os" - "runtime" - "syscall" - "github.com/golang/glog" - "github.com/hyperhq/hypercontainer-utils/hlog" "github.com/hyperhq/runv/hypervisor" + "os" ) const ( @@ -18,7 +14,7 @@ const ( X86_64_CONFIG_NR_CPUS = 64 ) -func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { +func (qc *QemuContext) arguments(ctx *hypervisor.VmContext, maxmem, maxcpus int) []string { if ctx.Boot == nil { ctx.Boot = &hypervisor.BootConfig{ CPU: 1, @@ -30,15 +26,6 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { boot := ctx.Boot qc.cpus = boot.CPU - maxmem := hypervisor.DefaultMaxMem - var sysInfo syscall.Sysinfo_t - err := syscall.Sysinfo(&sysInfo) - if err == nil { - maxmem = int(sysInfo.Totalram / 1024 / 1024) - } else { - ctx.Log(hlog.DEBUG, "syscall.Sysinfo got error %v, use hypervisor.DefaultMaxMem", err) - } - maxcpus := runtime.NumCPU() if maxcpus > X86_64_CONFIG_NR_CPUS { maxcpus = X86_64_CONFIG_NR_CPUS } diff --git a/hypervisor/qemu/qemu_arm64.go b/hypervisor/qemu/qemu_arm64.go index d8b4fb0e..3e596cd8 100644 --- a/hypervisor/qemu/qemu_arm64.go +++ b/hypervisor/qemu/qemu_arm64.go @@ -11,11 +11,12 @@ import ( ) const ( - QEMU_SYSTEM_EXE = "qemu-system-aarch64" - VM_MIN_MEMORY_SIZE = 128 + QEMU_SYSTEM_EXE = "qemu-system-aarch64" + VM_MIN_MEMORY_SIZE = 128 + AARCH64_CONFIG_NR_CPUS = 8 ) -func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { +func (qc *QemuContext) arguments(ctx *hypervisor.VmContext, maxmem, maxcpus int) []string { if ctx.Boot == nil { ctx.Boot = &hypervisor.BootConfig{ CPU: 1, @@ -27,13 +28,17 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { boot := ctx.Boot qc.cpus = boot.CPU + if maxcpus > AARCH64_CONFIG_NR_CPUS { + maxcpus = AARCH64_CONFIG_NR_CPUS + } + // Currently the default memory size is fixed to 128 MiB. if boot.Memory < VM_MIN_MEMORY_SIZE { boot.Memory = VM_MIN_MEMORY_SIZE } - memParams := fmt.Sprintf("size=%d,slots=1,maxmem=%dM", boot.Memory, hypervisor.DefaultMaxMem) - cpuParams := fmt.Sprintf("cpus=%d,maxcpus=%d", boot.CPU, hypervisor.DefaultMaxCpus) + memParams := fmt.Sprintf("size=%d,slots=1,maxmem=%dM", boot.Memory, maxmem) + cpuParams := fmt.Sprintf("cpus=%d,maxcpus=%d", boot.CPU, maxcpus) params := []string{"-machine", "virt,accel=kvm,gic-version=host,usb=off", "-global", "kvm-pit.lost_tick_policy=discard", "-cpu", "host"} if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { diff --git a/hypervisor/qemu/qemu_ppc64le.go b/hypervisor/qemu/qemu_ppc64le.go index d24d5ffe..d1416c42 100644 --- a/hypervisor/qemu/qemu_ppc64le.go +++ b/hypervisor/qemu/qemu_ppc64le.go @@ -14,7 +14,7 @@ const ( VM_MIN_MEMORY_SIZE = 256 // On ppc64le the minimum memory size of a VM is 256 MiB ) -func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { +func (qc *QemuContext) arguments(ctx *hypervisor.VmContext, maxmem, maxcpus int) []string { if ctx.Boot == nil { ctx.Boot = &hypervisor.BootConfig{ CPU: 1, diff --git a/hypervisor/qemu/qemu_process.go b/hypervisor/qemu/qemu_process.go index 5f55f56e..d3c20963 100644 --- a/hypervisor/qemu/qemu_process.go +++ b/hypervisor/qemu/qemu_process.go @@ -9,10 +9,13 @@ import ( "io" "os" "os/exec" + "runtime" "strings" + "syscall" "time" "github.com/golang/glog" + "github.com/hyperhq/hypercontainer-utils/hlog" "github.com/hyperhq/runv/hypervisor" ) @@ -126,7 +129,17 @@ func launchQemu(qc *QemuContext, ctx *hypervisor.VmContext) { return } - args := qc.arguments(ctx) + maxmem := hypervisor.DefaultMaxMem + var sysInfo syscall.Sysinfo_t + err := syscall.Sysinfo(&sysInfo) + if err == nil { + maxmem = int(sysInfo.Totalram / 1024 / 1024) + } else { + ctx.Log(hlog.ERROR, "syscall.Sysinfo got error %v, use hypervisor.DefaultMaxMem", err) + } + maxcpus := runtime.NumCPU() + + args := qc.arguments(ctx, maxmem, maxcpus) args = append(args, "-daemonize", "-pidfile", qc.qemuPidFile, "-D", qc.qemuLogFile.Name) if ctx.GDBTCPPort != 0 { args = append(args, "-gdb", fmt.Sprintf("tcp::%d", ctx.GDBTCPPort)) @@ -150,7 +163,7 @@ func launchQemu(qc *QemuContext, ctx *hypervisor.VmContext) { cmd.Stdout = stdout cmd.Stderr = stderr - err := cmd.Run() + err = cmd.Run() if stdout.Len() != 0 { glog.V(1).Info(stdout.String()) diff --git a/hypervisor/qemu/qemu_s390x.go b/hypervisor/qemu/qemu_s390x.go index 6df61506..e0a25c2a 100644 --- a/hypervisor/qemu/qemu_s390x.go +++ b/hypervisor/qemu/qemu_s390x.go @@ -13,7 +13,7 @@ const ( QEMU_SYSTEM_EXE = "qemu-system-s390x" ) -func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string { +func (qc *QemuContext) arguments(ctx *hypervisor.VmContext, maxmem, maxcpus int) []string { if ctx.Boot == nil { ctx.Boot = &hypervisor.BootConfig{ CPU: 1,