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