zram.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. zram: Compressed RAM based block devices
  2. ----------------------------------------
  3. * Introduction
  4. The zram module creates RAM based block devices named /dev/zram<id>
  5. (<id> = 0, 1, ...). Pages written to these disks are compressed and stored
  6. in memory itself. These disks allow very fast I/O and compression provides
  7. good amounts of memory savings. Some of the usecases include /tmp storage,
  8. use as swap disks, various caches under /var and maybe many more :)
  9. Statistics for individual zram devices are exported through sysfs nodes at
  10. /sys/block/zram<id>/
  11. * Usage
  12. Following shows a typical sequence of steps for using zram.
  13. 1) Load Module:
  14. modprobe zram num_devices=4
  15. This creates 4 devices: /dev/zram{0,1,2,3}
  16. (num_devices parameter is optional. Default: 1)
  17. 2) Set max number of compression streams
  18. Compression backend may use up to max_comp_streams compression streams,
  19. thus allowing up to max_comp_streams concurrent compression operations.
  20. By default, compression backend uses single compression stream.
  21. Examples:
  22. #show max compression streams number
  23. cat /sys/block/zram0/max_comp_streams
  24. #set max compression streams number to 3
  25. echo 3 > /sys/block/zram0/max_comp_streams
  26. Note:
  27. In order to enable compression backend's multi stream support max_comp_streams
  28. must be initially set to desired concurrency level before ZRAM device
  29. initialisation. Once the device initialised as a single stream compression
  30. backend (max_comp_streams equals to 1), you will see error if you try to change
  31. the value of max_comp_streams because single stream compression backend
  32. implemented as a special case by lock overhead issue and does not support
  33. dynamic max_comp_streams. Only multi stream backend supports dynamic
  34. max_comp_streams adjustment.
  35. 3) Select compression algorithm
  36. Using comp_algorithm device attribute one can see available and
  37. currently selected (shown in square brackets) compression algortithms,
  38. change selected compression algorithm (once the device is initialised
  39. there is no way to change compression algorithm).
  40. Examples:
  41. #show supported compression algorithms
  42. cat /sys/block/zram0/comp_algorithm
  43. lzo [lz4]
  44. #select lzo compression algorithm
  45. echo lzo > /sys/block/zram0/comp_algorithm
  46. 4) Set Disksize
  47. Set disk size by writing the value to sysfs node 'disksize'.
  48. The value can be either in bytes or you can use mem suffixes.
  49. Examples:
  50. # Initialize /dev/zram0 with 50MB disksize
  51. echo $((50*1024*1024)) > /sys/block/zram0/disksize
  52. # Using mem suffixes
  53. echo 256K > /sys/block/zram0/disksize
  54. echo 512M > /sys/block/zram0/disksize
  55. echo 1G > /sys/block/zram0/disksize
  56. Note:
  57. There is little point creating a zram of greater than twice the size of memory
  58. since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
  59. size of the disk when not in use so a huge zram is wasteful.
  60. 5) Set memory limit: Optional
  61. Set memory limit by writing the value to sysfs node 'mem_limit'.
  62. The value can be either in bytes or you can use mem suffixes.
  63. In addition, you could change the value in runtime.
  64. Examples:
  65. # limit /dev/zram0 with 50MB memory
  66. echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
  67. # Using mem suffixes
  68. echo 256K > /sys/block/zram0/mem_limit
  69. echo 512M > /sys/block/zram0/mem_limit
  70. echo 1G > /sys/block/zram0/mem_limit
  71. # To disable memory limit
  72. echo 0 > /sys/block/zram0/mem_limit
  73. 6) Activate:
  74. mkswap /dev/zram0
  75. swapon /dev/zram0
  76. mkfs.ext4 /dev/zram1
  77. mount /dev/zram1 /tmp
  78. 7) Stats:
  79. Per-device statistics are exported as various nodes under
  80. /sys/block/zram<id>/
  81. disksize
  82. num_reads
  83. num_writes
  84. failed_reads
  85. failed_writes
  86. invalid_io
  87. notify_free
  88. zero_pages
  89. orig_data_size
  90. compr_data_size
  91. mem_used_total
  92. mem_used_max
  93. 8) Deactivate:
  94. swapoff /dev/zram0
  95. umount /dev/zram1
  96. 9) Reset:
  97. Write any positive value to 'reset' sysfs node
  98. echo 1 > /sys/block/zram0/reset
  99. echo 1 > /sys/block/zram1/reset
  100. This frees all the memory allocated for the given device and
  101. resets the disksize to zero. You must set the disksize again
  102. before reusing the device.
  103. Nitin Gupta
  104. ngupta@vflare.org