Skip to content

Instantly share code, notes, and snippets.

@frafra
Created August 23, 2024 06:37
Show Gist options
  • Save frafra/f8a95229854d9232d4077a485758b07b to your computer and use it in GitHub Desktop.
Save frafra/f8a95229854d9232d4077a485758b07b to your computer and use it in GitHub Desktop.
A prototype of task spooler which avoids OOM
#!/bin/bash
set -xeu
export max=$((8*1024*1024*1024)) # 8 GB
memory_max() {
requested=$1
total=$(
find /sys/fs/cgroup/user.slice/ -wholename "*/run-*.*/cgroup.procs" |
datamash dirname 1 |
xargs -I% cat %/memory.max |
datamash sum 1
)
if [ $((total+requested)) -gt $max ]
then
exit 1
else
exit 0
fi
}
export -f memory_max
stresser() {
size=$((RANDOM%5+1))
echo | parallel -n0 --limit "memory_max $((size*1024*1024*1024))" -- \
systemd-run --user --scope --quiet \
-p MemoryMax=$((size+1))G \
-p MemorySwapMax=0 \
--setenv=size=$size -- \
stress-ng \
--minimize \
--timeout $((RANDOM%60+1))s \
--oomable --no-oom-adjust \
--memfd 1 --memfd-bytes ${size}G
}
export -f stresser
parallel -n0 --delay 1 --bar -- stresser ::: {1..10}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment