set up CPU and Memory resources

Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
Haiyan Meng 2016-07-25 14:02:30 -04:00 committed by Mrunal Patel
parent b98900eb55
commit 839463d837
4 changed files with 74 additions and 28 deletions

View file

@ -29,12 +29,12 @@
],
"resources": {
"cpu": {
"limits": 50000000,
"requests": 20000000
"limits": 3,
"requests": 2
},
"memory": {
"limits": 500000000000,
"requests": 200000000000
"limits": 50000000,
"requests": 2000000
}
},
"labels": {

View file

@ -5,3 +5,12 @@ const (
// "The search list is currently limited to six domains with a total of 256 characters."
maxDNSSearches = 6
)
const (
// by default, cpu.cfs_period_us is set to be 1000000 (i.e., 1s).
defaultCPUCFSPeriod = 1000000
// the upper limit of cpu.cfs_quota_us is 1000000.
maxCPUCFSQuota = 1000000
// the lower limit of cpu.cfs_quota_us is 1000.
minCPUCFSQuota = 1000
)

View file

@ -87,30 +87,6 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, err
}
// TODO: the unit of cpu here is cores. How to map it into specs.Spec.Linux.Resouces.CPU?
cpu := req.GetConfig().GetResources().GetCpu()
if cpu != nil {
limits := cpu.GetLimits()
requests := cpu.GetRequests()
fmt.Println(limits)
fmt.Println(requests)
}
memory := req.GetConfig().GetResources().GetMemory()
if memory != nil {
// limits sets specs.Spec.Linux.Resouces.Memory.Limit
limits := memory.GetLimits()
if limits != 0 {
g.SetLinuxResourcesMemoryLimit(uint64(limits))
}
// requests sets specs.Spec.Linux.Resouces.Memory.Reservation
requests := memory.GetRequests()
if requests != 0 {
g.SetLinuxResourcesMemoryReservation(uint64(requests))
}
}
labels := req.GetConfig().GetLabels()
s.sandboxes = append(s.sandboxes, &sandbox{
name: name,

View file

@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"
"strings"
"github.com/opencontainers/ocitools/generate"
)
func getGPRCVersion() (string, error) {
@ -91,3 +93,62 @@ func parseDNSOptions(servers, searches []string, path string) error {
return nil
}
// kubernetes compute resources - CPU: http://kubernetes.io/docs/user-guide/compute-resources/#meaning-of-cpu
func setResourcesCPU(limits, requests, defaultCores float64, g generate.Generator) error {
if requests > limits {
return fmt.Errorf("CPU.Requests should not be greater than CPU.Limits")
}
cores := defaultCores
if limits != 0 || requests != 0 {
if limits > requests {
cores = limits
} else {
cores = requests
}
}
period := uint64(defaultCPUCFSPeriod)
quota := uint64(float64(period) * cores)
if quota < minCPUCFSQuota {
quota = minCPUCFSQuota
}
// adjust quota and period for the case where multiple CPUs are requested
// so that cpu.cfs_quota_us <= maxCPUCFSQuota.
for quota > maxCPUCFSQuota {
quota /= 10
period /= 10
}
g.SetLinuxResourcesCPUPeriod(period)
g.SetLinuxResourcesCPUQuota(quota)
return nil
}
// kubernetes compute resources - Memory: http://kubernetes.io/docs/user-guide/compute-resources/#meaning-of-memory
func setResourcesMemory(limits, requests, defaultMem float64, g generate.Generator) error {
if requests > limits {
return fmt.Errorf("Memory.Requests should not be greater than Memory.Limits")
}
if limits != 0 {
if requests == 0 {
requests = limits
}
} else {
if requests == 0 {
// set the default values of limits and requests
requests = defaultMem
limits = defaultMem
} else {
limits = requests
}
}
g.SetLinuxResourcesMemoryLimit(uint64(limits))
g.SetLinuxResourcesMemoryReservation(uint64(requests))
return nil
}