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 #344 from wcwxyz/per-container-cpu
Browse files Browse the repository at this point in the history
pkg: oci: support OCI spec cpu quota/period
  • Loading branch information
Sebastien Boeuf authored Aug 17, 2017
2 parents 710c4bb + f82fc60 commit 71b5c95
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,14 @@ func (spec *CompatOCISpec) PodID() (string, error) {
return "", fmt.Errorf("Could not find pod ID")
}

// TODO: calculate number of VCPUs from OCI spec
func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error) {
resources := config.VMConfig

if ocispec.Linux == nil ||
ocispec.Linux.Resources == nil ||
ocispec.Linux.Resources.Memory == nil ||
ocispec.Linux.Resources.Memory.Limit == nil {
return config.VMConfig, nil
return resources, nil
}

memBytes := *ocispec.Linux.Resources.Memory.Limit
Expand All @@ -302,12 +303,29 @@ func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error)
}

// round up memory to 1MB
mem := uint((memBytes + (1024*1024 - 1)) / (1024 * 1024))
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 {
return resources, 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)
}

// round up to 1 CPU
resources.VCPUs = uint((uint64(quota) + (period - 1)) / period)

return vc.Resources{
VCPUs: config.VMConfig.VCPUs,
Memory: mem,
}, nil
return resources, nil
}

// PodConfig converts an OCI compatible runtime configuration file
Expand Down
25 changes: 25 additions & 0 deletions pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ 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 @@ -172,6 +174,7 @@ func TestVmConfig(t *testing.T) {

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

ocispec := CompatOCISpec{
Expand All @@ -181,6 +184,10 @@ func TestVmConfig(t *testing.T) {
Memory: &specs.LinuxMemory{
Limit: &limitBytes,
},
CPU: &specs.LinuxCPU{
Quota: &quota,
Period: &period,
},
},
},
},
Expand All @@ -202,6 +209,24 @@ func TestVmConfig(t *testing.T) {
if err == nil {
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)
}
}

func testStatusToOCIStateSuccessful(t *testing.T, cStatus vc.ContainerStatus, expected specs.State) {
Expand Down

0 comments on commit 71b5c95

Please sign in to comment.