@@ -5,13 +5,17 @@ package qemu
5
5
import (
6
6
"fmt"
7
7
"os"
8
+ "runtime"
9
+ "syscall"
8
10
9
11
"github.com/golang/glog"
12
+ "github.com/hyperhq/hypercontainer-utils/hlog"
10
13
"github.com/hyperhq/runv/hypervisor"
11
14
)
12
15
13
16
const (
14
- QEMU_SYSTEM_EXE = "qemu-system-x86_64"
17
+ QEMU_SYSTEM_EXE = "qemu-system-x86_64"
18
+ X86_64_CONFIG_NR_CPUS = 64
15
19
)
16
20
17
21
func (qc * QemuContext ) arguments (ctx * hypervisor.VmContext ) []string {
@@ -26,10 +30,23 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string {
26
30
boot := ctx .Boot
27
31
qc .cpus = boot .CPU
28
32
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
+
29
46
var machineClass , memParams , cpuParams string
30
47
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 )
33
50
34
51
cmdline := "console=ttyS0 panic=1 no_timer_check iommu=off"
35
52
params := []string {
@@ -64,20 +81,20 @@ func (qc *QemuContext) arguments(ctx *hypervisor.VmContext) []string {
64
81
//TODO: mount hugetlbfs on /dev/hugepages
65
82
boot .MemoryPath = "/dev/hugepages"
66
83
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 )
68
85
params = append (params , "-object" , memObject , "-numa" , nodeConfig )
69
86
} else if boot .BootToBeTemplate || boot .BootFromTemplate {
70
87
memObject := fmt .Sprintf ("memory-backend-file,id=hyper-template-memory,size=%dM,mem-path=%s" , boot .Memory , boot .MemoryPath )
71
88
if boot .BootToBeTemplate {
72
89
memObject = memObject + ",share=on"
73
90
}
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 )
75
92
params = append (params , "-object" , memObject , "-numa" , nodeConfig )
76
93
if boot .BootFromTemplate {
77
94
params = append (params , "-S" , "-incoming" , fmt .Sprintf ("exec:cat %s" , boot .DevicesStatePath ))
78
95
}
79
96
} 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 )
81
98
params = append (params , "-numa" , nodeConfig )
82
99
}
83
100
0 commit comments