From db2cb649cb543835c46b457cca1a2938b82976ea Mon Sep 17 00:00:00 2001 From: "xin.li" Date: Thu, 7 Dec 2023 00:10:20 +0800 Subject: [PATCH] [zh-cn] sync memory-available-cgroupv2.sh Signed-off-by: xin.li --- .../resource/memory-available-cgroupv2.sh | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 content/zh-cn/examples/admin/resource/memory-available-cgroupv2.sh diff --git a/content/zh-cn/examples/admin/resource/memory-available-cgroupv2.sh b/content/zh-cn/examples/admin/resource/memory-available-cgroupv2.sh new file mode 100644 index 0000000000..3cbdd09e47 --- /dev/null +++ b/content/zh-cn/examples/admin/resource/memory-available-cgroupv2.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# 此脚本重现了 kubelet 相对于 kubepods cgroup 计算 memory.available 的操作 + +# 当前内存使用情况 +memory_capacity_in_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}') +memory_capacity_in_bytes=$((memory_capacity_in_kb * 1024)) +memory_usage_in_bytes=$(cat /sys/fs/cgroup/kubepods.slice/memory.current) +memory_total_inactive_file=$(cat /sys/fs/cgroup/kubepods.slice/memory.stat | grep inactive_file | awk '{print $2}') + +memory_working_set=${memory_usage_in_bytes} +if [ "$memory_working_set" -lt "$memory_total_inactive_file" ]; +then + memory_working_set=0 +else + memory_working_set=$((memory_usage_in_bytes - memory_total_inactive_file)) +fi + +memory_available_in_bytes=$((memory_capacity_in_bytes - memory_working_set)) +memory_available_in_kb=$((memory_available_in_bytes / 1024)) +memory_available_in_mb=$((memory_available_in_kb / 1024)) + +echo "memory.capacity_in_bytes $memory_capacity_in_bytes" +echo "memory.usage_in_bytes $memory_usage_in_bytes" +echo "memory.total_inactive_file $memory_total_inactive_file" +echo "memory.working_set $memory_working_set" +echo "memory.available_in_bytes $memory_available_in_bytes" +echo "memory.available_in_kb $memory_available_in_kb" +echo "memory.available_in_mb $memory_available_in_mb"