vm_zeroidle.c revision 126588
1/*-
2 * Copyright (c) 1994 John Dyson
3 * Copyright (c) 2001 Matt Dillon
4 *
5 * All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
31 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
32 * from: FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 126588 2004-03-04 10:18:17Z bde $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/proc.h>
42#include <sys/resourcevar.h>
43#include <sys/vmmeter.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/sched.h>
47#include <sys/sysctl.h>
48#include <sys/kthread.h>
49#include <sys/unistd.h>
50
51#include <vm/vm.h>
52#include <vm/vm_page.h>
53
54SYSCTL_DECL(_vm_stats_misc);
55
56static int cnt_prezero;
57SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
58    &cnt_prezero, 0, "");
59
60static int idlezero_enable = 1;
61SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
62TUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
63
64static int idlezero_maxrun = 16;
65SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
66TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
67
68/*
69 * Implement the pre-zeroed page mechanism.
70 */
71
72#define ZIDLE_LO(v)	((v) * 2 / 3)
73#define ZIDLE_HI(v)	((v) * 4 / 5)
74
75static int zero_state;
76
77static int
78vm_page_zero_check(void)
79{
80
81	if (!idlezero_enable)
82		return (0);
83	/*
84	 * Attempt to maintain approximately 1/2 of our free pages in a
85	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
86	 * generally zeroing a page when the system is near steady-state.
87	 * Otherwise we might get 'flutter' during disk I/O / IPC or
88	 * fast sleeps.  We also do not want to be continuously zeroing
89	 * pages because doing so may flush our L1 and L2 caches too much.
90	 */
91	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
92		return (0);
93	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
94		return (0);
95	return (1);
96}
97
98static int
99vm_page_zero_idle(void)
100{
101	static int free_rover;
102	vm_page_t m;
103
104	mtx_lock_spin(&vm_page_queue_free_mtx);
105	zero_state = 0;
106	m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
107	if (m != NULL && (m->flags & PG_ZERO) == 0) {
108		vm_pageq_remove_nowakeup(m);
109		mtx_unlock_spin(&vm_page_queue_free_mtx);
110		pmap_zero_page_idle(m);
111		mtx_lock_spin(&vm_page_queue_free_mtx);
112		m->flags |= PG_ZERO;
113		vm_pageq_enqueue(PQ_FREE + m->pc, m);
114		++vm_page_zero_count;
115		++cnt_prezero;
116		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
117			zero_state = 1;
118	}
119	free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
120	mtx_unlock_spin(&vm_page_queue_free_mtx);
121	return (1);
122}
123
124/* Called by vm_page_free to hint that a new page is available. */
125void
126vm_page_zero_idle_wakeup(void)
127{
128
129	if (idlezero_enable && vm_page_zero_check())
130		wakeup(&zero_state);
131}
132
133static void
134vm_pagezero(void __unused *arg)
135{
136	struct proc *p;
137	struct rtprio rtp;
138	struct thread *td;
139	int pages, pri;
140
141	td = curthread;
142	p = td->td_proc;
143	rtp.prio = RTP_PRIO_MAX;
144	rtp.type = RTP_PRIO_IDLE;
145	pages = 0;
146	mtx_lock_spin(&sched_lock);
147	rtp_to_pri(&rtp, td->td_ksegrp);
148	pri = td->td_priority;
149	mtx_unlock_spin(&sched_lock);
150
151	for (;;) {
152		if (vm_page_zero_check()) {
153			pages += vm_page_zero_idle();
154			if (pages > idlezero_maxrun || sched_runnable()) {
155				mtx_lock_spin(&sched_lock);
156				mi_switch(SW_VOL);
157				mtx_unlock_spin(&sched_lock);
158				pages = 0;
159			}
160		} else {
161			tsleep(&zero_state, pri, "pgzero", hz * 300);
162			pages = 0;
163		}
164	}
165}
166
167static struct proc *pagezero_proc;
168
169static void
170pagezero_start(void __unused *arg)
171{
172	int error;
173
174	error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0,
175	    "pagezero");
176	if (error)
177		panic("pagezero_start: error %d\n", error);
178	/*
179	 * We're an idle task, don't count us in the load.
180	 */
181	PROC_LOCK(pagezero_proc);
182	pagezero_proc->p_flag |= P_NOLOAD;
183	PROC_UNLOCK(pagezero_proc);
184	mtx_lock_spin(&sched_lock);
185	setrunqueue(FIRST_THREAD_IN_PROC(pagezero_proc));
186	mtx_unlock_spin(&sched_lock);
187}
188SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL)
189