forked from jaskaran1312/dstn-assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
420 lines (329 loc) · 12.3 KB
/
main.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
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "hardware.h"
#include "frametable.h"
#include "init.h"
#include "l1cache.h"
#include "l2cache.h"
#include "lru.h"
#include "main.h"
#include "mainMemory.h"
#include "segmentTable.h"
#include "thrashing.h"
#include "tlb.h"
#include "victimcache.h"
#define MAX 18446744073709551615
void saveResult() {
FILE *fptr = fopen("result.txt", "w");
if (fptr == NULL) {
printf("Could not open result.txt\n");
return;
}
fprintf(fptr, "-----Statistics-----\n\n");
fprintf(fptr, "Number of Hits in TLB: %" PRIu32 "\n", tlbHit);
fprintf(fptr, "Number of References in TLB: %" PRIu32 "\n", tlbReferences);
fprintf(fptr, "Hit Ratio in TLB: %f\n", (double)tlbHit / (double)tlbReferences);
fprintf(fptr, "Number of Hits in L1 Data Cache: %" PRIu32 "\n", l1dHit);
fprintf(fptr, "Number of References in L1 Data Cache: %" PRIu32 "\n", l1dReferences);
fprintf(fptr, "Hit Ratio in L1 Data Cache: %f\n", (double)l1dHit / (double)l1dReferences);
fprintf(fptr, "Number of Hits in L1 Instruction Cache: %" PRIu32 "\n", l1iHit);
fprintf(fptr, "Number of References in L1 Instruction Cache: %" PRIu32 "\n", l1iReferences);
fprintf(fptr, "Hit Ratio in L1 Instruction Cache: %f\n", (double)l1iHit / (double)l1iReferences);
fprintf(fptr, "Number of Hits in Victim Cache: %" PRIu32 "\n", victimCacheHit);
fprintf(fptr, "Number of References in Victim Cache: %" PRIu32 "\n", victimCacheReferences);
fprintf(fptr, "Hit Ratio in Victim Cache: %f\n", (double)victimCacheHit / (double)victimCacheReferences);
fprintf(fptr, "Number of Hits in L2 Cache: %" PRIu32 "\n", l2Hit);
fprintf(fptr, "Number of References in L2 Cache: %" PRIu32 "\n", l2References);
fprintf(fptr, "Hit Ratio in L2 Cache: %f\n", (double)l2Hit / (double)l2References);
fprintf(fptr, "Number of Context Switches: %" PRIu32 "\n", contextSwitch);
fprintf(fptr, "Number of Page Faults: %" PRIu32 "\n", pageFault);
fprintf(fptr, "Number of times Thrashing occured: %" PRIu8 "\n", thrashing);
fclose(fptr);
return;
}
int fetchData(int64_t pa, struct Hardware *hardware, int selector) {
// If selector is zero we work with l1i else l1d
//First try L1 cache
if(!selector) l1iReferences++;
else l1dReferences ++;
if (!fetchL1Cache(pa, hardware, selector)){
if(!selector) l1iHit++;
else l1dHit ++;
printf("L1 Hit\n");
return 1; //Hit in L1
}
//Miss in L1; try Victim cache
victimCacheReferences++;
if (!fetchVictimCache(pa, hardware)) {
//Hit in Victim
victimCacheHit++;
printf("Victim Hit\n");
updateL1Cache(pa, hardware, 1, selector);
return 0;
}
//Miss in Victim; try L2
l2References++;
if (!fetchL2Cache(pa, hardware)) {
//Hit in L2
l2Hit++;
printf("L2 Hit\n");
updateL1Cache(pa, hardware, 0, selector);
return 0;
}
fflush(stdout);
//Miss in L2
updateL2Cache(pa, hardware);
updateL1Cache(pa, hardware, 0, selector);
return 0;
}
int readWriteSelector(int selector) {
if (selector == 1) { // Choosing write instructions from Data Segment
int makeRead = rand()%2;
return makeRead;
}
}
void prePage(struct Hardware *hardware, char *fileList[], int numFiles)
{
int filePos[numFiles];
//will be reinitialised in simulation, so don't need to reset later
for (int i = 0; i < numFiles; i++)
filePos[i] = 0;
FILE *fp[numFiles];
for (int i = 0; i < numFiles; i++)
fp[i] = fopen(fileList[i], "r");
// int processCompleted = 0;
int currProcess = 0;
struct Process *process[numFiles];
for (int i = 0; i < numFiles; i++)
process[i] = processInit(i);
printf("PREPAGING BEGINS. . .\n");
for(int jk=0;jk<numFiles;jk++) //iterate through files
{
fflush(stdout);
for (int i = 0; i < 2; i++) //run for first 2 instructions of each process
{
if (filePos[currProcess] == -1 || process[currProcess]->state == 0 )
{
break; //break into outer while
}
fflush(stdout);
char va[9];
fseek(fp[currProcess], filePos[currProcess], SEEK_SET);
if (fgets(va, 9, fp[currProcess]) == NULL)
{
//WILL BE ENTERED IF BY SOME MIRACLE, A PROCESS IS 2 INSTRUCTIONS LONG
printf("Process %d Completed\n", currProcess);
invalidateTLB(hardware, currProcess); //invalidate tlb
// process Completed
filePos[currProcess] = -1;
break;
}
filePos[currProcess] += 10;
printf("Process: %d, Logical Address: %s\n", currProcess, va);
fflush(stdout);
int64_t virtualAddress;
sscanf(va, "%lx", &virtualAddress);
printf("Virtual address: %ld\n", virtualAddress);
fflush(stdout);
// Fetch base from the segment table
int64_t pdpa = fetchBase(virtualAddress, process[currProcess], hardware);
printf("Base address %ld\n", pdpa);
fflush(stdout);
// Fetch linear address
int64_t la = fetchLinearAddress(virtualAddress);
printf("Linear address %ld\n", la);
fflush(stdout);
// tlbReferences++;
int64_t pa = fetchTLB(hardware, virtualAddress, process[currProcess]->pid);
printf("pa from tlb %ld\n", pa);
fflush(stdout);
// tlbHit++;
// Fetch physical address
if (pa == -1){
// tlbHit--;
while(pa == -1){
pa = fetchMainMemory(la, pdpa, hardware, process[currProcess]);
// pageFault++;
printf("PAGE FAULT\n");
fflush(stdout);
}
}
printf("Physical address %ld\n", pa);
fflush(stdout);
// Update TLB
updateTLB(hardware, virtualAddress, pa, process[currProcess]->pid);
fflush(stdout);
}
//context switch
currProcess = (currProcess + 1) % numFiles;
}
for (int i = 0; i < numFiles; i++)
fclose(fp[i]);
printf("PREPAGING ENDS.\n");
fflush(stdout);
return;
}
// Process numbering starts from 0
void simulate(struct Hardware *hardware, char *fileList[], int numFiles) {
int filePos[numFiles];
for (int i = 0; i < numFiles; i++)
filePos[i] = 0;
FILE *fp[numFiles];
for (int i = 0; i < numFiles; i++)
fp[i] = fopen(fileList[i], "r");
int processCompleted = 0;
int currProcess = 0;
uint32_t instructionCount = 0;
struct Process *process[numFiles];
for (int i = 0; i < numFiles; i++)
process[i] = processInit(i);
while (1) {
fflush(stdout);
for (int i = 0; i < 2000; i++) {
if (filePos[currProcess] == -1 || process[currProcess]->state == 0 ) {
break;
}
fflush(stdout);
instructionCount++;
if (instructionCount % 5000 == 0)
shiftMMLRU(hardware);
if (instructionCount % 50000 == 0) {
if (isThrashing(hardware)) {
thrashing++;
short processToSuspend = rand() % numFiles;
process[processToSuspend]->state = 0;
} else {
for (int i = 0; i < numFiles; i++) {
if (process[i]->state == 0)
process[i]->state = 1;
}
}
}
printf("Instruction count %" PRIu32 "\n", instructionCount);
char va[9];
fseek(fp[currProcess], filePos[currProcess], SEEK_SET);
if (fgets(va, 9, fp[currProcess]) == NULL) {
printf("Process %d Completed\n", currProcess);
invalidateTLB(hardware, currProcess); //invalidate tlb
processCompleted++;
filePos[currProcess] = -1;
break;
}
filePos[currProcess] += 10;
printf("Process: %d, Logical Address: %s\n", currProcess, va);
fflush(stdout);
int64_t virtualAddress;
sscanf(va, "%lx", &virtualAddress);
printf("Virtual address: %ld\n", virtualAddress);
fflush(stdout);
// selector = 0 for code segment, 1 for data segment
int selector = fetchSegment(virtualAddress);
int makeRead = 1; // 1 -> read, 0 -> write
makeRead = readWriteSelector(selector);
// Fetch base from the segment table
int64_t pdpa = fetchBase(virtualAddress, process[currProcess], hardware);
printf("Base address %ld\n", pdpa);
fflush(stdout);
// Fetch linear address
int64_t la = fetchLinearAddress(virtualAddress);
printf("Linear address %ld\n", la);
fflush(stdout);
tlbReferences++;
int64_t pa = fetchTLB(hardware, virtualAddress, process[currProcess]->pid);
printf("pa from tlb %ld\n", pa);
fflush(stdout);
tlbHit++;
// Fetch physical address
if (pa == -1){
tlbHit--;
while(pa == -1){
pa = fetchMainMemory(la, pdpa, hardware, process[currProcess]);
pageFault++;
printf("PAGE FAULT\n");
fflush(stdout);
}
}
printf("Physical address %ld\n", pa);
fflush(stdout);
// Update TLB
updateTLB(hardware, virtualAddress, pa, process[currProcess]->pid);
// Simulate in Memory management
fflush(stdout);
int dataFound = 0;
while(!dataFound){
dataFound = fetchData(pa, hardware, selector);
}
printf("Got Data\n");
fflush(stdout);
}
contextSwitch++;
currProcess = (currProcess + 1) % numFiles;
if (processCompleted == numFiles) {
printf("Simulation Completed\n");
for (int i = 0; i < numFiles; i++)
fclose(fp[i]);
saveResult();
return;
}
}
}
int main() {
struct Hardware *hardware = (struct Hardware *)malloc(sizeof(struct Hardware));
tlbInit(hardware); // tlb initialization
l1CacheInit(hardware); // l1cache initialization
victimCacheInit(hardware); // victimcache intialization
mainMemoryInit(hardware); // mainmemory initialization
l2CacheInit(hardware); // l2cache initialization
frameTableInit(hardware); //frameTable initialization
printf("##############################################\n");
printf("########## Memory Management System ##########\n");
printf("##############################################\n\n");
printf("For Debugging, check logs.txt\nFor Result, check result.txt\n\n");
printf("Simulation Running...\n\n");
FILE *out = freopen("logs.txt", "w", stdout);
DIR *dir;
struct dirent *dirEntry;
int numFiles = 0;
dir = opendir("./InputFiles/");
while ((dirEntry = readdir(dir)) != NULL) {
if (dirEntry->d_type != DT_REG)
continue;
else
numFiles++;
}
printf("Number of Input Files = %d\n", numFiles);
rewinddir(dir);
char *fileList[numFiles];
int i = 0;
while ((dirEntry = readdir(dir)) != NULL) {
if (dirEntry->d_type != DT_REG)
continue;
else {
char path[120] = "InputFiles/";
fileList[i] = (char *)malloc(strlen(dirEntry->d_name) + strlen(path) + 1);
strcat(path, dirEntry->d_name);
strncpy(fileList[i], path, strlen(path));
fileList[i][strlen(path)] = '\0';
i++;
}
}
if (closedir(dir) == -1) {
perror("closedir");
return 1;
}
for (int i = 0; i < numFiles; i++)
printf("%s\n", fileList[i]);
fflush(stdout);
prePage(hardware, fileList, numFiles); //prepaging
fflush(stdout);
simulate(hardware, fileList, numFiles);
fclose(out);
freopen("/dev/tty", "a", stdout);
printf("Simulation Completed!\n");
return 0;
}