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