zram.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. Regardless the value passed to this attribute, ZRAM will always
  47. allocate multiple compression streams - one per online CPUs - thus
  48. allowing several concurrent compression operations. The number of
  49. allocated compression streams goes down when some of the CPUs
  50. become offline. There is no single-compression-stream mode anymore,
  51. unless you are running a UP system or has only 1 CPU online.
  52. To find out how many streams are currently available:
  53. cat /sys/block/zram0/max_comp_streams
  54. 3) Select compression algorithm
  55. Using comp_algorithm device attribute one can see available and
  56. currently selected (shown in square brackets) compression algorithms,
  57. change selected compression algorithm (once the device is initialised
  58. there is no way to change compression algorithm).
  59. Examples:
  60. #show supported compression algorithms
  61. cat /sys/block/zram0/comp_algorithm
  62. lzo [lz4]
  63. #select lzo compression algorithm
  64. echo lzo > /sys/block/zram0/comp_algorithm
  65. For the time being, the `comp_algorithm' content does not necessarily
  66. show every compression algorithm supported by the kernel. We keep this
  67. list primarily to simplify device configuration and one can configure
  68. a new device with a compression algorithm that is not listed in
  69. `comp_algorithm'. The thing is that, internally, ZRAM uses Crypto API
  70. and, if some of the algorithms were built as modules, it's impossible
  71. to list all of them using, for instance, /proc/crypto or any other
  72. method. This, however, has an advantage of permitting the usage of
  73. custom crypto compression modules (implementing S/W or H/W compression).
  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. debug_stat RO this file is used for zram debugging purposes
  149. WARNING
  150. =======
  151. per-stat sysfs attributes are considered to be deprecated.
  152. The basic strategy is:
  153. -- the existing RW nodes will be downgraded to WO nodes (in linux 4.11)
  154. -- deprecated RO sysfs nodes will eventually be removed (in linux 4.11)
  155. The list of deprecated attributes can be found here:
  156. Documentation/ABI/obsolete/sysfs-block-zram
  157. Basically, every attribute that has its own read accessible sysfs node
  158. (e.g. num_reads) *AND* is accessible via one of the stat files (zram<id>/stat
  159. or zram<id>/io_stat or zram<id>/mm_stat) is considered to be deprecated.
  160. User space is advised to use the following files to read the device statistics.
  161. File /sys/block/zram<id>/stat
  162. Represents block layer statistics. Read Documentation/block/stat.txt for
  163. details.
  164. File /sys/block/zram<id>/io_stat
  165. The stat file represents device's I/O statistics not accounted by block
  166. layer and, thus, not available in zram<id>/stat file. It consists of a
  167. single line of text and contains the following stats separated by
  168. whitespace:
  169. failed_reads
  170. failed_writes
  171. invalid_io
  172. notify_free
  173. File /sys/block/zram<id>/mm_stat
  174. The stat file represents device's mm statistics. It consists of a single
  175. line of text and contains the following stats separated by whitespace:
  176. orig_data_size
  177. compr_data_size
  178. mem_used_total
  179. mem_limit
  180. mem_used_max
  181. zero_pages
  182. num_migrated
  183. 9) Deactivate:
  184. swapoff /dev/zram0
  185. umount /dev/zram1
  186. 10) Reset:
  187. Write any positive value to 'reset' sysfs node
  188. echo 1 > /sys/block/zram0/reset
  189. echo 1 > /sys/block/zram1/reset
  190. This frees all the memory allocated for the given device and
  191. resets the disksize to zero. You must set the disksize again
  192. before reusing the device.
  193. Nitin Gupta
  194. ngupta@vflare.org