-
Notifications
You must be signed in to change notification settings - Fork 3
/
tlb.c
424 lines (339 loc) · 9.4 KB
/
tlb.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
/*
* Author: Akhil
* Date: 18/3/20
*
* Contains the function definiions of the TLB ADT.
*/
#include <stdio.h>
#include "dataTypes.h"
#include "configuration.h"
#include "tlb.h"
#include "utility.h"
//extern inline void fileNotNull(FILE *file, char *fileName);
extern TLBL1 l1TLB;
extern TLBL2 l2TLB;
extern FILE *outputFile;
// To test the functions.
/*
int
main(int argc, char const *argv[])
{
FILE *outputFile = fopen(OUTPUT_FILE_NAME, "w");
fclose(outputFile);
outputFile = fopen(OUTPUT_FILE_NAME, "a");
TLBL1Flush();
TLBL1Print();
TLBL2Flush();
TLBL2Print();
int error;
int pageNum = 0;
int frameNum;
frameNum = TLBL1Search(pageNum, &error);
frameNum = TLBL2Search(pageNum, &error);
int count, index;
index = 0;
count = TLBL1UpdateLru(index);
printf("count=%d\n", count);
index = TLBL1GetLruIndex();
printf("index=%d\n", index);
index = 4;
count = TLBL1UpdateLru(index);
index = TLBL1Update(1, 1);
TLBL1Print();
fclose(outputFile);
return 0;
}
*/
// -----------------------------------------------------------------
// L1-TLB
/*
* This function invalidates all the entries in the L1 TLB.
*/
void
TLBL1Flush()
{
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterating through all the entries in the TLB.
{
// Setting the validInvalid to 0 makes the entry invalid.
l1TLB.entries[i].validInvalidBit = 0;
}
return;
}
/*
* This function prints the current data in the L1 TLB.
*/
void
TLBL1Print()
{
fprintf(outputFile, "-------------------------\n");
fprintf(outputFile, "L1-TLB Info\n");
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterates through every entry in the TLB.
{
fprintf(outputFile, "Index=%d, validInvalidBit=%d, pageNum=%d, frameNum=%d, lruCount=%d\n", i, l1TLB.entries[i].validInvalidBit, l1TLB.entries[i].pageNum, l1TLB.entries[i].frameNum, l1TLB.entries[i].lruCount);
}
return;
}
/*
* For a given pageNum, it returns the frame number if a valid record for that pageNum exists.
*/
unsigned int
TLBL1Search(unsigned int pageNum, unsigned int *error)
{
fprintf(outputFile, "\nL1-TLB: Searching for pageNum=%d\n", pageNum);
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterating through all the entries in the TLB.
{
// The entry has to be valid and it also has to match the pageNum we are looking for.
if (l1TLB.entries[i].validInvalidBit && pageNum == l1TLB.entries[i].pageNum)
{
fprintf(outputFile, "pageNum=%d found in l1TLB at index=%d, frameNum=%d\n", pageNum, i, l1TLB.entries[i].frameNum);
fflush(outputFile);
//fclose(outputFile);
// Update the LRU counts.
TLBL1UpdateLru(i);
*error = 0;
return l1TLB.entries[i].frameNum;
}
}
//fprintf(outputFile, "not found TLB1 ---\n");
fprintf(outputFile, "pageNum=%d not found in l1TLB\n", pageNum);
*error = ERROR_PAGE_NUM_NOT_FOUND;
return -1;
}
/*
* This function returns the LRU index in L1-TLB.
* This function does not check if the LRU is valid or not.
* It also does not check for placement before replacement.
* Following the counter implementation of LRU.
*/
int
TLBL1GetLruIndex()
{
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterating through all the entries in the TLB.
{
// Counter implementation of LRU makes the LRU have a count of 0.
if (l1TLB.entries[i].lruCount == 0)
{
return i;
}
}
}
/*
* This function updates the LRU counts in L1-TLB.
* This function does not care about the validInvalidBit.
* Returns the number of entries which had a greater LRU count.
* Counter implementation of LRU.
*/
int
TLBL1UpdateLru(int index)
{
int currentCount = l1TLB.entries[index].lruCount;
int count = 0;
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterating through all the entries in the TLB.
{
// Counter implementation of LRU requires all LRU counts greater than the one being accessed now to be decremented.
if (l1TLB.entries[i].lruCount > currentCount)
{
l1TLB.entries[i].lruCount--;
count++;
}
}
l1TLB.entries[index].lruCount = NUM_ENTRIES_IN_L1_TLB - 1;
return count;
}
/*
* Returns the index of the first entry that is invalid.
* If not found, returns -1.
*/
int
TLBL1GetFirstInvalidEntry()
{
for (int i = 0; i < NUM_ENTRIES_IN_L1_TLB; ++i) // Iterating through all the entries in the TLB.
{
if (l1TLB.entries[i].validInvalidBit == 0)
{
return i;
}
}
return -1;
}
/*
* Finds an entry in the L1-TLB and writes onto it.
* Returns the index of the new valid entry.
*/
int
TLBL1Update(unsigned int pageNum, unsigned int frameNum)
{
/* Algorithm
Placement first and then replacement.
To find if placement if possible, call GetFirstInvalidEntry()
If index != -1:
// Placement
Use the index that was returned.
Else:
// Replacement
Find the LRU index and replace it.
Else
End */
// Finding the first invalid entry in the TLB for placement.
int index = TLBL1GetFirstInvalidEntry();
// Finding the LRU index if no invalid entries.
if (index == -1) // Replacement.
{
index = TLBL1GetLruIndex();
fprintf(outputFile, "L1-TLB: Replacement in index=%d\n", index);
}
else // Placement.
{
fprintf(outputFile, "L1-TLB: Placement in index=%d\n", index);
}
// Initialising the new entry.
l1TLB.entries[index].frameNum = frameNum;
l1TLB.entries[index].pageNum = pageNum;
l1TLB.entries[index].validInvalidBit = 1;
// We do not change the LRU count as of now, but when the same instruction runs again, it will be changed then.
return index;
}
// -----------------------------------------------------------------
// L2-TLB
/*
* This function invalidates all the entries in the L2 TLB.
*/
void
TLBL2Flush()
{
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
l2TLB.entries[i].validInvalidBit = 0;
}
return;
}
/*
* This function prints the current data in the L2 TLB.
*/
void
TLBL2Print()
{
fprintf(outputFile, "-------------------------\n");
fprintf(outputFile, "L2-TLB Info\n");
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
fprintf(outputFile, "Index=%d, validInvalidBit=%d, pageNum=%d, frameNum=%d, lruCount=%d\n", i, l2TLB.entries[i].validInvalidBit, l2TLB.entries[i].pageNum, l2TLB.entries[i].frameNum, l2TLB.entries[i].lruCount);
}
return;
}
/*
* For a given pageNum, it returns the frame number if a valid record for that pageNum exists.
*/
unsigned int
TLBL2Search(unsigned int pageNum, unsigned int *error)
{
fprintf(outputFile, "\nL2-TLB: Searching for pageNum=%d\n", pageNum);
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
if (l2TLB.entries[i].validInvalidBit && pageNum == l2TLB.entries[i].pageNum)
{
fprintf(outputFile, "L2-TLB: pageNum=%d found in l2TLB at index=%d, frameNum=%d\n", pageNum, i, l2TLB.entries[i].frameNum);
fflush(outputFile);
//fclose(outputFile);
// Update the LRU counts.
TLBL2UpdateLru(i);
*error = 0;
return l2TLB.entries[i].frameNum;
}
}
fprintf(outputFile, "L2-TLB: pageNum=%d not found in l2TLB\n", pageNum);
*error = ERROR_PAGE_NUM_NOT_FOUND;
return -1;
}
/*
* This function returns the LRU index in L2-TLB.
* This function does not check if the LRU is valid or not.
* It also does not check for placement before replacement.
* Counter implementation of LRU.
*/
int
TLBL2GetLruIndex()
{
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
if (l2TLB.entries[i].lruCount == 0)
{
return i;
}
}
}
/*
* This function updates the LRU counts in L2-TLB.
* This function does not care about the validInvalidBit.
* Returns the number of entries which had a greater LRU count.
* Counter implementation of LRU.
*/
int
TLBL2UpdateLru(int index)
{
int currentCount = l2TLB.entries[index].lruCount;
int count = 0;
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
// Decrement LRU values of those ones which have a greater count than the current one.
if (l2TLB.entries[i].lruCount > currentCount)
{
l2TLB.entries[i].lruCount--;
count++;
}
}
l2TLB.entries[index].lruCount = NUM_ENTRIES_IN_L2_TLB - 1;
return count;
}
/*
* Returns the index of the first entry that is invalid.
* If not found, returns -1.
*/
int
TLBL2GetFirstInvalidEntry()
{
for (int i = 0; i < NUM_ENTRIES_IN_L2_TLB; ++i) // Iterating through all the entries in the TLB.
{
if (l2TLB.entries[i].validInvalidBit == 0)
{
return i;
}
}
return -1;
}
/*
* Finds an entry in the L2-TLB and writes onto it.
* Returns the index of the new valid entry.
*/
int
TLBL2Update(unsigned int pageNum, unsigned int frameNum)
{
/* Algorithm
Placement first and then replacement.
To find if placement if possible, call GetFirstInvalidEntry()
If index != -1:
// Placement
Use the index that was returned.
Else:
// Replacement
Find the LRU index and replace it.
Else
End */
// Finding the first invalid entry to see if placement is possible.
int index = TLBL2GetFirstInvalidEntry();
// Finding the LRU index if no invalid entries.
if (index == -1) // Replacement.
{
index = TLBL2GetLruIndex();
fprintf(outputFile, "L2-TLB: Replacement in index=%d\n", index);
}
else
{
fprintf(outputFile, "L2-TLB: Placement in index=%d\n", index);
}
l2TLB.entries[index].frameNum = frameNum;
l2TLB.entries[index].pageNum = pageNum;
l2TLB.entries[index].validInvalidBit = 1;
// We do not change the LRU count as of now, but when the same instruction runs again, it will be changed then.
return index;
}