1zram: Compressed RAM based block devices
2----------------------------------------
3
4* Introduction
5
6The 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 :)
11
12Statistics for individual zram devices are exported through sysfs nodes at
13/sys/block/zram<id>/
14
15* Usage
16
17There 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
29Following shows a typical sequence of steps for using zram.
30
31WARNING
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
541) Load Module:
55	modprobe zram num_devices=4
56	This creates 4 devices: /dev/zram{0,1,2,3}
57
58num_devices parameter is optional and tells zram how many devices should be
59pre-created. Default: 1.
60
612) Set max number of compression streams
62Regardless 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.
68
69To find out how many streams are currently available:
70	cat /sys/block/zram0/max_comp_streams
71
723) Select compression algorithm
73Using 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).
77
78Examples:
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
86For 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).
95
964) Set Disksize
97Set 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
102
103	# Using mem suffixes
104	echo 256K > /sys/block/zram0/disksize
105	echo 512M > /sys/block/zram0/disksize
106	echo 1G > /sys/block/zram0/disksize
107
108Note:
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
1135) Set memory limit: Optional
114Set 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
120
121	# 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
125
126	# To disable memory limit
127	echo 0 > /sys/block/zram0/mem_limit
128
1296) Activate:
130	mkswap /dev/zram0
131	swapon /dev/zram0
132
133	mkfs.ext4 /dev/zram1
134	mount /dev/zram1 /tmp
135
1367) 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:
154Per-device statistics are exported as various nodes under /sys/block/zram<id>/
155
156A brief description of exported device attributes. For more details please
157read 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
164mem_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
167max_comp_streams  RW    the number of possible concurrent compress operations
168comp_algorithm    RW    show and change the compression algorithm
169compact           WO    trigger memory compaction
170debug_stat        RO    this file is used for zram debugging purposes
171backing_dev	  RW	set up backend storage for zram to write out
172
173
174User space is advised to use the following files to read the device statistics.
175
176File /sys/block/zram<id>/stat
177
178Represents block layer statistics. Read Documentation/block/stat.txt for
179details.
180
181File /sys/block/zram<id>/io_stat
182
183The stat file represents device's I/O statistics not accounted by block
184layer and, thus, not available in zram<id>/stat file. It consists of a
185single line of text and contains the following stats separated by
186whitespace:
187 failed_reads     the number of failed reads
188 failed_writes    the number of failed writes
189 invalid_io       the number of non-page-size-aligned I/O requests
190 notify_free      Depending on device usage scenario it may account
191                  a) the number of pages freed because of swap slot free
192                  notifications or b) the number of pages freed because of
193                  REQ_DISCARD requests sent by bio. The former ones are
194                  sent to a swap block device when a swap slot is freed,
195                  which implies that this disk is being used as a swap disk.
196                  The latter ones are sent by filesystem mounted with
197                  discard option, whenever some data blocks are getting
198                  discarded.
199
200File /sys/block/zram<id>/mm_stat
201
202The stat file represents device's mm statistics. It consists of a single
203line of text and contains the following stats separated by whitespace:
204 orig_data_size   uncompressed size of data stored in this disk.
205		  This excludes same-element-filled pages (same_pages) since
206		  no memory is allocated for them.
207                  Unit: bytes
208 compr_data_size  compressed size of data stored in this disk
209 mem_used_total   the amount of memory allocated for this disk. This
210                  includes allocator fragmentation and metadata overhead,
211                  allocated for this disk. So, allocator space efficiency
212                  can be calculated using compr_data_size and this statistic.
213                  Unit: bytes
214 mem_limit        the maximum amount of memory ZRAM can use to store
215                  the compressed data
216 mem_used_max     the maximum amount of memory zram have consumed to
217                  store the data
218 same_pages       the number of same element filled pages written to this disk.
219                  No memory is allocated for such pages.
220 pages_compacted  the number of pages freed during compaction
221 huge_pages	  the number of incompressible pages
222
2239) Deactivate:
224	swapoff /dev/zram0
225	umount /dev/zram1
226
22710) Reset:
228	Write any positive value to 'reset' sysfs node
229	echo 1 > /sys/block/zram0/reset
230	echo 1 > /sys/block/zram1/reset
231
232	This frees all the memory allocated for the given device and
233	resets the disksize to zero. You must set the disksize again
234	before reusing the device.
235
236* Optional Feature
237
238= writeback
239
240With incompressible pages, there is no memory saving with zram.
241Instead, with CONFIG_ZRAM_WRITEBACK, zram can write incompressible page
242to backing storage rather than keeping it in memory.
243User should set up backing device via /sys/block/zramX/backing_dev
244before disksize setting.
245
246= memory tracking
247
248With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
249zram block. It could be useful to catch cold or incompressible
250pages of the process with*pagemap.
251If you enable the feature, you could see block state via
252/sys/kernel/debug/zram/zram0/block_state". The output is as follows,
253
254	  300    75.033841 .wh
255	  301    63.806904 s..
256	  302    63.806919 ..h
257
258First column is zram's block index.
259Second column is access time since the system was booted
260Third column is state of the block.
261(s: same page
262w: written page to backing store
263h: huge page)
264
265First line of above example says 300th block is accessed at 75.033841sec
266and the block's state is huge so it is written back to the backing
267storage. It's a debugging feature so anyone shouldn't rely on it to work
268properly.
269
270Nitin Gupta
271ngupta@vflare.org
272