Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
hypervisor: Add Default{VCPUs, MemSz} to HypervisorConfig
Browse files Browse the repository at this point in the history
A runtime implementation can use these fields to setup the default
vCPU number and default memory size for new VMs.
VMConfig can overwrite these settings.

Signed-off-by: Dmitry Voytik <[email protected]>
  • Loading branch information
Dmitry Voytik committed Jul 6, 2017
1 parent 65f177c commit 98ff4b8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
16 changes: 16 additions & 0 deletions hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ type HypervisorConfig struct {
// Debug changes the default hypervisor and kernel parameters to
// enable debug output where available.
Debug bool

// DefaultVCPUs specifies default number of vCPUs for the VM.
// Pod configuration VMConfig.VCPUs overwrites this.
DefaultVCPUs uint32

// DefaultMem specifies default memory size in MiB for the VM.
// Pod configuration VMConfig.Memory overwrites this.
DefaultMemSz uint32
}

func (conf *HypervisorConfig) valid() (bool, error) {
Expand All @@ -139,6 +147,14 @@ func (conf *HypervisorConfig) valid() (bool, error) {
return false, fmt.Errorf("Missing image path")
}

if conf.DefaultVCPUs == 0 {
conf.DefaultVCPUs = 1
}

if conf.DefaultMemSz == 0 {
conf.DefaultMemSz = 2048 // MiB
}

return true, nil
}

Expand Down
20 changes: 20 additions & 0 deletions hypervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ func TestHypervisorConfigIsValid(t *testing.T) {
testHypervisorConfigValid(t, hypervisorConfig, true)
}

func TestHypervisorConfigDefaults(t *testing.T) {
hypervisorConfig := &HypervisorConfig{
KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel),
ImagePath: fmt.Sprintf("%s/%s", testDir, testImage),
HypervisorPath: "",
}
testHypervisorConfigValid(t, hypervisorConfig, true)

hypervisorConfigDefaultsExpected := &HypervisorConfig{
KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel),
ImagePath: fmt.Sprintf("%s/%s", testDir, testImage),
HypervisorPath: "",
DefaultVCPUs: 1,
DefaultMemSz: 2048,
}
if reflect.DeepEqual(hypervisorConfig, hypervisorConfigDefaultsExpected) == false {
t.Fatal()
}
}

func TestAppendParams(t *testing.T) {
paramList := []Param{
{
Expand Down
15 changes: 6 additions & 9 deletions qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -88,8 +87,6 @@ const (
)

const (
defaultMemSize = "2G"
defaultMemMax = "3G"
defaultMemSlots uint8 = 2
)

Expand Down Expand Up @@ -448,14 +445,14 @@ func (q *qemu) qmpMonitor(connectedCh chan struct{}) {
}

func (q *qemu) setCPUResources(podConfig PodConfig) ciaoQemu.SMP {
vcpus := uint(runtime.NumCPU())
vcpus := q.config.DefaultVCPUs
if podConfig.VMConfig.VCPUs > 0 {
vcpus = podConfig.VMConfig.VCPUs
vcpus = uint32(podConfig.VMConfig.VCPUs)
}

smp := ciaoQemu.SMP{
CPUs: uint32(vcpus),
Cores: uint32(vcpus),
CPUs: vcpus,
Cores: vcpus,
Sockets: defaultSockets,
Threads: defaultThreads,
}
Expand All @@ -464,8 +461,8 @@ func (q *qemu) setCPUResources(podConfig PodConfig) ciaoQemu.SMP {
}

func (q *qemu) setMemoryResources(podConfig PodConfig) ciaoQemu.Memory {
mem := defaultMemSize
memMax := defaultMemMax
mem := fmt.Sprintf("%dM", q.config.DefaultMemSz)
memMax := fmt.Sprintf("%dM", int(float64(q.config.DefaultMemSz)*1.5))
if podConfig.VMConfig.Memory > 0 {
mem = fmt.Sprintf("%dM", podConfig.VMConfig.Memory)
intMemMax := int(float64(podConfig.VMConfig.Memory) * 1.5)
Expand Down
2 changes: 2 additions & 0 deletions qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func newQemuConfig() HypervisorConfig {
KernelPath: testQemuKernelPath,
ImagePath: testQemuImagePath,
HypervisorPath: testQemuPath,
DefaultVCPUs: 1,
DefaultMemSz: 2048,
}
}

Expand Down

0 comments on commit 98ff4b8

Please sign in to comment.