cache.rst 13 KB

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