b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | |
| 3 | START=11 |
| 4 | |
| 5 | extra_command "compact" "Trigger compaction for all zram swap devices" |
| 6 | extra_command "status" "Print out information & statistics about zram swap devices" |
| 7 | |
| 8 | ram_getsize() |
| 9 | { |
| 10 | local line |
| 11 | |
| 12 | while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo |
| 13 | } |
| 14 | |
| 15 | zram_getsize() # in megabytes |
| 16 | { |
| 17 | local zram_size="$( uci -q get system.@system[0].zram_size_mb )" |
| 18 | local ram_size="$( ram_getsize )" |
| 19 | |
| 20 | if [ -z "$zram_size" ]; then |
| 21 | # e.g. 6MB for 32MB-routers or 20MB for 96MB-routers |
| 22 | echo $(( ram_size / 4096 )) |
| 23 | else |
| 24 | echo "$zram_size" |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | zram_dev() |
| 29 | { |
| 30 | local idx="$1" |
| 31 | echo "/dev/zram${idx:-0}" |
| 32 | } |
| 33 | |
| 34 | zram_reset() |
| 35 | { |
| 36 | local dev="$1" |
| 37 | local message="$2" |
| 38 | local proc_entry="/sys/block/$( basename "$dev" )/reset" |
| 39 | |
| 40 | logger -s -t zram_reset -p daemon.debug "$message via $proc_entry" |
| 41 | echo "1" >"$proc_entry" |
| 42 | } |
| 43 | |
| 44 | zram_getdev() |
| 45 | { |
| 46 | #get unallocated zram dev |
| 47 | local zdev=$( zram_dev ) |
| 48 | |
| 49 | if [ "$(mount | grep $zdev)" ]; then |
| 50 | local idx=$(cat /sys/class/zram-control/hot_add) |
| 51 | zdev="$( zram_dev $idx )" |
| 52 | fi |
| 53 | |
| 54 | echo $zdev |
| 55 | } |
| 56 | |
| 57 | zram_comp_algo() |
| 58 | { |
| 59 | local dev="$1" |
| 60 | local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )" |
| 61 | |
| 62 | if [ -z "$zram_comp_algo" ]; then |
| 63 | # default to lzo, which is always available |
| 64 | zram_comp_algo="lzo" |
| 65 | fi |
| 66 | |
| 67 | if [ $(grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm) -ne 0 ]; then |
| 68 | logger -s -t zram_comp_algo -p daemon.debug "set compression algorithm '$zram_comp_algo' for zram '$dev'" |
| 69 | echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm" |
| 70 | else |
| 71 | logger -s -t zram_comp_algo -p daemon.debug "compression algorithm '$zram_comp_algo' is not supported for '$dev'" |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | #print various stats info about zram swap device |
| 76 | zram_stats() |
| 77 | { |
| 78 | local zdev="/sys/block/$( basename "$1" )" |
| 79 | |
| 80 | printf "\nGathering stats info for zram device \"$( basename "$1" )\"\n\n" |
| 81 | |
| 82 | printf "ZRAM\n----\n" |
| 83 | printf "%-25s - %s\n" "Block device" $zdev |
| 84 | awk '{ printf "%-25s - %d MiB\n", "Device size", $1/1024/1024 }' <$zdev/disksize |
| 85 | printf "%-25s - %s\n" "Compression algo" "$(cat $zdev/comp_algorithm)" |
| 86 | |
| 87 | awk 'BEGIN { fmt = "%-25s - %.2f %s\n" |
| 88 | fmt2 = "%-25s - %d\n" |
| 89 | print "\nDATA\n----" } |
| 90 | { printf fmt, "Original data size", $1/1024/1024, "MiB" |
| 91 | printf fmt, "Compressed data size", $2/1024/1024, "MiB" |
| 92 | printf fmt, "Compress ratio", $1/$2, "" |
| 93 | print "\nMEMORY\n------" |
| 94 | printf fmt, "Memory used, total", $3/1024/1024, "MiB" |
| 95 | printf fmt, "Allocator overhead", ($3-$2)/1024/1024, "MiB" |
| 96 | printf fmt, "Allocator efficiency", $2/$3*100, "%" |
| 97 | printf fmt, "Maximum memory ever used", $5/1024/1024, "MiB" |
| 98 | printf fmt, "Memory limit", $4/1024/1024, "MiB" |
| 99 | print "\nPAGES\n-----" |
| 100 | printf fmt2, "Same pages count", $6 |
| 101 | printf fmt2, "Pages compacted", $7 }' <$zdev/mm_stat |
| 102 | |
| 103 | awk '{ printf "%-25s - %d\n", "Free pages discarded", $4 }' <$zdev/io_stat |
| 104 | } |
| 105 | |
| 106 | zram_compact() |
| 107 | { |
| 108 | # compact zram device (reduce memory allocation overhead) |
| 109 | local zdev="/sys/block/$( basename "$1" )" |
| 110 | |
| 111 | local old_mem_used=$(awk '{print $3}' <$zdev/mm_stat) |
| 112 | local old_overhead=$(awk '{print $3-$2}' <$zdev/mm_stat) |
| 113 | |
| 114 | echo 1 > $zdev/compact |
| 115 | |
| 116 | # If not running interactively, than just return |
| 117 | [ -z "$PS1" ] && return 0 |
| 118 | |
| 119 | echo "" |
| 120 | echo "Compacting zram device $zdev" |
| 121 | awk -v old_mem="$old_mem_used" -v ovr="$old_overhead" 'BEGIN { fmt = "%-25s - %.1f %s\n" } |
| 122 | { printf fmt, "Memory usage reduced by ", (old_mem-$3)/1024/1024, "MiB" |
| 123 | printf fmt, "Overhead reduced by", (ovr-($3-$2))/ovr*100, "%" }' <$zdev/mm_stat |
| 124 | } |
| 125 | |
| 126 | start() |
| 127 | { |
| 128 | [ -e /proc/swaps ] || { |
| 129 | logger -s -t zram_start -p daemon.crit "kernel doesn't support swap" |
| 130 | return 1 |
| 131 | } |
| 132 | |
| 133 | if [ $( grep -cs zram /proc/swaps ) -ne 0 ]; then |
| 134 | logger -s -t zram_start -p daemon.notice "zram swap is already mounted" |
| 135 | return 1 |
| 136 | fi |
| 137 | |
| 138 | local zram_dev="$( zram_getdev )" |
| 139 | |
| 140 | [ -e "$zram_dev" ] || { |
| 141 | logger -s -t zram_start -p daemon.crit "[ERROR] device '$zram_dev' not found" |
| 142 | return 1 |
| 143 | } |
| 144 | |
| 145 | local zram_size="$( zram_getsize )" |
| 146 | local zram_priority="$( uci -q get system.@system[0].zram_priority )" |
| 147 | |
| 148 | if [ -z "$zram_priority" ]; then |
| 149 | zram_priority="100" |
| 150 | fi |
| 151 | |
| 152 | logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MiB)" |
| 153 | |
| 154 | zram_reset "$zram_dev" "enforcing defaults" |
| 155 | zram_comp_algo "$zram_dev" |
| 156 | echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize" |
| 157 | busybox mkswap "$zram_dev" |
| 158 | busybox swapon -d -p $zram_priority "$zram_dev" |
| 159 | # kernel defaults to '60', the lower the less it swaps out - 0 = only swap to avoid OOM |
| 160 | if [ $( ram_size ) -gt 65536 ]; then |
| 161 | echo 60 >'/proc/sys/vm/swappiness' |
| 162 | else |
| 163 | echo 90 >'/proc/sys/vm/swappiness' |
| 164 | fi |
| 165 | } |
| 166 | |
| 167 | stop() |
| 168 | { |
| 169 | local zram_dev |
| 170 | |
| 171 | for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do { |
| 172 | logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev" |
| 173 | busybox swapoff "$zram_dev" && zram_reset "$zram_dev" "claiming memory back" |
| 174 | local dev_index="$( echo $zram_dev | grep -o "[0-9]*$" )" |
| 175 | if [ $dev_index -ne 0 ]; then |
| 176 | logger -s -t zram_stop -p daemon.debug "removing zram $zram_dev" |
| 177 | echo $dev_index > /sys/class/zram-control/hot_remove |
| 178 | fi |
| 179 | } done |
| 180 | } |
| 181 | |
| 182 | # show memory stats for all zram swaps |
| 183 | status() |
| 184 | { |
| 185 | for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do { |
| 186 | zram_stats "$zram_dev" |
| 187 | } done |
| 188 | } |
| 189 | |
| 190 | # trigger compaction for all zram swaps |
| 191 | compact() |
| 192 | { |
| 193 | for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do { |
| 194 | zram_compact "$zram_dev" |
| 195 | } done |
| 196 | } |