vm_zeroidle.c revision 131481
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 131481 2004-07-02 20:21:44Z jhb $");
37116226Sobrien
3879265Sdillon#include <sys/param.h>
3979265Sdillon#include <sys/systm.h>
4082314Speter#include <sys/kernel.h>
4179265Sdillon#include <sys/proc.h>
4282314Speter#include <sys/resourcevar.h>
4379265Sdillon#include <sys/vmmeter.h>
4482314Speter#include <sys/lock.h>
4579265Sdillon#include <sys/mutex.h>
46104964Sjeff#include <sys/sched.h>
4779265Sdillon#include <sys/sysctl.h>
4882314Speter#include <sys/kthread.h>
49125314Sjeff#include <sys/unistd.h>
5079265Sdillon
5179265Sdillon#include <vm/vm.h>
5279265Sdillon#include <vm/vm_page.h>
5379265Sdillon
5479265SdillonSYSCTL_DECL(_vm_stats_misc);
5579265Sdillon
5679265Sdillonstatic int cnt_prezero;
57126588SbdeSYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
58126588Sbde    &cnt_prezero, 0, "");
5979265Sdillon
6099571Speterstatic int idlezero_enable = 1;
6182314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
6282314SpeterTUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
6382314Speter
6482314Speterstatic int idlezero_maxrun = 16;
6582314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
6682314SpeterTUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
6782314Speter
6879265Sdillon/*
6979265Sdillon * Implement the pre-zeroed page mechanism.
7079265Sdillon */
7179265Sdillon
7279265Sdillon#define ZIDLE_LO(v)	((v) * 2 / 3)
7379265Sdillon#define ZIDLE_HI(v)	((v) * 4 / 5)
7479265Sdillon
7582314Speterstatic int zero_state;
7682314Speter
7782314Speterstatic int
7882314Spetervm_page_zero_check(void)
7979265Sdillon{
8079265Sdillon
8182314Speter	if (!idlezero_enable)
82126588Sbde		return (0);
8379265Sdillon	/*
8479265Sdillon	 * Attempt to maintain approximately 1/2 of our free pages in a
8579265Sdillon	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
8679265Sdillon	 * generally zeroing a page when the system is near steady-state.
8779265Sdillon	 * Otherwise we might get 'flutter' during disk I/O / IPC or
8879265Sdillon	 * fast sleeps.  We also do not want to be continuously zeroing
8979265Sdillon	 * pages because doing so may flush our L1 and L2 caches too much.
9079265Sdillon	 */
9179265Sdillon	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
92126588Sbde		return (0);
9379265Sdillon	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
94126588Sbde		return (0);
95126588Sbde	return (1);
9682314Speter}
9779265Sdillon
9882314Speterstatic int
9982314Spetervm_page_zero_idle(void)
10082314Speter{
10182314Speter	static int free_rover;
10282314Speter	vm_page_t m;
10382314Speter
10499625Speter	mtx_lock_spin(&vm_page_queue_free_mtx);
10582314Speter	zero_state = 0;
10682314Speter	m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
10782314Speter	if (m != NULL && (m->flags & PG_ZERO) == 0) {
108100193Salc		vm_pageq_remove_nowakeup(m);
10999625Speter		mtx_unlock_spin(&vm_page_queue_free_mtx);
11099571Speter		pmap_zero_page_idle(m);
11199625Speter		mtx_lock_spin(&vm_page_queue_free_mtx);
112100331Salc		m->flags |= PG_ZERO;
113100193Salc		vm_pageq_enqueue(PQ_FREE + m->pc, m);
11482314Speter		++vm_page_zero_count;
11582314Speter		++cnt_prezero;
11682314Speter		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
11782314Speter			zero_state = 1;
11882314Speter	}
11982314Speter	free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
12099625Speter	mtx_unlock_spin(&vm_page_queue_free_mtx);
121126588Sbde	return (1);
12282314Speter}
12382314Speter
124126588Sbde/* Called by vm_page_free to hint that a new page is available. */
12582314Spetervoid
12682314Spetervm_page_zero_idle_wakeup(void)
12782314Speter{
12882314Speter
12999571Speter	if (idlezero_enable && vm_page_zero_check())
13082314Speter		wakeup(&zero_state);
13182314Speter}
13282314Speter
13382314Speterstatic void
134125314Sjeffvm_pagezero(void __unused *arg)
13582314Speter{
136100379Speter	struct proc *p;
13782314Speter	struct rtprio rtp;
138126588Sbde	struct thread *td;
139126588Sbde	int pages, pri;
14082314Speter
141100379Speter	td = curthread;
142100379Speter	p = td->td_proc;
14382314Speter	rtp.prio = RTP_PRIO_MAX;
14482314Speter	rtp.type = RTP_PRIO_IDLE;
145126588Sbde	pages = 0;
14682756Sjhb	mtx_lock_spin(&sched_lock);
14790538Sjulian	rtp_to_pri(&rtp, td->td_ksegrp);
14899571Speter	pri = td->td_priority;
14982756Sjhb	mtx_unlock_spin(&sched_lock);
15082314Speter
15182314Speter	for (;;) {
15282314Speter		if (vm_page_zero_check()) {
15382314Speter			pages += vm_page_zero_idle();
154131481Sjhb#ifndef PREEMPTION
155104964Sjeff			if (pages > idlezero_maxrun || sched_runnable()) {
15682314Speter				mtx_lock_spin(&sched_lock);
157131473Sjhb				mi_switch(SW_VOL, NULL);
15882314Speter				mtx_unlock_spin(&sched_lock);
15982314Speter				pages = 0;
16082314Speter			}
161131481Sjhb#endif
16282314Speter		} else {
16399571Speter			tsleep(&zero_state, pri, "pgzero", hz * 300);
16482314Speter			pages = 0;
16579265Sdillon		}
16679265Sdillon	}
16779265Sdillon}
16879265Sdillon
169113070Sdesstatic struct proc *pagezero_proc;
170125314Sjeff
171125314Sjeffstatic void
172125314Sjeffpagezero_start(void __unused *arg)
173125314Sjeff{
174125314Sjeff	int error;
175125314Sjeff
176125314Sjeff	error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0,
177125314Sjeff	    "pagezero");
178125314Sjeff	if (error)
179125314Sjeff		panic("pagezero_start: error %d\n", error);
180125314Sjeff	/*
181125314Sjeff	 * We're an idle task, don't count us in the load.
182125314Sjeff	 */
183125314Sjeff	PROC_LOCK(pagezero_proc);
184125314Sjeff	pagezero_proc->p_flag |= P_NOLOAD;
185125314Sjeff	PROC_UNLOCK(pagezero_proc);
186125314Sjeff	mtx_lock_spin(&sched_lock);
187125314Sjeff	setrunqueue(FIRST_THREAD_IN_PROC(pagezero_proc));
188125314Sjeff	mtx_unlock_spin(&sched_lock);
189125314Sjeff}
190125314SjeffSYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL)
191