]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/vm/vm_zeroidle.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / vm / vm_zeroidle.c
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$");
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 #include <vm/vm_phys.h>
55
56 static int idlezero_enable_default = 0;
57 TUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default);
58 /* Defer setting the enable flag until the kthread is running. */
59 static int idlezero_enable = 0;
60 SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0,
61     "Allow the kernel to use idle cpu cycles to zero-out pages");
62 /*
63  * Implement the pre-zeroed page mechanism.
64  */
65
66 #define ZIDLE_LO(v)     ((v) * 2 / 3)
67 #define ZIDLE_HI(v)     ((v) * 4 / 5)
68
69 static boolean_t wakeup_needed = FALSE;
70 static int zero_state;
71
72 static int
73 vm_page_zero_check(void)
74 {
75
76         if (!idlezero_enable)
77                 return (0);
78         /*
79          * Attempt to maintain approximately 1/2 of our free pages in a
80          * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
81          * generally zeroing a page when the system is near steady-state.
82          * Otherwise we might get 'flutter' during disk I/O / IPC or 
83          * fast sleeps.  We also do not want to be continuously zeroing
84          * pages because doing so may flush our L1 and L2 caches too much.
85          */
86         if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
87                 return (0);
88         if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
89                 return (0);
90         return (1);
91 }
92
93 static void
94 vm_page_zero_idle(void)
95 {
96
97         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
98         zero_state = 0;
99         if (vm_phys_zero_pages_idle()) {
100                 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
101                         zero_state = 1;
102         }
103 }
104
105 /* Called by vm_page_free to hint that a new page is available. */
106 void
107 vm_page_zero_idle_wakeup(void)
108 {
109
110         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
111         if (wakeup_needed && vm_page_zero_check()) {
112                 wakeup_needed = FALSE;
113                 wakeup(&zero_state);
114         }
115 }
116
117 static void
118 vm_pagezero(void __unused *arg)
119 {
120
121         idlezero_enable = idlezero_enable_default;
122
123         mtx_lock(&vm_page_queue_free_mtx);
124         for (;;) {
125                 if (vm_page_zero_check()) {
126                         vm_page_zero_idle();
127 #ifndef PREEMPTION
128                         if (sched_runnable()) {
129                                 thread_lock(curthread);
130                                 mi_switch(SW_VOL | SWT_IDLE, NULL);
131                                 thread_unlock(curthread);
132                         }
133 #endif
134                 } else {
135                         wakeup_needed = TRUE;
136                         msleep(&zero_state, &vm_page_queue_free_mtx, 0,
137                             "pgzero", hz * 300);
138                 }
139         }
140 }
141
142 static void
143 pagezero_start(void __unused *arg)
144 {
145         int error;
146         struct proc *p;
147         struct thread *td;
148
149         error = kproc_create(vm_pagezero, NULL, &p, RFSTOPPED, 0, "pagezero");
150         if (error)
151                 panic("pagezero_start: error %d\n", error);
152         td = FIRST_THREAD_IN_PROC(p);
153         thread_lock(td);
154
155         /* We're an idle task, don't count us in the load. */
156         td->td_flags |= TDF_NOLOAD;
157         sched_class(td, PRI_IDLE);
158         sched_prio(td, PRI_MAX_IDLE);
159         sched_add(td, SRQ_BORING);
160         thread_unlock(td);
161 }
162 SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL);