Allow the setting of cgroup limits using env variables; Allocate CPU resources via shares not cores

This commit is contained in:
Peter Wilmott 2015-08-03 18:22:07 +00:00
parent df8c4065c7
commit 74c2b197c7
2 changed files with 31 additions and 3 deletions

View File

@ -79,6 +79,34 @@ Created: img_42150
$ bocker run img_42150 which wget
/usr/bin/wget
$ bocker run img_42150 cat /proc/1/cgroup
...
4:memory:/ps_42152
3:cpuacct,cpu:/ps_42152
...
$ cat /sys/fs/cgroup/cpu/ps_42152/cpu.shares
512
$ cat cat /sys/fs/cgroup/memory/ps_42152/memory.limit_in_bytes
512000000
$ BOCKER_CPU_SHARE=1024 \
BOCKER_MEM_LIMIT=1024 \
bocker run img_42150 cat /proc/1/cgroup
...
4:memory:/ps_42188
3:cpuacct,cpu:/ps_42188
$ cat /sys/fs/cgroup/cpu/ps_42188/cpu.shares
1024
$ cat cat /sys/fs/cgroup/memory/ps_42188/memory.limit_in_bytes
1024000000
...
```
## Functionality: Currently Implemented

6
bocker
View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail; shopt -s nullglob
btrfs_path='/var/bocker'; cgroups='cpu,cpuacct,cpuset,memory';
btrfs_path='/var/bocker' && cgroups='cpu,cpuacct,memory';
function bocker_check() {
btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
@ -76,8 +76,8 @@ function bocker_run() { #HELP Create a container:\nBOCKER run <image_id> <comman
echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd"
cgcreate -g "$cgroups:/$uuid"
cgset -r cpuset.cpus=0-1 "$uuid" && cgset -r cpuset.mems=0 "$uuid"
cgset -r memory.limit_in_bytes=512000000 "$uuid"
: ${BOCKER_CPU_SHARE:=512} && cgset -r cpu.shares="$BOCKER_CPU_SHARE" "$uuid"
: ${BOCKER_MEM_LIMIT:=512} && cgset -r memory.limit_in_bytes="$((BOCKER_MEM_LIMIT * 1000000))" "$uuid"
cgexec -g "$cgroups:$uuid" \
ip netns exec netns_"$uuid" \
unshare -fmuip --mount-proc \