Skip to content

Commit

Permalink
hypervisor: rename DefaultVCPUs and DefaultMemSz
Browse files Browse the repository at this point in the history
Now that we only use hypervisor config to set them, they
are not overridden by other configs. So drop the default prefix.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Sep 6, 2018
1 parent 7af8e6c commit a1537a5
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 67 deletions.
22 changes: 11 additions & 11 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ type hypervisor struct {
KernelParams string `toml:"kernel_params"`
MachineType string `toml:"machine_type"`
BlockDeviceDriver string `toml:"block_device_driver"`
DefaultVCPUs int32 `toml:"default_vcpus"`
NumVCPUs int32 `toml:"default_vcpus"`
DefaultMaxVCPUs uint32 `toml:"default_maxvcpus"`
DefaultMemSz uint32 `toml:"default_memory"`
MemorySize uint32 `toml:"default_memory"`
DefaultBridges uint32 `toml:"default_bridges"`
Msize9p uint32 `toml:"msize_9p"`
DisableBlockDeviceUse bool `toml:"disable_block_device_use"`
Expand Down Expand Up @@ -202,14 +202,14 @@ func (h hypervisor) machineType() string {
func (h hypervisor) defaultVCPUs() uint32 {
numCPUs := goruntime.NumCPU()

if h.DefaultVCPUs < 0 || h.DefaultVCPUs > int32(numCPUs) {
if h.NumVCPUs < 0 || h.NumVCPUs > int32(numCPUs) {
return uint32(numCPUs)
}
if h.DefaultVCPUs == 0 { // or unspecified
if h.NumVCPUs == 0 { // or unspecified
return defaultVCPUCount
}

return uint32(h.DefaultVCPUs)
return uint32(h.NumVCPUs)
}

func (h hypervisor) defaultMaxVCPUs() uint32 {
Expand All @@ -232,11 +232,11 @@ func (h hypervisor) defaultMaxVCPUs() uint32 {
}

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

return h.DefaultMemSz
return h.MemorySize
}

func (h hypervisor) defaultBridges() uint32 {
Expand Down Expand Up @@ -360,9 +360,9 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
MachineAccelerators: machineAccelerators,
KernelParams: vc.DeserializeParams(strings.Fields(kernelParams)),
HypervisorMachineType: machineType,
DefaultVCPUs: h.defaultVCPUs(),
NumVCPUs: h.defaultVCPUs(),
DefaultMaxVCPUs: h.defaultMaxVCPUs(),
DefaultMemSz: h.defaultMemSz(),
MemorySize: h.defaultMemSz(),
DefaultBridges: h.defaultBridges(),
DisableBlockDeviceUse: h.DisableBlockDeviceUse,
MemPrealloc: h.MemPrealloc,
Expand Down Expand Up @@ -477,9 +477,9 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat
FirmwarePath: defaultFirmwarePath,
MachineAccelerators: defaultMachineAccelerators,
HypervisorMachineType: defaultMachineType,
DefaultVCPUs: defaultVCPUCount,
NumVCPUs: defaultVCPUCount,
DefaultMaxVCPUs: defaultMaxVCPUCount,
DefaultMemSz: defaultMemSize,
MemorySize: defaultMemSize,
DefaultBridges: defaultBridgesCount,
MemPrealloc: defaultEnableMemPrealloc,
HugePages: defaultEnableHugePages,
Expand Down
30 changes: 15 additions & 15 deletions cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
ImagePath: imagePath,
KernelParams: vc.DeserializeParams(strings.Fields(kernelParams)),
HypervisorMachineType: machineType,
DefaultVCPUs: defaultVCPUCount,
NumVCPUs: defaultVCPUCount,
DefaultMaxVCPUs: uint32(goruntime.NumCPU()),
DefaultMemSz: defaultMemSize,
MemorySize: defaultMemSize,
DisableBlockDeviceUse: disableBlockDevice,
BlockDeviceDriver: defaultBlockDeviceDriver,
DefaultBridges: defaultBridgesCount,
Expand Down Expand Up @@ -521,9 +521,9 @@ func TestMinimalRuntimeConfig(t *testing.T) {
ImagePath: defaultImagePath,
InitrdPath: defaultInitrdPath,
HypervisorMachineType: defaultMachineType,
DefaultVCPUs: defaultVCPUCount,
NumVCPUs: defaultVCPUCount,
DefaultMaxVCPUs: defaultMaxVCPUCount,
DefaultMemSz: defaultMemSize,
MemorySize: defaultMemSize,
DisableBlockDeviceUse: defaultDisableBlockDeviceUse,
DefaultBridges: defaultBridgesCount,
Mlock: !defaultEnableSwap,
Expand Down Expand Up @@ -790,13 +790,13 @@ func TestHypervisorDefaults(t *testing.T) {
assert.Equal(h.machineType(), machineType, "custom hypervisor machine type wrong")

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

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

h.DefaultVCPUs = int32(numCPUs) + 1
h.NumVCPUs = int32(numCPUs) + 1
assert.Equal(h.defaultVCPUs(), uint32(numCPUs), "default vCPU number is wrong")

h.DefaultMaxVCPUs = 2
Expand All @@ -809,7 +809,7 @@ func TestHypervisorDefaults(t *testing.T) {
h.DefaultMaxVCPUs = uint32(maxvcpus) + 1
assert.Equal(h.defaultMaxVCPUs(), uint32(numCPUs), "default max vCPU number is wrong")

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

Expand Down Expand Up @@ -1207,20 +1207,20 @@ func TestUpdateRuntimeConfigurationVMConfig(t *testing.T) {
tomlConf := tomlConfig{
Hypervisor: map[string]hypervisor{
qemuHypervisorTableType: {
DefaultVCPUs: int32(vcpus),
DefaultMemSz: uint32(mem),
Path: "/",
Kernel: "/",
Image: "/",
Firmware: "/",
NumVCPUs: int32(vcpus),
MemorySize: uint32(mem),
Path: "/",
Kernel: "/",
Image: "/",
Firmware: "/",
},
},
}

err := updateRuntimeConfig("", tomlConf, &config)
assert.NoError(err)

assert.Equal(expectedVMConfig, config.HypervisorConfig.DefaultMemSz)
assert.Equal(expectedVMConfig, config.HypervisorConfig.MemorySize)
}

func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,8 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
KernelPath: filepath.Join(testDir, testKernel),
ImagePath: filepath.Join(testDir, testImage),
HypervisorPath: filepath.Join(testDir, testHypervisor),
DefaultVCPUs: defaultVCPUs,
DefaultMemSz: defaultMemSzMiB,
NumVCPUs: defaultVCPUs,
MemorySize: defaultMemSzMiB,
DefaultBridges: defaultBridges,
BlockDeviceDriver: defaultBlockDriver,
DefaultMaxVCPUs: defaultMaxQemuVCPUs,
Expand Down Expand Up @@ -916,8 +916,8 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
KernelPath: filepath.Join(testDir, testKernel),
ImagePath: filepath.Join(testDir, testImage),
HypervisorPath: filepath.Join(testDir, testHypervisor),
DefaultVCPUs: defaultVCPUs,
DefaultMemSz: defaultMemSzMiB,
NumVCPUs: defaultVCPUs,
MemorySize: defaultMemSzMiB,
DefaultBridges: defaultBridges,
BlockDeviceDriver: defaultBlockDriver,
DefaultMaxVCPUs: defaultMaxQemuVCPUs,
Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/documentation/api/1.0/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ type HypervisorConfig struct {
// enable debug output where available.
Debug bool

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

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

// DefaultBridges specifies default number of bridges for the VM.
// Bridges can be used to hot plug devices
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/example_pod_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Example_createAndStartSandbox() {
KernelPath: "/usr/share/kata-containers/vmlinux.container",
ImagePath: "/usr/share/kata-containers/kata-containers.img",
HypervisorPath: "/usr/bin/qemu-system-x86_64",
DefaultMemSz: 1024,
MemorySize: 1024,
}

// Use hyperstart default values for the agent.
Expand Down
12 changes: 6 additions & 6 deletions virtcontainers/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (f *factory) log() *logrus.Entry {
}

func resetHypervisorConfig(config *vc.HypervisorConfig) {
config.DefaultVCPUs = 0
config.DefaultMemSz = 0
config.NumVCPUs = 0
config.MemorySize = 0
config.BootToBeTemplate = false
config.BootFromTemplate = false
config.MemoryPath = ""
Expand Down Expand Up @@ -175,16 +175,16 @@ func (f *factory) GetVM(ctx context.Context, config vc.VMConfig) (*vc.VM, error)

online := false
baseConfig := f.base.Config().HypervisorConfig
if baseConfig.DefaultVCPUs < hypervisorConfig.DefaultVCPUs {
err = vm.AddCPUs(hypervisorConfig.DefaultVCPUs - baseConfig.DefaultVCPUs)
if baseConfig.NumVCPUs < hypervisorConfig.NumVCPUs {
err = vm.AddCPUs(hypervisorConfig.NumVCPUs - baseConfig.NumVCPUs)
if err != nil {
return nil, err
}
online = true
}

if baseConfig.DefaultMemSz < hypervisorConfig.DefaultMemSz {
err = vm.AddMemory(hypervisorConfig.DefaultMemSz - baseConfig.DefaultMemSz)
if baseConfig.MemorySize < hypervisorConfig.MemorySize {
err = vm.AddMemory(hypervisorConfig.MemorySize - baseConfig.MemorySize)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ func TestFactoryGetVM(t *testing.T) {
assert.Nil(err)

// CPU hotplug
vmConfig.HypervisorConfig.DefaultVCPUs++
vmConfig.HypervisorConfig.NumVCPUs++
_, err = f.GetVM(ctx, vmConfig)
assert.Nil(err)

// Memory hotplug
vmConfig.HypervisorConfig.DefaultMemSz += 128
vmConfig.HypervisorConfig.MemorySize += 128
_, err = f.GetVM(ctx, vmConfig)
assert.Nil(err)

Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/factory/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (t *template) prepareTemplateFiles() error {
return err
}
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.DefaultMemSz+8)
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+8)
if err = syscall.Mount("tmpfs", t.statePath, "tmpfs", flags, opts); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/hack/virtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func buildSandboxConfig(context *cli.Context) (vc.SandboxConfig, error) {
KernelPath: kernelPath,
ImagePath: "/usr/share/clear-containers/clear-containers.img",
HypervisorMachineType: machineType,
DefaultMemSz: uint32(vmMemory),
MemorySize: uint32(vmMemory),
}

if err := buildKernelParams(&hypervisorConfig); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ type HypervisorConfig struct {
// it will be used for the sandbox's kernel path instead of KernelPath.
customAssets map[assetType]*asset

// DefaultVCPUs specifies default number of vCPUs for the VM.
DefaultVCPUs uint32
// NumVCPUs specifies default number of vCPUs for the VM.
NumVCPUs uint32

//DefaultMaxVCPUs specifies the maximum number of vCPUs for the VM.
DefaultMaxVCPUs uint32

// DefaultMem specifies default memory size in MiB for the VM.
DefaultMemSz uint32
MemorySize uint32

// DefaultBridges specifies default number of bridges for the VM.
// Bridges can be used to hot plug devices
Expand Down Expand Up @@ -273,12 +273,12 @@ func (conf *HypervisorConfig) valid() error {
return err
}

if conf.DefaultVCPUs == 0 {
conf.DefaultVCPUs = defaultVCPUs
if conf.NumVCPUs == 0 {
conf.NumVCPUs = defaultVCPUs
}

if conf.DefaultMemSz == 0 {
conf.DefaultMemSz = defaultMemSzMiB
if conf.MemorySize == 0 {
conf.MemorySize = defaultMemSzMiB
}

if conf.DefaultBridges == 0 {
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/hypervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ func TestHypervisorConfigDefaults(t *testing.T) {
KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel),
ImagePath: fmt.Sprintf("%s/%s", testDir, testImage),
HypervisorPath: "",
DefaultVCPUs: defaultVCPUs,
DefaultMemSz: defaultMemSzMiB,
NumVCPUs: defaultVCPUs,
MemorySize: defaultMemSzMiB,
DefaultBridges: defaultBridges,
BlockDeviceDriver: defaultBlockDriver,
DefaultMaxVCPUs: defaultMaxQemuVCPUs,
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func updateVMConfig(ocispec CompatOCISpec, config *RuntimeConfig) error {
}
// Use some math magic to round up to the nearest Mb.
// This has the side effect that we can never have <1Mb assigned.
config.HypervisorConfig.DefaultMemSz = uint32((memBytes + (1024*1024 - 1)) / (1024 * 1024))
config.HypervisorConfig.MemorySize = uint32((memBytes + (1024*1024 - 1)) / (1024 * 1024))
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestUpdateVmConfig(t *testing.T) {

config := RuntimeConfig{
HypervisorConfig: vc.HypervisorConfig{
DefaultMemSz: 2048,
MemorySize: 2048,
},
}

Expand All @@ -278,7 +278,7 @@ func TestUpdateVmConfig(t *testing.T) {

err := updateVMConfig(ocispec, &config)
assert.Nil(err)
assert.Equal(config.HypervisorConfig.DefaultMemSz, expectedMem)
assert.Equal(config.HypervisorConfig.MemorySize, expectedMem)

limitBytes = -128 * 1024 * 1024
ocispec.Linux.Resources.Memory.Limit = &limitBytes
Expand All @@ -297,7 +297,7 @@ func TestUpdateVmConfig(t *testing.T) {
ocispec.Linux.Resources.Memory = &specs.LinuxMemory{Limit: &limitBytes}
err = updateVMConfig(ocispec, &config)
assert.Nil(err)
assert.NotEqual(config.HypervisorConfig.DefaultMemSz, expectedMem)
assert.NotEqual(config.HypervisorConfig.MemorySize, expectedMem)
}

func testStatusToOCIStateSuccessful(t *testing.T, cStatus vc.ContainerStatus, expected specs.State) {
Expand Down
8 changes: 4 additions & 4 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (q *qemu) init(ctx context.Context, id string, hypervisorConfig *Hypervisor
}

func (q *qemu) cpuTopology() govmmQemu.SMP {
return q.arch.cpuTopology(q.config.DefaultVCPUs, q.config.DefaultMaxVCPUs)
return q.arch.cpuTopology(q.config.NumVCPUs, q.config.DefaultMaxVCPUs)
}

func (q *qemu) hostMemMB() (uint64, error) {
Expand All @@ -269,7 +269,7 @@ func (q *qemu) memoryTopology() (govmmQemu.Memory, error) {
return govmmQemu.Memory{}, err
}

memMb := uint64(q.config.DefaultMemSz)
memMb := uint64(q.config.MemorySize)

return q.arch.memoryTopology(memMb, hostMemMb), nil
}
Expand Down Expand Up @@ -1033,12 +1033,12 @@ func (q *qemu) hotplugMemory(memDev *memoryDevice, op operation) error {
}

// calculate current memory
currentMemory := int(q.config.DefaultMemSz) + q.state.HotpluggedMemory
currentMemory := int(q.config.MemorySize) + q.state.HotpluggedMemory

// Don't exceed the maximum amount of memory
if currentMemory+memDev.sizeMB > int(maxMem) {
return fmt.Errorf("Unable to hotplug %d MiB memory, the SB has %d MiB and the maximum amount is %d MiB",
memDev.sizeMB, currentMemory, q.config.DefaultMemSz)
memDev.sizeMB, currentMemory, q.config.MemorySize)
}

return q.hotplugAddMemory(memDev)
Expand Down
Loading

0 comments on commit a1537a5

Please sign in to comment.