Skip to content

Instantly share code, notes, and snippets.

@iavael
Last active Jun 4, 2021
Embed
What would you like to do?
Swap recommendataions (experimental)
#!/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"
#!/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"
#!/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