Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #297 from dvoytik/add_cfg_vcpus_memory
Browse files Browse the repository at this point in the history
config: Add default_{vcpus, memory} configurations
  • Loading branch information
jodh-intel authored Jul 18, 2017
2 parents 79abac4 + d14ce99 commit 0b30b64
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ PAUSEBINRELPATH := bin/pause

GLOBALLOGPATH := $(PKGLIBDIR)/runtime/runtime.log

# Default number of vCPUs
DEFVCPUS := 1
# Default memory size in MiB
DEFMEMSZ := 2048

SED = sed

SOURCES := $(shell find . 2>&1 | grep -E '.*\.(c|h|go)$$')
Expand Down Expand Up @@ -139,6 +144,8 @@ USER_VARS += SHAREDIR
USER_VARS += SHIMPATH
USER_VARS += SYSCONFDIR
USER_VARS += PAUSEDESTDIR
USER_VARS += DEFVCPUS
USER_VARS += DEFMEMSZ


V = @
Expand Down Expand Up @@ -178,6 +185,9 @@ const defaultRuntimeRun = "$(PKGRUNDIR)"
const defaultShimPath = "$(SHIMPATH)"
const pauseBinRelativePath = "$(PAUSEBINRELPATH)"

const defaultVCPUCount uint32 = $(DEFVCPUS)
const defaultMemSize uint32 = $(DEFMEMSZ) // MiB

// Required to be modifiable (for the tests)
var defaultRuntimeConfiguration = "$(DESTCONFIG)"
var defaultProxyPath = "$(PROXYPATH)"
Expand Down Expand Up @@ -223,6 +233,8 @@ $(CONFIG): $(CONFIG_IN)
-e "s|@QEMUPATH@|$(QEMUPATH)|g" \
-e "s|@SHIMPATH@|$(SHIMPATH)|g" \
-e "s|@GLOBALLOGPATH@|$(GLOBALLOGPATH)|g" \
-e "s|@DEFVCPUS@|$(DEFVCPUS)|g" \
-e "s|@DEFMEMSZ@|$(DEFMEMSZ)|g" \
$< > $@

generate-config: $(CONFIG)
Expand Down
35 changes: 32 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
goruntime "runtime"

"github.com/BurntSushi/toml"
vc "github.com/containers/virtcontainers"
Expand Down Expand Up @@ -73,9 +74,11 @@ type tomlConfig struct {
}

type hypervisor struct {
Path string
Kernel string
Image string
Path string
Kernel string
Image string
DefaultVCPUs int32 `toml:"default_vcpus"`
DefaultMemSz uint32 `toml:"default_memory"`
}

type proxy struct {
Expand Down Expand Up @@ -118,6 +121,28 @@ func (h hypervisor) image() string {
return h.Image
}

func (h hypervisor) defaultVCPUs() uint32 {
if h.DefaultVCPUs < 0 {
return uint32(goruntime.NumCPU())
}
if h.DefaultVCPUs == 0 { // or unspecified
return defaultVCPUCount
}
if h.DefaultVCPUs > 255 { // qemu supports max 255
return 255
}

return uint32(h.DefaultVCPUs)
}

func (h hypervisor) defaultMemSz() uint32 {
if h.DefaultMemSz < 8 {
return defaultMemSize // MiB
}

return h.DefaultMemSz
}

func (p proxy) url() string {
if p.URL == "" {
return defaultProxyURL
Expand Down Expand Up @@ -158,6 +183,8 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
HypervisorPath: hypervisor,
KernelPath: kernel,
ImagePath: image,
DefaultVCPUs: h.defaultVCPUs(),
DefaultMemSz: h.defaultMemSz(),
}, nil
}

Expand Down Expand Up @@ -264,6 +291,8 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat
HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath,
ImagePath: defaultImagePath,
DefaultVCPUs: defaultVCPUCount,
DefaultMemSz: defaultMemSize,
}

defaultAgentConfig := vc.HyperConfig{
Expand Down
8 changes: 8 additions & 0 deletions config/configuration.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
path = "@QEMUPATH@"
kernel = "@KERNELPATH@"
image = "@IMAGEPATH@"
# Default number of vCPUs per POD/VM:
# unspecified or 0 --> will be set to @DEFVCPUS@
# < 0 --> will be set to the actual number of physical cores
# > 255 --> will be set to 255.
default_vcpus = -1
# Default memory size in MiB for POD/VM.
# If unspecified then it will be set @DEFMEMSZ@ MiB.
#default_memory = @DEFMEMSZ@

[proxy.cc]
url = "@PROXYURL@"
Expand Down
24 changes: 24 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path"
"path/filepath"
"reflect"
goruntime "runtime"
"strconv"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -49,6 +51,8 @@ func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath
path = "` + hypervisorPath + `"
kernel = "` + kernelPath + `"
image = "` + imagePath + `"
default_vcpus = ` + strconv.FormatUint(uint64(defaultVCPUCount), 10) + `
default_memory = ` + strconv.FormatUint(uint64(defaultMemSize), 10) + `
[proxy.cc]
url = "` + proxyURL + `"
Expand Down Expand Up @@ -131,6 +135,8 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
HypervisorPath: hypervisorPath,
KernelPath: kernelPath,
ImagePath: imagePath,
DefaultVCPUs: defaultVCPUCount,
DefaultMemSz: defaultMemSize,
}

agentConfig := vc.HyperConfig{
Expand Down Expand Up @@ -586,6 +592,8 @@ func TestMinimalRuntimeConfig(t *testing.T) {
HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath,
ImagePath: defaultImagePath,
DefaultVCPUs: defaultVCPUCount,
DefaultMemSz: defaultMemSize,
}

expectedAgentConfig := vc.HyperConfig{
Expand Down Expand Up @@ -770,6 +778,8 @@ func TestHypervisorDefaults(t *testing.T) {
assert.Equal(t, h.path(), defaultHypervisorPath, "default hypervisor path wrong")
assert.Equal(t, h.kernel(), defaultKernelPath, "default hypervisor kernel wrong")
assert.Equal(t, h.image(), defaultImagePath, "default hypervisor image wrong")
assert.Equal(t, h.defaultVCPUs(), defaultVCPUCount, "default vCPU number is wrong")
assert.Equal(t, h.defaultMemSz(), defaultMemSize, "default memory size is wrong")

path := "/foo"
h.Path = path
Expand All @@ -782,6 +792,20 @@ func TestHypervisorDefaults(t *testing.T) {
image := "foo"
h.Image = image
assert.Equal(t, h.image(), image, "custom hypervisor image wrong")

// auto inferring
h.DefaultVCPUs = -1
assert.Equal(t, h.defaultVCPUs(), uint32(goruntime.NumCPU()), "default vCPU number is wrong")

h.DefaultVCPUs = 2
assert.Equal(t, h.defaultVCPUs(), uint32(2), "default vCPU number is wrong")

// qemu supports max 255
h.DefaultVCPUs = 8086
assert.Equal(t, h.defaultVCPUs(), uint32(255), "default vCPU number is wrong")

h.DefaultMemSz = 1024
assert.Equal(t, h.defaultMemSz(), uint32(1024), "default memory size is wrong")
}

func TestProxyDefaults(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/containers/virtcontainers/NEWS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/containers/virtcontainers/hypervisor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions vendor/github.com/containers/virtcontainers/qemu.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b30b64

Please sign in to comment.