1 /* $NetBSD: uvm_pdpolicy_clockpro.c,v 1.14 2008/03/22 05:50:42 bjs Exp $ */
4 * Copyright (c)2005, 2006 YAMAMOTO Takashi,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * CLOCK-Pro replacement policy:
31 * http://www.cs.wm.edu/hpcs/WWW/HTML/publications/abs05-3.html
33 * approximation of the list of non-resident pages using hash:
34 * http://linux-mm.org/ClockProApproximation
37 /* #define CLOCKPRO_DEBUG */
43 #else /* defined(PDSIM) */
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: uvm_pdpolicy_clockpro.c,v 1.14 2008/03/22 05:50:42 bjs Exp $");
50 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
57 #include <uvm/uvm_pdpolicy.h>
58 #include <uvm/uvm_pdpolicy_impl.h>
60 #if ((__STDC_VERSION__ - 0) >= 199901L)
61 #define DPRINTF(...) /* nothing */
62 #define WARN(...) printf(__VA_ARGS__)
63 #else /* ((__STDC_VERSION__ - 0) >= 199901L) */
64 #define DPRINTF(a...) /* nothing */ /* GCC */
65 #define WARN(a...) printf(a)
66 #endif /* ((__STDC_VERSION__ - 0) >= 199901L) */
68 #define dump(a) /* nothing */
74 #endif /* defined(PDSIM) */
76 #if !defined(CLOCKPRO_COLDPCT)
77 #define CLOCKPRO_COLDPCT 10
78 #endif /* !defined(CLOCKPRO_COLDPCT) */
80 #define CLOCKPRO_COLDPCTMAX 90
82 #if !defined(CLOCKPRO_HASHFACTOR)
83 #define CLOCKPRO_HASHFACTOR 2
84 #endif /* !defined(CLOCKPRO_HASHFACTOR) */
86 #define CLOCKPRO_NEWQMIN ((1024 * 1024) >> PAGE_SHIFT) /* XXX */
88 int clockpro_hashfactor
= CLOCKPRO_HASHFACTOR
;
90 PDPOL_EVCNT_DEFINE(nresrecordobj
)
91 PDPOL_EVCNT_DEFINE(nresrecordanon
)
92 PDPOL_EVCNT_DEFINE(nreslookupobj
)
93 PDPOL_EVCNT_DEFINE(nreslookupanon
)
94 PDPOL_EVCNT_DEFINE(nresfoundobj
)
95 PDPOL_EVCNT_DEFINE(nresfoundanon
)
96 PDPOL_EVCNT_DEFINE(nresanonfree
)
97 PDPOL_EVCNT_DEFINE(nresconflict
)
98 PDPOL_EVCNT_DEFINE(nresoverwritten
)
99 PDPOL_EVCNT_DEFINE(nreshandhot
)
101 PDPOL_EVCNT_DEFINE(hhottakeover
)
102 PDPOL_EVCNT_DEFINE(hhotref
)
103 PDPOL_EVCNT_DEFINE(hhotunref
)
104 PDPOL_EVCNT_DEFINE(hhotcold
)
105 PDPOL_EVCNT_DEFINE(hhotcoldtest
)
107 PDPOL_EVCNT_DEFINE(hcoldtakeover
)
108 PDPOL_EVCNT_DEFINE(hcoldref
)
109 PDPOL_EVCNT_DEFINE(hcoldunref
)
110 PDPOL_EVCNT_DEFINE(hcoldreftest
)
111 PDPOL_EVCNT_DEFINE(hcoldunreftest
)
112 PDPOL_EVCNT_DEFINE(hcoldunreftestspeculative
)
113 PDPOL_EVCNT_DEFINE(hcoldhot
)
115 PDPOL_EVCNT_DEFINE(speculativeenqueue
)
116 PDPOL_EVCNT_DEFINE(speculativehit1
)
117 PDPOL_EVCNT_DEFINE(speculativehit2
)
118 PDPOL_EVCNT_DEFINE(speculativemiss
)
120 #define PQ_REFERENCED PQ_PRIVATE1
121 #define PQ_HOT PQ_PRIVATE2
122 #define PQ_TEST PQ_PRIVATE3
123 #define PQ_INITIALREF PQ_PRIVATE4
124 #if PQ_PRIVATE6 != PQ_PRIVATE5 * 2 || PQ_PRIVATE7 != PQ_PRIVATE6 * 2
127 #define PQ_QMASK (PQ_PRIVATE5|PQ_PRIVATE6|PQ_PRIVATE7)
128 #define PQ_QFACTOR PQ_PRIVATE5
129 #define PQ_SPECULATIVE PQ_PRIVATE8
131 #define CLOCKPRO_NOQUEUE 0
132 #define CLOCKPRO_NEWQ 1 /* small queue to clear initial ref. */
134 #define CLOCKPRO_COLDQ 2
135 #define CLOCKPRO_HOTQ 3
136 #else /* defined(LISTQ) */
137 #define CLOCKPRO_COLDQ (2 + coldqidx) /* XXX */
138 #define CLOCKPRO_HOTQ (3 - coldqidx) /* XXX */
139 #endif /* defined(LISTQ) */
140 #define CLOCKPRO_LISTQ 4
141 #define CLOCKPRO_NQUEUE 4
144 clockpro_setq(struct vm_page
*pg
, int qidx
)
146 KASSERT(qidx
>= CLOCKPRO_NOQUEUE
);
147 KASSERT(qidx
<= CLOCKPRO_NQUEUE
);
149 pg
->pqflags
= (pg
->pqflags
& ~PQ_QMASK
) | (qidx
* PQ_QFACTOR
);
153 clockpro_getq(struct vm_page
*pg
)
157 qidx
= (pg
->pqflags
& PQ_QMASK
) / PQ_QFACTOR
;
158 KASSERT(qidx
>= CLOCKPRO_NOQUEUE
);
159 KASSERT(qidx
<= CLOCKPRO_NQUEUE
);
168 struct clockpro_state
{
174 pageq_t s_q
[CLOCKPRO_NQUEUE
];
176 struct uvm_pctparam s_coldtargetpct
;
180 clockpro_queue(struct clockpro_state
*s
, int qidx
)
183 KASSERT(CLOCKPRO_NOQUEUE
< qidx
);
184 KASSERT(qidx
<= CLOCKPRO_NQUEUE
);
186 return &s
->s_q
[qidx
- 1];
194 clockpro_switchqueue(void)
197 coldqidx
= 1 - coldqidx
;
200 #endif /* !defined(LISTQ) */
202 static struct clockpro_state clockpro
;
203 static struct clockpro_scanstate
{
207 /* ---------------------------------------- */
210 pageq_init(pageq_t
*q
)
218 pageq_len(const pageq_t
*q
)
224 static struct vm_page
*
225 pageq_first(const pageq_t
*q
)
228 return TAILQ_FIRST(&q
->q_q
);
232 pageq_insert_tail(pageq_t
*q
, struct vm_page
*pg
)
235 TAILQ_INSERT_TAIL(&q
->q_q
, pg
, pageq
.queue
);
241 pageq_insert_head(pageq_t
*q
, struct vm_page
*pg
)
244 TAILQ_INSERT_HEAD(&q
->q_q
, pg
, pageq
.queue
);
250 pageq_remove(pageq_t
*q
, struct vm_page
*pg
)
254 KASSERT(clockpro_queue(&clockpro
, clockpro_getq(pg
)) == q
);
256 KASSERT(q
->q_len
> 0);
257 TAILQ_REMOVE(&q
->q_q
, pg
, pageq
.queue
);
261 static struct vm_page
*
262 pageq_remove_head(pageq_t
*q
)
266 pg
= TAILQ_FIRST(&q
->q_q
);
268 KASSERT(q
->q_len
== 0);
275 /* ---------------------------------------- */
278 clockpro_insert_tail(struct clockpro_state
*s
, int qidx
, struct vm_page
*pg
)
280 pageq_t
*q
= clockpro_queue(s
, qidx
);
282 clockpro_setq(pg
, qidx
);
283 pageq_insert_tail(q
, pg
);
288 clockpro_insert_head(struct clockpro_state
*s
, int qidx
, struct vm_page
*pg
)
290 pageq_t
*q
= clockpro_queue(s
, qidx
);
292 clockpro_setq(pg
, qidx
);
293 pageq_insert_head(q
, pg
);
297 /* ---------------------------------------- */
299 typedef uint32_t nonres_cookie_t
;
300 #define NONRES_COOKIE_INVAL 0
302 typedef uintptr_t objid_t
;
305 * XXX maybe these hash functions need reconsideration,
306 * given that hash distribution is critical here.
310 pageidentityhash1(objid_t obj
, off_t idx
)
312 uint32_t hash
= HASH32_BUF_INIT
;
315 hash
= hash32_buf(&idx
, sizeof(idx
), hash
);
316 hash
= hash32_buf(&obj
, sizeof(obj
), hash
);
318 hash
= hash32_buf(&obj
, sizeof(obj
), hash
);
319 hash
= hash32_buf(&idx
, sizeof(idx
), hash
);
325 pageidentityhash2(objid_t obj
, off_t idx
)
327 uint32_t hash
= HASH32_BUF_INIT
;
329 hash
= hash32_buf(&obj
, sizeof(obj
), hash
);
330 hash
= hash32_buf(&idx
, sizeof(idx
), hash
);
334 static nonres_cookie_t
335 calccookie(objid_t obj
, off_t idx
)
337 uint32_t hash
= pageidentityhash2(obj
, idx
);
338 nonres_cookie_t cookie
= hash
;
340 if (__predict_false(cookie
== NONRES_COOKIE_INVAL
)) {
346 #define BUCKETSIZE 14
350 nonres_cookie_t pages
[BUCKETSIZE
];
352 static int cycle_target
;
353 static int cycle_target_frac
;
355 static struct bucket static_bucket
;
356 static struct bucket
*buckets
= &static_bucket
;
357 static size_t hashsize
= 1;
360 #define COLDTARGET_ADJ(d) coldadj += (d)
365 clockpro_hashalloc(int n
)
367 size_t allocsz
= sizeof(*buckets
) * n
;
369 return malloc(allocsz
);
373 clockpro_hashfree(void *p
, int n
)
379 #else /* defined(PDSIM) */
382 clockpro_hashalloc(int n
)
384 size_t allocsz
= round_page(sizeof(*buckets
) * n
);
386 return (void *)uvm_km_alloc(kernel_map
, allocsz
, 0, UVM_KMF_WIRED
);
390 clockpro_hashfree(void *p
, int n
)
392 size_t allocsz
= round_page(sizeof(*buckets
) * n
);
394 uvm_km_free(kernel_map
, (vaddr_t
)p
, allocsz
, UVM_KMF_WIRED
);
397 #endif /* defined(PDSIM) */
400 clockpro_hashinit(uint64_t n
)
402 struct bucket
*newbuckets
;
403 struct bucket
*oldbuckets
;
408 sz
= howmany(n
, BUCKETSIZE
);
409 sz
*= clockpro_hashfactor
;
410 newbuckets
= clockpro_hashalloc(sz
);
411 if (newbuckets
== NULL
) {
412 panic("%s: allocation failure", __func__
);
414 for (i
= 0; i
< sz
; i
++) {
415 struct bucket
*b
= &newbuckets
[i
];
418 b
->cycle
= cycle_target
;
420 for (j
= 0; j
< BUCKETSIZE
; j
++) {
421 b
->pages
[j
] = NONRES_COOKIE_INVAL
;
425 oldbuckets
= buckets
;
427 buckets
= newbuckets
;
430 if (oldbuckets
!= &static_bucket
) {
431 clockpro_hashfree(oldbuckets
, oldsz
);
435 static struct bucket
*
436 nonresident_getbucket(objid_t obj
, off_t idx
)
440 hash
= pageidentityhash1(obj
, idx
);
441 return &buckets
[hash
% hashsize
];
445 nonresident_rotate(struct bucket
*b
)
447 const int target
= cycle_target
;
448 const int cycle
= b
->cycle
;
452 todo
= target
- cycle
;
453 if (todo
>= BUCKETSIZE
* 2) {
454 todo
= (todo
% BUCKETSIZE
) + BUCKETSIZE
;
458 if (b
->pages
[cur
] != NONRES_COOKIE_INVAL
) {
459 PDPOL_EVCNT_INCR(nreshandhot
);
462 b
->pages
[cur
] = NONRES_COOKIE_INVAL
;
464 if (cur
== BUCKETSIZE
) {
474 nonresident_lookupremove(objid_t obj
, off_t idx
)
476 struct bucket
*b
= nonresident_getbucket(obj
, idx
);
477 nonres_cookie_t cookie
= calccookie(obj
, idx
);
480 nonresident_rotate(b
);
481 for (i
= 0; i
< BUCKETSIZE
; i
++) {
482 if (b
->pages
[i
] == cookie
) {
483 b
->pages
[i
] = NONRES_COOKIE_INVAL
;
491 pageobj(struct vm_page
*pg
)
496 * XXX object pointer is often freed and reused for unrelated object.
497 * for vnodes, it would be better to use something like
498 * a hash of fsid/fileid/generation.
504 KASSERT(obj
!= NULL
);
505 KASSERT(pg
->offset
== 0);
512 pageidx(struct vm_page
*pg
)
515 KASSERT((pg
->offset
& PAGE_MASK
) == 0);
516 return pg
->offset
>> PAGE_SHIFT
;
520 nonresident_pagelookupremove(struct vm_page
*pg
)
522 bool found
= nonresident_lookupremove(pageobj(pg
), pageidx(pg
));
525 PDPOL_EVCNT_INCR(nreslookupobj
);
527 PDPOL_EVCNT_INCR(nreslookupanon
);
531 PDPOL_EVCNT_INCR(nresfoundobj
);
533 PDPOL_EVCNT_INCR(nresfoundanon
);
540 nonresident_pagerecord(struct vm_page
*pg
)
542 objid_t obj
= pageobj(pg
);
543 off_t idx
= pageidx(pg
);
544 struct bucket
*b
= nonresident_getbucket(obj
, idx
);
545 nonres_cookie_t cookie
= calccookie(obj
, idx
);
550 for (i
= 0; i
< BUCKETSIZE
; i
++) {
551 if (b
->pages
[i
] == cookie
) {
552 PDPOL_EVCNT_INCR(nresconflict
);
555 #endif /* defined(DEBUG) */
558 PDPOL_EVCNT_INCR(nresrecordobj
);
560 PDPOL_EVCNT_INCR(nresrecordanon
);
562 nonresident_rotate(b
);
563 if (b
->pages
[b
->cur
] != NONRES_COOKIE_INVAL
) {
564 PDPOL_EVCNT_INCR(nresoverwritten
);
567 b
->pages
[b
->cur
] = cookie
;
568 b
->cur
= (b
->cur
+ 1) % BUCKETSIZE
;
571 /* ---------------------------------------- */
573 #if defined(CLOCKPRO_DEBUG)
578 #else /* defined(CLOCKPRO_DEBUG) */
579 #define check_sanity() /* nothing */
580 #endif /* defined(CLOCKPRO_DEBUG) */
583 clockpro_reinit(void)
586 clockpro_hashinit(uvmexp
.npages
);
592 struct clockpro_state
*s
= &clockpro
;
595 for (i
= 0; i
< CLOCKPRO_NQUEUE
; i
++) {
596 pageq_init(&s
->s_q
[i
]);
600 uvm_pctparam_init(&s
->s_coldtargetpct
, CLOCKPRO_COLDPCT
, NULL
);
606 struct clockpro_state
*s
= &clockpro
;
609 #if defined(ADAPTIVE)
610 int coldmax
= s
->s_npages
* CLOCKPRO_COLDPCTMAX
/ 100;
613 coldtarget
= s
->s_coldtarget
;
614 if (coldtarget
+ coldadj
< coldmin
) {
615 coldadj
= coldmin
- coldtarget
;
616 } else if (coldtarget
+ coldadj
> coldmax
) {
617 coldadj
= coldmax
- coldtarget
;
619 coldtarget
+= coldadj
;
620 #else /* defined(ADAPTIVE) */
621 coldtarget
= UVM_PCTPARAM_APPLY(&s
->s_coldtargetpct
, s
->s_npages
);
622 if (coldtarget
< 1) {
625 #endif /* defined(ADAPTIVE) */
627 s
->s_coldtarget
= coldtarget
;
628 s
->s_newqlenmax
= coldtarget
/ 4;
629 if (s
->s_newqlenmax
< CLOCKPRO_NEWQMIN
) {
630 s
->s_newqlenmax
= CLOCKPRO_NEWQMIN
;
635 clockpro_movereferencebit(struct vm_page
*pg
)
639 referenced
= pmap_clear_reference(pg
);
641 pg
->pqflags
|= PQ_REFERENCED
;
646 clockpro_clearreferencebit(struct vm_page
*pg
)
649 clockpro_movereferencebit(pg
);
650 pg
->pqflags
&= ~PQ_REFERENCED
;
654 clockpro___newqrotate(int len
)
656 struct clockpro_state
* const s
= &clockpro
;
657 pageq_t
* const newq
= clockpro_queue(s
, CLOCKPRO_NEWQ
);
660 while (pageq_len(newq
) > len
) {
661 pg
= pageq_remove_head(newq
);
663 KASSERT(clockpro_getq(pg
) == CLOCKPRO_NEWQ
);
664 if ((pg
->pqflags
& PQ_INITIALREF
) != 0) {
665 clockpro_clearreferencebit(pg
);
666 pg
->pqflags
&= ~PQ_INITIALREF
;
668 /* place at the list head */
669 clockpro_insert_tail(s
, CLOCKPRO_COLDQ
, pg
);
674 clockpro_newqrotate(void)
676 struct clockpro_state
* const s
= &clockpro
;
679 clockpro___newqrotate(s
->s_newqlenmax
);
684 clockpro_newqflush(int n
)
688 clockpro___newqrotate(n
);
693 clockpro_newqflushone(void)
695 struct clockpro_state
* const s
= &clockpro
;
698 MAX(pageq_len(clockpro_queue(s
, CLOCKPRO_NEWQ
)) - 1, 0));
702 * our "tail" is called "list-head" in the paper.
706 clockpro___enqueuetail(struct vm_page
*pg
)
708 struct clockpro_state
* const s
= &clockpro
;
710 KASSERT(clockpro_getq(pg
) == CLOCKPRO_NOQUEUE
);
713 #if !defined(USEONCE2)
714 clockpro_insert_tail(s
, CLOCKPRO_NEWQ
, pg
);
715 clockpro_newqrotate();
716 #else /* !defined(USEONCE2) */
718 KASSERT((pg
->pqflags
& PQ_REFERENCED
) == 0);
719 #endif /* defined(LISTQ) */
720 clockpro_insert_tail(s
, CLOCKPRO_COLDQ
, pg
);
721 #endif /* !defined(USEONCE2) */
726 clockpro_pageenqueue(struct vm_page
*pg
)
728 struct clockpro_state
* const s
= &clockpro
;
730 bool speculative
= (pg
->pqflags
& PQ_SPECULATIVE
) != 0; /* XXX */
732 KASSERT((~pg
->pqflags
& (PQ_INITIALREF
|PQ_SPECULATIVE
)) != 0);
733 KASSERT(mutex_owned(&uvm_pageqlock
));
735 KASSERT(clockpro_getq(pg
) == CLOCKPRO_NOQUEUE
);
737 pg
->pqflags
&= ~(PQ_HOT
|PQ_TEST
);
740 PDPOL_EVCNT_INCR(speculativeenqueue
);
742 hot
= nonresident_pagelookupremove(pg
);
749 * consider mmap'ed file:
751 * - read-ahead enqueues a page.
753 * - on the following read-ahead hit, the fault handler activates it.
755 * - finally, the userland code which caused the above fault
756 * actually accesses the page. it makes its reference bit set.
758 * we want to count the above as a single access, rather than
759 * three accesses with short reuse distances.
762 #if defined(USEONCE2)
763 pg
->pqflags
&= ~PQ_INITIALREF
;
765 pg
->pqflags
|= PQ_TEST
;
768 clockpro_clearreferencebit(pg
);
769 clockpro___enqueuetail(pg
);
770 #else /* defined(USEONCE2) */
774 pg
->pqflags
|= PQ_HOT
;
776 pg
->pqflags
|= PQ_TEST
;
779 clockpro___enqueuetail(pg
);
780 #endif /* defined(USEONCE2) */
781 KASSERT(s
->s_ncold
<= s
->s_npages
);
785 clockpro_pagequeue(struct vm_page
*pg
)
787 struct clockpro_state
* const s
= &clockpro
;
790 qidx
= clockpro_getq(pg
);
791 KASSERT(qidx
!= CLOCKPRO_NOQUEUE
);
793 return clockpro_queue(s
, qidx
);
797 clockpro_pagedequeue(struct vm_page
*pg
)
799 struct clockpro_state
* const s
= &clockpro
;
802 KASSERT(s
->s_npages
> 0);
804 q
= clockpro_pagequeue(pg
);
807 clockpro_setq(pg
, CLOCKPRO_NOQUEUE
);
808 if ((pg
->pqflags
& PQ_HOT
) == 0) {
809 KASSERT(s
->s_ncold
> 0);
812 KASSERT(s
->s_npages
> 0);
818 clockpro_pagerequeue(struct vm_page
*pg
)
820 struct clockpro_state
* const s
= &clockpro
;
823 qidx
= clockpro_getq(pg
);
824 KASSERT(qidx
== CLOCKPRO_HOTQ
|| qidx
== CLOCKPRO_COLDQ
);
825 pageq_remove(clockpro_queue(s
, qidx
), pg
);
827 clockpro_setq(pg
, CLOCKPRO_NOQUEUE
);
829 clockpro___enqueuetail(pg
);
833 handhot_endtest(struct vm_page
*pg
)
836 KASSERT((pg
->pqflags
& PQ_HOT
) == 0);
837 if ((pg
->pqflags
& PQ_TEST
) != 0) {
838 PDPOL_EVCNT_INCR(hhotcoldtest
);
840 pg
->pqflags
&= ~PQ_TEST
;
842 PDPOL_EVCNT_INCR(hhotcold
);
847 handhot_advance(void)
849 struct clockpro_state
* const s
= &clockpro
;
857 if (s
->s_ncold
>= s
->s_coldtarget
) {
860 hotq
= clockpro_queue(s
, CLOCKPRO_HOTQ
);
862 pg
= pageq_first(hotq
);
864 DPRINTF("%s: HHOT TAKEOVER\n", __func__
);
865 dump("hhottakeover");
866 PDPOL_EVCNT_INCR(hhottakeover
);
868 while (/* CONSTCOND */ 1) {
869 pageq_t
*coldq
= clockpro_queue(s
, CLOCKPRO_COLDQ
);
871 pg
= pageq_first(coldq
);
873 clockpro_newqflushone();
874 pg
= pageq_first(coldq
);
876 WARN("hhot: no page?\n");
880 KASSERT(clockpro_pagequeue(pg
) == coldq
);
881 pageq_remove(coldq
, pg
);
883 if ((pg
->pqflags
& PQ_HOT
) == 0) {
885 clockpro_insert_tail(s
, CLOCKPRO_LISTQ
, pg
);
887 clockpro_insert_head(s
, CLOCKPRO_HOTQ
, pg
);
891 #else /* defined(LISTQ) */
892 clockpro_newqflush(0); /* XXX XXX */
893 clockpro_switchqueue();
894 hotq
= clockpro_queue(s
, CLOCKPRO_HOTQ
);
896 #endif /* defined(LISTQ) */
899 KASSERT(clockpro_pagequeue(pg
) == hotq
);
902 * terminate test period of nonresident pages by cycling them.
905 cycle_target_frac
+= BUCKETSIZE
;
906 hotqlen
= pageq_len(hotq
);
907 while (cycle_target_frac
>= hotqlen
) {
909 cycle_target_frac
-= hotqlen
;
912 if ((pg
->pqflags
& PQ_HOT
) == 0) {
914 panic("cold page in hotq: %p", pg
);
915 #else /* defined(LISTQ) */
918 #endif /* defined(LISTQ) */
920 KASSERT((pg
->pqflags
& PQ_TEST
) == 0);
921 KASSERT((pg
->pqflags
& PQ_INITIALREF
) == 0);
922 KASSERT((pg
->pqflags
& PQ_SPECULATIVE
) == 0);
925 * once we met our target,
926 * stop at a hot page so that no cold pages in test period
927 * have larger recency than any hot pages.
930 if (s
->s_ncold
>= s
->s_coldtarget
) {
934 clockpro_movereferencebit(pg
);
935 if ((pg
->pqflags
& PQ_REFERENCED
) == 0) {
936 PDPOL_EVCNT_INCR(hhotunref
);
938 pg
->pqflags
&= ~PQ_HOT
;
940 KASSERT(s
->s_ncold
<= s
->s_npages
);
942 PDPOL_EVCNT_INCR(hhotref
);
944 pg
->pqflags
&= ~PQ_REFERENCED
;
947 #endif /* !defined(LISTQ) */
948 clockpro_pagerequeue(pg
);
953 static struct vm_page
*
954 handcold_advance(void)
956 struct clockpro_state
* const s
= &clockpro
;
961 pageq_t
*listq
= clockpro_queue(s
, CLOCKPRO_LISTQ
);
962 #endif /* defined(LISTQ) */
965 clockpro_newqrotate();
968 pg
= pageq_first(listq
);
970 KASSERT(clockpro_getq(pg
) == CLOCKPRO_LISTQ
);
971 KASSERT((pg
->pqflags
& PQ_TEST
) == 0);
972 KASSERT((pg
->pqflags
& PQ_HOT
) == 0);
973 KASSERT((pg
->pqflags
& PQ_INITIALREF
) == 0);
974 pageq_remove(listq
, pg
);
976 clockpro_insert_head(s
, CLOCKPRO_COLDQ
, pg
); /* XXX */
979 #endif /* defined(LISTQ) */
981 coldq
= clockpro_queue(s
, CLOCKPRO_COLDQ
);
982 pg
= pageq_first(coldq
);
984 clockpro_newqflushone();
985 pg
= pageq_first(coldq
);
988 DPRINTF("%s: HCOLD TAKEOVER\n", __func__
);
989 dump("hcoldtakeover");
990 PDPOL_EVCNT_INCR(hcoldtakeover
);
992 pageq_len(clockpro_queue(s
, CLOCKPRO_NEWQ
)) == 0);
995 pageq_len(clockpro_queue(s
, CLOCKPRO_HOTQ
)) == 0);
996 #else /* defined(LISTQ) */
997 clockpro_switchqueue();
998 coldq
= clockpro_queue(s
, CLOCKPRO_COLDQ
);
999 pg
= pageq_first(coldq
);
1000 #endif /* defined(LISTQ) */
1003 WARN("hcold: no page?\n");
1006 KASSERT((pg
->pqflags
& PQ_INITIALREF
) == 0);
1007 if ((pg
->pqflags
& PQ_HOT
) != 0) {
1008 PDPOL_EVCNT_INCR(hcoldhot
);
1009 pageq_remove(coldq
, pg
);
1010 clockpro_insert_tail(s
, CLOCKPRO_HOTQ
, pg
);
1012 KASSERT((pg
->pqflags
& PQ_TEST
) == 0);
1018 #endif /* defined(LISTQ) */
1019 KASSERT((pg
->pqflags
& PQ_HOT
) == 0);
1021 clockpro_movereferencebit(pg
);
1022 if ((pg
->pqflags
& PQ_SPECULATIVE
) != 0) {
1023 KASSERT((pg
->pqflags
& PQ_TEST
) == 0);
1024 if ((pg
->pqflags
& PQ_REFERENCED
) != 0) {
1025 PDPOL_EVCNT_INCR(speculativehit2
);
1026 pg
->pqflags
&= ~(PQ_SPECULATIVE
|PQ_REFERENCED
);
1027 clockpro_pagedequeue(pg
);
1028 clockpro_pageenqueue(pg
);
1031 PDPOL_EVCNT_INCR(speculativemiss
);
1033 switch (pg
->pqflags
& (PQ_REFERENCED
|PQ_TEST
)) {
1035 PDPOL_EVCNT_INCR(hcoldunreftest
);
1036 nonresident_pagerecord(pg
);
1039 PDPOL_EVCNT_INCR(hcoldunref
);
1041 KASSERT(s
->s_ncold
> 0);
1042 clockpro_pagerequeue(pg
); /* XXX */
1044 /* XXX "pg" is still in queue */
1048 case PQ_REFERENCED
|PQ_TEST
:
1049 PDPOL_EVCNT_INCR(hcoldreftest
);
1052 pg
->pqflags
|= PQ_HOT
;
1053 pg
->pqflags
&= ~PQ_TEST
;
1057 PDPOL_EVCNT_INCR(hcoldref
);
1058 pg
->pqflags
|= PQ_TEST
;
1061 pg
->pqflags
&= ~PQ_REFERENCED
;
1063 /* move to the list head */
1064 clockpro_pagerequeue(pg
);
1072 uvmpdpol_pageactivate(struct vm_page
*pg
)
1075 if (!uvmpdpol_pageisqueued_p(pg
)) {
1076 KASSERT((pg
->pqflags
& PQ_SPECULATIVE
) == 0);
1077 pg
->pqflags
|= PQ_INITIALREF
;
1078 clockpro_pageenqueue(pg
);
1079 } else if ((pg
->pqflags
& PQ_SPECULATIVE
)) {
1080 PDPOL_EVCNT_INCR(speculativehit1
);
1081 pg
->pqflags
&= ~PQ_SPECULATIVE
;
1082 pg
->pqflags
|= PQ_INITIALREF
;
1083 clockpro_pagedequeue(pg
);
1084 clockpro_pageenqueue(pg
);
1086 pg
->pqflags
|= PQ_REFERENCED
;
1090 uvmpdpol_pagedeactivate(struct vm_page
*pg
)
1093 clockpro_clearreferencebit(pg
);
1097 uvmpdpol_pagedequeue(struct vm_page
*pg
)
1100 if (!uvmpdpol_pageisqueued_p(pg
)) {
1103 clockpro_pagedequeue(pg
);
1104 pg
->pqflags
&= ~(PQ_INITIALREF
|PQ_SPECULATIVE
);
1108 uvmpdpol_pageenqueue(struct vm_page
*pg
)
1112 if (uvmpdpol_pageisqueued_p(pg
)) {
1115 clockpro_clearreferencebit(pg
);
1116 pg
->pqflags
|= PQ_SPECULATIVE
;
1117 clockpro_pageenqueue(pg
);
1119 uvmpdpol_pageactivate(pg
);
1124 uvmpdpol_anfree(struct vm_anon
*an
)
1127 KASSERT(an
->an_page
== NULL
);
1128 if (nonresident_lookupremove((objid_t
)an
, 0)) {
1129 PDPOL_EVCNT_INCR(nresanonfree
);
1141 uvmpdpol_reinit(void)
1148 uvmpdpol_estimatepageable(int *active
, int *inactive
)
1150 struct clockpro_state
* const s
= &clockpro
;
1153 *active
= s
->s_npages
- s
->s_ncold
;
1156 *inactive
= s
->s_ncold
;
1161 uvmpdpol_pageisqueued_p(struct vm_page
*pg
)
1164 return clockpro_getq(pg
) != CLOCKPRO_NOQUEUE
;
1168 uvmpdpol_scaninit(void)
1170 struct clockpro_scanstate
* const ss
= &scanstate
;
1172 ss
->ss_nscanned
= 0;
1176 uvmpdpol_selectvictim(void)
1178 struct clockpro_state
* const s
= &clockpro
;
1179 struct clockpro_scanstate
* const ss
= &scanstate
;
1182 if (ss
->ss_nscanned
> s
->s_npages
) {
1183 DPRINTF("scan too much\n");
1186 pg
= handcold_advance();
1192 clockpro_dropswap(pageq_t
*q
, int *todo
)
1196 TAILQ_FOREACH_REVERSE(pg
, &q
->q_q
, pglist
, pageq
.queue
) {
1200 if ((pg
->pqflags
& PQ_HOT
) == 0) {
1203 if ((pg
->pqflags
& PQ_SWAPBACKED
) == 0) {
1206 if (uvmpd_trydropswap(pg
)) {
1213 uvmpdpol_balancequeue(int swap_shortage
)
1215 struct clockpro_state
* const s
= &clockpro
;
1216 int todo
= swap_shortage
;
1223 * reclaim swap slots from hot pages
1226 DPRINTF("%s: swap_shortage=%d\n", __func__
, swap_shortage
);
1228 clockpro_dropswap(clockpro_queue(s
, CLOCKPRO_NEWQ
), &todo
);
1229 clockpro_dropswap(clockpro_queue(s
, CLOCKPRO_COLDQ
), &todo
);
1230 clockpro_dropswap(clockpro_queue(s
, CLOCKPRO_HOTQ
), &todo
);
1232 DPRINTF("%s: done=%d\n", __func__
, swap_shortage
- todo
);
1236 uvmpdpol_needsscan_p(void)
1238 struct clockpro_state
* const s
= &clockpro
;
1240 if (s
->s_ncold
< s
->s_coldtarget
) {
1255 #include <sys/sysctl.h> /* XXX SYSCTL_DESCR */
1258 uvmpdpol_sysctlsetup(void)
1260 #if !defined(ADAPTIVE)
1261 struct clockpro_state
* const s
= &clockpro
;
1263 uvm_pctparam_createsysctlnode(&s
->s_coldtargetpct
, "coldtargetpct",
1264 SYSCTL_DESCR("Percentage cold target queue of the entire queue"));
1265 #endif /* !defined(ADAPTIVE) */
1268 #endif /* !defined(PDSIM) */
1272 void clockpro_dump(void);
1277 struct clockpro_state
* const s
= &clockpro
;
1280 int ncold
, nhot
, ntest
, nspeculative
, ninitialref
, nref
;
1281 int newqlen
, coldqlen
, hotqlen
, listqlen
;
1283 newqlen
= coldqlen
= hotqlen
= listqlen
= 0;
1284 printf("npages=%d, ncold=%d, coldtarget=%d, newqlenmax=%d\n",
1285 s
->s_npages
, s
->s_ncold
, s
->s_coldtarget
, s
->s_newqlenmax
);
1287 #define INITCOUNT() \
1288 ncold = nhot = ntest = nspeculative = ninitialref = nref = 0
1291 if ((pg->pqflags & PQ_HOT) != 0) { \
1295 if ((pg->pqflags & PQ_TEST) != 0) { \
1298 if ((pg->pqflags & PQ_SPECULATIVE) != 0) { \
1301 if ((pg->pqflags & PQ_INITIALREF) != 0) { \
1303 } else if ((pg->pqflags & PQ_REFERENCED) != 0 || \
1304 pmap_is_referenced(pg)) { \
1309 #define PRINTCOUNT(name) \
1310 printf("%s hot=%d, cold=%d, test=%d, speculative=%d, initialref=%d, " \
1312 (name), nhot, ncold, ntest, nspeculative, ninitialref, nref)
1315 TAILQ_FOREACH(pg
, &clockpro_queue(s
, CLOCKPRO_NEWQ
)->q_q
, pageq
.queue
) {
1316 if (clockpro_getq(pg
) != CLOCKPRO_NEWQ
) {
1317 printf("newq corrupt %p\n", pg
);
1325 TAILQ_FOREACH(pg
, &clockpro_queue(s
, CLOCKPRO_COLDQ
)->q_q
, pageq
.queue
) {
1326 if (clockpro_getq(pg
) != CLOCKPRO_COLDQ
) {
1327 printf("coldq corrupt %p\n", pg
);
1332 PRINTCOUNT("coldq");
1335 TAILQ_FOREACH(pg
, &clockpro_queue(s
, CLOCKPRO_HOTQ
)->q_q
, pageq
.queue
) {
1336 if (clockpro_getq(pg
) != CLOCKPRO_HOTQ
) {
1337 printf("hotq corrupt %p\n", pg
);
1340 if ((pg
->pqflags
& PQ_HOT
) == 0) {
1341 printf("cold page in hotq: %p\n", pg
);
1343 #endif /* defined(LISTQ) */
1350 TAILQ_FOREACH(pg
, &clockpro_queue(s
, CLOCKPRO_LISTQ
)->q_q
, pageq
.queue
) {
1352 printf("listq %p\n", pg
);
1353 #endif /* !defined(LISTQ) */
1354 if (clockpro_getq(pg
) != CLOCKPRO_LISTQ
) {
1355 printf("listq corrupt %p\n", pg
);
1360 PRINTCOUNT("listq");
1362 printf("newqlen=%d/%d, coldqlen=%d/%d, hotqlen=%d/%d, listqlen=%d/%d\n",
1363 newqlen
, pageq_len(clockpro_queue(s
, CLOCKPRO_NEWQ
)),
1364 coldqlen
, pageq_len(clockpro_queue(s
, CLOCKPRO_COLDQ
)),
1365 hotqlen
, pageq_len(clockpro_queue(s
, CLOCKPRO_HOTQ
)),
1366 listqlen
, pageq_len(clockpro_queue(s
, CLOCKPRO_LISTQ
)));
1369 #endif /* defined(DDB) */
1374 pdsim_dumpq(int qidx
)
1376 struct clockpro_state
* const s
= &clockpro
;
1377 pageq_t
*q
= clockpro_queue(s
, qidx
);
1380 TAILQ_FOREACH(pg
, &q
->q_q
, pageq
.queue
) {
1381 DPRINTF(" %" PRIu64
"%s%s%s%s%s%s",
1382 pg
->offset
>> PAGE_SHIFT
,
1383 (pg
->pqflags
& PQ_HOT
) ? "H" : "",
1384 (pg
->pqflags
& PQ_TEST
) ? "T" : "",
1385 (pg
->pqflags
& PQ_REFERENCED
) ? "R" : "",
1386 pmap_is_referenced(pg
) ? "r" : "",
1387 (pg
->pqflags
& PQ_INITIALREF
) ? "I" : "",
1388 (pg
->pqflags
& PQ_SPECULATIVE
) ? "S" : ""
1392 #endif /* defined(DEBUG) */
1395 pdsim_dump(const char *id
)
1398 struct clockpro_state
* const s
= &clockpro
;
1400 DPRINTF(" %s L(", id
);
1401 pdsim_dumpq(CLOCKPRO_LISTQ
);
1403 pdsim_dumpq(CLOCKPRO_HOTQ
);
1405 pdsim_dumpq(CLOCKPRO_COLDQ
);
1407 pdsim_dumpq(CLOCKPRO_NEWQ
);
1408 DPRINTF(" ) ncold=%d/%d, coldadj=%d\n",
1409 s
->s_ncold
, s
->s_coldtarget
, coldadj
);
1410 #endif /* defined(DEBUG) */
1412 #endif /* defined(PDSIM) */