Last active
April 5, 2026 08:55
-
-
Save azhe403/a9837677ad264ffeb2232e79b0fa2614 to your computer and use it in GitHub Desktop.
my experiment dynamic zram & support files
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 | |
| # === Konfigurasi Agresif === | |
| THRESHOLD_NODE=75 | |
| THRESHOLD_FREE_ALL=45 | |
| ADD_SIZE="1G" | |
| MAX_ZRAM=8 | |
| BASE_DEVICE="zram0" | |
| BASE_PRIO=100 | |
| log() { | |
| echo "$(date '+%Y-%m-%d %H:%M:%S') $*" | |
| } | |
| # Menggunakan file sementara atau teknik variable injection agar data tidak hilang | |
| get_zram_stats() { | |
| local t_used=0 | |
| local t_size=0 | |
| # Mengambil data langsung dari swapon tanpa pipe berlebih agar lebih stabil | |
| while read -r line; do | |
| # Hanya ambil baris yang mengandung /dev/zram | |
| if [[ "$line" == *"/dev/zram"* ]]; then | |
| local u=$(echo "$line" | awk '{print $2}') | |
| local s=$(echo "$line" | awk '{print $3}') | |
| t_used=$(( t_used + u )) | |
| t_size=$(( t_size + s )) | |
| fi | |
| done < <(swapon --show=NAME,USED,SIZE --noheadings --bytes) | |
| echo "$t_used $t_size" | |
| } | |
| get_zram_global_pct() { | |
| local stats=$(get_zram_stats) | |
| local used=$(echo $stats | awk '{print $1}') | |
| local size=$(echo $stats | awk '{print $2}') | |
| if [[ $size -gt 0 ]]; then | |
| # Bagi 1024 agar tidak overflow saat dikali 100 | |
| echo $(( ( (used / 1024) * 100) / (size / 1024) )) | |
| else | |
| echo 0 | |
| fi | |
| } | |
| get_node_by_rank() { | |
| local rank=$1 | |
| swapon --show=NAME,USED,SIZE --noheadings --bytes | grep "^/dev/zram" | sort -V | tail -n $((rank+1)) | head -n 1 | |
| } | |
| get_zram_count() { | |
| zramctl --noheadings | grep -c "^/dev/zram" | |
| } | |
| log_system_info() { | |
| local ram_total ram_available ram_used ram_percent | |
| ram_total=$(awk '/MemTotal/ {print $2}' /proc/meminfo) | |
| ram_available=$(awk '/MemAvailable/ {print $2}' /proc/meminfo) | |
| ram_used=$(( ram_total - ram_available )) | |
| ram_percent=$(( ram_used * 100 / ram_total )) | |
| local stats=$(get_zram_stats) | |
| local z_used_b=$(echo $stats | awk '{print $1}') | |
| local z_size_b=$(echo $stats | awk '{print $2}') | |
| local g_pct=$(get_zram_global_pct) | |
| log "--- System Info ---" | |
| log "RAM : $(( ram_used / 1024 ))MB used / $(( ram_total / 1024 ))MB total (${ram_percent}%)" | |
| log "ZRAM : $(( z_used_b / 1024 / 1024 ))MB used / $(( z_size_b / 1024 / 1024 ))MB total (${g_pct}%)" | |
| log "--- Per Device ---" | |
| zramctl --noheadings --output NAME,ALGORITHM | sort -V | while read -r dev algo; do | |
| local swap_info=$(swapon --show=NAME,USED,SIZE,PRIO --noheadings --bytes | grep -w "$dev") | |
| if [[ -n "$swap_info" ]]; then | |
| local u_b=$(echo "$swap_info" | awk '{print $2}') | |
| local s_b=$(echo "$swap_info" | awk '{print $3}') | |
| local prio=$(echo "$swap_info" | awk '{print $4}') | |
| local d_pct=0 | |
| [[ $s_b -gt 0 ]] && d_pct=$(( ( (u_b/1024) * 100) / (s_b/1024) )) | |
| log "$dev : $(( u_b / 1024 / 1024 ))MB / $(( s_b / 1024 / 1024 ))MB (${d_pct}%) [Prio: $prio] [$algo]" | |
| fi | |
| done | |
| log "-------------------" | |
| } | |
| add_zram_node() { | |
| local count=$(get_zram_count) | |
| [[ $count -ge $MAX_ZRAM ]] && return | |
| local prio=$(( BASE_PRIO - count )) | |
| local zombie=$(zramctl --noheadings --output NAME | while read -r z; do | |
| if ! swapon --show=NAME --noheadings | grep -q "$z"; then echo "$z"; break; fi | |
| done) | |
| if [[ -n "$zombie" ]]; then | |
| mkswap "$zombie" > /dev/null 2>&1 | |
| swapon "$zombie" --priority "$prio" | |
| log "RECOVERY: Activated zombie $zombie" | |
| else | |
| local new_dev=$(zramctl --find --size "$ADD_SIZE" --algorithm zstd) | |
| if [[ -n "$new_dev" ]]; then | |
| mkswap "$new_dev" > /dev/null 2>&1 | |
| swapon "$new_dev" --priority "$prio" | |
| log "ACTION: Grow! Added $new_dev" | |
| fi | |
| fi | |
| } | |
| # === Main Execution === | |
| STATS=$(get_zram_stats) | |
| GLOBAL_PCT=$(get_zram_global_pct) | |
| COUNT=$(get_zram_count) | |
| read -r LAST_NODE LAST_USED LAST_SIZE < <(get_node_by_rank 0) | |
| LAST_PCT=0 | |
| if [[ -n "$LAST_SIZE" && $LAST_SIZE -gt 0 ]]; then | |
| LAST_PCT=$(( ( (LAST_USED/1024) * 100) / (LAST_SIZE/1024) )) | |
| fi | |
| log "CHECK : Last node ${LAST_NODE} is ${LAST_PCT}% full | Global ZRAM: ${GLOBAL_PCT}% | Active: ${COUNT}" | |
| if [[ "$LAST_NODE" == "" || $LAST_PCT -ge $THRESHOLD_NODE ]]; then | |
| add_zram_node | |
| log_system_info | |
| elif [[ $COUNT -gt 1 ]]; then | |
| if [[ $LAST_PCT -lt 10 && $GLOBAL_PCT -lt $THRESHOLD_FREE_ALL ]]; then | |
| log "ACTION: Shrink! Global usage safe (${GLOBAL_PCT}%) and node empty." | |
| swapoff "$LAST_NODE" && zramctl --reset "$LAST_NODE" | |
| log_system_info | |
| else | |
| log "STATUS: Stable (Global: ${GLOBAL_PCT}%, Last: ${LAST_PCT}%)." | |
| log_system_info | |
| fi | |
| else | |
| log "STATUS: Stable." | |
| log_system_info | |
| fi | |
| log "==================================================" |
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
| for dev in /dev/zram[1-9]*; do | |
| if [ -b "$dev" ]; then | |
| echo "Cleaning up $dev..." | |
| sudo swapoff "$dev" 2>/dev/null | |
| sudo zramctl --reset "$dev" 2>/dev/null | |
| fi | |
| done |
Comments are disabled for this gist.