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

Commit

Permalink
pkg: oci: support OCI spec cpu quota/period
Browse files Browse the repository at this point in the history
We should run VM(container) regarding OCI spec config.json cpu quota and
period.

The number of VM VCPUs is defined by quota/period (round up to 1).

Signed-off-by: WANG Chao <chao.wang@ucloud.cn>
  • Loading branch information
wcwxyz committed Aug 17, 2017
1 parent 710c4bb commit 8807c49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
28 changes: 21 additions & 7 deletions pkg/oci/utils.go
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,25 @@ 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)
}

// 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
16 changes: 16 additions & 0 deletions pkg/oci/utils_test.go
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,15 @@ 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)
}
}

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

0 comments on commit 8807c49

Please sign in to comment.