Last active
June 4, 2021 16:07
-
-
Save iavael/f64f392d61d452f247c87b90f5b4be1e to your computer and use it in GitHub Desktop.
Swap recommendataions (experimental)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ -z "${1}" ]; then | |
echo "no cgroup in arg" >&2 | |
exit 10 | |
fi | |
declare MEM_MAX="$(cat ${1}/memory.limit_in_bytes)" | |
declare MEM_USD=$(cat ${1}/memory.usage_in_bytes) | |
declare MSW_USD="$(cat ${1}/memory.memsw.usage_in_bytes)" | |
if [ "${MEM_MAX}" -eq 9223372036854771712 ]; then | |
# echo "cgroup is not limited" >&2 | |
# exit 10 | |
MEM_MAX="$(awk '$1 == "MemTotal:" { print $2*1024 }' /proc/meminfo)" | |
fi | |
awk -v msw_u="${MSW_USD}" -v mem_u="${MEM_USD}" -v total="${MEM_MAX}" 'BEGIN { | |
in_a=0 | |
in_t=0 | |
used=0 | |
} | |
$1 == "total_innactive_anon" { in_a=$2; used+=$2; in_t+=$2 } | |
$1 == "total_inactive_file" { used+=$2; in_t+=$2 } | |
$1 == "total_active_anon" || $1 == "total_active_file" { used+=$2 } | |
END { | |
printf "Swapsize\t%d\n", ((msw_u-mem_u)+(in_a/used)*total)/1000^2 | |
printf "Swappiness\t%d\n", 200*in_a/in_t | |
}' "${1}/memory.stat" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ -z "${1}" ]; then | |
echo "no cgroup in arg" >&2 | |
exit 10 | |
fi | |
declare MEM_MAX="$(cat ${1}/memory.max)" | |
declare SWP_USD="$(cat ${1}/memory.swap.current)" | |
if [ "${MEM_MAX}" = "max" ]; then | |
# echo "cgroup is not limited" >&2 | |
# exit 10 | |
MEM_MAX="$(awk '$1 == "MemTotal:" { print $2*1024 }' /proc/meminfo)" | |
fi | |
awk -v sw_u="${SWP_USD}" -v total="${MEM_MAX}" 'BEGIN { | |
in_a=0 | |
in_t=0 | |
used=0 | |
} | |
$1 == "inactive_anon" { in_a=$2; used+=$2; in_t+=$2 } | |
$1 == "inactive_file" { used+=$2; in_t+=$2 } | |
$1 == "active_anon" || $1 == "active_file" { used+=$2 } | |
END { | |
printf "Swapsize\t%d\n", (sw_u+(in_a/used)*total)/1000^2 | |
printf "Swappiness\t%d\n", 200*in_a/in_t | |
}' "${1}/memory.stat" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
awk 'BEGIN { | |
in_a=0 | |
in_t=0 | |
used=0 | |
total=0 | |
sw_u=0 | |
} | |
$1 == "Inactive(anon):" { in_a=$2 } | |
$1 == "SwapTotal:" { sw_u+=$2 } | |
$1 == "SwapFree:" { sw_u-=$2 } | |
$1 == "MemTotal:" { total=$2 } | |
$1 == "Active:" { used+=$2 } | |
$1 == "Inactive:" { used+=$2; in_t=$2 } | |
END { | |
printf "Swapsize\t%d\n", (sw_u+(in_a/used)*total)/1000 | |
printf "Swappiness\t%d\n", 200*in_a/in_t | |
}' /proc/meminfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment