Skip to content

Commit 0712629

Browse files
Adding script for cgroup v2 memory available.sh (#41931)
* script for cgroup v2 memory available.sh * Update content/en/docs/concepts/scheduling-eviction/node-pressure-eviction.md Co-authored-by: Michael <[email protected]> * Update memory-available-cgroupv2.sh --------- Co-authored-by: Michael <[email protected]>
1 parent 5853073 commit 0712629

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

content/en/docs/concepts/scheduling-eviction/node-pressure-eviction.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ like `free -m`. This is important because `free -m` does not work in a
8686
container, and if users use the [node allocatable](/docs/tasks/administer-cluster/reserve-compute-resources/#node-allocatable)
8787
feature, out of resource decisions
8888
are made local to the end user Pod part of the cgroup hierarchy as well as the
89-
root node. This [script](/examples/admin/resource/memory-available.sh)
89+
root node. This [script](/examples/admin/resource/memory-available.sh) or
90+
[cgroupv2 script](/examples/admin/resource/memory-available-cgroupv2.sh)
9091
reproduces the same set of steps that the kubelet performs to calculate
9192
`memory.available`. The kubelet excludes inactive_file (the number of bytes of
9293
file-backed memory on the inactive LRU list) from its calculation, as it assumes that
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# This script reproduces what the kubelet does
4+
# to calculate memory.available relative to kubepods cgroup.
5+
6+
# current memory usage
7+
memory_capacity_in_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
8+
memory_capacity_in_bytes=$((memory_capacity_in_kb * 1024))
9+
memory_usage_in_bytes=$(cat /sys/fs/cgroup/kubepods.slice/memory.current)
10+
memory_total_inactive_file=$(cat /sys/fs/cgroup/kubepods.slice/memory.stat | grep inactive_file | awk '{print $2}')
11+
12+
memory_working_set=${memory_usage_in_bytes}
13+
if [ "$memory_working_set" -lt "$memory_total_inactive_file" ];
14+
then
15+
memory_working_set=0
16+
else
17+
memory_working_set=$((memory_usage_in_bytes - memory_total_inactive_file))
18+
fi
19+
20+
memory_available_in_bytes=$((memory_capacity_in_bytes - memory_working_set))
21+
memory_available_in_kb=$((memory_available_in_bytes / 1024))
22+
memory_available_in_mb=$((memory_available_in_kb / 1024))
23+
24+
echo "memory.capacity_in_bytes $memory_capacity_in_bytes"
25+
echo "memory.usage_in_bytes $memory_usage_in_bytes"
26+
echo "memory.total_inactive_file $memory_total_inactive_file"
27+
echo "memory.working_set $memory_working_set"
28+
echo "memory.available_in_bytes $memory_available_in_bytes"
29+
echo "memory.available_in_kb $memory_available_in_kb"
30+
echo "memory.available_in_mb $memory_available_in_mb"

0 commit comments

Comments
 (0)