vm_pageout.c revision 3612
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.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
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.
27 *
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.
39 *
40 *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
41 *
42 *
43 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44 * All rights reserved.
45 *
46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
47 *
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.
53 *
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.
57 *
58 * Carnegie Mellon requests users of this software to return to
59 *
60 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61 *  School of Computer Science
62 *  Carnegie Mellon University
63 *  Pittsburgh PA 15213-3890
64 *
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
67 *
68 * $Id: vm_pageout.c,v 1.17 1994/10/13 21:01:39 davidg Exp $
69 */
70
71/*
72 *	The proverbial page-out daemon.
73 */
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/proc.h>
78#include <sys/resourcevar.h>
79#include <sys/malloc.h>
80
81#include <vm/vm.h>
82#include <vm/vm_page.h>
83#include <vm/vm_pageout.h>
84#include <vm/swap_pager.h>
85
86extern vm_map_t kmem_map;
87int	vm_pages_needed;		/* Event on which pageout daemon sleeps */
88int	vm_pagescanner;			/* Event on which pagescanner sleeps */
89int	vm_pageout_free_min = 0;	/* Stop pageout to wait for pagers at this free level */
90
91int	vm_pageout_pages_needed = 0;	/* flag saying that the pageout daemon needs pages */
92int	vm_page_pagesfreed;
93int	vm_desired_cache_size;
94
95extern int npendingio;
96extern int hz;
97int	vm_pageout_proc_limit;
98extern int nswiodone;
99extern int swap_pager_full;
100extern int swap_pager_ready();
101
102#define MAXREF 32767
103
104#define MAXSCAN 512	/* maximum number of pages to scan in active queue */
105			/* set the "clock" hands to be (MAXSCAN * 4096) Bytes */
106#define ACT_DECLINE	1
107#define ACT_ADVANCE	3
108#define ACT_MAX		100
109
110#define LOWATER ((2048*1024)/NBPG)
111
112#define VM_PAGEOUT_PAGE_COUNT 8
113int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
114int vm_pageout_req_do_stats;
115
116int	vm_page_max_wired = 0;	/* XXX max # of wired pages system-wide */
117
118
119/*
120 * vm_pageout_clean:
121 * 	cleans a vm_page
122 */
123int
124vm_pageout_clean(m, sync)
125	register vm_page_t m;
126	int sync;
127{
128	/*
129	 *	Clean the page and remove it from the
130	 *	laundry.
131	 *
132	 *	We set the busy bit to cause
133	 *	potential page faults on this page to
134	 *	block.
135	 *
136	 *	And we set pageout-in-progress to keep
137	 *	the object from disappearing during
138	 *	pageout.  This guarantees that the
139	 *	page won't move from the inactive
140	 *	queue.  (However, any other page on
141	 *	the inactive queue may move!)
142	 */
143
144	register vm_object_t	object;
145	register vm_pager_t	pager;
146	int			pageout_status[VM_PAGEOUT_PAGE_COUNT];
147	vm_page_t		ms[VM_PAGEOUT_PAGE_COUNT];
148	int			pageout_count;
149	int			anyok=0;
150	int			i;
151	vm_offset_t offset = m->offset;
152
153	object = m->object;
154	if (!object) {
155		printf("pager: object missing\n");
156		return 0;
157	}
158
159	/*
160	 *	Try to collapse the object before
161	 *	making a pager for it.  We must
162	 *	unlock the page queues first.
163	 *	We try to defer the creation of a pager
164	 *	until all shadows are not paging.  This
165	 *	allows vm_object_collapse to work better and
166	 *	helps control swap space size.
167	 *	(J. Dyson 11 Nov 93)
168	 */
169
170	if (!object->pager &&
171		cnt.v_free_count < vm_pageout_free_min)
172		return 0;
173
174	if (!object->pager &&
175		object->shadow &&
176		object->shadow->paging_in_progress)
177		return 0;
178
179	if( !sync) {
180		if (object->shadow) {
181			vm_object_collapse(object);
182			if (!vm_page_lookup(object, offset))
183				return 0;
184		}
185
186		if ((m->busy != 0) ||
187			(m->flags & PG_BUSY) || (m->hold_count != 0)) {
188			return 0;
189		}
190	}
191
192	pageout_count = 1;
193	ms[0] = m;
194
195	pager = object->pager;
196	if (pager) {
197		for (i = 1; i < vm_pageout_page_count; i++) {
198			ms[i] = vm_page_lookup(object, offset+i*NBPG);
199			if (ms[i]) {
200				if (( ((ms[i]->flags & (PG_CLEAN|PG_INACTIVE|PG_BUSY)) == PG_INACTIVE)
201					|| ( (ms[i]->flags & (PG_CLEAN|PG_BUSY)) == 0 && sync == VM_PAGEOUT_FORCE))
202					&& (ms[i]->wire_count == 0)
203					&& (ms[i]->busy == 0)
204					&& (ms[i]->hold_count == 0))
205					pageout_count++;
206				else
207					break;
208			} else
209				break;
210		}
211		for(i=0;i<pageout_count;i++) {
212			ms[i]->flags |= PG_BUSY;
213			pmap_page_protect(VM_PAGE_TO_PHYS(ms[i]), VM_PROT_READ);
214		}
215		object->paging_in_progress += pageout_count;
216	} else {
217
218		m->flags |= PG_BUSY;
219
220		pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_READ);
221
222		object->paging_in_progress++;
223
224		pager = vm_pager_allocate(PG_DFLT, (caddr_t)0,
225			object->size, VM_PROT_ALL, 0);
226		if (pager != NULL) {
227			vm_object_setpager(object, pager, 0, FALSE);
228		}
229	}
230
231	/*
232	 *	If there is no pager for the page,
233	 *	use the default pager.  If there's
234	 *	no place to put the page at the
235	 *	moment, leave it in the laundry and
236	 *	hope that there will be paging space
237	 *	later.
238	 */
239
240	if ((pager && pager->pg_type == PG_SWAP) ||
241		cnt.v_free_count >= vm_pageout_free_min) {
242		if( pageout_count == 1) {
243			pageout_status[0] = pager ?
244				vm_pager_put(pager, m,
245				    ((sync || (object == kernel_object)) ? TRUE: FALSE)) :
246				VM_PAGER_FAIL;
247		} else {
248			if( !pager) {
249				for(i=0;i<pageout_count;i++)
250					pageout_status[i] = VM_PAGER_FAIL;
251			} else {
252				vm_pager_put_pages(pager, ms, pageout_count,
253				    ((sync || (object == kernel_object)) ? TRUE : FALSE),
254				    pageout_status);
255			}
256		}
257
258	} else {
259		for(i=0;i<pageout_count;i++)
260			pageout_status[i] = VM_PAGER_FAIL;
261	}
262
263	for(i=0;i<pageout_count;i++) {
264		switch (pageout_status[i]) {
265		case VM_PAGER_OK:
266			ms[i]->flags &= ~PG_LAUNDRY;
267			++anyok;
268			break;
269		case VM_PAGER_PEND:
270			ms[i]->flags &= ~PG_LAUNDRY;
271			++anyok;
272			break;
273		case VM_PAGER_BAD:
274			/*
275			 * Page outside of range of object.
276			 * Right now we essentially lose the
277			 * changes by pretending it worked.
278			 */
279			ms[i]->flags &= ~PG_LAUNDRY;
280			ms[i]->flags |= PG_CLEAN;
281			pmap_clear_modify(VM_PAGE_TO_PHYS(ms[i]));
282			break;
283		case VM_PAGER_ERROR:
284		case VM_PAGER_FAIL:
285			/*
286			 * If page couldn't be paged out, then
287			 * reactivate the page so it doesn't
288			 * clog the inactive list.  (We will
289			 * try paging out it again later).
290			 */
291			if (ms[i]->flags & PG_INACTIVE)
292				vm_page_activate(ms[i]);
293			break;
294		case VM_PAGER_AGAIN:
295			break;
296		}
297
298
299		/*
300		 * If the operation is still going, leave
301		 * the page busy to block all other accesses.
302		 * Also, leave the paging in progress
303		 * indicator set so that we don't attempt an
304		 * object collapse.
305		 */
306		if (pageout_status[i] != VM_PAGER_PEND) {
307			PAGE_WAKEUP(ms[i]);
308			if (--object->paging_in_progress == 0)
309				wakeup((caddr_t) object);
310			if ((ms[i]->flags & PG_REFERENCED) ||
311				pmap_is_referenced(VM_PAGE_TO_PHYS(ms[i]))) {
312				pmap_clear_reference(VM_PAGE_TO_PHYS(ms[i]));
313				ms[i]->flags &= ~PG_REFERENCED;
314				if( ms[i]->flags & PG_INACTIVE)
315					vm_page_activate(ms[i]);
316			}
317		}
318	}
319	return anyok;
320}
321
322/*
323 *	vm_pageout_object_deactivate_pages
324 *
325 *	deactivate enough pages to satisfy the inactive target
326 *	requirements or if vm_page_proc_limit is set, then
327 *	deactivate all of the pages in the object and its
328 *	shadows.
329 *
330 *	The object and map must be locked.
331 */
332int
333vm_pageout_object_deactivate_pages(map, object, count)
334	vm_map_t map;
335	vm_object_t object;
336	int count;
337{
338	register vm_page_t	p, next;
339	int rcount;
340	int dcount;
341
342	dcount = 0;
343	if (count == 0)
344		count = 1;
345
346	if (object->shadow) {
347		int scount = count;
348		if( object->shadow->ref_count > 1)
349			scount /= object->shadow->ref_count;
350		if( scount)
351			dcount += vm_pageout_object_deactivate_pages(map, object->shadow, scount);
352	}
353
354	if (object->paging_in_progress)
355		return dcount;
356
357	/*
358	 * scan the objects entire memory queue
359	 */
360	rcount = object->resident_page_count;
361	p = object->memq.tqh_first;
362	while (p && (rcount-- > 0)) {
363		next = p->listq.tqe_next;
364		vm_page_lock_queues();
365		/*
366		 * if a page is active, not wired and is in the processes pmap,
367		 * then deactivate the page.
368		 */
369		if ((p->flags & (PG_ACTIVE|PG_BUSY)) == PG_ACTIVE &&
370			p->wire_count == 0 &&
371			p->hold_count == 0 &&
372			p->busy == 0 &&
373			pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
374			if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) &&
375				(p->flags & PG_REFERENCED) == 0) {
376				p->act_count -= min(p->act_count, ACT_DECLINE);
377				/*
378				 * if the page act_count is zero -- then we deactivate
379				 */
380				if (!p->act_count) {
381					vm_page_deactivate(p);
382					pmap_page_protect(VM_PAGE_TO_PHYS(p),
383						VM_PROT_NONE);
384				/*
385				 * else if on the next go-around we will deactivate the page
386				 * we need to place the page on the end of the queue to age
387				 * the other pages in memory.
388				 */
389				} else {
390					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
391					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
392					TAILQ_REMOVE(&object->memq, p, listq);
393					TAILQ_INSERT_TAIL(&object->memq, p, listq);
394				}
395				/*
396				 * see if we are done yet
397				 */
398				if (p->flags & PG_INACTIVE) {
399					--count;
400					++dcount;
401					if (count <= 0 &&
402						cnt.v_inactive_count > cnt.v_inactive_target) {
403							vm_page_unlock_queues();
404							return dcount;
405					}
406				}
407
408			} else {
409				/*
410				 * Move the page to the bottom of the queue.
411				 */
412				pmap_clear_reference(VM_PAGE_TO_PHYS(p));
413				p->flags &= ~PG_REFERENCED;
414				if (p->act_count < ACT_MAX)
415					p->act_count += ACT_ADVANCE;
416
417				TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
418				TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
419				TAILQ_REMOVE(&object->memq, p, listq);
420				TAILQ_INSERT_TAIL(&object->memq, p, listq);
421			}
422		}
423
424		vm_page_unlock_queues();
425		p = next;
426	}
427	return dcount;
428}
429
430
431/*
432 * deactivate some number of pages in a map, try to do it fairly, but
433 * that is really hard to do.
434 */
435
436void
437vm_pageout_map_deactivate_pages(map, entry, count, freeer)
438	vm_map_t map;
439	vm_map_entry_t entry;
440	int *count;
441	int (*freeer)(vm_map_t, vm_object_t, int);
442{
443	vm_map_t tmpm;
444	vm_map_entry_t tmpe;
445	vm_object_t obj;
446	if (*count <= 0)
447		return;
448	vm_map_reference(map);
449	if (!lock_try_read(&map->lock)) {
450		vm_map_deallocate(map);
451		return;
452	}
453	if (entry == 0) {
454		tmpe = map->header.next;
455		while (tmpe != &map->header && *count > 0) {
456			vm_pageout_map_deactivate_pages(map, tmpe, count, freeer);
457			tmpe = tmpe->next;
458		};
459	} else if (entry->is_sub_map || entry->is_a_map) {
460		tmpm = entry->object.share_map;
461		tmpe = tmpm->header.next;
462		while (tmpe != &tmpm->header && *count > 0) {
463			vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer);
464			tmpe = tmpe->next;
465		};
466	} else if ((obj = entry->object.vm_object) != 0) {
467		*count -= (*freeer)(map, obj, *count);
468	}
469	lock_read_done(&map->lock);
470	vm_map_deallocate(map);
471	return;
472}
473
474/*
475 *	vm_pageout_scan does the dirty work for the pageout daemon.
476 */
477int
478vm_pageout_scan()
479{
480	vm_page_t	m;
481	int		page_shortage, maxscan, maxlaunder;
482	int		pages_freed;
483	int		desired_free;
484	vm_page_t	next;
485	struct proc	*p;
486	vm_object_t	object;
487	int		force_wakeup = 0;
488	int		cache_size, orig_cache_size;
489
490	/*
491	 * We manage the cached memory by attempting to keep it
492	 * at about the desired level.
493	 * We deactivate the pages for the oldest cached objects
494	 * first.  This keeps pages that are "cached" from hogging
495	 * physical memory.
496	 */
497	orig_cache_size = 0;
498	object = vm_object_cached_list.tqh_first;
499
500	/* calculate the total cached size */
501
502	while( object) {
503		orig_cache_size += object->resident_page_count;
504		object = object->cached_list.tqe_next;
505	}
506
507redeact:
508	cache_size = orig_cache_size;
509	object = vm_object_cached_list.tqh_first;
510	vm_object_cache_lock();
511	while ( object && (cnt.v_inactive_count < cnt.v_inactive_target) &&
512		(cache_size >= vm_desired_cache_size)) {
513		vm_object_cache_unlock();
514
515		/*
516		 * if there are no resident pages -- get rid of the object
517		 */
518		if( object->resident_page_count == 0) {
519			if (object != vm_object_lookup(object->pager))
520				panic("vm_object_deactivate: I'm sooo confused.");
521			pager_cache(object, FALSE);
522			goto redeact;
523		} else {
524		/*
525		 * if there are resident pages -- deactivate them
526		 */
527			vm_object_deactivate_pages(object);
528			cache_size -= object->resident_page_count;
529			object = object->cached_list.tqe_next;
530		}
531
532		vm_object_cache_lock();
533	}
534	vm_object_cache_unlock();
535
536morefree:
537	/*
538	 * now swap processes out if we are in low memory conditions
539	 */
540	if (cnt.v_free_count <= cnt.v_free_min) {
541		/*
542		 * swap out inactive processes
543		 */
544		swapout_threads();
545	}
546
547	/*
548	 * scan the processes for exceeding their rlimits or if process
549	 * is swapped out -- deactivate pages
550	 */
551
552	for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
553		vm_offset_t size;
554		int overage;
555		quad_t limit;
556
557		/*
558		 * if this is a system process or if we have already
559		 * looked at this process, skip it.
560		 */
561		if (p->p_flag & (P_SYSTEM|P_WEXIT)) {
562			continue;
563		}
564
565		/*
566		 * if the process is in a non-running type state,
567		 * don't touch it.
568		 */
569		if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
570			continue;
571		}
572
573		/*
574		 * get a limit
575		 */
576		limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
577			    p->p_rlimit[RLIMIT_RSS].rlim_max);
578
579		/*
580		 * let processes that are swapped out really be swapped out
581		 * set the limit to nothing (will force a swap-out.)
582		 */
583		if ((p->p_flag & P_INMEM) == 0)
584			limit = 0;
585
586		size = p->p_vmspace->vm_pmap.pm_stats.resident_count * NBPG;
587		if (limit >= 0 && size >= limit) {
588			overage = (size - limit) / NBPG;
589			vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
590				(vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages);
591		}
592	}
593
594	if (((cnt.v_free_count + cnt.v_inactive_count) >=
595		(cnt.v_inactive_target + cnt.v_free_target)) &&
596		(cnt.v_free_count >= cnt.v_free_target))
597		return force_wakeup;
598
599	pages_freed = 0;
600	desired_free = cnt.v_free_target;
601
602	/*
603	 *	Start scanning the inactive queue for pages we can free.
604	 *	We keep scanning until we have enough free pages or
605	 *	we have scanned through the entire queue.  If we
606	 *	encounter dirty pages, we start cleaning them.
607	 */
608
609	maxlaunder = (cnt.v_free_target - cnt.v_free_count);
610	maxscan = cnt.v_inactive_count;
611rescan1:
612	m = vm_page_queue_inactive.tqh_first;
613	while (m && (maxscan-- > 0) &&
614		(cnt.v_free_count < desired_free) ) {
615		vm_page_t	next;
616
617		next = m->pageq.tqe_next;
618
619		if( (m->flags & PG_INACTIVE) == 0) {
620			printf("vm_pageout_scan: page not inactive?");
621			continue;
622		}
623
624		/*
625		 * activate held pages
626		 */
627		if (m->hold_count != 0) {
628			vm_page_activate(m);
629			m = next;
630			continue;
631		}
632
633		/*
634		 * dont mess with busy pages
635		 */
636		if (m->busy || (m->flags & PG_BUSY)) {
637			m = next;
638			continue;
639		}
640
641		/*
642		 * NOTE: PG_CLEAN doesn't guarantee that the page is clean.
643		 */
644		if (m->flags & PG_CLEAN) {
645			/*
646			 * If we're not low on memory and the page has been reference,
647			 * or if the page has been modified, then reactivate the page.
648			 */
649			if (((cnt.v_free_count > vm_pageout_free_min) &&
650			    (pmap_is_referenced(VM_PAGE_TO_PHYS(m)) || ((m->flags & PG_REFERENCED) != 0))) ||
651			    pmap_is_modified(VM_PAGE_TO_PHYS(m))) {
652				m->flags &= ~PG_REFERENCED;
653				vm_page_activate(m);
654			} else if (!m->act_count) {
655				pmap_page_protect(VM_PAGE_TO_PHYS(m),
656						  VM_PROT_NONE);
657				vm_page_free(m);
658				++cnt.v_dfree;
659				++pages_freed;
660			} else {
661				m->act_count -= min(m->act_count, ACT_DECLINE);
662				TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
663				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
664			}
665		} else if ((m->flags & PG_LAUNDRY) && maxlaunder > 0) {
666			int written;
667			if (pmap_is_referenced(VM_PAGE_TO_PHYS(m)) ||
668				((m->flags & PG_REFERENCED) != 0)) {
669				pmap_clear_reference(VM_PAGE_TO_PHYS(m));
670				vm_page_activate(m);
671				m->flags &= ~PG_REFERENCED;
672				m = next;
673				continue;
674			}
675			/*
676			 *	If a page is dirty, then it is either
677			 *	being washed (but not yet cleaned)
678			 *	or it is still in the laundry.  If it is
679			 *	still in the laundry, then we start the
680			 *	cleaning operation.
681			 */
682
683			written = vm_pageout_clean(m,0);
684			if (written)
685				maxlaunder -= written;
686
687			if (!next)
688				break;
689			/*
690			 * if the next page has been re-activated, start scanning again
691			 */
692			if ((next->flags & PG_INACTIVE) == 0)
693				goto rescan1;
694		} else if ((m->flags & PG_REFERENCED) ||
695				pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
696			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
697			m->flags &= ~PG_REFERENCED;
698			vm_page_activate(m);
699		}
700		m = next;
701	}
702
703	/*
704	 *	Compute the page shortage.  If we are still very low on memory
705	 *	be sure that we will move a minimal amount of pages from active
706	 *	to inactive.
707	 */
708
709	page_shortage = cnt.v_inactive_target -
710	    (cnt.v_free_count + cnt.v_inactive_count);
711
712	if (page_shortage <= 0) {
713		if (pages_freed == 0) {
714			if( cnt.v_free_count < cnt.v_free_min) {
715				page_shortage = cnt.v_free_min - cnt.v_free_count;
716			} else if(((cnt.v_free_count + cnt.v_inactive_count) <
717				(cnt.v_free_min + cnt.v_inactive_target))) {
718				page_shortage = 1;
719			} else {
720				page_shortage = 0;
721			}
722		}
723
724	}
725
726	maxscan = cnt.v_active_count;
727	m = vm_page_queue_active.tqh_first;
728	while (m && maxscan-- && (page_shortage > 0)) {
729
730		next = m->pageq.tqe_next;
731
732		/*
733 		 * Don't deactivate pages that are busy.
734		 */
735		if ((m->busy != 0) ||
736			(m->flags & PG_BUSY) || (m->hold_count != 0)) {
737			m = next;
738			continue;
739		}
740
741		if ((m->flags & PG_REFERENCED) ||
742			pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
743			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
744			m->flags &= ~PG_REFERENCED;
745			if (m->act_count < ACT_MAX)
746				m->act_count += ACT_ADVANCE;
747			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
748			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
749			TAILQ_REMOVE(&m->object->memq, m, listq);
750			TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
751		} else {
752			m->act_count -= min(m->act_count, ACT_DECLINE);
753
754			/*
755			 * if the page act_count is zero -- then we deactivate
756			 */
757			if (!m->act_count) {
758				vm_page_deactivate(m);
759				--page_shortage;
760			/*
761			 * else if on the next go-around we will deactivate the page
762			 * we need to place the page on the end of the queue to age
763			 * the other pages in memory.
764			 */
765			} else {
766				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
767				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
768				TAILQ_REMOVE(&m->object->memq, m, listq);
769				TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
770			}
771		}
772		m = next;
773	}
774
775	/*
776	 * if we have not freed any pages and we are desparate for memory
777	 * then we keep trying until we get some (any) memory.
778	 */
779
780	if( !force_wakeup && (swap_pager_full || !force_wakeup ||
781		(pages_freed == 0 && (cnt.v_free_count < cnt.v_free_min)))){
782		vm_pager_sync();
783		force_wakeup = 1;
784		goto morefree;
785	}
786	vm_page_pagesfreed += pages_freed;
787	return force_wakeup;
788}
789
790/*
791 *	vm_pageout is the high level pageout daemon.
792 */
793void
794vm_pageout()
795{
796	(void) spl0();
797
798	/*
799	 *	Initialize some paging parameters.
800	 */
801
802	cnt.v_free_min = 12;
803	/*
804	 * free_reserved needs to include enough for the largest
805	 * swap pager structures plus enough for any pv_entry
806	 * structs when paging.
807	 */
808	cnt.v_free_reserved = 4 + cnt.v_page_count / 1024;
809	if (cnt.v_free_min < 8)
810		cnt.v_free_min = 8;
811	if (cnt.v_free_min > 32)
812		cnt.v_free_min = 32;
813	vm_pageout_free_min = cnt.v_free_reserved;
814	cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved;
815	cnt.v_inactive_target = cnt.v_free_count / 12;
816	cnt.v_free_min += cnt.v_free_reserved;
817	vm_desired_cache_size = cnt.v_page_count / 3;
818
819        /* XXX does not really belong here */
820	if (vm_page_max_wired == 0)
821		vm_page_max_wired = cnt.v_free_count / 3;
822
823
824	(void) swap_pager_alloc(0, 0, 0, 0);
825
826	/*
827	 *	The pageout daemon is never done, so loop
828	 *	forever.
829	 */
830	while (TRUE) {
831		int force_wakeup;
832/*
833		cnt.v_free_min = 12 + averunnable.ldavg[0] / 1024;
834		cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved;
835		cnt.v_inactive_target = cnt.v_free_target*2;
836*/
837
838		tsleep((caddr_t) &vm_pages_needed, PVM, "psleep", 0);
839
840		vm_pager_sync();
841		/*
842		 * The force wakeup hack added to eliminate delays and potiential
843		 * deadlock.  It was possible for the page daemon to indefintely
844		 * postpone waking up a process that it might be waiting for memory
845		 * on.  The putmulti stuff seems to have aggravated the situation.
846		 */
847		force_wakeup = vm_pageout_scan();
848		vm_pager_sync();
849		if( force_wakeup)
850			wakeup( (caddr_t) &cnt.v_free_count);
851		cnt.v_scan++;
852		wakeup((caddr_t) kmem_map);
853	}
854}
855
856