Skip to content

Commit

Permalink
demo: support cgroup2
Browse files Browse the repository at this point in the history
This commit adds cgroup2 support in get_available_ram() function
defined in common_functions.sh

Signed-off-by: Alexandre DUVAL - @KannarFr <[email protected]>
  • Loading branch information
KannarFr authored and guits committed Aug 3, 2023
1 parent b86898a commit ed5d769
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/daemon/common_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -519,16 +519,28 @@ function ami_privileged {

# Detect how much ram is available
function get_available_ram {
limit_in_bytes="/sys/fs/cgroup/memory/memory.limit_in_bytes"
memory_limit=$(cat $limit_in_bytes)
memory_limit_in_bytes_file=""
memory_current_usage_in_bytes_file=""

if grep -q cgroup2 /proc/mounts; then
# cgroups v2
memory_limit_in_bytes_file="/sys/fs/cgroup/memory.max"
memory_current_usage_in_bytes_file="/sys/fs/cgroup/memory.current"
else
# cgroups V1
memory_limit_in_bytes_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
memory_current_usage_in_bytes_file="/sys/fs/cgroup/memory/memory.usage_in_bytes"
fi

memory_limit=$(cat $memory_limit_in_bytes_file)
# 8 ExaBytes is the value of an unbounded device
if [ "${memory_limit}" = "9223372036854771712" ]; then
if [ "${memory_limit}" = "9223372036854771712" ] || [ "${memory_limit}" = "max" ]; then
# Looks like we are not in a container
# Let's report the MemAvailable on this system
echo $(( $(awk '/MemAvailable/{print $2}' /proc/meminfo) * 1024))
else
current_usage=$(cat /sys/fs/cgroup/memory/memory.usage_in_bytes)
echo $(( memory_limit - current_usage ))
memory_current_usage=$(cat $memory_current_usage_in_bytes_file)
echo $(( memory_limit - memory_current_usage ))
fi
}

Expand Down

0 comments on commit ed5d769

Please sign in to comment.