uvm_pdpolicy_clockpro.c revision 1.1.6.1 1 1.1.6.1 ad /* $NetBSD: uvm_pdpolicy_clockpro.c,v 1.1.6.1 2006/11/18 21:39:50 ad Exp $ */
2 1.1.6.1 ad
3 1.1.6.1 ad /*-
4 1.1.6.1 ad * Copyright (c)2005, 2006 YAMAMOTO Takashi,
5 1.1.6.1 ad * All rights reserved.
6 1.1.6.1 ad *
7 1.1.6.1 ad * Redistribution and use in source and binary forms, with or without
8 1.1.6.1 ad * modification, are permitted provided that the following conditions
9 1.1.6.1 ad * are met:
10 1.1.6.1 ad * 1. Redistributions of source code must retain the above copyright
11 1.1.6.1 ad * notice, this list of conditions and the following disclaimer.
12 1.1.6.1 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.1 ad * notice, this list of conditions and the following disclaimer in the
14 1.1.6.1 ad * documentation and/or other materials provided with the distribution.
15 1.1.6.1 ad *
16 1.1.6.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.6.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.6.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.6.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.6.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.6.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.6.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.6.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.6.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.6.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.6.1 ad * SUCH DAMAGE.
27 1.1.6.1 ad */
28 1.1.6.1 ad
29 1.1.6.1 ad /*
30 1.1.6.1 ad * CLOCK-Pro replacement policy:
31 1.1.6.1 ad * http://www.cs.wm.edu/hpcs/WWW/HTML/publications/abs05-3.html
32 1.1.6.1 ad *
33 1.1.6.1 ad * approximation of the list of non-resident pages using hash:
34 1.1.6.1 ad * http://linux-mm.org/ClockProApproximation
35 1.1.6.1 ad */
36 1.1.6.1 ad
37 1.1.6.1 ad /* #define CLOCKPRO_DEBUG */
38 1.1.6.1 ad
39 1.1.6.1 ad #if defined(PDSIM)
40 1.1.6.1 ad
41 1.1.6.1 ad #include "pdsim.h"
42 1.1.6.1 ad
43 1.1.6.1 ad #else /* defined(PDSIM) */
44 1.1.6.1 ad
45 1.1.6.1 ad #include <sys/cdefs.h>
46 1.1.6.1 ad __KERNEL_RCSID(0, "$NetBSD: uvm_pdpolicy_clockpro.c,v 1.1.6.1 2006/11/18 21:39:50 ad Exp $");
47 1.1.6.1 ad
48 1.1.6.1 ad #include "opt_ddb.h"
49 1.1.6.1 ad
50 1.1.6.1 ad #include <sys/param.h>
51 1.1.6.1 ad #include <sys/proc.h>
52 1.1.6.1 ad #include <sys/systm.h>
53 1.1.6.1 ad #include <sys/kernel.h>
54 1.1.6.1 ad #include <sys/hash.h>
55 1.1.6.1 ad
56 1.1.6.1 ad #include <uvm/uvm.h>
57 1.1.6.1 ad #include <uvm/uvm_pdpolicy.h>
58 1.1.6.1 ad #include <uvm/uvm_pdpolicy_impl.h>
59 1.1.6.1 ad
60 1.1.6.1 ad #if ((__STDC_VERSION__ - 0) >= 199901L)
61 1.1.6.1 ad #define DPRINTF(...) /* nothing */
62 1.1.6.1 ad #define WARN(...) printf(__VA_ARGS__)
63 1.1.6.1 ad #else /* ((__STDC_VERSION__ - 0) >= 199901L) */
64 1.1.6.1 ad #define DPRINTF(a...) /* nothing */ /* GCC */
65 1.1.6.1 ad #define WARN(a...) printf(a)
66 1.1.6.1 ad #endif /* ((__STDC_VERSION__ - 0) >= 199901L) */
67 1.1.6.1 ad
68 1.1.6.1 ad #define dump(a) /* nothing */
69 1.1.6.1 ad
70 1.1.6.1 ad #undef USEONCE2
71 1.1.6.1 ad #define LISTQ
72 1.1.6.1 ad #undef ADAPTIVE
73 1.1.6.1 ad
74 1.1.6.1 ad #endif /* defined(PDSIM) */
75 1.1.6.1 ad
76 1.1.6.1 ad #if !defined(CLOCKPRO_COLDPCT)
77 1.1.6.1 ad #define CLOCKPRO_COLDPCT 10
78 1.1.6.1 ad #endif /* !defined(CLOCKPRO_COLDPCT) */
79 1.1.6.1 ad
80 1.1.6.1 ad #define CLOCKPRO_COLDPCTMAX 90
81 1.1.6.1 ad
82 1.1.6.1 ad #if !defined(CLOCKPRO_HASHFACTOR)
83 1.1.6.1 ad #define CLOCKPRO_HASHFACTOR 2
84 1.1.6.1 ad #endif /* !defined(CLOCKPRO_HASHFACTOR) */
85 1.1.6.1 ad
86 1.1.6.1 ad #define CLOCKPRO_NEWQMIN ((1024 * 1024) >> PAGE_SHIFT) /* XXX */
87 1.1.6.1 ad
88 1.1.6.1 ad int clockpro_hashfactor = CLOCKPRO_HASHFACTOR;
89 1.1.6.1 ad
90 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresrecordobj)
91 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresrecordanon)
92 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nreslookup)
93 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresfoundobj)
94 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresfoundanon)
95 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresanonfree)
96 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresconflict)
97 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nresoverwritten)
98 1.1.6.1 ad PDPOL_EVCNT_DEFINE(nreshandhot)
99 1.1.6.1 ad
100 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hhottakeover)
101 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hhotref)
102 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hhotunref)
103 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hhotcold)
104 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hhotcoldtest)
105 1.1.6.1 ad
106 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldtakeover)
107 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldref)
108 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldunref)
109 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldreftest)
110 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldunreftest)
111 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldunreftestspeculative)
112 1.1.6.1 ad PDPOL_EVCNT_DEFINE(hcoldhot)
113 1.1.6.1 ad
114 1.1.6.1 ad PDPOL_EVCNT_DEFINE(speculativeenqueue)
115 1.1.6.1 ad PDPOL_EVCNT_DEFINE(speculativehit1)
116 1.1.6.1 ad PDPOL_EVCNT_DEFINE(speculativehit2)
117 1.1.6.1 ad PDPOL_EVCNT_DEFINE(speculativemiss)
118 1.1.6.1 ad
119 1.1.6.1 ad #define PQ_REFERENCED PQ_PRIVATE1
120 1.1.6.1 ad #define PQ_HOT PQ_PRIVATE2
121 1.1.6.1 ad #define PQ_TEST PQ_PRIVATE3
122 1.1.6.1 ad #define PQ_INITIALREF PQ_PRIVATE4
123 1.1.6.1 ad #if PQ_PRIVATE6 != PQ_PRIVATE5 * 2 || PQ_PRIVATE7 != PQ_PRIVATE6 * 2
124 1.1.6.1 ad #error PQ_PRIVATE
125 1.1.6.1 ad #endif
126 1.1.6.1 ad #define PQ_QMASK (PQ_PRIVATE5|PQ_PRIVATE6|PQ_PRIVATE7)
127 1.1.6.1 ad #define PQ_QFACTOR PQ_PRIVATE5
128 1.1.6.1 ad #define PQ_SPECULATIVE PQ_PRIVATE8
129 1.1.6.1 ad
130 1.1.6.1 ad #define CLOCKPRO_NOQUEUE 0
131 1.1.6.1 ad #define CLOCKPRO_NEWQ 1 /* small queue to clear initial ref. */
132 1.1.6.1 ad #if defined(LISTQ)
133 1.1.6.1 ad #define CLOCKPRO_COLDQ 2
134 1.1.6.1 ad #define CLOCKPRO_HOTQ 3
135 1.1.6.1 ad #else /* defined(LISTQ) */
136 1.1.6.1 ad #define CLOCKPRO_COLDQ (2 + coldqidx) /* XXX */
137 1.1.6.1 ad #define CLOCKPRO_HOTQ (3 - coldqidx) /* XXX */
138 1.1.6.1 ad #endif /* defined(LISTQ) */
139 1.1.6.1 ad #define CLOCKPRO_LISTQ 4
140 1.1.6.1 ad #define CLOCKPRO_NQUEUE 4
141 1.1.6.1 ad
142 1.1.6.1 ad static inline void
143 1.1.6.1 ad clockpro_setq(struct vm_page *pg, int qidx)
144 1.1.6.1 ad {
145 1.1.6.1 ad KASSERT(qidx >= CLOCKPRO_NOQUEUE);
146 1.1.6.1 ad KASSERT(qidx <= CLOCKPRO_NQUEUE);
147 1.1.6.1 ad
148 1.1.6.1 ad pg->pqflags = (pg->pqflags & ~PQ_QMASK) | (qidx * PQ_QFACTOR);
149 1.1.6.1 ad }
150 1.1.6.1 ad
151 1.1.6.1 ad static inline int
152 1.1.6.1 ad clockpro_getq(struct vm_page *pg)
153 1.1.6.1 ad {
154 1.1.6.1 ad int qidx;
155 1.1.6.1 ad
156 1.1.6.1 ad qidx = (pg->pqflags & PQ_QMASK) / PQ_QFACTOR;
157 1.1.6.1 ad KASSERT(qidx >= CLOCKPRO_NOQUEUE);
158 1.1.6.1 ad KASSERT(qidx <= CLOCKPRO_NQUEUE);
159 1.1.6.1 ad return qidx;
160 1.1.6.1 ad }
161 1.1.6.1 ad
162 1.1.6.1 ad typedef struct {
163 1.1.6.1 ad struct pglist q_q;
164 1.1.6.1 ad int q_len;
165 1.1.6.1 ad } pageq_t;
166 1.1.6.1 ad
167 1.1.6.1 ad struct clockpro_state {
168 1.1.6.1 ad int s_npages;
169 1.1.6.1 ad int s_coldtarget;
170 1.1.6.1 ad int s_ncold;
171 1.1.6.1 ad
172 1.1.6.1 ad int s_newqlenmax;
173 1.1.6.1 ad pageq_t s_q[CLOCKPRO_NQUEUE];
174 1.1.6.1 ad
175 1.1.6.1 ad struct uvm_pctparam s_coldtargetpct;
176 1.1.6.1 ad };
177 1.1.6.1 ad
178 1.1.6.1 ad static pageq_t *
179 1.1.6.1 ad clockpro_queue(struct clockpro_state *s, int qidx)
180 1.1.6.1 ad {
181 1.1.6.1 ad
182 1.1.6.1 ad KASSERT(CLOCKPRO_NOQUEUE < qidx);
183 1.1.6.1 ad KASSERT(qidx <= CLOCKPRO_NQUEUE);
184 1.1.6.1 ad
185 1.1.6.1 ad return &s->s_q[qidx - 1];
186 1.1.6.1 ad }
187 1.1.6.1 ad
188 1.1.6.1 ad #if !defined(LISTQ)
189 1.1.6.1 ad
190 1.1.6.1 ad static int coldqidx;
191 1.1.6.1 ad
192 1.1.6.1 ad static void
193 1.1.6.1 ad clockpro_switchqueue(void)
194 1.1.6.1 ad {
195 1.1.6.1 ad
196 1.1.6.1 ad coldqidx = 1 - coldqidx;
197 1.1.6.1 ad }
198 1.1.6.1 ad
199 1.1.6.1 ad #endif /* !defined(LISTQ) */
200 1.1.6.1 ad
201 1.1.6.1 ad static struct clockpro_state clockpro;
202 1.1.6.1 ad static struct clockpro_scanstate {
203 1.1.6.1 ad int ss_nscanned;
204 1.1.6.1 ad } scanstate;
205 1.1.6.1 ad
206 1.1.6.1 ad /* ---------------------------------------- */
207 1.1.6.1 ad
208 1.1.6.1 ad static void
209 1.1.6.1 ad pageq_init(pageq_t *q)
210 1.1.6.1 ad {
211 1.1.6.1 ad
212 1.1.6.1 ad TAILQ_INIT(&q->q_q);
213 1.1.6.1 ad q->q_len = 0;
214 1.1.6.1 ad }
215 1.1.6.1 ad
216 1.1.6.1 ad static int
217 1.1.6.1 ad pageq_len(const pageq_t *q)
218 1.1.6.1 ad {
219 1.1.6.1 ad
220 1.1.6.1 ad return q->q_len;
221 1.1.6.1 ad }
222 1.1.6.1 ad
223 1.1.6.1 ad static struct vm_page *
224 1.1.6.1 ad pageq_first(const pageq_t *q)
225 1.1.6.1 ad {
226 1.1.6.1 ad
227 1.1.6.1 ad return TAILQ_FIRST(&q->q_q);
228 1.1.6.1 ad }
229 1.1.6.1 ad
230 1.1.6.1 ad static void
231 1.1.6.1 ad pageq_insert_tail(pageq_t *q, struct vm_page *pg)
232 1.1.6.1 ad {
233 1.1.6.1 ad
234 1.1.6.1 ad TAILQ_INSERT_TAIL(&q->q_q, pg, pageq);
235 1.1.6.1 ad q->q_len++;
236 1.1.6.1 ad }
237 1.1.6.1 ad
238 1.1.6.1 ad static void
239 1.1.6.1 ad pageq_insert_head(pageq_t *q, struct vm_page *pg)
240 1.1.6.1 ad {
241 1.1.6.1 ad
242 1.1.6.1 ad TAILQ_INSERT_HEAD(&q->q_q, pg, pageq);
243 1.1.6.1 ad q->q_len++;
244 1.1.6.1 ad }
245 1.1.6.1 ad
246 1.1.6.1 ad static void
247 1.1.6.1 ad pageq_remove(pageq_t *q, struct vm_page *pg)
248 1.1.6.1 ad {
249 1.1.6.1 ad
250 1.1.6.1 ad #if 1
251 1.1.6.1 ad KASSERT(clockpro_queue(&clockpro, clockpro_getq(pg)) == q);
252 1.1.6.1 ad #endif
253 1.1.6.1 ad KASSERT(q->q_len > 0);
254 1.1.6.1 ad TAILQ_REMOVE(&q->q_q, pg, pageq);
255 1.1.6.1 ad q->q_len--;
256 1.1.6.1 ad }
257 1.1.6.1 ad
258 1.1.6.1 ad static struct vm_page *
259 1.1.6.1 ad pageq_remove_head(pageq_t *q)
260 1.1.6.1 ad {
261 1.1.6.1 ad struct vm_page *pg;
262 1.1.6.1 ad
263 1.1.6.1 ad pg = TAILQ_FIRST(&q->q_q);
264 1.1.6.1 ad if (pg == NULL) {
265 1.1.6.1 ad KASSERT(q->q_len == 0);
266 1.1.6.1 ad return NULL;
267 1.1.6.1 ad }
268 1.1.6.1 ad pageq_remove(q, pg);
269 1.1.6.1 ad return pg;
270 1.1.6.1 ad }
271 1.1.6.1 ad
272 1.1.6.1 ad /* ---------------------------------------- */
273 1.1.6.1 ad
274 1.1.6.1 ad static void
275 1.1.6.1 ad clockpro_insert_tail(struct clockpro_state *s, int qidx, struct vm_page *pg)
276 1.1.6.1 ad {
277 1.1.6.1 ad pageq_t *q = clockpro_queue(s, qidx);
278 1.1.6.1 ad
279 1.1.6.1 ad clockpro_setq(pg, qidx);
280 1.1.6.1 ad pageq_insert_tail(q, pg);
281 1.1.6.1 ad }
282 1.1.6.1 ad
283 1.1.6.1 ad static void
284 1.1.6.1 ad clockpro_insert_head(struct clockpro_state *s, int qidx, struct vm_page *pg)
285 1.1.6.1 ad {
286 1.1.6.1 ad pageq_t *q = clockpro_queue(s, qidx);
287 1.1.6.1 ad
288 1.1.6.1 ad clockpro_setq(pg, qidx);
289 1.1.6.1 ad pageq_insert_head(q, pg);
290 1.1.6.1 ad }
291 1.1.6.1 ad
292 1.1.6.1 ad /* ---------------------------------------- */
293 1.1.6.1 ad
294 1.1.6.1 ad typedef uint32_t nonres_cookie_t;
295 1.1.6.1 ad #define NONRES_COOKIE_INVAL 0
296 1.1.6.1 ad
297 1.1.6.1 ad typedef uintptr_t objid_t;
298 1.1.6.1 ad
299 1.1.6.1 ad /*
300 1.1.6.1 ad * XXX maybe these hash functions need reconsideration,
301 1.1.6.1 ad * given that hash distribution is critical here.
302 1.1.6.1 ad */
303 1.1.6.1 ad
304 1.1.6.1 ad static uint32_t
305 1.1.6.1 ad pageidentityhash1(objid_t obj, off_t idx)
306 1.1.6.1 ad {
307 1.1.6.1 ad uint32_t hash = HASH32_BUF_INIT;
308 1.1.6.1 ad
309 1.1.6.1 ad #if 1
310 1.1.6.1 ad hash = hash32_buf(&idx, sizeof(idx), hash);
311 1.1.6.1 ad hash = hash32_buf(&obj, sizeof(obj), hash);
312 1.1.6.1 ad #else
313 1.1.6.1 ad hash = hash32_buf(&obj, sizeof(obj), hash);
314 1.1.6.1 ad hash = hash32_buf(&idx, sizeof(idx), hash);
315 1.1.6.1 ad #endif
316 1.1.6.1 ad return hash;
317 1.1.6.1 ad }
318 1.1.6.1 ad
319 1.1.6.1 ad static uint32_t
320 1.1.6.1 ad pageidentityhash2(objid_t obj, off_t idx)
321 1.1.6.1 ad {
322 1.1.6.1 ad uint32_t hash = HASH32_BUF_INIT;
323 1.1.6.1 ad
324 1.1.6.1 ad hash = hash32_buf(&obj, sizeof(obj), hash);
325 1.1.6.1 ad hash = hash32_buf(&idx, sizeof(idx), hash);
326 1.1.6.1 ad return hash;
327 1.1.6.1 ad }
328 1.1.6.1 ad
329 1.1.6.1 ad static nonres_cookie_t
330 1.1.6.1 ad calccookie(objid_t obj, off_t idx)
331 1.1.6.1 ad {
332 1.1.6.1 ad uint32_t hash = pageidentityhash2(obj, idx);
333 1.1.6.1 ad nonres_cookie_t cookie = hash;
334 1.1.6.1 ad
335 1.1.6.1 ad if (__predict_false(cookie == NONRES_COOKIE_INVAL)) {
336 1.1.6.1 ad cookie++; /* XXX */
337 1.1.6.1 ad }
338 1.1.6.1 ad return cookie;
339 1.1.6.1 ad }
340 1.1.6.1 ad
341 1.1.6.1 ad #define BUCKETSIZE 14
342 1.1.6.1 ad struct bucket {
343 1.1.6.1 ad int cycle;
344 1.1.6.1 ad int cur;
345 1.1.6.1 ad nonres_cookie_t pages[BUCKETSIZE];
346 1.1.6.1 ad };
347 1.1.6.1 ad static int cycle_target;
348 1.1.6.1 ad static int cycle_target_frac;
349 1.1.6.1 ad
350 1.1.6.1 ad static struct bucket static_bucket;
351 1.1.6.1 ad static struct bucket *buckets = &static_bucket;
352 1.1.6.1 ad static size_t hashsize = 1;
353 1.1.6.1 ad
354 1.1.6.1 ad static int coldadj;
355 1.1.6.1 ad #define COLDTARGET_ADJ(d) coldadj += (d)
356 1.1.6.1 ad
357 1.1.6.1 ad #if defined(PDSIM)
358 1.1.6.1 ad
359 1.1.6.1 ad static void *
360 1.1.6.1 ad clockpro_hashalloc(int n)
361 1.1.6.1 ad {
362 1.1.6.1 ad size_t allocsz = sizeof(*buckets) * n;
363 1.1.6.1 ad
364 1.1.6.1 ad return malloc(allocsz);
365 1.1.6.1 ad }
366 1.1.6.1 ad
367 1.1.6.1 ad static void
368 1.1.6.1 ad clockpro_hashfree(void *p, int n)
369 1.1.6.1 ad {
370 1.1.6.1 ad
371 1.1.6.1 ad free(p);
372 1.1.6.1 ad }
373 1.1.6.1 ad
374 1.1.6.1 ad #else /* defined(PDSIM) */
375 1.1.6.1 ad
376 1.1.6.1 ad static void *
377 1.1.6.1 ad clockpro_hashalloc(int n)
378 1.1.6.1 ad {
379 1.1.6.1 ad size_t allocsz = round_page(sizeof(*buckets) * n);
380 1.1.6.1 ad
381 1.1.6.1 ad return (void *)uvm_km_alloc(kernel_map, allocsz, 0, UVM_KMF_WIRED);
382 1.1.6.1 ad }
383 1.1.6.1 ad
384 1.1.6.1 ad static void
385 1.1.6.1 ad clockpro_hashfree(void *p, int n)
386 1.1.6.1 ad {
387 1.1.6.1 ad size_t allocsz = round_page(sizeof(*buckets) * n);
388 1.1.6.1 ad
389 1.1.6.1 ad uvm_km_free(kernel_map, (vaddr_t)p, allocsz, UVM_KMF_WIRED);
390 1.1.6.1 ad }
391 1.1.6.1 ad
392 1.1.6.1 ad #endif /* defined(PDSIM) */
393 1.1.6.1 ad
394 1.1.6.1 ad static void
395 1.1.6.1 ad clockpro_hashinit(uint64_t n)
396 1.1.6.1 ad {
397 1.1.6.1 ad struct bucket *newbuckets;
398 1.1.6.1 ad struct bucket *oldbuckets;
399 1.1.6.1 ad size_t sz;
400 1.1.6.1 ad size_t oldsz;
401 1.1.6.1 ad int i;
402 1.1.6.1 ad
403 1.1.6.1 ad sz = howmany(n, BUCKETSIZE);
404 1.1.6.1 ad sz *= clockpro_hashfactor;
405 1.1.6.1 ad newbuckets = clockpro_hashalloc(sz);
406 1.1.6.1 ad if (newbuckets == NULL) {
407 1.1.6.1 ad panic("%s: allocation failure", __func__);
408 1.1.6.1 ad }
409 1.1.6.1 ad for (i = 0; i < sz; i++) {
410 1.1.6.1 ad struct bucket *b = &newbuckets[i];
411 1.1.6.1 ad int j;
412 1.1.6.1 ad
413 1.1.6.1 ad b->cycle = cycle_target;
414 1.1.6.1 ad b->cur = 0;
415 1.1.6.1 ad for (j = 0; j < BUCKETSIZE; j++) {
416 1.1.6.1 ad b->pages[j] = NONRES_COOKIE_INVAL;
417 1.1.6.1 ad }
418 1.1.6.1 ad }
419 1.1.6.1 ad /* XXX lock */
420 1.1.6.1 ad oldbuckets = buckets;
421 1.1.6.1 ad oldsz = hashsize;
422 1.1.6.1 ad buckets = newbuckets;
423 1.1.6.1 ad hashsize = sz;
424 1.1.6.1 ad /* XXX unlock */
425 1.1.6.1 ad if (oldbuckets != &static_bucket) {
426 1.1.6.1 ad clockpro_hashfree(oldbuckets, oldsz);
427 1.1.6.1 ad }
428 1.1.6.1 ad }
429 1.1.6.1 ad
430 1.1.6.1 ad static struct bucket *
431 1.1.6.1 ad nonresident_getbucket(objid_t obj, off_t idx)
432 1.1.6.1 ad {
433 1.1.6.1 ad uint32_t hash;
434 1.1.6.1 ad
435 1.1.6.1 ad hash = pageidentityhash1(obj, idx);
436 1.1.6.1 ad return &buckets[hash % hashsize];
437 1.1.6.1 ad }
438 1.1.6.1 ad
439 1.1.6.1 ad static void
440 1.1.6.1 ad nonresident_rotate(struct bucket *b)
441 1.1.6.1 ad {
442 1.1.6.1 ad
443 1.1.6.1 ad while (b->cycle - cycle_target < 0) {
444 1.1.6.1 ad if (b->pages[b->cur] != NONRES_COOKIE_INVAL) {
445 1.1.6.1 ad PDPOL_EVCNT_INCR(nreshandhot);
446 1.1.6.1 ad COLDTARGET_ADJ(-1);
447 1.1.6.1 ad }
448 1.1.6.1 ad b->pages[b->cur] = NONRES_COOKIE_INVAL;
449 1.1.6.1 ad b->cur = (b->cur + 1) % BUCKETSIZE;
450 1.1.6.1 ad b->cycle++;
451 1.1.6.1 ad }
452 1.1.6.1 ad }
453 1.1.6.1 ad
454 1.1.6.1 ad static boolean_t
455 1.1.6.1 ad nonresident_lookupremove(objid_t obj, off_t idx)
456 1.1.6.1 ad {
457 1.1.6.1 ad struct bucket *b = nonresident_getbucket(obj, idx);
458 1.1.6.1 ad nonres_cookie_t cookie = calccookie(obj, idx);
459 1.1.6.1 ad int i;
460 1.1.6.1 ad
461 1.1.6.1 ad nonresident_rotate(b);
462 1.1.6.1 ad for (i = 0; i < BUCKETSIZE; i++) {
463 1.1.6.1 ad if (b->pages[i] == cookie) {
464 1.1.6.1 ad b->pages[i] = NONRES_COOKIE_INVAL;
465 1.1.6.1 ad return TRUE;
466 1.1.6.1 ad }
467 1.1.6.1 ad }
468 1.1.6.1 ad return FALSE;
469 1.1.6.1 ad }
470 1.1.6.1 ad
471 1.1.6.1 ad static objid_t
472 1.1.6.1 ad pageobj(struct vm_page *pg)
473 1.1.6.1 ad {
474 1.1.6.1 ad const void *obj;
475 1.1.6.1 ad
476 1.1.6.1 ad /*
477 1.1.6.1 ad * XXX object pointer is often freed and reused for unrelated object.
478 1.1.6.1 ad * for vnodes, it would be better to use something like
479 1.1.6.1 ad * a hash of fsid/fileid/generation.
480 1.1.6.1 ad */
481 1.1.6.1 ad
482 1.1.6.1 ad obj = pg->uobject;
483 1.1.6.1 ad if (obj == NULL) {
484 1.1.6.1 ad obj = pg->uanon;
485 1.1.6.1 ad KASSERT(obj != NULL);
486 1.1.6.1 ad KASSERT(pg->offset == 0);
487 1.1.6.1 ad }
488 1.1.6.1 ad
489 1.1.6.1 ad return (objid_t)obj;
490 1.1.6.1 ad }
491 1.1.6.1 ad
492 1.1.6.1 ad static off_t
493 1.1.6.1 ad pageidx(struct vm_page *pg)
494 1.1.6.1 ad {
495 1.1.6.1 ad
496 1.1.6.1 ad KASSERT((pg->offset & PAGE_MASK) == 0);
497 1.1.6.1 ad return pg->offset >> PAGE_SHIFT;
498 1.1.6.1 ad }
499 1.1.6.1 ad
500 1.1.6.1 ad static boolean_t
501 1.1.6.1 ad nonresident_pagelookupremove(struct vm_page *pg)
502 1.1.6.1 ad {
503 1.1.6.1 ad boolean_t found = nonresident_lookupremove(pageobj(pg), pageidx(pg));
504 1.1.6.1 ad
505 1.1.6.1 ad PDPOL_EVCNT_INCR(nreslookup);
506 1.1.6.1 ad if (found) {
507 1.1.6.1 ad if (pg->uobject) {
508 1.1.6.1 ad PDPOL_EVCNT_INCR(nresfoundobj);
509 1.1.6.1 ad } else {
510 1.1.6.1 ad PDPOL_EVCNT_INCR(nresfoundanon);
511 1.1.6.1 ad }
512 1.1.6.1 ad }
513 1.1.6.1 ad return found;
514 1.1.6.1 ad }
515 1.1.6.1 ad
516 1.1.6.1 ad static void
517 1.1.6.1 ad nonresident_pagerecord(struct vm_page *pg)
518 1.1.6.1 ad {
519 1.1.6.1 ad objid_t obj = pageobj(pg);
520 1.1.6.1 ad off_t idx = pageidx(pg);
521 1.1.6.1 ad struct bucket *b = nonresident_getbucket(obj, idx);
522 1.1.6.1 ad nonres_cookie_t cookie = calccookie(obj, idx);
523 1.1.6.1 ad
524 1.1.6.1 ad #if defined(DEBUG)
525 1.1.6.1 ad int i;
526 1.1.6.1 ad
527 1.1.6.1 ad for (i = 0; i < BUCKETSIZE; i++) {
528 1.1.6.1 ad if (b->pages[i] == cookie) {
529 1.1.6.1 ad PDPOL_EVCNT_INCR(nresconflict);
530 1.1.6.1 ad }
531 1.1.6.1 ad }
532 1.1.6.1 ad #endif /* defined(DEBUG) */
533 1.1.6.1 ad
534 1.1.6.1 ad if (pg->uobject) {
535 1.1.6.1 ad PDPOL_EVCNT_INCR(nresrecordobj);
536 1.1.6.1 ad } else {
537 1.1.6.1 ad PDPOL_EVCNT_INCR(nresrecordanon);
538 1.1.6.1 ad }
539 1.1.6.1 ad nonresident_rotate(b);
540 1.1.6.1 ad if (b->pages[b->cur] != NONRES_COOKIE_INVAL) {
541 1.1.6.1 ad PDPOL_EVCNT_INCR(nresoverwritten);
542 1.1.6.1 ad COLDTARGET_ADJ(-1);
543 1.1.6.1 ad }
544 1.1.6.1 ad b->pages[b->cur] = cookie;
545 1.1.6.1 ad b->cur = (b->cur + 1) % BUCKETSIZE;
546 1.1.6.1 ad }
547 1.1.6.1 ad
548 1.1.6.1 ad /* ---------------------------------------- */
549 1.1.6.1 ad
550 1.1.6.1 ad #if defined(CLOCKPRO_DEBUG)
551 1.1.6.1 ad static void
552 1.1.6.1 ad check_sanity(void)
553 1.1.6.1 ad {
554 1.1.6.1 ad }
555 1.1.6.1 ad #else /* defined(CLOCKPRO_DEBUG) */
556 1.1.6.1 ad #define check_sanity() /* nothing */
557 1.1.6.1 ad #endif /* defined(CLOCKPRO_DEBUG) */
558 1.1.6.1 ad
559 1.1.6.1 ad static void
560 1.1.6.1 ad clockpro_reinit(void)
561 1.1.6.1 ad {
562 1.1.6.1 ad
563 1.1.6.1 ad clockpro_hashinit(uvmexp.npages);
564 1.1.6.1 ad }
565 1.1.6.1 ad
566 1.1.6.1 ad static void
567 1.1.6.1 ad clockpro_init(void)
568 1.1.6.1 ad {
569 1.1.6.1 ad struct clockpro_state *s = &clockpro;
570 1.1.6.1 ad int i;
571 1.1.6.1 ad
572 1.1.6.1 ad for (i = 0; i < CLOCKPRO_NQUEUE; i++) {
573 1.1.6.1 ad pageq_init(&s->s_q[i]);
574 1.1.6.1 ad }
575 1.1.6.1 ad s->s_newqlenmax = 1;
576 1.1.6.1 ad s->s_coldtarget = 1;
577 1.1.6.1 ad uvm_pctparam_init(&s->s_coldtargetpct, CLOCKPRO_COLDPCT, NULL);
578 1.1.6.1 ad }
579 1.1.6.1 ad
580 1.1.6.1 ad static void
581 1.1.6.1 ad clockpro_tune(void)
582 1.1.6.1 ad {
583 1.1.6.1 ad struct clockpro_state *s = &clockpro;
584 1.1.6.1 ad int coldtarget;
585 1.1.6.1 ad
586 1.1.6.1 ad #if defined(ADAPTIVE)
587 1.1.6.1 ad int coldmax = s->s_npages * CLOCKPRO_COLDPCTMAX / 100;
588 1.1.6.1 ad int coldmin = 1;
589 1.1.6.1 ad
590 1.1.6.1 ad coldtarget = s->s_coldtarget;
591 1.1.6.1 ad if (coldtarget + coldadj < coldmin) {
592 1.1.6.1 ad coldadj = coldmin - coldtarget;
593 1.1.6.1 ad } else if (coldtarget + coldadj > coldmax) {
594 1.1.6.1 ad coldadj = coldmax - coldtarget;
595 1.1.6.1 ad }
596 1.1.6.1 ad coldtarget += coldadj;
597 1.1.6.1 ad #else /* defined(ADAPTIVE) */
598 1.1.6.1 ad coldtarget = UVM_PCTPARAM_APPLY(&s->s_coldtargetpct, s->s_npages);
599 1.1.6.1 ad if (coldtarget < 1) {
600 1.1.6.1 ad coldtarget = 1;
601 1.1.6.1 ad }
602 1.1.6.1 ad #endif /* defined(ADAPTIVE) */
603 1.1.6.1 ad
604 1.1.6.1 ad s->s_coldtarget = coldtarget;
605 1.1.6.1 ad s->s_newqlenmax = coldtarget / 4;
606 1.1.6.1 ad if (s->s_newqlenmax < CLOCKPRO_NEWQMIN) {
607 1.1.6.1 ad s->s_newqlenmax = CLOCKPRO_NEWQMIN;
608 1.1.6.1 ad }
609 1.1.6.1 ad }
610 1.1.6.1 ad
611 1.1.6.1 ad static void
612 1.1.6.1 ad clockpro_movereferencebit(struct vm_page *pg)
613 1.1.6.1 ad {
614 1.1.6.1 ad boolean_t referenced;
615 1.1.6.1 ad
616 1.1.6.1 ad referenced = pmap_clear_reference(pg);
617 1.1.6.1 ad if (referenced) {
618 1.1.6.1 ad pg->pqflags |= PQ_REFERENCED;
619 1.1.6.1 ad }
620 1.1.6.1 ad }
621 1.1.6.1 ad
622 1.1.6.1 ad static void
623 1.1.6.1 ad clockpro_clearreferencebit(struct vm_page *pg)
624 1.1.6.1 ad {
625 1.1.6.1 ad
626 1.1.6.1 ad clockpro_movereferencebit(pg);
627 1.1.6.1 ad pg->pqflags &= ~PQ_REFERENCED;
628 1.1.6.1 ad }
629 1.1.6.1 ad
630 1.1.6.1 ad static void
631 1.1.6.1 ad clockpro___newqrotate(int len)
632 1.1.6.1 ad {
633 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
634 1.1.6.1 ad pageq_t * const newq = clockpro_queue(s, CLOCKPRO_NEWQ);
635 1.1.6.1 ad struct vm_page *pg;
636 1.1.6.1 ad
637 1.1.6.1 ad while (pageq_len(newq) > len) {
638 1.1.6.1 ad pg = pageq_remove_head(newq);
639 1.1.6.1 ad KASSERT(pg != NULL);
640 1.1.6.1 ad KASSERT(clockpro_getq(pg) == CLOCKPRO_NEWQ);
641 1.1.6.1 ad if ((pg->pqflags & PQ_INITIALREF) != 0) {
642 1.1.6.1 ad clockpro_clearreferencebit(pg);
643 1.1.6.1 ad pg->pqflags &= ~PQ_INITIALREF;
644 1.1.6.1 ad }
645 1.1.6.1 ad /* place at the list head */
646 1.1.6.1 ad clockpro_insert_tail(s, CLOCKPRO_COLDQ, pg);
647 1.1.6.1 ad }
648 1.1.6.1 ad }
649 1.1.6.1 ad
650 1.1.6.1 ad static void
651 1.1.6.1 ad clockpro_newqrotate(void)
652 1.1.6.1 ad {
653 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
654 1.1.6.1 ad
655 1.1.6.1 ad check_sanity();
656 1.1.6.1 ad clockpro___newqrotate(s->s_newqlenmax);
657 1.1.6.1 ad check_sanity();
658 1.1.6.1 ad }
659 1.1.6.1 ad
660 1.1.6.1 ad static void
661 1.1.6.1 ad clockpro_newqflush(int n)
662 1.1.6.1 ad {
663 1.1.6.1 ad
664 1.1.6.1 ad check_sanity();
665 1.1.6.1 ad clockpro___newqrotate(n);
666 1.1.6.1 ad check_sanity();
667 1.1.6.1 ad }
668 1.1.6.1 ad
669 1.1.6.1 ad static void
670 1.1.6.1 ad clockpro_newqflushone(void)
671 1.1.6.1 ad {
672 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
673 1.1.6.1 ad
674 1.1.6.1 ad clockpro_newqflush(
675 1.1.6.1 ad MAX(pageq_len(clockpro_queue(s, CLOCKPRO_NEWQ)) - 1, 0));
676 1.1.6.1 ad }
677 1.1.6.1 ad
678 1.1.6.1 ad /*
679 1.1.6.1 ad * our "tail" is called "list-head" in the paper.
680 1.1.6.1 ad */
681 1.1.6.1 ad
682 1.1.6.1 ad static void
683 1.1.6.1 ad clockpro___enqueuetail(struct vm_page *pg)
684 1.1.6.1 ad {
685 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
686 1.1.6.1 ad
687 1.1.6.1 ad KASSERT(clockpro_getq(pg) == CLOCKPRO_NOQUEUE);
688 1.1.6.1 ad
689 1.1.6.1 ad check_sanity();
690 1.1.6.1 ad #if !defined(USEONCE2)
691 1.1.6.1 ad clockpro_insert_tail(s, CLOCKPRO_NEWQ, pg);
692 1.1.6.1 ad clockpro_newqrotate();
693 1.1.6.1 ad #else /* !defined(USEONCE2) */
694 1.1.6.1 ad #if defined(LISTQ)
695 1.1.6.1 ad KASSERT((pg->pqflags & PQ_REFERENCED) == 0);
696 1.1.6.1 ad #endif /* defined(LISTQ) */
697 1.1.6.1 ad clockpro_insert_tail(s, CLOCKPRO_COLDQ, pg);
698 1.1.6.1 ad #endif /* !defined(USEONCE2) */
699 1.1.6.1 ad check_sanity();
700 1.1.6.1 ad }
701 1.1.6.1 ad
702 1.1.6.1 ad static void
703 1.1.6.1 ad clockpro_pageenqueue(struct vm_page *pg)
704 1.1.6.1 ad {
705 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
706 1.1.6.1 ad boolean_t hot;
707 1.1.6.1 ad boolean_t speculative = (pg->pqflags & PQ_SPECULATIVE) != 0; /* XXX */
708 1.1.6.1 ad
709 1.1.6.1 ad KASSERT((~pg->pqflags & (PQ_INITIALREF|PQ_SPECULATIVE)) != 0);
710 1.1.6.1 ad UVM_LOCK_ASSERT_PAGEQ();
711 1.1.6.1 ad check_sanity();
712 1.1.6.1 ad KASSERT(clockpro_getq(pg) == CLOCKPRO_NOQUEUE);
713 1.1.6.1 ad s->s_npages++;
714 1.1.6.1 ad pg->pqflags &= ~(PQ_HOT|PQ_TEST);
715 1.1.6.1 ad if (speculative) {
716 1.1.6.1 ad hot = FALSE;
717 1.1.6.1 ad PDPOL_EVCNT_INCR(speculativeenqueue);
718 1.1.6.1 ad } else {
719 1.1.6.1 ad hot = nonresident_pagelookupremove(pg);
720 1.1.6.1 ad if (hot) {
721 1.1.6.1 ad COLDTARGET_ADJ(1);
722 1.1.6.1 ad }
723 1.1.6.1 ad }
724 1.1.6.1 ad
725 1.1.6.1 ad /*
726 1.1.6.1 ad * consider mmap'ed file:
727 1.1.6.1 ad *
728 1.1.6.1 ad * - read-ahead enqueues a page.
729 1.1.6.1 ad *
730 1.1.6.1 ad * - on the following read-ahead hit, the fault handler activates it.
731 1.1.6.1 ad *
732 1.1.6.1 ad * - finally, the userland code which caused the above fault
733 1.1.6.1 ad * actually accesses the page. it makes its reference bit set.
734 1.1.6.1 ad *
735 1.1.6.1 ad * we want to count the above as a single access, rather than
736 1.1.6.1 ad * three accesses with short reuse distances.
737 1.1.6.1 ad */
738 1.1.6.1 ad
739 1.1.6.1 ad #if defined(USEONCE2)
740 1.1.6.1 ad pg->pqflags &= ~PQ_INITIALREF;
741 1.1.6.1 ad if (hot) {
742 1.1.6.1 ad pg->pqflags |= PQ_TEST;
743 1.1.6.1 ad }
744 1.1.6.1 ad s->s_ncold++;
745 1.1.6.1 ad clockpro_clearreferencebit(pg);
746 1.1.6.1 ad clockpro___enqueuetail(pg);
747 1.1.6.1 ad #else /* defined(USEONCE2) */
748 1.1.6.1 ad if (speculative) {
749 1.1.6.1 ad s->s_ncold++;
750 1.1.6.1 ad } else if (hot) {
751 1.1.6.1 ad pg->pqflags |= PQ_HOT;
752 1.1.6.1 ad } else {
753 1.1.6.1 ad pg->pqflags |= PQ_TEST;
754 1.1.6.1 ad s->s_ncold++;
755 1.1.6.1 ad }
756 1.1.6.1 ad clockpro___enqueuetail(pg);
757 1.1.6.1 ad #endif /* defined(USEONCE2) */
758 1.1.6.1 ad KASSERT(s->s_ncold <= s->s_npages);
759 1.1.6.1 ad }
760 1.1.6.1 ad
761 1.1.6.1 ad static pageq_t *
762 1.1.6.1 ad clockpro_pagequeue(struct vm_page *pg)
763 1.1.6.1 ad {
764 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
765 1.1.6.1 ad int qidx;
766 1.1.6.1 ad
767 1.1.6.1 ad qidx = clockpro_getq(pg);
768 1.1.6.1 ad KASSERT(qidx != CLOCKPRO_NOQUEUE);
769 1.1.6.1 ad
770 1.1.6.1 ad return clockpro_queue(s, qidx);
771 1.1.6.1 ad }
772 1.1.6.1 ad
773 1.1.6.1 ad static void
774 1.1.6.1 ad clockpro_pagedequeue(struct vm_page *pg)
775 1.1.6.1 ad {
776 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
777 1.1.6.1 ad pageq_t *q;
778 1.1.6.1 ad
779 1.1.6.1 ad KASSERT(s->s_npages > 0);
780 1.1.6.1 ad check_sanity();
781 1.1.6.1 ad q = clockpro_pagequeue(pg);
782 1.1.6.1 ad pageq_remove(q, pg);
783 1.1.6.1 ad check_sanity();
784 1.1.6.1 ad clockpro_setq(pg, CLOCKPRO_NOQUEUE);
785 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) == 0) {
786 1.1.6.1 ad KASSERT(s->s_ncold > 0);
787 1.1.6.1 ad s->s_ncold--;
788 1.1.6.1 ad }
789 1.1.6.1 ad KASSERT(s->s_npages > 0);
790 1.1.6.1 ad s->s_npages--;
791 1.1.6.1 ad check_sanity();
792 1.1.6.1 ad }
793 1.1.6.1 ad
794 1.1.6.1 ad static void
795 1.1.6.1 ad clockpro_pagerequeue(struct vm_page *pg)
796 1.1.6.1 ad {
797 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
798 1.1.6.1 ad int qidx;
799 1.1.6.1 ad
800 1.1.6.1 ad qidx = clockpro_getq(pg);
801 1.1.6.1 ad KASSERT(qidx == CLOCKPRO_HOTQ || qidx == CLOCKPRO_COLDQ);
802 1.1.6.1 ad pageq_remove(clockpro_queue(s, qidx), pg);
803 1.1.6.1 ad check_sanity();
804 1.1.6.1 ad clockpro_setq(pg, CLOCKPRO_NOQUEUE);
805 1.1.6.1 ad
806 1.1.6.1 ad clockpro___enqueuetail(pg);
807 1.1.6.1 ad }
808 1.1.6.1 ad
809 1.1.6.1 ad static void
810 1.1.6.1 ad handhot_endtest(struct vm_page *pg)
811 1.1.6.1 ad {
812 1.1.6.1 ad
813 1.1.6.1 ad KASSERT((pg->pqflags & PQ_HOT) == 0);
814 1.1.6.1 ad if ((pg->pqflags & PQ_TEST) != 0) {
815 1.1.6.1 ad PDPOL_EVCNT_INCR(hhotcoldtest);
816 1.1.6.1 ad COLDTARGET_ADJ(-1);
817 1.1.6.1 ad pg->pqflags &= ~PQ_TEST;
818 1.1.6.1 ad } else {
819 1.1.6.1 ad PDPOL_EVCNT_INCR(hhotcold);
820 1.1.6.1 ad }
821 1.1.6.1 ad }
822 1.1.6.1 ad
823 1.1.6.1 ad static void
824 1.1.6.1 ad handhot_advance(void)
825 1.1.6.1 ad {
826 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
827 1.1.6.1 ad struct vm_page *pg;
828 1.1.6.1 ad pageq_t *hotq;
829 1.1.6.1 ad int hotqlen;
830 1.1.6.1 ad
831 1.1.6.1 ad clockpro_tune();
832 1.1.6.1 ad
833 1.1.6.1 ad dump("hot called");
834 1.1.6.1 ad if (s->s_ncold >= s->s_coldtarget) {
835 1.1.6.1 ad return;
836 1.1.6.1 ad }
837 1.1.6.1 ad hotq = clockpro_queue(s, CLOCKPRO_HOTQ);
838 1.1.6.1 ad again:
839 1.1.6.1 ad pg = pageq_first(hotq);
840 1.1.6.1 ad if (pg == NULL) {
841 1.1.6.1 ad DPRINTF("%s: HHOT TAKEOVER\n", __func__);
842 1.1.6.1 ad dump("hhottakeover");
843 1.1.6.1 ad PDPOL_EVCNT_INCR(hhottakeover);
844 1.1.6.1 ad #if defined(LISTQ)
845 1.1.6.1 ad while (/* CONSTCOND */ 1) {
846 1.1.6.1 ad pageq_t *coldq = clockpro_queue(s, CLOCKPRO_COLDQ);
847 1.1.6.1 ad
848 1.1.6.1 ad pg = pageq_first(coldq);
849 1.1.6.1 ad if (pg == NULL) {
850 1.1.6.1 ad clockpro_newqflushone();
851 1.1.6.1 ad pg = pageq_first(coldq);
852 1.1.6.1 ad if (pg == NULL) {
853 1.1.6.1 ad WARN("hhot: no page?\n");
854 1.1.6.1 ad return;
855 1.1.6.1 ad }
856 1.1.6.1 ad }
857 1.1.6.1 ad KASSERT(clockpro_pagequeue(pg) == coldq);
858 1.1.6.1 ad pageq_remove(coldq, pg);
859 1.1.6.1 ad check_sanity();
860 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) == 0) {
861 1.1.6.1 ad handhot_endtest(pg);
862 1.1.6.1 ad clockpro_insert_tail(s, CLOCKPRO_LISTQ, pg);
863 1.1.6.1 ad } else {
864 1.1.6.1 ad clockpro_insert_head(s, CLOCKPRO_HOTQ, pg);
865 1.1.6.1 ad break;
866 1.1.6.1 ad }
867 1.1.6.1 ad }
868 1.1.6.1 ad #else /* defined(LISTQ) */
869 1.1.6.1 ad clockpro_newqflush(0); /* XXX XXX */
870 1.1.6.1 ad clockpro_switchqueue();
871 1.1.6.1 ad hotq = clockpro_queue(s, CLOCKPRO_HOTQ);
872 1.1.6.1 ad goto again;
873 1.1.6.1 ad #endif /* defined(LISTQ) */
874 1.1.6.1 ad }
875 1.1.6.1 ad
876 1.1.6.1 ad KASSERT(clockpro_pagequeue(pg) == hotq);
877 1.1.6.1 ad
878 1.1.6.1 ad /*
879 1.1.6.1 ad * terminate test period of nonresident pages by cycling them.
880 1.1.6.1 ad */
881 1.1.6.1 ad
882 1.1.6.1 ad cycle_target_frac += BUCKETSIZE;
883 1.1.6.1 ad hotqlen = pageq_len(hotq);
884 1.1.6.1 ad while (cycle_target_frac >= hotqlen) {
885 1.1.6.1 ad cycle_target++;
886 1.1.6.1 ad cycle_target_frac -= hotqlen;
887 1.1.6.1 ad }
888 1.1.6.1 ad
889 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) == 0) {
890 1.1.6.1 ad #if defined(LISTQ)
891 1.1.6.1 ad panic("cold page in hotq: %p", pg);
892 1.1.6.1 ad #else /* defined(LISTQ) */
893 1.1.6.1 ad handhot_endtest(pg);
894 1.1.6.1 ad goto next;
895 1.1.6.1 ad #endif /* defined(LISTQ) */
896 1.1.6.1 ad }
897 1.1.6.1 ad KASSERT((pg->pqflags & PQ_TEST) == 0);
898 1.1.6.1 ad KASSERT((pg->pqflags & PQ_INITIALREF) == 0);
899 1.1.6.1 ad KASSERT((pg->pqflags & PQ_SPECULATIVE) == 0);
900 1.1.6.1 ad
901 1.1.6.1 ad /*
902 1.1.6.1 ad * once we met our target,
903 1.1.6.1 ad * stop at a hot page so that no cold pages in test period
904 1.1.6.1 ad * have larger recency than any hot pages.
905 1.1.6.1 ad */
906 1.1.6.1 ad
907 1.1.6.1 ad if (s->s_ncold >= s->s_coldtarget) {
908 1.1.6.1 ad dump("hot done");
909 1.1.6.1 ad return;
910 1.1.6.1 ad }
911 1.1.6.1 ad clockpro_movereferencebit(pg);
912 1.1.6.1 ad if ((pg->pqflags & PQ_REFERENCED) == 0) {
913 1.1.6.1 ad PDPOL_EVCNT_INCR(hhotunref);
914 1.1.6.1 ad uvmexp.pddeact++;
915 1.1.6.1 ad pg->pqflags &= ~PQ_HOT;
916 1.1.6.1 ad clockpro.s_ncold++;
917 1.1.6.1 ad KASSERT(s->s_ncold <= s->s_npages);
918 1.1.6.1 ad } else {
919 1.1.6.1 ad PDPOL_EVCNT_INCR(hhotref);
920 1.1.6.1 ad }
921 1.1.6.1 ad pg->pqflags &= ~PQ_REFERENCED;
922 1.1.6.1 ad #if !defined(LISTQ)
923 1.1.6.1 ad next:
924 1.1.6.1 ad #endif /* !defined(LISTQ) */
925 1.1.6.1 ad clockpro_pagerequeue(pg);
926 1.1.6.1 ad dump("hot");
927 1.1.6.1 ad goto again;
928 1.1.6.1 ad }
929 1.1.6.1 ad
930 1.1.6.1 ad static struct vm_page *
931 1.1.6.1 ad handcold_advance(void)
932 1.1.6.1 ad {
933 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
934 1.1.6.1 ad struct vm_page *pg;
935 1.1.6.1 ad
936 1.1.6.1 ad for (;;) {
937 1.1.6.1 ad #if defined(LISTQ)
938 1.1.6.1 ad pageq_t *listq = clockpro_queue(s, CLOCKPRO_LISTQ);
939 1.1.6.1 ad #endif /* defined(LISTQ) */
940 1.1.6.1 ad pageq_t *coldq;
941 1.1.6.1 ad
942 1.1.6.1 ad clockpro_newqrotate();
943 1.1.6.1 ad handhot_advance();
944 1.1.6.1 ad #if defined(LISTQ)
945 1.1.6.1 ad pg = pageq_first(listq);
946 1.1.6.1 ad if (pg != NULL) {
947 1.1.6.1 ad KASSERT(clockpro_getq(pg) == CLOCKPRO_LISTQ);
948 1.1.6.1 ad KASSERT((pg->pqflags & PQ_TEST) == 0);
949 1.1.6.1 ad KASSERT((pg->pqflags & PQ_HOT) == 0);
950 1.1.6.1 ad KASSERT((pg->pqflags & PQ_INITIALREF) == 0);
951 1.1.6.1 ad pageq_remove(listq, pg);
952 1.1.6.1 ad check_sanity();
953 1.1.6.1 ad clockpro_insert_head(s, CLOCKPRO_COLDQ, pg); /* XXX */
954 1.1.6.1 ad goto gotcold;
955 1.1.6.1 ad }
956 1.1.6.1 ad #endif /* defined(LISTQ) */
957 1.1.6.1 ad check_sanity();
958 1.1.6.1 ad coldq = clockpro_queue(s, CLOCKPRO_COLDQ);
959 1.1.6.1 ad pg = pageq_first(coldq);
960 1.1.6.1 ad if (pg == NULL) {
961 1.1.6.1 ad clockpro_newqflushone();
962 1.1.6.1 ad pg = pageq_first(coldq);
963 1.1.6.1 ad }
964 1.1.6.1 ad if (pg == NULL) {
965 1.1.6.1 ad DPRINTF("%s: HCOLD TAKEOVER\n", __func__);
966 1.1.6.1 ad dump("hcoldtakeover");
967 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldtakeover);
968 1.1.6.1 ad KASSERT(
969 1.1.6.1 ad pageq_len(clockpro_queue(s, CLOCKPRO_NEWQ)) == 0);
970 1.1.6.1 ad #if defined(LISTQ)
971 1.1.6.1 ad KASSERT(
972 1.1.6.1 ad pageq_len(clockpro_queue(s, CLOCKPRO_HOTQ)) == 0);
973 1.1.6.1 ad #else /* defined(LISTQ) */
974 1.1.6.1 ad clockpro_switchqueue();
975 1.1.6.1 ad coldq = clockpro_queue(s, CLOCKPRO_COLDQ);
976 1.1.6.1 ad pg = pageq_first(coldq);
977 1.1.6.1 ad #endif /* defined(LISTQ) */
978 1.1.6.1 ad }
979 1.1.6.1 ad if (pg == NULL) {
980 1.1.6.1 ad WARN("hcold: no page?\n");
981 1.1.6.1 ad return NULL;
982 1.1.6.1 ad }
983 1.1.6.1 ad KASSERT((pg->pqflags & PQ_INITIALREF) == 0);
984 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) != 0) {
985 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldhot);
986 1.1.6.1 ad pageq_remove(coldq, pg);
987 1.1.6.1 ad clockpro_insert_tail(s, CLOCKPRO_HOTQ, pg);
988 1.1.6.1 ad check_sanity();
989 1.1.6.1 ad KASSERT((pg->pqflags & PQ_TEST) == 0);
990 1.1.6.1 ad uvmexp.pdscans++;
991 1.1.6.1 ad continue;
992 1.1.6.1 ad }
993 1.1.6.1 ad #if defined(LISTQ)
994 1.1.6.1 ad gotcold:
995 1.1.6.1 ad #endif /* defined(LISTQ) */
996 1.1.6.1 ad KASSERT((pg->pqflags & PQ_HOT) == 0);
997 1.1.6.1 ad uvmexp.pdscans++;
998 1.1.6.1 ad clockpro_movereferencebit(pg);
999 1.1.6.1 ad if ((pg->pqflags & PQ_SPECULATIVE) != 0) {
1000 1.1.6.1 ad KASSERT((pg->pqflags & PQ_TEST) == 0);
1001 1.1.6.1 ad if ((pg->pqflags & PQ_REFERENCED) != 0) {
1002 1.1.6.1 ad PDPOL_EVCNT_INCR(speculativehit2);
1003 1.1.6.1 ad pg->pqflags &= ~(PQ_SPECULATIVE|PQ_REFERENCED);
1004 1.1.6.1 ad clockpro_pagedequeue(pg);
1005 1.1.6.1 ad clockpro_pageenqueue(pg);
1006 1.1.6.1 ad continue;
1007 1.1.6.1 ad }
1008 1.1.6.1 ad PDPOL_EVCNT_INCR(speculativemiss);
1009 1.1.6.1 ad }
1010 1.1.6.1 ad switch (pg->pqflags & (PQ_REFERENCED|PQ_TEST)) {
1011 1.1.6.1 ad case PQ_TEST:
1012 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldunreftest);
1013 1.1.6.1 ad nonresident_pagerecord(pg);
1014 1.1.6.1 ad goto gotit;
1015 1.1.6.1 ad case 0:
1016 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldunref);
1017 1.1.6.1 ad gotit:
1018 1.1.6.1 ad KASSERT(s->s_ncold > 0);
1019 1.1.6.1 ad clockpro_pagerequeue(pg); /* XXX */
1020 1.1.6.1 ad dump("cold done");
1021 1.1.6.1 ad /* XXX "pg" is still in queue */
1022 1.1.6.1 ad handhot_advance();
1023 1.1.6.1 ad goto done;
1024 1.1.6.1 ad
1025 1.1.6.1 ad case PQ_REFERENCED|PQ_TEST:
1026 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldreftest);
1027 1.1.6.1 ad s->s_ncold--;
1028 1.1.6.1 ad COLDTARGET_ADJ(1);
1029 1.1.6.1 ad pg->pqflags |= PQ_HOT;
1030 1.1.6.1 ad pg->pqflags &= ~PQ_TEST;
1031 1.1.6.1 ad break;
1032 1.1.6.1 ad
1033 1.1.6.1 ad case PQ_REFERENCED:
1034 1.1.6.1 ad PDPOL_EVCNT_INCR(hcoldref);
1035 1.1.6.1 ad pg->pqflags |= PQ_TEST;
1036 1.1.6.1 ad break;
1037 1.1.6.1 ad }
1038 1.1.6.1 ad pg->pqflags &= ~PQ_REFERENCED;
1039 1.1.6.1 ad uvmexp.pdreact++;
1040 1.1.6.1 ad /* move to the list head */
1041 1.1.6.1 ad clockpro_pagerequeue(pg);
1042 1.1.6.1 ad dump("cold");
1043 1.1.6.1 ad }
1044 1.1.6.1 ad done:;
1045 1.1.6.1 ad return pg;
1046 1.1.6.1 ad }
1047 1.1.6.1 ad
1048 1.1.6.1 ad void
1049 1.1.6.1 ad uvmpdpol_pageactivate(struct vm_page *pg)
1050 1.1.6.1 ad {
1051 1.1.6.1 ad
1052 1.1.6.1 ad if (!uvmpdpol_pageisqueued_p(pg)) {
1053 1.1.6.1 ad KASSERT((pg->pqflags & PQ_SPECULATIVE) == 0);
1054 1.1.6.1 ad pg->pqflags |= PQ_INITIALREF;
1055 1.1.6.1 ad clockpro_pageenqueue(pg);
1056 1.1.6.1 ad } else if ((pg->pqflags & PQ_SPECULATIVE)) {
1057 1.1.6.1 ad PDPOL_EVCNT_INCR(speculativehit1);
1058 1.1.6.1 ad pg->pqflags &= ~PQ_SPECULATIVE;
1059 1.1.6.1 ad pg->pqflags |= PQ_INITIALREF;
1060 1.1.6.1 ad clockpro_pagedequeue(pg);
1061 1.1.6.1 ad clockpro_pageenqueue(pg);
1062 1.1.6.1 ad }
1063 1.1.6.1 ad pg->pqflags |= PQ_REFERENCED;
1064 1.1.6.1 ad }
1065 1.1.6.1 ad
1066 1.1.6.1 ad void
1067 1.1.6.1 ad uvmpdpol_pagedeactivate(struct vm_page *pg)
1068 1.1.6.1 ad {
1069 1.1.6.1 ad
1070 1.1.6.1 ad pg->pqflags &= ~PQ_REFERENCED;
1071 1.1.6.1 ad }
1072 1.1.6.1 ad
1073 1.1.6.1 ad void
1074 1.1.6.1 ad uvmpdpol_pagedequeue(struct vm_page *pg)
1075 1.1.6.1 ad {
1076 1.1.6.1 ad
1077 1.1.6.1 ad if (!uvmpdpol_pageisqueued_p(pg)) {
1078 1.1.6.1 ad return;
1079 1.1.6.1 ad }
1080 1.1.6.1 ad clockpro_pagedequeue(pg);
1081 1.1.6.1 ad pg->pqflags &= ~PQ_SPECULATIVE;
1082 1.1.6.1 ad }
1083 1.1.6.1 ad
1084 1.1.6.1 ad void
1085 1.1.6.1 ad uvmpdpol_pageenqueue(struct vm_page *pg)
1086 1.1.6.1 ad {
1087 1.1.6.1 ad
1088 1.1.6.1 ad #if 1
1089 1.1.6.1 ad if (uvmpdpol_pageisqueued_p(pg)) {
1090 1.1.6.1 ad return;
1091 1.1.6.1 ad }
1092 1.1.6.1 ad clockpro_clearreferencebit(pg);
1093 1.1.6.1 ad pg->pqflags |= PQ_SPECULATIVE;
1094 1.1.6.1 ad clockpro_pageenqueue(pg);
1095 1.1.6.1 ad #else
1096 1.1.6.1 ad uvmpdpol_pageactivate(pg);
1097 1.1.6.1 ad #endif
1098 1.1.6.1 ad }
1099 1.1.6.1 ad
1100 1.1.6.1 ad void
1101 1.1.6.1 ad uvmpdpol_anfree(struct vm_anon *an)
1102 1.1.6.1 ad {
1103 1.1.6.1 ad
1104 1.1.6.1 ad KASSERT(an->an_page == NULL);
1105 1.1.6.1 ad if (nonresident_lookupremove((objid_t)an, 0)) {
1106 1.1.6.1 ad PDPOL_EVCNT_INCR(nresanonfree);
1107 1.1.6.1 ad }
1108 1.1.6.1 ad }
1109 1.1.6.1 ad
1110 1.1.6.1 ad void
1111 1.1.6.1 ad uvmpdpol_init(void)
1112 1.1.6.1 ad {
1113 1.1.6.1 ad
1114 1.1.6.1 ad clockpro_init();
1115 1.1.6.1 ad }
1116 1.1.6.1 ad
1117 1.1.6.1 ad void
1118 1.1.6.1 ad uvmpdpol_reinit(void)
1119 1.1.6.1 ad {
1120 1.1.6.1 ad
1121 1.1.6.1 ad clockpro_reinit();
1122 1.1.6.1 ad }
1123 1.1.6.1 ad
1124 1.1.6.1 ad void
1125 1.1.6.1 ad uvmpdpol_estimatepageable(int *active, int *inactive)
1126 1.1.6.1 ad {
1127 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1128 1.1.6.1 ad
1129 1.1.6.1 ad if (active) {
1130 1.1.6.1 ad *active = s->s_npages - s->s_ncold;
1131 1.1.6.1 ad }
1132 1.1.6.1 ad if (inactive) {
1133 1.1.6.1 ad *inactive = s->s_ncold;
1134 1.1.6.1 ad }
1135 1.1.6.1 ad }
1136 1.1.6.1 ad
1137 1.1.6.1 ad boolean_t
1138 1.1.6.1 ad uvmpdpol_pageisqueued_p(struct vm_page *pg)
1139 1.1.6.1 ad {
1140 1.1.6.1 ad
1141 1.1.6.1 ad return clockpro_getq(pg) != CLOCKPRO_NOQUEUE;
1142 1.1.6.1 ad }
1143 1.1.6.1 ad
1144 1.1.6.1 ad void
1145 1.1.6.1 ad uvmpdpol_scaninit(void)
1146 1.1.6.1 ad {
1147 1.1.6.1 ad struct clockpro_scanstate * const ss = &scanstate;
1148 1.1.6.1 ad
1149 1.1.6.1 ad ss->ss_nscanned = 0;
1150 1.1.6.1 ad }
1151 1.1.6.1 ad
1152 1.1.6.1 ad struct vm_page *
1153 1.1.6.1 ad uvmpdpol_selectvictim(void)
1154 1.1.6.1 ad {
1155 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1156 1.1.6.1 ad struct clockpro_scanstate * const ss = &scanstate;
1157 1.1.6.1 ad struct vm_page *pg;
1158 1.1.6.1 ad
1159 1.1.6.1 ad if (ss->ss_nscanned > s->s_npages) {
1160 1.1.6.1 ad DPRINTF("scan too much\n");
1161 1.1.6.1 ad return NULL;
1162 1.1.6.1 ad }
1163 1.1.6.1 ad pg = handcold_advance();
1164 1.1.6.1 ad ss->ss_nscanned++;
1165 1.1.6.1 ad return pg;
1166 1.1.6.1 ad }
1167 1.1.6.1 ad
1168 1.1.6.1 ad static void
1169 1.1.6.1 ad clockpro_dropswap(pageq_t *q, int *todo)
1170 1.1.6.1 ad {
1171 1.1.6.1 ad struct vm_page *pg;
1172 1.1.6.1 ad
1173 1.1.6.1 ad TAILQ_FOREACH_REVERSE(pg, &q->q_q, pglist, pageq) {
1174 1.1.6.1 ad if (*todo <= 0) {
1175 1.1.6.1 ad break;
1176 1.1.6.1 ad }
1177 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) == 0) {
1178 1.1.6.1 ad continue;
1179 1.1.6.1 ad }
1180 1.1.6.1 ad if ((pg->pqflags & PQ_SWAPBACKED) == 0) {
1181 1.1.6.1 ad continue;
1182 1.1.6.1 ad }
1183 1.1.6.1 ad if (uvmpd_trydropswap(pg)) {
1184 1.1.6.1 ad (*todo)--;
1185 1.1.6.1 ad }
1186 1.1.6.1 ad }
1187 1.1.6.1 ad }
1188 1.1.6.1 ad
1189 1.1.6.1 ad void
1190 1.1.6.1 ad uvmpdpol_balancequeue(int swap_shortage)
1191 1.1.6.1 ad {
1192 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1193 1.1.6.1 ad int todo = swap_shortage;
1194 1.1.6.1 ad
1195 1.1.6.1 ad if (todo == 0) {
1196 1.1.6.1 ad return;
1197 1.1.6.1 ad }
1198 1.1.6.1 ad
1199 1.1.6.1 ad /*
1200 1.1.6.1 ad * reclaim swap slots from hot pages
1201 1.1.6.1 ad */
1202 1.1.6.1 ad
1203 1.1.6.1 ad DPRINTF("%s: swap_shortage=%d\n", __func__, swap_shortage);
1204 1.1.6.1 ad
1205 1.1.6.1 ad clockpro_dropswap(clockpro_queue(s, CLOCKPRO_NEWQ), &todo);
1206 1.1.6.1 ad clockpro_dropswap(clockpro_queue(s, CLOCKPRO_COLDQ), &todo);
1207 1.1.6.1 ad clockpro_dropswap(clockpro_queue(s, CLOCKPRO_HOTQ), &todo);
1208 1.1.6.1 ad
1209 1.1.6.1 ad DPRINTF("%s: done=%d\n", __func__, swap_shortage - todo);
1210 1.1.6.1 ad }
1211 1.1.6.1 ad
1212 1.1.6.1 ad boolean_t
1213 1.1.6.1 ad uvmpdpol_needsscan_p(void)
1214 1.1.6.1 ad {
1215 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1216 1.1.6.1 ad
1217 1.1.6.1 ad if (s->s_ncold < s->s_coldtarget) {
1218 1.1.6.1 ad return TRUE;
1219 1.1.6.1 ad }
1220 1.1.6.1 ad return FALSE;
1221 1.1.6.1 ad }
1222 1.1.6.1 ad
1223 1.1.6.1 ad void
1224 1.1.6.1 ad uvmpdpol_tune(void)
1225 1.1.6.1 ad {
1226 1.1.6.1 ad
1227 1.1.6.1 ad clockpro_tune();
1228 1.1.6.1 ad }
1229 1.1.6.1 ad
1230 1.1.6.1 ad #if !defined(PDSIM)
1231 1.1.6.1 ad
1232 1.1.6.1 ad #include <sys/sysctl.h> /* XXX SYSCTL_DESCR */
1233 1.1.6.1 ad
1234 1.1.6.1 ad void
1235 1.1.6.1 ad uvmpdpol_sysctlsetup(void)
1236 1.1.6.1 ad {
1237 1.1.6.1 ad #if !defined(ADAPTIVE)
1238 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1239 1.1.6.1 ad
1240 1.1.6.1 ad uvm_pctparam_createsysctlnode(&s->s_coldtargetpct, "coldtargetpct",
1241 1.1.6.1 ad SYSCTL_DESCR("Percentage cold target queue of the entire queue"));
1242 1.1.6.1 ad #endif /* !defined(ADAPTIVE) */
1243 1.1.6.1 ad }
1244 1.1.6.1 ad
1245 1.1.6.1 ad #endif /* !defined(PDSIM) */
1246 1.1.6.1 ad
1247 1.1.6.1 ad #if defined(DDB)
1248 1.1.6.1 ad
1249 1.1.6.1 ad void clockpro_dump(void);
1250 1.1.6.1 ad
1251 1.1.6.1 ad void
1252 1.1.6.1 ad clockpro_dump(void)
1253 1.1.6.1 ad {
1254 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1255 1.1.6.1 ad
1256 1.1.6.1 ad struct vm_page *pg;
1257 1.1.6.1 ad int ncold, nhot, ntest, nspeculative, ninitialref, nref;
1258 1.1.6.1 ad int newqlen, coldqlen, hotqlen, listqlen;
1259 1.1.6.1 ad
1260 1.1.6.1 ad newqlen = coldqlen = hotqlen = listqlen = 0;
1261 1.1.6.1 ad printf("npages=%d, ncold=%d, coldtarget=%d, newqlenmax=%d\n",
1262 1.1.6.1 ad s->s_npages, s->s_ncold, s->s_coldtarget, s->s_newqlenmax);
1263 1.1.6.1 ad
1264 1.1.6.1 ad #define INITCOUNT() \
1265 1.1.6.1 ad ncold = nhot = ntest = nspeculative = ninitialref = nref = 0
1266 1.1.6.1 ad
1267 1.1.6.1 ad #define COUNT(pg) \
1268 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) != 0) { \
1269 1.1.6.1 ad nhot++; \
1270 1.1.6.1 ad } else { \
1271 1.1.6.1 ad ncold++; \
1272 1.1.6.1 ad if ((pg->pqflags & PQ_TEST) != 0) { \
1273 1.1.6.1 ad ntest++; \
1274 1.1.6.1 ad } \
1275 1.1.6.1 ad if ((pg->pqflags & PQ_SPECULATIVE) != 0) { \
1276 1.1.6.1 ad nspeculative++; \
1277 1.1.6.1 ad } \
1278 1.1.6.1 ad if ((pg->pqflags & PQ_INITIALREF) != 0) { \
1279 1.1.6.1 ad ninitialref++; \
1280 1.1.6.1 ad } else if ((pg->pqflags & PQ_REFERENCED) != 0 || \
1281 1.1.6.1 ad pmap_is_referenced(pg)) { \
1282 1.1.6.1 ad nref++; \
1283 1.1.6.1 ad } \
1284 1.1.6.1 ad }
1285 1.1.6.1 ad
1286 1.1.6.1 ad #define PRINTCOUNT(name) \
1287 1.1.6.1 ad printf("%s hot=%d, cold=%d, test=%d, speculative=%d, initialref=%d, " \
1288 1.1.6.1 ad "nref=%d\n", \
1289 1.1.6.1 ad (name), nhot, ncold, ntest, nspeculative, ninitialref, nref)
1290 1.1.6.1 ad
1291 1.1.6.1 ad INITCOUNT();
1292 1.1.6.1 ad TAILQ_FOREACH(pg, &clockpro_queue(s, CLOCKPRO_NEWQ)->q_q, pageq) {
1293 1.1.6.1 ad if (clockpro_getq(pg) != CLOCKPRO_NEWQ) {
1294 1.1.6.1 ad printf("newq corrupt %p\n", pg);
1295 1.1.6.1 ad }
1296 1.1.6.1 ad COUNT(pg)
1297 1.1.6.1 ad newqlen++;
1298 1.1.6.1 ad }
1299 1.1.6.1 ad PRINTCOUNT("newq");
1300 1.1.6.1 ad
1301 1.1.6.1 ad INITCOUNT();
1302 1.1.6.1 ad TAILQ_FOREACH(pg, &clockpro_queue(s, CLOCKPRO_COLDQ)->q_q, pageq) {
1303 1.1.6.1 ad if (clockpro_getq(pg) != CLOCKPRO_COLDQ) {
1304 1.1.6.1 ad printf("coldq corrupt %p\n", pg);
1305 1.1.6.1 ad }
1306 1.1.6.1 ad COUNT(pg)
1307 1.1.6.1 ad coldqlen++;
1308 1.1.6.1 ad }
1309 1.1.6.1 ad PRINTCOUNT("coldq");
1310 1.1.6.1 ad
1311 1.1.6.1 ad INITCOUNT();
1312 1.1.6.1 ad TAILQ_FOREACH(pg, &clockpro_queue(s, CLOCKPRO_HOTQ)->q_q, pageq) {
1313 1.1.6.1 ad if (clockpro_getq(pg) != CLOCKPRO_HOTQ) {
1314 1.1.6.1 ad printf("hotq corrupt %p\n", pg);
1315 1.1.6.1 ad }
1316 1.1.6.1 ad #if defined(LISTQ)
1317 1.1.6.1 ad if ((pg->pqflags & PQ_HOT) == 0) {
1318 1.1.6.1 ad printf("cold page in hotq: %p\n", pg);
1319 1.1.6.1 ad }
1320 1.1.6.1 ad #endif /* defined(LISTQ) */
1321 1.1.6.1 ad COUNT(pg)
1322 1.1.6.1 ad hotqlen++;
1323 1.1.6.1 ad }
1324 1.1.6.1 ad PRINTCOUNT("hotq");
1325 1.1.6.1 ad
1326 1.1.6.1 ad INITCOUNT();
1327 1.1.6.1 ad TAILQ_FOREACH(pg, &clockpro_queue(s, CLOCKPRO_LISTQ)->q_q, pageq) {
1328 1.1.6.1 ad #if !defined(LISTQ)
1329 1.1.6.1 ad printf("listq %p\n");
1330 1.1.6.1 ad #endif /* !defined(LISTQ) */
1331 1.1.6.1 ad if (clockpro_getq(pg) != CLOCKPRO_LISTQ) {
1332 1.1.6.1 ad printf("listq corrupt %p\n", pg);
1333 1.1.6.1 ad }
1334 1.1.6.1 ad COUNT(pg)
1335 1.1.6.1 ad listqlen++;
1336 1.1.6.1 ad }
1337 1.1.6.1 ad PRINTCOUNT("listq");
1338 1.1.6.1 ad
1339 1.1.6.1 ad printf("newqlen=%d/%d, coldqlen=%d/%d, hotqlen=%d/%d, listqlen=%d/%d\n",
1340 1.1.6.1 ad newqlen, pageq_len(clockpro_queue(s, CLOCKPRO_NEWQ)),
1341 1.1.6.1 ad coldqlen, pageq_len(clockpro_queue(s, CLOCKPRO_COLDQ)),
1342 1.1.6.1 ad hotqlen, pageq_len(clockpro_queue(s, CLOCKPRO_HOTQ)),
1343 1.1.6.1 ad listqlen, pageq_len(clockpro_queue(s, CLOCKPRO_LISTQ)));
1344 1.1.6.1 ad }
1345 1.1.6.1 ad
1346 1.1.6.1 ad #endif /* defined(DDB) */
1347 1.1.6.1 ad
1348 1.1.6.1 ad #if defined(PDSIM)
1349 1.1.6.1 ad #if defined(DEBUG)
1350 1.1.6.1 ad static void
1351 1.1.6.1 ad pdsim_dumpq(int qidx)
1352 1.1.6.1 ad {
1353 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1354 1.1.6.1 ad pageq_t *q = clockpro_queue(s, qidx);
1355 1.1.6.1 ad struct vm_page *pg;
1356 1.1.6.1 ad
1357 1.1.6.1 ad TAILQ_FOREACH(pg, &q->q_q, pageq) {
1358 1.1.6.1 ad DPRINTF(" %" PRIu64 "%s%s%s%s%s%s",
1359 1.1.6.1 ad pg->offset >> PAGE_SHIFT,
1360 1.1.6.1 ad (pg->pqflags & PQ_HOT) ? "H" : "",
1361 1.1.6.1 ad (pg->pqflags & PQ_TEST) ? "T" : "",
1362 1.1.6.1 ad (pg->pqflags & PQ_REFERENCED) ? "R" : "",
1363 1.1.6.1 ad pmap_is_referenced(pg) ? "r" : "",
1364 1.1.6.1 ad (pg->pqflags & PQ_INITIALREF) ? "I" : "",
1365 1.1.6.1 ad (pg->pqflags & PQ_SPECULATIVE) ? "S" : ""
1366 1.1.6.1 ad );
1367 1.1.6.1 ad }
1368 1.1.6.1 ad }
1369 1.1.6.1 ad #endif /* defined(DEBUG) */
1370 1.1.6.1 ad
1371 1.1.6.1 ad void
1372 1.1.6.1 ad pdsim_dump(const char *id)
1373 1.1.6.1 ad {
1374 1.1.6.1 ad #if defined(DEBUG)
1375 1.1.6.1 ad struct clockpro_state * const s = &clockpro;
1376 1.1.6.1 ad
1377 1.1.6.1 ad DPRINTF(" %s L(", id);
1378 1.1.6.1 ad pdsim_dumpq(CLOCKPRO_LISTQ);
1379 1.1.6.1 ad DPRINTF(" ) H(");
1380 1.1.6.1 ad pdsim_dumpq(CLOCKPRO_HOTQ);
1381 1.1.6.1 ad DPRINTF(" ) C(");
1382 1.1.6.1 ad pdsim_dumpq(CLOCKPRO_COLDQ);
1383 1.1.6.1 ad DPRINTF(" ) N(");
1384 1.1.6.1 ad pdsim_dumpq(CLOCKPRO_NEWQ);
1385 1.1.6.1 ad DPRINTF(" ) ncold=%d/%d, coldadj=%d\n",
1386 1.1.6.1 ad s->s_ncold, s->s_coldtarget, coldadj);
1387 1.1.6.1 ad #endif /* defined(DEBUG) */
1388 1.1.6.1 ad }
1389 1.1.6.1 ad #endif /* defined(PDSIM) */
1390