kernel - zero pages during idle
[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  * $Id: vm_zeroidle.c,v 1.3 2010/05/12 04:50:45 sv5679 Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/vmmeter.h>
47 #include <sys/sched.h>
48 #include <sys/sysctl.h>
49 #include <sys/thread.h>
50 #include <sys/thread2.h>
51 #include <sys/kthread.h>
52 #include <sys/mplock2.h>
53 #include <sys/unistd.h>
54 #include <vm/vm.h>
55 #include <vm/vm_page.h>
56 #include <cpu/lwbuf.h>
57
58 /*
59  * Implement the pre-zeroed page mechanism.
60  */
61 #define ZIDLE_LO(v)     ((v) * 2 / 3)
62 #define ZIDLE_HI(v)     ((v) * 4 / 5)
63
64 /* Number of bytes to zero between reschedule checks */
65 #define IDLEZERO_RUN    (32)
66
67 /* Maximum number of pages per second to zero */
68 #define NPAGES_RUN      (20000)
69
70
71 static int idlezero_enable = 0;
72 TUNABLE_INT("vm.idlezero_enable", &idlezero_enable);
73 SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0,
74            "Allow the kernel to use idle CPU cycles to zero pages");
75 static int idlezero_rate = NPAGES_RUN;
76 SYSCTL_INT(_vm, OID_AUTO, idlezero_rate, CTLFLAG_RW, &idlezero_rate, 0,
77            "Maximum pages per second to zero");
78 static int idlezero_nocache = 0;
79 SYSCTL_INT(_vm, OID_AUTO, idlezero_nocache, CTLFLAG_RW, &idlezero_nocache, 0,
80            "Maximum pages per second to zero");
81
82 static int idlezero_count = 0;
83 SYSCTL_INT(_vm, OID_AUTO, idlezero_count, CTLFLAG_RD, &idlezero_count, 0,
84            "The number of physical pages prezeroed at idle time");
85
86 enum zeroidle_state {
87         STATE_IDLE,
88         STATE_GET_PAGE,
89         STATE_ZERO_PAGE,
90         STATE_RELEASE_PAGE
91 };
92
93 static int zero_state;
94
95 /*
96  * Attempt to maintain approximately 1/2 of our free pages in a
97  * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid
98  * generally zeroing a page when the system is near steady-state.
99  * Otherwise we might get 'flutter' during disk I/O / IPC or
100  * fast sleeps. We also do not want to be continuously zeroing
101  * pages because doing so may flush our L1 and L2 caches too much.
102  */
103 static int
104 vm_page_zero_check(void)
105 {
106         if (idlezero_enable == 0)
107                 return (0);
108         if (zero_state && vm_page_zero_count >= ZIDLE_LO(vmstats.v_free_count))
109                 return (0);
110         if (vm_page_zero_count >= ZIDLE_HI(vmstats.v_free_count))
111                 return (0);
112         return (1);
113 }
114
115 static void
116 vm_pagezero(void __unused *arg)
117 {
118         vm_page_t m = NULL;
119         struct lwbuf *buf = NULL;
120         enum zeroidle_state state = STATE_IDLE;
121         char *pg = NULL;
122         int npages = 0;
123         int i = 0;
124
125         /*
126          * Adjust thread parameters before entering our loop.  The thread
127          * is started with the MP lock held and with normal kernel thread
128          * priority.
129          *
130          * Also put us on the last cpu for now.
131          */
132         rel_mplock();
133         lwkt_setpri_self(TDPRI_IDLE_WORK);
134         lwkt_setcpu_self(globaldata_find(ncpus - 1));
135
136         /*
137          * Loop forever
138          */
139         for (;;) {
140                 switch(state) {
141                 case STATE_IDLE:
142                         /*
143                          * Wait for work.
144                          */
145                         tsleep(&zero_state, 0, "pgzero", hz / 10);
146                         if (vm_page_zero_check())
147                                 npages = idlezero_rate / 10;
148                         if (npages)
149                                 state = STATE_GET_PAGE; /* Fallthrough */
150                         break;
151                 case STATE_GET_PAGE:
152                         /*
153                          * Acquire page to zero
154                          */
155                         if (try_mplock() == 0) {
156                                 state = STATE_IDLE;
157                         } else if (--npages == 0) {
158                                 state = STATE_IDLE;
159                                 rel_mplock();
160                         } else {
161                                 m = vm_page_free_fromq_fast();
162                                 if (m == NULL) {
163                                         state = STATE_IDLE;
164                                 } else {
165                                         state = STATE_ZERO_PAGE;
166                                         buf = lwbuf_alloc(m);
167                                         pg = (char *)lwbuf_kva(buf);
168                                         i = 0;
169                                 }
170                                 rel_mplock();
171                         }
172                         break;
173                 case STATE_ZERO_PAGE:
174                         /*
175                          * Zero-out the page, stop immediately if a
176                          * resched has been requested.
177                          */
178                         while (i < PAGE_SIZE) {
179                                 if (lwkt_check_resched(curthread))
180                                         break;
181                                 if (idlezero_nocache == 1)
182                                         bzeront(&pg[i], IDLEZERO_RUN);
183                                 else
184                                         bzero(&pg[i], IDLEZERO_RUN);
185                                 i += IDLEZERO_RUN;
186                         }
187                         if (i == PAGE_SIZE)
188                                 state = STATE_RELEASE_PAGE;
189                         break;
190                 case STATE_RELEASE_PAGE:
191                         if (try_mplock()) {
192                                 lwbuf_free(buf);
193                                 vm_page_flag_set(m, PG_ZERO);
194                                 vm_page_free_toq(m);
195                                 state = STATE_GET_PAGE;
196                                 ++idlezero_count;
197                                 rel_mplock();
198                         }
199                         break;
200                 }
201                 lwkt_switch();
202         }
203 }
204
205 static void
206 pagezero_start(void __unused *arg)
207 {
208         int error;
209         struct thread *td;
210
211         error = kthread_create(vm_pagezero, NULL, &td, "pagezero");
212         if (error)
213                 panic("pagezero_start: error %d\n", error);
214 }
215
216 SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL);