kernel - SWAP CACHE part 7/many - Add vm_swapcache.c core (write side)
[dragonfly.git] / sys / vm / vm_swapcache.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34
35 /*
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.
41  *
42  * The daemon manages write bandwidth based on sysctl settings to control
43  * wear on the SSD.
44  *
45  * The vnode strategy code will check for the swap assignments and divert
46  * reads to the swap device.
47  *
48  * This operates on both regular files and the block device vnodes used by
49  * filesystems to manage meta-data.
50  */
51
52 #include "opt_vm.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.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>
63
64 #include <vm/vm.h>
65 #include <vm/vm_param.h>
66 #include <sys/lock.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>
74
75 #include <sys/thread2.h>
76 #include <vm/vm_page2.h>
77
78 #define INACTIVE_LIST   (&vm_page_queues[PQ_INACTIVE].pl)
79
80 /* the kernel process "vm_pageout"*/
81 static void vm_swapcached (void);
82 static void vm_swapcached_flush (vm_page_t m);
83 struct thread *swapcached_thread;
84
85 static struct kproc_desc swpc_kp = {
86         "swapcached",
87         vm_swapcached,
88         &swapcached_thread
89 };
90 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
91
92 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
93
94 static int vm_swapcache_sleep;
95 static int vm_swapcache_maxlaunder = 64;
96 static int vm_swapcache_data_enable = 0;
97 static int vm_swapcache_meta_enable = 0;
98 static int64_t vm_swapcache_write_count;
99
100 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
101         CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
102 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
103         CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
104 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
105         CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
106 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
107         CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
108
109 /*
110  * vm_swapcached is the high level pageout daemon.
111  */
112 static void
113 vm_swapcached(void)
114 {
115         struct vm_page marker;
116         vm_object_t object;
117         vm_page_t m;
118         int count;
119
120         /*
121          * Thread setup
122          */
123         curthread->td_flags |= TDF_SYSTHREAD;
124
125         /*
126          * Initialize our marker
127          */
128         bzero(&marker, sizeof(marker));
129         marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
130         marker.queue = PQ_INACTIVE;
131         marker.wire_count = 1;
132
133         crit_enter();
134         TAILQ_INSERT_HEAD(INACTIVE_LIST, &marker, pageq);
135
136         for (;;) {
137                 /*
138                  * Loop once a second or so looking for work when enabled.
139                  */
140                 if (vm_swapcache_data_enable == 0 &&
141                     vm_swapcache_meta_enable == 0) {
142                         tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
143                         continue;
144                 }
145                 tsleep(&vm_swapcache_sleep, 0, "csleep", hz);
146
147                 /*
148                  * Calculate the number of pages to test.  We don't want
149                  * to get into a cpu-bound loop.
150                  */
151                 count = vmstats.v_inactive_count;
152                 if (count > vm_swapcache_maxlaunder)
153                         count = vm_swapcache_maxlaunder;
154
155                 /*
156                  * Scan the inactive queue from our marker to locate
157                  * suitable pages to push to the swap cache.
158                  *
159                  * We are looking for clean vnode-backed pages.
160                  */
161                 m = &marker;
162                 while ((m = TAILQ_NEXT(m, pageq)) != NULL && count--) {
163                         if (m->flags & PG_MARKER) {
164                                 ++count;
165                                 continue;
166                         }
167                         if (m->flags & (PG_SWAPPED | PG_BUSY | PG_UNMANAGED))
168                                 continue;
169                         if (m->busy || m->hold_count || m->wire_count)
170                                 continue;
171                         if (m->valid != VM_PAGE_BITS_ALL)
172                                 continue;
173                         if (m->dirty & m->valid)
174                                 continue;
175                         if ((object = m->object) == NULL)
176                                 continue;
177                         if (object->type != OBJT_VNODE)
178                                 continue;
179                         vm_page_test_dirty(m);
180                         if (m->dirty & m->valid)
181                                 continue;
182
183                         /*
184                          * Ok, move the marker and soft-busy the page.
185                          */
186                         TAILQ_REMOVE(INACTIVE_LIST, &marker, pageq);
187                         TAILQ_INSERT_AFTER(INACTIVE_LIST, m, &marker, pageq);
188
189                         /*
190                          * Assign swap and initiate I/O
191                          */
192                         vm_swapcached_flush(m);
193
194                         /*
195                          * Setup for next loop using marker.
196                          */
197                         m = &marker;
198                 }
199                 TAILQ_REMOVE(INACTIVE_LIST, &marker, pageq);
200                 if (m)
201                         TAILQ_INSERT_BEFORE(m, &marker, pageq);
202                 else
203                         TAILQ_INSERT_HEAD(INACTIVE_LIST, &marker, pageq);
204
205         }
206         TAILQ_REMOVE(INACTIVE_LIST, &marker, pageq);
207         crit_exit();
208 }
209
210 /*
211  * Flush the specified page using the swap_pager.
212  */
213 static
214 void
215 vm_swapcached_flush(vm_page_t m)
216 {
217         vm_object_t object;
218         int rtvals;
219
220         vm_page_io_start(m);
221         vm_page_protect(m, VM_PROT_READ);
222
223         object = m->object;
224         vm_object_pip_add(object, 1);
225         swap_pager_putpages(object, &m, 1, FALSE, &rtvals);
226
227         if (rtvals != VM_PAGER_PEND) {
228                 vm_object_pip_wakeup(object);
229                 vm_page_io_finish(m);
230         }
231 }