blob: 1072a267fc1020c343f4c25c9ba06f31857006b0 [file] [log] [blame]
Nitin Gupta00ac9ba2010-06-01 13:31:26 +05301zram: Compressed RAM based block devices
2----------------------------------------
Nitin Gupta47f9afb2009-09-22 10:26:54 +05303
Nitin Gupta47f9afb2009-09-22 10:26:54 +05304* Introduction
5
Nitin Gupta9b9913d2010-08-09 22:56:55 +05306The zram module creates RAM based block devices named /dev/zram<id>
7(<id> = 0, 1, ...). Pages written to these disks are compressed and stored
8in memory itself. These disks allow very fast I/O and compression provides
9good amounts of memory savings. Some of the usecases include /tmp storage,
10use as swap disks, various caches under /var and maybe many more :)
Nitin Gupta47f9afb2009-09-22 10:26:54 +053011
Nitin Gupta9b9913d2010-08-09 22:56:55 +053012Statistics for individual zram devices are exported through sysfs nodes at
13/sys/block/zram<id>/
Nitin Gupta47f9afb2009-09-22 10:26:54 +053014
15* Usage
16
Sergey SENOZHATSKY3657c202015-09-24 18:56:41 +090017There are several ways to configure and manage zram device(-s):
18a) using zram and zram_control sysfs attributes
19b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org).
20
21In this document we will describe only 'manual' zram configuration steps,
22IOW, zram and zram_control sysfs attributes.
23
24In order to get a better idea about zramctl please consult util-linux
25documentation, zramctl man-page or `zramctl --help'. Please be informed
26that zram maintainers do not develop/maintain util-linux or zramctl, should
27you have any questions please contact util-linux@vger.kernel.org
28
Nitin Gupta00ac9ba2010-06-01 13:31:26 +053029Following shows a typical sequence of steps for using zram.
Nitin Gupta47f9afb2009-09-22 10:26:54 +053030
Sergey SENOZHATSKY3657c202015-09-24 18:56:41 +090031WARNING
32=======
33For the sake of simplicity we skip error checking parts in most of the
34examples below. However, it is your sole responsibility to handle errors.
35
36zram sysfs attributes always return negative values in case of errors.
37The list of possible return codes:
38-EBUSY -- an attempt to modify an attribute that cannot be changed once
39the device has been initialised. Please reset device first;
40-ENOMEM -- zram was not able to allocate enough memory to fulfil your
41needs;
42-EINVAL -- invalid input has been provided.
43
44If you use 'echo', the returned value that is changed by 'echo' utility,
45and, 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
52should suffice.
53
Nitin Gupta9b9913d2010-08-09 22:56:55 +0530541) Load Module:
Nitin Gupta00ac9ba2010-06-01 13:31:26 +053055 modprobe zram num_devices=4
Nitin Gupta9b9913d2010-08-09 22:56:55 +053056 This creates 4 devices: /dev/zram{0,1,2,3}
Sergey Senozhatskyc3cdb402015-06-25 15:00:11 -070057
58num_devices parameter is optional and tells zram how many devices should be
59pre-created. Default: 1.
Nitin Gupta47f9afb2009-09-22 10:26:54 +053060
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700612) Set max number of compression streams
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070062Regardless the value passed to this attribute, ZRAM will always
63allocate multiple compression streams - one per online CPUs - thus
64allowing several concurrent compression operations. The number of
65allocated compression streams goes down when some of the CPUs
66become offline. There is no single-compression-stream mode anymore,
67unless you are running a UP system or has only 1 CPU online.
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -070068
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070069To find out how many streams are currently available:
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -070070 cat /sys/block/zram0/max_comp_streams
71
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700723) Select compression algorithm
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070073Using comp_algorithm device attribute one can see available and
74currently selected (shown in square brackets) compression algorithms,
75change selected compression algorithm (once the device is initialised
76there is no way to change compression algorithm).
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070077
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070078Examples:
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070079 #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
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070086For the time being, the `comp_algorithm' content does not necessarily
87show every compression algorithm supported by the kernel. We keep this
88list primarily to simplify device configuration and one can configure
89a new device with a compression algorithm that is not listed in
90`comp_algorithm'. The thing is that, internally, ZRAM uses Crypto API
91and, if some of the algorithms were built as modules, it's impossible
92to list all of them using, for instance, /proc/crypto or any other
93method. This, however, has an advantage of permitting the usage of
94custom crypto compression modules (implementing S/W or H/W compression).
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070095
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700964) Set Disksize
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -070097Set disk size by writing the value to sysfs node 'disksize'.
98The value can be either in bytes or you can use mem suffixes.
99Examples:
100 # Initialize /dev/zram0 with 50MB disksize
101 echo $((50*1024*1024)) > /sys/block/zram0/disksize
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530102
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -0700103 # Using mem suffixes
104 echo 256K > /sys/block/zram0/disksize
105 echo 512M > /sys/block/zram0/disksize
106 echo 1G > /sys/block/zram0/disksize
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530107
Sergey Senozhatskye64cd512014-04-07 15:38:07 -0700108Note:
109There is little point creating a zram of greater than twice the size of memory
110since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
111size of the disk when not in use so a huge zram is wasteful.
112
Minchan Kim9ada9da2014-10-09 15:29:53 -07001135) Set memory limit: Optional
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -0700114Set memory limit by writing the value to sysfs node 'mem_limit'.
115The value can be either in bytes or you can use mem suffixes.
116In addition, you could change the value in runtime.
117Examples:
118 # limit /dev/zram0 with 50MB memory
119 echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
Minchan Kim9ada9da2014-10-09 15:29:53 -0700120
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -0700121 # Using mem suffixes
122 echo 256K > /sys/block/zram0/mem_limit
123 echo 512M > /sys/block/zram0/mem_limit
124 echo 1G > /sys/block/zram0/mem_limit
Minchan Kim9ada9da2014-10-09 15:29:53 -0700125
Sergey Senozhatsky69a30a82016-07-26 15:22:51 -0700126 # To disable memory limit
127 echo 0 > /sys/block/zram0/mem_limit
Minchan Kim9ada9da2014-10-09 15:29:53 -0700128
1296) Activate:
Nitin Gupta00ac9ba2010-06-01 13:31:26 +0530130 mkswap /dev/zram0
131 swapon /dev/zram0
132
133 mkfs.ext4 /dev/zram1
134 mount /dev/zram1 /tmp
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530135
Sergey Senozhatsky6566d1a2015-06-25 15:00:24 -07001367) Add/remove zram devices
137
138zram provides a control interface, which enables dynamic (on-demand) device
139addition and removal.
140
141In order to add a new /dev/zramX device, perform read operation on hot_add
142attribute. This will return either new device's device id (meaning that you
143can use /dev/zram<id>) or error code.
144
145Example:
146 cat /sys/class/zram-control/hot_add
147 1
148
149To remove the existing /dev/zramX device (where X is a device id)
150execute
151 echo X > /sys/class/zram-control/hot_remove
152
1538) Stats:
Sergey Senozhatsky77ba0152015-04-15 16:16:00 -0700154Per-device statistics are exported as various nodes under /sys/block/zram<id>/
155
Sergey SENOZHATSKY3657c202015-09-24 18:56:41 +0900156A brief description of exported device attributes. For more details please
Sergey Senozhatsky77ba0152015-04-15 16:16:00 -0700157read Documentation/ABI/testing/sysfs-block-zram.
158
159Name access description
160---- ------ -----------
161disksize RW show and set the device's disk size
162initstate RO shows the initialization state of the device
163reset WO trigger device reset
Sergey Senozhatskyf29eb692017-02-22 15:46:45 -0800164mem_used_max WO reset the `mem_used_max' counter (see later)
165mem_limit WO specifies the maximum amount of memory ZRAM can use
166 to store the compressed data
Minchan Kim2cf97fa2018-12-28 00:36:54 -0800167writeback_limit WO specifies the maximum amount of write IO zram can
168 write out to backing device as 4KB unit
Sergey Senozhatsky77ba0152015-04-15 16:16:00 -0700169max_comp_streams RW the number of possible concurrent compress operations
170comp_algorithm RW show and change the compression algorithm
Sergey Senozhatsky3d8ed882015-06-25 15:00:00 -0700171compact WO trigger memory compaction
Sergey Senozhatsky623e47f2016-05-20 17:00:02 -0700172debug_stat RO this file is used for zram debugging purposes
Minchan Kimbc6f4ed2017-09-06 16:20:10 -0700173backing_dev RW set up backend storage for zram to write out
Minchan Kim149be472018-12-28 00:36:44 -0800174idle WO mark allocated slot as idle
Sergey Senozhatsky77ba0152015-04-15 16:16:00 -0700175
Sergey Senozhatsky8f7d2822015-04-15 16:16:09 -0700176
177User space is advised to use the following files to read the device statistics.
178
Sergey Senozhatsky77ba0152015-04-15 16:16:00 -0700179File /sys/block/zram<id>/stat
180
181Represents block layer statistics. Read Documentation/block/stat.txt for
182details.
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530183
Sergey Senozhatsky2f6a3be2015-04-15 16:16:03 -0700184File /sys/block/zram<id>/io_stat
185
186The stat file represents device's I/O statistics not accounted by block
187layer and, thus, not available in zram<id>/stat file. It consists of a
188single line of text and contains the following stats separated by
189whitespace:
Sergey Senozhatskyf29eb692017-02-22 15:46:45 -0800190 failed_reads the number of failed reads
191 failed_writes the number of failed writes
192 invalid_io the number of non-page-size-aligned I/O requests
193 notify_free Depending on device usage scenario it may account
194 a) the number of pages freed because of swap slot free
195 notifications or b) the number of pages freed because of
196 REQ_DISCARD requests sent by bio. The former ones are
197 sent to a swap block device when a swap slot is freed,
198 which implies that this disk is being used as a swap disk.
199 The latter ones are sent by filesystem mounted with
200 discard option, whenever some data blocks are getting
201 discarded.
Sergey Senozhatsky2f6a3be2015-04-15 16:16:03 -0700202
Sergey Senozhatsky4f2109f2015-04-15 16:16:06 -0700203File /sys/block/zram<id>/mm_stat
204
205The stat file represents device's mm statistics. It consists of a single
206line of text and contains the following stats separated by whitespace:
Sergey Senozhatskyf29eb692017-02-22 15:46:45 -0800207 orig_data_size uncompressed size of data stored in this disk.
zhouxianrong74ccaa72017-02-24 14:59:27 -0800208 This excludes same-element-filled pages (same_pages) since
209 no memory is allocated for them.
Sergey Senozhatskyf29eb692017-02-22 15:46:45 -0800210 Unit: bytes
211 compr_data_size compressed size of data stored in this disk
212 mem_used_total the amount of memory allocated for this disk. This
213 includes allocator fragmentation and metadata overhead,
214 allocated for this disk. So, allocator space efficiency
215 can be calculated using compr_data_size and this statistic.
216 Unit: bytes
217 mem_limit the maximum amount of memory ZRAM can use to store
218 the compressed data
219 mem_used_max the maximum amount of memory zram have consumed to
220 store the data
zhouxianrong74ccaa72017-02-24 14:59:27 -0800221 same_pages the number of same element filled pages written to this disk.
Sergey Senozhatskyf29eb692017-02-22 15:46:45 -0800222 No memory is allocated for such pages.
223 pages_compacted the number of pages freed during compaction
Minchan Kimf185f712018-06-07 17:05:42 -0700224 huge_pages the number of incompressible pages
Sergey Senozhatsky4f2109f2015-04-15 16:16:06 -0700225
Minchan Kime1dd5d12018-12-28 00:36:51 -0800226File /sys/block/zram<id>/bd_stat
227
228The stat file represents device's backing device statistics. It consists of
229a single line of text and contains the following stats separated by whitespace:
230 bd_count size of data written in backing device.
231 Unit: 4K bytes
232 bd_reads the number of reads from backing device
233 Unit: 4K bytes
234 bd_writes the number of writes to backing device
235 Unit: 4K bytes
236
Sergey Senozhatsky6566d1a2015-06-25 15:00:24 -07002379) Deactivate:
Nitin Gupta00ac9ba2010-06-01 13:31:26 +0530238 swapoff /dev/zram0
239 umount /dev/zram1
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530240
Sergey Senozhatsky6566d1a2015-06-25 15:00:24 -070024110) Reset:
Nitin Gupta9b9913d2010-08-09 22:56:55 +0530242 Write any positive value to 'reset' sysfs node
243 echo 1 > /sys/block/zram0/reset
244 echo 1 > /sys/block/zram1/reset
245
Minchan Kim0231c402013-01-30 11:41:40 +0900246 This frees all the memory allocated for the given device and
247 resets the disksize to zero. You must set the disksize again
248 before reusing the device.
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530249
Minchan Kimbc6f4ed2017-09-06 16:20:10 -0700250* Optional Feature
251
252= writeback
253
Minchan Kim86d820b2018-12-28 00:36:47 -0800254With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page
Minchan Kimbc6f4ed2017-09-06 16:20:10 -0700255to backing storage rather than keeping it in memory.
Minchan Kim86d820b2018-12-28 00:36:47 -0800256To use the feature, admin should set up backing device via
257
258 "echo /dev/sda5 > /sys/block/zramX/backing_dev"
259
260before disksize setting. It supports only partition at this moment.
261If admin want to use incompressible page writeback, they could do via
262
263 "echo huge > /sys/block/zramX/write"
264
265To use idle page writeback, first, user need to declare zram pages
266as idle.
267
268 "echo all > /sys/block/zramX/idle"
269
270From now on, any pages on zram are idle pages. The idle mark
271will be removed until someone request access of the block.
272IOW, unless there is access request, those pages are still idle pages.
273
274Admin can request writeback of those idle pages at right timing via
275
276 "echo idle > /sys/block/zramX/writeback"
277
278With the command, zram writeback idle pages from memory to the storage.
Minchan Kimbc6f4ed2017-09-06 16:20:10 -0700279
Minchan Kim2cf97fa2018-12-28 00:36:54 -0800280If there are lots of write IO with flash device, potentially, it has
281flash wearout problem so that admin needs to design write limitation
282to guarantee storage health for entire product life.
283To overcome the concern, zram supports "writeback_limit".
284The "writeback_limit"'s default value is 0 so that it doesn't limit
285any writeback. If admin want to measure writeback count in a certain
286period, he could know it via /sys/block/zram0/bd_stat's 3rd column.
287
288If admin want to limit writeback as per-day 400M, he could do it
289like below.
290
291 MB_SHIFT=20
292 4K_SHIFT=12
293 echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
294 /sys/block/zram0/writeback_limit.
295
296If admin want to allow further write again, he could do it like below
297
298 echo 0 > /sys/block/zram0/writeback_limit
299
300If admin want to see remaining writeback budget since he set,
301
302 cat /sys/block/zram0/writeback_limit
303
304The writeback_limit count will reset whenever you reset zram(e.g.,
305system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
306writeback happened until you reset the zram to allocate extra writeback
307budget in next setting is user's job.
308
Minchan Kimf1dcb852018-06-07 17:05:49 -0700309= memory tracking
310
311With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
312zram block. It could be useful to catch cold or incompressible
313pages of the process with*pagemap.
314If you enable the feature, you could see block state via
315/sys/kernel/debug/zram/zram0/block_state". The output is as follows,
316
Minchan Kim149be472018-12-28 00:36:44 -0800317 300 75.033841 .wh.
318 301 63.806904 s...
319 302 63.806919 ..hi
Minchan Kimf1dcb852018-06-07 17:05:49 -0700320
321First column is zram's block index.
322Second column is access time since the system was booted
323Third column is state of the block.
324(s: same page
325w: written page to backing store
Minchan Kim149be472018-12-28 00:36:44 -0800326h: huge page
327i: idle page)
Minchan Kimf1dcb852018-06-07 17:05:49 -0700328
329First line of above example says 300th block is accessed at 75.033841sec
330and the block's state is huge so it is written back to the backing
331storage. It's a debugging feature so anyone shouldn't rely on it to work
332properly.
333
Nitin Gupta47f9afb2009-09-22 10:26:54 +0530334Nitin Gupta
335ngupta@vflare.org