vm_zeroidle.c revision 170307
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 170307 2007-06-05 00:00:57Z jeff $");
37
38#include <opt_sched.h>
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/proc.h>
44#include <sys/vmmeter.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/sched.h>
48#include <sys/sysctl.h>
49#include <sys/kthread.h>
50#include <sys/unistd.h>
51
52#include <vm/vm.h>
53#include <vm/vm_page.h>
54
55static int cnt_prezero;
56SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
57    &cnt_prezero, 0, "");
58
59static int idlezero_enable_default = 1;
60TUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default);
61/* Defer setting the enable flag until the kthread is running. */
62static int idlezero_enable = 0;
63SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
64
65static int idlezero_maxrun = 16;
66SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
67TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
68
69/*
70 * Implement the pre-zeroed page mechanism.
71 */
72
73#define ZIDLE_LO(v)	((v) * 2 / 3)
74#define ZIDLE_HI(v)	((v) * 4 / 5)
75
76static boolean_t wakeup_needed = FALSE;
77static int zero_state;
78
79static int
80vm_page_zero_check(void)
81{
82
83	if (!idlezero_enable)
84		return (0);
85	/*
86	 * Attempt to maintain approximately 1/2 of our free pages in a
87	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
88	 * generally zeroing a page when the system is near steady-state.
89	 * Otherwise we might get 'flutter' during disk I/O / IPC or
90	 * fast sleeps.  We also do not want to be continuously zeroing
91	 * pages because doing so may flush our L1 and L2 caches too much.
92	 */
93	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
94		return (0);
95	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
96		return (0);
97	return (1);
98}
99
100static void
101vm_page_zero_idle(void)
102{
103	static int free_rover;
104	vm_page_t m;
105
106	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
107	zero_state = 0;
108	m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
109	if (m != NULL && (m->flags & PG_ZERO) == 0) {
110		vm_pageq_remove_nowakeup(m);
111		mtx_unlock(&vm_page_queue_free_mtx);
112		pmap_zero_page_idle(m);
113		mtx_lock(&vm_page_queue_free_mtx);
114		m->flags |= PG_ZERO;
115		vm_pageq_enqueue(PQ_FREE + m->pc, m);
116		++vm_page_zero_count;
117		++cnt_prezero;
118		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
119			zero_state = 1;
120	}
121	free_rover = (free_rover + PQ_PRIME2) & PQ_COLORMASK;
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	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
130	if (wakeup_needed && vm_page_zero_check()) {
131		wakeup_needed = FALSE;
132		wakeup(&zero_state);
133	}
134}
135
136static void
137vm_pagezero(void __unused *arg)
138{
139
140	idlezero_enable = idlezero_enable_default;
141
142	mtx_lock(&vm_page_queue_free_mtx);
143	for (;;) {
144		if (vm_page_zero_check()) {
145			vm_page_zero_idle();
146#ifndef PREEMPTION
147			if (sched_runnable()) {
148				thread_lock(curthread);
149				mi_switch(SW_VOL, NULL);
150				thread_unlock(curthread);
151			}
152#endif
153		} else {
154			wakeup_needed = TRUE;
155			msleep(&zero_state, &vm_page_queue_free_mtx, 0,
156			    "pgzero", hz * 300);
157		}
158	}
159}
160
161static struct proc *pagezero_proc;
162
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	td = FIRST_THREAD_IN_PROC(pagezero_proc);
180	thread_lock(td);
181	sched_class(td, PRI_IDLE);
182	sched_prio(td, PRI_MAX_IDLE);
183	sched_add(td, SRQ_BORING);
184	thread_unlock(td);
185}
186SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL)
187