-
Notifications
You must be signed in to change notification settings - Fork 3
/
driverCode.c
428 lines (335 loc) · 15.9 KB
/
driverCode.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <stdio.h>
#include "cache.h"
#include "configuration.h"
#include <stdlib.h>
//#include "dataTypes.h"
//#include "frameTable.h"
//#include "pageTable.h"
#include "pcb.h"
//#include "segmentTable.h"
//#include "tlb.h"
#include "utility.h"
#define NUM_LINES_BEFORE_CONTEXT_SWITCH 200
PCB pcbObj[30];
segmentTable* GDT;
//TLB objects of L1 and L2
TLBL1 tlbL1Obj;
TLBL2 tlbL2Obj;
//Cache objects L1 and L2
CacheL1 cacheL1obj[2]; //L1 instruction and data cache
CacheL2 cacheL2obj;
frameTable frameTableObj;
long long current_time = 0;
int main()
{
int n; //Number of processes
scanf("%d", &n);
char SegAddrInputFileName[n][100];
char LinearAddrInputFileName[n][100];
for(int i = 0; i < n; ++i)
{
scanf("%s", LinearAddrInputFileName[i]);
}
for(int i = 0; i < n; ++i)
{
scanf("%s", SegAddrInputFileName[i]);
}
//Flushing the TLBs initially
TLBL1Flush();
TLBL2Flush();
//Initializing Cache
initL1Cache();
initL2Cache();
//Initialize Frame Table
initFrameTable();
//Global descriptor Table initialize
GDT = initGDTable();
//Initializing PCBs of all Processes
//Initialize segment Table for each Process
for(int i = 0; i < n; ++i)
{
//Initializes PCB
initPCB(pcbObj[i], LinearAddrInputFileName[i], SegAddrInputFileName[i]);
}
//Opening outputFile
FILE *outputFile = fopen(OUTPUT_FILE_NAME, "a");
for(int i = 0; i < n; ++i)
{
if(numProcessAlive == 0)
{
break;
}
//Changes the status of the process
if(getState(PCB[i]) == TERMINATED)
{
++current_time;
continue;
}
else if(getState(PCB[i]) == WAIT)
{
++current_time;
if(current_time-swapStartTime < WAITING_TIME)
{
continue;
}
else
{
setState(PCB[i], READY);
++current_time;
}
}
setState(PCB[i], RUNNING);
++current_time;
fprintf(outputFile, "\n\n\nDriver: Process-%d running in the processor.\n", i);
for(int j = 0; j < NUM_LINES_BEFORE_CONTEXT_SWITCH; ++j)
{
//time initialized to zero after every context switch
long time = 0;
//Reads the Memory reference from input file
unsignned int inputAddr = readAddr(PCB[i].LinearAddrInputFile[i], Addr);
//Store Segment Number and whether read/write
char write;
int4 segNum = readSegNum(PCB[i].SegNumInputFile[i], &write);
if(inputAddr == -1) //To check if we have reached the end of the file
{
setState(PCB[i], TERMINATED);
--numProcessAlive;
break;
}
//First we find the correct mapping of Logical Address to Physical Address
// Split the inputAddr to find the requested pageNum.
int requestedPageOffset = inputAddr & 1023; // (pow(2, 10) - 1), Since page size is 2**10B.
int requestedPageNum = inputAddr >> 10; // Since we need to discard the least significant 10b.
// Based on the MSB of the segNum, decide which cache to hit (data or instr).
bool dataCache = (segNum == 0) ? true : false;
// Search this pageNum in both levels of TLB.
fprintf(outputFile, "Driver: Memory access for %x requested by process-%d\n", Addr, i);
int frameNum = TLBL1Search(requestedPageNum);
time += L1_TLB_SEARCH_TIME;
fprintf(outputFile, "Driver: Searched through L1 TLB. Time cost: %d\n", L1_TLB_SEARCH_TIME);
if (frameNum < 0)
{
time += L2_TLB_SEARCH_TIME;
fprintf(outputFile, "Driver: Did not find required data in L1 TLB. Searched through L2 TLB. Time cost: %d\n", L2_TLB_SEARCH_TIME);
frameNum = TLBL2Search(requestedPageNum);
// Get the data from MM.
// --------------------------------
if (frameNum < 0) // If the data is not present in TLBL2 also.
{
int level = 3;
if(dataCache) //if memory reference is data
{
//
frameNum = searchPageTable(PCB[i].segTableObj->entries[segNum], ptrToPageFaultTable, inputAddr, &requestedPageNum, &level);
printf("After returing to driver searchPageTable: level = %d, pageFaultPageNumber = %d\n", level, &requestedPageNo);
//allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
if(frameNum == -2)
{
//Invalid Address
continue;
}
if(frameNum < 0)
{
//allocateFrame()
//fseek() for previous instruction
break;
//level = 2;
//frameNum = searchPageTable(PCB[i].segTableObj->entries[segNum], ptrToPageFaultTable, inputAddr, requestedPageNum, &level);
//allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
//if(frameNum < 0)
//{
// level = 1;
// frameNum = searchPageTable(PCB[i].segTableObj->entries[segNum], ptrToPageFaultTable, inputAddr, requestedPageNum, &level);
// allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
//}
}
}
//else //if memory reference is an instruction
//{
// frameNum = searchPageTable(GDT[PCB[i].GDTindex]->pageTableObj, ptrToPageFaultTable, inputAddr, &requestedPageNum, &level);
// printf("After returing to driver searchPageTable: level = %d, pageFaultPageNumber = %d\n", level, &requestedPageNo);
// allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
// if(frameNum == -2)
// {
// //Invalid Address
// continue;
// }
// if(frameNum < 0)
// {
// level = 2;
// frameNum = searchPageTable(GDT[PCB[i].GDTindex], ptrToPageFaultTable, inputAddr, requestedPageNum, &level);
// allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
// if(frameNum < 0)
// {
// level = 1;
// frameNum = searchPageTable(GDT[PCB[i].GDTindex], ptrToPageFaultTable, inputAddr, requestedPageNum, &level);
// allocateFrame(i, segNum, *ptrToPageFaultPageTable, &requestedpageNo, &level);
// }
// }
//}
fprintf(outputFile, "Driver: Searched through PageTable, found the frameNumber for the required memory reference\n");
time += MM_SEARCH_TIME;
fprintf(outputFile, "Driver: Did not find required Addr mapping in L2 TLB. Searched through MM PageTable. Time cost: %d\n", MM_SEARCH_TIME);
TLBL2Update(requestedPageNum, frameNum);
time += L2_TLB_UPDATE_TIME;
fprintf(outputFile, "Driver: Updated L2 TLB. Update time: %d\n", L2_TLB_UPDATE_TIME);
frameNum = TLBL2Search(requestedPageNum);
time += L2_TLB_SEARCH_TIME;
fprintf(outputFile, "Driver: Re-searched through L2 TLB. Time cost: %d\n", L2_TLB_SEARCH_TIME);
}
TLBL1Update(requestedPageNum, frameNum);
time += L1_TLB_UPDATE_TIME;
fprintf(outputFile, "Driver: Updated L1 TLB. Update time: %d\n", L1_TLB_UPDATE_TIME);
frameNum = TLBL1Search(requestedPageNum);
time += L1_TLB_SEARCH_TIME;
fprintf(outputFile, "Driver: Re-searched through L1 TLB. Time cost: %d\n", L1_TLB_SEARCH_TIME);
}
//Physical Addr (Frame Number) obtained. Now search for data
physicalAddr = (frameNum << 10) & (inputAddr & (pow(2, 10) - 1));
// Split the frameNum into index, tag and offset according to both levels of cache's size.
l1CacheOffset = physicalAddr & (pow(2, 5) - 1);
l1CacheIndex = (physicalAddr & (pow(2, 10) - 1)) >> 5;
l1CacheTag = physicalAddr >> 10;
l2CacheOffset = physicalAddr & (pow(2, 6) - 1);
l2CacheIndex = (physicalAddr & (pow(2, 12) - 1)) >> 6;
l2CacheTag = physicalAddr >> 12;
int retValue;
if (write)
{
// Since L1 cache is write through, the driver function only writes to the L1 cache.
// And the writeL1Cache() calls writeL2Cache().
retValue = writeL1Cache(l1CacheIndex, l1CacheTag, j, l2CacheIndex, l2CacheTag, dataCache);
time += L1_CACHE_WRITE_TIME;
if (retValue < 0)
{
//-------------------------------
// Error Codes.
if (retValue == ERROR_WRITE_FAILED_NO_TAG_MATCH)
{
fprintf(outputFile, "Driver: Write to L1 Cache Failed. Tag did not match. Time cost: %d\n", L1_CACHE_WRITE_TIME);
retValue = searchL2Cache(l2CacheIndex, l2CacheTag);
time += L2_CACHE_SEARCH_TIME;
if(retValue < 0)
{
fprintf(outputFile, "Driver: Search in L2 failed! Time cost: %d\n", L2_CACHE_SEARCH_TIME);
// Write to Main Memory
status = writeInMainMemory(physicalAddr);
time += MAIN_MEMORY_WRITE_TIME;
fprintf(outputFile, "Driver: Write in Main Memory successfull! %d\n",MAIN_MEMORY_WRITE_TIME);
if(status == ERROR_WRITE_FAILED_NO_PERMISSION)
{
fprintf(outputFile, "Driver: Error! write permission not for this memory address\n");
continue;
}
updateL2Cache(l2CacheIndex, l2CacheTag, write, 0, dataCache);
time += L2_CACHE_UPDATE_TIME;
fprintf(outputFile, "Driver: Updated L2 Cache. Time cost: %d\n", L2_CACHE_WRITE_TIME);
updateL1Cache(l1CacheIndex, l1CacheTag, write, 0, dataCache);
time += L1_CACHE_UPDATE_TIME;
fprintf(outputFile, "Driver: Updated L1 Cache. Time cost: %d\n", L1_CACHE_WRITE_TIME);
writeL1Cache(l1CacheIndex, l1CacheTag, 0, dataCache);
time += L1_CACHE_WRITE_TIME;
fprintf(outputFile, "Driver: Write to L1 Cache successfully completed.\n");
}
else
{
fprintf(outputFile, "Driver: Search in L2 successfull! Search Time: %d\n", L2_CACHE_SEARCH_TIME);
updateL1Cache(l1CacheIndex, l1CacheTag, write, 0, dataCache);
time += L1_CACHE_UPDATE_TIME;
fprintf(outputFile, "Driver: Updated L1 Cache. Time cost: %d\n", L1_CACHE_WRITE_TIME);
writeL1Cache(l1CacheIndex, l1CacheTag, 0, dataCache);
time += L1_CACHE_WRITE_TIME;
fprintf(outputFile, "Driver: Write to L1 Cache successfully completed.\n");
}
}
else if (retValue == ERROR_WRITE_FAILED_NO_PERMISSION)
{
fprintf(outputFile, "Driver: Error! write permission not for this memory address\n")
}
else if (retValue == ERROR_CANNOT_WRITE_IN_INSTR_CACHE)
{
fprintf(outputFile, "Driver: Error! write permission not for this memory address\n")
}
//-------------------------------
}
else
{
time += L1_CACHE_WRITE_TIME;
fprintf(outputFile, "Driver: Write to L1 Cache successfully completed.\n");
continue;
}
}
else
{
//if instruction is read
// Since L1 cache is look aside, we essentially search L1 and L2 cache simultaneously.
// But this cannot be shown in the simulation, hence we consider the time taken to do the search to be MIN() of the two of them.
// For look-through we would have added the time they take to search.
retValue1 = searchL1Cache(l1CacheIndex, l1CacheTag, dataCache);
retValue2 = searchL2Cache(l2CacheIndex, l2CacheTag);
if (retValue1 >= 0 && retValue2 >= 0)
{
// Return the data to the processor.
//return retValue;
previousActionTime = min(L1_CACHE_SEARCH_TIME, L2_CACHE_SEARCH_TIME);
time += previousActionTime;
fprintf(outputFile, "Driver: Found the required data in L1 cache.\n");
fprintf(outputFile, "Search time taken: %d.\n", previousActionTime);
}
else if (retValue1 < 0 && retValue2 < 0)
{
// Not there in both the levels of cache.
previousActionTime = max(L1_CACHE_SEARCH_TIME, L2_CACHE_SEARCH_TIME);
time += previousActionTime;
fprintf(outputFile, "Driver: Did NOT find the required data in both L1 and L2 cache\n");
fprintf(outputFile, "Search time taken: %d.\n", previousActionTime);
fprintf(outputFile, "Driver: Will update L1 and L2 caches.\n");
// search in the MM and update caches.
int status = readFromMainMem(physicalAddr);
if(status == -1)
{
fprintf(outputFile, "Driver: Error reading From Main Memory\n");
continue;
}
else
{
fprintf(outputFile, "Driver: Data read from Main Memory\n");
}
//----------------------------
updateL2Cache(l2CacheIndex, l2CacheTag, write, 0, dataCache);
// Update the LRU in L2 cache
time += L2_CACHE_UPDATE_TIME;
fprintf(outputFile, "Driver: L2 Cache update time: %d\n", L2_CACHE_UPDATE_TIME);
updateL1Cache(l1CacheIndex, l1CacheTag, write, 0, dataCache);
// Update the LRU in L1 cache
time += L1_CACHE_UPDATE_TIME;
fprintf(outputFile, "Driver: L1 Cache update time: %d\n", L1_CACHE_UPDATE_TIME);
}
else if (retValue1 < 0 && retValue2 >= 0)
{
// Present in L2 cache, but not in L1.
previousActionTime = max(L1_CACHE_SEARCH_TIME, L2_CACHE_SEARCH_TIME);
time += previousActionTime;
fprintf(outputFile, "Driver: Did NOT find the required data in L1 cache, but found it in L2 cache.\n");
fprintf(outputFile, "Search time taken: %d.\n", previousActionTime);
fprintf(outputFile, "Driver: Will update L1 Cache.\n");
// Update L1 cache.
updateL1Cache(l1CacheIndex, l1CacheTag, write, 0, dataCache);
time += L1_CACHE_UPDATE_TIME;
// Update the LRU in L1 cache if required.
// ----------------------------
fprintf(outputFile, "Driver: L1 Cache update time: %d\n", L1_CACHE_UPDATE_TIME);
}
} // End of that memory access.
// Updating the time taken by the previous memory access.
currentTime += time;
pcb[i].runTime += time;
++current_time;
}
TLBL1flush();
TLBL2flush();
LFUAging();
fprintf(outputFile, "\n\n\nSIMULATION COMPLETED. ALL PROCESSES FINISHED EXECUTION.\n\n\n");
for(int i = 0; i < n; ++i)
fclose(inputFile[i]);
}