zram.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. 4) Set Disksize
  66. Set disk size by writing the value to sysfs node 'disksize'.
  67. The value can be either in bytes or you can use mem suffixes.
  68. Examples:
  69. # Initialize /dev/zram0 with 50MB disksize
  70. echo $((50*1024*1024)) > /sys/block/zram0/disksize
  71. # Using mem suffixes
  72. echo 256K > /sys/block/zram0/disksize
  73. echo 512M > /sys/block/zram0/disksize
  74. echo 1G > /sys/block/zram0/disksize
  75. Note:
  76. There is little point creating a zram of greater than twice the size of memory
  77. since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
  78. size of the disk when not in use so a huge zram is wasteful.
  79. 5) Set memory limit: Optional
  80. Set memory limit by writing the value to sysfs node 'mem_limit'.
  81. The value can be either in bytes or you can use mem suffixes.
  82. In addition, you could change the value in runtime.
  83. Examples:
  84. # limit /dev/zram0 with 50MB memory
  85. echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
  86. # Using mem suffixes
  87. echo 256K > /sys/block/zram0/mem_limit
  88. echo 512M > /sys/block/zram0/mem_limit
  89. echo 1G > /sys/block/zram0/mem_limit
  90. # To disable memory limit
  91. echo 0 > /sys/block/zram0/mem_limit
  92. 6) Activate:
  93. mkswap /dev/zram0
  94. swapon /dev/zram0
  95. mkfs.ext4 /dev/zram1
  96. mount /dev/zram1 /tmp
  97. 7) Add/remove zram devices
  98. zram provides a control interface, which enables dynamic (on-demand) device
  99. addition and removal.
  100. In order to add a new /dev/zramX device, perform read operation on hot_add
  101. attribute. This will return either new device's device id (meaning that you
  102. can use /dev/zram<id>) or error code.
  103. Example:
  104. cat /sys/class/zram-control/hot_add
  105. 1
  106. To remove the existing /dev/zramX device (where X is a device id)
  107. execute
  108. echo X > /sys/class/zram-control/hot_remove
  109. 8) Stats:
  110. Per-device statistics are exported as various nodes under /sys/block/zram<id>/
  111. A brief description of exported device attributes. For more details please
  112. read Documentation/ABI/testing/sysfs-block-zram.
  113. Name access description
  114. ---- ------ -----------
  115. disksize RW show and set the device's disk size
  116. initstate RO shows the initialization state of the device
  117. reset WO trigger device reset
  118. num_reads RO the number of reads
  119. failed_reads RO the number of failed reads
  120. num_write RO the number of writes
  121. failed_writes RO the number of failed writes
  122. invalid_io RO the number of non-page-size-aligned I/O requests
  123. max_comp_streams RW the number of possible concurrent compress operations
  124. comp_algorithm RW show and change the compression algorithm
  125. notify_free RO the number of notifications to free pages (either
  126. slot free notifications or REQ_DISCARD requests)
  127. zero_pages RO the number of zero filled pages written to this disk
  128. orig_data_size RO uncompressed size of data stored in this disk
  129. compr_data_size RO compressed size of data stored in this disk
  130. mem_used_total RO the amount of memory allocated for this disk
  131. mem_used_max RW the maximum amount of memory zram have consumed to
  132. store the data (to reset this counter to the actual
  133. current value, write 1 to this attribute)
  134. mem_limit RW the maximum amount of memory ZRAM can use to store
  135. the compressed data
  136. pages_compacted RO the number of pages freed during compaction
  137. (available only via zram<id>/mm_stat node)
  138. compact WO trigger memory compaction
  139. WARNING
  140. =======
  141. per-stat sysfs attributes are considered to be deprecated.
  142. The basic strategy is:
  143. -- the existing RW nodes will be downgraded to WO nodes (in linux 4.11)
  144. -- deprecated RO sysfs nodes will eventually be removed (in linux 4.11)
  145. The list of deprecated attributes can be found here:
  146. Documentation/ABI/obsolete/sysfs-block-zram
  147. Basically, every attribute that has its own read accessible sysfs node
  148. (e.g. num_reads) *AND* is accessible via one of the stat files (zram<id>/stat
  149. or zram<id>/io_stat or zram<id>/mm_stat) is considered to be deprecated.
  150. User space is advised to use the following files to read the device statistics.
  151. File /sys/block/zram<id>/stat
  152. Represents block layer statistics. Read Documentation/block/stat.txt for
  153. details.
  154. File /sys/block/zram<id>/io_stat
  155. The stat file represents device's I/O statistics not accounted by block
  156. layer and, thus, not available in zram<id>/stat file. It consists of a
  157. single line of text and contains the following stats separated by
  158. whitespace:
  159. failed_reads
  160. failed_writes
  161. invalid_io
  162. notify_free
  163. File /sys/block/zram<id>/mm_stat
  164. The stat file represents device's mm statistics. It consists of a single
  165. line of text and contains the following stats separated by whitespace:
  166. orig_data_size
  167. compr_data_size
  168. mem_used_total
  169. mem_limit
  170. mem_used_max
  171. zero_pages
  172. num_migrated
  173. 9) Deactivate:
  174. swapoff /dev/zram0
  175. umount /dev/zram1
  176. 10) Reset:
  177. Write any positive value to 'reset' sysfs node
  178. echo 1 > /sys/block/zram0/reset
  179. echo 1 > /sys/block/zram1/reset
  180. This frees all the memory allocated for the given device and
  181. resets the disksize to zero. You must set the disksize again
  182. before reusing the device.
  183. Nitin Gupta
  184. ngupta@vflare.org