zram.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. There are several ways to configure and manage zram device(-s):
  13. a) using zram and zram_control sysfs attributes
  14. b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org).
  15. In this document we will describe only 'manual' zram configuration steps,
  16. IOW, zram and zram_control sysfs attributes.
  17. In order to get a better idea about zramctl please consult util-linux
  18. documentation, zramctl man-page or `zramctl --help'. Please be informed
  19. that zram maintainers do not develop/maintain util-linux or zramctl, should
  20. you have any questions please contact util-linux@vger.kernel.org
  21. Following shows a typical sequence of steps for using zram.
  22. WARNING
  23. =======
  24. For the sake of simplicity we skip error checking parts in most of the
  25. examples below. However, it is your sole responsibility to handle errors.
  26. zram sysfs attributes always return negative values in case of errors.
  27. The list of possible return codes:
  28. -EBUSY -- an attempt to modify an attribute that cannot be changed once
  29. the device has been initialised. Please reset device first;
  30. -ENOMEM -- zram was not able to allocate enough memory to fulfil your
  31. needs;
  32. -EINVAL -- invalid input has been provided.
  33. If you use 'echo', the returned value that is changed by 'echo' utility,
  34. and, in general case, something like:
  35. echo 3 > /sys/block/zram0/max_comp_streams
  36. if [ $? -ne 0 ];
  37. handle_error
  38. fi
  39. should suffice.
  40. 1) Load Module:
  41. modprobe zram num_devices=4
  42. This creates 4 devices: /dev/zram{0,1,2,3}
  43. num_devices parameter is optional and tells zram how many devices should be
  44. pre-created. Default: 1.
  45. 2) Set max number of compression streams
  46. Compression backend may use up to max_comp_streams compression streams,
  47. thus allowing up to max_comp_streams concurrent compression operations.
  48. By default, compression backend uses single compression stream.
  49. Examples:
  50. #show max compression streams number
  51. cat /sys/block/zram0/max_comp_streams
  52. #set max compression streams number to 3
  53. echo 3 > /sys/block/zram0/max_comp_streams
  54. Note:
  55. In order to enable compression backend's multi stream support max_comp_streams
  56. must be initially set to desired concurrency level before ZRAM device
  57. initialisation. Once the device initialised as a single stream compression
  58. backend (max_comp_streams equals to 1), you will see error if you try to change
  59. the value of max_comp_streams because single stream compression backend
  60. implemented as a special case by lock overhead issue and does not support
  61. dynamic max_comp_streams. Only multi stream backend supports dynamic
  62. max_comp_streams adjustment.
  63. 3) Select compression algorithm
  64. Using comp_algorithm device attribute one can see available and
  65. currently selected (shown in square brackets) compression algorithms,
  66. change selected compression algorithm (once the device is initialised
  67. there is no way to change compression algorithm).
  68. Examples:
  69. #show supported compression algorithms
  70. cat /sys/block/zram0/comp_algorithm
  71. lzo [lz4]
  72. #select lzo compression algorithm
  73. echo lzo > /sys/block/zram0/comp_algorithm
  74. 4) Set Disksize
  75. Set disk size by writing the value to sysfs node 'disksize'.
  76. The value can be either in bytes or you can use mem suffixes.
  77. Examples:
  78. # Initialize /dev/zram0 with 50MB disksize
  79. echo $((50*1024*1024)) > /sys/block/zram0/disksize
  80. # Using mem suffixes
  81. echo 256K > /sys/block/zram0/disksize
  82. echo 512M > /sys/block/zram0/disksize
  83. echo 1G > /sys/block/zram0/disksize
  84. Note:
  85. There is little point creating a zram of greater than twice the size of memory
  86. since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
  87. size of the disk when not in use so a huge zram is wasteful.
  88. 5) Set memory limit: Optional
  89. Set memory limit by writing the value to sysfs node 'mem_limit'.
  90. The value can be either in bytes or you can use mem suffixes.
  91. In addition, you could change the value in runtime.
  92. Examples:
  93. # limit /dev/zram0 with 50MB memory
  94. echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
  95. # Using mem suffixes
  96. echo 256K > /sys/block/zram0/mem_limit
  97. echo 512M > /sys/block/zram0/mem_limit
  98. echo 1G > /sys/block/zram0/mem_limit
  99. # To disable memory limit
  100. echo 0 > /sys/block/zram0/mem_limit
  101. 6) Activate:
  102. mkswap /dev/zram0
  103. swapon /dev/zram0
  104. mkfs.ext4 /dev/zram1
  105. mount /dev/zram1 /tmp
  106. 7) Add/remove zram devices
  107. zram provides a control interface, which enables dynamic (on-demand) device
  108. addition and removal.
  109. In order to add a new /dev/zramX device, perform read operation on hot_add
  110. attribute. This will return either new device's device id (meaning that you
  111. can use /dev/zram<id>) or error code.
  112. Example:
  113. cat /sys/class/zram-control/hot_add
  114. 1
  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. 8) Stats:
  119. Per-device statistics are exported as various nodes under /sys/block/zram<id>/
  120. A brief description of exported device attributes. For more details please
  121. read Documentation/ABI/testing/sysfs-block-zram.
  122. Name access description
  123. ---- ------ -----------
  124. disksize RW show and set the device's disk size
  125. initstate RO shows the initialization state of the device
  126. reset WO trigger device reset
  127. num_reads RO the number of reads
  128. failed_reads RO the number of failed reads
  129. num_write RO the number of writes
  130. failed_writes RO the number of failed writes
  131. invalid_io RO the number of non-page-size-aligned I/O requests
  132. max_comp_streams RW the number of possible concurrent compress operations
  133. comp_algorithm RW show and change the compression algorithm
  134. notify_free RO the number of notifications to free pages (either
  135. slot free notifications or REQ_DISCARD requests)
  136. zero_pages RO the number of zero filled pages written to this disk
  137. orig_data_size RO uncompressed size of data stored in this disk
  138. compr_data_size RO compressed size of data stored in this disk
  139. mem_used_total RO the amount of memory allocated for this disk
  140. mem_used_max RW the maximum amount of memory zram have consumed to
  141. store the data (to reset this counter to the actual
  142. current value, write 1 to this attribute)
  143. mem_limit RW the maximum amount of memory ZRAM can use to store
  144. the compressed data
  145. pages_compacted RO the number of pages freed during compaction
  146. (available only via zram<id>/mm_stat node)
  147. compact WO trigger memory compaction
  148. WARNING
  149. =======
  150. per-stat sysfs attributes are considered to be deprecated.
  151. The basic strategy is:
  152. -- the existing RW nodes will be downgraded to WO nodes (in linux 4.11)
  153. -- deprecated RO sysfs nodes will eventually be removed (in linux 4.11)
  154. The list of deprecated attributes can be found here:
  155. Documentation/ABI/obsolete/sysfs-block-zram
  156. Basically, every attribute that has its own read accessible sysfs node
  157. (e.g. num_reads) *AND* is accessible via one of the stat files (zram<id>/stat
  158. or zram<id>/io_stat or zram<id>/mm_stat) is considered to be deprecated.
  159. User space is advised to use the following files to read the device statistics.
  160. File /sys/block/zram<id>/stat
  161. Represents block layer statistics. Read Documentation/block/stat.txt for
  162. details.
  163. File /sys/block/zram<id>/io_stat
  164. The stat file represents device's I/O statistics not accounted by block
  165. layer and, thus, not available in zram<id>/stat file. It consists of a
  166. single line of text and contains the following stats separated by
  167. whitespace:
  168. failed_reads
  169. failed_writes
  170. invalid_io
  171. notify_free
  172. File /sys/block/zram<id>/mm_stat
  173. The stat file represents device's mm statistics. It consists of a single
  174. line of text and contains the following stats separated by whitespace:
  175. orig_data_size
  176. compr_data_size
  177. mem_used_total
  178. mem_limit
  179. mem_used_max
  180. zero_pages
  181. num_migrated
  182. 9) Deactivate:
  183. swapoff /dev/zram0
  184. umount /dev/zram1
  185. 10) Reset:
  186. Write any positive value to 'reset' sysfs node
  187. echo 1 > /sys/block/zram0/reset
  188. echo 1 > /sys/block/zram1/reset
  189. This frees all the memory allocated for the given device and
  190. resets the disksize to zero. You must set the disksize again
  191. before reusing the device.
  192. Nitin Gupta
  193. ngupta@vflare.org