#!/bin/bash ######## # zswap-stats # version: 2023-06-09.1 # # Licence: CC-BY-SA 4.0 # https://wiki.tnonline.net ######## # 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" "non_same_filled_pages_enabled" "same_filled_pages_enabled" "zpool") # Array of zswap statistics statistics=("duplicate_entry" "pool_limit_hit" "pool_total_size" "reject_alloc_fail" "reject_compress_poor" "reject_kmemcache_fail" "reject_reclaim_fail" "same_filled_pages" "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 variable 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"