1 /* $NetBSD: lirs.c,v 1.2 2006/10/09 12:40:00 yamt Exp $ */
4 * Copyright (c)2005 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
29 #include <sys/queue.h>
37 #define DFPRINTF(...) fprintf(__VA_ARGS__)
39 #define DFPRINTF(...) /* nothing */
57 struct buf bufs
[MAXID
];
59 TAILQ_HEAD(, buf
) q_q
;
60 TAILQ_HEAD(, buf
) q_s
;
67 void buf_print(struct buf
*, char *);
70 buf_print(struct buf
*bp
, char *s
)
73 DFPRINTF(stderr
, "%d(%s,%s,%d)%s", (bp
- bufs
),
74 (bp
->b_type
== B_L
) ? "L" :
75 (bp
->b_type
== (B_H
| B_R
)) ? "HR" :
76 (bp
->b_type
== B_H
) ? "H" :
77 (bp
->b_type
== 0) ? "free" :
79 (bp
->b_flags
& B_S
) ? "S" : "",
91 DFPRINTF(stderr
, "S: ");
92 TAILQ_FOREACH(bp
, &q_s
, b_s
) {
95 DFPRINTF(stderr
, "\n");
97 DFPRINTF(stderr
, "Q: ");
98 TAILQ_FOREACH(bp
, &q_q
, b_q
) {
101 DFPRINTF(stderr
, "\n");
104 for (i
= 0; i
< 256; i
++) {
107 if (bufs
->b_type
== 0) {
113 DFPRINTF(stderr
, "nlirs=%d, nbufs=%d\n", nlirs
, nbufs
);
114 #endif /* defined(DEBUG) */
122 if (nbufs
<= nbufs_max
) {
126 bp
= TAILQ_FIRST(&q_q
);
127 buf_print(bp
, ": reclaim\n");
128 assert(bp
->b_type
== (B_H
| B_R
));
129 TAILQ_REMOVE(&q_q
, bp
, b_q
);
141 bp
= TAILQ_FIRST(&q_s
);
142 if (bp
->b_type
== B_L
) {
145 buf_print(bp
, ": prune\n");
146 TAILQ_REMOVE(&q_s
, bp
, b_s
);
147 assert(bp
->b_flags
& B_S
);
149 if ((bp
->b_type
& B_R
) == 0) {
160 if (nlirs
<= nlirs_max
) {
164 bp
= TAILQ_FIRST(&q_s
);
165 buf_print(bp
, ": reclaim_l\n");
166 assert(bp
->b_type
== B_L
);
167 assert(bp
->b_flags
& B_S
);
168 bp
->b_type
= B_H
| B_R
;
170 TAILQ_REMOVE(&q_s
, bp
, b_s
);
171 TAILQ_INSERT_TAIL(&q_q
, bp
, b_q
);
182 memset(&bufs
, 0, sizeof(bufs
));
185 nlirs_max
= nbufs_max
* 2 / 3;
187 nlirs_max
= nbufs_max
* 90 / 100;
191 struct object
{int dummy
;};
195 fault(struct object
*dummy
, int i
)
199 DFPRINTF(stderr
, "----------\n");
202 DFPRINTF(stderr
, "---------- ts %d\n", ts
);
205 buf_print(bp
, ": access\n");
206 if (bp
->b_type
== 0) {
209 bp
->b_irr
= ts
- bp
->b_lastts
- 1;
213 if (bp
->b_type
== B_L
) {
214 assert(bp
->b_flags
& B_S
);
215 TAILQ_REMOVE(&q_s
, bp
, b_s
);
216 TAILQ_INSERT_TAIL(&q_s
, bp
, b_s
);
220 if (bp
->b_type
== (B_H
| B_R
)) {
221 if (bp
->b_flags
& B_S
) {
222 TAILQ_REMOVE(&q_s
, bp
, b_s
);
223 TAILQ_REMOVE(&q_q
, bp
, b_q
);
228 TAILQ_REMOVE(&q_q
, bp
, b_q
);
229 TAILQ_INSERT_TAIL(&q_q
, bp
, b_q
);
231 TAILQ_INSERT_TAIL(&q_s
, bp
, b_s
);
237 if ((bp
->b_flags
& (B_R
| B_L
)) == 0) {
240 if (bp
->b_type
== 0) {
241 buf_print(bp
, ": new\n");
242 if (nlirs
< nlirs_max
) {
244 TAILQ_INSERT_TAIL(&q_s
, bp
, b_s
);
251 if (bp
->b_type
== B_H
) {
252 if (bp
->b_flags
& B_S
) {
253 TAILQ_REMOVE(&q_s
, bp
, b_s
);
259 TAILQ_INSERT_TAIL(&q_q
, bp
, b_q
);
261 TAILQ_INSERT_TAIL(&q_s
, bp
, b_s
);
272 memset(&obj
, 0, sizeof(obj
));
279 ln
= fparseln(stdin
, NULL
, NULL
, NULL
, 0);
299 main(int argc
, char *argv
[])