Skip to content

Commit 5812f10

Browse files
committed
Merge pull request #117 from rjnagal/cpumask
Handle cpumask in raw driver for unified hierarchy.
2 parents 93b5e2b + f5fde11 commit 5812f10

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

container/raw/factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ var supportedSubsystems map[string]struct{} = map[string]struct{}{
9292
"cpu": {},
9393
"cpuacct": {},
9494
"memory": {},
95+
"cpuset": {},
9596
}

container/raw/handler.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,32 @@ func (self *rawContainerHandler) ContainerReference() (info.ContainerReference,
5959
}, nil
6060
}
6161

62-
func readInt64(path string, file string) uint64 {
62+
func readString(path string, file string) string {
6363
cgroupFile := filepath.Join(path, file)
6464

6565
// Ignore non-existent files
6666
if !utils.FileExists(cgroupFile) {
67-
return 0
67+
return ""
6868
}
6969

7070
// Read
7171
out, err := ioutil.ReadFile(cgroupFile)
7272
if err != nil {
7373
log.Printf("raw driver: Failed to read %q: %s", cgroupFile, err)
74+
return ""
75+
}
76+
return string(out)
77+
}
78+
79+
func readInt64(path string, file string) uint64 {
80+
out := readString(path, file)
81+
if out == "" {
7482
return 0
7583
}
76-
val, err := strconv.ParseUint(strings.TrimSpace(string(out)), 10, 64)
84+
85+
val, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
7786
if err != nil {
78-
log.Printf("raw driver: Failed to parse in %q from file %q: %s", string(out), cgroupFile, err)
87+
log.Printf("raw driver: Failed to parse in %q from file %q: %s", out, filepath.Join(path, file), err)
7988
return 0
8089
}
8190

@@ -87,22 +96,35 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
8796

8897
// The raw driver assumes unified hierarchy containers.
8998

99+
// Get machine info.
100+
mi, err := self.machineInfoFactory.GetMachineInfo()
101+
if err != nil {
102+
return nil, err
103+
}
104+
90105
// CPU.
91106
cpuRoot, ok := self.cgroupSubsystems.mountPoints["cpu"]
92107
if ok {
93108
cpuRoot = filepath.Join(cpuRoot, self.name)
94109
if utils.FileExists(cpuRoot) {
95-
// Get machine info.
96-
mi, err := self.machineInfoFactory.GetMachineInfo()
97-
if err != nil {
98-
return nil, err
99-
}
100-
101110
spec.Cpu = new(info.CpuSpec)
102111
spec.Cpu.Limit = readInt64(cpuRoot, "cpu.shares")
112+
}
113+
}
103114

104-
// TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus
105-
spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1)
115+
// Cpu Mask.
116+
// This will fail for non-unified hierarchies. We'll return the whole machine mask in that case.
117+
cpusetRoot, ok := self.cgroupSubsystems.mountPoints["cpuset"]
118+
if ok {
119+
if spec.Cpu == nil {
120+
spec.Cpu = new(info.CpuSpec)
121+
}
122+
cpusetRoot = filepath.Join(cpusetRoot, self.name)
123+
if utils.FileExists(cpusetRoot) {
124+
spec.Cpu.Mask = readString(cpusetRoot, "cpuset.cpus")
125+
if spec.Cpu.Mask == "" {
126+
spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1)
127+
}
106128
}
107129
}
108130

@@ -113,7 +135,7 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
113135
if utils.FileExists(memoryRoot) {
114136
spec.Memory = new(info.MemorySpec)
115137
spec.Memory.Limit = readInt64(memoryRoot, "memory.limit_in_bytes")
116-
spec.Memory.SwapLimit = readInt64(memoryRoot, "memory.limit_in_bytes")
138+
spec.Memory.SwapLimit = readInt64(memoryRoot, "memory.memsw.limit_in_bytes")
117139
}
118140
}
119141

0 commit comments

Comments
 (0)