-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCache.java
167 lines (141 loc) · 4.63 KB
/
Cache.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
import java.util.*;
public class Cache
{
private int bSize;
private int victim;
private Vector<byte[]> cache;
private class Entry
{
public int frame;
public boolean refbit;
public boolean dirtybit;
public Entry()
{
frame = -1;
refbit = false;
dirtybit = false;
}
}
private Entry[] pageTable = null;
public Cache(int blockSize, int cacheBlocks)
{
bSize = blockSize;
victim = 0;
cache = new Vector<byte[]>();
pageTable = new Entry[cacheBlocks];
for (int i = 0; i < cacheBlocks; i++)
{
byte[] block = new byte[blockSize];
cache.add(block);
pageTable[i] = new Entry();
}
}
private int findFreePage()
{
for (int i = 0; i < pageTable.length; i++) // For each page in the table,
{
if (pageTable[i].frame == -1) // If the page is invalid,
return i; // Return its position.
}
return -1;
}
private int nextVictim()
{
// Entry.refbit is a bool that shows that a block has been recently used.
// Entry.dirtybit is a bool that shows that a block has been modified.
int wrapMark = victim; // Place marker to indicate full rotation.
boolean allowDirty = false;
boolean falsifyRefBit = false;
while (true)
{
victim++;
if (victim >= pageTable.length) victim = 0;
if (!pageTable[victim].refbit && (!pageTable[victim].dirtybit || allowDirty))
return victim;
if (victim == wrapMark) // If we've completed a cycle around the cache,
{
if (allowDirty) falsifyRefBit = true; // If we've completed the second cycle, start removing second chances.
allowDirty = !allowDirty; // Allow overwriting of dirty bits
}
if (falsifyRefBit) pageTable[victim].refbit = false;
}
}
private void writeBack(int victimEntry)
{
if (pageTable[victimEntry].frame != -1)
{
SysLib.rawwrite(pageTable[victimEntry].frame, cache.elementAt(victimEntry));
pageTable[victimEntry].dirtybit = false;
}
}
public synchronized boolean read(int blockId, byte buffer[])
{
if (blockId < 0)
return false;
for (int i = 0; i < pageTable.length; i++)
{
if (pageTable[i].frame == blockId)
{
byte[] block = cache.elementAt(i);
System.arraycopy(block, 0, buffer, 0, bSize);
pageTable[i].refbit = true;
return true;
}
}
int freePage = findFreePage();
if (freePage == -1) freePage = nextVictim();
if (pageTable[freePage].dirtybit) writeBack(freePage);
SysLib.rawread(blockId, buffer);
byte[] block = new byte[bSize];
System.arraycopy(buffer, 0, block, 0, bSize);
cache.set(freePage, block);
pageTable[freePage].frame = blockId;
pageTable[freePage].refbit = true;
return true;
}
public synchronized boolean write(int blockId, byte buffer[])
{
if (blockId < 0)
return false;
for (int i = 0; i < pageTable.length; i++)
{
if (pageTable[i].frame == blockId)
{
byte[] block = new byte[bSize];
System.arraycopy(buffer, 0, block, 0, bSize);
cache.set(i, block);
pageTable[i].refbit = true;
pageTable[i].dirtybit = true;
}
}
int freePage = findFreePage();
if (freePage == -1) freePage = nextVictim();
if (pageTable[freePage].dirtybit) writeBack(freePage);
SysLib.rawwrite(blockId, buffer);
byte[] block = new byte[bSize];
System.arraycopy(buffer, 0, block, 0, bSize);
cache.set(freePage, block);
pageTable[freePage].frame = blockId;
pageTable[freePage].refbit = true;
pageTable[freePage].dirtybit = true;
return true;
}
public synchronized void sync()
{
for (int i = 0; i < pageTable.length; i++)
{
if (pageTable[i].dirtybit) writeBack(i);
}
SysLib.sync();
}
public synchronized void flush()
{
for (int i = 0; i < pageTable.length; i++)
{
if (pageTable[i].dirtybit) writeBack(i);
pageTable[i].refbit = false;
pageTable[i].frame = -1;
}
SysLib.sync();
}
}