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 static void vm_swapcache_movemarker(vm_object_t marker, vm_object_t object);
88 struct thread *swapcached_thread;
90 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
92 int vm_swapcache_read_enable;
93 int vm_swapcache_inactive_heuristic;
94 static int vm_swapcache_sleep;
95 static int vm_swapcache_maxscan = 256 * 4;
96 static int vm_swapcache_maxlaunder = 256;
97 static int vm_swapcache_data_enable = 0;
98 static int vm_swapcache_meta_enable = 0;
99 static int vm_swapcache_maxswappct = 75;
100 static int vm_swapcache_hysteresis;
101 int vm_swapcache_use_chflags = 1; /* require chflags cache */
102 static int64_t vm_swapcache_minburst = 10000000LL; /* 10MB */
103 static int64_t vm_swapcache_curburst = 4000000000LL; /* 4G after boot */
104 static int64_t vm_swapcache_maxburst = 2000000000LL; /* 2G nominal max */
105 static int64_t vm_swapcache_accrate = 100000LL; /* 100K/s */
106 static int64_t vm_swapcache_write_count;
107 static int64_t vm_swapcache_maxfilesize;
108 static int64_t vm_swapcache_cleanperobj = 16*1024*1024;
110 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
111 CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
112 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxscan,
113 CTLFLAG_RW, &vm_swapcache_maxscan, 0, "");
115 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
116 CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
117 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
118 CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
119 SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
120 CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
121 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
122 CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
123 SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
124 CTLFLAG_RW, &vm_swapcache_hysteresis, 0, "");
125 SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
126 CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
128 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
129 CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
130 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
131 CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
132 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
133 CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
134 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
135 CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
136 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
137 CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
138 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
139 CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
140 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, cleanperobj,
141 CTLFLAG_RW, &vm_swapcache_cleanperobj, 0, "");
143 #define SWAPMAX(adj) \
144 ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
147 * When shutting down the machine we want to stop swapcache operation
148 * immediately so swap is not accessed after devices have been shuttered.
151 shutdown_swapcache(void *arg __unused)
153 vm_swapcache_read_enable = 0;
154 vm_swapcache_data_enable = 0;
155 vm_swapcache_meta_enable = 0;
156 wakeup(&vm_swapcache_sleep); /* shortcut 5-second wait */
160 * vm_swapcached is the high level pageout daemon.
165 vm_swapcached_thread(void)
167 enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
168 enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
169 static struct vm_page page_marker[PQ_L2_SIZE];
170 static struct vm_object object_marker;
176 curthread->td_flags |= TDF_SYSTHREAD;
177 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
178 swapcached_thread, SHUTDOWN_PRI_FIRST);
179 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
180 NULL, SHUTDOWN_PRI_SECOND);
183 * Initialize our marker for the inactive scan (SWAPC_WRITING)
185 bzero(&page_marker, sizeof(page_marker));
186 for (q = 0; q < PQ_L2_SIZE; ++q) {
187 page_marker[q].flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
188 page_marker[q].queue = PQ_INACTIVE + q;
189 page_marker[q].pc = q;
190 page_marker[q].wire_count = 1;
191 vm_page_queues_spin_lock(PQ_INACTIVE + q);
193 &vm_page_queues[PQ_INACTIVE + q].pl,
194 &page_marker[q], pageq);
195 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
198 vm_swapcache_hysteresis = vmstats.v_inactive_target / 2;
199 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
202 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
204 bzero(&object_marker, sizeof(object_marker));
205 object_marker.type = OBJT_MARKER;
206 lwkt_gettoken(&vmobj_token);
207 TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
208 lwkt_reltoken(&vmobj_token);
214 kproc_suspend_loop();
217 * Check every 5 seconds when not enabled or if no swap
220 if ((vm_swapcache_data_enable == 0 &&
221 vm_swapcache_meta_enable == 0) ||
223 tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
228 * Polling rate when enabled is approximately 10 hz.
230 tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
233 * State hysteresis. Generate write activity up to 75% of
234 * swap, then clean out swap assignments down to 70%, then
237 if (state == SWAPC_WRITING) {
238 if (vm_swap_cache_use > SWAPMAX(0))
239 state = SWAPC_CLEANING;
241 if (vm_swap_cache_use < SWAPMAX(-10))
242 state = SWAPC_WRITING;
246 * We are allowed to continue accumulating burst value
247 * in either state. Allow the user to set curburst > maxburst
248 * for the initial load-in.
250 if (vm_swapcache_curburst < vm_swapcache_maxburst) {
251 vm_swapcache_curburst += vm_swapcache_accrate / 10;
252 if (vm_swapcache_curburst > vm_swapcache_maxburst)
253 vm_swapcache_curburst = vm_swapcache_maxburst;
257 * We don't want to nickle-and-dime the scan as that will
258 * create unnecessary fragmentation. The minimum burst
259 * is one-seconds worth of accumulation.
261 if (state == SWAPC_WRITING) {
262 if (vm_swapcache_curburst >= vm_swapcache_accrate) {
263 if (burst == SWAPB_BURSTING) {
264 for (q = 0; q < PQ_L2_SIZE; ++q) {
265 vm_swapcache_writing(
268 if (vm_swapcache_curburst <= 0)
269 burst = SWAPB_RECOVERING;
270 } else if (vm_swapcache_curburst >
271 vm_swapcache_minburst) {
272 for (q = 0; q < PQ_L2_SIZE; ++q) {
273 vm_swapcache_writing(
276 burst = SWAPB_BURSTING;
280 vm_swapcache_cleaning(&object_marker);
285 * Cleanup (NOT REACHED)
287 for (q = 0; q < PQ_L2_SIZE; ++q) {
288 vm_page_queues_spin_lock(PQ_INACTIVE + q);
290 &vm_page_queues[PQ_INACTIVE + q].pl,
291 &page_marker[q], pageq);
292 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
295 lwkt_gettoken(&vmobj_token);
296 TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
297 lwkt_reltoken(&vmobj_token);
300 static struct kproc_desc swpc_kp = {
302 vm_swapcached_thread,
305 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
308 vm_swapcache_writing(vm_page_t marker)
318 * Deal with an overflow of the heuristic counter or if the user
319 * manually changes the hysteresis.
321 * Try to avoid small incremental pageouts by waiting for enough
322 * pages to buildup in the inactive queue to hopefully get a good
323 * burst in. This heuristic is bumped by the VM system and reset
324 * when our scan hits the end of the queue.
326 if (vm_swapcache_inactive_heuristic < -vm_swapcache_hysteresis)
327 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
328 if (vm_swapcache_inactive_heuristic < 0)
332 * Scan the inactive queue from our marker to locate
333 * suitable pages to push to the swap cache.
335 * We are looking for clean vnode-backed pages.
337 count = vm_swapcache_maxlaunder;
338 scount = vm_swapcache_maxscan;
340 vm_page_queues_spin_lock(marker->queue);
341 while ((m = TAILQ_NEXT(marker, pageq)) != NULL &&
342 count > 0 && scount-- > 0) {
343 KKASSERT(m->queue == marker->queue);
345 if (vm_swapcache_curburst < 0)
348 &vm_page_queues[marker->queue].pl, marker, pageq);
350 &vm_page_queues[marker->queue].pl, m, marker, pageq);
353 * Ignore markers and ignore pages that already have a swap
356 if (m->flags & (PG_MARKER | PG_SWAPPED))
358 if (vm_page_busy_try(m, TRUE))
360 vm_page_queues_spin_unlock(marker->queue);
362 if ((object = m->object) == NULL) {
364 vm_page_queues_spin_lock(marker->queue);
367 vm_object_hold(object);
368 if (m->object != object) {
369 vm_object_drop(object);
371 vm_page_queues_spin_lock(marker->queue);
374 if (vm_swapcache_test(m)) {
375 vm_object_drop(object);
377 vm_page_queues_spin_lock(marker->queue);
383 vm_object_drop(object);
385 vm_page_queues_spin_lock(marker->queue);
392 * PG_NOTMETA generically means 'don't swapcache this',
393 * and HAMMER will set this for regular data buffers
394 * (and leave it unset for meta-data buffers) as
395 * appropriate when double buffering is enabled.
397 if (m->flags & PG_NOTMETA) {
398 vm_object_drop(object);
400 vm_page_queues_spin_lock(marker->queue);
405 * If data_enable is 0 do not try to swapcache data.
406 * If use_chflags is set then only swapcache data for
407 * VSWAPCACHE marked vnodes, otherwise any vnode.
409 if (vm_swapcache_data_enable == 0 ||
410 ((vp->v_flag & VSWAPCACHE) == 0 &&
411 vm_swapcache_use_chflags)) {
412 vm_object_drop(object);
414 vm_page_queues_spin_lock(marker->queue);
417 if (vm_swapcache_maxfilesize &&
419 (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
420 vm_object_drop(object);
422 vm_page_queues_spin_lock(marker->queue);
429 * PG_NOTMETA generically means 'don't swapcache this',
430 * and HAMMER will set this for regular data buffers
431 * (and leave it unset for meta-data buffers) as
432 * appropriate when double buffering is enabled.
434 if (m->flags & PG_NOTMETA) {
435 vm_object_drop(object);
437 vm_page_queues_spin_lock(marker->queue);
440 if (vm_swapcache_meta_enable == 0) {
441 vm_object_drop(object);
443 vm_page_queues_spin_lock(marker->queue);
449 vm_object_drop(object);
451 vm_page_queues_spin_lock(marker->queue);
457 * Assign swap and initiate I/O.
459 * (adjust for the --count which also occurs in the loop)
461 count -= vm_swapcached_flush(m, isblkdev);
464 * Setup for next loop using marker.
466 vm_object_drop(object);
467 vm_page_queues_spin_lock(marker->queue);
471 * The marker could wind up at the end, which is ok. If we hit the
472 * end of the list adjust the heuristic.
474 * Earlier inactive pages that were dirty and become clean
475 * are typically moved to the end of PQ_INACTIVE by virtue
476 * of vfs_vmio_release() when they become unwired from the
480 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
481 vm_page_queues_spin_unlock(marker->queue);
485 * Flush the specified page using the swap_pager. The page
486 * must be busied by the caller and its disposition will become
487 * the responsibility of this function.
489 * Try to collect surrounding pages, including pages which may
490 * have already been assigned swap. Try to cluster within a
491 * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
492 * to match what swap_pager_putpages() can do.
494 * We also want to try to match against the buffer cache blocksize
495 * but we don't really know what it is here. Since the buffer cache
496 * wires and unwires pages in groups the fact that we skip wired pages
497 * should be sufficient.
499 * Returns a count of pages we might have flushed (minimum 1)
503 vm_swapcached_flush(vm_page_t m, int isblkdev)
506 vm_page_t marray[SWAP_META_PAGES];
508 int rtvals[SWAP_META_PAGES];
516 vm_page_protect(m, VM_PROT_READ);
518 vm_object_hold(object);
521 * Try to cluster around (m), keeping in mind that the swap pager
522 * can only do SMAP_META_PAGES worth of continguous write.
524 x = (int)m->pindex & SWAP_META_MASK;
529 for (i = x - 1; i >= 0; --i) {
530 m = vm_page_lookup_busy_try(object, basei - x + i,
532 if (error || m == NULL)
534 if (vm_swapcache_test(m)) {
538 if (isblkdev && (m->flags & PG_NOTMETA)) {
543 vm_page_protect(m, VM_PROT_READ);
544 if (m->queue - m->pc == PQ_CACHE) {
545 vm_page_unqueue_nowakeup(m);
546 vm_page_deactivate(m);
553 for (j = x + 1; j < SWAP_META_PAGES; ++j) {
554 m = vm_page_lookup_busy_try(object, basei - x + j,
556 if (error || m == NULL)
558 if (vm_swapcache_test(m)) {
562 if (isblkdev && (m->flags & PG_NOTMETA)) {
567 vm_page_protect(m, VM_PROT_READ);
568 if (m->queue - m->pc == PQ_CACHE) {
569 vm_page_unqueue_nowakeup(m);
570 vm_page_deactivate(m);
577 vm_object_pip_add(object, count);
578 swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
579 vm_swapcache_write_count += count * PAGE_SIZE;
580 vm_swapcache_curburst -= count * PAGE_SIZE;
583 if (rtvals[i] != VM_PAGER_PEND) {
584 vm_page_busy_wait(marray[i], FALSE, "swppgfd");
585 vm_page_io_finish(marray[i]);
586 vm_page_wakeup(marray[i]);
587 vm_object_pip_wakeup(object);
591 vm_object_drop(object);
596 * Test whether a VM page is suitable for writing to the swapcache.
597 * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
599 * Returns 0 on success, 1 on failure
602 vm_swapcache_test(vm_page_t m)
606 if (m->flags & PG_UNMANAGED)
608 if (m->hold_count || m->wire_count)
610 if (m->valid != VM_PAGE_BITS_ALL)
612 if (m->dirty & m->valid)
614 if ((object = m->object) == NULL)
616 if (object->type != OBJT_VNODE ||
617 (object->flags & OBJ_DEAD)) {
620 vm_page_test_dirty(m);
621 if (m->dirty & m->valid)
629 * We clean whole objects up to 16MB
633 vm_swapcache_cleaning(vm_object_t marker)
641 count = vm_swapcache_maxlaunder;
642 scount = vm_swapcache_maxscan;
645 * Look for vnode objects
647 lwkt_gettoken(&vmobj_token);
649 while ((object = TAILQ_NEXT(marker, object_list)) != NULL) {
651 * We have to skip markers. We cannot hold/drop marker
654 if (object->type == OBJT_MARKER) {
655 vm_swapcache_movemarker(marker, object);
660 * Safety, or in case there are millions of VM objects
661 * without swapcache backing.
667 * We must hold the object before potentially yielding.
669 vm_object_hold(object);
673 * Only operate on live VNODE objects that are either
674 * VREG or VCHR (VCHR for meta-data).
676 if ((object->type != OBJT_VNODE) ||
677 ((object->flags & OBJ_DEAD) ||
678 object->swblock_count == 0) ||
679 ((vp = object->handle) == NULL) ||
680 (vp->v_type != VREG && vp->v_type != VCHR)) {
681 vm_object_drop(object);
682 /* object may be invalid now */
683 vm_swapcache_movemarker(marker, object);
688 * Reset the object pindex stored in the marker if the
689 * working object has changed.
691 if (marker->backing_object != object) {
693 marker->backing_object_offset = 0;
694 marker->backing_object = object;
698 * Look for swblocks starting at our iterator.
700 * The swap_pager_condfree() function attempts to free
701 * swap space starting at the specified index. The index
702 * will be updated on return. The function will return
703 * a scan factor (NOT the number of blocks freed).
705 * If it must cut its scan of the object short due to an
706 * excessive number of swblocks, or is able to free the
707 * requested number of blocks, it will return n >= count
708 * and we break and pick it back up on a future attempt.
710 * Scan the object linearly and try to batch large sets of
711 * blocks that are likely to clean out entire swap radix
715 lwkt_reltoken(&vmobj_token);
717 n = swap_pager_condfree(object, &marker->size,
718 (count + SWAP_META_MASK) & ~SWAP_META_MASK);
720 vm_object_drop(object); /* object may be invalid now */
721 lwkt_gettoken(&vmobj_token);
724 * If we have exhausted the object or deleted our per-pass
725 * page limit then move us to the next object. Note that
726 * the current object may no longer be on the vm_object_list.
729 marker->backing_object_offset > vm_swapcache_cleanperobj) {
730 vm_swapcache_movemarker(marker, object);
734 * If we have exhausted our max-launder stop for now.
737 marker->backing_object_offset += n * PAGE_SIZE;
743 * If we wound up at the end of the list this will move the
744 * marker back to the beginning.
747 vm_swapcache_movemarker(marker, NULL);
749 lwkt_reltoken(&vmobj_token);
753 * Move the marker past the current object. Object can be stale, but we
754 * still need it to determine if the marker has to be moved. If the object
755 * is still the 'current object' (object after the marker), we hop-scotch
756 * the marker past it.
759 vm_swapcache_movemarker(vm_object_t marker, vm_object_t object)
761 if (TAILQ_NEXT(marker, object_list) == object) {
762 TAILQ_REMOVE(&vm_object_list, marker, object_list);
764 TAILQ_INSERT_AFTER(&vm_object_list, object,
765 marker, object_list);
767 TAILQ_INSERT_HEAD(&vm_object_list,
768 marker, object_list);