/
TWOQ.java
244 lines (199 loc) · 5.09 KB
/
TWOQ.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//Author: Sun Siyue
package org.apache.derby.impl.services.cache;
final class TWOQ extends Policy {
//2 Q implementation need to be delayed
//THIS IS A TEMPLATE!!!!
public TWOQ(){
// cached jobs go into these two buffers
Am = new PriorityQueue();
A1in = new PriorityQueue();
// "headers" go in this buffer, so they are
// separate jobs that represent headers of
// previously cached jobs.
A1out = new PriorityQueue();
}
public TWOQ(int cachesize){
// cached jobs go into these two buffers
Am = new PriorityQueue();
A1in = new PriorityQueue();
// "headers" go in this buffer, so they are
// separate jobs that represent headers of
// previously cached jobs.
A1out = new PriorityQueue();
FreeSlots = cachesize;
Kin = (int) (0.2 * cachesize); // 25%
Kout = (int) (0.5 * cachesize); // 50%
}
synchronized void addEntry(CacheEntry entry) {
count++;
entries.add(new Item(entry,(double) count));
FreeSlots--;
count++;
if (A1out.getJob(job.JobID()) != null) {
//reclaimed = Reclaim(id);
//Am.EnqueueJob(0, reclaimed);
Am.EnqueueJob(0, job);
}
else {
if (A1in.getJob(job.JobID()) != null) {
// do nothing
}
else {
//reclaimed = Reclaim(id);
//A1in.EnqueueJob(0, reclaimed);
A1in.EnqueueJob(0, job);
}
}
//pq.EnqueueJob((double)count, job);
incrUsed(job.JobSize());
}
synchronized CacheEntry findVictim(boolean forUncache) {
Item victim = findMin();
if (forUncache)
entries.remove(victim);
return victim.entry;
int id_out;
FreeSlots++;
if (A1in.getLength() > Kin) {
Job A1inTail = A1in.DequeueFront();
id_out = A1inTail.JobID();
Job headerJob = new Job(id_out, 1);
A1out.EnqueueJob(0, headerJob);
if (A1out.getLength() > Kout) {
Job removedHeader = A1out.DequeueFront();
removedHeader = null;
}
// put X into the reclaimed page slot
return A1inTail;
}
else {
Job AmTail = Am.DequeueFront();
// put X into the reclaimed page slot
return AmTail;
}
}
void incrHit(CacheEntry e) {
count++;
count++;
hit++;
byteHit += size;
if (Am.getJob(id) != null) {
Am.EnqueueJob(0, Am.DequeueJob(id));
}
}
String Name() {
return "TWOQ";
}
//***************************************************************
//child class functions
//virtual void PrintBuffer(ofstream outfile, const char policyname, int id);
/* buffers to be used with 2Q */
public PriorityQueue Am;
public PriorityQueue A1in;
public PriorityQueue A1out;
int Kin, Kout, FreeSlots;
public Job getJob(int id) {
Job retJob = null;
retJob = Am.getJob(id);
if (retJob == null) {
retJob = A1in.getJob(id);
}
return retJob;
}
public Job release(int id) {
if (Am.getJob(id) != null) {
return Am.DequeueJob(id);
}
else {
if (A1in.getJob(id) != null) {
return A1in.DequeueJob(id);
}
else {
// ERROR!
// job is neither in Am nor A1in, so why are we here???!
assert(false);
return null;
}
}
}
/* methods for the Am buffer */
public Job FindInAm(int id) {
return Am.getJob(id);
}
public void AddtoAm(Job job) {
Am.EnqueueJob(0, job);
}
public Job RemoveFromAm(int id) {
return Am.DequeueJob(id);
}
/* methods for the A1in buffer */
public Job FindInA1in(int id) {
return A1in.getJob(id);
}
public void AddtoA1in(Job job) {
A1in.EnqueueJob(0, job);
}
public Job RemoveFromA1in(int id) {
return A1in.DequeueJob(id);
}
/* methods for the A1out buffer */
public Job FindInA1out(int id) {
return A1out.getJob(id);
}
public void AddtoA1out(Job job) {
A1out.EnqueueJob(0, job);
}
public Job RemoveFromA1out(int id) {
return A1out.DequeueJob(id);
}
public Job Reclaim(int id) {
Job newjob = new Job(id, 1);
int id_out;
if (FreeSlots > 0) {
// put X into a free page slot
return newjob;
}
else {
if (A1in.getLength() > Kin) {
Job A1inTail = A1in.DequeueFront();
id_out = A1inTail.JobID();
//delete A1inTail; ??? paged out of A1in buffer (but may still need to keep the job)
Job headerJob = new Job(id_out, 1);
A1out.EnqueueJob(0, headerJob);
if (A1out.getLength() > Kout) {
Job removedHeader = A1out.DequeueFront();
removedHeader = null;
}
// put X into the reclaimed page slot
return newjob;
}
else {
Job AmTail = Am.DequeueFront();
AmTail = null;
// put X into the reclaimed page slot
return newjob;
}
}
}
public void Request(int id) {
Job reclaimed = null;
if (Am.getJob(id) != null) {
Am.EnqueueJob(0, Am.DequeueJob(id));
}
else {
if (A1out.getJob(id) != null) {
reclaimed = Reclaim(id);
Am.EnqueueJob(0, reclaimed);
}
else {
if (A1in.getJob(id) != null) {
// do nothing
}
else {
reclaimed = Reclaim(id);
A1in.EnqueueJob(0, reclaimed);
}
}
}
}
}