2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
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
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
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.36 2008/07/01 02:02:56 dillon Exp $
73 * The proverbial page-out daemon.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.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>
89 #include <vm/vm_param.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 int 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
= {
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
= {
130 SYSINIT(vmdaemon
, SI_SUB_KTHREAD_VM
, SI_ORDER_FIRST
, kproc_start
, &vm_kp
)
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
;
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;
154 static int vm_swap_enabled
=1;
155 static int vm_swap_idle_enabled
=0;
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, "");
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");
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");
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");
206 SYSCTL_INT(_vm
, OID_AUTO
, vm_load_debug
,
207 CTLFLAG_RW
, &vm_load_debug
, 0, "debug vm_load");
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);
221 static void vm_pageout_page_stats(void);
224 * Update vm_load to slow down faulting processes.
227 vm_fault_ratecheck(void)
229 if (vm_pages_needed
) {
241 * Clean the page and remove it from the laundry. The page must not be
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.
250 vm_pageout_clean(vm_page_t m
)
253 vm_page_t mc
[2*vm_pageout_page_count
];
255 int ib
, is
, page_base
;
256 vm_pindex_t pindex
= m
->pindex
;
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
)))) {
277 mc
[vm_pageout_page_count
] = m
;
279 page_base
= vm_pageout_page_count
;
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
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.
304 while (ib
&& pageout_count
< vm_pageout_page_count
) {
312 if ((p
= vm_page_lookup(object
, pindex
- ib
)) == NULL
) {
316 if (((p
->queue
- p
->pc
) == PQ_CACHE
) ||
317 (p
->flags
& (PG_BUSY
|PG_UNMANAGED
)) || p
->busy
) {
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 */
333 * alignment boundry, stop here and switch directions. Do
336 if ((pindex
- (ib
- 1)) % vm_pageout_page_count
== 0)
340 while (pageout_count
< vm_pageout_page_count
&&
341 pindex
+ is
< object
->size
) {
344 if ((p
= vm_page_lookup(object
, pindex
+ is
)) == NULL
)
346 if (((p
->queue
- p
->pc
) == PQ_CACHE
) ||
347 (p
->flags
& (PG_BUSY
|PG_UNMANAGED
)) || p
->busy
) {
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 */
357 mc
[page_base
+ pageout_count
] = p
;
363 * If we exhausted our forward scan, continue with the reverse scan
364 * when possible, even past a page boundry. This catches boundry
367 if (ib
&& pageout_count
< vm_pageout_page_count
)
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
387 vm_pageout_flush(vm_page_t
*mc
, int count
, int flags
)
390 int pageout_status
[count
];
395 * Initiate I/O. Bump the vm_page_t->busy counter.
397 for (i
= 0; i
< count
; i
++) {
398 KASSERT(mc
[i
]->valid
== VM_PAGE_BITS_ALL
, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc
[i
], i
, count
));
399 vm_page_io_start(mc
[i
]);
403 * We must make the pages read-only. This will also force the
404 * modified bit in the related pmaps to be cleared. The pager
405 * cannot clear the bit for us since the I/O completion code
406 * typically runs from an interrupt. The act of making the page
407 * read-only handles the case for us.
409 for (i
= 0; i
< count
; i
++) {
410 vm_page_protect(mc
[i
], VM_PROT_READ
);
413 object
= mc
[0]->object
;
414 vm_object_pip_add(object
, count
);
416 vm_pager_put_pages(object
, mc
, count
,
417 (flags
| ((object
== &kernel_object
) ? VM_PAGER_PUT_SYNC
: 0)),
420 for (i
= 0; i
< count
; i
++) {
421 vm_page_t mt
= mc
[i
];
423 switch (pageout_status
[i
]) {
432 * Page outside of range of object. Right now we
433 * essentially lose the changes by pretending it
436 pmap_clear_modify(mt
);
442 * A page typically cannot be paged out when we
443 * have run out of swap. We leave the page
444 * marked inactive and will try to page it out
447 * Starvation of the active page list is used to
448 * determine when the system is massively memory
457 * If the operation is still going, leave the page busy to
458 * block all other accesses. Also, leave the paging in
459 * progress indicator set so that we don't attempt an object
462 * For any pages which have completed synchronously,
463 * deactivate the page if we are under a severe deficit.
464 * Do not try to enter them into the cache, though, they
465 * might still be read-heavy.
467 if (pageout_status
[i
] != VM_PAGER_PEND
) {
468 vm_object_pip_wakeup(object
);
469 vm_page_io_finish(mt
);
470 if (vm_page_count_severe())
471 vm_page_deactivate(mt
);
473 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt
))
474 vm_page_protect(mt
, VM_PROT_READ
);
481 #if !defined(NO_SWAPPING)
483 * vm_pageout_object_deactivate_pages
485 * deactivate enough pages to satisfy the inactive target
486 * requirements or if vm_page_proc_limit is set, then
487 * deactivate all of the pages in the object and its
490 * The object and map must be locked.
492 static int vm_pageout_object_deactivate_pages_callback(vm_page_t
, void *);
495 vm_pageout_object_deactivate_pages(vm_map_t map
, vm_object_t object
,
496 vm_pindex_t desired
, int map_remove_only
)
498 struct rb_vm_page_scan_info info
;
501 if (object
->type
== OBJT_DEVICE
|| object
->type
== OBJT_PHYS
)
505 if (pmap_resident_count(vm_map_pmap(map
)) <= desired
)
507 if (object
->paging_in_progress
)
510 remove_mode
= map_remove_only
;
511 if (object
->shadow_count
> 1)
515 * scan the objects entire memory queue. spl protection is
516 * required to avoid an interrupt unbusy/free race against
520 info
.limit
= remove_mode
;
522 info
.desired
= desired
;
523 vm_page_rb_tree_RB_SCAN(&object
->rb_memq
, NULL
,
524 vm_pageout_object_deactivate_pages_callback
,
528 object
= object
->backing_object
;
533 vm_pageout_object_deactivate_pages_callback(vm_page_t p
, void *data
)
535 struct rb_vm_page_scan_info
*info
= data
;
538 if (pmap_resident_count(vm_map_pmap(info
->map
)) <= info
->desired
) {
541 mycpu
->gd_cnt
.v_pdpages
++;
542 if (p
->wire_count
!= 0 || p
->hold_count
!= 0 || p
->busy
!= 0 ||
543 (p
->flags
& (PG_BUSY
|PG_UNMANAGED
)) ||
544 !pmap_page_exists_quick(vm_map_pmap(info
->map
), p
)) {
548 actcount
= pmap_ts_referenced(p
);
550 vm_page_flag_set(p
, PG_REFERENCED
);
551 } else if (p
->flags
& PG_REFERENCED
) {
555 if ((p
->queue
!= PQ_ACTIVE
) &&
556 (p
->flags
& PG_REFERENCED
)) {
558 p
->act_count
+= actcount
;
559 vm_page_flag_clear(p
, PG_REFERENCED
);
560 } else if (p
->queue
== PQ_ACTIVE
) {
561 if ((p
->flags
& PG_REFERENCED
) == 0) {
562 p
->act_count
-= min(p
->act_count
, ACT_DECLINE
);
563 if (!info
->limit
&& (vm_pageout_algorithm
|| (p
->act_count
== 0))) {
565 vm_page_protect(p
, VM_PROT_NONE
);
567 vm_page_deactivate(p
);
569 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, p
, pageq
);
570 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, p
, pageq
);
574 vm_page_flag_clear(p
, PG_REFERENCED
);
575 if (p
->act_count
< (ACT_MAX
- ACT_ADVANCE
))
576 p
->act_count
+= ACT_ADVANCE
;
577 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, p
, pageq
);
578 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, p
, pageq
);
580 } else if (p
->queue
== PQ_INACTIVE
) {
582 vm_page_protect(p
, VM_PROT_NONE
);
589 * deactivate some number of pages in a map, try to do it fairly, but
590 * that is really hard to do.
593 vm_pageout_map_deactivate_pages(vm_map_t map
, vm_pindex_t desired
)
596 vm_object_t obj
, bigobj
;
599 if (lockmgr(&map
->lock
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
607 * first, search out the biggest object, and try to free pages from
610 tmpe
= map
->header
.next
;
611 while (tmpe
!= &map
->header
) {
612 switch(tmpe
->maptype
) {
613 case VM_MAPTYPE_NORMAL
:
614 case VM_MAPTYPE_VPAGETABLE
:
615 obj
= tmpe
->object
.vm_object
;
616 if ((obj
!= NULL
) && (obj
->shadow_count
<= 1) &&
618 (bigobj
->resident_page_count
< obj
->resident_page_count
))) {
625 if (tmpe
->wired_count
> 0)
626 nothingwired
= FALSE
;
631 vm_pageout_object_deactivate_pages(map
, bigobj
, desired
, 0);
634 * Next, hunt around for other pages to deactivate. We actually
635 * do this search sort of wrong -- .text first is not the best idea.
637 tmpe
= map
->header
.next
;
638 while (tmpe
!= &map
->header
) {
639 if (pmap_resident_count(vm_map_pmap(map
)) <= desired
)
641 switch(tmpe
->maptype
) {
642 case VM_MAPTYPE_NORMAL
:
643 case VM_MAPTYPE_VPAGETABLE
:
644 obj
= tmpe
->object
.vm_object
;
646 vm_pageout_object_deactivate_pages(map
, obj
, desired
, 0);
655 * Remove all mappings if a process is swapped out, this will free page
658 if (desired
== 0 && nothingwired
)
659 pmap_remove(vm_map_pmap(map
),
660 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
666 * Don't try to be fancy - being fancy can lead to vnode deadlocks. We
667 * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can
668 * be trivially freed.
671 vm_pageout_page_free(vm_page_t m
)
673 vm_object_t object
= m
->object
;
674 int type
= object
->type
;
676 if (type
== OBJT_SWAP
|| type
== OBJT_DEFAULT
)
677 vm_object_reference(object
);
679 vm_page_protect(m
, VM_PROT_NONE
);
681 if (type
== OBJT_SWAP
|| type
== OBJT_DEFAULT
)
682 vm_object_deallocate(object
);
686 * vm_pageout_scan does the dirty work for the pageout daemon.
688 struct vm_pageout_scan_info
{
689 struct proc
*bigproc
;
693 static int vm_pageout_scan_callback(struct proc
*p
, void *data
);
696 vm_pageout_scan(int pass
)
698 struct vm_pageout_scan_info info
;
700 struct vm_page marker
;
703 int inactive_shortage
, active_shortage
;
706 int vnodes_skipped
= 0;
710 * Do whatever cleanup that the pmap code can.
715 * Calculate our target for the number of free+cache pages we
716 * want to get to. This is higher then the number that causes
717 * allocations to stall (severe) in order to provide hysteresis,
718 * and if we don't make it all the way but get to the minimum
721 inactive_shortage
= vm_paging_target() + vm_pageout_deficit
;
722 vm_pageout_deficit
= 0;
725 * Initialize our marker
727 bzero(&marker
, sizeof(marker
));
728 marker
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_MARKER
;
729 marker
.queue
= PQ_INACTIVE
;
730 marker
.wire_count
= 1;
733 * Start scanning the inactive queue for pages we can move to the
734 * cache or free. The scan will stop when the target is reached or
735 * we have scanned the entire inactive queue. Note that m->act_count
736 * is not used to form decisions for the inactive queue, only for the
739 * maxlaunder limits the number of dirty pages we flush per scan.
740 * For most systems a smaller value (16 or 32) is more robust under
741 * extreme memory and disk pressure because any unnecessary writes
742 * to disk can result in extreme performance degredation. However,
743 * systems with excessive dirty pages (especially when MAP_NOSYNC is
744 * used) will die horribly with limited laundering. If the pageout
745 * daemon cannot clean enough pages in the first pass, we let it go
746 * all out in succeeding passes.
748 if ((maxlaunder
= vm_max_launder
) <= 1)
754 * We will generally be in a critical section throughout the
755 * scan, but we can release it temporarily when we are sitting on a
756 * non-busy page without fear. this is required to prevent an
757 * interrupt from unbusying or freeing a page prior to our busy
758 * check, leaving us on the wrong queue or checking the wrong
763 maxscan
= vmstats
.v_inactive_count
;
764 for (m
= TAILQ_FIRST(&vm_page_queues
[PQ_INACTIVE
].pl
);
765 m
!= NULL
&& maxscan
-- > 0 && inactive_shortage
> 0;
768 mycpu
->gd_cnt
.v_pdpages
++;
771 * Give interrupts a chance
777 * It's easier for some of the conditions below to just loop
778 * and catch queue changes here rather then check everywhere
781 if (m
->queue
!= PQ_INACTIVE
)
783 next
= TAILQ_NEXT(m
, pageq
);
788 if (m
->flags
& PG_MARKER
)
792 * A held page may be undergoing I/O, so skip it.
795 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
796 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
801 * Dont mess with busy pages, keep in the front of the
802 * queue, most likely are being paged out.
804 if (m
->busy
|| (m
->flags
& PG_BUSY
)) {
808 if (m
->object
->ref_count
== 0) {
810 * If the object is not being used, we ignore previous
813 vm_page_flag_clear(m
, PG_REFERENCED
);
814 pmap_clear_reference(m
);
816 } else if (((m
->flags
& PG_REFERENCED
) == 0) &&
817 (actcount
= pmap_ts_referenced(m
))) {
819 * Otherwise, if the page has been referenced while
820 * in the inactive queue, we bump the "activation
821 * count" upwards, making it less likely that the
822 * page will be added back to the inactive queue
823 * prematurely again. Here we check the page tables
824 * (or emulated bits, if any), given the upper level
825 * VM system not knowing anything about existing
829 m
->act_count
+= (actcount
+ ACT_ADVANCE
);
834 * If the upper level VM system knows about any page
835 * references, we activate the page. We also set the
836 * "activation count" higher than normal so that we will less
837 * likely place pages back onto the inactive queue again.
839 if ((m
->flags
& PG_REFERENCED
) != 0) {
840 vm_page_flag_clear(m
, PG_REFERENCED
);
841 actcount
= pmap_ts_referenced(m
);
843 m
->act_count
+= (actcount
+ ACT_ADVANCE
+ 1);
848 * If the upper level VM system doesn't know anything about
849 * the page being dirty, we have to check for it again. As
850 * far as the VM code knows, any partially dirty pages are
853 * Pages marked PG_WRITEABLE may be mapped into the user
854 * address space of a process running on another cpu. A
855 * user process (without holding the MP lock) running on
856 * another cpu may be able to touch the page while we are
857 * trying to remove it. vm_page_cache() will handle this
861 vm_page_test_dirty(m
);
868 * Invalid pages can be easily freed
870 vm_pageout_page_free(m
);
871 mycpu
->gd_cnt
.v_dfree
++;
873 } else if (m
->dirty
== 0) {
875 * Clean pages can be placed onto the cache queue.
876 * This effectively frees them.
880 } else if ((m
->flags
& PG_WINATCFLS
) == 0 && pass
== 0) {
882 * Dirty pages need to be paged out, but flushing
883 * a page is extremely expensive verses freeing
884 * a clean page. Rather then artificially limiting
885 * the number of pages we can flush, we instead give
886 * dirty pages extra priority on the inactive queue
887 * by forcing them to be cycled through the queue
888 * twice before being flushed, after which the
889 * (now clean) page will cycle through once more
890 * before being freed. This significantly extends
891 * the thrash point for a heavily loaded machine.
893 vm_page_flag_set(m
, PG_WINATCFLS
);
894 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
895 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
896 } else if (maxlaunder
> 0) {
898 * We always want to try to flush some dirty pages if
899 * we encounter them, to keep the system stable.
900 * Normally this number is small, but under extreme
901 * pressure where there are insufficient clean pages
902 * on the inactive queue, we may have to go all out.
904 int swap_pageouts_ok
;
905 struct vnode
*vp
= NULL
;
909 if ((object
->type
!= OBJT_SWAP
) && (object
->type
!= OBJT_DEFAULT
)) {
910 swap_pageouts_ok
= 1;
912 swap_pageouts_ok
= !(defer_swap_pageouts
|| disable_swap_pageouts
);
913 swap_pageouts_ok
|= (!disable_swap_pageouts
&& defer_swap_pageouts
&&
914 vm_page_count_min(0));
919 * We don't bother paging objects that are "dead".
920 * Those objects are in a "rundown" state.
922 if (!swap_pageouts_ok
|| (object
->flags
& OBJ_DEAD
)) {
923 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
924 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
929 * The object is already known NOT to be dead. It
930 * is possible for the vget() to block the whole
931 * pageout daemon, but the new low-memory handling
932 * code should prevent it.
934 * The previous code skipped locked vnodes and, worse,
935 * reordered pages in the queue. This results in
936 * completely non-deterministic operation because,
937 * quite often, a vm_fault has initiated an I/O and
938 * is holding a locked vnode at just the point where
939 * the pageout daemon is woken up.
941 * We can't wait forever for the vnode lock, we might
942 * deadlock due to a vn_read() getting stuck in
943 * vm_wait while holding this vnode. We skip the
944 * vnode if we can't get it in a reasonable amount
948 if (object
->type
== OBJT_VNODE
) {
951 if (vget(vp
, LK_EXCLUSIVE
|LK_NOOBJ
|LK_TIMELOCK
)) {
953 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
959 * The page might have been moved to another
960 * queue during potential blocking in vget()
961 * above. The page might have been freed and
962 * reused for another vnode. The object might
963 * have been reused for another vnode.
965 if (m
->queue
!= PQ_INACTIVE
||
966 m
->object
!= object
||
967 object
->handle
!= vp
) {
968 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
975 * The page may have been busied during the
976 * blocking in vput(); We don't move the
977 * page back onto the end of the queue so that
978 * statistics are more correct if we don't.
980 if (m
->busy
|| (m
->flags
& PG_BUSY
)) {
986 * If the page has become held it might
987 * be undergoing I/O, so skip it
990 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
991 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, pageq
);
992 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
1000 * If a page is dirty, then it is either being washed
1001 * (but not yet cleaned) or it is still in the
1002 * laundry. If it is still in the laundry, then we
1003 * start the cleaning operation.
1005 * This operation may cluster, invalidating the 'next'
1006 * pointer. To prevent an inordinate number of
1007 * restarts we use our marker to remember our place.
1009 * decrement inactive_shortage on success to account
1010 * for the (future) cleaned page. Otherwise we
1011 * could wind up laundering or cleaning too many
1014 TAILQ_INSERT_AFTER(&vm_page_queues
[PQ_INACTIVE
].pl
, m
, &marker
, pageq
);
1015 if (vm_pageout_clean(m
) != 0) {
1016 --inactive_shortage
;
1019 next
= TAILQ_NEXT(&marker
, pageq
);
1020 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
].pl
, &marker
, pageq
);
1027 * We want to move pages from the active queue to the inactive
1028 * queue to get the inactive queue to the inactive target. If
1029 * we still have a page shortage from above we try to directly free
1030 * clean pages instead of moving them.
1032 * If we do still have a shortage we keep track of the number of
1033 * pages we free or cache (recycle_count) as a measure of thrashing
1034 * between the active and inactive queues.
1036 * We do not do this if we were able to satisfy the requirement
1037 * entirely from the inactive queue.
1039 * NOTE: Both variables can end up negative.
1040 * NOTE: We are still in a critical section.
1042 active_shortage
= vmstats
.v_inactive_target
- vmstats
.v_inactive_count
;
1043 if (inactive_shortage
<= 0)
1044 active_shortage
= 0;
1046 pcount
= vmstats
.v_active_count
;
1048 m
= TAILQ_FIRST(&vm_page_queues
[PQ_ACTIVE
].pl
);
1050 while ((m
!= NULL
) && (pcount
-- > 0) &&
1051 (inactive_shortage
> 0 || active_shortage
> 0)
1054 * Give interrupts a chance.
1060 * If the page was ripped out from under us, just stop.
1062 if (m
->queue
!= PQ_ACTIVE
)
1064 next
= TAILQ_NEXT(m
, pageq
);
1067 * Don't deactivate pages that are busy.
1069 if ((m
->busy
!= 0) ||
1070 (m
->flags
& PG_BUSY
) ||
1071 (m
->hold_count
!= 0)) {
1072 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1073 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1079 * The count for pagedaemon pages is done after checking the
1080 * page for eligibility...
1082 mycpu
->gd_cnt
.v_pdpages
++;
1085 * Check to see "how much" the page has been used and clear
1086 * the tracking access bits. If the object has no references
1087 * don't bother paying the expense.
1090 if (m
->object
->ref_count
!= 0) {
1091 if (m
->flags
& PG_REFERENCED
)
1093 actcount
+= pmap_ts_referenced(m
);
1095 m
->act_count
+= ACT_ADVANCE
+ actcount
;
1096 if (m
->act_count
> ACT_MAX
)
1097 m
->act_count
= ACT_MAX
;
1100 vm_page_flag_clear(m
, PG_REFERENCED
);
1103 * actcount is only valid if the object ref_count is non-zero.
1105 if (actcount
&& m
->object
->ref_count
!= 0) {
1106 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1107 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1109 m
->act_count
-= min(m
->act_count
, ACT_DECLINE
);
1110 if (vm_pageout_algorithm
||
1111 m
->object
->ref_count
== 0 ||
1112 m
->act_count
< pass
+ 1
1115 * Deactivate the page. If we had a
1116 * shortage from our inactive scan try to
1117 * free (cache) the page instead.
1120 if (inactive_shortage
> 0 ||
1121 m
->object
->ref_count
== 0) {
1122 if (inactive_shortage
> 0)
1125 vm_page_protect(m
, VM_PROT_NONE
);
1127 if (m
->dirty
== 0) {
1128 --inactive_shortage
;
1131 vm_page_deactivate(m
);
1134 vm_page_deactivate(m
);
1137 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1138 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1145 * We try to maintain some *really* free pages, this allows interrupt
1146 * code to be guaranteed space. Since both cache and free queues
1147 * are considered basically 'free', moving pages from cache to free
1148 * does not effect other calculations.
1150 * NOTE: we are still in a critical section.
1152 * Pages moved from PQ_CACHE to totally free are not counted in the
1153 * pages_freed counter.
1155 while (vmstats
.v_free_count
< vmstats
.v_free_reserved
) {
1156 static int cache_rover
= 0;
1157 m
= vm_page_list_find(PQ_CACHE
, cache_rover
, FALSE
);
1160 if ((m
->flags
& (PG_BUSY
|PG_UNMANAGED
)) ||
1165 kprintf("Warning: busy page %p found in cache\n", m
);
1167 vm_page_deactivate(m
);
1170 KKASSERT((m
->flags
& PG_MAPPED
) == 0);
1171 KKASSERT(m
->dirty
== 0);
1172 cache_rover
= (cache_rover
+ PQ_PRIME2
) & PQ_L2_MASK
;
1173 vm_pageout_page_free(m
);
1174 mycpu
->gd_cnt
.v_dfree
++;
1179 #if !defined(NO_SWAPPING)
1181 * Idle process swapout -- run once per second.
1183 if (vm_swap_idle_enabled
) {
1185 if (time_second
!= lsec
) {
1186 vm_pageout_req_swapout
|= VM_SWAP_IDLE
;
1194 * If we didn't get enough free pages, and we have skipped a vnode
1195 * in a writeable object, wakeup the sync daemon. And kick swapout
1196 * if we did not get enough free pages.
1198 if (vm_paging_target() > 0) {
1199 if (vnodes_skipped
&& vm_page_count_min(0))
1201 #if !defined(NO_SWAPPING)
1202 if (vm_swap_enabled
&& vm_page_count_target()) {
1204 vm_pageout_req_swapout
|= VM_SWAP_NORMAL
;
1210 * Handle catastrophic conditions. Under good conditions we should
1211 * be at the target, well beyond our minimum. If we could not even
1212 * reach our minimum the system is under heavy stress.
1214 * Determine whether we have run out of memory. This occurs when
1215 * swap_pager_full is TRUE and the only pages left in the page
1216 * queues are dirty. We will still likely have page shortages.
1218 * - swap_pager_full is set if insufficient swap was
1219 * available to satisfy a requested pageout.
1221 * - the inactive queue is bloated (4 x size of active queue),
1222 * meaning it is unable to get rid of dirty pages and.
1224 * - vm_page_count_min() without counting pages recycled from the
1225 * active queue (recycle_count) means we could not recover
1226 * enough pages to meet bare minimum needs. This test only
1227 * works if the inactive queue is bloated.
1229 * - due to a positive inactive_shortage we shifted the remaining
1230 * dirty pages from the active queue to the inactive queue
1231 * trying to find clean ones to free.
1233 if (swap_pager_full
&& vm_page_count_min(recycle_count
))
1234 kprintf("Warning: system low on memory+swap!\n");
1235 if (swap_pager_full
&& vm_page_count_min(recycle_count
) &&
1236 vmstats
.v_inactive_count
> vmstats
.v_active_count
* 4 &&
1237 inactive_shortage
> 0) {
1241 info
.bigproc
= NULL
;
1243 allproc_scan(vm_pageout_scan_callback
, &info
);
1244 if (info
.bigproc
!= NULL
) {
1245 killproc(info
.bigproc
, "out of swap space");
1246 info
.bigproc
->p_nice
= PRIO_MIN
;
1247 info
.bigproc
->p_usched
->resetpriority(
1248 FIRST_LWP_IN_PROC(info
.bigproc
));
1249 wakeup(&vmstats
.v_free_count
);
1250 PRELE(info
.bigproc
);
1253 return(inactive_shortage
);
1257 vm_pageout_scan_callback(struct proc
*p
, void *data
)
1259 struct vm_pageout_scan_info
*info
= data
;
1263 * Never kill system processes or init. If we have configured swap
1264 * then try to avoid killing low-numbered pids.
1266 if ((p
->p_flag
& P_SYSTEM
) || (p
->p_pid
== 1) ||
1267 ((p
->p_pid
< 48) && (vm_swap_size
!= 0))) {
1272 * if the process is in a non-running type state,
1275 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
)
1279 * Get the approximate process size. Note that anonymous pages
1280 * with backing swap will be counted twice, but there should not
1281 * be too many such pages due to the stress the VM system is
1282 * under at this point.
1284 size
= vmspace_anonymous_count(p
->p_vmspace
) +
1285 vmspace_swap_count(p
->p_vmspace
);
1288 * If the this process is bigger than the biggest one
1291 if (info
->bigsize
< size
) {
1293 PRELE(info
->bigproc
);
1296 info
->bigsize
= size
;
1302 * This routine tries to maintain the pseudo LRU active queue,
1303 * so that during long periods of time where there is no paging,
1304 * that some statistic accumulation still occurs. This code
1305 * helps the situation where paging just starts to occur.
1308 vm_pageout_page_stats(void)
1311 int pcount
,tpcount
; /* Number of pages to check */
1312 static int fullintervalcount
= 0;
1316 (vmstats
.v_inactive_target
+ vmstats
.v_cache_max
+ vmstats
.v_free_min
) -
1317 (vmstats
.v_free_count
+ vmstats
.v_inactive_count
+ vmstats
.v_cache_count
);
1319 if (page_shortage
<= 0)
1324 pcount
= vmstats
.v_active_count
;
1325 fullintervalcount
+= vm_pageout_stats_interval
;
1326 if (fullintervalcount
< vm_pageout_full_stats_interval
) {
1327 tpcount
= (vm_pageout_stats_max
* vmstats
.v_active_count
) / vmstats
.v_page_count
;
1328 if (pcount
> tpcount
)
1331 fullintervalcount
= 0;
1334 m
= TAILQ_FIRST(&vm_page_queues
[PQ_ACTIVE
].pl
);
1335 while ((m
!= NULL
) && (pcount
-- > 0)) {
1338 if (m
->queue
!= PQ_ACTIVE
) {
1342 next
= TAILQ_NEXT(m
, pageq
);
1344 * Don't deactivate pages that are busy.
1346 if ((m
->busy
!= 0) ||
1347 (m
->flags
& PG_BUSY
) ||
1348 (m
->hold_count
!= 0)) {
1349 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1350 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1356 if (m
->flags
& PG_REFERENCED
) {
1357 vm_page_flag_clear(m
, PG_REFERENCED
);
1361 actcount
+= pmap_ts_referenced(m
);
1363 m
->act_count
+= ACT_ADVANCE
+ actcount
;
1364 if (m
->act_count
> ACT_MAX
)
1365 m
->act_count
= ACT_MAX
;
1366 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1367 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1369 if (m
->act_count
== 0) {
1371 * We turn off page access, so that we have
1372 * more accurate RSS stats. We don't do this
1373 * in the normal page deactivation when the
1374 * system is loaded VM wise, because the
1375 * cost of the large number of page protect
1376 * operations would be higher than the value
1377 * of doing the operation.
1380 vm_page_protect(m
, VM_PROT_NONE
);
1382 vm_page_deactivate(m
);
1384 m
->act_count
-= min(m
->act_count
, ACT_DECLINE
);
1385 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1386 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_ACTIVE
].pl
, m
, pageq
);
1396 vm_pageout_free_page_calc(vm_size_t count
)
1398 if (count
< vmstats
.v_page_count
)
1401 * free_reserved needs to include enough for the largest swap pager
1402 * structures plus enough for any pv_entry structs when paging.
1404 if (vmstats
.v_page_count
> 1024)
1405 vmstats
.v_free_min
= 4 + (vmstats
.v_page_count
- 1024) / 200;
1407 vmstats
.v_free_min
= 4;
1408 vmstats
.v_pageout_free_min
= (2*MAXBSIZE
)/PAGE_SIZE
+
1409 vmstats
.v_interrupt_free_min
;
1410 vmstats
.v_free_reserved
= vm_pageout_page_count
+
1411 vmstats
.v_pageout_free_min
+ (count
/ 768) + PQ_L2_SIZE
;
1412 vmstats
.v_free_severe
= vmstats
.v_free_min
/ 2;
1413 vmstats
.v_free_min
+= vmstats
.v_free_reserved
;
1414 vmstats
.v_free_severe
+= vmstats
.v_free_reserved
;
1420 * vm_pageout is the high level pageout daemon.
1426 int inactive_shortage
;
1429 * Initialize some paging parameters.
1431 curthread
->td_flags
|= TDF_SYSTHREAD
;
1433 vmstats
.v_interrupt_free_min
= 2;
1434 if (vmstats
.v_page_count
< 2000)
1435 vm_pageout_page_count
= 8;
1437 vm_pageout_free_page_calc(vmstats
.v_page_count
);
1440 * v_free_target and v_cache_min control pageout hysteresis. Note
1441 * that these are more a measure of the VM cache queue hysteresis
1442 * then the VM free queue. Specifically, v_free_target is the
1443 * high water mark (free+cache pages).
1445 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1446 * low water mark, while v_free_min is the stop. v_cache_min must
1447 * be big enough to handle memory needs while the pageout daemon
1448 * is signalled and run to free more pages.
1450 if (vmstats
.v_free_count
> 6144)
1451 vmstats
.v_free_target
= 4 * vmstats
.v_free_min
+ vmstats
.v_free_reserved
;
1453 vmstats
.v_free_target
= 2 * vmstats
.v_free_min
+ vmstats
.v_free_reserved
;
1455 if (vmstats
.v_free_count
> 2048) {
1456 vmstats
.v_cache_min
= vmstats
.v_free_target
;
1457 vmstats
.v_cache_max
= 2 * vmstats
.v_cache_min
;
1458 vmstats
.v_inactive_target
= (3 * vmstats
.v_free_target
) / 2;
1460 vmstats
.v_cache_min
= 0;
1461 vmstats
.v_cache_max
= 0;
1462 vmstats
.v_inactive_target
= vmstats
.v_free_count
/ 4;
1464 if (vmstats
.v_inactive_target
> vmstats
.v_free_count
/ 3)
1465 vmstats
.v_inactive_target
= vmstats
.v_free_count
/ 3;
1467 /* XXX does not really belong here */
1468 if (vm_page_max_wired
== 0)
1469 vm_page_max_wired
= vmstats
.v_free_count
/ 3;
1471 if (vm_pageout_stats_max
== 0)
1472 vm_pageout_stats_max
= vmstats
.v_free_target
;
1475 * Set interval in seconds for stats scan.
1477 if (vm_pageout_stats_interval
== 0)
1478 vm_pageout_stats_interval
= 5;
1479 if (vm_pageout_full_stats_interval
== 0)
1480 vm_pageout_full_stats_interval
= vm_pageout_stats_interval
* 4;
1484 * Set maximum free per pass
1486 if (vm_pageout_stats_free_max
== 0)
1487 vm_pageout_stats_free_max
= 5;
1489 swap_pager_swap_init();
1493 * The pageout daemon is never done, so loop forever.
1498 if (vm_pages_needed
== 0) {
1500 * Wait for an action request
1502 error
= tsleep(&vm_pages_needed
,
1504 vm_pageout_stats_interval
* hz
);
1505 if (error
&& vm_pages_needed
== 0) {
1506 vm_pageout_page_stats();
1509 vm_pages_needed
= 1;
1513 * If we have enough free memory, wakeup waiters.
1516 if (!vm_page_count_min(0))
1517 wakeup(&vmstats
.v_free_count
);
1518 mycpu
->gd_cnt
.v_pdwakeups
++;
1520 inactive_shortage
= vm_pageout_scan(pass
);
1523 * Try to avoid thrashing the system with activity.
1525 if (inactive_shortage
> 0) {
1527 if (swap_pager_full
) {
1529 * Running out of memory, catastrophic back-off
1530 * to one-second intervals.
1532 tsleep(&vm_pages_needed
, 0, "pdelay", hz
);
1533 } else if (pass
< 10 && vm_pages_needed
> 1) {
1535 * Normal operation, additional processes
1536 * have already kicked us. Retry immediately.
1538 } else if (pass
< 10) {
1540 * Normal operation, fewer processes. Delay
1541 * a bit but allow wakeups.
1543 vm_pages_needed
= 0;
1544 tsleep(&vm_pages_needed
, 0, "pdelay", hz
/ 10);
1545 vm_pages_needed
= 1;
1548 * We've taken too many passes, forced delay.
1550 tsleep(&vm_pages_needed
, 0, "pdelay", hz
/ 10);
1554 vm_pages_needed
= 0;
1560 * Called after allocating a page out of the cache or free queue
1561 * to possibly wake the pagedaemon up to replentish our supply.
1563 * We try to generate some hysteresis by waking the pagedaemon up
1564 * when our free+cache pages go below the severe level. The pagedaemon
1565 * tries to get the count back up to at least the minimum, and through
1566 * to the target level if possible.
1568 * If the pagedaemon is already active bump vm_pages_needed as a hint
1569 * that there are even more requests pending.
1572 pagedaemon_wakeup(void)
1574 if (vm_page_count_severe() && curthread
!= pagethread
) {
1575 if (vm_pages_needed
== 0) {
1576 vm_pages_needed
= 1;
1577 wakeup(&vm_pages_needed
);
1578 } else if (vm_page_count_min(0)) {
1584 #if !defined(NO_SWAPPING)
1586 vm_req_vmdaemon(void)
1588 static int lastrun
= 0;
1590 if ((ticks
> (lastrun
+ hz
)) || (ticks
< lastrun
)) {
1591 wakeup(&vm_daemon_needed
);
1596 static int vm_daemon_callback(struct proc
*p
, void *data __unused
);
1602 tsleep(&vm_daemon_needed
, 0, "psleep", 0);
1603 if (vm_pageout_req_swapout
) {
1604 swapout_procs(vm_pageout_req_swapout
);
1605 vm_pageout_req_swapout
= 0;
1608 * scan the processes for exceeding their rlimits or if
1609 * process is swapped out -- deactivate pages
1611 allproc_scan(vm_daemon_callback
, NULL
);
1616 vm_daemon_callback(struct proc
*p
, void *data __unused
)
1618 vm_pindex_t limit
, size
;
1621 * if this is a system process or if we have already
1622 * looked at this process, skip it.
1624 if (p
->p_flag
& (P_SYSTEM
| P_WEXIT
))
1628 * if the process is in a non-running type state,
1631 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
)
1637 limit
= OFF_TO_IDX(qmin(p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
,
1638 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
));
1641 * let processes that are swapped out really be
1642 * swapped out. Set the limit to nothing to get as
1643 * many pages out to swap as possible.
1645 if (p
->p_flag
& P_SWAPPEDOUT
)
1648 size
= vmspace_resident_count(p
->p_vmspace
);
1649 if (limit
>= 0 && size
>= limit
) {
1650 vm_pageout_map_deactivate_pages(
1651 &p
->p_vmspace
->vm_map
, limit
);