vm_zeroidle.c revision 116226
1/*-
2 * Copyright (c) 1994 John Dyson
3 * Copyright (c) 2001 Matt Dillon
4 *
5 * All rights reserved.  Terms for use and redistribution
6 * are covered by the BSD Copyright as found in /usr/src/COPYRIGHT.
7 *
8 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
9 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
10 */
11
12#include <sys/cdefs.h>
13__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 116226 2003-06-11 23:50:51Z obrien $");
14
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/kernel.h>
18#include <sys/proc.h>
19#include <sys/resourcevar.h>
20#include <sys/vmmeter.h>
21#include <sys/lock.h>
22#include <sys/mutex.h>
23#include <sys/sched.h>
24#include <sys/sysctl.h>
25#include <sys/kthread.h>
26
27#include <vm/vm.h>
28#include <vm/vm_page.h>
29
30SYSCTL_DECL(_vm_stats_misc);
31
32static int cnt_prezero;
33SYSCTL_INT(_vm_stats_misc, OID_AUTO,
34	cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
35
36static int idlezero_enable = 1;
37SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
38TUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
39
40static int idlezero_maxrun = 16;
41SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
42TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
43
44/*
45 * Implement the pre-zeroed page mechanism.
46 */
47
48#define ZIDLE_LO(v)	((v) * 2 / 3)
49#define ZIDLE_HI(v)	((v) * 4 / 5)
50
51static int zero_state;
52
53static int
54vm_page_zero_check(void)
55{
56
57	if (!idlezero_enable)
58		return 0;
59	/*
60	 * Attempt to maintain approximately 1/2 of our free pages in a
61	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
62	 * generally zeroing a page when the system is near steady-state.
63	 * Otherwise we might get 'flutter' during disk I/O / IPC or
64	 * fast sleeps.  We also do not want to be continuously zeroing
65	 * pages because doing so may flush our L1 and L2 caches too much.
66	 */
67	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
68		return 0;
69	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
70		return 0;
71	return 1;
72}
73
74static int
75vm_page_zero_idle(void)
76{
77	static int free_rover;
78	vm_page_t m;
79
80	mtx_lock_spin(&vm_page_queue_free_mtx);
81	zero_state = 0;
82	m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
83	if (m != NULL && (m->flags & PG_ZERO) == 0) {
84		vm_pageq_remove_nowakeup(m);
85		mtx_unlock_spin(&vm_page_queue_free_mtx);
86		pmap_zero_page_idle(m);
87		mtx_lock_spin(&vm_page_queue_free_mtx);
88		m->flags |= PG_ZERO;
89		vm_pageq_enqueue(PQ_FREE + m->pc, m);
90		++vm_page_zero_count;
91		++cnt_prezero;
92		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
93			zero_state = 1;
94	}
95	free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
96	mtx_unlock_spin(&vm_page_queue_free_mtx);
97	return 1;
98}
99
100
101/* Called by vm_page_free to hint that a new page is available */
102void
103vm_page_zero_idle_wakeup(void)
104{
105
106	if (idlezero_enable && vm_page_zero_check())
107		wakeup(&zero_state);
108}
109
110static void
111vm_pagezero(void)
112{
113	struct thread *td;
114	struct proc *p;
115	struct rtprio rtp;
116	int pages = 0;
117	int pri;
118
119	td = curthread;
120	p = td->td_proc;
121	rtp.prio = RTP_PRIO_MAX;
122	rtp.type = RTP_PRIO_IDLE;
123	mtx_lock_spin(&sched_lock);
124	rtp_to_pri(&rtp, td->td_ksegrp);
125	pri = td->td_priority;
126	mtx_unlock_spin(&sched_lock);
127	PROC_LOCK(p);
128	p->p_flag |= P_NOLOAD;
129	PROC_UNLOCK(p);
130
131	for (;;) {
132		if (vm_page_zero_check()) {
133			pages += vm_page_zero_idle();
134			if (pages > idlezero_maxrun || sched_runnable()) {
135				mtx_lock_spin(&sched_lock);
136				td->td_proc->p_stats->p_ru.ru_nvcsw++;
137				mi_switch();
138				mtx_unlock_spin(&sched_lock);
139				pages = 0;
140			}
141		} else {
142			tsleep(&zero_state, pri, "pgzero", hz * 300);
143			pages = 0;
144		}
145	}
146}
147
148static struct proc *pagezero_proc;
149static struct kproc_desc pagezero_kp = {
150	 "pagezero",
151	 vm_pagezero,
152	 &pagezero_proc
153};
154SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &pagezero_kp)
155