vm_zeroidle.c revision 170170
179265Sdillon/*-
279265Sdillon * Copyright (c) 1994 John Dyson
379265Sdillon * Copyright (c) 2001 Matt Dillon
479265Sdillon *
5118848Simp * All Rights Reserved.
6118848Simp * Redistribution and use in source and binary forms, with or without
7118848Simp * modification, are permitted provided that the following conditions
8118848Simp * are met:
9118848Simp * 1. Redistributions of source code must retain the above copyright
10118848Simp *    notice, this list of conditions and the following disclaimer.
11118848Simp * 2. Redistributions in binary form must reproduce the above copyright
12118848Simp *    notice, this list of conditions and the following disclaimer in the
13118848Simp *    documentation and/or other materials provided with the distribution.
14118848Simp * 4. Neither the name of the University nor the names of its contributors
15118848Simp *    may be used to endorse or promote products derived from this software
16118848Simp *    without specific prior written permission.
1779265Sdillon *
18118848Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19118848Simp * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20118848Simp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21118848Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22118848Simp * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23118848Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24118848Simp * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25118848Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26118848Simp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27118848Simp * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28118848Simp * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29118848Simp *
3079265Sdillon *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
3179265Sdillon *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
32126588Sbde * from: FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon
3379265Sdillon */
3479265Sdillon
35116226Sobrien#include <sys/cdefs.h>
36116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 170170 2007-05-31 22:52:15Z attilio $");
37116226Sobrien
38134649Sscottl#include <opt_sched.h>
39134649Sscottl
4079265Sdillon#include <sys/param.h>
4179265Sdillon#include <sys/systm.h>
4282314Speter#include <sys/kernel.h>
4379265Sdillon#include <sys/proc.h>
4479265Sdillon#include <sys/vmmeter.h>
4582314Speter#include <sys/lock.h>
4679265Sdillon#include <sys/mutex.h>
47104964Sjeff#include <sys/sched.h>
4879265Sdillon#include <sys/sysctl.h>
4982314Speter#include <sys/kthread.h>
50125314Sjeff#include <sys/unistd.h>
5179265Sdillon
5279265Sdillon#include <vm/vm.h>
5379265Sdillon#include <vm/vm_page.h>
54170816Salc
5579265Sdillonstatic int cnt_prezero;
56170816SalcSYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
57134461Siedowse    &cnt_prezero, 0, "");
58134461Siedowse
59134461Siedowsestatic int idlezero_enable_default = 1;
60181239StrhodesTUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default);
61181239Strhodes/* Defer setting the enable flag until the kthread is running. */
6279265Sdillonstatic int idlezero_enable = 0;
6379265SdillonSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
6479265Sdillon
6579265Sdillonstatic int idlezero_maxrun = 16;
6679265SdillonSYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
6779265SdillonTUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
6879265Sdillon
69137104Salc/*
7082314Speter * Implement the pre-zeroed page mechanism.
7182314Speter */
7282314Speter
7382314Speter#define ZIDLE_LO(v)	((v) * 2 / 3)
7479265Sdillon#define ZIDLE_HI(v)	((v) * 4 / 5)
7579265Sdillon
7682314Speterstatic boolean_t wakeup_needed = FALSE;
77126588Sbdestatic int zero_state;
7879265Sdillon
7979265Sdillonstatic int
8079265Sdillonvm_page_zero_check(void)
8179265Sdillon{
8279265Sdillon
8379265Sdillon	if (!idlezero_enable)
8479265Sdillon		return (0);
8579265Sdillon	/*
86170170Sattilio	 * Attempt to maintain approximately 1/2 of our free pages in a
87126588Sbde	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
88170170Sattilio	 * generally zeroing a page when the system is near steady-state.
89126588Sbde	 * Otherwise we might get 'flutter' during disk I/O / IPC or
90126588Sbde	 * fast sleeps.  We also do not want to be continuously zeroing
9182314Speter	 * pages because doing so may flush our L1 and L2 caches too much.
9279265Sdillon	 */
93161489Salc	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
9482314Speter		return (0);
9582314Speter	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
9682314Speter		return (0);
97166637Salc	return (1);
9882314Speter}
99170816Salc
100170170Sattiliostatic void
10182314Spetervm_page_zero_idle(void)
10282314Speter{
10382314Speter	static int free_rover;
10482314Speter	vm_page_t m;
105126588Sbde
10682314Speter	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
10782314Speter	zero_state = 0;
10882314Speter	m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
10982314Speter	if (m != NULL && (m->flags & PG_ZERO) == 0) {
110166637Salc		vm_pageq_remove_nowakeup(m);
111137104Salc		mtx_unlock(&vm_page_queue_free_mtx);
112137104Salc		pmap_zero_page_idle(m);
11382314Speter		mtx_lock(&vm_page_queue_free_mtx);
114137104Salc		m->flags |= PG_ZERO;
11582314Speter		vm_pageq_enqueue(PQ_FREE + m->pc, m);
11682314Speter		++vm_page_zero_count;
11782314Speter		++cnt_prezero;
118125314Sjeff		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
11982314Speter			zero_state = 1;
12082314Speter	}
121134461Siedowse	free_rover = (free_rover + PQ_PRIME2) & PQ_COLORMASK;
12282314Speter}
123166637Salc
12482314Speter/* Called by vm_page_free to hint that a new page is available. */
12582314Spetervoid
126137268Sjhbvm_page_zero_idle_wakeup(void)
127131481Sjhb{
128137268Sjhb
129170307Sjeff	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
130178272Sjeff	if (wakeup_needed && vm_page_zero_check()) {
131170307Sjeff		wakeup_needed = FALSE;
13282314Speter		wakeup(&zero_state);
133131481Sjhb	}
13482314Speter}
135137104Salc
136166637Salcstatic void
137166637Salcvm_pagezero(void __unused *arg)
13879265Sdillon{
13979265Sdillon
14079265Sdillon	idlezero_enable = idlezero_enable_default;
14179265Sdillon
142125314Sjeff	mtx_lock(&vm_page_queue_free_mtx);
143125314Sjeff	for (;;) {
144125314Sjeff		if (vm_page_zero_check()) {
145125314Sjeff			vm_page_zero_idle();
146198854Sattilio#ifndef PREEMPTION
147141247Sssouhlal			if (sched_runnable()) {
148125314Sjeff				mtx_lock_spin(&sched_lock);
149198854Sattilio				mi_switch(SW_VOL, NULL);
150125314Sjeff				mtx_unlock_spin(&sched_lock);
151125314Sjeff			}
152198854Sattilio#endif
153170307Sjeff		} else {
154198854Sattilio			wakeup_needed = TRUE;
155198854Sattilio			msleep(&zero_state, &vm_page_queue_free_mtx, 0,
156198854Sattilio			    "pgzero", hz * 300);
157163709Sjb		}
158141247Sssouhlal	}
159166188Sjeff}
160170307Sjeff
161125314Sjeffstatic struct proc *pagezero_proc;
162177253Srwatson
163static void
164pagezero_start(void __unused *arg)
165{
166	int error;
167	struct thread *td;
168
169	error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0,
170	    "pagezero");
171	if (error)
172		panic("pagezero_start: error %d\n", error);
173	/*
174	 * We're an idle task, don't count us in the load.
175	 */
176	PROC_LOCK(pagezero_proc);
177	pagezero_proc->p_flag |= P_NOLOAD;
178	PROC_UNLOCK(pagezero_proc);
179	mtx_lock_spin(&sched_lock);
180	td = FIRST_THREAD_IN_PROC(pagezero_proc);
181	sched_class(td, PRI_IDLE);
182	sched_prio(td, PRI_MAX_IDLE);
183	sched_add(td, SRQ_BORING);
184	mtx_unlock_spin(&sched_lock);
185}
186SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL)
187