2d286e6e485dd34347698db4b523885ba378466a
[dragonfly.git] / sys / vm / vm_zeroidle.c
1 /*
2  * Copyright (c) 1994 John Dyson
3  * Copyright (c) 2001 Matt Dillon
4  * Copyright (c) 2010 The DragonFly Project
5  *
6  * All Rights Reserved.
7  *
8  * This code is derived from software contributed to The DragonFly Project
9  * by Venkatesh Srinivas <me@endeavour.zapto.org>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
36  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
37  * from FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon
38  */
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/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread.h>
48 #include <sys/thread2.h>
49 #include <sys/kthread.h>
50 #include <sys/mplock2.h>
51 #include <sys/unistd.h>
52 #include <vm/vm.h>
53 #include <vm/vm_page.h>
54 #include <cpu/lwbuf.h>
55
56 /*
57  * Implement the pre-zeroed page mechanism.
58  */
59 #define ZIDLE_LO(v)     ((v) * 2 / 3)
60 #define ZIDLE_HI(v)     ((v) * 4 / 5)
61
62 /* Number of bytes to zero between reschedule checks */
63 #define IDLEZERO_RUN    (64)
64
65 /* Maximum number of pages per second to zero */
66 #define NPAGES_RUN      (20000)
67
68 static int idlezero_enable = 1;
69 TUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
70 SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0,
71            "Allow the kernel to use idle CPU cycles to zero pages");
72 static int idlezero_rate = NPAGES_RUN;
73 SYSCTL_INT(_vm, OID_AUTO, idlezero_rate, CTLFLAG_RW, &idlezero_rate, 0,
74            "Maximum pages per second to zero");
75 int bzeront_avail = 0;
76 static int idlezero_nocache = 0;
77 SYSCTL_INT(_vm, OID_AUTO, idlezero_nocache, CTLFLAG_RW, &idlezero_nocache, 0,
78            "Maximum pages per second to zero");
79
80 static int idlezero_count = 0;
81 SYSCTL_INT(_vm, OID_AUTO, idlezero_count, CTLFLAG_RD, &idlezero_count, 0,
82            "The number of physical pages prezeroed at idle time");
83
84 enum zeroidle_state {
85         STATE_IDLE,
86         STATE_GET_PAGE,
87         STATE_ZERO_PAGE,
88         STATE_RELEASE_PAGE
89 };
90
91 #define DEFAULT_SLEEP_TIME      (hz / 10)
92 #define LONG_SLEEP_TIME         (hz * 10)
93
94 static int zero_state;
95
96 /*
97  * Attempt to maintain approximately 1/2 of our free pages in a
98  * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid
99  * generally zeroing a page when the system is near steady-state.
100  * Otherwise we might get 'flutter' during disk I/O / IPC or
101  * fast sleeps. We also do not want to be continuously zeroing
102  * pages because doing so may flush our L1 and L2 caches too much.
103  *
104  * Returns non-zero if pages should be zerod.
105  */
106 static int
107 vm_page_zero_check(void)
108 {
109         if (idlezero_enable == 0)
110                 return (0);
111         if (zero_state == 0) {
112                 /*
113                  * Wait for the count to fall to LO before starting
114                  * to zero pages.
115                  */
116                 if (vm_page_zero_count <= ZIDLE_LO(vmstats.v_free_count))
117                         zero_state = 1;
118         } else {
119                 /*
120                  * Once we are zeroing pages wait for the count to
121                  * increase to HI before we stop zeroing pages.
122                  */
123                 if (vm_page_zero_count >= ZIDLE_HI(vmstats.v_free_count))
124                         zero_state = 0;
125         }
126         return (zero_state);
127 }
128
129 /*
130  * vm_pagezero should sleep for a longer time when idlezero is disabled or
131  * when there is an excess of zeroed pages.
132  */
133 static int
134 vm_page_zero_time(void)
135 {
136         if (idlezero_enable == 0)
137                 return (LONG_SLEEP_TIME);
138         if (vm_page_zero_count >= ZIDLE_HI(vmstats.v_free_count))
139                 return (LONG_SLEEP_TIME);
140         return (DEFAULT_SLEEP_TIME);
141 }
142
143 static void
144 vm_pagezero(void __unused *arg)
145 {
146         vm_page_t m = NULL;
147         struct lwbuf *buf = NULL;
148         enum zeroidle_state state = STATE_IDLE;
149         char *pg = NULL;
150         int npages = 0;
151         int sleep_time; 
152         int i = 0;
153
154         /*
155          * Adjust thread parameters before entering our loop.  The thread
156          * is started with the MP lock held and with normal kernel thread
157          * priority.
158          *
159          * Also put us on the last cpu for now.
160          *
161          * For now leave the MP lock held, the VM routines cannot be called
162          * with it released until tokenization is finished.
163          */
164         /* rel_mplock(); */
165         lwkt_setpri_self(TDPRI_IDLE_WORK);
166         lwkt_setcpu_self(globaldata_find(ncpus - 1));
167         sleep_time = DEFAULT_SLEEP_TIME;
168
169         /*
170          * Loop forever
171          */
172         for (;;) {
173                 switch(state) {
174                 case STATE_IDLE:
175                         /*
176                          * Wait for work.
177                          */
178                         tsleep(&zero_state, 0, "pgzero", sleep_time);
179                         if (vm_page_zero_check())
180                                 npages = idlezero_rate / 10;
181                         sleep_time = vm_page_zero_time();
182                         if (npages)
183                                 state = STATE_GET_PAGE; /* Fallthrough */
184                         break;
185                 case STATE_GET_PAGE:
186                         /*
187                          * Acquire page to zero
188                          */
189                         if (--npages == 0) {
190                                 state = STATE_IDLE;
191                         } else {
192                                 m = vm_page_free_fromq_fast();
193                                 if (m == NULL) {
194                                         state = STATE_IDLE;
195                                 } else {
196                                         state = STATE_ZERO_PAGE;
197                                         buf = lwbuf_alloc(m);
198                                         pg = (char *)lwbuf_kva(buf);
199                                         i = 0;
200                                 }
201                         }
202                         break;
203                 case STATE_ZERO_PAGE:
204                         /*
205                          * Zero-out the page
206                          */
207                         while (i < PAGE_SIZE) {
208                                 if (idlezero_nocache == 1)
209                                         bzeront(&pg[i], IDLEZERO_RUN);
210                                 else
211                                         bzero(&pg[i], IDLEZERO_RUN);
212                                 i += IDLEZERO_RUN;
213                                 lwkt_yield();
214                         }
215                         state = STATE_RELEASE_PAGE;
216                         break;
217                 case STATE_RELEASE_PAGE:
218                         lwbuf_free(buf);
219                         vm_page_flag_set(m, PG_ZERO);
220                         vm_page_free_toq(m);
221                         state = STATE_GET_PAGE;
222                         ++idlezero_count;
223                         break;
224                 }
225                 lwkt_yield();
226         }
227 }
228
229 static void
230 pagezero_start(void __unused *arg)
231 {
232         int error;
233         struct thread *td;
234
235         idlezero_nocache = bzeront_avail;
236
237         error = kthread_create(vm_pagezero, NULL, &td, "pagezero");
238         if (error)
239                 panic("pagezero_start: error %d\n", error);
240 }
241
242 SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL);