-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.cc
More file actions
784 lines (718 loc) · 24.6 KB
/
executor.cc
File metadata and controls
784 lines (718 loc) · 24.6 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#include <iostream>
#include <cmath>
#include <sys/time.h>
#include <vector>
#include <cstdlib>
#include <stdint.h>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <string>
#include <time.h>
#include <chrono>
#include <random>
#include <climits>
#include <functional>
#include "parameter.h"
#include "executor.h"
using namespace bufmanager;
// Parameter initialization
Buffer *Buffer::buffer_instance;
long Buffer::max_buffer_size = 0;
int Buffer::buffer_hit = 0;
int Buffer::buffer_miss = 0;
int Buffer::read_io = 0;
int Buffer::write_io = 0;
int Buffer::pageSize = 0;
char Buffer::disk_write_char = '0';
chrono::duration <double, milli> Buffer::timing;
const vector<string> algorithms{ "LRU", "LRUWSR", "FIFO", "CFLRU"};
double window_size = .3; //default
Buffer::Buffer(Simulation_Environment *_env) {
// lru_candidate is for LRU list
//chrono::duration <double, milli> (chrono::steady_clock::now() - chrono::steady_clock::now());
lru_candidate.clear();
max_buffer_size = _env->buffer_size_in_pages;
pageSize = _env->page_size;
window_size = (_env->perct_window) * .01;
// initialize the cache
// for(int i = 0; i < max_buffer_size; i++) bufferpool.push_back(make_pair(-1, false));
}
Buffer *Buffer::getBufferInstance(Simulation_Environment *_env) {
if (buffer_instance == 0) buffer_instance = new Buffer(_env);
return buffer_instance;
}
// search in the bufferpool
int WorkloadExecutor::search(Buffer* buffer_instance, int pageId, int algorithm) {
switch (algorithm) {
// 1 LRU
case 1: {
// the bufferpool still empty
if(buffer_instance->bufferpool.size() == 0) {
return -1;
}
for(int i = 0; i < buffer_instance->max_buffer_size; i++) {
// find the page in the bufferpool, hit
if(buffer_instance->bufferpool[i].first == pageId) {
buffer_instance->buffer_hit += 1;
return i;
}
}
break;
}
// 2 LRU_WSR
case 2: {
if(buffer_instance->bufferpool_wsr.size() == 0) {
return -1;
}
for(int i = 0; i < buffer_instance->max_buffer_size; i++) {
// find the page in the bufferpool, hit
if(get<0>(buffer_instance->bufferpool_wsr[i]) == pageId) {
buffer_instance->buffer_hit += 1;
return i;
}
}
break;
}
// 3 FIFO
case 3: {
// bufferpool is empty
if(buffer_instance->bufferpool.size() == 0) {
return -1;
}
for(int i = 0; i < buffer_instance->max_buffer_size; i++) {
// find the page in the bufferpool, hit
if(buffer_instance->bufferpool[i].first == pageId) {
buffer_instance->buffer_hit += 1;
return i;
}
}
break;
}
//4 -> CFLRU
case 4: {
// the bufferpool still empty
if(buffer_instance->bufferpool.size() == 0) {
return -1;
}
//search through both the working part and clean-first part for a hit, just in case
for(int i = 0; i < buffer_instance->max_buffer_size; i++) {
// find the page in the bufferpool, hit
if(buffer_instance->bufferpool[i].first == pageId) {
buffer_instance->buffer_hit += 1;
return i;
}
}
break;
}
}
// did not find the page, miss
buffer_instance->buffer_miss += 1;
return -1;
}
// allocate empty frame and read the page into that frame
int WorkloadExecutor::read(Buffer* buffer_instance, int pageId, int algorithm) {
// TODO
switch (algorithm) {
// 1 LRU
case 1: {
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
if(pos != -1) {
// found, only need to update lru
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
buffer_instance->lru_candidate.push_front(pageId);
}
// miss
else {
// cache is not full
if(cur_size < capacity) {
// read the page from disk, mark the page clean
// TODO
buffer_instance->bufferpool.push_back(make_pair(pageId, false));
// add read_io
// add disk read functionality
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// cache is full
else {
// find the position to evict
// cout << "enter LRU func!!!" << endl;
int pos = buffer_instance->LRU();
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
// add disk write functionality
}
// erase the target page
buffer_instance->bufferpool[pos].first = -1;
buffer_instance->bufferpool[pos].second = false;
// put new page in the blank
buffer_instance->bufferpool[pos].first = pageId;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// update lru
buffer_instance->lru_candidate.push_front(pageId);
}
break;
}
// 1 LRUWSR
case 2: {
int cur_size = buffer_instance->bufferpool_wsr.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
if(pos != -1) {
// found, only need to update lru
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
buffer_instance->lru_candidate.push_front(pageId);
}
// miss
else {
// cache is not full
if(cur_size < capacity) {
// read the page from disk, mark the page clean
buffer_instance->bufferpool_wsr.push_back(make_tuple(pageId, false, false));
// add read_io
// add disk read functionality
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// cache is full
else {
// find the position to evict
// cout << "enter LRU func!!!" << endl;
int pos = buffer_instance->LRUWSR();
// if the page is dirty, write the page into the disk
if(get<1>(buffer_instance->bufferpool_wsr[pos]) == true) {
// add disk write functionality
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
}
// erase the target page
get<0>(buffer_instance->bufferpool_wsr[pos]) = -1;
get<1>(buffer_instance->bufferpool_wsr[pos]) = false;
// put new page in the blank
get<0>(buffer_instance->bufferpool_wsr[pos]) = pageId;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// update lru
buffer_instance->lru_candidate.push_front(pageId);
}
break;
}
// FIFO
case 3: {
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
// if page is already in the bufferpool do nothing except read from bufferpool
if(pos != -1) {
}
// otherwise this is a miss
else {
// not full, no need to evict
if(cur_size < capacity) {
buffer_instance->bufferpool.push_back(make_pair(pageId, false));
buffer_instance->fifo_candidates.push_back(pageId);
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// full, perform eviction
else {
// get position to replace (pops in FIFO function)
int replacement_pos = buffer_instance->FIFO();
// add incoming page to FIFO queue
buffer_instance->fifo_candidates.push_back(pageId);
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[replacement_pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
// add disk write functionality
}
// erase the target page
buffer_instance->bufferpool[replacement_pos].first = -1;
buffer_instance->bufferpool[replacement_pos].second = false;
// put new page in the blank
buffer_instance->bufferpool[replacement_pos].first = pageId;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// need to add disk read when we eventually add that functionality
}
break;
}
// CFLRU
case 4:{
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
if(pos != -1) {
// found, only need to update bufferpool
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
//pushing it to the very front bc it will be in the working zone there
buffer_instance->lru_candidate.push_front(pageId);
}
else{//bufferpool miss
if(cur_size < capacity) { //space in cache
// read the page from disk, mark the page clean
//add to the front so it is the first page in the working zone
buffer_instance->bufferpool.push_back(make_pair(pageId, false));
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
else { //cache is full
// find the position to evict
int pos = buffer_instance->CFLRU();
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
}
// erase the target page
buffer_instance->bufferpool[pos].first = -1;
buffer_instance->bufferpool[pos].second = false;
// put new page in the blank
buffer_instance->bufferpool[pos].first = pageId;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// update lru
buffer_instance->lru_candidate.push_front(pageId);
}
break;
}
}
return -1;
}
int WorkloadExecutor::write(Buffer* buffer_instance, int pageId, int algorithm) {
switch (algorithm) {
case 1: {
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
// if page is already in the bufferpool, update it
if(pos != -1) {
// found, update lru
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
buffer_instance->lru_candidate.push_front(pageId);
// marking the page as dirty bc it's been updated
// buffer_instance->bufferpool.push_back(make_pair(pageId, true));
// BUG FIXED
buffer_instance->bufferpool[pos].second = true;
}
// otherwise this is a miss
else {
// if bufferpool isnt empty, simply add the page
if(cur_size < capacity) {
// read the page from disk, mark the page dirty since we are updatin git
buffer_instance->bufferpool.push_back(make_pair(pageId, true));
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// otherwise, the cache is full
else {
// find the position to evict
int pos = buffer_instance->LRU();
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
}
// erase the target page
buffer_instance->bufferpool[pos].first = -1;
buffer_instance->bufferpool[pos].second = false;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
// put new page in the blank, and then set that page to be dirty
buffer_instance->bufferpool[pos].first = pageId;
buffer_instance->bufferpool[pos].second = true;
}
// update lru
// BUG FIXED
buffer_instance->lru_candidate.push_front(pageId);
// return -1;
}
break;
}
// LRU_WSR
case 2: {
int cur_size = buffer_instance->bufferpool_wsr.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
// if page is already in the bufferpool, update it
if(pos != -1) {
// found, update lru
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
buffer_instance->lru_candidate.push_front(pageId);
// marking the page as dirty bc it's been updated
// BUG FIXED
get<1>(buffer_instance->bufferpool_wsr[pos]) = true;
// make sure cold flag is cleared
get<2>(buffer_instance->bufferpool_wsr[pos]) = false;
}
// otherwise this is a miss
else {
// if bufferpool isnt empty, simply add the page
if(cur_size < capacity) {
// read the page from disk, mark the page dirty since we are updatin git
buffer_instance->bufferpool_wsr.push_back(make_tuple(pageId, true, false));
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// otherwise, the cache is full
else {
// find the position to evict
int pos = buffer_instance->LRUWSR();
// if the page is dirty, write the page into the disk
if(get<1>(buffer_instance->bufferpool_wsr[pos]) == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
}
// erase the target page in the bufferpool
get<0>(buffer_instance->bufferpool_wsr[pos]) = -1;
// put new page in the blank, and then set that page to be dirty
get<0>(buffer_instance->bufferpool_wsr[pos]) = pageId;
get<1>(buffer_instance->bufferpool_wsr[pos]) = true;
get<2>(buffer_instance->bufferpool_wsr[pos]) = false;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
// get<1>(buffer_instance->bufferpool_wsr[pos]) = true;
}
// update lru
// BUG FIXED
buffer_instance->lru_candidate.push_front(pageId);
// return -1;
}
break;
}
// FIFO
case 3: {
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
// if page is already in the bufferpool, update it
if(pos != -1) {
// found, mark dirty
buffer_instance->bufferpool[pos].second = true;
}
// otherwise this is a miss
else {
if(cur_size < capacity) {
buffer_instance->bufferpool.push_back(make_pair(pageId, true));
buffer_instance->fifo_candidates.push_back(pageId);
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// full, time to replace
else {
// get position to replace (pops in FIFO function)
int replacement_pos = buffer_instance->FIFO();
buffer_instance->fifo_candidates.push_back(pageId);
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[replacement_pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
// add disk write functionality
}
// erase the target page put new page in the blank
buffer_instance->bufferpool[replacement_pos].second = true;
buffer_instance->bufferpool[replacement_pos].first = pageId;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
}
break;
}
// CFLRU
case 4: {
//cflru write logic
int cur_size = buffer_instance->bufferpool.size();
int capacity = buffer_instance->max_buffer_size;
int pos = search(buffer_instance, pageId, algorithm);
if(pos != -1) {
// found, update bufferpool
deque<int>::iterator it = buffer_instance->lru_candidate.begin();
while(*it != pageId) it++;
buffer_instance->lru_candidate.erase(it);
//pushing page to front to be first element in working zone
buffer_instance->lru_candidate.push_front(pageId);
// marking the page as dirty bc it's been updated
buffer_instance->bufferpool[pos].second = true;
}
else{ //page not in cache
// if bufferpool isnt empty, simply add the page
if(cur_size < capacity) {
// read the page from disk, mark the page dirty since we are updatin git
//adding to front to be first element in working zone
buffer_instance->bufferpool.push_back(make_pair(pageId, true));
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
}
// otherwise, the cache is full
else {
// find the position to evict
int pos = buffer_instance->CFLRU();
// if the page is dirty, write the page into the disk
if(buffer_instance->bufferpool[pos].second == true) {
buffer_instance->write_io += 1;
diskOp(buffer_instance, 1, pageId);
}
// erase the target page
buffer_instance->bufferpool[pos].first = -1;
buffer_instance->bufferpool[pos].second = false;
// add read_io
buffer_instance->read_io += 1;
diskOp(buffer_instance, 0, pageId);
// put new page in the blank, and then set that page to be dirty
buffer_instance->bufferpool[pos].first = pageId;
buffer_instance->bufferpool[pos].second = true;
}
buffer_instance->lru_candidate.push_front(pageId);
}
break;
}
}
return -1;
}
// Need to calculate disk sector and page size to accomplish proper functionality
// Perform disk read or write
void WorkloadExecutor::diskOp(Buffer* buffer_instance, int operation, int pageID) {
Simulation_Environment* _env = Simulation_Environment::getInstance();
// read
if(operation == 0) {
// add in memory variable to the stack
string reader;
// Calculate pointer in the disk
buffer_instance->disk.seekg(buffer_instance->pageSize * pageID, std::ios::beg);
for(int i = 0; i < buffer_instance->pageSize; i++) {
// simulate reading byte by byte into cache/memory/registers
reader += char(buffer_instance->disk.get());
}
}
// write
else if(operation == 1) {
// increment to next ascii value
buffer_instance->disk_write_char = char((int(buffer_instance->disk_write_char) % 26)+int('a'));
char c = buffer_instance->disk_write_char;
// Adjust pointer
buffer_instance->disk.seekg(buffer_instance->pageSize * pageID, std::ios::beg);
for(int j = 0; j < buffer_instance->pageSize - 1; j++) {
// write byte-by-byte
buffer_instance->disk.put(c);
}
// add endline as the last character in the page
buffer_instance->disk.put('\n');
}
return;
}
void WorkloadExecutor::writeDisk(Buffer* buffer_instance) {
Simulation_Environment* _env = Simulation_Environment::getInstance();
string str = "";
char c;
srand(time(NULL));
for(int i = 0; i < _env->disk_size_in_pages; i++) {
str = "";
for(int j = 0; j < buffer_instance->pageSize - 1; j++) {
c = 'a' + rand()%26;
str = str + c;
}
buffer_instance->disk << str << endl;
}
return;
}
// return the evict position in bufferpool
int Buffer::LRU() {
// get the least used page id
int index = -1;
if(lru_candidate.size() > 0) {
int pageId = lru_candidate.back();
// delete it from the LRU list
lru_candidate.pop_back();
for(int i = 0; i < bufferpool.size(); i++) {
if(pageId == bufferpool[i].first) {
index = i;
break;
}
}
}
return index;
}
//return the evict position in bufferpool for cflru algorithm
int Buffer::CFLRU(){
int cur_size = lru_candidate.size();
int real_window_size = ceil(window_size * cur_size);
/*
the windowsize for the clean_first section is the ceilingcurrent amount of
items in buffer/3, so that it is flexible based on the current demands
(never too big or too small)
*/
int index = -1;
int count = 0;
int pageId = -1;
//going to go through the candidates in reverse order in the given window looking for a clean page
for(auto i = lru_candidate.rbegin(); count < real_window_size; i++){
if(buffer_instance->bufferpool[i[0]].second == false){ //first clean page
//this is the page to evict
pageId = i[0];
lru_candidate.erase(lru_candidate.begin() + (lru_candidate.size() - count - 1)); //erase this element from the cflru
break;
}
count++;
}
if(pageId == -1){
//if there was not a clean page in the window, then you will just take the least accessed page
pageId = lru_candidate.back();
// delete it from the LRU list
lru_candidate.pop_back();
}
for(int i = 0; i < bufferpool.size(); i++) {
if(pageId == bufferpool[i].first) {
index = i;
break;
}
}
return index;
}
// return the evict position in bufferpool
// second chance algo
int Buffer::LRUWSR() {
int index = -1;
// get the lru page id
int victim = lru_candidate.back();
// mapping the page id to actual page in the bufferpool
for(int i = 0; i < bufferpool_wsr.size(); i++) {
if(victim == get<0>(bufferpool_wsr[i])) {
index = i;
break;
}
}
// while the victim is dirty
while(get<1>(bufferpool_wsr[index])) {
// if the cold flag is set
if(get<2>(bufferpool_wsr[index])) {
// exit the while loop
break;
} else {
// move victim to MRU position
lru_candidate.pop_back();
lru_candidate.push_front(victim);
// set its cold flag
get<2>(bufferpool_wsr[index]) = true;
// replace victim with next LRU page
victim = lru_candidate.back();
// update index mapping
for(int i = 0; i < bufferpool_wsr.size(); i++) {
if(victim == get<0>(bufferpool_wsr[i])) {
index = i;
break;
}
}
}
}
// remove victim from the LRU list
lru_candidate.pop_back();
return index;
}
// return the evict position in bufferpool
// First-In-First-Out
int Buffer::FIFO() {
// get the page position to be replace and pop it
int index = -1;
// get page ID to be replaced
int pageToReplace = fifo_candidates.front();
fifo_candidates.pop_front();
// parse bufferpool to find it
for(int i = 0; i < bufferpool.size(); i++) {
if(pageToReplace == bufferpool[i].first) {
index = i;
break;
}
}
// position in bufferpool to be replaced by incoming page
return index;
}
int Buffer::printBuffer() {
int counter = 0;
if(buffer_instance->bufferpool_wsr.empty()) {
for(std::pair<int, bool> page : buffer_instance->bufferpool) {
std::cout << "Page: " << counter << " PageID: " << page.first
<< " Dirty-Bit: " << page.second << endl;
counter++;
}
} else {
for(std::tuple<int, bool, bool> page : buffer_instance->bufferpool_wsr) {
std::cout << "Page: " << counter << " PageId: " << get<0>(page)
<< " Dirty-Bit: " << get<1>(page) << " Cold Flag: " << get<2>(page) << endl;
counter++;
}
}
return 0;
}
int Buffer::printStats() {
auto end = chrono::steady_clock::now();
Simulation_Environment* _env = Simulation_Environment::getInstance();
std::cout << "******************************************************" << endl;
std::cout << "Printing Stats..." << endl;
std::cout << "Number of operations: " << _env->num_operations << endl;
std::cout << "Buffer Hit: " << buffer_hit << endl;
std::cout << "Buffer Miss: " << buffer_miss << endl;
std::cout << "Read IO: " << read_io << endl;
std::cout << "Write IO: " << write_io << endl;
std::cout << "Global Clock: " << Buffer::timing.count() << "ms" << endl;
std::cout << "******************************************************" << endl;
return 0;
}
void Buffer::writeResults() {
Simulation_Environment* _env = Simulation_Environment::getInstance();
string buffer_size_in_pages = std::to_string(_env->buffer_size_in_pages); // b
string disk_size_in_pages = std::to_string(_env->disk_size_in_pages); // n
string num_operations = std::to_string(_env->num_operations); // x
string perct_reads = std::to_string(_env->perct_reads); // e
string perct_writes = std::to_string(_env->perct_writes);
string skewed_perct = std::to_string(_env->skewed_perct); //s
string skewed_data_perct = std::to_string(_env->skewed_data_perct); // d
string pin_mode = std::to_string(_env->pin_mode); // p
string verbosity = std::to_string(_env->verbosity); // v
string page_size = std::to_string(_env->page_size); //k
int algo = _env->algorithm; // a
string u = "_";
// Make filename such that it can be parsed for stats of interest
string filename = "b-" + buffer_size_in_pages + u + "n-" + disk_size_in_pages + u + "x-"
+ num_operations + u + "e-" + perct_reads + perct_writes + u + "s-" + skewed_perct
+ "d-" + skewed_data_perct + u + "v-" + verbosity + u + "a-" + algorithms.at(algo-1)
+ "k-" + page_size;
// write the file with the run statistics
ofstream statFile;
statFile.open("workload_suite/runs/" + filename + ".txt");
statFile << num_operations << endl;
statFile << buffer_hit << endl;
statFile << buffer_miss << endl;
statFile << read_io << endl;
statFile << write_io << endl;
statFile << Buffer::timing.count();
statFile.close();
return;
}