1 zram: Compressed RAM based block devices
2 ----------------------------------------
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 :)
12 Statistics for individual zram devices are exported through sysfs nodes at
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).
21 In this document we will describe only 'manual' zram configuration steps,
22 IOW, zram and zram_control sysfs attributes.
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
29 Following shows a typical sequence of steps for using zram.
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.
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
42 -EINVAL -- invalid input has been provided.
44 If you use 'echo', the returned value that is changed by 'echo' utility,
45 and, in general case, something like:
47 echo 3 > /sys/block/zram0/max_comp_streams
55 modprobe zram num_devices=4
56 This creates 4 devices: /dev/zram{0,1,2,3}
58 num_devices parameter is optional and tells zram how many devices should be
59 pre-created. Default: 1.
61 2) Set max number of compression streams
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.
69 To find out how many streams are currently available:
70 cat /sys/block/zram0/max_comp_streams
72 3) Select compression algorithm
73 Using comp_algorithm device attribute one can see available and
74 currently selected (shown in square brackets) compression algorithms,
75 change selected compression algorithm (once the device is initialised
76 there is no way to change compression algorithm).
79 #show supported compression algorithms
80 cat /sys/block/zram0/comp_algorithm
83 #select lzo compression algorithm
84 echo lzo > /sys/block/zram0/comp_algorithm
86 For the time being, the `comp_algorithm' content does not necessarily
87 show every compression algorithm supported by the kernel. We keep this
88 list primarily to simplify device configuration and one can configure
89 a new device with a compression algorithm that is not listed in
90 `comp_algorithm'. The thing is that, internally, ZRAM uses Crypto API
91 and, if some of the algorithms were built as modules, it's impossible
92 to list all of them using, for instance, /proc/crypto or any other
93 method. This, however, has an advantage of permitting the usage of
94 custom crypto compression modules (implementing S/W or H/W compression).
97 Set disk size by writing the value to sysfs node 'disksize'.
98 The value can be either in bytes or you can use mem suffixes.
100 # Initialize /dev/zram0 with 50MB disksize
101 echo $((50*1024*1024)) > /sys/block/zram0/disksize
104 echo 256K > /sys/block/zram0/disksize
105 echo 512M > /sys/block/zram0/disksize
106 echo 1G > /sys/block/zram0/disksize
109 There is little point creating a zram of greater than twice the size of memory
110 since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
111 size of the disk when not in use so a huge zram is wasteful.
113 5) Set memory limit: Optional
114 Set memory limit by writing the value to sysfs node 'mem_limit'.
115 The value can be either in bytes or you can use mem suffixes.
116 In addition, you could change the value in runtime.
118 # limit /dev/zram0 with 50MB memory
119 echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
122 echo 256K > /sys/block/zram0/mem_limit
123 echo 512M > /sys/block/zram0/mem_limit
124 echo 1G > /sys/block/zram0/mem_limit
126 # To disable memory limit
127 echo 0 > /sys/block/zram0/mem_limit
134 mount /dev/zram1 /tmp
136 7) Add/remove zram devices
138 zram provides a control interface, which enables dynamic (on-demand) device
139 addition and removal.
141 In order to add a new /dev/zramX device, perform read operation on hot_add
142 attribute. This will return either new device's device id (meaning that you
143 can use /dev/zram<id>) or error code.
146 cat /sys/class/zram-control/hot_add
149 To remove the existing /dev/zramX device (where X is a device id)
151 echo X > /sys/class/zram-control/hot_remove
154 Per-device statistics are exported as various nodes under /sys/block/zram<id>/
156 A brief description of exported device attributes. For more details please
157 read Documentation/ABI/testing/sysfs-block-zram.
159 Name access description
160 ---- ------ -----------
161 disksize RW show and set the device's disk size
162 initstate RO shows the initialization state of the device
163 reset WO trigger device reset
164 mem_used_max WO reset the `mem_used_max' counter (see later)
165 mem_limit WO specifies the maximum amount of memory ZRAM can use
166 to store the compressed data
167 max_comp_streams RW the number of possible concurrent compress operations
168 comp_algorithm RW show and change the compression algorithm
169 compact WO trigger memory compaction
170 debug_stat RO this file is used for zram debugging purposes
171 backing_dev RW set up backend storage for zram to write out
172 idle WO mark allocated slot as idle
175 User space is advised to use the following files to read the device statistics.
177 File /sys/block/zram<id>/stat
179 Represents block layer statistics. Read Documentation/block/stat.txt for
182 File /sys/block/zram<id>/io_stat
184 The stat file represents device's I/O statistics not accounted by block
185 layer and, thus, not available in zram<id>/stat file. It consists of a
186 single line of text and contains the following stats separated by
188 failed_reads the number of failed reads
189 failed_writes the number of failed writes
190 invalid_io the number of non-page-size-aligned I/O requests
191 notify_free Depending on device usage scenario it may account
192 a) the number of pages freed because of swap slot free
193 notifications or b) the number of pages freed because of
194 REQ_OP_DISCARD requests sent by bio. The former ones are
195 sent to a swap block device when a swap slot is freed,
196 which implies that this disk is being used as a swap disk.
197 The latter ones are sent by filesystem mounted with
198 discard option, whenever some data blocks are getting
201 File /sys/block/zram<id>/mm_stat
203 The stat file represents device's mm statistics. It consists of a single
204 line of text and contains the following stats separated by whitespace:
205 orig_data_size uncompressed size of data stored in this disk.
206 This excludes same-element-filled pages (same_pages) since
207 no memory is allocated for them.
209 compr_data_size compressed size of data stored in this disk
210 mem_used_total the amount of memory allocated for this disk. This
211 includes allocator fragmentation and metadata overhead,
212 allocated for this disk. So, allocator space efficiency
213 can be calculated using compr_data_size and this statistic.
215 mem_limit the maximum amount of memory ZRAM can use to store
217 mem_used_max the maximum amount of memory zram have consumed to
219 same_pages the number of same element filled pages written to this disk.
220 No memory is allocated for such pages.
221 pages_compacted the number of pages freed during compaction
222 huge_pages the number of incompressible pages
224 File /sys/block/zram<id>/bd_stat
226 The stat file represents device's backing device statistics. It consists of
227 a single line of text and contains the following stats separated by whitespace:
228 bd_count size of data written in backing device.
230 bd_reads the number of reads from backing device
232 bd_writes the number of writes to backing device
240 Write any positive value to 'reset' sysfs node
241 echo 1 > /sys/block/zram0/reset
242 echo 1 > /sys/block/zram1/reset
244 This frees all the memory allocated for the given device and
245 resets the disksize to zero. You must set the disksize again
246 before reusing the device.
252 With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page
253 to backing storage rather than keeping it in memory.
254 To use the feature, admin should set up backing device via
256 "echo /dev/sda5 > /sys/block/zramX/backing_dev"
258 before disksize setting. It supports only partition at this moment.
259 If admin want to use incompressible page writeback, they could do via
261 "echo huge > /sys/block/zramX/write"
263 To use idle page writeback, first, user need to declare zram pages
266 "echo all > /sys/block/zramX/idle"
268 From now on, any pages on zram are idle pages. The idle mark
269 will be removed until someone request access of the block.
270 IOW, unless there is access request, those pages are still idle pages.
272 Admin can request writeback of those idle pages at right timing via
274 "echo idle > /sys/block/zramX/writeback"
276 With the command, zram writeback idle pages from memory to the storage.
280 With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
281 zram block. It could be useful to catch cold or incompressible
282 pages of the process with*pagemap.
283 If you enable the feature, you could see block state via
284 /sys/kernel/debug/zram/zram0/block_state". The output is as follows,
290 First column is zram's block index.
291 Second column is access time since the system was booted
292 Third column is state of the block.
294 w: written page to backing store
298 First line of above example says 300th block is accessed at 75.033841sec
299 and the block's state is huge so it is written back to the backing
300 storage. It's a debugging feature so anyone shouldn't rely on it to work