Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 1 | zram: Compressed RAM based block devices |
| 2 | ---------------------------------------- |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 3 | |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 4 | * Introduction |
| 5 | |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 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 :) |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 11 | |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 12 | Statistics for individual zram devices are exported through sysfs nodes at |
| 13 | /sys/block/zram<id>/ |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 14 | |
| 15 | * Usage |
| 16 | |
Sergey SENOZHATSKY | 3657c20 | 2015-09-24 18:56:41 +0900 | [diff] [blame] | 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 | |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 29 | Following shows a typical sequence of steps for using zram. |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 30 | |
Sergey SENOZHATSKY | 3657c20 | 2015-09-24 18:56:41 +0900 | [diff] [blame] | 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 | |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 54 | 1) Load Module: |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 55 | modprobe zram num_devices=4 |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 56 | This creates 4 devices: /dev/zram{0,1,2,3} |
Sergey Senozhatsky | c3cdb40 | 2015-06-25 15:00:11 -0700 | [diff] [blame] | 57 | |
| 58 | num_devices parameter is optional and tells zram how many devices should be |
| 59 | pre-created. Default: 1. |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 60 | |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 61 | 2) Set max number of compression streams |
| 62 | Compression backend may use up to max_comp_streams compression streams, |
| 63 | thus allowing up to max_comp_streams concurrent compression operations. |
| 64 | By default, compression backend uses single compression stream. |
| 65 | |
| 66 | Examples: |
| 67 | #show max compression streams number |
| 68 | cat /sys/block/zram0/max_comp_streams |
| 69 | |
| 70 | #set max compression streams number to 3 |
| 71 | echo 3 > /sys/block/zram0/max_comp_streams |
| 72 | |
| 73 | Note: |
| 74 | In order to enable compression backend's multi stream support max_comp_streams |
| 75 | must be initially set to desired concurrency level before ZRAM device |
| 76 | initialisation. Once the device initialised as a single stream compression |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 77 | backend (max_comp_streams equals to 1), you will see error if you try to change |
| 78 | the value of max_comp_streams because single stream compression backend |
| 79 | implemented as a special case by lock overhead issue and does not support |
| 80 | dynamic max_comp_streams. Only multi stream backend supports dynamic |
| 81 | max_comp_streams adjustment. |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 82 | |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 83 | 3) Select compression algorithm |
| 84 | Using comp_algorithm device attribute one can see available and |
Sergey SENOZHATSKY | 3657c20 | 2015-09-24 18:56:41 +0900 | [diff] [blame] | 85 | currently selected (shown in square brackets) compression algorithms, |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 86 | change selected compression algorithm (once the device is initialised |
| 87 | there is no way to change compression algorithm). |
| 88 | |
| 89 | Examples: |
| 90 | #show supported compression algorithms |
| 91 | cat /sys/block/zram0/comp_algorithm |
| 92 | lzo [lz4] |
| 93 | |
| 94 | #select lzo compression algorithm |
| 95 | echo lzo > /sys/block/zram0/comp_algorithm |
| 96 | |
| 97 | 4) Set Disksize |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 98 | Set disk size by writing the value to sysfs node 'disksize'. |
| 99 | The value can be either in bytes or you can use mem suffixes. |
| 100 | Examples: |
| 101 | # Initialize /dev/zram0 with 50MB disksize |
| 102 | echo $((50*1024*1024)) > /sys/block/zram0/disksize |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 103 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 104 | # Using mem suffixes |
| 105 | echo 256K > /sys/block/zram0/disksize |
| 106 | echo 512M > /sys/block/zram0/disksize |
| 107 | echo 1G > /sys/block/zram0/disksize |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 108 | |
Sergey Senozhatsky | e64cd51 | 2014-04-07 15:38:07 -0700 | [diff] [blame] | 109 | Note: |
| 110 | There is little point creating a zram of greater than twice the size of memory |
| 111 | since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the |
| 112 | size of the disk when not in use so a huge zram is wasteful. |
| 113 | |
Minchan Kim | 9ada9da | 2014-10-09 15:29:53 -0700 | [diff] [blame] | 114 | 5) Set memory limit: Optional |
| 115 | Set memory limit by writing the value to sysfs node 'mem_limit'. |
| 116 | The value can be either in bytes or you can use mem suffixes. |
| 117 | In addition, you could change the value in runtime. |
| 118 | Examples: |
| 119 | # limit /dev/zram0 with 50MB memory |
| 120 | echo $((50*1024*1024)) > /sys/block/zram0/mem_limit |
| 121 | |
| 122 | # Using mem suffixes |
| 123 | echo 256K > /sys/block/zram0/mem_limit |
| 124 | echo 512M > /sys/block/zram0/mem_limit |
| 125 | echo 1G > /sys/block/zram0/mem_limit |
| 126 | |
| 127 | # To disable memory limit |
| 128 | echo 0 > /sys/block/zram0/mem_limit |
| 129 | |
| 130 | 6) Activate: |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 131 | mkswap /dev/zram0 |
| 132 | swapon /dev/zram0 |
| 133 | |
| 134 | mkfs.ext4 /dev/zram1 |
| 135 | mount /dev/zram1 /tmp |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 136 | |
Sergey Senozhatsky | 6566d1a | 2015-06-25 15:00:24 -0700 | [diff] [blame] | 137 | 7) Add/remove zram devices |
| 138 | |
| 139 | zram provides a control interface, which enables dynamic (on-demand) device |
| 140 | addition and removal. |
| 141 | |
| 142 | In order to add a new /dev/zramX device, perform read operation on hot_add |
| 143 | attribute. This will return either new device's device id (meaning that you |
| 144 | can use /dev/zram<id>) or error code. |
| 145 | |
| 146 | Example: |
| 147 | cat /sys/class/zram-control/hot_add |
| 148 | 1 |
| 149 | |
| 150 | To remove the existing /dev/zramX device (where X is a device id) |
| 151 | execute |
| 152 | echo X > /sys/class/zram-control/hot_remove |
| 153 | |
| 154 | 8) Stats: |
Sergey Senozhatsky | 77ba015 | 2015-04-15 16:16:00 -0700 | [diff] [blame] | 155 | Per-device statistics are exported as various nodes under /sys/block/zram<id>/ |
| 156 | |
Sergey SENOZHATSKY | 3657c20 | 2015-09-24 18:56:41 +0900 | [diff] [blame] | 157 | A brief description of exported device attributes. For more details please |
Sergey Senozhatsky | 77ba015 | 2015-04-15 16:16:00 -0700 | [diff] [blame] | 158 | read Documentation/ABI/testing/sysfs-block-zram. |
| 159 | |
| 160 | Name access description |
| 161 | ---- ------ ----------- |
| 162 | disksize RW show and set the device's disk size |
| 163 | initstate RO shows the initialization state of the device |
| 164 | reset WO trigger device reset |
| 165 | num_reads RO the number of reads |
| 166 | failed_reads RO the number of failed reads |
| 167 | num_write RO the number of writes |
| 168 | failed_writes RO the number of failed writes |
| 169 | invalid_io RO the number of non-page-size-aligned I/O requests |
| 170 | max_comp_streams RW the number of possible concurrent compress operations |
| 171 | comp_algorithm RW show and change the compression algorithm |
| 172 | notify_free RO the number of notifications to free pages (either |
| 173 | slot free notifications or REQ_DISCARD requests) |
| 174 | zero_pages RO the number of zero filled pages written to this disk |
| 175 | orig_data_size RO uncompressed size of data stored in this disk |
| 176 | compr_data_size RO compressed size of data stored in this disk |
| 177 | mem_used_total RO the amount of memory allocated for this disk |
Sergey SENOZHATSKY | 3657c20 | 2015-09-24 18:56:41 +0900 | [diff] [blame] | 178 | mem_used_max RW the maximum amount of memory zram have consumed to |
| 179 | store the data (to reset this counter to the actual |
| 180 | current value, write 1 to this attribute) |
Sergey Senozhatsky | 77ba015 | 2015-04-15 16:16:00 -0700 | [diff] [blame] | 181 | mem_limit RW the maximum amount of memory ZRAM can use to store |
| 182 | the compressed data |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 183 | pages_compacted RO the number of pages freed during compaction |
| 184 | (available only via zram<id>/mm_stat node) |
Sergey Senozhatsky | 3d8ed88 | 2015-06-25 15:00:00 -0700 | [diff] [blame] | 185 | compact WO trigger memory compaction |
Sergey Senozhatsky | 77ba015 | 2015-04-15 16:16:00 -0700 | [diff] [blame] | 186 | |
Sergey Senozhatsky | 8f7d282 | 2015-04-15 16:16:09 -0700 | [diff] [blame] | 187 | WARNING |
| 188 | ======= |
| 189 | per-stat sysfs attributes are considered to be deprecated. |
| 190 | The basic strategy is: |
| 191 | -- the existing RW nodes will be downgraded to WO nodes (in linux 4.11) |
| 192 | -- deprecated RO sysfs nodes will eventually be removed (in linux 4.11) |
| 193 | |
| 194 | The list of deprecated attributes can be found here: |
| 195 | Documentation/ABI/obsolete/sysfs-block-zram |
| 196 | |
| 197 | Basically, every attribute that has its own read accessible sysfs node |
| 198 | (e.g. num_reads) *AND* is accessible via one of the stat files (zram<id>/stat |
| 199 | or zram<id>/io_stat or zram<id>/mm_stat) is considered to be deprecated. |
| 200 | |
| 201 | User space is advised to use the following files to read the device statistics. |
| 202 | |
Sergey Senozhatsky | 77ba015 | 2015-04-15 16:16:00 -0700 | [diff] [blame] | 203 | File /sys/block/zram<id>/stat |
| 204 | |
| 205 | Represents block layer statistics. Read Documentation/block/stat.txt for |
| 206 | details. |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 207 | |
Sergey Senozhatsky | 2f6a3be | 2015-04-15 16:16:03 -0700 | [diff] [blame] | 208 | File /sys/block/zram<id>/io_stat |
| 209 | |
| 210 | The stat file represents device's I/O statistics not accounted by block |
| 211 | layer and, thus, not available in zram<id>/stat file. It consists of a |
| 212 | single line of text and contains the following stats separated by |
| 213 | whitespace: |
| 214 | failed_reads |
| 215 | failed_writes |
| 216 | invalid_io |
| 217 | notify_free |
| 218 | |
Sergey Senozhatsky | 4f2109f | 2015-04-15 16:16:06 -0700 | [diff] [blame] | 219 | File /sys/block/zram<id>/mm_stat |
| 220 | |
| 221 | The stat file represents device's mm statistics. It consists of a single |
| 222 | line of text and contains the following stats separated by whitespace: |
| 223 | orig_data_size |
| 224 | compr_data_size |
| 225 | mem_used_total |
| 226 | mem_limit |
| 227 | mem_used_max |
| 228 | zero_pages |
| 229 | num_migrated |
| 230 | |
Sergey Senozhatsky | 6566d1a | 2015-06-25 15:00:24 -0700 | [diff] [blame] | 231 | 9) Deactivate: |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 232 | swapoff /dev/zram0 |
| 233 | umount /dev/zram1 |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 234 | |
Sergey Senozhatsky | 6566d1a | 2015-06-25 15:00:24 -0700 | [diff] [blame] | 235 | 10) Reset: |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 236 | Write any positive value to 'reset' sysfs node |
| 237 | echo 1 > /sys/block/zram0/reset |
| 238 | echo 1 > /sys/block/zram1/reset |
| 239 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 240 | This frees all the memory allocated for the given device and |
| 241 | resets the disksize to zero. You must set the disksize again |
| 242 | before reusing the device. |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 243 | |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 244 | Nitin Gupta |
| 245 | ngupta@vflare.org |