Commit | Line | Data |
---|---|---|
00ac9ba0 NG |
1 | zram: Compressed RAM based block devices |
2 | ---------------------------------------- | |
47f9afb3 | 3 | |
47f9afb3 NG |
4 | * Introduction |
5 | ||
9b9913d8 NG |
6 | The zram module creates RAM based block devices named /dev/zram<id> |
7 | (<id> = 0, 1, ...). Pages written to these disks are compressed and stored | |
8 | in memory itself. These disks allow very fast I/O and compression provides | |
9 | good amounts of memory savings. Some of the usecases include /tmp storage, | |
10 | use as swap disks, various caches under /var and maybe many more :) | |
47f9afb3 | 11 | |
9b9913d8 NG |
12 | Statistics for individual zram devices are exported through sysfs nodes at |
13 | /sys/block/zram<id>/ | |
47f9afb3 NG |
14 | |
15 | * Usage | |
16 | ||
3657c20d SS |
17 | There are several ways to configure and manage zram device(-s): |
18 | a) using zram and zram_control sysfs attributes | |
19 | b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org). | |
20 | ||
21 | In this document we will describe only 'manual' zram configuration steps, | |
22 | IOW, zram and zram_control sysfs attributes. | |
23 | ||
24 | In order to get a better idea about zramctl please consult util-linux | |
25 | documentation, zramctl man-page or `zramctl --help'. Please be informed | |
26 | that zram maintainers do not develop/maintain util-linux or zramctl, should | |
27 | you have any questions please contact util-linux@vger.kernel.org | |
28 | ||
00ac9ba0 | 29 | Following shows a typical sequence of steps for using zram. |
47f9afb3 | 30 | |
3657c20d SS |
31 | WARNING |
32 | ======= | |
33 | For the sake of simplicity we skip error checking parts in most of the | |
34 | examples below. However, it is your sole responsibility to handle errors. | |
35 | ||
36 | zram sysfs attributes always return negative values in case of errors. | |
37 | The list of possible return codes: | |
38 | -EBUSY -- an attempt to modify an attribute that cannot be changed once | |
39 | the device has been initialised. Please reset device first; | |
40 | -ENOMEM -- zram was not able to allocate enough memory to fulfil your | |
41 | needs; | |
42 | -EINVAL -- invalid input has been provided. | |
43 | ||
44 | If you use 'echo', the returned value that is changed by 'echo' utility, | |
45 | and, in general case, something like: | |
46 | ||
47 | echo 3 > /sys/block/zram0/max_comp_streams | |
48 | if [ $? -ne 0 ]; | |
49 | handle_error | |
50 | fi | |
51 | ||
52 | should suffice. | |
53 | ||
9b9913d8 | 54 | 1) Load Module: |
00ac9ba0 | 55 | modprobe zram num_devices=4 |
9b9913d8 | 56 | This creates 4 devices: /dev/zram{0,1,2,3} |
c3cdb40e SS |
57 | |
58 | num_devices parameter is optional and tells zram how many devices should be | |
59 | pre-created. Default: 1. | |
47f9afb3 | 60 | |
beca3ec7 | 61 | 2) Set max number of compression streams |
43209ea2 SS |
62 | Regardless the value passed to this attribute, ZRAM will always |
63 | allocate multiple compression streams - one per online CPUs - thus | |
64 | allowing several concurrent compression operations. The number of | |
65 | allocated compression streams goes down when some of the CPUs | |
66 | become offline. There is no single-compression-stream mode anymore, | |
67 | unless you are running a UP system or has only 1 CPU online. | |
68 | ||
69 | To find out how many streams are currently available: | |
beca3ec7 SS |
70 | cat /sys/block/zram0/max_comp_streams |
71 | ||
e46b8a03 SS |
72 | 3) Select compression algorithm |
73 | Using comp_algorithm device attribute one can see available and | |
3657c20d | 74 | currently selected (shown in square brackets) compression algorithms, |
e46b8a03 SS |
75 | change selected compression algorithm (once the device is initialised |
76 | there is no way to change compression algorithm). | |
77 | ||
78 | Examples: | |
79 | #show supported compression algorithms | |
80 | cat /sys/block/zram0/comp_algorithm | |
81 | lzo [lz4] | |
82 | ||
83 | #select lzo compression algorithm | |
84 | echo lzo > /sys/block/zram0/comp_algorithm | |
85 | ||
86 | 4) Set Disksize | |
0231c403 MK |
87 | Set disk size by writing the value to sysfs node 'disksize'. |
88 | The value can be either in bytes or you can use mem suffixes. | |
89 | Examples: | |
90 | # Initialize /dev/zram0 with 50MB disksize | |
91 | echo $((50*1024*1024)) > /sys/block/zram0/disksize | |
92 | ||
93 | # Using mem suffixes | |
94 | echo 256K > /sys/block/zram0/disksize | |
95 | echo 512M > /sys/block/zram0/disksize | |
96 | echo 1G > /sys/block/zram0/disksize | |
47f9afb3 | 97 | |
e64cd51d SS |
98 | Note: |
99 | There is little point creating a zram of greater than twice the size of memory | |
100 | since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the | |
101 | size of the disk when not in use so a huge zram is wasteful. | |
102 | ||
9ada9da9 MK |
103 | 5) Set memory limit: Optional |
104 | Set memory limit by writing the value to sysfs node 'mem_limit'. | |
105 | The value can be either in bytes or you can use mem suffixes. | |
106 | In addition, you could change the value in runtime. | |
107 | Examples: | |
108 | # limit /dev/zram0 with 50MB memory | |
109 | echo $((50*1024*1024)) > /sys/block/zram0/mem_limit | |
110 | ||
111 | # Using mem suffixes | |
112 | echo 256K > /sys/block/zram0/mem_limit | |
113 | echo 512M > /sys/block/zram0/mem_limit | |
114 | echo 1G > /sys/block/zram0/mem_limit | |
115 | ||
116 | # To disable memory limit | |
117 | echo 0 > /sys/block/zram0/mem_limit | |
118 | ||
119 | 6) Activate: | |
00ac9ba0 NG |
120 | mkswap /dev/zram0 |
121 | swapon /dev/zram0 | |
122 | ||
123 | mkfs.ext4 /dev/zram1 | |
124 | mount /dev/zram1 /tmp | |
47f9afb3 | 125 | |
6566d1a3 SS |
126 | 7) Add/remove zram devices |
127 | ||
128 | zram provides a control interface, which enables dynamic (on-demand) device | |
129 | addition and removal. | |
130 | ||
131 | In order to add a new /dev/zramX device, perform read operation on hot_add | |
132 | attribute. This will return either new device's device id (meaning that you | |
133 | can use /dev/zram<id>) or error code. | |
134 | ||
135 | Example: | |
136 | cat /sys/class/zram-control/hot_add | |
137 | 1 | |
138 | ||
139 | To remove the existing /dev/zramX device (where X is a device id) | |
140 | execute | |
141 | echo X > /sys/class/zram-control/hot_remove | |
142 | ||
143 | 8) Stats: | |
77ba015f SS |
144 | Per-device statistics are exported as various nodes under /sys/block/zram<id>/ |
145 | ||
3657c20d | 146 | A brief description of exported device attributes. For more details please |
77ba015f SS |
147 | read Documentation/ABI/testing/sysfs-block-zram. |
148 | ||
149 | Name access description | |
150 | ---- ------ ----------- | |
151 | disksize RW show and set the device's disk size | |
152 | initstate RO shows the initialization state of the device | |
153 | reset WO trigger device reset | |
154 | num_reads RO the number of reads | |
155 | failed_reads RO the number of failed reads | |
156 | num_write RO the number of writes | |
157 | failed_writes RO the number of failed writes | |
158 | invalid_io RO the number of non-page-size-aligned I/O requests | |
159 | max_comp_streams RW the number of possible concurrent compress operations | |
160 | comp_algorithm RW show and change the compression algorithm | |
161 | notify_free RO the number of notifications to free pages (either | |
162 | slot free notifications or REQ_DISCARD requests) | |
163 | zero_pages RO the number of zero filled pages written to this disk | |
164 | orig_data_size RO uncompressed size of data stored in this disk | |
165 | compr_data_size RO compressed size of data stored in this disk | |
166 | mem_used_total RO the amount of memory allocated for this disk | |
3657c20d SS |
167 | mem_used_max RW the maximum amount of memory zram have consumed to |
168 | store the data (to reset this counter to the actual | |
169 | current value, write 1 to this attribute) | |
77ba015f SS |
170 | mem_limit RW the maximum amount of memory ZRAM can use to store |
171 | the compressed data | |
860c707d SS |
172 | pages_compacted RO the number of pages freed during compaction |
173 | (available only via zram<id>/mm_stat node) | |
3d8ed88b | 174 | compact WO trigger memory compaction |
623e47fc | 175 | debug_stat RO this file is used for zram debugging purposes |
77ba015f | 176 | |
8f7d282c SS |
177 | WARNING |
178 | ======= | |
179 | per-stat sysfs attributes are considered to be deprecated. | |
180 | The basic strategy is: | |
181 | -- the existing RW nodes will be downgraded to WO nodes (in linux 4.11) | |
182 | -- deprecated RO sysfs nodes will eventually be removed (in linux 4.11) | |
183 | ||
184 | The list of deprecated attributes can be found here: | |
185 | Documentation/ABI/obsolete/sysfs-block-zram | |
186 | ||
187 | Basically, every attribute that has its own read accessible sysfs node | |
188 | (e.g. num_reads) *AND* is accessible via one of the stat files (zram<id>/stat | |
189 | or zram<id>/io_stat or zram<id>/mm_stat) is considered to be deprecated. | |
190 | ||
191 | User space is advised to use the following files to read the device statistics. | |
192 | ||
77ba015f SS |
193 | File /sys/block/zram<id>/stat |
194 | ||
195 | Represents block layer statistics. Read Documentation/block/stat.txt for | |
196 | details. | |
47f9afb3 | 197 | |
2f6a3bed SS |
198 | File /sys/block/zram<id>/io_stat |
199 | ||
200 | The stat file represents device's I/O statistics not accounted by block | |
201 | layer and, thus, not available in zram<id>/stat file. It consists of a | |
202 | single line of text and contains the following stats separated by | |
203 | whitespace: | |
204 | failed_reads | |
205 | failed_writes | |
206 | invalid_io | |
207 | notify_free | |
208 | ||
4f2109f6 SS |
209 | File /sys/block/zram<id>/mm_stat |
210 | ||
211 | The stat file represents device's mm statistics. It consists of a single | |
212 | line of text and contains the following stats separated by whitespace: | |
213 | orig_data_size | |
214 | compr_data_size | |
215 | mem_used_total | |
216 | mem_limit | |
217 | mem_used_max | |
218 | zero_pages | |
219 | num_migrated | |
220 | ||
6566d1a3 | 221 | 9) Deactivate: |
00ac9ba0 NG |
222 | swapoff /dev/zram0 |
223 | umount /dev/zram1 | |
47f9afb3 | 224 | |
6566d1a3 | 225 | 10) Reset: |
9b9913d8 NG |
226 | Write any positive value to 'reset' sysfs node |
227 | echo 1 > /sys/block/zram0/reset | |
228 | echo 1 > /sys/block/zram1/reset | |
229 | ||
0231c403 MK |
230 | This frees all the memory allocated for the given device and |
231 | resets the disksize to zero. You must set the disksize again | |
232 | before reusing the device. | |
47f9afb3 | 233 | |
47f9afb3 NG |
234 | Nitin Gupta |
235 | ngupta@vflare.org |