Skip to content

Commit 4e65e0e

Browse files
author
Shishir Mahajan
committed
Throw an error if cgroup tries to set cpu-shares more/less than the maximum/minimum permissible value.
Signed-off-by: Shishir Mahajan <[email protected]>
1 parent c851275 commit 4e65e0e

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

cgroups/fs/apply_raw.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package fs
22

33
import (
4+
"fmt"
5+
"io"
46
"io/ioutil"
57
"os"
8+
"path"
69
"path/filepath"
710
"strconv"
811
"sync"
@@ -75,10 +78,13 @@ type data struct {
7578
}
7679

7780
func (m *Manager) Apply(pid int) error {
81+
7882
if m.Cgroups == nil {
7983
return nil
8084
}
8185

86+
var c = m.Cgroups
87+
8288
d, err := getCgroupData(m.Cgroups, pid)
8389
if err != nil {
8490
return err
@@ -108,6 +114,28 @@ func (m *Manager) Apply(pid int) error {
108114
}
109115
m.Paths = paths
110116

117+
var cpuShares int64
118+
119+
fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares"))
120+
if err != nil {
121+
return err
122+
}
123+
124+
_, err = fmt.Fscanf(fd, "%d", &cpuShares)
125+
if err != nil && err != io.EOF {
126+
return err
127+
}
128+
129+
fd.Close()
130+
131+
if c.CpuShares != 0 {
132+
if c.CpuShares > cpuShares {
133+
return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares)
134+
} else if c.CpuShares < cpuShares {
135+
return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares)
136+
}
137+
}
138+
111139
return nil
112140
}
113141

cgroups/systemd/apply_systemd.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package systemd
55
import (
66
"bytes"
77
"fmt"
8+
"io"
89
"io/ioutil"
910
"os"
11+
"path"
1012
"path/filepath"
1113
"strconv"
1214
"strings"
@@ -241,6 +243,28 @@ func (m *Manager) Apply(pid int) error {
241243

242244
m.Paths = paths
243245

246+
var cpuShares int64
247+
248+
fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares"))
249+
if err != nil {
250+
return err
251+
}
252+
253+
_, err = fmt.Fscanf(fd, "%d", &cpuShares)
254+
if err != nil && err != io.EOF {
255+
return err
256+
}
257+
258+
fd.Close()
259+
260+
if c.CpuShares != 0 {
261+
if c.CpuShares > cpuShares {
262+
return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares)
263+
} else if c.CpuShares < cpuShares {
264+
return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares)
265+
}
266+
}
267+
244268
return nil
245269
}
246270

0 commit comments

Comments
 (0)