]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_pageq.c
This commit was generated by cvs2svn to compensate for changes in r165538,
[FreeBSD/FreeBSD.git] / sys / vm / vm_pageq.c
1 /*-
2  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * 1. Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  *    notice, this list of conditions and the following disclaimer in the
10  *    documentation and/or other materials provided with the distribution.
11  * 4. Neither the name of the University nor the names of its contributors
12  *    may be used to endorse or promote products derived from this software
13  *    without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_vmpage.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/linker_set.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/proc.h>
41 #include <sys/vmmeter.h>
42 #include <sys/vnode.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_pageout.h>
50 #include <vm/vm_pager.h>
51 #include <vm/vm_extern.h>
52
53 static void vm_coloring_init(void);
54 void setPQL2(int *const size, int *const ways);
55
56 struct vpgqueues vm_page_queues[PQ_MAXCOUNT];
57 struct pq_coloring page_queue_coloring;
58
59 static int pq_cachesize = 0;    /* size of the cache in KB */
60 static int pq_cachenways = 0;   /* associativity of the cache */
61
62 SYSCTL_NODE(_vm_stats, OID_AUTO, pagequeue, CTLFLAG_RW, 0, "VM meter stats");
63 SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, page_colors, CTLFLAG_RD,
64     &(PQ_NUMCOLORS), 0, "Number of colors in the page queue");
65 SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, cachesize, CTLFLAG_RD,
66     &pq_cachesize, 0, "Size of the processor cache in KB");
67 SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, cachenways, CTLFLAG_RD,
68     &pq_cachenways, 0, "Associativity of the processor cache");
69 SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, prime1, CTLFLAG_RD,
70     &(PQ_PRIME1), 0, "Cache tuning value");
71 SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, prime2, CTLFLAG_RD,
72     &(PQ_PRIME2), 0, "Cache tuning value");
73
74 static void
75 vm_coloring_init(void)
76 {
77 #ifdef PQ_NOOPT
78         PQ_NUMCOLORS = PQ_PRIME1 = PQ_PRIME2 = 1;
79 #else
80
81         setPQL2(&pq_cachesize, &pq_cachenways);
82
83         CTASSERT(PAGE_SIZE/1024 > 0);
84
85         if (pq_cachesize > 0 && pq_cachenways > 0)
86                 PQ_NUMCOLORS = pq_cachesize / (PAGE_SIZE/1024) / \
87                     pq_cachenways;
88         else
89                 PQ_NUMCOLORS = 32;
90
91         if (PQ_MAXCOLORS < PQ_NUMCOLORS) {
92                 printf("VM-PQ color limit (PQ_MAXCOLORS=%u) exceeded (%u), see vm_page.h", PQ_MAXCOLORS, PQ_NUMCOLORS);
93                 PQ_NUMCOLORS = PQ_MAXCOLORS;
94         }
95
96         if (PQ_NUMCOLORS >= 128) {
97                 PQ_PRIME1 = 31;
98                 PQ_PRIME2 = 23;
99         } else if (PQ_NUMCOLORS >= 64) {
100                 PQ_PRIME1 = 13;
101                 PQ_PRIME2 = 7;
102         } else if (PQ_NUMCOLORS >= 32) {
103                 PQ_PRIME1 = 9;
104                 PQ_PRIME2 = 5;
105         } else if (PQ_NUMCOLORS >= 16) {
106                 PQ_PRIME1 = 5;
107                 PQ_PRIME2 = 3;
108         } else
109                 PQ_NUMCOLORS = PQ_PRIME1 = PQ_PRIME2 = 1;
110 #endif
111
112         /*
113          * PQ_CACHE represents a
114          * PQ_NUMCOLORS consecutive queue.
115          */
116         PQ_COLORMASK = PQ_NUMCOLORS - 1;
117         PQ_INACTIVE  = 1 + PQ_NUMCOLORS;
118         PQ_ACTIVE    = 2 + PQ_NUMCOLORS;
119         PQ_CACHE     = 3 + PQ_NUMCOLORS;
120         PQ_HOLD      = 3 + 2 * PQ_NUMCOLORS;
121         PQ_COUNT     = 4 + 2 * PQ_NUMCOLORS;
122         PQ_MAXLENGTH = PQ_NUMCOLORS / 3 + PQ_PRIME1;
123
124 #if 0
125         /* XXX: is it possible to allocate vm_page_queues[PQ_COUNT] here? */
126 #error XXX: vm_page_queues = malloc(PQ_COUNT * sizeof(struct vpgqueues));
127 #endif
128
129         if (bootverbose)
130                 if (PQ_NUMCOLORS > 1)
131                     printf("Using %d colors for the VM-PQ tuning (%d, %d)\n",
132                     PQ_NUMCOLORS, pq_cachesize, pq_cachenways);
133 }
134
135 void
136 vm_pageq_init(void)
137 {
138         int i;
139
140         vm_coloring_init();
141
142         for (i = 0; i < PQ_NUMCOLORS; ++i) {
143                 vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
144         }
145         for (i = 0; i < PQ_NUMCOLORS; ++i) {
146                 vm_page_queues[PQ_CACHE + i].cnt = &cnt.v_cache_count;
147         }
148         vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
149         vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
150         vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
151
152         for (i = 0; i < PQ_COUNT; i++) {
153                 TAILQ_INIT(&vm_page_queues[i].pl);
154         }
155 }
156
157 void
158 vm_pageq_requeue(vm_page_t m)
159 {
160         int queue = VM_PAGE_GETQUEUE(m);
161         struct vpgqueues *vpq;
162
163         if (queue != PQ_NONE) {
164                 vpq = &vm_page_queues[queue];
165                 TAILQ_REMOVE(&vpq->pl, m, pageq);
166                 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
167         }
168 }
169
170 /*
171  *      vm_pageq_enqueue:
172  */
173 void
174 vm_pageq_enqueue(int queue, vm_page_t m)
175 {
176         struct vpgqueues *vpq;
177
178         vpq = &vm_page_queues[queue];
179         VM_PAGE_SETQUEUE2(m, queue);
180         TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
181         ++*vpq->cnt;
182         ++vpq->lcnt;
183 }
184
185 /*
186  *      vm_add_new_page:
187  *
188  *      Add a new page to the freelist for use by the system.
189  */
190 void
191 vm_pageq_add_new_page(vm_paddr_t pa)
192 {
193         vm_page_t m;
194
195         atomic_add_int(&cnt.v_page_count, 1);
196         m = PHYS_TO_VM_PAGE(pa);
197         m->phys_addr = pa;
198         m->flags = 0;
199         m->pc = (pa >> PAGE_SHIFT) & PQ_COLORMASK;
200         pmap_page_init(m);
201         mtx_lock_spin(&vm_page_queue_free_mtx);
202         vm_pageq_enqueue(m->pc + PQ_FREE, m);
203         mtx_unlock_spin(&vm_page_queue_free_mtx);
204 }
205
206 /*
207  * vm_pageq_remove_nowakeup:
208  *
209  *      vm_page_unqueue() without any wakeup
210  *
211  *      The queue containing the given page must be locked.
212  *      This routine may not block.
213  */
214 void
215 vm_pageq_remove_nowakeup(vm_page_t m)
216 {
217         int queue = VM_PAGE_GETQUEUE(m);
218         struct vpgqueues *pq;
219
220         if (queue != PQ_NONE) {
221                 pq = &vm_page_queues[queue];
222                 VM_PAGE_SETQUEUE2(m, PQ_NONE);
223                 TAILQ_REMOVE(&pq->pl, m, pageq);
224                 (*pq->cnt)--;
225                 pq->lcnt--;
226         }
227 }
228
229 /*
230  * vm_pageq_remove:
231  *
232  *      Remove a page from its queue.
233  *
234  *      The queue containing the given page must be locked.
235  *      This routine may not block.
236  */
237 void
238 vm_pageq_remove(vm_page_t m)
239 {
240         int queue = VM_PAGE_GETQUEUE(m);
241         struct vpgqueues *pq;
242
243         if (queue != PQ_NONE) {
244                 VM_PAGE_SETQUEUE2(m, PQ_NONE);
245                 pq = &vm_page_queues[queue];
246                 TAILQ_REMOVE(&pq->pl, m, pageq);
247                 (*pq->cnt)--;
248                 pq->lcnt--;
249                 if (VM_PAGE_RESOLVEQUEUE(m, queue) == PQ_CACHE) {
250                         if (vm_paging_needed())
251                                 pagedaemon_wakeup();
252                 }
253         }
254 }
255
256 #ifndef PQ_NOOPT
257
258 /*
259  *      vm_pageq_find:
260  *
261  *      Find a page on the specified queue with color optimization.
262  *
263  *      The page coloring optimization attempts to locate a page
264  *      that does not overload other nearby pages in the object in
265  *      the cpu's L2 cache.  We need this optimization because cpu
266  *      caches tend to be physical caches, while object spaces tend 
267  *      to be virtual.
268  *
269  *      The specified queue must be locked.
270  *      This routine may not block.
271  *
272  *      This routine may only be called from the vm_pageq_find()
273  *      function in this file.
274  */
275 static inline vm_page_t
276 _vm_pageq_find(int basequeue, int index)
277 {
278         int i;
279         vm_page_t m = NULL;
280         struct vpgqueues *pq;
281
282         pq = &vm_page_queues[basequeue];
283
284         /*
285          * Note that for the first loop, index+i and index-i wind up at the
286          * same place.  Even though this is not totally optimal, we've already
287          * blown it by missing the cache case so we do not care.
288          */
289         for (i = PQ_NUMCOLORS / 2; i > 0; --i) {
290                 if ((m = TAILQ_FIRST(&pq[(index + i) & PQ_COLORMASK].pl)) \
291                     != NULL)
292                         break;
293
294                 if ((m = TAILQ_FIRST(&pq[(index - i) & PQ_COLORMASK].pl)) \
295                     != NULL)
296                         break;
297         }
298         return (m);
299 }
300 #endif /* PQ_NOOPT */
301
302 vm_page_t
303 vm_pageq_find(int basequeue, int index, boolean_t prefer_zero)
304 {
305         vm_page_t m;
306
307 #ifndef PQ_NOOPT
308         if (PQ_NUMCOLORS > 1) {
309                 if (prefer_zero) {
310                         m = TAILQ_LAST(&vm_page_queues[basequeue+index].pl, \
311                             pglist);
312                 } else {
313                         m = TAILQ_FIRST(&vm_page_queues[basequeue+index].pl);
314                 }
315                 if (m == NULL) {
316                         m = _vm_pageq_find(basequeue, index);
317                 }
318         } else {
319 #endif
320                 if (prefer_zero) {
321                         m = TAILQ_LAST(&vm_page_queues[basequeue].pl, pglist);
322                 } else {
323                         m = TAILQ_FIRST(&vm_page_queues[basequeue].pl);
324                 }
325 #ifndef PQ_NOOPT
326         }
327 #endif
328         return (m);
329 }
330