zram.init 6.0 KB

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