4 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Implement the swapcache daemon. When enabled swap is assumed to be
39 * configured on a fast storage device such as a SSD. Swap is assigned
40 * to clean vnode-backed pages in the inactive queue, clustered by object
41 * if possible, and written out. The swap assignment sticks around even
42 * after the underlying pages have been recycled.
44 * The daemon manages write bandwidth based on sysctl settings to control
47 * The vnode strategy code will check for the swap assignments and divert
48 * reads to the swap device when the data is present in the swapcache.
50 * This operates on both regular files and the block device vnodes used by
51 * filesystems to manage meta-data.
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/vnode.h>
63 #include <sys/vmmeter.h>
64 #include <sys/sysctl.h>
65 #include <sys/eventhandler.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_pager.h>
75 #include <vm/swap_pager.h>
76 #include <vm/vm_extern.h>
78 #include <sys/thread2.h>
79 #include <sys/spinlock2.h>
80 #include <vm/vm_page2.h>
82 /* the kernel process "vm_pageout"*/
83 static int vm_swapcached_flush (vm_page_t m, int isblkdev);
84 static int vm_swapcache_test(vm_page_t m);
85 static void vm_swapcache_writing(vm_page_t marker);
86 static void vm_swapcache_cleaning(vm_object_t marker);
87 struct thread *swapcached_thread;
89 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
91 int vm_swapcache_read_enable;
92 int vm_swapcache_inactive_heuristic;
93 static int vm_swapcache_sleep;
94 static int vm_swapcache_maxlaunder = 256;
95 static int vm_swapcache_data_enable = 0;
96 static int vm_swapcache_meta_enable = 0;
97 static int vm_swapcache_maxswappct = 75;
98 static int vm_swapcache_hysteresis;
99 int vm_swapcache_use_chflags = 1; /* require chflags cache */
100 static int64_t vm_swapcache_minburst = 10000000LL; /* 10MB */
101 static int64_t vm_swapcache_curburst = 4000000000LL; /* 4G after boot */
102 static int64_t vm_swapcache_maxburst = 2000000000LL; /* 2G nominal max */
103 static int64_t vm_swapcache_accrate = 100000LL; /* 100K/s */
104 static int64_t vm_swapcache_write_count;
105 static int64_t vm_swapcache_maxfilesize;
107 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
108 CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
110 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
111 CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
112 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
113 CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
114 SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
115 CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
116 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
117 CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
118 SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
119 CTLFLAG_RW, &vm_swapcache_hysteresis, 0, "");
120 SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
121 CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
123 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
124 CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
125 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
126 CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
127 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
128 CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
129 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
130 CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
131 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
132 CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
133 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
134 CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
136 #define SWAPMAX(adj) \
137 ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
140 * When shutting down the machine we want to stop swapcache operation
141 * immediately so swap is not accessed after devices have been shuttered.
144 shutdown_swapcache(void *arg __unused)
146 vm_swapcache_read_enable = 0;
147 vm_swapcache_data_enable = 0;
148 vm_swapcache_meta_enable = 0;
149 wakeup(&vm_swapcache_sleep); /* shortcut 5-second wait */
153 * vm_swapcached is the high level pageout daemon.
158 vm_swapcached_thread(void)
160 enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
161 enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
162 static struct vm_page page_marker[PQ_MAXL2_SIZE];
163 static struct vm_object object_marker;
169 curthread->td_flags |= TDF_SYSTHREAD;
170 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
171 swapcached_thread, SHUTDOWN_PRI_FIRST);
172 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
173 NULL, SHUTDOWN_PRI_SECOND);
176 * Initialize our marker for the inactive scan (SWAPC_WRITING)
178 bzero(&page_marker, sizeof(page_marker));
179 for (q = 0; q < PQ_MAXL2_SIZE; ++q) {
180 page_marker[q].flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
181 page_marker[q].queue = PQ_INACTIVE + q;
182 page_marker[q].pc = q;
183 page_marker[q].wire_count = 1;
184 vm_page_queues_spin_lock(PQ_INACTIVE + q);
186 &vm_page_queues[PQ_INACTIVE + q].pl,
187 &page_marker[q], pageq);
188 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
191 vm_swapcache_hysteresis = vmstats.v_inactive_target / 2;
192 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
195 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
197 bzero(&object_marker, sizeof(object_marker));
198 object_marker.type = OBJT_MARKER;
199 lwkt_gettoken(&vmobj_token);
200 TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
201 lwkt_reltoken(&vmobj_token);
207 kproc_suspend_loop();
210 * Check every 5 seconds when not enabled or if no swap
213 if ((vm_swapcache_data_enable == 0 &&
214 vm_swapcache_meta_enable == 0) ||
216 tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
221 * Polling rate when enabled is approximately 10 hz.
223 tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
226 * State hysteresis. Generate write activity up to 75% of
227 * swap, then clean out swap assignments down to 70%, then
230 if (state == SWAPC_WRITING) {
231 if (vm_swap_cache_use > SWAPMAX(0))
232 state = SWAPC_CLEANING;
234 if (vm_swap_cache_use < SWAPMAX(-5))
235 state = SWAPC_WRITING;
239 * We are allowed to continue accumulating burst value
240 * in either state. Allow the user to set curburst > maxburst
241 * for the initial load-in.
243 if (vm_swapcache_curburst < vm_swapcache_maxburst) {
244 vm_swapcache_curburst += vm_swapcache_accrate / 10;
245 if (vm_swapcache_curburst > vm_swapcache_maxburst)
246 vm_swapcache_curburst = vm_swapcache_maxburst;
250 * We don't want to nickle-and-dime the scan as that will
251 * create unnecessary fragmentation. The minimum burst
252 * is one-seconds worth of accumulation.
254 if (state == SWAPC_WRITING) {
255 if (vm_swapcache_curburst >= vm_swapcache_accrate) {
256 if (burst == SWAPB_BURSTING) {
257 for (q = 0; q < PQ_MAXL2_SIZE; ++q) {
258 vm_swapcache_writing(
261 if (vm_swapcache_curburst <= 0)
262 burst = SWAPB_RECOVERING;
263 } else if (vm_swapcache_curburst >
264 vm_swapcache_minburst) {
265 for (q = 0; q < PQ_MAXL2_SIZE; ++q) {
266 vm_swapcache_writing(
269 burst = SWAPB_BURSTING;
273 vm_swapcache_cleaning(&object_marker);
278 * Cleanup (NOT REACHED)
280 for (q = 0; q < PQ_MAXL2_SIZE; ++q) {
281 vm_page_queues_spin_lock(PQ_INACTIVE + q);
283 &vm_page_queues[PQ_INACTIVE + q].pl,
284 &page_marker[q], pageq);
285 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
288 lwkt_gettoken(&vmobj_token);
289 TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
290 lwkt_reltoken(&vmobj_token);
293 static struct kproc_desc swpc_kp = {
295 vm_swapcached_thread,
298 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
301 vm_swapcache_writing(vm_page_t marker)
310 * Deal with an overflow of the heuristic counter or if the user
311 * manually changes the hysteresis.
313 * Try to avoid small incremental pageouts by waiting for enough
314 * pages to buildup in the inactive queue to hopefully get a good
315 * burst in. This heuristic is bumped by the VM system and reset
316 * when our scan hits the end of the queue.
318 if (vm_swapcache_inactive_heuristic < -vm_swapcache_hysteresis)
319 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
320 if (vm_swapcache_inactive_heuristic < 0)
324 * Scan the inactive queue from our marker to locate
325 * suitable pages to push to the swap cache.
327 * We are looking for clean vnode-backed pages.
329 * NOTE: PG_SWAPPED pages in particular are not part of
330 * our count because once the cache stabilizes we
331 * can end up with a very high datarate of VM pages
334 count = vm_swapcache_maxlaunder;
336 vm_page_queues_spin_lock(marker->queue);
337 while ((m = TAILQ_NEXT(marker, pageq)) != NULL && count-- > 0) {
338 KKASSERT(m->queue == marker->queue);
340 if (vm_swapcache_curburst < 0)
343 &vm_page_queues[marker->queue].pl, marker, pageq);
345 &vm_page_queues[marker->queue].pl, m, marker, pageq);
346 if (m->flags & (PG_MARKER | PG_SWAPPED)) {
350 if (vm_page_busy_try(m, TRUE))
352 vm_page_queues_spin_unlock(marker->queue);
354 if ((object = m->object) == NULL) {
356 vm_page_queues_spin_lock(marker->queue);
359 vm_object_hold(object);
360 if (m->object != object) {
361 vm_object_drop(object);
363 vm_page_queues_spin_lock(marker->queue);
366 if (vm_swapcache_test(m)) {
367 vm_object_drop(object);
369 vm_page_queues_spin_lock(marker->queue);
375 vm_object_drop(object);
377 vm_page_queues_spin_lock(marker->queue);
384 * PG_NOTMETA generically means 'don't swapcache this',
385 * and HAMMER will set this for regular data buffers
386 * (and leave it unset for meta-data buffers) as
387 * appropriate when double buffering is enabled.
389 if (m->flags & PG_NOTMETA) {
390 vm_object_drop(object);
392 vm_page_queues_spin_lock(marker->queue);
397 * If data_enable is 0 do not try to swapcache data.
398 * If use_chflags is set then only swapcache data for
399 * VSWAPCACHE marked vnodes, otherwise any vnode.
401 if (vm_swapcache_data_enable == 0 ||
402 ((vp->v_flag & VSWAPCACHE) == 0 &&
403 vm_swapcache_use_chflags)) {
404 vm_object_drop(object);
406 vm_page_queues_spin_lock(marker->queue);
409 if (vm_swapcache_maxfilesize &&
411 (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
412 vm_object_drop(object);
414 vm_page_queues_spin_lock(marker->queue);
421 * PG_NOTMETA generically means 'don't swapcache this',
422 * and HAMMER will set this for regular data buffers
423 * (and leave it unset for meta-data buffers) as
424 * appropriate when double buffering is enabled.
426 if (m->flags & PG_NOTMETA) {
427 vm_object_drop(object);
429 vm_page_queues_spin_lock(marker->queue);
432 if (vm_swapcache_meta_enable == 0) {
433 vm_object_drop(object);
435 vm_page_queues_spin_lock(marker->queue);
441 vm_object_drop(object);
443 vm_page_queues_spin_lock(marker->queue);
449 * Assign swap and initiate I/O.
451 * (adjust for the --count which also occurs in the loop)
453 count -= vm_swapcached_flush(m, isblkdev) - 1;
456 * Setup for next loop using marker.
458 vm_object_drop(object);
459 vm_page_queues_spin_lock(marker->queue);
463 * The marker could wind up at the end, which is ok. If we hit the
464 * end of the list adjust the heuristic.
466 * Earlier inactive pages that were dirty and become clean
467 * are typically moved to the end of PQ_INACTIVE by virtue
468 * of vfs_vmio_release() when they become unwired from the
472 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
473 vm_page_queues_spin_unlock(marker->queue);
477 * Flush the specified page using the swap_pager. The page
478 * must be busied by the caller and its disposition will become
479 * the responsibility of this function.
481 * Try to collect surrounding pages, including pages which may
482 * have already been assigned swap. Try to cluster within a
483 * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
484 * to match what swap_pager_putpages() can do.
486 * We also want to try to match against the buffer cache blocksize
487 * but we don't really know what it is here. Since the buffer cache
488 * wires and unwires pages in groups the fact that we skip wired pages
489 * should be sufficient.
491 * Returns a count of pages we might have flushed (minimum 1)
495 vm_swapcached_flush(vm_page_t m, int isblkdev)
498 vm_page_t marray[SWAP_META_PAGES];
500 int rtvals[SWAP_META_PAGES];
508 vm_page_protect(m, VM_PROT_READ);
510 vm_object_hold(object);
513 * Try to cluster around (m), keeping in mind that the swap pager
514 * can only do SMAP_META_PAGES worth of continguous write.
516 x = (int)m->pindex & SWAP_META_MASK;
521 for (i = x - 1; i >= 0; --i) {
522 m = vm_page_lookup_busy_try(object, basei - x + i,
524 if (error || m == NULL)
526 if (vm_swapcache_test(m)) {
530 if (isblkdev && (m->flags & PG_NOTMETA)) {
535 vm_page_protect(m, VM_PROT_READ);
536 if (m->queue - m->pc == PQ_CACHE) {
537 vm_page_unqueue_nowakeup(m);
538 vm_page_deactivate(m);
545 for (j = x + 1; j < SWAP_META_PAGES; ++j) {
546 m = vm_page_lookup_busy_try(object, basei - x + j,
548 if (error || m == NULL)
550 if (vm_swapcache_test(m)) {
554 if (isblkdev && (m->flags & PG_NOTMETA)) {
559 vm_page_protect(m, VM_PROT_READ);
560 if (m->queue - m->pc == PQ_CACHE) {
561 vm_page_unqueue_nowakeup(m);
562 vm_page_deactivate(m);
569 vm_object_pip_add(object, count);
570 swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
571 vm_swapcache_write_count += count * PAGE_SIZE;
572 vm_swapcache_curburst -= count * PAGE_SIZE;
575 if (rtvals[i] != VM_PAGER_PEND) {
576 vm_page_busy_wait(marray[i], FALSE, "swppgfd");
577 vm_page_io_finish(marray[i]);
578 vm_page_wakeup(marray[i]);
579 vm_object_pip_wakeup(object);
583 vm_object_drop(object);
588 * Test whether a VM page is suitable for writing to the swapcache.
589 * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
591 * Returns 0 on success, 1 on failure
594 vm_swapcache_test(vm_page_t m)
598 if (m->flags & PG_UNMANAGED)
600 if (m->hold_count || m->wire_count)
602 if (m->valid != VM_PAGE_BITS_ALL)
604 if (m->dirty & m->valid)
606 if ((object = m->object) == NULL)
608 if (object->type != OBJT_VNODE ||
609 (object->flags & OBJ_DEAD)) {
612 vm_page_test_dirty(m);
613 if (m->dirty & m->valid)
623 vm_swapcache_cleaning(vm_object_t marker)
631 count = vm_swapcache_maxlaunder;
634 * Look for vnode objects
636 lwkt_gettoken(&vmobj_token);
638 while ((object = TAILQ_NEXT(object, object_list)) != NULL) {
642 vm_object_hold(object);
645 * Only operate on live VNODE objects with regular/chardev types
647 if ((object->type != OBJT_VNODE) ||
648 ((object->flags & OBJ_DEAD) || object->swblock_count == 0) ||
649 ((vp = object->handle) == NULL) ||
650 (vp->v_type != VREG && vp->v_type != VCHR)) {
651 vm_object_drop(object);
658 if (marker->backing_object != object)
662 * Move the marker so we can work on the VM object
664 TAILQ_REMOVE(&vm_object_list, marker, object_list);
665 TAILQ_INSERT_AFTER(&vm_object_list, object,
666 marker, object_list);
669 * Look for swblocks starting at our iterator.
671 * The swap_pager_condfree() function attempts to free
672 * swap space starting at the specified index. The index
673 * will be updated on return. The function will return
674 * a scan factor (NOT the number of blocks freed).
676 * If it must cut its scan of the object short due to an
677 * excessive number of swblocks, or is able to free the
678 * requested number of blocks, it will return n >= count
679 * and we break and pick it back up on a future attempt.
681 n = swap_pager_condfree(object, &marker->size, count);
683 vm_object_drop(object);
697 * Adjust marker so we continue the scan from where we left off.
698 * When we reach the end we start back at the beginning.
700 TAILQ_REMOVE(&vm_object_list, marker, object_list);
702 TAILQ_INSERT_BEFORE(object, marker, object_list);
704 TAILQ_INSERT_HEAD(&vm_object_list, marker, object_list);
705 marker->backing_object = object;
707 lwkt_reltoken(&vmobj_token);