Skip to content

Commit d49ece5

Browse files
committed
Merge pull request #790 from mlaventure/runc-update-cgroup-kmem-limit
Runc update cgroup kmem limit
2 parents 4ad7bbc + d78ae51 commit d49ece5

File tree

7 files changed

+524
-18
lines changed

7 files changed

+524
-18
lines changed

libcontainer/cgroups/fs/memory.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package fs
55
import (
66
"bufio"
77
"fmt"
8+
"math"
89
"os"
910
"path/filepath"
1011
"strconv"
1112
"strings"
1213

1314
"github.com/opencontainers/runc/libcontainer/cgroups"
1415
"github.com/opencontainers/runc/libcontainer/configs"
16+
"github.com/opencontainers/runc/libcontainer/system"
1517
)
1618

1719
type MemoryGroup struct {
@@ -33,7 +35,7 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
3335
}
3436
}
3537
// We have to set kernel memory here, as we can't change it once
36-
// processes have been attached.
38+
// processes have been attached to the cgroup.
3739
if err := s.SetKernelMemory(path, d.config); err != nil {
3840
return err
3941
}
@@ -55,9 +57,44 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
5557
}
5658

5759
func (s *MemoryGroup) SetKernelMemory(path string, cgroup *configs.Cgroup) error {
58-
// This has to be done separately because it has special constraints (it
59-
// can't be done after there are processes attached to the cgroup).
60-
if cgroup.Resources.KernelMemory > 0 {
60+
// This has to be done separately because it has special
61+
// constraints (it can only be initialized before setting up a
62+
// hierarchy or adding a task to the cgroups. However, if
63+
// sucessfully initialized, it can be updated anytime afterwards)
64+
if cgroup.Resources.KernelMemory != 0 {
65+
kmemInitialized := false
66+
// Is kmem.limit_in_bytes already set?
67+
kmemValue, err := getCgroupParamUint(path, "memory.kmem.limit_in_bytes")
68+
if err != nil {
69+
return err
70+
}
71+
switch system.GetLongBit() {
72+
case 32:
73+
kmemInitialized = uint32(kmemValue) != uint32(math.MaxUint32)
74+
case 64:
75+
kmemInitialized = kmemValue != uint64(math.MaxUint64)
76+
}
77+
78+
if !kmemInitialized {
79+
// If hierarchy is set, we can't change the limit
80+
usesHierarchy, err := getCgroupParamUint(path, "memory.use_hierarchy")
81+
if err != nil {
82+
return err
83+
}
84+
if usesHierarchy != 0 {
85+
return fmt.Errorf("cannot initialize kmem.limit_in_bytes if use_hierarchy is already set")
86+
}
87+
88+
// If there's already tasks in the cgroup, we can't change the limit either
89+
tasks, err := getCgroupParamString(path, "tasks")
90+
if err != nil {
91+
return err
92+
}
93+
if tasks != "" {
94+
return fmt.Errorf("cannot initialize kmem.limit_in_bytes after task have joined this cgroup")
95+
}
96+
}
97+
6198
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemory, 10)); err != nil {
6299
return err
63100
}
@@ -113,6 +150,10 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
113150
return err
114151
}
115152

153+
if err := s.SetKernelMemory(path, cgroup); err != nil {
154+
return err
155+
}
156+
116157
if cgroup.Resources.MemoryReservation != 0 {
117158
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
118159
return err

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,9 @@ func (m *Manager) Apply(pid int) error {
214214
newProp("BlockIOWeight", uint64(c.Resources.BlkioWeight)))
215215
}
216216

217-
// We need to set kernel memory before processes join cgroup because
218-
// kmem.limit_in_bytes can only be set when the cgroup is empty.
219-
// And swap memory limit needs to be set after memory limit, only
220-
// memory limit is handled by systemd, so it's kind of ugly here.
221-
if c.Resources.KernelMemory > 0 {
217+
// We have to set kernel memory here, as we can't change it once
218+
// processes have been attached to the cgroup.
219+
if c.Resources.KernelMemory != 0 {
222220
if err := setKernelMemory(c); err != nil {
223221
return err
224222
}
@@ -469,11 +467,5 @@ func setKernelMemory(c *configs.Cgroup) error {
469467
return err
470468
}
471469

472-
if err := os.MkdirAll(path, 0755); err != nil {
473-
return err
474-
}
475-
476-
// This doesn't get called by manager.Set, so we need to do it here.
477-
s := &fs.MemoryGroup{}
478-
return s.SetKernelMemory(path, c)
470+
return os.MkdirAll(path, 0755)
479471
}

libcontainer/system/sysconfig.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ import "C"
1010
func GetClockTicks() int {
1111
return int(C.sysconf(C._SC_CLK_TCK))
1212
}
13+
14+
func GetLongBit() int {
15+
return int(C.sysconf(C._SC_LONG_BIT))
16+
}

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
version = "0.1.1"
1919
specConfig = "config.json"
2020
usage = `Open Container Initiative runtime
21-
21+
2222
runc is a command line client for running applications packaged according to
2323
the Open Container Initiative (OCI) format and is a compliant implementation of the
2424
Open Container Initiative specification.
@@ -30,7 +30,7 @@ direct child of the process supervisor.
3030
3131
Containers are configured using bundles. A bundle for a container is a directory
3232
that includes a specification file named "` + specConfig + `" and a root filesystem.
33-
The root filesystem contains the contents of the container.
33+
The root filesystem contains the contents of the container.
3434
3535
To start a new instance of a container:
3636
@@ -99,6 +99,7 @@ func main() {
9999
specCommand,
100100
startCommand,
101101
stateCommand,
102+
updateCommand,
102103
}
103104
app.Before = func(context *cli.Context) error {
104105
if context.GlobalBool("debug") {

tests/integration/cgroups.bats

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
UPDATE_TEST_RUNC_ROOT="$BATS_TMPDIR/runc-cgroups-integration-test"
6+
7+
CGROUP_MEMORY=""
8+
9+
TEST_CGROUP_NAME="runc-cgroups-integration-test"
10+
11+
function init_cgroup_path() {
12+
base_path=$(grep "rw," /proc/self/mountinfo | grep -i -m 1 'MEMORY$' | cut -d ' ' -f 5)
13+
CGROUP_MEMORY="${base_path}/${TEST_CGROUP_NAME}"
14+
}
15+
16+
function teardown() {
17+
rm -f $BATS_TMPDIR/runc-update-integration-test.json
18+
teardown_running_container_inroot test_cgroups_kmem $UPDATE_TEST_RUNC_ROOT
19+
teardown_busybox
20+
}
21+
22+
function setup() {
23+
teardown
24+
setup_busybox
25+
26+
init_cgroup_path
27+
}
28+
29+
function check_cgroup_value() {
30+
cgroup=$1
31+
source=$2
32+
expected=$3
33+
34+
current=$(cat $cgroup/$source)
35+
echo $cgroup/$source
36+
echo "current" $current "!?" "$expected"
37+
[ "$current" -eq "$expected" ]
38+
}
39+
40+
@test "cgroups-kernel-memory-initialized" {
41+
# Add cgroup path
42+
sed -i 's/\("linux": {\)/\1\n "cgroupsPath": "runc-cgroups-integration-test",/' ${BUSYBOX_BUNDLE}/config.json
43+
44+
# Set some initial known values
45+
DATA=$(cat <<-EOF
46+
"memory": {
47+
"kernel": 16777216
48+
},
49+
EOF
50+
)
51+
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
52+
sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json
53+
54+
# start a detached busybox to work with
55+
"$RUNC" --root $UPDATE_TEST_RUNC_ROOT start -d --console /dev/pts/ptmx test_cgroups_kmem
56+
[ "$status" -eq 0 ]
57+
wait_for_container_inroot 15 1 test_cgroups_kmem $UPDATE_TEST_RUNC_ROOT
58+
59+
# update kernel memory limit
60+
"$RUNC" --root $UPDATE_TEST_RUNC_ROOT update test_cgroups_kmem --kernel-memory 50331648
61+
[ "$status" -eq 0 ]
62+
check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 50331648
63+
}
64+
65+
@test "cgroups-kernel-memory-uninitialized" {
66+
# Add cgroup path
67+
sed -i 's/\("linux": {\)/\1\n "cgroupsPath": "runc-cgroups-integration-test",/' ${BUSYBOX_BUNDLE}/config.json
68+
69+
# start a detached busybox to work with
70+
run "$RUNC" --root $UPDATE_TEST_RUNC_ROOT start -d --console /dev/pts/ptmx test_cgroups_kmem
71+
[ "$status" -eq 0 ]
72+
wait_for_container_inroot 15 1 test_cgroups_kmem $UPDATE_TEST_RUNC_ROOT
73+
74+
# update kernel memory limit
75+
run "$RUNC" --root $UPDATE_TEST_RUNC_ROOT update test_cgroups_kmem --kernel-memory 50331648
76+
[ ! "$status" -eq 0 ]
77+
}

0 commit comments

Comments
 (0)