cache.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Introduction
  2. ============
  3. dm-cache is a device mapper target written by Joe Thornber, Heinz
  4. Mauelshagen, and Mike Snitzer.
  5. It aims to improve performance of a block device (eg, a spindle) by
  6. dynamically migrating some of its data to a faster, smaller device
  7. (eg, an SSD).
  8. This device-mapper solution allows us to insert this caching at
  9. different levels of the dm stack, for instance above the data device for
  10. a thin-provisioning pool. Caching solutions that are integrated more
  11. closely with the virtual memory system should give better performance.
  12. The target reuses the metadata library used in the thin-provisioning
  13. library.
  14. The decision as to what data to migrate and when is left to a plug-in
  15. policy module. Several of these have been written as we experiment,
  16. and we hope other people will contribute others for specific io
  17. scenarios (eg. a vm image server).
  18. Glossary
  19. ========
  20. Migration - Movement of the primary copy of a logical block from one
  21. device to the other.
  22. Promotion - Migration from slow device to fast device.
  23. Demotion - Migration from fast device to slow device.
  24. The origin device always contains a copy of the logical block, which
  25. may be out of date or kept in sync with the copy on the cache device
  26. (depending on policy).
  27. Design
  28. ======
  29. Sub-devices
  30. -----------
  31. The target is constructed by passing three devices to it (along with
  32. other parameters detailed later):
  33. 1. An origin device - the big, slow one.
  34. 2. A cache device - the small, fast one.
  35. 3. A small metadata device - records which blocks are in the cache,
  36. which are dirty, and extra hints for use by the policy object.
  37. This information could be put on the cache device, but having it
  38. separate allows the volume manager to configure it differently,
  39. e.g. as a mirror for extra robustness. This metadata device may only
  40. be used by a single cache device.
  41. Fixed block size
  42. ----------------
  43. The origin is divided up into blocks of a fixed size. This block size
  44. is configurable when you first create the cache. Typically we've been
  45. using block sizes of 256KB - 1024KB. The block size must be between 64
  46. (32KB) and 2097152 (1GB) and a multiple of 64 (32KB).
  47. Having a fixed block size simplifies the target a lot. But it is
  48. something of a compromise. For instance, a small part of a block may be
  49. getting hit a lot, yet the whole block will be promoted to the cache.
  50. So large block sizes are bad because they waste cache space. And small
  51. block sizes are bad because they increase the amount of metadata (both
  52. in core and on disk).
  53. Cache operating modes
  54. ---------------------
  55. The cache has three operating modes: writeback, writethrough and
  56. passthrough.
  57. If writeback, the default, is selected then a write to a block that is
  58. cached will go only to the cache and the block will be marked dirty in
  59. the metadata.
  60. If writethrough is selected then a write to a cached block will not
  61. complete until it has hit both the origin and cache devices. Clean
  62. blocks should remain clean.
  63. If passthrough is selected, useful when the cache contents are not known
  64. to be coherent with the origin device, then all reads are served from
  65. the origin device (all reads miss the cache) and all writes are
  66. forwarded to the origin device; additionally, write hits cause cache
  67. block invalidates. To enable passthrough mode the cache must be clean.
  68. Passthrough mode allows a cache device to be activated without having to
  69. worry about coherency. Coherency that exists is maintained, although
  70. the cache will gradually cool as writes take place. If the coherency of
  71. the cache can later be verified, or established through use of the
  72. "invalidate_cblocks" message, the cache device can be transitioned to
  73. writethrough or writeback mode while still warm. Otherwise, the cache
  74. contents can be discarded prior to transitioning to the desired
  75. operating mode.
  76. A simple cleaner policy is provided, which will clean (write back) all
  77. dirty blocks in a cache. Useful for decommissioning a cache or when
  78. shrinking a cache. Shrinking the cache's fast device requires all cache
  79. blocks, in the area of the cache being removed, to be clean. If the
  80. area being removed from the cache still contains dirty blocks the resize
  81. will fail. Care must be taken to never reduce the volume used for the
  82. cache's fast device until the cache is clean. This is of particular
  83. importance if writeback mode is used. Writethrough and passthrough
  84. modes already maintain a clean cache. Future support to partially clean
  85. the cache, above a specified threshold, will allow for keeping the cache
  86. warm and in writeback mode during resize.
  87. Migration throttling
  88. --------------------
  89. Migrating data between the origin and cache device uses bandwidth.
  90. The user can set a throttle to prevent more than a certain amount of
  91. migration occurring at any one time. Currently we're not taking any
  92. account of normal io traffic going to the devices. More work needs
  93. doing here to avoid migrating during those peak io moments.
  94. For the time being, a message "migration_threshold <#sectors>"
  95. can be used to set the maximum number of sectors being migrated,
  96. the default being 204800 sectors (or 100MB).
  97. Updating on-disk metadata
  98. -------------------------
  99. On-disk metadata is committed every time a FLUSH or FUA bio is written.
  100. If no such requests are made then commits will occur every second. This
  101. means the cache behaves like a physical disk that has a volatile write
  102. cache. If power is lost you may lose some recent writes. The metadata
  103. should always be consistent in spite of any crash.
  104. The 'dirty' state for a cache block changes far too frequently for us
  105. to keep updating it on the fly. So we treat it as a hint. In normal
  106. operation it will be written when the dm device is suspended. If the
  107. system crashes all cache blocks will be assumed dirty when restarted.
  108. Per-block policy hints
  109. ----------------------
  110. Policy plug-ins can store a chunk of data per cache block. It's up to
  111. the policy how big this chunk is, but it should be kept small. Like the
  112. dirty flags this data is lost if there's a crash so a safe fallback
  113. value should always be possible.
  114. For instance, the 'mq' policy, which is currently the default policy,
  115. uses this facility to store the hit count of the cache blocks. If
  116. there's a crash this information will be lost, which means the cache
  117. may be less efficient until those hit counts are regenerated.
  118. Policy hints affect performance, not correctness.
  119. Policy messaging
  120. ----------------
  121. Policies will have different tunables, specific to each one, so we
  122. need a generic way of getting and setting these. Device-mapper
  123. messages are used. Refer to cache-policies.txt.
  124. Discard bitset resolution
  125. -------------------------
  126. We can avoid copying data during migration if we know the block has
  127. been discarded. A prime example of this is when mkfs discards the
  128. whole block device. We store a bitset tracking the discard state of
  129. blocks. However, we allow this bitset to have a different block size
  130. from the cache blocks. This is because we need to track the discard
  131. state for all of the origin device (compare with the dirty bitset
  132. which is just for the smaller cache device).
  133. Target interface
  134. ================
  135. Constructor
  136. -----------
  137. cache <metadata dev> <cache dev> <origin dev> <block size>
  138. <#feature args> [<feature arg>]*
  139. <policy> <#policy args> [policy args]*
  140. metadata dev : fast device holding the persistent metadata
  141. cache dev : fast device holding cached data blocks
  142. origin dev : slow device holding original data blocks
  143. block size : cache unit size in sectors
  144. #feature args : number of feature arguments passed
  145. feature args : writethrough or passthrough (The default is writeback.)
  146. policy : the replacement policy to use
  147. #policy args : an even number of arguments corresponding to
  148. key/value pairs passed to the policy
  149. policy args : key/value pairs passed to the policy
  150. E.g. 'sequential_threshold 1024'
  151. See cache-policies.txt for details.
  152. Optional feature arguments are:
  153. writethrough : write through caching that prohibits cache block
  154. content from being different from origin block content.
  155. Without this argument, the default behaviour is to write
  156. back cache block contents later for performance reasons,
  157. so they may differ from the corresponding origin blocks.
  158. passthrough : a degraded mode useful for various cache coherency
  159. situations (e.g., rolling back snapshots of
  160. underlying storage). Reads and writes always go to
  161. the origin. If a write goes to a cached origin
  162. block, then the cache block is invalidated.
  163. To enable passthrough mode the cache must be clean.
  164. A policy called 'default' is always registered. This is an alias for
  165. the policy we currently think is giving best all round performance.
  166. As the default policy could vary between kernels, if you are relying on
  167. the characteristics of a specific policy, always request it by name.
  168. Status
  169. ------
  170. <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  171. <cache block size> <#used cache blocks>/<#total cache blocks>
  172. <#read hits> <#read misses> <#write hits> <#write misses>
  173. <#demotions> <#promotions> <#dirty> <#features> <features>*
  174. <#core args> <core args>* <policy name> <#policy args> <policy args>*
  175. <cache metadata mode>
  176. metadata block size : Fixed block size for each metadata block in
  177. sectors
  178. #used metadata blocks : Number of metadata blocks used
  179. #total metadata blocks : Total number of metadata blocks
  180. cache block size : Configurable block size for the cache device
  181. in sectors
  182. #used cache blocks : Number of blocks resident in the cache
  183. #total cache blocks : Total number of cache blocks
  184. #read hits : Number of times a READ bio has been mapped
  185. to the cache
  186. #read misses : Number of times a READ bio has been mapped
  187. to the origin
  188. #write hits : Number of times a WRITE bio has been mapped
  189. to the cache
  190. #write misses : Number of times a WRITE bio has been
  191. mapped to the origin
  192. #demotions : Number of times a block has been removed
  193. from the cache
  194. #promotions : Number of times a block has been moved to
  195. the cache
  196. #dirty : Number of blocks in the cache that differ
  197. from the origin
  198. #feature args : Number of feature args to follow
  199. feature args : 'writethrough' (optional)
  200. #core args : Number of core arguments (must be even)
  201. core args : Key/value pairs for tuning the core
  202. e.g. migration_threshold
  203. policy name : Name of the policy
  204. #policy args : Number of policy arguments to follow (must be even)
  205. policy args : Key/value pairs e.g. sequential_threshold
  206. cache metadata mode : ro if read-only, rw if read-write
  207. In serious cases where even a read-only mode is deemed unsafe
  208. no further I/O will be permitted and the status will just
  209. contain the string 'Fail'. The userspace recovery tools
  210. should then be used.
  211. needs_check : 'needs_check' if set, '-' if not set
  212. A metadata operation has failed, resulting in the needs_check
  213. flag being set in the metadata's superblock. The metadata
  214. device must be deactivated and checked/repaired before the
  215. cache can be made fully operational again. '-' indicates
  216. needs_check is not set.
  217. Messages
  218. --------
  219. Policies will have different tunables, specific to each one, so we
  220. need a generic way of getting and setting these. Device-mapper
  221. messages are used. (A sysfs interface would also be possible.)
  222. The message format is:
  223. <key> <value>
  224. E.g.
  225. dmsetup message my_cache 0 sequential_threshold 1024
  226. Invalidation is removing an entry from the cache without writing it
  227. back. Cache blocks can be invalidated via the invalidate_cblocks
  228. message, which takes an arbitrary number of cblock ranges. Each cblock
  229. range's end value is "one past the end", meaning 5-10 expresses a range
  230. of values from 5 to 9. Each cblock must be expressed as a decimal
  231. value, in the future a variant message that takes cblock ranges
  232. expressed in hexidecimal may be needed to better support efficient
  233. invalidation of larger caches. The cache must be in passthrough mode
  234. when invalidate_cblocks is used.
  235. invalidate_cblocks [<cblock>|<cblock begin>-<cblock end>]*
  236. E.g.
  237. dmsetup message my_cache 0 invalidate_cblocks 2345 3456-4567 5678-6789
  238. Examples
  239. ========
  240. The test suite can be found here:
  241. https://github.com/jthornber/device-mapper-test-suite
  242. dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
  243. /dev/mapper/ssd /dev/mapper/origin 512 1 writeback default 0'
  244. dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
  245. /dev/mapper/ssd /dev/mapper/origin 1024 1 writeback \
  246. mq 4 sequential_threshold 1024 random_threshold 8'