-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUK.java
165 lines (121 loc) · 2.92 KB
/
LRUK.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
// auther: Sun Siyue
package org.apache.derby.impl.services.cache;
final class LRUK extends Policy{
private int K_value = 2;
LRUK(int maxSize) {
super(maxSize);
// TODO Auto-generated constructor stub
Hist = new ArrayList<Item>;
CRP = 5000;
RIP = 200;
trimtime = (int) (System.currentTimeMillis() / 1000L);
}
synchronized void addEntry(CacheEntry entry) {
count++;
entries.add(new Item(entry, (double) count));
count++;
//incrUsed(job.JobSize());
CacheEntry i = entry;
//int id = job.JobID();
CacheEntry histjob = null;
histjob = Hist.getJob(id);
if (histjob == null) {
histjob = new Job(id,1);
Hist.EnqueueJob(0, histjob);
// for (int i=0;i<=1;i++)
histjob.k[1] = 0;
}
else {
histjob.k[1] = histjob.k[0];
}
int curtime = (int) (System.currentTimeMillis() / 1000L);
histjob.k_time[1] = curtime;
histjob.k[0] = count;
histjob.k_time[0] = curtime;
job.last = count;
}
synchronized CacheEntry findVictim(boolean forUncache) {
Item victim = findMin();
if (forUncache)
entries.remove(victim);
return victim.entry;
int min, id, victim=0;
Node curr = pq.jobList;
TrimHistory();
min = count;
if (curr!=null) {
id = curr.getJob().JobID();
victim = id;
curr = curr.next;
}
while (curr!=null) {
Job pqjob = curr.getJob();
id = pqjob.JobID();
Job histjob = Hist.getJob(id);
if ((count - pqjob.last > CRP) && (histjob.k[1] < min)) {
victim = id;
min = histjob.k[1];
if (min == 0) break;
}
curr = curr.next;
}
return pq.DequeueJob(victim);
}
void incrHit(CacheEntry e) {
count++;
if (count == 2) {
adjustPrio(e, count);
}
count++;
hit++;
byteHit += size;
int cprp;
Job histjob = Hist.getJob(id), pqjob = pq.getJob(id);
if (count - pqjob.last > CRP) {
cprp = pqjob.last - histjob.k[0];
// for (int i=0;i<=1;i++)
histjob.k[1] = histjob.k[0] + cprp;
histjob.k[0] = count;
int curtime = (int) (System.currentTimeMillis() / 1000L);;
histjob.k_time[1] = curtime;
histjob.k_time[0] = curtime;
pqjob.last = count;
}
else {
pqjob.last = count;
}
}
String Name() {
return "LRUK";
}
//***************************************************************
//child class functions
public void TrimHistory(){
int curtime = (int) (System.currentTimeMillis() / 1000L);
if ((curtime - trimtime) < RIP) {
//cout << "no trim" << endl;
return;
}
//cout << "TrimHistory" << endl;
trimtime = curtime;
int id;
Node curr = Hist.jobList;
double diff1, diff2;
Job histjob = null;
while (curr!=null) {
histjob = curr.getJob();
id = histjob.JobID();
diff1 = curtime - histjob.k_time[0];
diff2 = curtime - histjob.k_time[1];
curr = curr.next;
if ((diff1 > RIP) && (diff2 > RIP) && (pq.getJob(id) == null)) {
Job jobToDelete = Hist.DequeueJob(id);
jobToDelete = null;
}
}
return;
}
public ArrayList<Item> Hist;
int CRP, RIP;
int trimtime;
}