Home | History | Annotate | Line # | Download | only in uvm
uvm_pdpolicy_clock.c revision 1.21
      1  1.21       ad /*	$NetBSD: uvm_pdpolicy_clock.c,v 1.21 2019/12/21 13:00:25 ad Exp $	*/
      2   1.2     yamt /*	NetBSD: uvm_pdaemon.c,v 1.72 2006/01/05 10:47:33 yamt Exp $	*/
      3   1.2     yamt 
      4   1.2     yamt /*
      5   1.2     yamt  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      6   1.2     yamt  * Copyright (c) 1991, 1993, The Regents of the University of California.
      7   1.2     yamt  *
      8   1.2     yamt  * All rights reserved.
      9   1.2     yamt  *
     10   1.2     yamt  * This code is derived from software contributed to Berkeley by
     11   1.2     yamt  * The Mach Operating System project at Carnegie-Mellon University.
     12   1.2     yamt  *
     13   1.2     yamt  * Redistribution and use in source and binary forms, with or without
     14   1.2     yamt  * modification, are permitted provided that the following conditions
     15   1.2     yamt  * are met:
     16   1.2     yamt  * 1. Redistributions of source code must retain the above copyright
     17   1.2     yamt  *    notice, this list of conditions and the following disclaimer.
     18   1.2     yamt  * 2. Redistributions in binary form must reproduce the above copyright
     19   1.2     yamt  *    notice, this list of conditions and the following disclaimer in the
     20   1.2     yamt  *    documentation and/or other materials provided with the distribution.
     21  1.13    chuck  * 3. Neither the name of the University nor the names of its contributors
     22   1.2     yamt  *    may be used to endorse or promote products derived from this software
     23   1.2     yamt  *    without specific prior written permission.
     24   1.2     yamt  *
     25   1.2     yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.2     yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.2     yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.2     yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.2     yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.2     yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.2     yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.2     yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.2     yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.2     yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.2     yamt  * SUCH DAMAGE.
     36   1.2     yamt  *
     37   1.2     yamt  *	@(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
     38   1.2     yamt  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
     39   1.2     yamt  *
     40   1.2     yamt  *
     41   1.2     yamt  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     42   1.2     yamt  * All rights reserved.
     43   1.2     yamt  *
     44   1.2     yamt  * Permission to use, copy, modify and distribute this software and
     45   1.2     yamt  * its documentation is hereby granted, provided that both the copyright
     46   1.2     yamt  * notice and this permission notice appear in all copies of the
     47   1.2     yamt  * software, derivative works or modified versions, and any portions
     48   1.2     yamt  * thereof, and that both notices appear in supporting documentation.
     49   1.2     yamt  *
     50   1.2     yamt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51   1.2     yamt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     52   1.2     yamt  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53   1.2     yamt  *
     54   1.2     yamt  * Carnegie Mellon requests users of this software to return to
     55   1.2     yamt  *
     56   1.2     yamt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57   1.2     yamt  *  School of Computer Science
     58   1.2     yamt  *  Carnegie Mellon University
     59   1.2     yamt  *  Pittsburgh PA 15213-3890
     60   1.2     yamt  *
     61   1.2     yamt  * any improvements or extensions that they make and grant Carnegie the
     62   1.2     yamt  * rights to redistribute these changes.
     63   1.2     yamt  */
     64   1.2     yamt 
     65   1.2     yamt #if defined(PDSIM)
     66   1.2     yamt 
     67   1.2     yamt #include "pdsim.h"
     68   1.2     yamt 
     69   1.2     yamt #else /* defined(PDSIM) */
     70   1.2     yamt 
     71   1.2     yamt #include <sys/cdefs.h>
     72  1.21       ad __KERNEL_RCSID(0, "$NetBSD: uvm_pdpolicy_clock.c,v 1.21 2019/12/21 13:00:25 ad Exp $");
     73   1.2     yamt 
     74   1.2     yamt #include <sys/param.h>
     75   1.2     yamt #include <sys/proc.h>
     76   1.2     yamt #include <sys/systm.h>
     77   1.2     yamt #include <sys/kernel.h>
     78   1.2     yamt 
     79   1.2     yamt #include <uvm/uvm.h>
     80   1.2     yamt #include <uvm/uvm_pdpolicy.h>
     81   1.2     yamt #include <uvm/uvm_pdpolicy_impl.h>
     82  1.18       ad #include <uvm/uvm_stat.h>
     83   1.2     yamt 
     84   1.2     yamt #endif /* defined(PDSIM) */
     85   1.2     yamt 
     86  1.19       ad #define	PQ_TIME		0xfffffffc	/* time of last activation */
     87  1.19       ad #define PQ_INACTIVE	0x00000001	/* page is in inactive list */
     88  1.19       ad #define PQ_ACTIVE	0x00000002	/* page is in active list */
     89   1.2     yamt 
     90   1.2     yamt #if !defined(CLOCK_INACTIVEPCT)
     91   1.2     yamt #define	CLOCK_INACTIVEPCT	33
     92   1.2     yamt #endif /* !defined(CLOCK_INACTIVEPCT) */
     93   1.2     yamt 
     94   1.2     yamt struct uvmpdpol_globalstate {
     95  1.18       ad 	kmutex_t lock;			/* lock on state */
     96  1.18       ad 					/* <= compiler pads here */
     97  1.18       ad 	struct pglist s_activeq		/* allocated pages, in use */
     98  1.18       ad 	    __aligned(COHERENCY_UNIT);
     99   1.2     yamt 	struct pglist s_inactiveq;	/* pages between the clock hands */
    100   1.2     yamt 	int s_active;
    101   1.2     yamt 	int s_inactive;
    102   1.2     yamt 	int s_inactarg;
    103   1.2     yamt 	struct uvm_pctparam s_anonmin;
    104   1.2     yamt 	struct uvm_pctparam s_filemin;
    105   1.2     yamt 	struct uvm_pctparam s_execmin;
    106   1.2     yamt 	struct uvm_pctparam s_anonmax;
    107   1.2     yamt 	struct uvm_pctparam s_filemax;
    108   1.2     yamt 	struct uvm_pctparam s_execmax;
    109   1.2     yamt 	struct uvm_pctparam s_inactivepct;
    110   1.2     yamt };
    111   1.2     yamt 
    112   1.2     yamt struct uvmpdpol_scanstate {
    113   1.7  thorpej 	bool ss_first;
    114   1.7  thorpej 	bool ss_anonreact, ss_filereact, ss_execreact;
    115   1.2     yamt 	struct vm_page *ss_nextpg;
    116   1.2     yamt };
    117   1.2     yamt 
    118  1.18       ad static void	uvmpdpol_pageactivate_locked(struct vm_page *);
    119  1.18       ad static void	uvmpdpol_pagedeactivate_locked(struct vm_page *);
    120  1.18       ad static void	uvmpdpol_pagedequeue_locked(struct vm_page *);
    121  1.18       ad 
    122  1.18       ad static struct uvmpdpol_globalstate pdpol_state __cacheline_aligned;
    123   1.2     yamt static struct uvmpdpol_scanstate pdpol_scanstate;
    124   1.2     yamt 
    125   1.2     yamt PDPOL_EVCNT_DEFINE(reactexec)
    126   1.2     yamt PDPOL_EVCNT_DEFINE(reactfile)
    127   1.2     yamt PDPOL_EVCNT_DEFINE(reactanon)
    128   1.2     yamt 
    129   1.2     yamt static void
    130   1.2     yamt clock_tune(void)
    131   1.2     yamt {
    132   1.2     yamt 	struct uvmpdpol_globalstate *s = &pdpol_state;
    133   1.2     yamt 
    134   1.2     yamt 	s->s_inactarg = UVM_PCTPARAM_APPLY(&s->s_inactivepct,
    135   1.2     yamt 	    s->s_active + s->s_inactive);
    136   1.2     yamt 	if (s->s_inactarg <= uvmexp.freetarg) {
    137   1.2     yamt 		s->s_inactarg = uvmexp.freetarg + 1;
    138   1.2     yamt 	}
    139   1.2     yamt }
    140   1.2     yamt 
    141   1.2     yamt void
    142   1.2     yamt uvmpdpol_scaninit(void)
    143   1.2     yamt {
    144   1.2     yamt 	struct uvmpdpol_globalstate *s = &pdpol_state;
    145   1.2     yamt 	struct uvmpdpol_scanstate *ss = &pdpol_scanstate;
    146   1.2     yamt 	int t;
    147   1.7  thorpej 	bool anonunder, fileunder, execunder;
    148   1.7  thorpej 	bool anonover, fileover, execover;
    149   1.7  thorpej 	bool anonreact, filereact, execreact;
    150  1.20       ad 	int64_t freepg, anonpg, filepg, execpg;
    151   1.2     yamt 
    152   1.2     yamt 	/*
    153   1.2     yamt 	 * decide which types of pages we want to reactivate instead of freeing
    154   1.2     yamt 	 * to keep usage within the minimum and maximum usage limits.
    155   1.2     yamt 	 */
    156   1.2     yamt 
    157  1.20       ad 	cpu_count_sync_all();
    158  1.21       ad 	freepg = uvm_free();
    159  1.20       ad 	anonpg = cpu_count_get(CPU_COUNT_ANONPAGES);
    160  1.20       ad 	filepg = cpu_count_get(CPU_COUNT_FILEPAGES);
    161  1.20       ad 	execpg = cpu_count_get(CPU_COUNT_EXECPAGES);
    162  1.20       ad 
    163  1.18       ad 	mutex_enter(&s->lock);
    164  1.20       ad 	t = s->s_active + s->s_inactive + freepg;
    165  1.20       ad 	anonunder = anonpg <= UVM_PCTPARAM_APPLY(&s->s_anonmin, t);
    166  1.20       ad 	fileunder = filepg <= UVM_PCTPARAM_APPLY(&s->s_filemin, t);
    167  1.20       ad 	execunder = execpg <= UVM_PCTPARAM_APPLY(&s->s_execmin, t);
    168  1.20       ad 	anonover = anonpg > UVM_PCTPARAM_APPLY(&s->s_anonmax, t);
    169  1.20       ad 	fileover = filepg > UVM_PCTPARAM_APPLY(&s->s_filemax, t);
    170  1.20       ad 	execover = execpg > UVM_PCTPARAM_APPLY(&s->s_execmax, t);
    171   1.2     yamt 	anonreact = anonunder || (!anonover && (fileover || execover));
    172   1.2     yamt 	filereact = fileunder || (!fileover && (anonover || execover));
    173   1.2     yamt 	execreact = execunder || (!execover && (anonover || fileover));
    174   1.2     yamt 	if (filereact && execreact && (anonreact || uvm_swapisfull())) {
    175   1.8  thorpej 		anonreact = filereact = execreact = false;
    176   1.2     yamt 	}
    177   1.2     yamt 	ss->ss_anonreact = anonreact;
    178   1.2     yamt 	ss->ss_filereact = filereact;
    179   1.2     yamt 	ss->ss_execreact = execreact;
    180   1.2     yamt 
    181   1.8  thorpej 	ss->ss_first = true;
    182  1.18       ad 	mutex_exit(&s->lock);
    183   1.2     yamt }
    184   1.2     yamt 
    185   1.2     yamt struct vm_page *
    186  1.18       ad uvmpdpol_selectvictim(kmutex_t **plock)
    187   1.2     yamt {
    188  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    189   1.2     yamt 	struct uvmpdpol_scanstate *ss = &pdpol_scanstate;
    190   1.2     yamt 	struct vm_page *pg;
    191  1.14    rmind 	kmutex_t *lock;
    192   1.2     yamt 
    193  1.18       ad 	mutex_enter(&s->lock);
    194   1.2     yamt 	while (/* CONSTCOND */ 1) {
    195   1.2     yamt 		struct vm_anon *anon;
    196   1.2     yamt 		struct uvm_object *uobj;
    197   1.2     yamt 
    198   1.2     yamt 		if (ss->ss_first) {
    199   1.2     yamt 			pg = TAILQ_FIRST(&pdpol_state.s_inactiveq);
    200   1.8  thorpej 			ss->ss_first = false;
    201   1.2     yamt 		} else {
    202   1.2     yamt 			pg = ss->ss_nextpg;
    203   1.2     yamt 			if (pg != NULL && (pg->pqflags & PQ_INACTIVE) == 0) {
    204   1.2     yamt 				pg = TAILQ_FIRST(&pdpol_state.s_inactiveq);
    205   1.2     yamt 			}
    206   1.2     yamt 		}
    207   1.2     yamt 		if (pg == NULL) {
    208   1.2     yamt 			break;
    209   1.2     yamt 		}
    210  1.12       ad 		ss->ss_nextpg = TAILQ_NEXT(pg, pageq.queue);
    211  1.18       ad 		KASSERT(pg->wire_count == 0);
    212   1.2     yamt 
    213   1.2     yamt 		uvmexp.pdscans++;
    214   1.2     yamt 
    215   1.2     yamt 		/*
    216  1.18       ad 		 * acquire interlock to stablize page identity.
    217  1.18       ad 		 * if we have caught the page in a state of flux
    218  1.18       ad 		 * and it should be dequeued, do it now and then
    219  1.18       ad 		 * move on to the next.
    220   1.2     yamt 		 */
    221  1.18       ad 		mutex_enter(&pg->interlock);
    222  1.18       ad 	        if ((pg->uobject == NULL && pg->uanon == NULL) ||
    223  1.18       ad 	            pg->wire_count > 0) {
    224  1.18       ad 	            	mutex_exit(&pg->interlock);
    225  1.18       ad 	            	uvmpdpol_pagedequeue_locked(pg);
    226  1.18       ad 	            	continue;
    227   1.2     yamt 		}
    228   1.2     yamt 
    229   1.2     yamt 		/*
    230   1.2     yamt 		 * enforce the minimum thresholds on different
    231   1.2     yamt 		 * types of memory usage.  if reusing the current
    232   1.2     yamt 		 * page would reduce that type of usage below its
    233   1.2     yamt 		 * minimum, reactivate the page instead and move
    234   1.2     yamt 		 * on to the next page.
    235   1.2     yamt 		 */
    236  1.18       ad 		anon = pg->uanon;
    237  1.18       ad 		uobj = pg->uobject;
    238   1.2     yamt 		if (uobj && UVM_OBJ_IS_VTEXT(uobj) && ss->ss_execreact) {
    239  1.18       ad 			mutex_exit(&pg->interlock);
    240  1.18       ad 			uvmpdpol_pageactivate_locked(pg);
    241   1.2     yamt 			PDPOL_EVCNT_INCR(reactexec);
    242   1.2     yamt 			continue;
    243   1.2     yamt 		}
    244   1.2     yamt 		if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
    245   1.2     yamt 		    !UVM_OBJ_IS_VTEXT(uobj) && ss->ss_filereact) {
    246  1.18       ad 			mutex_exit(&pg->interlock);
    247  1.18       ad 			uvmpdpol_pageactivate_locked(pg);
    248   1.2     yamt 			PDPOL_EVCNT_INCR(reactfile);
    249   1.2     yamt 			continue;
    250   1.2     yamt 		}
    251   1.2     yamt 		if ((anon || UVM_OBJ_IS_AOBJ(uobj)) && ss->ss_anonreact) {
    252  1.18       ad 			mutex_exit(&pg->interlock);
    253  1.18       ad 			uvmpdpol_pageactivate_locked(pg);
    254   1.2     yamt 			PDPOL_EVCNT_INCR(reactanon);
    255   1.2     yamt 			continue;
    256   1.2     yamt 		}
    257   1.2     yamt 
    258  1.18       ad 		/*
    259  1.18       ad 		 * try to lock the object that owns the page.
    260  1.18       ad 		 *
    261  1.18       ad 		 * with the page interlock held, we can drop s->lock, which
    262  1.18       ad 		 * could otherwise serve as a barrier to us getting the
    263  1.18       ad 		 * object locked, because the owner of the object's lock may
    264  1.18       ad 		 * be blocked on s->lock (i.e. a deadlock).
    265  1.18       ad 		 *
    266  1.18       ad 		 * whatever happens, uvmpd_trylockowner() will release the
    267  1.18       ad 		 * interlock.  with the interlock dropped we can then
    268  1.18       ad 		 * re-acquire our own lock.  the order is:
    269  1.18       ad 		 *
    270  1.18       ad 		 *	object -> pdpol -> interlock.
    271  1.18       ad 	         */
    272  1.18       ad 	        mutex_exit(&s->lock);
    273  1.18       ad         	lock = uvmpd_trylockowner(pg);
    274  1.18       ad         	/* pg->interlock now released */
    275  1.18       ad         	mutex_enter(&s->lock);
    276  1.18       ad 		if (lock == NULL) {
    277  1.18       ad 			/* didn't get it - try the next page. */
    278  1.18       ad 			continue;
    279  1.18       ad 		}
    280  1.18       ad 
    281  1.18       ad 		/*
    282  1.18       ad 		 * move referenced pages back to active queue and skip to
    283  1.18       ad 		 * next page.
    284  1.18       ad 		 */
    285  1.18       ad 		if (pmap_is_referenced(pg)) {
    286  1.18       ad 			uvmpdpol_pageactivate_locked(pg);
    287  1.18       ad 			uvmexp.pdreact++;
    288  1.18       ad 			mutex_exit(lock);
    289  1.18       ad 			continue;
    290  1.18       ad 		}
    291  1.18       ad 
    292  1.18       ad 		/* we have a potential victim. */
    293  1.18       ad 		*plock = lock;
    294   1.2     yamt 		break;
    295   1.2     yamt 	}
    296  1.18       ad 	mutex_exit(&s->lock);
    297   1.2     yamt 	return pg;
    298   1.2     yamt }
    299   1.2     yamt 
    300   1.2     yamt void
    301   1.2     yamt uvmpdpol_balancequeue(int swap_shortage)
    302   1.2     yamt {
    303  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    304   1.2     yamt 	int inactive_shortage;
    305   1.2     yamt 	struct vm_page *p, *nextpg;
    306  1.14    rmind 	kmutex_t *lock;
    307   1.2     yamt 
    308   1.2     yamt 	/*
    309   1.2     yamt 	 * we have done the scan to get free pages.   now we work on meeting
    310   1.2     yamt 	 * our inactive target.
    311   1.2     yamt 	 */
    312   1.2     yamt 
    313  1.18       ad 	mutex_enter(&s->lock);
    314   1.2     yamt 	inactive_shortage = pdpol_state.s_inactarg - pdpol_state.s_inactive;
    315   1.2     yamt 	for (p = TAILQ_FIRST(&pdpol_state.s_activeq);
    316   1.2     yamt 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
    317   1.2     yamt 	     p = nextpg) {
    318  1.12       ad 		nextpg = TAILQ_NEXT(p, pageq.queue);
    319   1.2     yamt 
    320   1.2     yamt 		/*
    321   1.2     yamt 		 * if there's a shortage of swap slots, try to free it.
    322   1.2     yamt 		 */
    323   1.2     yamt 
    324  1.18       ad 		if (swap_shortage > 0 && (p->flags & PG_SWAPBACKED) != 0) {
    325  1.18       ad 			mutex_enter(&p->interlock);
    326  1.18       ad 			mutex_exit(&s->lock);
    327   1.2     yamt 			if (uvmpd_trydropswap(p)) {
    328   1.2     yamt 				swap_shortage--;
    329   1.2     yamt 			}
    330  1.18       ad 			/* p->interlock now released */
    331  1.18       ad 			mutex_enter(&s->lock);
    332   1.2     yamt 		}
    333   1.2     yamt 
    334   1.2     yamt 		/*
    335   1.2     yamt 		 * if there's a shortage of inactive pages, deactivate.
    336   1.2     yamt 		 */
    337   1.2     yamt 
    338  1.14    rmind 		if (inactive_shortage <= 0) {
    339  1.14    rmind 			continue;
    340  1.14    rmind 		}
    341  1.14    rmind 
    342  1.18       ad 		/*
    343  1.18       ad 		 * acquire interlock to stablize page identity.
    344  1.18       ad 		 * if we have caught the page in a state of flux
    345  1.18       ad 		 * and it should be dequeued, do it now and then
    346  1.18       ad 		 * move on to the next.
    347  1.18       ad 		 */
    348  1.18       ad 		mutex_enter(&p->interlock);
    349  1.18       ad 	        if ((p->uobject == NULL && p->uanon == NULL) ||
    350  1.18       ad 	            p->wire_count > 0) {
    351  1.18       ad 	            	mutex_exit(&p->interlock);
    352  1.18       ad 	            	uvmpdpol_pagedequeue_locked(p);
    353  1.18       ad 	            	continue;
    354  1.18       ad 		}
    355  1.18       ad 		mutex_exit(&s->lock);
    356  1.14    rmind 		lock = uvmpd_trylockowner(p);
    357  1.18       ad 		/* p->interlock now released */
    358  1.18       ad 		mutex_enter(&s->lock);
    359  1.14    rmind 		if (lock != NULL) {
    360  1.18       ad 			uvmpdpol_pagedeactivate_locked(p);
    361   1.2     yamt 			uvmexp.pddeact++;
    362   1.2     yamt 			inactive_shortage--;
    363  1.14    rmind 			mutex_exit(lock);
    364   1.2     yamt 		}
    365   1.2     yamt 	}
    366  1.18       ad 	mutex_exit(&s->lock);
    367   1.2     yamt }
    368   1.2     yamt 
    369  1.18       ad static void
    370  1.18       ad uvmpdpol_pagedeactivate_locked(struct vm_page *pg)
    371   1.2     yamt {
    372   1.2     yamt 
    373  1.14    rmind 	KASSERT(uvm_page_locked_p(pg));
    374  1.14    rmind 
    375   1.2     yamt 	if (pg->pqflags & PQ_ACTIVE) {
    376  1.12       ad 		TAILQ_REMOVE(&pdpol_state.s_activeq, pg, pageq.queue);
    377  1.18       ad 		pg->pqflags &= ~(PQ_ACTIVE | PQ_TIME);
    378   1.2     yamt 		KASSERT(pdpol_state.s_active > 0);
    379   1.2     yamt 		pdpol_state.s_active--;
    380   1.2     yamt 	}
    381   1.2     yamt 	if ((pg->pqflags & PQ_INACTIVE) == 0) {
    382   1.2     yamt 		KASSERT(pg->wire_count == 0);
    383  1.10     yamt 		pmap_clear_reference(pg);
    384  1.12       ad 		TAILQ_INSERT_TAIL(&pdpol_state.s_inactiveq, pg, pageq.queue);
    385   1.2     yamt 		pg->pqflags |= PQ_INACTIVE;
    386   1.2     yamt 		pdpol_state.s_inactive++;
    387   1.2     yamt 	}
    388   1.2     yamt }
    389   1.2     yamt 
    390   1.2     yamt void
    391  1.18       ad uvmpdpol_pagedeactivate(struct vm_page *pg)
    392  1.18       ad {
    393  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    394  1.18       ad 
    395  1.18       ad 	mutex_enter(&s->lock);
    396  1.18       ad 	uvmpdpol_pagedeactivate_locked(pg);
    397  1.18       ad 	mutex_exit(&s->lock);
    398  1.18       ad }
    399  1.18       ad 
    400  1.18       ad static void
    401  1.18       ad uvmpdpol_pageactivate_locked(struct vm_page *pg)
    402   1.2     yamt {
    403   1.2     yamt 
    404  1.18       ad 	uvmpdpol_pagedequeue_locked(pg);
    405  1.12       ad 	TAILQ_INSERT_TAIL(&pdpol_state.s_activeq, pg, pageq.queue);
    406  1.18       ad 	pg->pqflags = PQ_ACTIVE | (hardclock_ticks & PQ_TIME);
    407   1.2     yamt 	pdpol_state.s_active++;
    408   1.2     yamt }
    409   1.2     yamt 
    410   1.2     yamt void
    411  1.18       ad uvmpdpol_pageactivate(struct vm_page *pg)
    412  1.18       ad {
    413  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    414  1.18       ad 
    415  1.18       ad 	/* Safety: PQ_ACTIVE clear also tells us if it is not enqueued. */
    416  1.18       ad 	if ((pg->pqflags & PQ_ACTIVE) == 0 ||
    417  1.19       ad 	    ((hardclock_ticks & PQ_TIME) - (pg->pqflags & PQ_TIME)) >= hz) {
    418  1.18       ad 		mutex_enter(&s->lock);
    419  1.18       ad 		uvmpdpol_pageactivate_locked(pg);
    420  1.18       ad 		mutex_exit(&s->lock);
    421  1.18       ad 	}
    422  1.18       ad }
    423  1.18       ad 
    424  1.18       ad static void
    425  1.18       ad uvmpdpol_pagedequeue_locked(struct vm_page *pg)
    426   1.2     yamt {
    427   1.2     yamt 
    428   1.2     yamt 	if (pg->pqflags & PQ_ACTIVE) {
    429  1.12       ad 		TAILQ_REMOVE(&pdpol_state.s_activeq, pg, pageq.queue);
    430  1.18       ad 		pg->pqflags &= ~(PQ_ACTIVE | PQ_TIME);
    431   1.2     yamt 		KASSERT(pdpol_state.s_active > 0);
    432   1.2     yamt 		pdpol_state.s_active--;
    433   1.2     yamt 	} else if (pg->pqflags & PQ_INACTIVE) {
    434  1.12       ad 		TAILQ_REMOVE(&pdpol_state.s_inactiveq, pg, pageq.queue);
    435   1.2     yamt 		pg->pqflags &= ~PQ_INACTIVE;
    436   1.2     yamt 		KASSERT(pdpol_state.s_inactive > 0);
    437   1.2     yamt 		pdpol_state.s_inactive--;
    438   1.2     yamt 	}
    439   1.2     yamt }
    440   1.2     yamt 
    441   1.2     yamt void
    442  1.18       ad uvmpdpol_pagedequeue(struct vm_page *pg)
    443  1.18       ad {
    444  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    445  1.18       ad 
    446  1.18       ad 	mutex_enter(&s->lock);
    447  1.18       ad 	uvmpdpol_pagedequeue_locked(pg);
    448  1.18       ad 	mutex_exit(&s->lock);
    449  1.18       ad }
    450  1.18       ad 
    451  1.18       ad void
    452   1.2     yamt uvmpdpol_pageenqueue(struct vm_page *pg)
    453   1.2     yamt {
    454  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    455   1.2     yamt 
    456  1.18       ad 	mutex_enter(&s->lock);
    457  1.18       ad 	uvmpdpol_pageactivate_locked(pg);
    458  1.18       ad 	mutex_exit(&s->lock);
    459   1.2     yamt }
    460   1.2     yamt 
    461   1.2     yamt void
    462   1.5     yamt uvmpdpol_anfree(struct vm_anon *an)
    463   1.2     yamt {
    464   1.2     yamt }
    465   1.2     yamt 
    466   1.7  thorpej bool
    467   1.2     yamt uvmpdpol_pageisqueued_p(struct vm_page *pg)
    468   1.2     yamt {
    469   1.2     yamt 
    470  1.18       ad 	/* Safe to test unlocked due to page life-cycle. */
    471   1.2     yamt 	return (pg->pqflags & (PQ_ACTIVE | PQ_INACTIVE)) != 0;
    472   1.2     yamt }
    473   1.2     yamt 
    474   1.2     yamt void
    475   1.2     yamt uvmpdpol_estimatepageable(int *active, int *inactive)
    476   1.2     yamt {
    477  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    478   1.2     yamt 
    479  1.18       ad 	mutex_enter(&s->lock);
    480   1.2     yamt 	if (active) {
    481   1.2     yamt 		*active = pdpol_state.s_active;
    482   1.2     yamt 	}
    483   1.2     yamt 	if (inactive) {
    484   1.2     yamt 		*inactive = pdpol_state.s_inactive;
    485   1.2     yamt 	}
    486  1.18       ad 	mutex_exit(&s->lock);
    487   1.2     yamt }
    488   1.2     yamt 
    489   1.2     yamt #if !defined(PDSIM)
    490   1.2     yamt static int
    491   1.2     yamt min_check(struct uvm_pctparam *pct, int t)
    492   1.2     yamt {
    493   1.2     yamt 	struct uvmpdpol_globalstate *s = &pdpol_state;
    494   1.2     yamt 	int total = t;
    495   1.2     yamt 
    496   1.2     yamt 	if (pct != &s->s_anonmin) {
    497   1.2     yamt 		total += uvm_pctparam_get(&s->s_anonmin);
    498   1.2     yamt 	}
    499   1.2     yamt 	if (pct != &s->s_filemin) {
    500   1.2     yamt 		total += uvm_pctparam_get(&s->s_filemin);
    501   1.2     yamt 	}
    502   1.2     yamt 	if (pct != &s->s_execmin) {
    503   1.2     yamt 		total += uvm_pctparam_get(&s->s_execmin);
    504   1.2     yamt 	}
    505   1.2     yamt 	if (total > 95) {
    506   1.2     yamt 		return EINVAL;
    507   1.2     yamt 	}
    508   1.2     yamt 	return 0;
    509   1.2     yamt }
    510   1.2     yamt #endif /* !defined(PDSIM) */
    511   1.2     yamt 
    512   1.2     yamt void
    513   1.2     yamt uvmpdpol_init(void)
    514   1.2     yamt {
    515   1.2     yamt 	struct uvmpdpol_globalstate *s = &pdpol_state;
    516   1.2     yamt 
    517  1.18       ad 	mutex_init(&s->lock, MUTEX_DEFAULT, IPL_NONE);
    518   1.2     yamt 	TAILQ_INIT(&s->s_activeq);
    519   1.2     yamt 	TAILQ_INIT(&s->s_inactiveq);
    520   1.2     yamt 	uvm_pctparam_init(&s->s_inactivepct, CLOCK_INACTIVEPCT, NULL);
    521   1.2     yamt 	uvm_pctparam_init(&s->s_anonmin, 10, min_check);
    522   1.2     yamt 	uvm_pctparam_init(&s->s_filemin, 10, min_check);
    523   1.2     yamt 	uvm_pctparam_init(&s->s_execmin,  5, min_check);
    524   1.2     yamt 	uvm_pctparam_init(&s->s_anonmax, 80, NULL);
    525   1.2     yamt 	uvm_pctparam_init(&s->s_filemax, 50, NULL);
    526   1.2     yamt 	uvm_pctparam_init(&s->s_execmax, 30, NULL);
    527   1.2     yamt }
    528   1.2     yamt 
    529   1.2     yamt void
    530   1.2     yamt uvmpdpol_reinit(void)
    531   1.2     yamt {
    532   1.2     yamt }
    533   1.2     yamt 
    534   1.7  thorpej bool
    535   1.2     yamt uvmpdpol_needsscan_p(void)
    536   1.2     yamt {
    537   1.2     yamt 
    538  1.18       ad 	/* This must be an unlocked check: can be called from interrupt. */
    539  1.17     para 	return pdpol_state.s_inactive < pdpol_state.s_inactarg;
    540   1.2     yamt }
    541   1.2     yamt 
    542   1.2     yamt void
    543   1.2     yamt uvmpdpol_tune(void)
    544   1.2     yamt {
    545  1.18       ad 	struct uvmpdpol_globalstate *s = &pdpol_state;
    546   1.2     yamt 
    547  1.18       ad 	mutex_enter(&s->lock);
    548   1.2     yamt 	clock_tune();
    549  1.18       ad 	mutex_exit(&s->lock);
    550   1.2     yamt }
    551   1.2     yamt 
    552   1.2     yamt #if !defined(PDSIM)
    553   1.2     yamt 
    554   1.2     yamt #include <sys/sysctl.h>	/* XXX SYSCTL_DESCR */
    555   1.2     yamt 
    556   1.2     yamt void
    557   1.2     yamt uvmpdpol_sysctlsetup(void)
    558   1.2     yamt {
    559   1.2     yamt 	struct uvmpdpol_globalstate *s = &pdpol_state;
    560   1.2     yamt 
    561   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_anonmin, "anonmin",
    562   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory reserved "
    563   1.2     yamt 	    "for anonymous application data"));
    564   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_filemin, "filemin",
    565   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory reserved "
    566  1.11   martin 	    "for cached file data"));
    567   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_execmin, "execmin",
    568   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory reserved "
    569  1.11   martin 	    "for cached executable data"));
    570   1.2     yamt 
    571   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_anonmax, "anonmax",
    572   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory which will "
    573   1.2     yamt 	    "be reclaimed from other usage for "
    574   1.2     yamt 	    "anonymous application data"));
    575   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_filemax, "filemax",
    576   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory which will "
    577   1.2     yamt 	    "be reclaimed from other usage for cached "
    578   1.2     yamt 	    "file data"));
    579   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_execmax, "execmax",
    580   1.2     yamt 	    SYSCTL_DESCR("Percentage of physical memory which will "
    581   1.2     yamt 	    "be reclaimed from other usage for cached "
    582   1.2     yamt 	    "executable data"));
    583   1.2     yamt 
    584   1.2     yamt 	uvm_pctparam_createsysctlnode(&s->s_inactivepct, "inactivepct",
    585   1.2     yamt 	    SYSCTL_DESCR("Percentage of inactive queue of "
    586   1.2     yamt 	    "the entire (active + inactive) queue"));
    587   1.2     yamt }
    588   1.2     yamt 
    589   1.2     yamt #endif /* !defined(PDSIM) */
    590   1.2     yamt 
    591   1.2     yamt #if defined(PDSIM)
    592   1.2     yamt void
    593   1.2     yamt pdsim_dump(const char *id)
    594   1.2     yamt {
    595   1.2     yamt #if defined(DEBUG)
    596   1.2     yamt 	/* XXX */
    597   1.2     yamt #endif /* defined(DEBUG) */
    598   1.2     yamt }
    599   1.2     yamt #endif /* defined(PDSIM) */
    600