Kernel part of bluetooth stack ported by Dmitry Komissaroff. Very much work
[dragonfly.git] / sys / vm / vm_pageout.c
blob2cbe27100d24a078a8f261d954aecb14c3612991
1 /*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
5 * All rights reserved.
6 * Copyright (c) 1994 David Greenman
7 * All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
40 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
43 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44 * All rights reserved.
46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 * Carnegie Mellon requests users of this software to return to
60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
68 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
69 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.32 2007/03/20 00:54:26 dillon Exp $
73 * The proverbial page-out daemon.
76 #include "opt_vm.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/proc.h>
81 #include <sys/kthread.h>
82 #include <sys/resourcevar.h>
83 #include <sys/signalvar.h>
84 #include <sys/vnode.h>
85 #include <sys/vmmeter.h>
86 #include <sys/sysctl.h>
88 #include <vm/vm.h>
89 #include <vm/vm_param.h>
90 #include <sys/lock.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_pager.h>
96 #include <vm/swap_pager.h>
97 #include <vm/vm_extern.h>
99 #include <sys/thread2.h>
100 #include <vm/vm_page2.h>
103 * System initialization
106 /* the kernel process "vm_pageout"*/
107 static void vm_pageout (void);
108 static int vm_pageout_clean (vm_page_t);
109 static void vm_pageout_scan (int pass);
110 static int vm_pageout_free_page_calc (vm_size_t count);
111 struct thread *pagethread;
113 static struct kproc_desc page_kp = {
114 "pagedaemon",
115 vm_pageout,
116 &pagethread
118 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
120 #if !defined(NO_SWAPPING)
121 /* the kernel process "vm_daemon"*/
122 static void vm_daemon (void);
123 static struct thread *vmthread;
125 static struct kproc_desc vm_kp = {
126 "vmdaemon",
127 vm_daemon,
128 &vmthread
130 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
131 #endif
134 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
135 int vm_pageout_deficit=0; /* Estimated number of pages deficit */
136 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
138 #if !defined(NO_SWAPPING)
139 static int vm_pageout_req_swapout; /* XXX */
140 static int vm_daemon_needed;
141 #endif
142 extern int vm_swap_size;
143 static int vm_max_launder = 32;
144 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
145 static int vm_pageout_full_stats_interval = 0;
146 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
147 static int defer_swap_pageouts=0;
148 static int disable_swap_pageouts=0;
150 #if defined(NO_SWAPPING)
151 static int vm_swap_enabled=0;
152 static int vm_swap_idle_enabled=0;
153 #else
154 static int vm_swap_enabled=1;
155 static int vm_swap_idle_enabled=0;
156 #endif
158 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
159 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
161 SYSCTL_INT(_vm, OID_AUTO, max_launder,
162 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
164 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
165 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
167 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
168 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
170 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
171 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
173 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
174 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
176 #if defined(NO_SWAPPING)
177 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
178 CTLFLAG_RD, &vm_swap_enabled, 0, "");
179 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
180 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
181 #else
182 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
183 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
184 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
185 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
186 #endif
188 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
189 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
191 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
192 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
194 static int pageout_lock_miss;
195 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
196 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
198 int vm_load;
199 SYSCTL_INT(_vm, OID_AUTO, vm_load,
200 CTLFLAG_RD, &vm_load, 0, "load on the VM system");
201 int vm_load_enable = 1;
202 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
203 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
204 #ifdef INVARIANTS
205 int vm_load_debug;
206 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
207 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
208 #endif
210 #define VM_PAGEOUT_PAGE_COUNT 16
211 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
213 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
215 #if !defined(NO_SWAPPING)
216 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
217 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
218 static freeer_fcn_t vm_pageout_object_deactivate_pages;
219 static void vm_req_vmdaemon (void);
220 #endif
221 static void vm_pageout_page_stats(void);
224 * Update
226 void
227 vm_fault_ratecheck(void)
229 if (vm_pages_needed) {
230 if (vm_load < 1000)
231 ++vm_load;
232 } else {
233 if (vm_load > 0)
234 --vm_load;
239 * vm_pageout_clean:
241 * Clean the page and remove it from the laundry. The page must not be
242 * busy on-call.
244 * We set the busy bit to cause potential page faults on this page to
245 * block. Note the careful timing, however, the busy bit isn't set till
246 * late and we cannot do anything that will mess with the page.
249 static int
250 vm_pageout_clean(vm_page_t m)
252 vm_object_t object;
253 vm_page_t mc[2*vm_pageout_page_count];
254 int pageout_count;
255 int ib, is, page_base;
256 vm_pindex_t pindex = m->pindex;
258 object = m->object;
261 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
262 * with the new swapper, but we could have serious problems paging
263 * out other object types if there is insufficient memory.
265 * Unfortunately, checking free memory here is far too late, so the
266 * check has been moved up a procedural level.
270 * Don't mess with the page if it's busy, held, or special
272 if ((m->hold_count != 0) ||
273 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
274 return 0;
277 mc[vm_pageout_page_count] = m;
278 pageout_count = 1;
279 page_base = vm_pageout_page_count;
280 ib = 1;
281 is = 1;
284 * Scan object for clusterable pages.
286 * We can cluster ONLY if: ->> the page is NOT
287 * clean, wired, busy, held, or mapped into a
288 * buffer, and one of the following:
289 * 1) The page is inactive, or a seldom used
290 * active page.
291 * -or-
292 * 2) we force the issue.
294 * During heavy mmap/modification loads the pageout
295 * daemon can really fragment the underlying file
296 * due to flushing pages out of order and not trying
297 * align the clusters (which leave sporatic out-of-order
298 * holes). To solve this problem we do the reverse scan
299 * first and attempt to align our cluster, then do a
300 * forward scan if room remains.
303 more:
304 while (ib && pageout_count < vm_pageout_page_count) {
305 vm_page_t p;
307 if (ib > pindex) {
308 ib = 0;
309 break;
312 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
313 ib = 0;
314 break;
316 if (((p->queue - p->pc) == PQ_CACHE) ||
317 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
318 ib = 0;
319 break;
321 vm_page_test_dirty(p);
322 if ((p->dirty & p->valid) == 0 ||
323 p->queue != PQ_INACTIVE ||
324 p->wire_count != 0 || /* may be held by buf cache */
325 p->hold_count != 0) { /* may be undergoing I/O */
326 ib = 0;
327 break;
329 mc[--page_base] = p;
330 ++pageout_count;
331 ++ib;
333 * alignment boundry, stop here and switch directions. Do
334 * not clear ib.
336 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
337 break;
340 while (pageout_count < vm_pageout_page_count &&
341 pindex + is < object->size) {
342 vm_page_t p;
344 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
345 break;
346 if (((p->queue - p->pc) == PQ_CACHE) ||
347 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
348 break;
350 vm_page_test_dirty(p);
351 if ((p->dirty & p->valid) == 0 ||
352 p->queue != PQ_INACTIVE ||
353 p->wire_count != 0 || /* may be held by buf cache */
354 p->hold_count != 0) { /* may be undergoing I/O */
355 break;
357 mc[page_base + pageout_count] = p;
358 ++pageout_count;
359 ++is;
363 * If we exhausted our forward scan, continue with the reverse scan
364 * when possible, even past a page boundry. This catches boundry
365 * conditions.
367 if (ib && pageout_count < vm_pageout_page_count)
368 goto more;
371 * we allow reads during pageouts...
373 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
377 * vm_pageout_flush() - launder the given pages
379 * The given pages are laundered. Note that we setup for the start of
380 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
381 * reference count all in here rather then in the parent. If we want
382 * the parent to do more sophisticated things we may have to change
383 * the ordering.
387 vm_pageout_flush(vm_page_t *mc, int count, int flags)
389 vm_object_t object;
390 int pageout_status[count];
391 int numpagedout = 0;
392 int i;
395 * Initiate I/O. Bump the vm_page_t->busy counter and
396 * mark the pages read-only.
398 * We do not have to fixup the clean/dirty bits here... we can
399 * allow the pager to do it after the I/O completes.
402 for (i = 0; i < count; i++) {
403 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
404 vm_page_io_start(mc[i]);
405 vm_page_protect(mc[i], VM_PROT_READ);
408 object = mc[0]->object;
409 vm_object_pip_add(object, count);
411 vm_pager_put_pages(object, mc, count,
412 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
413 pageout_status);
415 for (i = 0; i < count; i++) {
416 vm_page_t mt = mc[i];
418 switch (pageout_status[i]) {
419 case VM_PAGER_OK:
420 numpagedout++;
421 break;
422 case VM_PAGER_PEND:
423 numpagedout++;
424 break;
425 case VM_PAGER_BAD:
427 * Page outside of range of object. Right now we
428 * essentially lose the changes by pretending it
429 * worked.
431 pmap_clear_modify(mt);
432 vm_page_undirty(mt);
433 break;
434 case VM_PAGER_ERROR:
435 case VM_PAGER_FAIL:
437 * If page couldn't be paged out, then reactivate the
438 * page so it doesn't clog the inactive list. (We
439 * will try paging out it again later).
441 vm_page_activate(mt);
442 break;
443 case VM_PAGER_AGAIN:
444 break;
448 * If the operation is still going, leave the page busy to
449 * block all other accesses. Also, leave the paging in
450 * progress indicator set so that we don't attempt an object
451 * collapse.
453 if (pageout_status[i] != VM_PAGER_PEND) {
454 vm_object_pip_wakeup(object);
455 vm_page_io_finish(mt);
456 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
457 vm_page_protect(mt, VM_PROT_READ);
460 return numpagedout;
463 #if !defined(NO_SWAPPING)
465 * vm_pageout_object_deactivate_pages
467 * deactivate enough pages to satisfy the inactive target
468 * requirements or if vm_page_proc_limit is set, then
469 * deactivate all of the pages in the object and its
470 * backing_objects.
472 * The object and map must be locked.
474 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
476 static void
477 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
478 vm_pindex_t desired, int map_remove_only)
480 struct rb_vm_page_scan_info info;
481 int remove_mode;
483 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
484 return;
486 while (object) {
487 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
488 return;
489 if (object->paging_in_progress)
490 return;
492 remove_mode = map_remove_only;
493 if (object->shadow_count > 1)
494 remove_mode = 1;
497 * scan the objects entire memory queue. spl protection is
498 * required to avoid an interrupt unbusy/free race against
499 * our busy check.
501 crit_enter();
502 info.limit = remove_mode;
503 info.map = map;
504 info.desired = desired;
505 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
506 vm_pageout_object_deactivate_pages_callback,
507 &info
509 crit_exit();
510 object = object->backing_object;
514 static int
515 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
517 struct rb_vm_page_scan_info *info = data;
518 int actcount;
520 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
521 return(-1);
523 mycpu->gd_cnt.v_pdpages++;
524 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 ||
525 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
526 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
527 return(0);
530 actcount = pmap_ts_referenced(p);
531 if (actcount) {
532 vm_page_flag_set(p, PG_REFERENCED);
533 } else if (p->flags & PG_REFERENCED) {
534 actcount = 1;
537 if ((p->queue != PQ_ACTIVE) &&
538 (p->flags & PG_REFERENCED)) {
539 vm_page_activate(p);
540 p->act_count += actcount;
541 vm_page_flag_clear(p, PG_REFERENCED);
542 } else if (p->queue == PQ_ACTIVE) {
543 if ((p->flags & PG_REFERENCED) == 0) {
544 p->act_count -= min(p->act_count, ACT_DECLINE);
545 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) {
546 vm_page_protect(p, VM_PROT_NONE);
547 vm_page_deactivate(p);
548 } else {
549 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
550 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
552 } else {
553 vm_page_activate(p);
554 vm_page_flag_clear(p, PG_REFERENCED);
555 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
556 p->act_count += ACT_ADVANCE;
557 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
558 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
560 } else if (p->queue == PQ_INACTIVE) {
561 vm_page_protect(p, VM_PROT_NONE);
563 return(0);
567 * deactivate some number of pages in a map, try to do it fairly, but
568 * that is really hard to do.
570 static void
571 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
573 vm_map_entry_t tmpe;
574 vm_object_t obj, bigobj;
575 int nothingwired;
577 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
578 return;
581 bigobj = NULL;
582 nothingwired = TRUE;
585 * first, search out the biggest object, and try to free pages from
586 * that.
588 tmpe = map->header.next;
589 while (tmpe != &map->header) {
590 switch(tmpe->maptype) {
591 case VM_MAPTYPE_NORMAL:
592 case VM_MAPTYPE_VPAGETABLE:
593 obj = tmpe->object.vm_object;
594 if ((obj != NULL) && (obj->shadow_count <= 1) &&
595 ((bigobj == NULL) ||
596 (bigobj->resident_page_count < obj->resident_page_count))) {
597 bigobj = obj;
599 break;
600 default:
601 break;
603 if (tmpe->wired_count > 0)
604 nothingwired = FALSE;
605 tmpe = tmpe->next;
608 if (bigobj)
609 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
612 * Next, hunt around for other pages to deactivate. We actually
613 * do this search sort of wrong -- .text first is not the best idea.
615 tmpe = map->header.next;
616 while (tmpe != &map->header) {
617 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
618 break;
619 switch(tmpe->maptype) {
620 case VM_MAPTYPE_NORMAL:
621 case VM_MAPTYPE_VPAGETABLE:
622 obj = tmpe->object.vm_object;
623 if (obj)
624 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
625 break;
626 default:
627 break;
629 tmpe = tmpe->next;
633 * Remove all mappings if a process is swapped out, this will free page
634 * table pages.
636 if (desired == 0 && nothingwired)
637 pmap_remove(vm_map_pmap(map),
638 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
639 vm_map_unlock(map);
641 #endif
644 * Don't try to be fancy - being fancy can lead to vnode deadlocks. We
645 * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can
646 * be trivially freed.
648 void
649 vm_pageout_page_free(vm_page_t m)
651 vm_object_t object = m->object;
652 int type = object->type;
654 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
655 vm_object_reference(object);
656 vm_page_busy(m);
657 vm_page_protect(m, VM_PROT_NONE);
658 vm_page_free(m);
659 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
660 vm_object_deallocate(object);
664 * vm_pageout_scan does the dirty work for the pageout daemon.
667 struct vm_pageout_scan_info {
668 struct proc *bigproc;
669 vm_offset_t bigsize;
672 static int vm_pageout_scan_callback(struct proc *p, void *data);
674 static void
675 vm_pageout_scan(int pass)
677 struct vm_pageout_scan_info info;
678 vm_page_t m, next;
679 struct vm_page marker;
680 int page_shortage, maxscan, pcount;
681 int addl_page_shortage, addl_page_shortage_init;
682 vm_object_t object;
683 int actcount;
684 int vnodes_skipped = 0;
685 int maxlaunder;
688 * Do whatever cleanup that the pmap code can.
690 pmap_collect();
692 addl_page_shortage_init = vm_pageout_deficit;
693 vm_pageout_deficit = 0;
696 * Calculate the number of pages we want to either free or move
697 * to the cache.
699 page_shortage = vm_paging_target() + addl_page_shortage_init;
702 * Initialize our marker
704 bzero(&marker, sizeof(marker));
705 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
706 marker.queue = PQ_INACTIVE;
707 marker.wire_count = 1;
710 * Start scanning the inactive queue for pages we can move to the
711 * cache or free. The scan will stop when the target is reached or
712 * we have scanned the entire inactive queue. Note that m->act_count
713 * is not used to form decisions for the inactive queue, only for the
714 * active queue.
716 * maxlaunder limits the number of dirty pages we flush per scan.
717 * For most systems a smaller value (16 or 32) is more robust under
718 * extreme memory and disk pressure because any unnecessary writes
719 * to disk can result in extreme performance degredation. However,
720 * systems with excessive dirty pages (especially when MAP_NOSYNC is
721 * used) will die horribly with limited laundering. If the pageout
722 * daemon cannot clean enough pages in the first pass, we let it go
723 * all out in succeeding passes.
725 if ((maxlaunder = vm_max_launder) <= 1)
726 maxlaunder = 1;
727 if (pass)
728 maxlaunder = 10000;
731 * We will generally be in a critical section throughout the
732 * scan, but we can release it temporarily when we are sitting on a
733 * non-busy page without fear. this is required to prevent an
734 * interrupt from unbusying or freeing a page prior to our busy
735 * check, leaving us on the wrong queue or checking the wrong
736 * page.
738 crit_enter();
739 rescan0:
740 addl_page_shortage = addl_page_shortage_init;
741 maxscan = vmstats.v_inactive_count;
742 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
743 m != NULL && maxscan-- > 0 && page_shortage > 0;
744 m = next
746 mycpu->gd_cnt.v_pdpages++;
749 * Give interrupts a chance
751 crit_exit();
752 crit_enter();
755 * It's easier for some of the conditions below to just loop
756 * and catch queue changes here rather then check everywhere
757 * else.
759 if (m->queue != PQ_INACTIVE)
760 goto rescan0;
761 next = TAILQ_NEXT(m, pageq);
764 * skip marker pages
766 if (m->flags & PG_MARKER)
767 continue;
770 * A held page may be undergoing I/O, so skip it.
772 if (m->hold_count) {
773 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
774 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
775 addl_page_shortage++;
776 continue;
780 * Dont mess with busy pages, keep in the front of the
781 * queue, most likely are being paged out.
783 if (m->busy || (m->flags & PG_BUSY)) {
784 addl_page_shortage++;
785 continue;
788 if (m->object->ref_count == 0) {
790 * If the object is not being used, we ignore previous
791 * references.
793 vm_page_flag_clear(m, PG_REFERENCED);
794 pmap_clear_reference(m);
796 } else if (((m->flags & PG_REFERENCED) == 0) &&
797 (actcount = pmap_ts_referenced(m))) {
799 * Otherwise, if the page has been referenced while
800 * in the inactive queue, we bump the "activation
801 * count" upwards, making it less likely that the
802 * page will be added back to the inactive queue
803 * prematurely again. Here we check the page tables
804 * (or emulated bits, if any), given the upper level
805 * VM system not knowing anything about existing
806 * references.
808 vm_page_activate(m);
809 m->act_count += (actcount + ACT_ADVANCE);
810 continue;
814 * If the upper level VM system knows about any page
815 * references, we activate the page. We also set the
816 * "activation count" higher than normal so that we will less
817 * likely place pages back onto the inactive queue again.
819 if ((m->flags & PG_REFERENCED) != 0) {
820 vm_page_flag_clear(m, PG_REFERENCED);
821 actcount = pmap_ts_referenced(m);
822 vm_page_activate(m);
823 m->act_count += (actcount + ACT_ADVANCE + 1);
824 continue;
828 * If the upper level VM system doesn't know anything about
829 * the page being dirty, we have to check for it again. As
830 * far as the VM code knows, any partially dirty pages are
831 * fully dirty.
833 * Pages marked PG_WRITEABLE may be mapped into the user
834 * address space of a process running on another cpu. A
835 * user process (without holding the MP lock) running on
836 * another cpu may be able to touch the page while we are
837 * trying to remove it. To prevent this from occuring we
838 * must call pmap_remove_all() or otherwise make the page
839 * read-only. If the race occured pmap_remove_all() is
840 * responsible for setting m->dirty.
842 if (m->dirty == 0) {
843 vm_page_test_dirty(m);
844 #if 0
845 if (m->dirty == 0 && (m->flags & PG_WRITEABLE) != 0)
846 pmap_remove_all(m);
847 #endif
848 } else {
849 vm_page_dirty(m);
852 if (m->valid == 0) {
854 * Invalid pages can be easily freed
856 vm_pageout_page_free(m);
857 mycpu->gd_cnt.v_dfree++;
858 --page_shortage;
859 } else if (m->dirty == 0) {
861 * Clean pages can be placed onto the cache queue.
862 * This effectively frees them.
864 vm_page_cache(m);
865 --page_shortage;
866 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
868 * Dirty pages need to be paged out, but flushing
869 * a page is extremely expensive verses freeing
870 * a clean page. Rather then artificially limiting
871 * the number of pages we can flush, we instead give
872 * dirty pages extra priority on the inactive queue
873 * by forcing them to be cycled through the queue
874 * twice before being flushed, after which the
875 * (now clean) page will cycle through once more
876 * before being freed. This significantly extends
877 * the thrash point for a heavily loaded machine.
879 vm_page_flag_set(m, PG_WINATCFLS);
880 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
881 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
882 } else if (maxlaunder > 0) {
884 * We always want to try to flush some dirty pages if
885 * we encounter them, to keep the system stable.
886 * Normally this number is small, but under extreme
887 * pressure where there are insufficient clean pages
888 * on the inactive queue, we may have to go all out.
890 int swap_pageouts_ok;
891 struct vnode *vp = NULL;
893 object = m->object;
895 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
896 swap_pageouts_ok = 1;
897 } else {
898 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
899 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
900 vm_page_count_min());
905 * We don't bother paging objects that are "dead".
906 * Those objects are in a "rundown" state.
908 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
909 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
910 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
911 continue;
915 * The object is already known NOT to be dead. It
916 * is possible for the vget() to block the whole
917 * pageout daemon, but the new low-memory handling
918 * code should prevent it.
920 * The previous code skipped locked vnodes and, worse,
921 * reordered pages in the queue. This results in
922 * completely non-deterministic operation because,
923 * quite often, a vm_fault has initiated an I/O and
924 * is holding a locked vnode at just the point where
925 * the pageout daemon is woken up.
927 * We can't wait forever for the vnode lock, we might
928 * deadlock due to a vn_read() getting stuck in
929 * vm_wait while holding this vnode. We skip the
930 * vnode if we can't get it in a reasonable amount
931 * of time.
934 if (object->type == OBJT_VNODE) {
935 vp = object->handle;
937 if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK)) {
938 ++pageout_lock_miss;
939 if (object->flags & OBJ_MIGHTBEDIRTY)
940 vnodes_skipped++;
941 continue;
945 * The page might have been moved to another
946 * queue during potential blocking in vget()
947 * above. The page might have been freed and
948 * reused for another vnode. The object might
949 * have been reused for another vnode.
951 if (m->queue != PQ_INACTIVE ||
952 m->object != object ||
953 object->handle != vp) {
954 if (object->flags & OBJ_MIGHTBEDIRTY)
955 vnodes_skipped++;
956 vput(vp);
957 continue;
961 * The page may have been busied during the
962 * blocking in vput(); We don't move the
963 * page back onto the end of the queue so that
964 * statistics are more correct if we don't.
966 if (m->busy || (m->flags & PG_BUSY)) {
967 vput(vp);
968 continue;
972 * If the page has become held it might
973 * be undergoing I/O, so skip it
975 if (m->hold_count) {
976 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
977 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
978 if (object->flags & OBJ_MIGHTBEDIRTY)
979 vnodes_skipped++;
980 vput(vp);
981 continue;
986 * If a page is dirty, then it is either being washed
987 * (but not yet cleaned) or it is still in the
988 * laundry. If it is still in the laundry, then we
989 * start the cleaning operation.
991 * This operation may cluster, invalidating the 'next'
992 * pointer. To prevent an inordinate number of
993 * restarts we use our marker to remember our place.
995 * decrement page_shortage on success to account for
996 * the (future) cleaned page. Otherwise we could wind
997 * up laundering or cleaning too many pages.
999 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
1000 if (vm_pageout_clean(m) != 0) {
1001 --page_shortage;
1002 --maxlaunder;
1004 next = TAILQ_NEXT(&marker, pageq);
1005 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1006 if (vp != NULL)
1007 vput(vp);
1012 * Compute the number of pages we want to try to move from the
1013 * active queue to the inactive queue.
1015 page_shortage = vm_paging_target() +
1016 vmstats.v_inactive_target - vmstats.v_inactive_count;
1017 page_shortage += addl_page_shortage;
1020 * Scan the active queue for things we can deactivate. We nominally
1021 * track the per-page activity counter and use it to locate
1022 * deactivation candidates.
1024 * NOTE: we are still in a critical section.
1026 pcount = vmstats.v_active_count;
1027 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1029 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1031 * Give interrupts a chance.
1033 crit_exit();
1034 crit_enter();
1037 * If the page was ripped out from under us, just stop.
1039 if (m->queue != PQ_ACTIVE)
1040 break;
1041 next = TAILQ_NEXT(m, pageq);
1044 * Don't deactivate pages that are busy.
1046 if ((m->busy != 0) ||
1047 (m->flags & PG_BUSY) ||
1048 (m->hold_count != 0)) {
1049 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1050 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1051 m = next;
1052 continue;
1056 * The count for pagedaemon pages is done after checking the
1057 * page for eligibility...
1059 mycpu->gd_cnt.v_pdpages++;
1062 * Check to see "how much" the page has been used.
1064 actcount = 0;
1065 if (m->object->ref_count != 0) {
1066 if (m->flags & PG_REFERENCED) {
1067 actcount += 1;
1069 actcount += pmap_ts_referenced(m);
1070 if (actcount) {
1071 m->act_count += ACT_ADVANCE + actcount;
1072 if (m->act_count > ACT_MAX)
1073 m->act_count = ACT_MAX;
1078 * Since we have "tested" this bit, we need to clear it now.
1080 vm_page_flag_clear(m, PG_REFERENCED);
1083 * Only if an object is currently being used, do we use the
1084 * page activation count stats.
1086 if (actcount && (m->object->ref_count != 0)) {
1087 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1088 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1089 } else {
1090 m->act_count -= min(m->act_count, ACT_DECLINE);
1091 if (vm_pageout_algorithm ||
1092 m->object->ref_count == 0 ||
1093 m->act_count < pass) {
1094 page_shortage--;
1095 if (m->object->ref_count == 0) {
1096 vm_page_protect(m, VM_PROT_NONE);
1097 if (m->dirty == 0)
1098 vm_page_cache(m);
1099 else
1100 vm_page_deactivate(m);
1101 } else {
1102 vm_page_deactivate(m);
1104 } else {
1105 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1106 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1109 m = next;
1113 * We try to maintain some *really* free pages, this allows interrupt
1114 * code to be guaranteed space. Since both cache and free queues
1115 * are considered basically 'free', moving pages from cache to free
1116 * does not effect other calculations.
1118 * NOTE: we are still in a critical section.
1121 while (vmstats.v_free_count < vmstats.v_free_reserved) {
1122 static int cache_rover = 0;
1123 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1124 if (!m)
1125 break;
1126 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1127 m->busy ||
1128 m->hold_count ||
1129 m->wire_count) {
1130 #ifdef INVARIANTS
1131 kprintf("Warning: busy page %p found in cache\n", m);
1132 #endif
1133 vm_page_deactivate(m);
1134 continue;
1136 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1137 vm_pageout_page_free(m);
1138 mycpu->gd_cnt.v_dfree++;
1141 crit_exit();
1143 #if !defined(NO_SWAPPING)
1145 * Idle process swapout -- run once per second.
1147 if (vm_swap_idle_enabled) {
1148 static long lsec;
1149 if (time_second != lsec) {
1150 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1151 vm_req_vmdaemon();
1152 lsec = time_second;
1155 #endif
1158 * If we didn't get enough free pages, and we have skipped a vnode
1159 * in a writeable object, wakeup the sync daemon. And kick swapout
1160 * if we did not get enough free pages.
1162 if (vm_paging_target() > 0) {
1163 if (vnodes_skipped && vm_page_count_min())
1164 speedup_syncer();
1165 #if !defined(NO_SWAPPING)
1166 if (vm_swap_enabled && vm_page_count_target()) {
1167 vm_req_vmdaemon();
1168 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1170 #endif
1174 * If we are out of swap and were not able to reach our paging
1175 * target, kill the largest process.
1177 if ((vm_swap_size < 64 && vm_page_count_min()) ||
1178 (swap_pager_full && vm_paging_target() > 0)) {
1179 #if 0
1180 if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) {
1181 #endif
1182 info.bigproc = NULL;
1183 info.bigsize = 0;
1184 allproc_scan(vm_pageout_scan_callback, &info);
1185 if (info.bigproc != NULL) {
1186 killproc(info.bigproc, "out of swap space");
1187 info.bigproc->p_nice = PRIO_MIN;
1188 info.bigproc->p_usched->resetpriority(
1189 FIRST_LWP_IN_PROC(info.bigproc));
1190 wakeup(&vmstats.v_free_count);
1191 PRELE(info.bigproc);
1196 static int
1197 vm_pageout_scan_callback(struct proc *p, void *data)
1199 struct vm_pageout_scan_info *info = data;
1200 vm_offset_t size;
1203 * if this is a system process, skip it
1205 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1206 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1207 return (0);
1211 * if the process is in a non-running type state,
1212 * don't touch it.
1214 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) {
1215 return (0);
1219 * get the process size
1221 size = vmspace_resident_count(p->p_vmspace) +
1222 vmspace_swap_count(p->p_vmspace);
1225 * If the this process is bigger than the biggest one
1226 * remember it.
1228 if (size > info->bigsize) {
1229 if (info->bigproc)
1230 PRELE(info->bigproc);
1231 PHOLD(p);
1232 info->bigproc = p;
1233 info->bigsize = size;
1235 return(0);
1239 * This routine tries to maintain the pseudo LRU active queue,
1240 * so that during long periods of time where there is no paging,
1241 * that some statistic accumulation still occurs. This code
1242 * helps the situation where paging just starts to occur.
1244 static void
1245 vm_pageout_page_stats(void)
1247 vm_page_t m,next;
1248 int pcount,tpcount; /* Number of pages to check */
1249 static int fullintervalcount = 0;
1250 int page_shortage;
1252 page_shortage =
1253 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1254 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
1256 if (page_shortage <= 0)
1257 return;
1259 crit_enter();
1261 pcount = vmstats.v_active_count;
1262 fullintervalcount += vm_pageout_stats_interval;
1263 if (fullintervalcount < vm_pageout_full_stats_interval) {
1264 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
1265 if (pcount > tpcount)
1266 pcount = tpcount;
1267 } else {
1268 fullintervalcount = 0;
1271 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1272 while ((m != NULL) && (pcount-- > 0)) {
1273 int actcount;
1275 if (m->queue != PQ_ACTIVE) {
1276 break;
1279 next = TAILQ_NEXT(m, pageq);
1281 * Don't deactivate pages that are busy.
1283 if ((m->busy != 0) ||
1284 (m->flags & PG_BUSY) ||
1285 (m->hold_count != 0)) {
1286 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1287 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1288 m = next;
1289 continue;
1292 actcount = 0;
1293 if (m->flags & PG_REFERENCED) {
1294 vm_page_flag_clear(m, PG_REFERENCED);
1295 actcount += 1;
1298 actcount += pmap_ts_referenced(m);
1299 if (actcount) {
1300 m->act_count += ACT_ADVANCE + actcount;
1301 if (m->act_count > ACT_MAX)
1302 m->act_count = ACT_MAX;
1303 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1304 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1305 } else {
1306 if (m->act_count == 0) {
1308 * We turn off page access, so that we have
1309 * more accurate RSS stats. We don't do this
1310 * in the normal page deactivation when the
1311 * system is loaded VM wise, because the
1312 * cost of the large number of page protect
1313 * operations would be higher than the value
1314 * of doing the operation.
1316 vm_page_protect(m, VM_PROT_NONE);
1317 vm_page_deactivate(m);
1318 } else {
1319 m->act_count -= min(m->act_count, ACT_DECLINE);
1320 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1321 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1325 m = next;
1327 crit_exit();
1330 static int
1331 vm_pageout_free_page_calc(vm_size_t count)
1333 if (count < vmstats.v_page_count)
1334 return 0;
1336 * free_reserved needs to include enough for the largest swap pager
1337 * structures plus enough for any pv_entry structs when paging.
1339 if (vmstats.v_page_count > 1024)
1340 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
1341 else
1342 vmstats.v_free_min = 4;
1343 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1344 vmstats.v_interrupt_free_min;
1345 vmstats.v_free_reserved = vm_pageout_page_count +
1346 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1347 vmstats.v_free_severe = vmstats.v_free_min / 2;
1348 vmstats.v_free_min += vmstats.v_free_reserved;
1349 vmstats.v_free_severe += vmstats.v_free_reserved;
1350 return 1;
1355 * vm_pageout is the high level pageout daemon.
1357 static void
1358 vm_pageout(void)
1360 int pass;
1363 * Initialize some paging parameters.
1366 vmstats.v_interrupt_free_min = 2;
1367 if (vmstats.v_page_count < 2000)
1368 vm_pageout_page_count = 8;
1370 vm_pageout_free_page_calc(vmstats.v_page_count);
1372 * v_free_target and v_cache_min control pageout hysteresis. Note
1373 * that these are more a measure of the VM cache queue hysteresis
1374 * then the VM free queue. Specifically, v_free_target is the
1375 * high water mark (free+cache pages).
1377 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1378 * low water mark, while v_free_min is the stop. v_cache_min must
1379 * be big enough to handle memory needs while the pageout daemon
1380 * is signalled and run to free more pages.
1382 if (vmstats.v_free_count > 6144)
1383 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1384 else
1385 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1387 if (vmstats.v_free_count > 2048) {
1388 vmstats.v_cache_min = vmstats.v_free_target;
1389 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1390 vmstats.v_inactive_target = (3 * vmstats.v_free_target) / 2;
1391 } else {
1392 vmstats.v_cache_min = 0;
1393 vmstats.v_cache_max = 0;
1394 vmstats.v_inactive_target = vmstats.v_free_count / 4;
1396 if (vmstats.v_inactive_target > vmstats.v_free_count / 3)
1397 vmstats.v_inactive_target = vmstats.v_free_count / 3;
1399 /* XXX does not really belong here */
1400 if (vm_page_max_wired == 0)
1401 vm_page_max_wired = vmstats.v_free_count / 3;
1403 if (vm_pageout_stats_max == 0)
1404 vm_pageout_stats_max = vmstats.v_free_target;
1407 * Set interval in seconds for stats scan.
1409 if (vm_pageout_stats_interval == 0)
1410 vm_pageout_stats_interval = 5;
1411 if (vm_pageout_full_stats_interval == 0)
1412 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1416 * Set maximum free per pass
1418 if (vm_pageout_stats_free_max == 0)
1419 vm_pageout_stats_free_max = 5;
1421 swap_pager_swap_init();
1422 pass = 0;
1424 * The pageout daemon is never done, so loop forever.
1426 while (TRUE) {
1427 int error;
1430 * If we have enough free memory, wakeup waiters. Do
1431 * not clear vm_pages_needed until we reach our target,
1432 * otherwise we may be woken up over and over again and
1433 * waste a lot of cpu.
1435 crit_enter();
1436 if (vm_pages_needed && !vm_page_count_min()) {
1437 if (vm_paging_needed() <= 0)
1438 vm_pages_needed = 0;
1439 wakeup(&vmstats.v_free_count);
1441 if (vm_pages_needed) {
1443 * Still not done, take a second pass without waiting
1444 * (unlimited dirty cleaning), otherwise sleep a bit
1445 * and try again.
1447 ++pass;
1448 if (pass > 1)
1449 tsleep(&vm_pages_needed, 0, "psleep", hz/2);
1450 } else {
1452 * Good enough, sleep & handle stats. Prime the pass
1453 * for the next run.
1455 if (pass > 1)
1456 pass = 1;
1457 else
1458 pass = 0;
1459 error = tsleep(&vm_pages_needed,
1460 0, "psleep", vm_pageout_stats_interval * hz);
1461 if (error && !vm_pages_needed) {
1462 crit_exit();
1463 pass = 0;
1464 vm_pageout_page_stats();
1465 continue;
1469 if (vm_pages_needed)
1470 mycpu->gd_cnt.v_pdwakeups++;
1471 crit_exit();
1472 vm_pageout_scan(pass);
1473 vm_pageout_deficit = 0;
1477 void
1478 pagedaemon_wakeup(void)
1480 if (!vm_pages_needed && curthread != pagethread) {
1481 vm_pages_needed++;
1482 wakeup(&vm_pages_needed);
1486 #if !defined(NO_SWAPPING)
1487 static void
1488 vm_req_vmdaemon(void)
1490 static int lastrun = 0;
1492 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1493 wakeup(&vm_daemon_needed);
1494 lastrun = ticks;
1498 static int vm_daemon_callback(struct proc *p, void *data __unused);
1500 static void
1501 vm_daemon(void)
1503 while (TRUE) {
1504 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1505 if (vm_pageout_req_swapout) {
1506 swapout_procs(vm_pageout_req_swapout);
1507 vm_pageout_req_swapout = 0;
1510 * scan the processes for exceeding their rlimits or if
1511 * process is swapped out -- deactivate pages
1513 allproc_scan(vm_daemon_callback, NULL);
1517 static int
1518 vm_daemon_callback(struct proc *p, void *data __unused)
1520 vm_pindex_t limit, size;
1523 * if this is a system process or if we have already
1524 * looked at this process, skip it.
1526 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1527 return (0);
1530 * if the process is in a non-running type state,
1531 * don't touch it.
1533 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1534 return (0);
1537 * get a limit
1539 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1540 p->p_rlimit[RLIMIT_RSS].rlim_max));
1543 * let processes that are swapped out really be
1544 * swapped out. Set the limit to nothing to get as
1545 * many pages out to swap as possible.
1547 if (p->p_flag & P_SWAPPEDOUT)
1548 limit = 0;
1550 size = vmspace_resident_count(p->p_vmspace);
1551 if (limit >= 0 && size >= limit) {
1552 vm_pageout_map_deactivate_pages(
1553 &p->p_vmspace->vm_map, limit);
1555 return (0);
1558 #endif