OpenWrt – Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/bin/sh /etc/rc.common
2  
3 START=15
4  
5 ram_size()
6 {
7 local line
8  
9 while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
10 }
11  
12 zram_size() # in megabytes
13 {
14 local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
15 local ram_size="$( ram_size )"
16  
17 if [ -z "$zram_size" ]; then
18 # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
3 office 19 echo $(( $ram_size / 2048 ))
1 office 20 else
21 echo "$zram_size"
22 fi
23 }
24  
25 zram_applicable()
26 {
27 local zram_dev="$1"
28  
3 office 29 grep -sq ^"$zram_dev " /proc/swaps && {
30 logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active"
31 return 1
32 }
33  
1 office 34 [ -e "$zram_dev" ] || {
35 logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
36 return 1
37 }
38  
39 which mkswap >/dev/null || {
40 logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
41 return 1
42 }
43  
44 which swapon >/dev/null || {
45 logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
46 return 1
47 }
48  
49 which swapoff >/dev/null || {
50 logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
51 return 1
52 }
53 }
54  
55 zram_dev()
56 {
3 office 57 local core="$1"
58  
59 echo "/dev/zram${core:-0}"
1 office 60 }
61  
62 zram_reset()
63 {
64 local dev="$1"
65 local message="$2"
66 local proc_entry="/sys/block/$( basename "$dev" )/reset"
67  
68 logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
69 echo "1" >"$proc_entry"
70 }
71  
3 office 72 list_cpu_idx()
1 office 73 {
3 office 74 # Offset by 1 if /dev/zram0 is in use by /tmp
75 if [ "$(mount | grep /dev/zram0)" ]; then
76 local line i=1
77 # Hot-add new ZRAM device (if necessary)
78 if [ ! -b /dev/zram1 ]; then
79 cat /sys/class/zram-control/hot_add
80 fi
1 office 81 else
3 office 82 local line i=0
1 office 83 fi
3 office 84  
85 while read line; do {
86 case "$line" in
87 [Pp]rocessor*)
88 echo $i
89 i=$(( $i + 1 ))
90 ;;
91 esac
92 } done <"/proc/cpuinfo"
1 office 93 }
94  
3 office 95 start()
1 office 96 {
3 office 97 # http://shmilyxbq-compcache.googlecode.com/hg/README
98 # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
1 office 99  
100 local zram_size="$( zram_size )"
3 office 101 local zram_dev core
1 office 102  
3 office 103 for core in $( list_cpu_idx ); do {
104 zram_dev="$( zram_dev "$core" )"
105 zram_applicable "$zram_dev" || return 1
1 office 106  
3 office 107 logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
1 office 108  
3 office 109 zram_reset "$zram_dev" "enforcing defaults"
110 echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
111 mkswap "$zram_dev"
112 swapon "$zram_dev"
113 } done
1 office 114 }
115  
116 stop()
117 {
3 office 118 local zram_dev proc_entry
1 office 119  
3 office 120 for core in $( list_cpu_idx ); do {
121 zram_dev="$( zram_dev "$core" )"
122 proc_entry="/sys/block/$( basename "$zram_dev" )/reset"
123  
124 grep -sq ^"$zram_dev " /proc/swaps && {
125 logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
126 swapoff "$zram_dev"
127 }
128  
129 zram_reset "$zram_dev" "claiming memory back"
1 office 130 } done
131 }
132