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

Commit

Permalink
Merge pull request #662 from devimc/cpu/honor_default_vcpus
Browse files Browse the repository at this point in the history
qemu: honor DefaultVCPUs
  • Loading branch information
Samuel Ortiz authored Mar 7, 2018
2 parents 1dce29d + c3ad9c2 commit 4fb2dac
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 72 deletions.
1 change: 0 additions & 1 deletion example_pod_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func Example_createAndStartPod() {

// VM resources
vmConfig := vc.Resources{
VCPUs: 4,
Memory: 1024,
}

Expand Down
2 changes: 0 additions & 2 deletions hack/virtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) {
proxyPath := context.String("proxy-path")
shimPath := context.String("shim-path")
machineType := context.String("machine-type")
vmVCPUs := context.Uint("vm-vcpus")
vmMemory := context.Uint("vm-memory")
agentType, ok := context.Generic("agent").(*vc.AgentType)
if ok != true {
Expand Down Expand Up @@ -208,7 +207,6 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) {
shimConfig := getShimConfig(*shimType, shimPath)

vmConfig := vc.Resources{
VCPUs: vmVCPUs,
Memory: vmMemory,
}

Expand Down
1 change: 0 additions & 1 deletion hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ type HypervisorConfig struct {
Debug bool

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

//DefaultMaxVCPUs specifies the maximum number of vCPUs for the VM.
Expand Down
17 changes: 0 additions & 17 deletions pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,23 +440,6 @@ func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error)
resources.Memory = uint((memBytes + (1024*1024 - 1)) / (1024 * 1024))
}

if ocispec.Linux.Resources.CPU != nil &&
ocispec.Linux.Resources.CPU.Quota != nil &&
ocispec.Linux.Resources.CPU.Period != nil {
quota := *ocispec.Linux.Resources.CPU.Quota
period := *ocispec.Linux.Resources.CPU.Period

if quota <= 0 {
return vc.Resources{}, fmt.Errorf("Invalid OCI cpu quota %d", quota)
}

if period == 0 {
return vc.Resources{}, fmt.Errorf("Invalid OCI cpu period %d", period)
}

resources.VCPUs = vc.ConstraintsToVCPUs(quota, period)
}

return resources, nil
}

Expand Down
31 changes: 0 additions & 31 deletions pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ func TestMinimalPodConfig(t *testing.T) {

func TestVmConfig(t *testing.T) {
var limitBytes int64 = 128 * 1024 * 1024
var quota int64 = 200000
var period uint64 = 100000

config := RuntimeConfig{
VMConfig: vc.Resources{
Expand All @@ -224,7 +222,6 @@ func TestVmConfig(t *testing.T) {

expectedResources := vc.Resources{
Memory: 128,
VCPUs: 2,
}

ocispec := CompatOCISpec{
Expand All @@ -234,10 +231,6 @@ func TestVmConfig(t *testing.T) {
Memory: &specs.LinuxMemory{
Limit: &limitBytes,
},
CPU: &specs.LinuxCPU{
Quota: &quota,
Period: &period,
},
},
},
},
Expand All @@ -260,32 +253,9 @@ func TestVmConfig(t *testing.T) {
t.Fatalf("Got %v\n expecting error", resources)
}

limitBytes = 128 * 1024 * 1024
quota = -1
ocispec.Linux.Resources.CPU.Quota = &quota

resources, err = vmConfig(ocispec, config)
if err == nil {
t.Fatalf("Got %v\n expecting error", resources)
}

quota = 100000
period = 0
ocispec.Linux.Resources.CPU.Quota = &quota

resources, err = vmConfig(ocispec, config)
if err == nil {
t.Fatalf("Got %v\n expecting error", resources)
}

// Test case when Memory is nil
ocispec.Spec.Linux.Resources.Memory = nil
expectedResources.Memory = config.VMConfig.Memory
quota = 50000
period = 20000
ocispec.Linux.Resources.CPU.Quota = &quota
ocispec.Linux.Resources.CPU.Period = &period
expectedResources.VCPUs = 3
resources, err = vmConfig(ocispec, config)
if err != nil {
t.Fatal(err)
Expand All @@ -297,7 +267,6 @@ func TestVmConfig(t *testing.T) {

// Test case when CPU is nil
ocispec.Spec.Linux.Resources.CPU = nil
expectedResources.VCPUs = config.VMConfig.VCPUs
limitBytes = 20
ocispec.Linux.Resources.Memory = &specs.LinuxMemory{Limit: &limitBytes}
expectedResources.Memory = 1
Expand Down
3 changes: 0 additions & 3 deletions pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ type Cmd struct {

// Resources describes VM resources configuration.
type Resources struct {
// VCPUs is the number of available virtual CPUs.
VCPUs uint

// Memory is the amount of available memory in MiB.
Memory uint
}
Expand Down
11 changes: 3 additions & 8 deletions qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,8 @@ func (q *qemu) init(pod *Pod) error {
return nil
}

func (q *qemu) cpuTopology(podConfig PodConfig) govmmQemu.SMP {
vcpus := q.config.DefaultVCPUs
if podConfig.VMConfig.VCPUs > 0 {
vcpus = uint32(podConfig.VMConfig.VCPUs)
}

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

func (q *qemu) memoryTopology(podConfig PodConfig) (govmmQemu.Memory, error) {
Expand Down Expand Up @@ -266,7 +261,7 @@ func (q *qemu) createPod(podConfig PodConfig) error {
machine.Options += accelerators
}

smp := q.cpuTopology(podConfig)
smp := q.cpuTopology()

memory, err := q.memoryTopology(podConfig)
if err != nil {
Expand Down
13 changes: 4 additions & 9 deletions qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func TestQemuCPUTopology(t *testing.T) {

q := &qemu{
arch: &qemuArchBase{},
config: HypervisorConfig{
DefaultVCPUs: uint32(vcpus),
},
}

expectedOut := govmmQemu.SMP{
Expand All @@ -146,15 +149,7 @@ func TestQemuCPUTopology(t *testing.T) {
MaxCPUs: defaultMaxQemuVCPUs,
}

vmConfig := Resources{
VCPUs: uint(vcpus),
}

podConfig := PodConfig{
VMConfig: vmConfig,
}

smp := q.cpuTopology(podConfig)
smp := q.cpuTopology()

if reflect.DeepEqual(smp, expectedOut) == false {
t.Fatalf("Got %v\nExpecting %v", smp, expectedOut)
Expand Down

0 comments on commit 4fb2dac

Please sign in to comment.