zram.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. mem_used_max WO reset the `mem_used_max' counter (see later)
  128. mem_limit WO specifies the maximum amount of memory ZRAM can use
  129. to store the compressed data
  130. max_comp_streams RW the number of possible concurrent compress operations
  131. comp_algorithm RW show and change the compression algorithm
  132. compact WO trigger memory compaction
  133. debug_stat RO this file is used for zram debugging purposes
  134. backing_dev RW set up backend storage for zram to write out
  135. User space is advised to use the following files to read the device statistics.
  136. File /sys/block/zram<id>/stat
  137. Represents block layer statistics. Read Documentation/block/stat.txt for
  138. details.
  139. File /sys/block/zram<id>/io_stat
  140. The stat file represents device's I/O statistics not accounted by block
  141. layer and, thus, not available in zram<id>/stat file. It consists of a
  142. single line of text and contains the following stats separated by
  143. whitespace:
  144. failed_reads the number of failed reads
  145. failed_writes the number of failed writes
  146. invalid_io the number of non-page-size-aligned I/O requests
  147. notify_free Depending on device usage scenario it may account
  148. a) the number of pages freed because of swap slot free
  149. notifications or b) the number of pages freed because of
  150. REQ_DISCARD requests sent by bio. The former ones are
  151. sent to a swap block device when a swap slot is freed,
  152. which implies that this disk is being used as a swap disk.
  153. The latter ones are sent by filesystem mounted with
  154. discard option, whenever some data blocks are getting
  155. discarded.
  156. File /sys/block/zram<id>/mm_stat
  157. The stat file represents device's mm statistics. It consists of a single
  158. line of text and contains the following stats separated by whitespace:
  159. orig_data_size uncompressed size of data stored in this disk.
  160. This excludes same-element-filled pages (same_pages) since
  161. no memory is allocated for them.
  162. Unit: bytes
  163. compr_data_size compressed size of data stored in this disk
  164. mem_used_total the amount of memory allocated for this disk. This
  165. includes allocator fragmentation and metadata overhead,
  166. allocated for this disk. So, allocator space efficiency
  167. can be calculated using compr_data_size and this statistic.
  168. Unit: bytes
  169. mem_limit the maximum amount of memory ZRAM can use to store
  170. the compressed data
  171. mem_used_max the maximum amount of memory zram have consumed to
  172. store the data
  173. same_pages the number of same element filled pages written to this disk.
  174. No memory is allocated for such pages.
  175. pages_compacted the number of pages freed during compaction
  176. huge_pages the number of incompressible pages
  177. 9) Deactivate:
  178. swapoff /dev/zram0
  179. umount /dev/zram1
  180. 10) Reset:
  181. Write any positive value to 'reset' sysfs node
  182. echo 1 > /sys/block/zram0/reset
  183. echo 1 > /sys/block/zram1/reset
  184. This frees all the memory allocated for the given device and
  185. resets the disksize to zero. You must set the disksize again
  186. before reusing the device.
  187. * Optional Feature
  188. = writeback
  189. With incompressible pages, there is no memory saving with zram.
  190. Instead, with CONFIG_ZRAM_WRITEBACK, zram can write incompressible page
  191. to backing storage rather than keeping it in memory.
  192. User should set up backing device via /sys/block/zramX/backing_dev
  193. before disksize setting.
  194. = memory tracking
  195. With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
  196. zram block. It could be useful to catch cold or incompressible
  197. pages of the process with*pagemap.
  198. If you enable the feature, you could see block state via
  199. /sys/kernel/debug/zram/zram0/block_state". The output is as follows,
  200. 300 75.033841 .wh
  201. 301 63.806904 s..
  202. 302 63.806919 ..h
  203. First column is zram's block index.
  204. Second column is access time since the system was booted
  205. Third column is state of the block.
  206. (s: same page
  207. w: written page to backing store
  208. h: huge page)
  209. First line of above example says 300th block is accessed at 75.033841sec
  210. and the block's state is huge so it is written back to the backing
  211. storage. It's a debugging feature so anyone shouldn't rely on it to work
  212. properly.
  213. Nitin Gupta
  214. ngupta@vflare.org