2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Implement the swapcache daemon. When enabled swap is assumed to be
37 * configured on a fast storage device such as a SSD. Swap is assigned
38 * to clean vnode-backed pages in the inactive queue, clustered by object
39 * if possible, and written out. The swap assignment sticks around even
40 * after the underlying pages have been recycled.
42 * The daemon manages write bandwidth based on sysctl settings to control
45 * The vnode strategy code will check for the swap assignments and divert
46 * reads to the swap device when the data is present in the swapcache.
48 * This operates on both regular files and the block device vnodes used by
49 * filesystems to manage meta-data.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/resourcevar.h>
59 #include <sys/signalvar.h>
60 #include <sys/vnode.h>
61 #include <sys/vmmeter.h>
62 #include <sys/sysctl.h>
65 #include <vm/vm_param.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_pager.h>
72 #include <vm/swap_pager.h>
73 #include <vm/vm_extern.h>
75 #include <sys/thread2.h>
76 #include <vm/vm_page2.h>
78 #define INACTIVE_LIST (&vm_page_queues[PQ_INACTIVE].pl)
80 /* the kernel process "vm_pageout"*/
81 static void vm_swapcached (void);
82 static int vm_swapcached_flush (vm_page_t m
);
83 static int vm_swapcache_test(vm_page_t m
);
84 static void vm_swapcache_writing(vm_page_t marker
);
85 static void vm_swapcache_cleaning(vm_object_t marker
);
86 struct thread
*swapcached_thread
;
88 static struct kproc_desc swpc_kp
= {
93 SYSINIT(swapcached
, SI_SUB_KTHREAD_PAGE
, SI_ORDER_SECOND
, kproc_start
, &swpc_kp
)
95 SYSCTL_NODE(_vm
, OID_AUTO
, swapcache
, CTLFLAG_RW
, NULL
, NULL
);
97 int vm_swapcache_read_enable
;
98 static int vm_swapcache_sleep
;
99 static int vm_swapcache_maxlaunder
= 256;
100 static int vm_swapcache_data_enable
= 0;
101 static int vm_swapcache_meta_enable
= 0;
102 static int vm_swapcache_maxswappct
= 75;
103 static int vm_swapcache_use_chflags
= 1; /* require chflags cache */
104 static int64_t vm_swapcache_minburst
= 10000000LL; /* 10MB */
105 static int64_t vm_swapcache_curburst
= 4000000000LL; /* 4G after boot */
106 static int64_t vm_swapcache_maxburst
= 2000000000LL; /* 2G nominal max */
107 static int64_t vm_swapcache_accrate
= 100000LL; /* 100K/s */
108 static int64_t vm_swapcache_write_count
;
109 static int64_t vm_swapcache_maxfilesize
;
111 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, maxlaunder
,
112 CTLFLAG_RW
, &vm_swapcache_maxlaunder
, 0, "");
114 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, data_enable
,
115 CTLFLAG_RW
, &vm_swapcache_data_enable
, 0, "");
116 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, meta_enable
,
117 CTLFLAG_RW
, &vm_swapcache_meta_enable
, 0, "");
118 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, read_enable
,
119 CTLFLAG_RW
, &vm_swapcache_read_enable
, 0, "");
120 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, maxswappct
,
121 CTLFLAG_RW
, &vm_swapcache_maxswappct
, 0, "");
122 SYSCTL_INT(_vm_swapcache
, OID_AUTO
, use_chflags
,
123 CTLFLAG_RW
, &vm_swapcache_use_chflags
, 0, "");
125 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, minburst
,
126 CTLFLAG_RW
, &vm_swapcache_minburst
, 0, "");
127 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, curburst
,
128 CTLFLAG_RW
, &vm_swapcache_curburst
, 0, "");
129 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, maxburst
,
130 CTLFLAG_RW
, &vm_swapcache_maxburst
, 0, "");
131 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, maxfilesize
,
132 CTLFLAG_RW
, &vm_swapcache_maxfilesize
, 0, "");
133 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, accrate
,
134 CTLFLAG_RW
, &vm_swapcache_accrate
, 0, "");
135 SYSCTL_QUAD(_vm_swapcache
, OID_AUTO
, write_count
,
136 CTLFLAG_RW
, &vm_swapcache_write_count
, 0, "");
138 #define SWAPMAX(adj) \
139 ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
142 * vm_swapcached is the high level pageout daemon.
147 enum { SWAPC_WRITING
, SWAPC_CLEANING
} state
= SWAPC_WRITING
;
148 enum { SWAPB_BURSTING
, SWAPB_RECOVERING
} burst
= SWAPB_BURSTING
;
149 struct vm_page page_marker
;
150 struct vm_object object_marker
;
155 curthread
->td_flags
|= TDF_SYSTHREAD
;
159 * Initialize our marker for the inactive scan (SWAPC_WRITING)
161 bzero(&page_marker
, sizeof(page_marker
));
162 page_marker
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_MARKER
;
163 page_marker
.queue
= PQ_INACTIVE
;
164 page_marker
.wire_count
= 1;
165 TAILQ_INSERT_HEAD(INACTIVE_LIST
, &page_marker
, pageq
);
168 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
170 bzero(&object_marker
, sizeof(object_marker
));
171 object_marker
.type
= OBJT_MARKER
;
172 TAILQ_INSERT_HEAD(&vm_object_list
, &object_marker
, object_list
);
176 * Check every 5 seconds when not enabled or if no swap
179 if ((vm_swapcache_data_enable
== 0 &&
180 vm_swapcache_meta_enable
== 0) ||
182 tsleep(&vm_swapcache_sleep
, 0, "csleep", hz
* 5);
187 * Polling rate when enabled is approximately 10 hz.
189 tsleep(&vm_swapcache_sleep
, 0, "csleep", hz
/ 10);
192 * State hysteresis. Generate write activity up to 75% of
193 * swap, then clean out swap assignments down to 70%, then
196 if (state
== SWAPC_WRITING
) {
197 if (vm_swap_cache_use
> SWAPMAX(0))
198 state
= SWAPC_CLEANING
;
200 if (vm_swap_cache_use
< SWAPMAX(-5))
201 state
= SWAPC_WRITING
;
205 * We are allowed to continue accumulating burst value
206 * in either state. Allow the user to set curburst > maxburst
207 * for the initial load-in.
209 if (vm_swapcache_curburst
< vm_swapcache_maxburst
) {
210 vm_swapcache_curburst
+= vm_swapcache_accrate
/ 10;
211 if (vm_swapcache_curburst
> vm_swapcache_maxburst
)
212 vm_swapcache_curburst
= vm_swapcache_maxburst
;
216 * We don't want to nickle-and-dime the scan as that will
217 * create unnecessary fragmentation. The minimum burst
218 * is one-seconds worth of accumulation.
220 if (state
== SWAPC_WRITING
) {
221 if (vm_swapcache_curburst
>= vm_swapcache_accrate
) {
222 if (burst
== SWAPB_BURSTING
) {
223 vm_swapcache_writing(&page_marker
);
224 if (vm_swapcache_curburst
<= 0)
225 burst
= SWAPB_RECOVERING
;
226 } else if (vm_swapcache_curburst
>
227 vm_swapcache_minburst
) {
228 vm_swapcache_writing(&page_marker
);
229 burst
= SWAPB_BURSTING
;
233 vm_swapcache_cleaning(&object_marker
);
236 TAILQ_REMOVE(INACTIVE_LIST
, &page_marker
, pageq
);
237 TAILQ_REMOVE(&vm_object_list
, &object_marker
, object_list
);
242 vm_swapcache_writing(vm_page_t marker
)
250 * Scan the inactive queue from our marker to locate
251 * suitable pages to push to the swap cache.
253 * We are looking for clean vnode-backed pages.
255 * NOTE: PG_SWAPPED pages in particular are not part of
256 * our count because once the cache stabilizes we
257 * can end up with a very high datarate of VM pages
261 count
= vm_swapcache_maxlaunder
;
263 while ((m
= TAILQ_NEXT(m
, pageq
)) != NULL
&& count
--) {
264 if (m
->flags
& (PG_MARKER
| PG_SWAPPED
)) {
268 if (vm_swapcache_curburst
< 0)
270 if (vm_swapcache_test(m
))
280 * If data_enable is 0 do not try to swapcache data.
281 * If use_chflags is set then only swapcache data for
282 * VSWAPCACHE marked vnodes, otherwise any vnode.
284 if (vm_swapcache_data_enable
== 0 ||
285 ((vp
->v_flag
& VSWAPCACHE
) == 0 &&
286 vm_swapcache_use_chflags
)) {
289 if (vm_swapcache_maxfilesize
&&
291 (vm_swapcache_maxfilesize
>> PAGE_SHIFT
)) {
296 if (vm_swapcache_meta_enable
== 0)
304 * Ok, move the marker and soft-busy the page.
306 TAILQ_REMOVE(INACTIVE_LIST
, marker
, pageq
);
307 TAILQ_INSERT_AFTER(INACTIVE_LIST
, m
, marker
, pageq
);
310 * Assign swap and initiate I/O.
312 * (adjust for the --count which also occurs in the loop)
314 count
-= vm_swapcached_flush(m
) - 1;
317 * Setup for next loop using marker.
323 * Cleanup marker position. If we hit the end of the
324 * list the marker is placed at the tail. Newly deactivated
325 * pages will be placed after it.
327 * Earlier inactive pages that were dirty and become clean
328 * are typically moved to the end of PQ_INACTIVE by virtue
329 * of vfs_vmio_release() when they become unwired from the
332 TAILQ_REMOVE(INACTIVE_LIST
, marker
, pageq
);
334 TAILQ_INSERT_BEFORE(m
, marker
, pageq
);
336 TAILQ_INSERT_TAIL(INACTIVE_LIST
, marker
, pageq
);
340 * Flush the specified page using the swap_pager.
342 * Try to collect surrounding pages, including pages which may
343 * have already been assigned swap. Try to cluster within a
344 * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
345 * to match what swap_pager_putpages() can do.
347 * We also want to try to match against the buffer cache blocksize
348 * but we don't really know what it is here. Since the buffer cache
349 * wires and unwires pages in groups the fact that we skip wired pages
350 * should be sufficient.
352 * Returns a count of pages we might have flushed (minimum 1)
356 vm_swapcached_flush(vm_page_t m
)
359 vm_page_t marray
[SWAP_META_PAGES
];
361 int rtvals
[SWAP_META_PAGES
];
368 vm_page_protect(m
, VM_PROT_READ
);
372 * Try to cluster around (m), keeping in mind that the swap pager
373 * can only do SMAP_META_PAGES worth of continguous write.
375 x
= (int)m
->pindex
& SWAP_META_MASK
;
379 for (i
= x
- 1; i
>= 0; --i
) {
380 m
= vm_page_lookup(object
, basei
- x
+ i
);
383 if (vm_swapcache_test(m
))
386 vm_page_protect(m
, VM_PROT_READ
);
387 if (m
->queue
- m
->pc
== PQ_CACHE
) {
388 vm_page_unqueue_nowakeup(m
);
389 vm_page_deactivate(m
);
395 for (j
= x
+ 1; j
< SWAP_META_PAGES
; ++j
) {
396 m
= vm_page_lookup(object
, basei
- x
+ j
);
399 if (vm_swapcache_test(m
))
402 vm_page_protect(m
, VM_PROT_READ
);
403 if (m
->queue
- m
->pc
== PQ_CACHE
) {
404 vm_page_unqueue_nowakeup(m
);
405 vm_page_deactivate(m
);
411 vm_object_pip_add(object
, count
);
412 swap_pager_putpages(object
, marray
+ i
, count
, FALSE
, rtvals
+ i
);
413 vm_swapcache_write_count
+= count
* PAGE_SIZE
;
414 vm_swapcache_curburst
-= count
* PAGE_SIZE
;
417 if (rtvals
[i
] != VM_PAGER_PEND
) {
418 vm_page_io_finish(marray
[i
]);
419 vm_object_pip_wakeup(object
);
427 * Test whether a VM page is suitable for writing to the swapcache.
428 * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
430 * Returns 0 on success, 1 on failure
433 vm_swapcache_test(vm_page_t m
)
437 if (m
->flags
& (PG_BUSY
| PG_UNMANAGED
| PG_NOTMETA
))
439 if (m
->busy
|| m
->hold_count
|| m
->wire_count
)
441 if (m
->valid
!= VM_PAGE_BITS_ALL
)
443 if (m
->dirty
& m
->valid
)
445 if ((object
= m
->object
) == NULL
)
447 if (object
->type
!= OBJT_VNODE
||
448 (object
->flags
& OBJ_DEAD
)) {
451 vm_page_test_dirty(m
);
452 if (m
->dirty
& m
->valid
)
462 vm_swapcache_cleaning(vm_object_t marker
)
470 count
= vm_swapcache_maxlaunder
;
473 * Look for vnode objects
475 while ((object
= TAILQ_NEXT(object
, object_list
)) != NULL
&& count
--) {
476 if (object
->type
!= OBJT_VNODE
)
478 if ((object
->flags
& OBJ_DEAD
) || object
->swblock_count
== 0)
480 if ((vp
= object
->handle
) == NULL
)
482 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
)
488 if (marker
->backing_object
!= object
)
492 * Move the marker so we can work on the VM object
494 TAILQ_REMOVE(&vm_object_list
, marker
, object_list
);
495 TAILQ_INSERT_AFTER(&vm_object_list
, object
,
496 marker
, object_list
);
499 * Look for swblocks starting at our iterator.
501 * The swap_pager_condfree() function attempts to free
502 * swap space starting at the specified index. The index
503 * will be updated on return. The function will return
504 * a scan factor (NOT the number of blocks freed).
506 * If it must cut its scan of the object short due to an
507 * excessive number of swblocks, or is able to free the
508 * requested number of blocks, it will return n >= count
509 * and we break and pick it back up on a future attempt.
511 n
= swap_pager_condfree(object
, &marker
->size
, count
);
524 * Adjust marker so we continue the scan from where we left off.
525 * When we reach the end we start back at the beginning.
527 TAILQ_REMOVE(&vm_object_list
, marker
, object_list
);
529 TAILQ_INSERT_BEFORE(object
, marker
, object_list
);
531 TAILQ_INSERT_HEAD(&vm_object_list
, marker
, object_list
);
532 marker
->backing_object
= object
;