2001-07-05 01:32:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 John Dyson
|
|
|
|
* Copyright (c) 2001 Matt Dillon
|
|
|
|
*
|
|
|
|
* All rights reserved. Terms for use and redistribution
|
|
|
|
* are covered by the BSD Copyright as found in /usr/src/COPYRIGHT.
|
|
|
|
*
|
|
|
|
* from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
|
|
|
|
* Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
|
|
|
|
*/
|
|
|
|
|
2003-06-11 23:50:51 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-07-05 01:32:42 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-08-25 05:00:44 +00:00
|
|
|
#include <sys/kernel.h>
|
2001-07-05 01:32:42 +00:00
|
|
|
#include <sys/proc.h>
|
2001-08-25 05:00:44 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2001-07-05 01:32:42 +00:00
|
|
|
#include <sys/vmmeter.h>
|
2001-08-25 05:00:44 +00:00
|
|
|
#include <sys/lock.h>
|
2001-07-05 01:32:42 +00:00
|
|
|
#include <sys/mutex.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/sched.h>
|
2001-07-05 01:32:42 +00:00
|
|
|
#include <sys/sysctl.h>
|
2001-08-25 05:00:44 +00:00
|
|
|
#include <sys/kthread.h>
|
2001-07-05 01:32:42 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
|
|
|
|
SYSCTL_DECL(_vm_stats_misc);
|
|
|
|
|
|
|
|
static int cnt_prezero;
|
|
|
|
SYSCTL_INT(_vm_stats_misc, OID_AUTO,
|
|
|
|
cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
|
|
|
|
|
2002-07-08 04:24:26 +00:00
|
|
|
static int idlezero_enable = 1;
|
2001-08-25 05:00:44 +00:00
|
|
|
SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
|
|
|
|
TUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
|
|
|
|
|
|
|
|
static int idlezero_maxrun = 16;
|
|
|
|
SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
|
|
|
|
TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
|
|
|
|
|
2001-07-05 01:32:42 +00:00
|
|
|
/*
|
|
|
|
* Implement the pre-zeroed page mechanism.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ZIDLE_LO(v) ((v) * 2 / 3)
|
|
|
|
#define ZIDLE_HI(v) ((v) * 4 / 5)
|
|
|
|
|
2001-08-25 05:00:44 +00:00
|
|
|
static int zero_state;
|
|
|
|
|
|
|
|
static int
|
|
|
|
vm_page_zero_check(void)
|
2001-07-05 01:32:42 +00:00
|
|
|
{
|
|
|
|
|
2001-08-25 05:00:44 +00:00
|
|
|
if (!idlezero_enable)
|
|
|
|
return 0;
|
2001-07-05 01:32:42 +00:00
|
|
|
/*
|
|
|
|
* Attempt to maintain approximately 1/2 of our free pages in a
|
|
|
|
* PG_ZERO'd state. Add some hysteresis to (attempt to) avoid
|
|
|
|
* generally zeroing a page when the system is near steady-state.
|
|
|
|
* Otherwise we might get 'flutter' during disk I/O / IPC or
|
|
|
|
* fast sleeps. We also do not want to be continuously zeroing
|
|
|
|
* pages because doing so may flush our L1 and L2 caches too much.
|
|
|
|
*/
|
|
|
|
if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
|
2001-08-25 05:00:44 +00:00
|
|
|
return 0;
|
2001-07-05 01:32:42 +00:00
|
|
|
if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
|
2001-08-25 05:00:44 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vm_page_zero_idle(void)
|
|
|
|
{
|
|
|
|
static int free_rover;
|
|
|
|
vm_page_t m;
|
|
|
|
|
2002-07-08 23:12:37 +00:00
|
|
|
mtx_lock_spin(&vm_page_queue_free_mtx);
|
2001-08-25 05:00:44 +00:00
|
|
|
zero_state = 0;
|
|
|
|
m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
|
|
|
|
if (m != NULL && (m->flags & PG_ZERO) == 0) {
|
2002-07-16 19:39:40 +00:00
|
|
|
vm_pageq_remove_nowakeup(m);
|
2002-07-08 23:12:37 +00:00
|
|
|
mtx_unlock_spin(&vm_page_queue_free_mtx);
|
2002-07-08 04:24:26 +00:00
|
|
|
pmap_zero_page_idle(m);
|
2002-07-08 23:12:37 +00:00
|
|
|
mtx_lock_spin(&vm_page_queue_free_mtx);
|
2002-07-18 17:40:07 +00:00
|
|
|
m->flags |= PG_ZERO;
|
2002-07-16 19:39:40 +00:00
|
|
|
vm_pageq_enqueue(PQ_FREE + m->pc, m);
|
2001-08-25 05:00:44 +00:00
|
|
|
++vm_page_zero_count;
|
|
|
|
++cnt_prezero;
|
|
|
|
if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
|
|
|
|
zero_state = 1;
|
|
|
|
}
|
|
|
|
free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
|
2002-07-08 23:12:37 +00:00
|
|
|
mtx_unlock_spin(&vm_page_queue_free_mtx);
|
2001-08-25 05:00:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Called by vm_page_free to hint that a new page is available */
|
|
|
|
void
|
|
|
|
vm_page_zero_idle_wakeup(void)
|
|
|
|
{
|
|
|
|
|
2002-07-08 04:24:26 +00:00
|
|
|
if (idlezero_enable && vm_page_zero_check())
|
2001-08-25 05:00:44 +00:00
|
|
|
wakeup(&zero_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vm_pagezero(void)
|
|
|
|
{
|
2002-07-19 21:06:01 +00:00
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
2001-08-25 05:00:44 +00:00
|
|
|
struct rtprio rtp;
|
|
|
|
int pages = 0;
|
2002-07-08 04:24:26 +00:00
|
|
|
int pri;
|
2001-08-25 05:00:44 +00:00
|
|
|
|
2002-07-19 21:06:01 +00:00
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
2001-08-25 05:00:44 +00:00
|
|
|
rtp.prio = RTP_PRIO_MAX;
|
|
|
|
rtp.type = RTP_PRIO_IDLE;
|
2001-09-01 20:16:30 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2002-02-11 20:37:54 +00:00
|
|
|
rtp_to_pri(&rtp, td->td_ksegrp);
|
2002-07-08 04:24:26 +00:00
|
|
|
pri = td->td_priority;
|
2001-09-01 20:16:30 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-07-19 21:06:01 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_flag |= P_NOLOAD;
|
|
|
|
PROC_UNLOCK(p);
|
2001-08-25 05:00:44 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (vm_page_zero_check()) {
|
|
|
|
pages += vm_page_zero_idle();
|
2002-10-12 05:32:24 +00:00
|
|
|
if (pages > idlezero_maxrun || sched_runnable()) {
|
2001-08-25 05:00:44 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_stats->p_ru.ru_nvcsw++;
|
2001-08-25 05:00:44 +00:00
|
|
|
mi_switch();
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
pages = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2002-07-08 04:24:26 +00:00
|
|
|
tsleep(&zero_state, pri, "pgzero", hz * 300);
|
2001-08-25 05:00:44 +00:00
|
|
|
pages = 0;
|
2001-07-05 01:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-04 12:08:42 +00:00
|
|
|
static struct proc *pagezero_proc;
|
2001-08-25 05:00:44 +00:00
|
|
|
static struct kproc_desc pagezero_kp = {
|
|
|
|
"pagezero",
|
|
|
|
vm_pagezero,
|
2003-04-04 12:08:42 +00:00
|
|
|
&pagezero_proc
|
2001-08-25 05:00:44 +00:00
|
|
|
};
|
|
|
|
SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &pagezero_kp)
|