zram.txt 7.3 KB

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