kernel - SWAP CACHE part 18/many - Minor enablement swapspace check
[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 when the data is present in the swapcache.
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 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;
87
88 static struct kproc_desc swpc_kp = {
89         "swapcached",
90         vm_swapcached,
91         &swapcached_thread
92 };
93 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
94
95 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
96
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 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
109 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
110         CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
111
112 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
113         CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
114 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
115         CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
116 SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
117         CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
118
119 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
120         CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
121 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
122         CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
123 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
124         CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
125 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
126         CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
127 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
128         CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
129 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
130         CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
131
132 /*
133  * vm_swapcached is the high level pageout daemon.
134  */
135 static void
136 vm_swapcached(void)
137 {
138         enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
139         enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
140         struct vm_page page_marker;
141         struct vm_object object_marker;
142
143         /*
144          * Thread setup
145          */
146         curthread->td_flags |= TDF_SYSTHREAD;
147         crit_enter();
148
149         /*
150          * Initialize our marker for the inactive scan (SWAPC_WRITING)
151          */
152         bzero(&page_marker, sizeof(page_marker));
153         page_marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
154         page_marker.queue = PQ_INACTIVE;
155         page_marker.wire_count = 1;
156         TAILQ_INSERT_HEAD(INACTIVE_LIST, &page_marker, pageq);
157
158         /*
159          * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
160          */
161         bzero(&object_marker, sizeof(object_marker));
162         object_marker.type = OBJT_MARKER;
163         TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
164
165         for (;;) {
166                 /*
167                  * Check every 5 seconds when not enabled or if no swap
168                  * is present.
169                  */
170                 if ((vm_swapcache_data_enable == 0 &&
171                      vm_swapcache_meta_enable == 0) ||
172                     vm_swap_max == 0) {
173                         tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
174                         continue;
175                 }
176
177                 /*
178                  * Polling rate when enabled is approximately 10 hz.
179                  */
180                 tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
181
182                 /*
183                  * State hysteresis.  Generate write activity up to 75% of
184                  * swap, then clean out swap assignments down to 70%, then
185                  * repeat.
186                  */
187                 if (state == SWAPC_WRITING) {
188                         if (vm_swap_cache_use > (int64_t)vm_swap_max * 75 / 100)
189                                 state = SWAPC_CLEANING;
190                 } else {
191                         if (vm_swap_cache_use < (int64_t)vm_swap_max * 70 / 100)
192                                 state = SWAPC_WRITING;
193                 }
194
195                 /*
196                  * We are allowed to continue accumulating burst value
197                  * in either state.  Allow the user to set curburst > maxburst
198                  * for the initial load-in.
199                  */
200                 if (vm_swapcache_curburst < vm_swapcache_maxburst) {
201                         vm_swapcache_curburst += vm_swapcache_accrate / 10;
202                         if (vm_swapcache_curburst > vm_swapcache_maxburst)
203                                 vm_swapcache_curburst = vm_swapcache_maxburst;
204                 }
205
206                 /*
207                  * We don't want to nickle-and-dime the scan as that will
208                  * create unnecessary fragmentation.  The minimum burst
209                  * is one-seconds worth of accumulation.
210                  */
211                 if (state == SWAPC_WRITING) {
212                         if (vm_swapcache_curburst >= vm_swapcache_accrate) {
213                                 if (burst == SWAPB_BURSTING) {
214                                         vm_swapcache_writing(&page_marker);
215                                         if (vm_swapcache_curburst <= 0)
216                                                 burst = SWAPB_RECOVERING;
217                                 } else if (vm_swapcache_curburst >
218                                            vm_swapcache_minburst) {
219                                         vm_swapcache_writing(&page_marker);
220                                         burst = SWAPB_BURSTING;
221                                 }
222                         }
223                 } else {
224                         vm_swapcache_cleaning(&object_marker);
225                 }
226         }
227         TAILQ_REMOVE(INACTIVE_LIST, &page_marker, pageq);
228         TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
229         crit_exit();
230 }
231
232 static void
233 vm_swapcache_writing(vm_page_t marker)
234 {
235         vm_object_t object;
236         struct vnode *vp;
237         vm_page_t m;
238         int count;
239
240         /*
241          * Scan the inactive queue from our marker to locate
242          * suitable pages to push to the swap cache.
243          *
244          * We are looking for clean vnode-backed pages.
245          *
246          * NOTE: PG_SWAPPED pages in particular are not part of
247          *       our count because once the cache stabilizes we
248          *       can end up with a very high datarate of VM pages
249          *       cycling from it.
250          */
251         m = marker;
252         count = vm_swapcache_maxlaunder;
253
254         while ((m = TAILQ_NEXT(m, pageq)) != NULL && count--) {
255                 if (m->flags & (PG_MARKER | PG_SWAPPED)) {
256                         ++count;
257                         continue;
258                 }
259                 if (vm_swapcache_curburst < 0)
260                         break;
261                 if (vm_swapcache_test(m))
262                         continue;
263                 object = m->object;
264                 vp = object->handle;
265                 if (vp == NULL)
266                         continue;
267
268                 switch(vp->v_type) {
269                 case VREG:
270                         if (vm_swapcache_data_enable == 0)
271                                 continue;
272                         if (vm_swapcache_maxfilesize &&
273                             object->size >
274                             (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
275                                 continue;
276                         }
277                         break;
278                 case VCHR:
279                         if (vm_swapcache_meta_enable == 0)
280                                 continue;
281                         break;
282                 default:
283                         continue;
284                 }
285
286                 /*
287                  * Ok, move the marker and soft-busy the page.
288                  */
289                 TAILQ_REMOVE(INACTIVE_LIST, marker, pageq);
290                 TAILQ_INSERT_AFTER(INACTIVE_LIST, m, marker, pageq);
291
292                 /*
293                  * Assign swap and initiate I/O.
294                  *
295                  * (adjust for the --count which also occurs in the loop)
296                  */
297                 count -= vm_swapcached_flush(m) - 1;
298
299                 /*
300                  * Setup for next loop using marker.
301                  */
302                 m = marker;
303         }
304
305         /*
306          * Cleanup marker position.  If we hit the end of the
307          * list the marker is placed at the tail.  Newly deactivated
308          * pages will be placed after it.
309          *
310          * Earlier inactive pages that were dirty and become clean
311          * are typically moved to the end of PQ_INACTIVE by virtue
312          * of vfs_vmio_release() when they become unwired from the
313          * buffer cache.
314          */
315         TAILQ_REMOVE(INACTIVE_LIST, marker, pageq);
316         if (m)
317                 TAILQ_INSERT_BEFORE(m, marker, pageq);
318         else
319                 TAILQ_INSERT_TAIL(INACTIVE_LIST, marker, pageq);
320 }
321
322 /*
323  * Flush the specified page using the swap_pager.
324  *
325  * Try to collect surrounding pages, including pages which may
326  * have already been assigned swap.  Try to cluster within a
327  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
328  * to match what swap_pager_putpages() can do.
329  *
330  * We also want to try to match against the buffer cache blocksize
331  * but we don't really know what it is here.  Since the buffer cache
332  * wires and unwires pages in groups the fact that we skip wired pages
333  * should be sufficient.
334  *
335  * Returns a count of pages we might have flushed (minimum 1)
336  */
337 static
338 int
339 vm_swapcached_flush(vm_page_t m)
340 {
341         vm_object_t object;
342         vm_page_t marray[SWAP_META_PAGES];
343         vm_pindex_t basei;
344         int rtvals[SWAP_META_PAGES];
345         int x;
346         int i;
347         int j;
348         int count;
349
350         vm_page_io_start(m);
351         vm_page_protect(m, VM_PROT_READ);
352         object = m->object;
353
354         /*
355          * Try to cluster around (m), keeping in mind that the swap pager
356          * can only do SMAP_META_PAGES worth of continguous write.
357          */
358         x = (int)m->pindex & SWAP_META_MASK;
359         marray[x] = m;
360         basei = m->pindex;
361
362         for (i = x - 1; i >= 0; --i) {
363                 m = vm_page_lookup(object, basei - x + i);
364                 if (m == NULL)
365                         break;
366                 if (vm_swapcache_test(m))
367                         break;
368                 vm_page_io_start(m);
369                 vm_page_protect(m, VM_PROT_READ);
370                 if (m->queue - m->pc == PQ_CACHE) {
371                         vm_page_unqueue_nowakeup(m);
372                         vm_page_deactivate(m);
373                 }
374                 marray[i] = m;
375         }
376         ++i;
377
378         for (j = x + 1; j < SWAP_META_PAGES; ++j) {
379                 m = vm_page_lookup(object, basei - x + j);
380                 if (m == NULL)
381                         break;
382                 if (vm_swapcache_test(m))
383                         break;
384                 vm_page_io_start(m);
385                 vm_page_protect(m, VM_PROT_READ);
386                 if (m->queue - m->pc == PQ_CACHE) {
387                         vm_page_unqueue_nowakeup(m);
388                         vm_page_deactivate(m);
389                 }
390                 marray[j] = m;
391         }
392
393         count = j - i;
394         vm_object_pip_add(object, count);
395         swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
396         vm_swapcache_write_count += count * PAGE_SIZE;
397         vm_swapcache_curburst -= count * PAGE_SIZE;
398
399         while (i < j) {
400                 if (rtvals[i] != VM_PAGER_PEND) {
401                         vm_page_io_finish(marray[i]);
402                         vm_object_pip_wakeup(object);
403                 }
404                 ++i;
405         }
406         return(count);
407 }
408
409 /*
410  * Test whether a VM page is suitable for writing to the swapcache.
411  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
412  *
413  * Returns 0 on success, 1 on failure
414  */
415 static int
416 vm_swapcache_test(vm_page_t m)
417 {
418         vm_object_t object;
419
420         if (m->flags & (PG_BUSY | PG_UNMANAGED))
421                 return(1);
422         if (m->busy || m->hold_count || m->wire_count)
423                 return(1);
424         if (m->valid != VM_PAGE_BITS_ALL)
425                 return(1);
426         if (m->dirty & m->valid)
427                 return(1);
428         if ((object = m->object) == NULL)
429                 return(1);
430         if (object->type != OBJT_VNODE ||
431             (object->flags & OBJ_DEAD)) {
432                 return(1);
433         }
434         vm_page_test_dirty(m);
435         if (m->dirty & m->valid)
436                 return(1);
437         return(0);
438 }
439
440 /*
441  * Cleaning pass
442  */
443 static
444 void
445 vm_swapcache_cleaning(vm_object_t marker)
446 {
447         vm_object_t object;
448         struct vnode *vp;
449         int count;
450         int n;
451
452         object = marker;
453         count = vm_swapcache_maxlaunder;
454
455         /*
456          * Look for vnode objects
457          */
458         while ((object = TAILQ_NEXT(object, object_list)) != NULL && count--) {
459                 if (object->type != OBJT_VNODE)
460                         continue;
461                 if ((object->flags & OBJ_DEAD) || object->swblock_count == 0)
462                         continue;
463                 if ((vp = object->handle) == NULL)
464                         continue;
465                 if (vp->v_type != VREG && vp->v_type != VCHR)
466                         continue;
467
468                 /*
469                  * Adjust iterator.
470                  */
471                 if (marker->backing_object != object)
472                         marker->size = 0;
473
474                 /*
475                  * Move the marker so we can work on the VM object
476                  */
477                 TAILQ_REMOVE(&vm_object_list, marker, object_list);
478                 TAILQ_INSERT_AFTER(&vm_object_list, object,
479                                    marker, object_list);
480
481                 /*
482                  * Look for swblocks starting at our iterator.
483                  *
484                  * The swap_pager_condfree() function attempts to free
485                  * swap space starting at the specified index.  The index
486                  * will be updated on return.  The function will return
487                  * a scan factor (NOT the number of blocks freed).
488                  *
489                  * If it must cut its scan of the object short due to an
490                  * excessive number of swblocks, or is able to free the
491                  * requested number of blocks, it will return n >= count
492                  * and we break and pick it back up on a future attempt.
493                  */
494                 n = swap_pager_condfree(object, &marker->size, count);
495                 count -= n;
496                 if (count < 0)
497                         break;
498
499                 /*
500                  * Setup for loop.
501                  */
502                 marker->size = 0;
503                 object = marker;
504         }
505
506         /*
507          * Adjust marker so we continue the scan from where we left off.
508          * When we reach the end we start back at the beginning.
509          */
510         TAILQ_REMOVE(&vm_object_list, marker, object_list);
511         if (object)
512                 TAILQ_INSERT_BEFORE(object, marker, object_list);
513         else
514                 TAILQ_INSERT_HEAD(&vm_object_list, marker, object_list);
515         marker->backing_object = object;
516 }