1
0
mirror of https://github.com/artizirk/dotfiles.git synced 2026-04-17 03:34:45 +03:00

Add zswapstats script

This commit is contained in:
2026-04-16 15:52:26 +03:00
parent efa8607afe
commit d247c250c3

86
.bin/zswapstats Executable file
View File

@@ -0,0 +1,86 @@
#!/bin/bash
# Source: https://wiki.tnonline.net/w/Blog/Zswap_statistics
# System page size
page_size=$(getconf PAGESIZE)
# Location of zswap settings
settings_dir="/sys/module/zswap/parameters"
# Location of zswap statistics
statistics_dir="/sys/kernel/debug/zswap"
# Array of zswap settings
settings=("accept_threshold_percent" "compressor" "enabled" "max_pool_percent")
# Array of zswap statistics
statistics=("pool_limit_hit" "pool_total_size" "reject_alloc_fail" "reject_compress_poor" "reject_kmemcache_fail" "reject_reclaim_fail" "stored_pages")
# Declare an associative array to store zswap data
declare -A zswap_data
# Read zswap settings
for setting in "${settings[@]}"; do
read -r value < "$settings_dir/$setting"
zswap_data["$setting"]=$value
done
# Read zswap statistics
for stat in "${statistics[@]}"; do
read -r value < "$statistics_dir/$stat"
zswap_data["$stat"]=$value
done
# Determine the maximum length of keys (setting/statistic names)
max_length=0
for key in "${!zswap_data[@]}"; do
if [ ${#key} -gt $max_length ]; then
max_length=${#key}
fi
done
((width = max_length + 4))
# Calculate the total size and compressed size in MiB
total_size=$((zswap_data["stored_pages"] * page_size / (1024 * 1024) ))
compressed_size=$((zswap_data["pool_total_size"] / (1024 * 1024) ))
# Calculate the compression ratio
if [ "${zswap_data["stored_pages"]}" -ne 0 ]; then
compression_ratio=$(bc <<< "scale=2; (${zswap_data["stored_pages"]} * $page_size / ${zswap_data["pool_total_size"]})")
else
compression_ratio=0
fi
# Output the zswap settings
printf "========\n"
printf "SETTINGS"
printf "\n========\n"
for key in "${settings[@]}"; do
# Get the value from the associative array
value=${zswap_data["$key"]}
# Output the key (name) and value in columns
printf "%-*s%s\n" "$width" "$key" "$value"
done
# Output the zswap data
printf "\n========\n"
printf "VALUES"
printf "\n========\n"
for key in "${statistics[@]}"; do
# Get the value from the associative array
value=${zswap_data["$key"]}
# Output the key (name) and value in columns
printf "%-*s%s\n" "$width" "$key" "$value"
done
# Output the total size, compressed size, and compression ratio
printf "\n========\n"
printf "SUMMARY"
printf "\n========\n"
printf "%-*s%s MiB\n" "$width" "Total Size:" "$total_size"
printf "%-*s%s MiB\n" "$width" "Compressed Size:" "$compressed_size"
printf "%-*s%s\n" "$width" "Compression Ratio:" "$compression_ratio"