1 ========================================
2 zram: Compressed RAM-based block devices
3 ========================================
8 The zram module creates RAM-based block devices named /dev/zram<id>
9 (<id> = 0, 1, ...). Pages written to these disks are compressed and stored
10 in memory itself. These disks allow very fast I/O and compression provides
11 good amounts of memory savings. Some of the use cases include /tmp storage,
12 use as swap disks, various caches under /var and maybe many more. :)
14 Statistics for individual zram devices are exported through sysfs nodes at
20 There are several ways to configure and manage zram device(-s):
22 a) using zram and zram_control sysfs attributes
23 b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org).
25 In this document we will describe only 'manual' zram configuration steps,
26 IOW, zram and zram_control sysfs attributes.
28 In order to get a better idea about zramctl please consult util-linux
29 documentation, zramctl man-page or `zramctl --help`. Please be informed
30 that zram maintainers do not develop/maintain util-linux or zramctl, should
31 you have any questions please contact util-linux@vger.kernel.org
33 Following shows a typical sequence of steps for using zram.
38 For the sake of simplicity we skip error checking parts in most of the
39 examples below. However, it is your sole responsibility to handle errors.
41 zram sysfs attributes always return negative values in case of errors.
42 The list of possible return codes:
44 ======== =============================================================
45 -EBUSY an attempt to modify an attribute that cannot be changed once
46 the device has been initialised. Please reset device first.
47 -ENOMEM zram was not able to allocate enough memory to fulfil your
49 -EINVAL invalid input has been provided.
50 ======== =============================================================
52 If you use 'echo', the returned value is set by the 'echo' utility,
53 and, in general case, something like::
55 echo 3 > /sys/block/zram0/max_comp_streams
67 modprobe zram num_devices=4
69 This creates 4 devices: /dev/zram{0,1,2,3}
71 num_devices parameter is optional and tells zram how many devices should be
72 pre-created. Default: 1.
74 2) Set max number of compression streams
75 ========================================
77 Regardless of the value passed to this attribute, ZRAM will always
78 allocate multiple compression streams - one per online CPU - thus
79 allowing several concurrent compression operations. The number of
80 allocated compression streams goes down when some of the CPUs
81 become offline. There is no single-compression-stream mode anymore,
82 unless you are running a UP system or have only 1 CPU online.
84 To find out how many streams are currently available::
86 cat /sys/block/zram0/max_comp_streams
88 3) Select compression algorithm
89 ===============================
91 Using comp_algorithm device attribute one can see available and
92 currently selected (shown in square brackets) compression algorithms,
93 or change the selected compression algorithm (once the device is initialised
94 there is no way to change compression algorithm).
98 #show supported compression algorithms
99 cat /sys/block/zram0/comp_algorithm
102 #select lzo compression algorithm
103 echo lzo > /sys/block/zram0/comp_algorithm
105 For the time being, the `comp_algorithm` content does not necessarily
106 show every compression algorithm supported by the kernel. We keep this
107 list primarily to simplify device configuration and one can configure
108 a new device with a compression algorithm that is not listed in
109 `comp_algorithm`. The thing is that, internally, ZRAM uses Crypto API
110 and, if some of the algorithms were built as modules, it's impossible
111 to list all of them using, for instance, /proc/crypto or any other
112 method. This, however, has an advantage of permitting the usage of
113 custom crypto compression modules (implementing S/W or H/W compression).
118 Set disk size by writing the value to sysfs node 'disksize'.
119 The value can be either in bytes or you can use mem suffixes.
122 # Initialize /dev/zram0 with 50MB disksize
123 echo $((50*1024*1024)) > /sys/block/zram0/disksize
126 echo 256K > /sys/block/zram0/disksize
127 echo 512M > /sys/block/zram0/disksize
128 echo 1G > /sys/block/zram0/disksize
131 There is little point creating a zram of greater than twice the size of memory
132 since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
133 size of the disk when not in use so a huge zram is wasteful.
135 5) Set memory limit: Optional
136 =============================
138 Set memory limit by writing the value to sysfs node 'mem_limit'.
139 The value can be either in bytes or you can use mem suffixes.
140 In addition, you could change the value in runtime.
143 # limit /dev/zram0 with 50MB memory
144 echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
147 echo 256K > /sys/block/zram0/mem_limit
148 echo 512M > /sys/block/zram0/mem_limit
149 echo 1G > /sys/block/zram0/mem_limit
151 # To disable memory limit
152 echo 0 > /sys/block/zram0/mem_limit
163 mount /dev/zram1 /tmp
165 7) Add/remove zram devices
166 ==========================
168 zram provides a control interface, which enables dynamic (on-demand) device
169 addition and removal.
171 In order to add a new /dev/zramX device, perform a read operation on the hot_add
172 attribute. This will return either the new device's device id (meaning that you
173 can use /dev/zram<id>) or an error code.
177 cat /sys/class/zram-control/hot_add
180 To remove the existing /dev/zramX device (where X is a device id)
183 echo X > /sys/class/zram-control/hot_remove
188 Per-device statistics are exported as various nodes under /sys/block/zram<id>/
190 A brief description of exported device attributes follows. For more details
191 please read Documentation/ABI/testing/sysfs-block-zram.
193 ====================== ====== ===============================================
194 Name access description
195 ====================== ====== ===============================================
196 disksize RW show and set the device's disk size
197 initstate RO shows the initialization state of the device
198 reset WO trigger device reset
199 mem_used_max WO reset the `mem_used_max` counter (see later)
200 mem_limit WO specifies the maximum amount of memory ZRAM can
201 use to store the compressed data
202 writeback_limit WO specifies the maximum amount of write IO zram
203 can write out to backing device as 4KB unit
204 writeback_limit_enable RW show and set writeback_limit feature
205 max_comp_streams RW the number of possible concurrent compress
207 comp_algorithm RW show and change the compression algorithm
208 compact WO trigger memory compaction
209 debug_stat RO this file is used for zram debugging purposes
210 backing_dev RW set up backend storage for zram to write out
211 idle WO mark allocated slot as idle
212 ====================== ====== ===============================================
215 User space is advised to use the following files to read the device statistics.
217 File /sys/block/zram<id>/stat
219 Represents block layer statistics. Read Documentation/block/stat.rst for
222 File /sys/block/zram<id>/io_stat
224 The stat file represents device's I/O statistics not accounted by block
225 layer and, thus, not available in zram<id>/stat file. It consists of a
226 single line of text and contains the following stats separated by
229 ============= =============================================================
230 failed_reads The number of failed reads
231 failed_writes The number of failed writes
232 invalid_io The number of non-page-size-aligned I/O requests
233 notify_free Depending on device usage scenario it may account
235 a) the number of pages freed because of swap slot free
237 b) the number of pages freed because of
238 REQ_OP_DISCARD requests sent by bio. The former ones are
239 sent to a swap block device when a swap slot is freed,
240 which implies that this disk is being used as a swap disk.
242 The latter ones are sent by filesystem mounted with
243 discard option, whenever some data blocks are getting
245 ============= =============================================================
247 File /sys/block/zram<id>/mm_stat
249 The mm_stat file represents the device's mm statistics. It consists of a single
250 line of text and contains the following stats separated by whitespace:
252 ================ =============================================================
253 orig_data_size uncompressed size of data stored in this disk.
255 compr_data_size compressed size of data stored in this disk
256 mem_used_total the amount of memory allocated for this disk. This
257 includes allocator fragmentation and metadata overhead,
258 allocated for this disk. So, allocator space efficiency
259 can be calculated using compr_data_size and this statistic.
261 mem_limit the maximum amount of memory ZRAM can use to store
263 mem_used_max the maximum amount of memory zram has consumed to
265 same_pages the number of same element filled pages written to this disk.
266 No memory is allocated for such pages.
267 pages_compacted the number of pages freed during compaction
268 huge_pages the number of incompressible pages
269 huge_pages_since the number of incompressible pages since zram set up
270 ================ =============================================================
272 File /sys/block/zram<id>/bd_stat
274 The bd_stat file represents a device's backing device statistics. It consists of
275 a single line of text and contains the following stats separated by whitespace:
277 ============== =============================================================
278 bd_count size of data written in backing device.
280 bd_reads the number of reads from backing device
282 bd_writes the number of writes to backing device
284 ============== =============================================================
297 Write any positive value to 'reset' sysfs node::
299 echo 1 > /sys/block/zram0/reset
300 echo 1 > /sys/block/zram1/reset
302 This frees all the memory allocated for the given device and
303 resets the disksize to zero. You must set the disksize again
304 before reusing the device.
312 With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page
313 to backing storage rather than keeping it in memory.
314 To use the feature, admin should set up backing device via::
316 echo /dev/sda5 > /sys/block/zramX/backing_dev
318 before disksize setting. It supports only partitions at this moment.
319 If admin wants to use incompressible page writeback, they could do it via::
321 echo huge > /sys/block/zramX/writeback
323 To use idle page writeback, first, user need to declare zram pages
326 echo all > /sys/block/zramX/idle
328 From now on, any pages on zram are idle pages. The idle mark
329 will be removed until someone requests access of the block.
330 IOW, unless there is access request, those pages are still idle pages.
331 Additionally, when CONFIG_ZRAM_TRACK_ENTRY_ACTIME is enabled pages can be
332 marked as idle based on how long (in seconds) it's been since they were
335 echo 86400 > /sys/block/zramX/idle
337 In this example all pages which haven't been accessed in more than 86400
338 seconds (one day) will be marked idle.
340 Admin can request writeback of those idle pages at right timing via::
342 echo idle > /sys/block/zramX/writeback
344 With the command, zram will writeback idle pages from memory to the storage.
346 Additionally, if a user choose to writeback only huge and idle pages
347 this can be accomplished with::
349 echo huge_idle > /sys/block/zramX/writeback
351 If a user chooses to writeback only incompressible pages (pages that none of
352 algorithms can compress) this can be accomplished with::
354 echo incompressible > /sys/block/zramX/writeback
356 If an admin wants to write a specific page in zram device to the backing device,
357 they could write a page index into the interface::
359 echo "page_index=1251" > /sys/block/zramX/writeback
361 If there are lots of write IO with flash device, potentially, it has
362 flash wearout problem so that admin needs to design write limitation
363 to guarantee storage health for entire product life.
365 To overcome the concern, zram supports "writeback_limit" feature.
366 The "writeback_limit_enable"'s default value is 0 so that it doesn't limit
367 any writeback. IOW, if admin wants to apply writeback budget, they should
368 enable writeback_limit_enable via::
370 $ echo 1 > /sys/block/zramX/writeback_limit_enable
372 Once writeback_limit_enable is set, zram doesn't allow any writeback
373 until admin sets the budget via /sys/block/zramX/writeback_limit.
375 (If admin doesn't enable writeback_limit_enable, writeback_limit's value
376 assigned via /sys/block/zramX/writeback_limit is meaningless.)
378 If admin wants to limit writeback as per-day 400M, they could do it
383 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
384 /sys/block/zram0/writeback_limit.
385 $ echo 1 > /sys/block/zram0/writeback_limit_enable
387 If admins want to allow further write again once the budget is exhausted,
388 they could do it like below::
390 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
391 /sys/block/zram0/writeback_limit
393 If an admin wants to see the remaining writeback budget since last set::
395 $ cat /sys/block/zramX/writeback_limit
397 If an admin wants to disable writeback limit, they could do::
399 $ echo 0 > /sys/block/zramX/writeback_limit_enable
401 The writeback_limit count will reset whenever you reset zram (e.g.,
402 system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
403 writeback happened until you reset the zram to allocate extra writeback
404 budget in next setting is user's job.
406 If admin wants to measure writeback count in a certain period, they could
407 know it via /sys/block/zram0/bd_stat's 3rd column.
412 With CONFIG_ZRAM_MULTI_COMP, zram can recompress pages using alternative
413 (secondary) compression algorithms. The basic idea is that alternative
414 compression algorithm can provide better compression ratio at a price of
415 (potentially) slower compression/decompression speeds. Alternative compression
416 algorithm can, for example, be more successful compressing huge pages (those
417 that default algorithm failed to compress). Another application is idle pages
418 recompression - pages that are cold and sit in the memory can be recompressed
419 using more effective algorithm and, hence, reduce zsmalloc memory usage.
421 With CONFIG_ZRAM_MULTI_COMP, zram supports up to 4 compression algorithms:
422 one primary and up to 3 secondary ones. Primary zram compressor is explained
423 in "3) Select compression algorithm", secondary algorithms are configured
424 using recomp_algorithm device attribute.
428 #show supported recompression algorithms
429 cat /sys/block/zramX/recomp_algorithm
430 #1: lzo lzo-rle lz4 lz4hc [zstd]
431 #2: lzo lzo-rle lz4 [lz4hc] zstd
433 Alternative compression algorithms are sorted by priority. In the example
434 above, zstd is used as the first alternative algorithm, which has priority
435 of 1, while lz4hc is configured as a compression algorithm with priority 2.
436 Alternative compression algorithm's priority is provided during algorithms
439 #select zstd recompression algorithm, priority 1
440 echo "algo=zstd priority=1" > /sys/block/zramX/recomp_algorithm
442 #select deflate recompression algorithm, priority 2
443 echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm
445 Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress,
446 which controls recompression.
450 #IDLE pages recompression is activated by `idle` mode
451 echo "type=idle" > /sys/block/zramX/recompress
453 #HUGE pages recompression is activated by `huge` mode
454 echo "type=huge" > /sys/block/zram0/recompress
456 #HUGE_IDLE pages recompression is activated by `huge_idle` mode
457 echo "type=huge_idle" > /sys/block/zramX/recompress
459 The number of idle pages can be significant, so user-space can pass a size
460 threshold (in bytes) to the recompress knob: zram will recompress only pages
461 of equal or greater size:::
463 #recompress all pages larger than 3000 bytes
464 echo "threshold=3000" > /sys/block/zramX/recompress
466 #recompress idle pages larger than 2000 bytes
467 echo "type=idle threshold=2000" > /sys/block/zramX/recompress
469 It is also possible to limit the number of pages zram re-compression will
470 attempt to recompress:::
472 echo "type=huge_idle max_pages=42" > /sys/block/zramX/recompress
474 Recompression of idle pages requires memory tracking.
476 During re-compression for every page, that matches re-compression criteria,
477 ZRAM iterates the list of registered alternative compression algorithms in
478 order of their priorities. ZRAM stops either when re-compression was
479 successful (re-compressed object is smaller in size than the original one)
480 and matches re-compression criteria (e.g. size threshold) or when there are
481 no secondary algorithms left to try. If none of the secondary algorithms can
482 successfully re-compressed the page such a page is marked as incompressible,
483 so ZRAM will not attempt to re-compress it in the future.
485 This re-compression behaviour, when it iterates through the list of
486 registered compression algorithms, increases our chances of finding the
487 algorithm that successfully compresses a particular page. Sometimes, however,
488 it is convenient (and sometimes even necessary) to limit recompression to
489 only one particular algorithm so that it will not try any other algorithms.
490 This can be achieved by providing a algo=NAME parameter:::
492 #use zstd algorithm only (if registered)
493 echo "type=huge algo=zstd" > /sys/block/zramX/recompress
498 With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
499 zram block. It could be useful to catch cold or incompressible
500 pages of the process with*pagemap.
502 If you enable the feature, you could see block state via
503 /sys/kernel/debug/zram/zram0/block_state". The output is as follows::
509 304 146.781902 ..hi.n
514 access time since the system was booted
521 written page to backing store
527 recompressed page (secondary compression algorithm)
529 none (including secondary) of algorithms could compress it
531 First line of above example says 300th block is accessed at 75.033841sec
532 and the block's state is huge so it is written back to the backing
533 storage. It's a debugging feature so anyone shouldn't rely on it to work