diff --git a/src/memory.sh b/src/memory.sh index f052abb..53fc704 100644 --- a/src/memory.sh +++ b/src/memory.sh @@ -4,11 +4,12 @@ set -Eeuo pipefail msg="Checking memory..." enabled "$DEBUG" && echo "$msg" -RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') -AVAIL_MEM=$(formatBytes "$RAM_AVAIL") +app() { + echo "Virtual DSM" + return 0 +} checkConfiguredMemory() { - local wanted msg if disabled "$RAM_CHECK" || [[ "${RAM_SIZE,,}" == "max" || "${RAM_SIZE,,}" == "half" ]]; then @@ -40,7 +41,6 @@ checkConfiguredMemory() { } configureHalfMemory() { - local wanted if [[ "${RAM_SIZE,,}" != "half" ]]; then @@ -50,7 +50,7 @@ configureHalfMemory() { if (( (RAM_AVAIL / 2) > RAM_SPARE )); then wanted=$(( (RAM_AVAIL / 2) / 1048577 )) RAM_SIZE="${wanted}M" - info "Allocated $wanted MB of RAM for the virtual machine." + info "Allocated $wanted MB of RAM for $(app)." else RAM_SIZE="max" fi @@ -59,7 +59,6 @@ configureHalfMemory() { } configureMaxMemory() { - local wanted if [[ "${RAM_SIZE,,}" != "max" ]]; then @@ -83,13 +82,12 @@ configureMaxMemory() { wanted=$(( wanted / 1048577 )) RAM_SIZE="${wanted}M" - info "Allocated $wanted MB of RAM for the virtual machine." + info "Allocated $wanted MB of RAM for $(app)." return 0 } checkMinimumMemory() { - local wanted wanted=$(numfmt --from=iec "$RAM_SIZE") @@ -103,6 +101,9 @@ checkMinimumMemory() { return 0 } +getMemoryInfo +AVAIL_MEM=$(formatBytes "$RAM_AVAIL") + checkConfiguredMemory configureHalfMemory configureMaxMemory diff --git a/src/reset.sh b/src/reset.sh index 3761561..cdf6359 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -139,11 +139,67 @@ checkFilesystem() { return 0 } +finiteMemoryLimit() { + + local limit="$1" + local sentinel="4611686018427387904" + local i=0 + local left="" + local right="" + + [[ "$limit" =~ ^[0-9]+$ ]] || return 1 + + (( ${#limit} < ${#sentinel} )) && return 0 + (( ${#limit} > ${#sentinel} )) && return 1 + + for (( i=0; i<${#sentinel}; i++ )); do + left="${limit:i:1}" + right="${sentinel:i:1}" + + (( left < right )) && return 0 + (( left > right )) && return 1 + done + + return 1 +} + +getMemoryInfo() { + + local host_total="" + local host_avail="" + local limit="" + local current="" + local available="" + + host_total=$(free -b | awk '/^Mem:/ {print $2; exit}') + host_avail=$(free -b | awk '/^Mem:/ {print $7; exit}') + + RAM_TOTAL="$host_total" + RAM_AVAIL="$host_avail" + + if [ -r /sys/fs/cgroup/memory.max ] && [ -r /sys/fs/cgroup/memory.current ]; then + limit=$(< /sys/fs/cgroup/memory.max) + current=$(< /sys/fs/cgroup/memory.current) + elif [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ] && [ -r /sys/fs/cgroup/memory/memory.usage_in_bytes ]; then + limit=$(< /sys/fs/cgroup/memory/memory.limit_in_bytes) + current=$(< /sys/fs/cgroup/memory/memory.usage_in_bytes) + fi + + if finiteMemoryLimit "$limit" && [[ "$current" =~ ^[0-9]+$ ]]; then + (( limit < RAM_TOTAL )) && RAM_TOTAL="$limit" + + available=$(( limit - current )) + (( available < 0 )) && available=0 + (( available < RAM_AVAIL )) && RAM_AVAIL="$available" + fi + + return 0 +} + normalizeRamSize() { - # Read memory - RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') - RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}') + # Read host and container memory limits. + getMemoryInfo RAM_SPARE=500000000 RAM_MINIMUM=136314880