zram.init 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/sh /etc/rc.common
  2. START=15
  3. ram_size()
  4. {
  5. local line
  6. while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
  7. }
  8. zram_size() # in megabytes
  9. {
  10. local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
  11. local ram_size="$( ram_size )"
  12. if [ -z "$zram_size" ]; then
  13. # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
  14. echo $(( $ram_size / 2048 ))
  15. else
  16. echo "$zram_size"
  17. fi
  18. }
  19. zram_applicable()
  20. {
  21. local zram_dev="$1"
  22. grep -sq ^"$zram_dev " /proc/swaps && {
  23. logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active"
  24. return 1
  25. }
  26. [ -e "$zram_dev" ] || {
  27. logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
  28. return 1
  29. }
  30. which mkswap >/dev/null || {
  31. logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
  32. return 1
  33. }
  34. which swapon >/dev/null || {
  35. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
  36. return 1
  37. }
  38. which swapoff >/dev/null || {
  39. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
  40. return 1
  41. }
  42. }
  43. zram_dev()
  44. {
  45. local core="$1"
  46. echo "/dev/zram${core:-0}"
  47. }
  48. zram_reset()
  49. {
  50. local dev="$1"
  51. local message="$2"
  52. local proc_entry="/sys/block/$( basename "$dev" )/reset"
  53. logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
  54. echo "1" >"$proc_entry"
  55. }
  56. list_cpu_idx()
  57. {
  58. local line i=0
  59. while read line; do {
  60. case "$line" in
  61. [Pp]rocessor*)
  62. echo $i
  63. i=$(( $i + 1 ))
  64. ;;
  65. esac
  66. } done <"/proc/cpuinfo"
  67. }
  68. start()
  69. {
  70. # http://shmilyxbq-compcache.googlecode.com/hg/README
  71. # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
  72. local zram_size="$( zram_size )"
  73. local zram_dev core
  74. for core in $( list_cpu_idx ); do {
  75. zram_dev="$( zram_dev "$core" )"
  76. zram_applicable "$zram_dev" || return 1
  77. logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
  78. zram_reset "$zram_dev" "enforcing defaults"
  79. echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
  80. mkswap "$zram_dev"
  81. swapon "$zram_dev"
  82. } done
  83. }
  84. stop()
  85. {
  86. local zram_dev proc_entry
  87. for core in $( list_cpu_idx ); do {
  88. zram_dev="$( zram_dev "$core" )"
  89. proc_entry="/sys/block/$( basename "$zram_dev" )/reset"
  90. grep -sq ^"$zram_dev " /proc/swaps && {
  91. logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
  92. swapoff "$zram_dev"
  93. }
  94. zram_reset "$zram_dev" "claiming memory back"
  95. } done
  96. }