-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryModule.cpp
More file actions
375 lines (314 loc) · 11.5 KB
/
MemoryModule.cpp
File metadata and controls
375 lines (314 loc) · 11.5 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "MemoryModule.h"
#include "part.h"
#include <cmath>
int utilities::getBitFromBitvector(char *vector, int bitNo) {
int entry = bitNo / (sizeof(char) * 8);
return ((vector[entry] >> (bitNo % (8 * sizeof(char)))) & 1);
}
void utilities::setBitInBitvector(char *vector, int bitNo, int value) {
int entry = bitNo / (sizeof(char) * 8);
char mask = 1 << (bitNo % (8 * sizeof(char)));
if (value == 0) {
vector[entry] &= (~mask);
} else {
vector[entry] |= mask;
}
}
MemoryInterface::~MemoryInterface() {
}
MemoryModule::~MemoryModule() {
}
CacheMemory::CacheMemory(int _numOfEntries, int _sizeOfEntry, SwapAlgorithm *_swapAlgorithm, MemoryInterface *_memoryModule) : numOfEntries(_numOfEntries), sizeOfEntry(_sizeOfEntry), swapAlgorithm(_swapAlgorithm), memoryModule(_memoryModule) {
data = new char**[numOfEntries];
for (int i = 0; i < numOfEntries; i++) {
data[i] = new char*[sizeOfEntry];
for (int j = 0; j < sizeOfEntry; j++) {
data[i][j] = new char[ClusterSize];
}
}
//allocata V and D bitvectors
Vcapacity = static_cast<int>(ceil(static_cast<double>(numOfEntries) / (sizeof(char) * 8)));
V = new char[Vcapacity];
memset(V, 0, Vcapacity);
Dcapacity = static_cast<int>(ceil(static_cast<double>(numOfEntries * sizeOfEntry) / (sizeof(char) * 8)));
D = new char[Dcapacity];
memset(D, 0, Dcapacity);
//allocate tag memory;
tagMemory = new TagMemory(numOfEntries);
}
int CacheMemory::getNewBlock(unsigned long blockNo) {
//get swap entry
int swapEntry = swapAlgorithm->getSwapEntry();
//check if block in swap entry was modified
if (utilities::getBitFromBitvector(V, swapEntry) == 1) {
for (int i = 0; i < sizeOfEntry; i++) {
if (utilities::getBitFromBitvector(D, swapEntry * sizeOfEntry + i) == 1) {
memoryModule->write(tagMemory->get(swapEntry) + i, data[swapEntry][i]);
}
}
}
//first get tag
unsigned long tag = (blockNo / sizeOfEntry) * sizeOfEntry;
//get blocks
for (int i = 0; i < sizeOfEntry; i++) {
memoryModule->read(tag + i, data[swapEntry][i]);
}
//update tag memory
tagMemory->write(swapEntry, tag);
//update V and D bits
utilities::setBitInBitvector(V, swapEntry, 1);
for (int i = 0; i < sizeOfEntry; i++) {
utilities::setBitInBitvector(D, swapEntry * sizeOfEntry + i, 0);
}
//return block entry
return swapEntry * sizeOfEntry + (blockNo % sizeOfEntry);
}
int CacheMemory::preOperation(unsigned long blockNo) {
//get entry from tag memory
int entry = tagMemory->compare(blockNo, sizeOfEntry);
//check if block is in cache
if (entry == -1 || utilities::getBitFromBitvector(V, entry / sizeOfEntry) == 0) {
entry = getNewBlock(blockNo);
}
swapAlgorithm->update(entry / sizeOfEntry);
return entry;
}
void CacheMemory::flush() {
//return all modified blocks
for (int i = 0; i < numOfEntries; i++) {
for (int j = 0; j < sizeOfEntry; j++) {
if (utilities::getBitFromBitvector(D, i * sizeOfEntry + j) == 1) {
memoryModule->write(tagMemory->get(i) + j, data[i][j]);
}
}
}
//invalidate V and D bit
for (int i = 0; i < numOfEntries; i++) {
utilities::setBitInBitvector(V, i, 0);
for (int j = 0; j < sizeOfEntry; j++) {
utilities::setBitInBitvector(D, i * sizeOfEntry + j, 0);
}
}
}
CacheMemory::~CacheMemory() {
//first flush()
flush();
//free data
for (int i = 0; i < numOfEntries; i++) {
for (int j = 0; j < sizeOfEntry; j++) {
delete[] data[i][j];
}
delete[] data[i];
}
delete[] data;
data = nullptr;
//free D and V bitvectors
delete[] V;
delete[] D;
//free tag memory
delete tagMemory;
//free swap algorithm
delete swapAlgorithm;
}
ByteCacheMemory::ByteCacheMemory(int _numOfEntries, int _sizeOfEntry, MemoryInterface *_memoryModule) : ByteCacheMemory(_numOfEntries, _sizeOfEntry, new LRU(_numOfEntries), _memoryModule) {
}
ByteCacheMemory::ByteCacheMemory(int _numOfEntries, int _sizeOfEntry, SwapAlgorithm *_swapAlgorithm, MemoryInterface *_memoryModule) : CacheMemory(_numOfEntries, _sizeOfEntry, _swapAlgorithm, _memoryModule), lastAccessedBlock(~0), lastAccessedEntry(~0) {
}
int ByteCacheMemory::read(unsigned long position, char *buffer) {
unsigned long blockNo = position / ClusterSize;
if (lastAccessedBlock != blockNo) {
lastAccessedEntry = preOperation(blockNo);
lastAccessedBlock = blockNo;
}
(*buffer) = data[lastAccessedEntry / sizeOfEntry][lastAccessedEntry % sizeOfEntry][position % ClusterSize];
return 0;
}
int ByteCacheMemory::write(unsigned long position, const char *buffer) {
unsigned long blockNo = position / ClusterSize;
if (lastAccessedBlock != blockNo) {
lastAccessedEntry = preOperation(blockNo);
lastAccessedBlock = blockNo;
}
data[lastAccessedEntry / sizeOfEntry][lastAccessedEntry % sizeOfEntry][position % ClusterSize] = (*buffer);
utilities::setBitInBitvector(D, lastAccessedEntry, 1);
return 0;
}
BlockCacheMemory::BlockCacheMemory(int _numOfEntries, int _sizeOfEntry, MemoryInterface *_memoryModule) : BlockCacheMemory(_numOfEntries, _sizeOfEntry, new LRU(_numOfEntries), _memoryModule) {
}
BlockCacheMemory::BlockCacheMemory(int _numOfEntries, int _sizeOfEntry, SwapAlgorithm *_swapAlgorithm, MemoryInterface *_memoryModule) : CacheMemory(_numOfEntries, _sizeOfEntry, _swapAlgorithm, _memoryModule) {
}
int BlockCacheMemory::read(unsigned long blockNo, char *buffer) {
int entry = preOperation(blockNo);
memcpy(buffer, data[entry / sizeOfEntry][entry % sizeOfEntry], ClusterSize);
return 0;
}
int BlockCacheMemory::write(unsigned long blockNo, const char *buffer) {
int entry = preOperation(blockNo);
memcpy(data[entry / sizeOfEntry][entry % sizeOfEntry], buffer, ClusterSize);
utilities::setBitInBitvector(D, entry, 1);
return 0;
}
ThreadSafeBlockCache::ThreadSafeBlockCache(int _numOfEntries, int _sizeOfEntrie, int _lruRequestBufferCapacity, MemoryInterface *_memoryModule) : BlockCacheMemory(_numOfEntries, _sizeOfEntrie, new ThreadSafeLRU(_numOfEntries, _lruRequestBufferCapacity), _memoryModule) {
monitor = new UpgradeMonitor();
}
ThreadSafeBlockCache::~ThreadSafeBlockCache() {
delete monitor;
}
int ThreadSafeBlockCache::read(unsigned long blockNo, char *buffer) {
monitor->startRead();
int ret = BlockCacheMemory::read(blockNo, buffer);
monitor->endRead();
return ret;
}
int ThreadSafeBlockCache::write(unsigned long blockNo, const char *buffer) {
monitor->startRead();
int ret = BlockCacheMemory::write(blockNo, buffer);
monitor->endRead();
return ret;
}
int ThreadSafeBlockCache::getNewBlock(unsigned long blockNo) {
monitor->startWrite();
unsigned long tag = (blockNo / sizeOfEntry) * sizeOfEntry;
int entry = tagMemory->compare(tag, sizeOfEntry);
if (entry == -1) {
entry = CacheMemory::getNewBlock(blockNo);
}
monitor->endWrite();
return entry;
}
void ThreadSafeBlockCache::flush() {
monitor->startRead();
monitor->startWrite();
CacheMemory::flush();
monitor->endWrite();
monitor->endRead();
}
WriteRequest::WriteRequest(unsigned long _blockNo, const char *_buffer) : blockNo(_blockNo), buffer(_buffer) {
}
WriteRequest::~WriteRequest() {
delete[] buffer;
}
WriteRequestHandler::WriteRequestHandler(BlockBuffer *_buffer, MemoryInterface *_memoryModule, ModuleMonitor *_monitor) : buffer(_buffer), memoryModule(_memoryModule), monitor(_monitor) {
}
WriteRequestHandler::~WriteRequestHandler() {
waitToComplete();
}
void WriteRequestHandler::handleRequest() {
monitor->startWrite();
WriteRequest *request = buffer->get();
memoryModule->write(request->blockNo, request->buffer);
delete request;
//this thread is responsible for block and request
monitor->endWrite();
}
BufferedWriter::BufferedWriter(int _lineSize, MemoryInterface *_memoryModule) : memoryModule(_memoryModule) {
buffer = new BlockBuffer(_lineSize);
monitor = new ModuleMonitor(_lineSize);
handler = new WriteRequestHandler(buffer, memoryModule, monitor);
handler->start();
}
BufferedWriter::~BufferedWriter() {
handler->stop();
delete handler;
delete monitor;
delete buffer;
}
int BufferedWriter::read(unsigned long blockNo, char *buffer) {
monitor->startRead();
int ret = memoryModule->read(blockNo, buffer);
monitor->endRead();
return ret;
}
int BufferedWriter::write(unsigned long blockNo, const char *bbuffer) {
char *tmpBuffer = new char[ClusterSize];
memcpy(tmpBuffer, bbuffer, ClusterSize);
WriteRequest *request = new WriteRequest(blockNo, tmpBuffer);
buffer->put(request);
monitor->putBlock();
handler->putRequest();
return 0;
}
PartitionWrapper::PartitionWrapper(Partition *_partition, unsigned long _numOfClusters) : partition(_partition), numOfClusters(_numOfClusters) {
}
int PartitionWrapper::read(unsigned long clusterNo, char *buffer) {
int ret = 1;
if (clusterNo > numOfClusters) {
memset(buffer, 20, ClusterSize);
ret = 0;
} else {
ret = partition->readCluster(clusterNo, buffer);
}
return ret;
}
int PartitionWrapper::write(unsigned long clusterNo, const char *buffer) {
int ret = 1;
if (clusterNo > numOfClusters) {
ret = 0;
} else {
ret = partition->writeCluster(clusterNo, buffer);
}
return ret;
}
MemoryMapper::MemoryMapper(MemoryInterface *_memoryModule, CacheMemory *_indexCache, unsigned long _indexCluster) : memoryModule(_memoryModule), indexCache(_indexCache), indexCluster(_indexCluster) {
}
int MemoryMapper::read(unsigned long blockNo, char *buffer) {
return memoryModule->read(LogToPh(blockNo), buffer);
}
int MemoryMapper::write(unsigned long blockNo, const char *buffer) {
return memoryModule->write(LogToPh(blockNo), buffer);
}
void MemoryMapper::setIndexCluster(unsigned long _indexCluster) {
indexCluster = _indexCluster;
}
unsigned long MemoryMapper::LogToPh(unsigned long logBlockNo) {
unsigned long offset = 0;
if (logBlockNo < 256) {
offset = indexCluster * ClusterSize + logBlockNo * sizeof(unsigned long);
} else {
int entry = (logBlockNo - 256) / 512;
unsigned long indexClusterNo = 0;
char *indexClusterNoPointer = reinterpret_cast<char*>(&indexClusterNo);
unsigned long indexClusterOffset = indexCluster * ClusterSize + (256 + entry) * sizeof(unsigned long);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->read(indexClusterOffset + i, indexClusterNoPointer + i);
}
offset = indexClusterNo * ClusterSize + (logBlockNo - 256) % 512 * sizeof(unsigned long);
}
unsigned long phBlockNo = 0;
char *phBlockNoPointer = reinterpret_cast<char*>(&phBlockNo);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->read(offset + i, phBlockNoPointer + i);
}
return phBlockNo;
}
DataMemoryChecker::DataMemoryChecker(MemoryInterface *_memoryModule) : memoryModule(_memoryModule) {
}
int DataMemoryChecker::read(unsigned long blockNo, char *buffer) {
if (blockNo == 0) {
memset(buffer, 15, ClusterSize);//izbrisi ovo ti je trebalo za testiranje
return 1;
}
return memoryModule->read(blockNo, buffer);
}
int DataMemoryChecker::write(unsigned long blockNo, const char *buffer) {
if (blockNo == 0) {
return 1;
}
return memoryModule->write(blockNo, buffer);
}
IndexMemoryChecker::IndexMemoryChecker(MemoryInterface *_memoryModule) : memoryModule(_memoryModule) {
}
int IndexMemoryChecker::read(unsigned long clusterNo, char *buffer) {
if (clusterNo == 0) {
memset(buffer, 0, ClusterSize);
} else {
memoryModule->read(clusterNo, buffer);
}
return 1;
}
int IndexMemoryChecker::write(unsigned long clusterNo, const char *buffer) {
if (clusterNo != 0) {
memoryModule->write(clusterNo, buffer);
}
return 1;
}