blob: af99c458c841dd248b443ad0ac74cacc8657614e [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001Introduction
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,
Mike Snitzer66bb2642013-08-16 10:54:20 -040053 e.g. as a mirror for extra robustness. This metadata device may only
54 be used by a single cache device.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000055
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 256k - 1024k.
62
63Having a fixed block size simplifies the target a lot. But it is
64something of a compromise. For instance, a small part of a block may be
65getting hit a lot, yet the whole block will be promoted to the cache.
66So large block sizes are bad because they waste cache space. And small
67block sizes are bad because they increase the amount of metadata (both
68in core and on disk).
69
70Writeback/writethrough
71----------------------
72
73The cache has two modes, writeback and writethrough.
74
75If writeback, the default, is selected then a write to a block that is
76cached will go only to the cache and the block will be marked dirty in
77the metadata.
78
79If writethrough is selected then a write to a cached block will not
80complete until it has hit both the origin and cache devices. Clean
81blocks should remain clean.
82
83A simple cleaner policy is provided, which will clean (write back) all
84dirty blocks in a cache. Useful for decommissioning a cache.
85
86Migration throttling
87--------------------
88
89Migrating data between the origin and cache device uses bandwidth.
90The user can set a throttle to prevent more than a certain amount of
Anatol Pomozovf884ab12013-05-08 16:56:16 -070091migration occurring at any one time. Currently we're not taking any
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000092account of normal io traffic going to the devices. More work needs
93doing here to avoid migrating during those peak io moments.
94
95For the time being, a message "migration_threshold <#sectors>"
96can be used to set the maximum number of sectors being migrated,
97the default being 204800 sectors (or 100MB).
98
99Updating on-disk metadata
100-------------------------
101
102On-disk metadata is committed every time a REQ_SYNC or REQ_FUA bio is
103written. If no such requests are made then commits will occur every
104second. This means the cache behaves like a physical disk that has a
105write cache (the same is true of the thin-provisioning target). If
106power is lost you may lose some recent writes. The metadata should
107always be consistent in spite of any crash.
108
109The 'dirty' state for a cache block changes far too frequently for us
110to keep updating it on the fly. So we treat it as a hint. In normal
111operation it will be written when the dm device is suspended. If the
112system crashes all cache blocks will be assumed dirty when restarted.
113
114Per-block policy hints
115----------------------
116
117Policy plug-ins can store a chunk of data per cache block. It's up to
118the policy how big this chunk is, but it should be kept small. Like the
119dirty flags this data is lost if there's a crash so a safe fallback
120value should always be possible.
121
122For instance, the 'mq' policy, which is currently the default policy,
123uses this facility to store the hit count of the cache blocks. If
124there's a crash this information will be lost, which means the cache
125may be less efficient until those hit counts are regenerated.
126
127Policy hints affect performance, not correctness.
128
129Policy messaging
130----------------
131
132Policies will have different tunables, specific to each one, so we
133need a generic way of getting and setting these. Device-mapper
134messages are used. Refer to cache-policies.txt.
135
136Discard bitset resolution
137-------------------------
138
139We can avoid copying data during migration if we know the block has
140been discarded. A prime example of this is when mkfs discards the
141whole block device. We store a bitset tracking the discard state of
142blocks. However, we allow this bitset to have a different block size
143from the cache blocks. This is because we need to track the discard
144state for all of the origin device (compare with the dirty bitset
145which is just for the smaller cache device).
146
147Target interface
148================
149
150Constructor
151-----------
152
153 cache <metadata dev> <cache dev> <origin dev> <block size>
154 <#feature args> [<feature arg>]*
155 <policy> <#policy args> [policy args]*
156
157 metadata dev : fast device holding the persistent metadata
158 cache dev : fast device holding cached data blocks
159 origin dev : slow device holding original data blocks
160 block size : cache unit size in sectors
161
162 #feature args : number of feature arguments passed
163 feature args : writethrough. (The default is writeback.)
164
165 policy : the replacement policy to use
166 #policy args : an even number of arguments corresponding to
167 key/value pairs passed to the policy
168 policy args : key/value pairs passed to the policy
169 E.g. 'sequential_threshold 1024'
170 See cache-policies.txt for details.
171
172Optional feature arguments are:
173 writethrough : write through caching that prohibits cache block
174 content from being different from origin block content.
175 Without this argument, the default behaviour is to write
176 back cache block contents later for performance reasons,
177 so they may differ from the corresponding origin blocks.
178
179A policy called 'default' is always registered. This is an alias for
180the policy we currently think is giving best all round performance.
181
182As the default policy could vary between kernels, if you are relying on
183the characteristics of a specific policy, always request it by name.
184
185Status
186------
187
188<#used metadata blocks>/<#total metadata blocks> <#read hits> <#read misses>
189<#write hits> <#write misses> <#demotions> <#promotions> <#blocks in cache>
190<#dirty> <#features> <features>* <#core args> <core args>* <#policy args>
191<policy args>*
192
193#used metadata blocks : Number of metadata blocks used
194#total metadata blocks : Total number of metadata blocks
195#read hits : Number of times a READ bio has been mapped
196 to the cache
197#read misses : Number of times a READ bio has been mapped
198 to the origin
199#write hits : Number of times a WRITE bio has been mapped
200 to the cache
201#write misses : Number of times a WRITE bio has been
202 mapped to the origin
203#demotions : Number of times a block has been removed
204 from the cache
205#promotions : Number of times a block has been moved to
206 the cache
207#blocks in cache : Number of blocks resident in the cache
208#dirty : Number of blocks in the cache that differ
209 from the origin
210#feature args : Number of feature args to follow
211feature args : 'writethrough' (optional)
212#core args : Number of core arguments (must be even)
213core args : Key/value pairs for tuning the core
214 e.g. migration_threshold
215#policy args : Number of policy arguments to follow (must be even)
216policy args : Key/value pairs
217 e.g. 'sequential_threshold 1024
218
219Messages
220--------
221
222Policies will have different tunables, specific to each one, so we
223need a generic way of getting and setting these. Device-mapper
224messages are used. (A sysfs interface would also be possible.)
225
226The message format is:
227
228 <key> <value>
229
230E.g.
231 dmsetup message my_cache 0 sequential_threshold 1024
232
233Examples
234========
235
236The test suite can be found here:
237
238https://github.com/jthornber/thinp-test-suite
239
240dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
241 /dev/mapper/ssd /dev/mapper/origin 512 1 writeback default 0'
242dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
243 /dev/mapper/ssd /dev/mapper/origin 1024 1 writeback \
244 mq 4 sequential_threshold 1024 random_threshold 8'