cache-policies.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Guidance for writing policies
  2. =============================
  3. Try to keep transactionality out of it. The core is careful to
  4. avoid asking about anything that is migrating. This is a pain, but
  5. makes it easier to write the policies.
  6. Mappings are loaded into the policy at construction time.
  7. Every bio that is mapped by the target is referred to the policy.
  8. The policy can return a simple HIT or MISS or issue a migration.
  9. Currently there's no way for the policy to issue background work,
  10. e.g. to start writing back dirty blocks that are going to be evicted
  11. soon.
  12. Because we map bios, rather than requests it's easy for the policy
  13. to get fooled by many small bios. For this reason the core target
  14. issues periodic ticks to the policy. It's suggested that the policy
  15. doesn't update states (eg, hit counts) for a block more than once
  16. for each tick. The core ticks by watching bios complete, and so
  17. trying to see when the io scheduler has let the ios run.
  18. Overview of supplied cache replacement policies
  19. ===============================================
  20. multiqueue (mq)
  21. ---------------
  22. This policy is now an alias for smq (see below).
  23. The following tunables are accepted, but have no effect:
  24. 'sequential_threshold <#nr_sequential_ios>'
  25. 'random_threshold <#nr_random_ios>'
  26. 'read_promote_adjustment <value>'
  27. 'write_promote_adjustment <value>'
  28. 'discard_promote_adjustment <value>'
  29. Stochastic multiqueue (smq)
  30. ---------------------------
  31. This policy is the default.
  32. The stochastic multi-queue (smq) policy addresses some of the problems
  33. with the multiqueue (mq) policy.
  34. The smq policy (vs mq) offers the promise of less memory utilization,
  35. improved performance and increased adaptability in the face of changing
  36. workloads. smq also does not have any cumbersome tuning knobs.
  37. Users may switch from "mq" to "smq" simply by appropriately reloading a
  38. DM table that is using the cache target. Doing so will cause all of the
  39. mq policy's hints to be dropped. Also, performance of the cache may
  40. degrade slightly until smq recalculates the origin device's hotspots
  41. that should be cached.
  42. Memory usage:
  43. The mq policy used a lot of memory; 88 bytes per cache block on a 64
  44. bit machine.
  45. smq uses 28bit indexes to implement it's data structures rather than
  46. pointers. It avoids storing an explicit hit count for each block. It
  47. has a 'hotspot' queue, rather than a pre-cache, which uses a quarter of
  48. the entries (each hotspot block covers a larger area than a single
  49. cache block).
  50. All this means smq uses ~25bytes per cache block. Still a lot of
  51. memory, but a substantial improvement nontheless.
  52. Level balancing:
  53. mq placed entries in different levels of the multiqueue structures
  54. based on their hit count (~ln(hit count)). This meant the bottom
  55. levels generally had the most entries, and the top ones had very
  56. few. Having unbalanced levels like this reduced the efficacy of the
  57. multiqueue.
  58. smq does not maintain a hit count, instead it swaps hit entries with
  59. the least recently used entry from the level above. The overall
  60. ordering being a side effect of this stochastic process. With this
  61. scheme we can decide how many entries occupy each multiqueue level,
  62. resulting in better promotion/demotion decisions.
  63. Adaptability:
  64. The mq policy maintained a hit count for each cache block. For a
  65. different block to get promoted to the cache it's hit count has to
  66. exceed the lowest currently in the cache. This meant it could take a
  67. long time for the cache to adapt between varying IO patterns.
  68. smq doesn't maintain hit counts, so a lot of this problem just goes
  69. away. In addition it tracks performance of the hotspot queue, which
  70. is used to decide which blocks to promote. If the hotspot queue is
  71. performing badly then it starts moving entries more quickly between
  72. levels. This lets it adapt to new IO patterns very quickly.
  73. Performance:
  74. Testing smq shows substantially better performance than mq.
  75. cleaner
  76. -------
  77. The cleaner writes back all dirty blocks in a cache to decommission it.
  78. Examples
  79. ========
  80. The syntax for a table is:
  81. cache <metadata dev> <cache dev> <origin dev> <block size>
  82. <#feature_args> [<feature arg>]*
  83. <policy> <#policy_args> [<policy arg>]*
  84. The syntax to send a message using the dmsetup command is:
  85. dmsetup message <mapped device> 0 sequential_threshold 1024
  86. dmsetup message <mapped device> 0 random_threshold 8
  87. Using dmsetup:
  88. dmsetup create blah --table "0 268435456 cache /dev/sdb /dev/sdc \
  89. /dev/sdd 512 0 mq 4 sequential_threshold 1024 random_threshold 8"
  90. creates a 128GB large mapped device named 'blah' with the
  91. sequential threshold set to 1024 and the random_threshold set to 8.