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