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 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 24 | 2) Set Disksize |
| 25 | Set disk size by writing the value to sysfs node 'disksize'. |
| 26 | The value can be either in bytes or you can use mem suffixes. |
| 27 | Examples: |
| 28 | # Initialize /dev/zram0 with 50MB disksize |
| 29 | echo $((50*1024*1024)) > /sys/block/zram0/disksize |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 30 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 31 | # Using mem suffixes |
| 32 | echo 256K > /sys/block/zram0/disksize |
| 33 | echo 512M > /sys/block/zram0/disksize |
| 34 | echo 1G > /sys/block/zram0/disksize |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 35 | |
Sergey Senozhatsky | e64cd51 | 2014-04-07 15:38:07 -0700 | [diff] [blame] | 36 | Note: |
| 37 | There is little point creating a zram of greater than twice the size of memory |
| 38 | since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the |
| 39 | size of the disk when not in use so a huge zram is wasteful. |
| 40 | |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 41 | 3) Activate: |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 42 | mkswap /dev/zram0 |
| 43 | swapon /dev/zram0 |
| 44 | |
| 45 | mkfs.ext4 /dev/zram1 |
| 46 | mount /dev/zram1 /tmp |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 47 | |
| 48 | 4) Stats: |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 49 | Per-device statistics are exported as various nodes under |
| 50 | /sys/block/zram<id>/ |
| 51 | disksize |
| 52 | num_reads |
| 53 | num_writes |
Sergey Senozhatsky | 8dd1d32 | 2014-04-07 15:38:08 -0700 | [diff] [blame^] | 54 | failed_reads |
| 55 | failed_writes |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 56 | invalid_io |
| 57 | notify_free |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 58 | zero_pages |
| 59 | orig_data_size |
| 60 | compr_data_size |
| 61 | mem_used_total |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 62 | |
| 63 | 5) Deactivate: |
Nitin Gupta | 00ac9ba | 2010-06-01 13:31:26 +0530 | [diff] [blame] | 64 | swapoff /dev/zram0 |
| 65 | umount /dev/zram1 |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 66 | |
| 67 | 6) Reset: |
Nitin Gupta | 9b9913d | 2010-08-09 22:56:55 +0530 | [diff] [blame] | 68 | Write any positive value to 'reset' sysfs node |
| 69 | echo 1 > /sys/block/zram0/reset |
| 70 | echo 1 > /sys/block/zram1/reset |
| 71 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 72 | This frees all the memory allocated for the given device and |
| 73 | resets the disksize to zero. You must set the disksize again |
| 74 | before reusing the device. |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 75 | |
Nitin Gupta | 47f9afb | 2009-09-22 10:26:54 +0530 | [diff] [blame] | 76 | Nitin Gupta |
| 77 | ngupta@vflare.org |