Merge pull request #117 from rjnagal/cpumask

Handle cpumask in raw driver for unified hierarchy.
This commit is contained in:
Victor Marmol 2014-07-24 11:36:01 -07:00
commit 5812f10db0
2 changed files with 36 additions and 13 deletions

View File

@ -92,4 +92,5 @@ var supportedSubsystems map[string]struct{} = map[string]struct{}{
"cpu": {}, "cpu": {},
"cpuacct": {}, "cpuacct": {},
"memory": {}, "memory": {},
"cpuset": {},
} }

View File

@ -59,23 +59,32 @@ func (self *rawContainerHandler) ContainerReference() (info.ContainerReference,
}, nil }, nil
} }
func readInt64(path string, file string) uint64 { func readString(path string, file string) string {
cgroupFile := filepath.Join(path, file) cgroupFile := filepath.Join(path, file)
// Ignore non-existent files // Ignore non-existent files
if !utils.FileExists(cgroupFile) { if !utils.FileExists(cgroupFile) {
return 0 return ""
} }
// Read // Read
out, err := ioutil.ReadFile(cgroupFile) out, err := ioutil.ReadFile(cgroupFile)
if err != nil { if err != nil {
log.Printf("raw driver: Failed to read %q: %s", cgroupFile, err) log.Printf("raw driver: Failed to read %q: %s", cgroupFile, err)
return ""
}
return string(out)
}
func readInt64(path string, file string) uint64 {
out := readString(path, file)
if out == "" {
return 0 return 0
} }
val, err := strconv.ParseUint(strings.TrimSpace(string(out)), 10, 64)
val, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
if err != nil { if err != nil {
log.Printf("raw driver: Failed to parse in %q from file %q: %s", string(out), cgroupFile, err) log.Printf("raw driver: Failed to parse in %q from file %q: %s", out, filepath.Join(path, file), err)
return 0 return 0
} }
@ -87,24 +96,37 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
// The raw driver assumes unified hierarchy containers. // The raw driver assumes unified hierarchy containers.
// CPU.
cpuRoot, ok := self.cgroupSubsystems.mountPoints["cpu"]
if ok {
cpuRoot = filepath.Join(cpuRoot, self.name)
if utils.FileExists(cpuRoot) {
// Get machine info. // Get machine info.
mi, err := self.machineInfoFactory.GetMachineInfo() mi, err := self.machineInfoFactory.GetMachineInfo()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// CPU.
cpuRoot, ok := self.cgroupSubsystems.mountPoints["cpu"]
if ok {
cpuRoot = filepath.Join(cpuRoot, self.name)
if utils.FileExists(cpuRoot) {
spec.Cpu = new(info.CpuSpec) spec.Cpu = new(info.CpuSpec)
spec.Cpu.Limit = readInt64(cpuRoot, "cpu.shares") spec.Cpu.Limit = readInt64(cpuRoot, "cpu.shares")
}
}
// TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus // Cpu Mask.
// This will fail for non-unified hierarchies. We'll return the whole machine mask in that case.
cpusetRoot, ok := self.cgroupSubsystems.mountPoints["cpuset"]
if ok {
if spec.Cpu == nil {
spec.Cpu = new(info.CpuSpec)
}
cpusetRoot = filepath.Join(cpusetRoot, self.name)
if utils.FileExists(cpusetRoot) {
spec.Cpu.Mask = readString(cpusetRoot, "cpuset.cpus")
if spec.Cpu.Mask == "" {
spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1) spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1)
} }
} }
}
// Memory. // Memory.
memoryRoot, ok := self.cgroupSubsystems.mountPoints["memory"] memoryRoot, ok := self.cgroupSubsystems.mountPoints["memory"]
@ -113,7 +135,7 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
if utils.FileExists(memoryRoot) { if utils.FileExists(memoryRoot) {
spec.Memory = new(info.MemorySpec) spec.Memory = new(info.MemorySpec)
spec.Memory.Limit = readInt64(memoryRoot, "memory.limit_in_bytes") spec.Memory.Limit = readInt64(memoryRoot, "memory.limit_in_bytes")
spec.Memory.SwapLimit = readInt64(memoryRoot, "memory.limit_in_bytes") spec.Memory.SwapLimit = readInt64(memoryRoot, "memory.memsw.limit_in_bytes")
} }
} }