diff --git a/show_zram_status.sh b/show_zram_status.sh index d771cb3..008fefa 100644 --- a/show_zram_status.sh +++ b/show_zram_status.sh @@ -7,27 +7,65 @@ show_zram_status() { echo "========== zram 运行战报 ==========" - # 获取 zram 信息并进行格式化处理 - local stats=$(zramctl --raw --noheadings --output DATA,COMPR,TOTAL) + + if ! command -v zramctl >/dev/null 2>&1; then + echo "未安装 zramctl。" + return 1 + fi + + local table + table=$(zramctl --noheadings --output NAME,ALGORITHM,DISKSIZE,DATA,COMPR,TOTAL,COMP-RATIO,MOUNTPOINT 2>/dev/null || true) + + if [ -z "$table" ]; then + echo "未发现活跃的 zram 设备。" + return 0 + fi + + printf '%s\n' "$table" + + local stats + stats=$(zramctl --bytes --raw --noheadings --output DATA,COMPR,TOTAL 2>/dev/null || true) if [ -z "$stats" ]; then echo "未发现活跃的 zram 设备。" - return + return 0 fi - # 提取数据 (单位通常是字节) - local data_size=$(echo "$stats" | awk '{print $1}') # 存入的原始数据 - local compr_size=$(echo "$stats" | awk '{print $2}') # 压缩后的大小 - local total_mem=$(echo "$stats" | awk '{print $3}') # 实际占用的物理内存(含元数据) + local data_size compr_size total_mem saved_bytes ratio + read -r data_size compr_size total_mem <= 1024 && idx < 6) { + bytes /= 1024 + idx++ + } + printf "%.2f %s", bytes, units[idx] + }' + } + + echo "----------- 汇总 -----------" + echo "1. 原始数据量: $(format_bytes "$data_size")" + echo "2. 压缩后大小: $(format_bytes "$compr_size")" + echo "3. 实际占用内存: $(format_bytes "$total_mem")" + echo "4. 压缩倍率: ${ratio}x" + echo "5. 为系统节省了: $(format_bytes "$saved_bytes") 物理内存" echo "===================================" }