-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwait_for_memory.sh
More file actions
executable file
·43 lines (40 loc) · 1.24 KB
/
wait_for_memory.sh
File metadata and controls
executable file
·43 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/zsh
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright © 2025–2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
# Matthias Kretz <m.kretz@gsi.de>
#
[[ -z "$1" ]] && exit 0 # single job => unconditionally start
max_jobs=$1
if grep -q :memory: /proc/$$/cgroup; then
cgroup=$(grep :memory: /proc/$$/cgroup | cut -d: -f3)
sysfs=/sys/fs/cgroup/memory${cgroup%/*}
limit=$(<$sysfs/memory.limit_in_bytes)
buffer=$((limit >> 33))
usage=$sysfs/memory.usage_in_bytes
set_avail() {
avail=$(((limit - $(<$usage)) >> 30))
}
else
grep MemTotal /proc/meminfo|read -A foo
buffer=$((${foo[2]} >> 23))
set_avail() {
grep MemAvail /proc/meminfo|read -A foo
grep SwapFree /proc/meminfo|read -A swap
avail=$(((${foo[2]} - ${swap[2]}) >> 20))
}
fi
((buffer < 4)) && buffer=4
while true; do
running=$(ps h -C cc1plus|wc -l)
set_avail
atleast=$(((3 * (max_jobs - running)) >> 1))
((buffer > atleast)) && atleast=$buffer
if ((avail >= atleast || 4 * running < max_jobs)); then
#echo "Good to go. Avail: $avail GiB (at least $atleast GiB, buffer: $buffer)"
exit 0
else
echo "Waiting for memory (at least $atleast GiB). Avail: $avail GiB"
sleep $((10 + ($max_jobs >> 4)))
fi
done