vile / tests / resource_limits / linux_memory.sh
29 lines · 27 sloc · 1.07 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#!/bin/sh
2# Run with a Vile executable and a delegated cgroup v2 directory whose
3# subtree_control includes memory. Only the new child group receives limits.
4set -eu
5if [ "$#" -ne 2 ] || [ ! -x "$1" ] || [ ! -f "$2/cgroup.controllers" ]; then
6 echo "usage: $0 /path/to/vile /path/to/delegated/cgroup" >&2
7 exit 2
8fi
9group=$(mktemp -d "$2/vile-memory.XXXXXX")
10trap 'rmdir "$group"' EXIT
11if [ ! -f "$group/memory.max" ]; then
12 echo "the delegated cgroup requires the memory controller" >&2
13 exit 2
14fi
15echo 50331648 > "$group/memory.max"
16if [ -f "$group/memory.swap.max" ]; then echo 0 > "$group/memory.swap.max"; fi
17
18# The worker joins before exec. The shell driving the checks stays outside.
19run() {
20 sh -ec 'echo 0 > "$1/cgroup.procs"; exec "$2" -s -e "$3"' sh "$group" "$1" "$2"
21}
22
23[ "$(run "$1" '(+ 20 22)')" = 42 ]
24status=0
25run "$1" '(bytevector-u8-ref (make-bytevector 100663296 17) 0)' || status=$?
26[ "$status" -eq 137 ]
27awk '$1 == "oom_kill" && $2 > 0 { killed = 1 } END { exit !killed }' "$group/memory.events"
28[ "$(run "$1" '(+ 20 22)')" = 42 ]
29cat "$group/memory.events"
30