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