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