kernel - Update swapcache manual page, fix breakage in last commit
[dragonfly.git] / share / man / man8 / swapcache.8
blob54bf88e57f2fd213574fc0eda7c77a563c07d541
1 .\"
2 .\" swapcache - Cache clean filesystem data & meta-data on SSD-based swap
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .Dd February 7, 2010
13 .Dt SWAPCACHE 8
14 .Os
15 .Sh NAME
16 .Nm swapcache
17 .Nd a
18 mechanism which allows the system to use fast swap to cache filesystem
19 data and meta-data.
20 .Sh SYNOPSIS (defaults shown)
21 .Cd sysctl vm.swapcache.accrate=100000
22 .Cd sysctl vm.swapcache.maxfilesize=0
23 .Cd sysctl vm.swapcache.maxburst=2000000000
24 .Cd sysctl vm.swapcache.curburst=4000000000
25 .Cd sysctl vm.swapcache.minburst=10000000
26 .Cd sysctl vm.swapcache.read_enable=0
27 .Cd sysctl vm.swapcache.meta_enable=0
28 .Cd sysctl vm.swapcache.data_enable=0
29 .Cd sysctl vm.swapcache.use_chflags=1
30 .Cd sysctl vm.swapcache.maxlaunder=256
31 .Sh DESCRIPTION
32 .Nm
33 is a system capability which allows a solid state disk (SSD) in a swap
34 space configuration to be used to cache clean filesystem data and meta-data
35 in addition to its normal function of backing anonymous memory.
36 .Pp
37 Sysctls are used to manage operational parameters and can be adjusted at
38 any time.  Typically a large initial burst is desired after system boot,
39 controlled by the initial
40 .Cd vm.swapcache.curburst
41 parameter.
42 This parameter is reduced as data is written to swap by the swapcache
43 and increased at a rate specified by
44 .Cd vm.swapcache.accrate .
45 Once this parameter reaches zero write activity ceases until it has
46 recovered sufficiently for write activity to resume.
47 .Pp
48 .Cd vm.swapcache.meta_enable
49 enables the writing of filesystem meta-data to the swapcache.  Filesystem
50 metadata is any data which the filesystem accesses via the disk device
51 using buffercache.  Meta-data is cached globally regardless of file
52 or directory flags.
53 .Pp
54 .Cd vm.swapcache.data_enable
55 enables the writing of filesystem file-data to the swapcache.  Filesystem
56 filedata is any data which the filesystem accesses via a regular file.
57 In technical terms, when the buffer cache is used to access a regular
58 file through its vnode.  Please do not blindly turn on this option,
59 see the PERFORMANCE TUNING section for more information.
60 .Pp
61 .Cd vm.swapcache.use_chflags
62 enables the use of the
63 .Cm cache
64 and
65 .Cm noscache
66 .Xr chflags 1
67 flags to control which files will be data-cached.
68 If this sysctl is disabled and data_enable is enabled,
69 the system will ignore file flags and attempt to swapcache all
70 regular files.
71 .Pp
72 .Cd vm.swapcache.read_enable
73 enables reading from the swapcache and should be set to 1 for normal
74 operation.
75 .Pp
76 .Cd vm.swapcache.maxfilesize
77 controls which files are to be cached based on their size.
78 If set to non-zero only files smaller than the specified size
79 will be cached.  Larger files will not be cached.
80 .Sh PERFORMANCE TUNING
81 Best operation is achieved when the active data set fits within the
82 swapcache.
83 .Pp
84 .Bl -tag -width 4n -compact
85 .It Cd vm.swapcache.accrate
86 This specifies the burst accumulation rate in bytes per second and
87 ultimately controls the write bandwidth to swap averaged over a long
88 period of time.
89 This parameter must be carefully chosen to manage the write endurance of
90 the SSD in order to avoid wearing it out too quickly.
91 Even though SSDs have limited write endurance, there is massive
92 cost/performance benefit to using one in a swapcache configuration.
93 .Pp
94 Let's use the Intel X25V 40G MLC SATA SSD as an example.  This device
95 has approximately a
96 40TB (40 terabyte) write endurance, but see later
97 notes on this, it is more a minimum value.
98 Limiting the long term average bandwidth to 100K/sec leads to no more
99 than ~9G/day writing which calculates approximately to a 12 year
100 endurance.
101 Endurance scales linearly with size.  The 80G version of this SSD
102 will have a write endurance of approximately 80TB.
104 MLC SSDs have a 1000-10000x write endurance, while the lower density
105 higher-cost SLC SSDs have an approximately 10000-100000x write endurance.
106 MLC SSDs can be used for the swapcache (and swap) as long as the system
107 manager is cognizant of its limitations.
109 .It Cd vm.swapcache.meta_enable
110 Turning on just
111 .Cd meta_enable
112 causes only filesystem meta-data to be cached and will result
113 in very fast directory operations even over millions of inodes
114 and even in the face of other invasive operations being run
115 by other processes.
117 .It Cd vm.swapcache.data_enable
118 Turning on
119 .Cd data_enable
120 (with or without other features) allows bulk file data to be
121 cached.
122 This feature is very useful for web server operation when the
123 operational data set fits in swap.
124 The usefulness is somewhat mitigated by the maximum number
125 of vnodes supported by the system via
126 .Cd kern.maxfiles ,
127 because the bulk data in the cache is lost when the related
128 vnode is recycled.  In this case it might be desireable to
129 take the plunge into running a 64-bit kernel which can support
130 far more vnodes.  32-bit kernels have limited kernel virtual
131 memory (KVM) and cannot reliably support more than around
132 100,000 active vnodes.  64-bit kernels can support 300,000+
133 active vnodes.
135 Data caching is definitely more wasteful of SSD write bandwidth
136 than meta-data caching.  It doesn't hurt performance per se,
137 but may cause the
139 to exhaust its burst and smack against the long term average
140 bandwidth limit, causing the SSD to wear out at the maximum rate you
141 programmed.  Data caching is far less wasteful and more efficient
142 if (on a 64-bit system only) you provide a sufficiently large SSD and
143 increase
144 .Cd kern.maxvnodes
145 to cover the entire directory topology being served.
146 Each vnode requires about 1K of physical ram.
148 When data caching is turned on you generally want to use
149 .Xr chflags 1
150 with the
151 .Cm cache
152 flag to enable data caching on a directory.
153 This flag is tracked by the namecache and does not need to be
154 recursively set in the directory tree.
155 Simply setting the flag in a top level directory is sufficient.
156 A typical setup is something like this:
158 .Dl chflags cache /etc /sbin /bin /usr /home
159 .Dl chflags noscache /usr/obj
161 Alternatively if you have NFS mounts where chflags does not work you
162 can enable caching in some parent directory, then selectively disable
165 .Dl chflags cache /
166 .Dl chflags noscache /usr/obj /tmp /var/tmp
168 If that doesn't work you can turn off
169 .Cd vm.swapcache.use_chflags
170 entirely and not bother with any chflagging.
172 .It Cd vm.swapcache.maxfilesize
173 This may be used to reduce cache thrashing when a focus on a small
174 potentially fragmented filespace is desired, leaving the
175 larger files alone.
177 .It Cd vm.swapcache.minburst
178 This controls hysteresis and prevents nickel-and-dime write bursting.
179 Once
180 .Cd curburst
181 drops to zero, writing to the swapcache ceases until it has recovered
182 past
183 .Cd minburst .
184 The idea here is to avoid creating a heavily fragmented swapcache where
185 reading data from a file must alternate between the cache and the primary
186 filesystem.  Doing so does not save disk seeks on the primary filesystem
187 so we want to avoid doing small bursts.  This parameter allows us to do
188 larger bursts.
189 The larger bursts also tend to improve SSD performance as the SSD itself
190 can do a better job write-combining and erasing blocks.
192 .It Cd vm_swapcache.maxswappct
193 This controls the maximum amount of swapspace
195 may use, in percentage terms.
198 It is important to note that you should always use
199 .Xr disklabel64 8
200 to label your SSD.  Disklabel64 will properly align the base of the
201 partition space relative to the physical drive regardless of how badly
202 aligned the fdisk slice is.
203 This will significantly reduce write amplification and write combining
204 inefficiencies on the SSD.
206 Finally, interleaved swap (multiple SSDs) may be used to increase
207 performance even further.  A single SATA SSD is typically capable of
208 reading 120-220MB/sec.  Configuring two SSDs for your swap will
209 improve aggregate swapcache read performance by 1.5x to 1.8x.
210 In tests with two Intel 40G SSDs 300MB/sec was easily achieved.
212 At this point you will be configuring more swap space than a 32 bit
214 kernel can handle (due to KVM limitations).  By default, 32 bit
216 systems only support 32G of configured swap and while this limit
217 can be increased somewhat in
218 .Pa /boot/loader.conf
219 you should really be using a 64-bit
221 kernel instead.  64-bit systems support up to 512G of swap by default
222 and can be boosted to up to 8TB if you are really crazy and have enough ram.
223 Each 1GB of swap requires around 1MB of physical memory to manage it so
224 the practical limit is more around 1TB of swap.
226 Of course, a 1TB SSD is something on the order of $3000+ as of this writing.
227 Even though a 1TB configuration might not be cost effective, storage levels
228 more in the 100-200G range certainly are.  If the machine has only a 1GigE
229 ethernet (100MB/s) there's no point configuring it for more SSD bandwidth.
230 A single SSD of the desired size would be sufficient.
231 .Sh INITIAL BURSTING & REPEATED BURSTING
232 Even though the average write bandwidth is limited it is desireable
233 to have a large initial burst after boot to load the cache.
234 .Cd curburst
235 is initialized to 4GB by default and you can force rebursting
236 by adjusting it with a sysctl.
237 Remember that
238 .Cd curburst
239 dynamically tracks burst and will go up and down depending.
241 In addition there will be periods of time where the system is in
242 steady state and not writing to the swapcache.  During these periods
243 .Cd curburst
244 will inch back up but will not exceed
245 .Cd maxburst .
246 Thus the
247 .Cd maxburst
248 value controls how large a repeated burst can be.
250 A second bursting parameter called
251 .Cd vm.swapcache.minburst
252 controls bursting when the maximum write bandwidth has been reached.
253 When
254 .Cd minburst
255 reaches zero write activity ceases and
256 .Cd curburst
257 is allowed to recover up to
258 .Cd minburst
259 before write activity resumes.  The recommended range for the
260 .Cd minburst
261 parameter is 1MB to 50MB.  This parameter has a relationship to
262 how fragmented the swapcache gets when not in a steady state.
263 Large bursts reduce fragmentation and reduce incidences of
264 excessive seeking on the hard drive.  If set too low the
265 swapcache will become fragmented within a single regular file
266 and the constant back-and-forth between the swapcache and the
267 hard drive will result in excessive seeking on the hard drive.
268 .Sh SWAPCACHE SIZE & MANAGEMENT
269 The swapcache feature will use up to 75% of configured swap space
270 by default.
271 The remaining 25% is reserved for normal paging operation.
272 The system operator should configure at least 4 times the SWAP space
273 versus main memory and no less than 8G of swap space.
274 If a 40G SSD is used the recommendation is to configure 16G to 32G of
275 swap (note: 32-bit is limited to 32G of swap by default, for 64-bit
276 it is 512G of swap), and to leave the remainder unwritten and unused.
279 .Cd vm_swapcache.maxswappct
280 sysctl may be used to change the default.
281 You may have to change this default if you also use
282 .Xr tmpfs 5 ,
283 .Xr vn 4 ,
284 or if you have not allocated enough swap for reasonable normal paging
285 activity to occur (in which case you probably shouldn't be using
287 anyway).
289 If swapcache reaches the 75% limit it will begin tearing down swap
290 in linear bursts by iterating through available VM objects, until
291 swap space use drops to 70%.  The tear-down is limited by the rate at
292 which new data is written and this rate in turn is often limited
294 .Cd vm.swapcache.accrate ,
295 resulting in an orderly replacement of cached data and meta-data.
296 The limit is typically only reached when doing full data+meta-data
297 caching with no file size limitations and serving primarily large
298 files, or (on a 64-bit system) bumping kern.maxvnodes up to very
299 high values.
300 .Sh NORMAL SWAP PAGING ACTIVITY WITH SSD SWAP
301 This is not a function of
303 per se but instead a normal function of the system.  Most systems have
304 sufficient memory that they do not need to page memory to swap.  These
305 types of systems are the ones best suited for MLC SSD configured swap
306 running with a
308 configuration.
309 Systems which modestly page to swap, in the range of a few hundred
310 megabytes a day worth of writing, are also well suited for MLC SSD
311 configured swap.  Desktops usually fall into this category even if they
312 page out a bit more because swap activity is governed by the actions of
313 a single person.
315 Systems which page anonymous memory heavily when
317 would otherwise be turned off are not usually well suited for MLC SSD
318 configured swap.  Heavy paging activity is not governed by
320 bandwidth control parameters and can lead to excessive uncontrolled
321 writing to the MLC SSD, causing premature wearout.  You would have to
322 use the lower density, more expensive SLC SSD technology (which has 10x
323 the durability).  This isn't to say that
325 would be ineffective, just that the aggregate write bandwidth required
326 to support the system would be too large for MLC flash technologies.
328 With this caveat in mind, SSD based paging on systems with insufficient
329 ram can be extremely effective in extending the useful life of the system.
330 For example, a system with a measly 192MB of ram and SSD swap can run
331 a -j 8 parallel build world in a little less than twice the time it
332 would take if the system had 2G of ram, whereas it would take 5x to 10x
333 as long with normal HD based swap.
334 .Sh WARNINGS
335 I am going to repeat and expand a bit on SSD wear.
336 Wear on SSDs is a function of the write durability of the cells,
337 whether the SSD implements static or dynamic wear leveling, and
338 write amplification effects based on the type of write activity.
339 Write amplification occurs due to wasted space when the SSD must
340 erase and rewrite the underlying flash blocks.  e.g. MLC flash uses
341 128KB erase/write blocks.
344 parameters should be carefully chosen to avoid early wearout.
345 For example, the Intel X25V 40G SSD has a minimum write durability
346 of 40TB and an actual durability that can be quite a bit higher.
347 Generally speaking, you want to select parameters that will give you
348 at least 10 years of service life.
349 The most important parameter to control this is
350 .Cd vm.swapcache.accrate .
352 uses a very conservative 100KB/sec default but even a small X25V
353 can probably handle 300KB/sec of continuous writing and still last
354 10 years.
356 Depending on the wear leveling algorithm the drive uses, durability
357 and performance can sometimes be improved by configuring less
358 space (in a manufacturer-fresh drive) than the drive's probed capacity.
359 For example, by only using 32G of a 40G SSD.
360 SSDs typically implement 10% more storage than advertised and
361 use this storage to improve wear leveling.  As cells begin to fail
362 this overallotment slowly becomes part of the primary storage
363 until it has been exhausted.  After that the SSD has basically failed.
364 Keep in mind that if you use a larger portion of the SSD's advertised
365 storage the SSD will not know if/when you decide to use less unless
366 appropriate TRIM commands are sent (if supported), or a low level
367 factory erase is issued.
369 The swapcache is designed for use with SSDs configured as swap and
370 will generally not improve performance when a normal hard drive is used
371 for swap.
373 .Nm smartctl
374 (from pkgsrc's sysutils/smartmontools) may be used to retrieve 
375 the wear indicator from the drive.
376 One usually runs something like 'smartctl -d sat -a /dev/daXX'
377 (for AHCI/SILI/SCSI), or 'smartctl -a /dev/adXX' for NATA.  Some SSDs
378 (particularly the Intels) will brick the SATA port when smart operations
379 are done while the drive is busy with normal activity, so the tool should
380 only be run when the SSD is idle.
382 ID 232 (0xe8) in the SMART data dump indicates available reserved
383 space and ID 233 (0xe9) is the wear-out meter.  Reserved space
384 typically starts at 100 and decrements to 10, after which the SSD
385 is considered to operate in a degraded mode.  The wear-out meter
386 typically starts at 99 and decrements to 0, after which the SSD
387 has failed.
390 tends to use large 64K writes and tends to cluster multiple writes
391 linearly.  The SSD is able to take significant advantage of this
392 and write amplification effects are greatly reduced.  If we
393 take a 40G Intel X25V as an example the vendor specifies a write
394 durability of approximately 40TB, but
396 should be able to squeeze out upwards of 200TB due the fairly optimal
397 write clustering it does.
398 The theoretical limit for the Intel X25V is 400TB (10,000 erase cycles
399 per MLC cell, 40G drive), but the firmware doesn't do perfect static
400 wear leveling so the actual durability is less.
402 In contrast, most filesystems directly stored on a SSD have
403 fairly severe write amplification effects and will have durabilities
404 ranging closer to the vendor-specified limit.
405 Power-on hours, power cycles, and read operations do not really affect
406 wear.
408 SSD's with MLC-based flash technology are high-density, low-cost solutions
409 with limited write durability.  SLC-based flash technology is a low-density,
410 higher-cost solution with 10x the write durability as MLC.  The durability
411 also scales with the amount of flash storage.  SLC based flash is typically
412 twice as expensive per gigabyte.  From a cost perspective, SLC based flash
413 is at least 5x more cost effective in situations where high write
414 bandwidths are required (because it lasts 10x longer).  MLC is at least
415 2x more cost effective in situations where high write bandwidth is not
416 required.
417 When wear calculations are in years, these differences become huge, but
418 often the quantity of storage needed trumps the wear life so we expect most
419 people will be using MLC.
421 is usable with both technologies.
422 .Sh SEE ALSO
423 .Xr swapon 8 ,
424 .Xr disklabel64 8 ,
425 .Xr fstab 5
426 .Sh HISTORY
428 first appeared in
429 .Dx 2.5 .
430 .Sh AUTHORS
431 .An Matthew Dillon