@@ -5,13 +5,17 @@ package qemu
55import (
66 "fmt"
77 "os"
8+ "runtime"
9+ "syscall"
810
911 "github.com/golang/glog"
12+ "github.com/hyperhq/hypercontainer-utils/hlog"
1013 "github.com/hyperhq/runv/hypervisor"
1114)
1215
1316const (
14- QEMU_SYSTEM_EXE = "qemu-system-x86_64"
17+ QEMU_SYSTEM_EXE = "qemu-system-x86_64"
18+ X86_64_CONFIG_NR_CPUS = 64
1519)
1620
1721func (qc * QemuContext ) arguments (ctx * hypervisor.VmContext ) []string {
@@ -26,10 +30,23 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string {
2630 boot := ctx .Boot
2731 qc .cpus = boot .CPU
2832
33+ maxmem := hypervisor .DefaultMaxMem
34+ var sysInfo syscall.Sysinfo_t
35+ err := syscall .Sysinfo (& sysInfo )
36+ if err == nil {
37+ maxmem = int (sysInfo .Totalram / 1024 / 1024 )
38+ } else {
39+ ctx .Log (hlog .DEBUG , "syscall.Sysinfo got error %v, use hypervisor.DefaultMaxMem" , err )
40+ }
41+ maxcpus := runtime .NumCPU ()
42+ if maxcpus > X86_64_CONFIG_NR_CPUS {
43+ maxcpus = X86_64_CONFIG_NR_CPUS
44+ }
45+
2946 var machineClass , memParams , cpuParams string
3047 machineClass = "pc-i440fx-2.1"
31- memParams = fmt .Sprintf ("size=%d,slots=1,maxmem=%dM" , boot .Memory , hypervisor . DefaultMaxMem ) // TODO set maxmem to the total memory of the system
32- cpuParams = fmt .Sprintf ("cpus=%d,maxcpus=%d" , boot .CPU , hypervisor . DefaultMaxCpus ) // TODO set it to the cpus of the system
48+ memParams = fmt .Sprintf ("size=%d,slots=1,maxmem=%dM" , boot .Memory , maxmem )
49+ cpuParams = fmt .Sprintf ("cpus=%d,maxcpus=%d" , boot .CPU , maxcpus )
3350
3451 cmdline := "console=ttyS0 panic=1 no_timer_check iommu=off"
3552 params := []string {
@@ -64,20 +81,20 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string {
6481 //TODO: mount hugetlbfs on /dev/hugepages
6582 boot .MemoryPath = "/dev/hugepages"
6683 memObject := fmt .Sprintf ("memory-backend-file,id=hyper-memory,size=%dM,mem-path=%s,share=on" , boot .Memory , boot .MemoryPath )
67- nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,memdev=hyper-memory" , hypervisor . DefaultMaxCpus - 1 )
84+ nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,memdev=hyper-memory" , maxcpus - 1 )
6885 params = append (params , "-object" , memObject , "-numa" , nodeConfig )
6986 } else if boot .BootToBeTemplate || boot .BootFromTemplate {
7087 memObject := fmt .Sprintf ("memory-backend-file,id=hyper-template-memory,size=%dM,mem-path=%s" , boot .Memory , boot .MemoryPath )
7188 if boot .BootToBeTemplate {
7289 memObject = memObject + ",share=on"
7390 }
74- nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,memdev=hyper-template-memory" , hypervisor . DefaultMaxCpus - 1 )
91+ nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,memdev=hyper-template-memory" , maxcpus - 1 )
7592 params = append (params , "-object" , memObject , "-numa" , nodeConfig )
7693 if boot .BootFromTemplate {
7794 params = append (params , "-S" , "-incoming" , fmt .Sprintf ("exec:cat %s" , boot .DevicesStatePath ))
7895 }
7996 } else {
80- nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,mem=%d" , hypervisor . DefaultMaxCpus - 1 , boot .Memory )
97+ nodeConfig := fmt .Sprintf ("node,nodeid=0,cpus=0-%d,mem=%d" , maxcpus - 1 , boot .Memory )
8198 params = append (params , "-numa" , nodeConfig )
8299 }
83100
0 commit comments