forked from jimstevens2001/HybridSim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHybridSystem.cpp
2341 lines (1912 loc) · 69.8 KB
/
HybridSystem.cpp
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
* Copyright (c) 2010-2011,
* Jim Stevens, Paul Tschirhart, Ishwar Singh Bhati, Mu-Tien Chang, Peter Enns,
* Elliott Cooper-Balis, Paul Rosenfeld, Bruce Jacob
* University of Maryland
* Contact: jims [at] cs [dot] umd [dot] edu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*********************************************************************************/
#include "HybridSystem.h"
using namespace std;
namespace HybridSim {
HybridSystem::HybridSystem(uint id, string ini)
{
if (ini == "")
{
hybridsim_ini = "";
char *base_path = getenv("HYBRIDSIM_BASE");
if (base_path != NULL)
{
hybridsim_ini.append(base_path);
hybridsim_ini.append("/");
}
hybridsim_ini.append("../HybridSim/ini/hybridsim.ini");
}
else
{
hybridsim_ini = ini;
}
string pattern = "/ini/";
string inipathPrefix;
unsigned found = hybridsim_ini.rfind(pattern); // Find the last occurrence of "/ini/"
assert (found != string::npos);
inipathPrefix = hybridsim_ini.substr(0,found);
inipathPrefix.append("/");
iniReader.read(hybridsim_ini);
if (ENABLE_LOGGER)
log.init();
// Make sure that there are more cache pages than pages per set.
assert(CACHE_PAGES >= SET_SIZE);
systemID = id;
cerr << "Creating DRAM with " << dram_ini << "\n";
uint64_t dram_size = (CACHE_PAGES * PAGE_SIZE) >> 20;
dram_size = (dram_size == 0) ? 1 : dram_size; // DRAMSim requires a minimum of 1 MB, even if HybridSim isn't going to use it.
dram_size = (OVERRIDE_DRAM_SIZE == 0) ? dram_size : OVERRIDE_DRAM_SIZE; // If OVERRIDE_DRAM_SIZE is non-zero, then use it.
dram = DRAMSim::getMemorySystemInstance(dram_ini, sys_ini, inipathPrefix, "resultsfilename", dram_size);
cerr << "Creating Flash with " << flash_ini << "\n";
flash = NVDSim::getNVDIMMInstance(1,flash_ini,"ini/def_system.ini",inipathPrefix,"");
cerr << "Done with creating memories" << endl;
// Set up the callbacks for DRAM.
typedef DRAMSim::Callback <HybridSystem, void, uint, uint64_t, uint64_t> dramsim_callback_t;
DRAMSim::TransactionCompleteCB *read_cb = new dramsim_callback_t(this, &HybridSystem::DRAMReadCallback);
DRAMSim::TransactionCompleteCB *write_cb = new dramsim_callback_t(this, &HybridSystem::DRAMWriteCallback);
dram->RegisterCallbacks(read_cb, write_cb, NULL);
// Set up the callbacks for NVDIMM.
typedef NVDSim::Callback <HybridSystem, void, uint64_t, uint64_t, uint64_t, bool> nvdsim_callback_t;
NVDSim::Callback_t *nv_read_cb = new nvdsim_callback_t(this, &HybridSystem::FlashReadCallback);
NVDSim::Callback_t *nv_write_cb = new nvdsim_callback_t(this, &HybridSystem::FlashWriteCallback);
NVDSim::Callback_t *nv_crit_cb = new nvdsim_callback_t(this, &HybridSystem::FlashCriticalLineCallback);
flash->RegisterCallbacks(nv_read_cb, nv_crit_cb, nv_write_cb, NULL);
// Need to check the queue when we start.
check_queue = true;
// No delay to start with.
delay_counter = 0;
// No active transaction to start with.
active_transaction_flag = false;
// Call the restore cache state function.
// If ENABLE_RESTORE is set, then this will fill the cache table.
restoreCacheTable();
// Load prefetch data.
if (ENABLE_PERFECT_PREFETCHING)
{
// MOVE THIS TO A FUNCTION.
// Open prefetch file.
ifstream prefetch_file;
prefetch_file.open(PREFETCH_FILE, ifstream::in);
if (!prefetch_file.is_open())
{
cerr << "ERROR: Failed to load prefetch file: " << PREFETCH_FILE << "\n";
abort();
}
// Declare variables for parsing string and uint64_t.
string parse_string;
uint64_t num_sets, cur_set, set_size, set_counter, tmp_num;
// Parse prefetch data.
prefetch_file >> parse_string;
if (parse_string != "NUM_SETS")
{
cerr << "ERROR: Invalid prefetch file format. NUM_SETS does not appear at beginning.\n";
abort();
}
prefetch_file >> num_sets;
for (cur_set = 0; cur_set < num_sets; cur_set++)
{
prefetch_file >> parse_string;
if (parse_string != "SET")
{
cerr << "ERROR: Invalid prefetch file format. SET does not appear at beginning of set " << cur_set << ".\n";
abort();
}
prefetch_file >> tmp_num;
if (tmp_num != cur_set)
{
cerr << "ERROR: Invalid prefetch file format. Sets not given in order. (" << cur_set << ")\n";
abort();
}
// Read the size fo this set.
prefetch_file >> set_size;
// Create a new entry in the maps.
prefetch_access_number[cur_set] = list<uint64_t>();
prefetch_flush_addr[cur_set] = list<uint64_t>();
prefetch_new_addr[cur_set] = list<uint64_t>();
prefetch_counter[cur_set] = 0;
// Process each prefetch.
for (set_counter = 0; set_counter < set_size; set_counter++)
{
// Read and store the access counter.
prefetch_file >> tmp_num;
prefetch_access_number[cur_set].push_back(tmp_num);
// Read and store the flush address.
prefetch_file >> tmp_num;
prefetch_flush_addr[cur_set].push_back(tmp_num);
// Read and store the new address.
prefetch_file >> tmp_num;
prefetch_new_addr[cur_set].push_back(tmp_num);
}
}
prefetch_file.close();
}
// Initialize size/max counters.
// Note: Some of this is just debug info, but I'm keeping it around because it is useful.
pending_count = 0; // This is used by TraceBasedSim for MAX_PENDING.
max_dram_pending = 0;
pending_pages_max = 0;
trans_queue_max = 0;
trans_queue_size = 0; // This is not debugging info.
tlb_misses = 0;
tlb_hits = 0;
total_prefetches = 0;
unused_prefetches = 0;
unused_prefetch_victims = 0;
prefetch_hit_nops = 0;
prefetch_cheat_count = 0;
unique_one_misses = 0;
unique_stream_buffers = 0;
stream_buffer_hits = 0;
// Create file descriptors for debugging output (if needed).
if (DEBUG_VICTIM)
{
debug_victim.open("debug_victim.log", ios_base::out | ios_base::trunc);
if (!debug_victim.is_open())
{
cerr << "ERROR: HybridSim debug_victim file failed to open.\n";
abort();
}
}
if (DEBUG_NVDIMM_TRACE)
{
debug_nvdimm_trace.open("nvdimm_trace.log", ios_base::out | ios_base::trunc);
if (!debug_nvdimm_trace.is_open())
{
cerr << "ERROR: HybridSim debug_nvdimm_trace file failed to open.\n";
abort();
}
}
if (DEBUG_FULL_TRACE)
{
debug_full_trace.open("full_trace.log", ios_base::out | ios_base::trunc);
if (!debug_full_trace.is_open())
{
cerr << "ERROR: HybridSim debug_full_trace file failed to open.\n";
abort();
}
}
}
HybridSystem::~HybridSystem()
{
if (DEBUG_VICTIM)
debug_victim.close();
if (DEBUG_NVDIMM_TRACE)
debug_nvdimm_trace.close();
if (DEBUG_FULL_TRACE)
debug_full_trace.close();
}
// static allocator for the library interface
HybridSystem *getMemorySystemInstance(uint id, string ini)
{
return new HybridSystem(id, ini);
}
void HybridSystem::update()
{
// Process the transaction queue.
// This will fill the dram_queue and flash_queue.
if (dram_pending.size() > max_dram_pending)
max_dram_pending = dram_pending.size();
if (pending_pages.size() > pending_pages_max)
pending_pages_max = pending_pages.size();
if (trans_queue_size > trans_queue_max)
trans_queue_max = trans_queue_size;
// Log the queue length.
bool idle = (trans_queue.empty()) && (pending_pages.empty());
bool flash_idle = (flash_queue.empty()) && (flash_pending.empty());
bool dram_idle = (dram_queue.empty()) && (dram_pending.empty());
if (ENABLE_LOGGER)
log.access_update(trans_queue_size, idle, flash_idle, dram_idle);
// See if there are any transactions ready to be processed.
if ((active_transaction_flag) && (delay_counter == 0))
{
ProcessTransaction(active_transaction);
active_transaction_flag = false;
}
// Used to see if any work is done on this cycle.
bool sent_transaction = false;
list<Transaction>::iterator it = trans_queue.begin();
while((it != trans_queue.end()) && (pending_pages.size() < NUM_SETS) && (check_queue) && (delay_counter == 0))
{
// Compute the page address.
uint64_t flash_addr = ALIGN((*it).address);
uint64_t page_addr = PAGE_ADDRESS(flash_addr);
// Check to see if this page is open under contention rules.
if (contention_is_unlocked(flash_addr))
{
// Lock the page.
contention_lock(flash_addr);
// Log the page access.
if (ENABLE_LOGGER)
log.access_page(page_addr);
// Set this transaction as active and start the delay counter, which
// simulates the SRAM cache tag lookup time.
active_transaction = *it;
active_transaction_flag = true;
delay_counter = CONTROLLER_DELAY;
sent_transaction = true;
// Check that this page is in the TLB.
// Do not do this for SYNC_ALL_COUNTER transactions because the page address refers
// to the cache line, not the flash page address, so the TLB isn't needed.
if ((*it).transactionType != SYNC_ALL_COUNTER)
check_tlb(page_addr);
// Delete this item and skip to the next.
it = trans_queue.erase(it);
trans_queue_size--;
if ((active_transaction.transactionType == PREFETCH) &&
(prefetch_cheat_map.count(PAGE_ADDRESS(ALIGN(active_transaction.address)))))
{
// If this is a cheat prefetch, then just do it now and continue processing.
// Cheat prefetches are effectively "free" in terms of clock cycles.
//cout << "Issuing cheat prefetch to address " << PAGE_ADDRESS(ALIGN(active_transaction.address))
// << " on cycle " << currentClockCycle << "\n";
ProcessTransaction(active_transaction);
active_transaction_flag = false;
delay_counter = 0;
sent_transaction = false;
continue;
}
else
{
// We've found the transaction we want.
break;
}
}
else
{
// Log the set conflict.
if (ENABLE_LOGGER)
log.access_set_conflict(SET_INDEX(page_addr));
// Skip to the next and do nothing else.
++it;
}
}
// If there is nothing to do, wait until a new transaction arrives or a pending set is released.
// Only set check_queue to false if the delay counter is 0. Otherwise, a transaction that arrives
// while delay_counter is running might get missed and stuck in the queue.
if ((sent_transaction == false) && (delay_counter == 0))
{
this->check_queue = false;
}
// Process DRAM transaction queue until it is empty or addTransaction returns false.
// Note: This used to be a while, but was changed ot an if to only allow one
// transaction to be sent to the DRAM per cycle.
bool not_full = true;
if (not_full && !dram_queue.empty())
{
Transaction tmp = dram_queue.front();
bool isWrite;
if (tmp.transactionType == DATA_WRITE)
isWrite = true;
else
isWrite = false;
not_full = dram->addTransaction(isWrite, tmp.address);
if (not_full)
{
dram_queue.pop_front();
dram_pending_set.insert(tmp.address);
}
}
// Process Flash transaction queue until it is empty or addTransaction returns false.
// Note: This used to be a while, but was changed ot an if to only allow one
// transaction to be sent to the flash per cycle.
not_full = true;
if (not_full && !flash_queue.empty())
{
bool isWrite;
Transaction tmp = flash_queue.front();
if (tmp.transactionType == DATA_WRITE)
isWrite = true;
else
isWrite = false;
not_full = flash->addTransaction(isWrite, tmp.address);
if (not_full)
{
flash_queue.pop_front();
if (DEBUG_NVDIMM_TRACE)
{
debug_nvdimm_trace << currentClockCycle << " " << (isWrite ? 1 : 0) << " " << tmp.address << "\n";
debug_nvdimm_trace.flush();
}
}
}
// Decrement the delay counter.
if (delay_counter > 0)
{
delay_counter--;
}
// Update the logger.
if (ENABLE_LOGGER)
log.update();
// Update the memories.
dram->update();
flash->update();
// Increment the cycle count.
step();
}
bool HybridSystem::addTransaction(bool isWrite, uint64_t addr)
{
if (DEBUG_CACHE)
cerr << "\n" << currentClockCycle << ": " << "Adding transaction for address=" << addr << " isWrite=" << isWrite << endl;
TransactionType type;
if (isWrite)
{
type = DATA_WRITE;
}
else
{
type = DATA_READ;
}
Transaction t = Transaction(type, addr, NULL);
return addTransaction(t);
}
bool HybridSystem::addTransaction(Transaction &trans)
{
if (REMAP_MMIO)
{
if ((trans.address >= THREEPOINTFIVEGB) && (trans.address < FOURGB))
{
// Do not add this transaction to the queue because it is in the MMIO range.
// Just issue the callback and return.
if (trans.transactionType == DATA_READ)
{
if (ReadDone != NULL)
(*ReadDone)(systemID, trans.address, currentClockCycle);
}
else if (trans.transactionType == DATA_WRITE)
{
if (WriteDone != NULL)
(*WriteDone)(systemID, trans.address, currentClockCycle);
}
else
assert(0);
log.mmio_dropped();
return true;
}
else if (trans.address >= FOURGB)
{
// Subtract 0.5 GB from the address to adjust for MMIO.
trans.address -= HALFGB;
log.mmio_remapped();
}
}
pending_count += 1;
trans_queue.push_back(trans);
trans_queue_size++;
if ((trans.transactionType == PREFETCH) || (trans.transactionType == FLUSH))
{
ERROR("PREFETCH/FLUSH not allowed in addTransaction()");
abort();
}
// Start the logging for this access.
if (ENABLE_LOGGER)
log.access_start(trans.address);
if (DEBUG_FULL_TRACE)
{
debug_full_trace << currentClockCycle << " " << ((trans.transactionType == DATA_WRITE) ? 1 : 0) << " " << trans.address << "\n";
debug_full_trace.flush();
}
// Restart queue checking.
this->check_queue = true;
return true; // TODO: Figure out when this could be false.
}
void HybridSystem::addPrefetch(uint64_t prefetch_addr)
{
// Create prefetch transaction.
Transaction prefetch_transaction = Transaction(PREFETCH, prefetch_addr, NULL);
// Push the operation onto the front of the transaction queue (so it executes immediately).
trans_queue.push_front(prefetch_transaction);
trans_queue_size += 1;
pending_count += 1;
// Restart queue checking.
this->check_queue = true;
}
void HybridSystem::addFlush(uint64_t flush_addr)
{
// Create flush transaction.
Transaction flush_transaction = Transaction(FLUSH, flush_addr, NULL);
// Push the operation onto the front of the transaction queue (so it executes immediately).
trans_queue.push_front(flush_transaction);
trans_queue_size += 1;
pending_count += 1;
// Restart queue checking.
this->check_queue = true;
}
bool HybridSystem::WillAcceptTransaction()
{
// Always true for now since MARSS expects this.
// Might change later.
return true;
}
void HybridSystem::ProcessTransaction(Transaction &trans)
{
// trans.address is the original address that we must use to callback.
// But for our processing, we must use an aligned address (which is aligned to a page in the NV address space).
uint64_t addr = ALIGN(trans.address);
if (trans.transactionType == SYNC_ALL_COUNTER)
{
// SYNC_ALL_COUNTER transactions are handled elsewhere.
syncAllCounter(addr, trans);
return;
}
if (DEBUG_CACHE)
cerr << "\n" << currentClockCycle << ": " << "Starting transaction for address " << addr << endl;
if (addr >= (TOTAL_PAGES * PAGE_SIZE))
{
// Note: This should be technically impossible due to the modulo in ALIGN. But this is just a sanity check.
cerr << "ERROR: Address out of bounds - orig:" << trans.address << " aligned:" << addr << "\n";
abort();
}
// Compute the set number and tag
uint64_t set_index = SET_INDEX(addr);
uint64_t tag = TAG(addr);
list<uint64_t> set_address_list;
for (uint64_t i=0; i<SET_SIZE; i++)
{
uint64_t next_address = (i * NUM_SETS + set_index) * PAGE_SIZE;
set_address_list.push_back(next_address);
}
bool hit = false;
uint64_t cache_address = *(set_address_list.begin());
uint64_t cur_address;
cache_line cur_line;
for (list<uint64_t>::iterator it = set_address_list.begin(); it != set_address_list.end(); ++it)
{
cur_address = *it;
if (cache.count(cur_address) == 0)
{
// If i is not allocated yet, allocate it.
cache[cur_address] = cache_line();
}
cur_line = cache[cur_address];
if (cur_line.valid && (cur_line.tag == tag))
{
hit = true;
cache_address = cur_address;
if (DEBUG_CACHE)
{
cerr << currentClockCycle << ": " << "HIT: " << cur_address << " " << " " << cur_line.str() <<
" (set: " << set_index << ")" << endl;
}
break;
}
}
// Place access_process here and combine it with access_cache.
// Tell the logger when the access is processed (used for timing the time in queue).
// Only do this for DATA_READ and DATA_WRITE.
if ((ENABLE_LOGGER) && ((trans.transactionType == DATA_READ) || (trans.transactionType == DATA_WRITE)))
log.access_process(trans.address, trans.transactionType == DATA_READ, hit);
// Handle prefetching operations.
if (ENABLE_PERFECT_PREFETCHING && ((trans.transactionType == DATA_READ) || (trans.transactionType == DATA_WRITE)))
{
// Increment prefetch counter for this set.
prefetch_counter[set_index]++;
// If there are any prefetches left in this set prefetch list.
if (!prefetch_access_number[set_index].empty())
{
// If the counter exceeds the front of the access list,
// then issue a prefetch and pop the front of the prefetch lists for this set.
// This must be a > because the prefetch must only happen AFTER the access.
if (prefetch_counter[set_index] > prefetch_access_number[set_index].front())
{
// Add prefetch, then add flush (this makes flush run first).
addPrefetch(prefetch_new_addr[set_index].front());
addFlush(prefetch_flush_addr[set_index].front());
// Go to the next prefetch in this set.
prefetch_access_number[set_index].pop_front();
prefetch_flush_addr[set_index].pop_front();
prefetch_new_addr[set_index].pop_front();
}
}
}
if (hit)
{
// Lock the line that was hit (so it cannot be selected as a victim while being processed).
contention_cache_line_lock(cache_address);
if ((ENABLE_STREAM_BUFFER) &&
((trans.transactionType == DATA_READ) || (trans.transactionType == DATA_WRITE)))
{
stream_buffer_hit_handler(PAGE_ADDRESS(addr));
}
// Issue operation to the DRAM.
if (trans.transactionType == DATA_READ)
CacheRead(trans.address, addr, cache_address);
else if(trans.transactionType == DATA_WRITE)
CacheWrite(trans.address, addr, cache_address);
else if(trans.transactionType == FLUSH)
{
Flush(cache_address);
}
else if(trans.transactionType == PREFETCH)
{
// Prefetch hits are just NOPs.
uint64_t flash_address = addr;
contention_unlock(flash_address, flash_address, "PREFETCH (hit)", false, 0, true, cache_address);
prefetch_hit_nops++;
return;
}
else if(trans.transactionType == SYNC)
{
sync(addr, cache_address, trans);
//contention_unlock(addr, addr, "SYNC (hit)", false, 0, true, cache_address);
}
else
{
assert(0);
}
}
if (!hit)
{
// Lock the whole page.
contention_page_lock(addr);
// Make sure this isn't a FLUSH before proceeding.
if(trans.transactionType == FLUSH)
{
// We allow FLUSH to miss the cache because of non-determinism from marss.
uint64_t flash_address = addr;
contention_unlock(flash_address, flash_address, "FLUSH (miss)", false, 0, false, 0);
// TODO: Add some logging for this event.
return;
}
assert(trans.transactionType != SYNC);
if ((SEQUENTIAL_PREFETCHING_WINDOW > 0) && (trans.transactionType != PREFETCH))
{
issue_sequential_prefetches(addr);
}
if ((ENABLE_STREAM_BUFFER) && (trans.transactionType != PREFETCH))
{
stream_buffer_miss_handler(PAGE_ADDRESS(addr));
}
// Select a victim offset within the set (LRU)
uint64_t victim = *(set_address_list.begin());
uint64_t min_ts = (uint64_t) 18446744073709551615U; // Max uint64_t
bool min_init = false;
if (DEBUG_VICTIM)
{
debug_victim << "--------------------------------------------------------------------\n";
debug_victim << currentClockCycle << ": new miss. time to pick the unlucky line.\n";
debug_victim << "set: " << set_index << "\n";
debug_victim << "new flash addr: 0x" << hex << addr << dec << "\n";
debug_victim << "new tag: " << TAG(addr)<< "\n";
debug_victim << "scanning set address list...\n\n";
}
uint64_t victim_counter = 0;
uint64_t victim_set_offset = 0;
for (list<uint64_t>::iterator it=set_address_list.begin(); it != set_address_list.end(); it++)
{
cur_address = *it;
cur_line = cache[cur_address];
if (DEBUG_VICTIM)
{
debug_victim << "cur_address= 0x" << hex << cur_address << dec << "\n";
debug_victim << "cur_tag= " << cur_line.tag << "\n";
debug_victim << "dirty= " << cur_line.dirty << "\n";
debug_victim << "valid= " << cur_line.valid << "\n";
debug_victim << "ts= " << cur_line.ts << "\n";
debug_victim << "min_ts= " << min_ts << "\n\n";
}
// If the current line is the least recent we've seen so far, then select it.
// But do not select it if the line is locked.
if (((cur_line.ts < min_ts) || (!min_init)) && (!cur_line.locked))
{
victim = cur_address;
min_ts = cur_line.ts;
min_init = true;
if (DEBUG_VICTIM)
{
victim_set_offset = victim_counter;
debug_victim << "FOUND NEW MINIMUM!\n\n";
}
}
victim_counter++;
}
if (DEBUG_VICTIM)
{
debug_victim << "Victim in set_offset: " << victim_set_offset << "\n\n";
}
cache_address = victim;
cur_line = cache[cache_address];
uint64_t victim_flash_addr = FLASH_ADDRESS(cur_line.tag, set_index);
if ((cur_line.prefetched) && (cur_line.used == false))
{
// An unused prefetch is being removed from the cache, so transfer the count
// to the unused_prefetch_victims counter.
unused_prefetches--;
unused_prefetch_victims++;
}
// If this is a cheating PREFETCH operation, then we don't actually want to do any work.
// Just set the cache tags as if the operation completed and then bail.
if (trans.transactionType == PREFETCH)
{
uint64_t page_address = PAGE_ADDRESS(addr);
if (prefetch_cheat_map.count(page_address) != 0)
{
// Remove the page_address from the prefetch_cheat_map.
prefetch_cheat_map[page_address] -= 1;
if (prefetch_cheat_map[page_address] == 0)
{
size_t retval = prefetch_cheat_map.erase(page_address);
assert(retval == 1);
}
// Set the cache lines as if the transaction was already done.
cur_line.tag = TAG(page_address);
cur_line.dirty = false;
cur_line.valid = true;
cur_line.ts = currentClockCycle;
cur_line.used = false;
// Since this is a prefetch, also keep track of that.
cur_line.prefetched = true;
total_prefetches++;
unused_prefetches++;
prefetch_cheat_count++;
cache[cache_address] = cur_line;
// Unlock the address.
contention_unlock(addr, addr, "PREFETCH (cheat)", false, 0, false, 0);
// Note: No callback is necessary since this is a prefetch operation.
return;
}
// TODO: Figure out if the logger or anything else is going to break.
}
// Log the victim, set, etc.
// THIS MUST HAPPEN AFTER THE CUR_LINE IS SET TO THE VICTIM LINE.
if ((ENABLE_LOGGER) && ((trans.transactionType == DATA_READ) || (trans.transactionType == DATA_WRITE)))
log.access_miss(PAGE_ADDRESS(addr), victim_flash_addr, set_index, victim, cur_line.dirty, cur_line.valid);
// Lock the victim page so it will not be selected for eviction again during the processing of this
// transaction's miss and so further transactions to this page cannot happen.
// Only lock it if the cur_line is valid.
if (cur_line.valid)
contention_victim_lock(victim_flash_addr);
// Lock the cache line so no one else tries to use it while this miss is being serviced.
contention_cache_line_lock(cache_address);
if (DEBUG_CACHE)
{
cerr << currentClockCycle << ": " << "MISS: victim is cache_address " << cache_address <<
" (set: " << set_index << ")" << endl;
cerr << cur_line.str() << endl;
cerr << currentClockCycle << ": " << "The victim is dirty? " << cur_line.dirty << endl;
}
Pending p;
p.orig_addr = trans.address;
p.flash_addr = addr;
p.cache_addr = cache_address;
p.victim_tag = cur_line.tag;
p.victim_valid = cur_line.valid;
p.callback_sent = false;
p.type = trans.transactionType;
// Read the line that missed from the NVRAM.
// This is started immediately to minimize the latency of the waiting user of HybridSim.
LineRead(p);
// If the cur_line is dirty, then do a victim writeback process (starting with VictimRead).
if (cur_line.dirty)
{
VictimRead(p);
}
}
}
void HybridSystem::VictimRead(Pending p)
{
if (DEBUG_CACHE)
cerr << currentClockCycle << ": " << "Performing VICTIM_READ for (" << p.flash_addr << ", " << p.cache_addr << ")\n";
// flash_addr is the original Flash address requested from the top level Transaction.
// victim is the base address of the DRAM page to read.
// victim_tag is the cache tag for the victim page (used to compute the victim's flash address).
// Increment the pending set/page counter (this is used to ensure that the pending set/page entry isn't removed until both LineRead
// and VictimRead (if needed) are completely done.
contention_increment(p.flash_addr);
#if SINGLE_WORD
// Schedule a read from DRAM to get the line being evicted.
Transaction t = Transaction(DATA_READ, p.cache_addr, NULL);
dram_queue.push_back(t);
#else
// Schedule reads for the entire page.
dram_pending_wait[p.cache_addr] = unordered_set<uint64_t>();
for(uint64_t i=0; i<PAGE_SIZE/BURST_SIZE; i++)
{
uint64_t addr = p.cache_addr + i*BURST_SIZE;
dram_pending_wait[p.cache_addr].insert(addr);
Transaction t = Transaction(DATA_READ, addr, NULL);
dram_queue.push_back(t);
}
#endif
// Add a record in the DRAM's pending table.
p.op = VICTIM_READ;
assert(dram_pending.count(p.cache_addr) == 0);
dram_pending[p.cache_addr] = p;
}
void HybridSystem::VictimReadFinish(uint64_t addr, Pending p)
{
if (DEBUG_CACHE)
{
cerr << currentClockCycle << ": " << "VICTIM_READ callback for (" << p.flash_addr << ", " << p.cache_addr << ") offset="
<< PAGE_OFFSET(addr);
}
#if SINGLE_WORD
if (DEBUG_CACHE)
cerr << " num_left=0 (SINGLE_WORD)\n";
#else
uint64_t cache_page_addr = p.cache_addr;
if (DEBUG_CACHE)
cerr << " num_left=" << dram_pending_wait[cache_page_addr].size() << "\n";
// Remove the read that just finished from the wait set.
dram_pending_wait[cache_page_addr].erase(addr);
if (!dram_pending_wait[cache_page_addr].empty())
{
// If not done with this line, then re-enter pending map.
dram_pending[cache_page_addr] = p;
dram_pending_set.erase(addr);
return;
}
// The line has completed. Delete the wait set object and move on.
dram_pending_wait.erase(cache_page_addr);
#endif
// Decrement the pending set counter (this is used to ensure that the pending set entry isn't removed until both LineRead
// and VictimRead (if needed) are completely done.
contention_decrement(p.flash_addr);
if (DEBUG_CACHE)
{
cerr << "The victim read to DRAM line " << PAGE_ADDRESS(addr) << " has completed.\n";
cerr << "pending_pages[" << PAGE_ADDRESS(p.flash_addr) << "] = " << pending_pages[PAGE_ADDRESS(p.flash_addr)] << "\n";
}
// contention_unlock will only unlock if the pending_page counter is 0.
// This means that LINE_READ finished first and that the pending set was not removed
// in the CacheReadFinish or CacheWriteFinish functions (or LineReadFinish for PREFETCH).
uint64_t victim_address = FLASH_ADDRESS(p.victim_tag, SET_INDEX(p.cache_addr));
contention_unlock(p.flash_addr, p.orig_addr, "VICTIM_READ", p.victim_valid, victim_address, true, p.cache_addr);
// Schedule a write to the flash to simulate the transfer
VictimWrite(p);
}
void HybridSystem::VictimWrite(Pending p)
{
if (DEBUG_CACHE)
cerr << currentClockCycle << ": " << "Performing VICTIM_WRITE for (" << p.flash_addr << ", " << p.cache_addr << ")\n";
// Compute victim flash address.
// This is where the victim line is stored in the Flash address space.
uint64_t victim_flash_addr = (p.victim_tag * NUM_SETS + SET_INDEX(p.flash_addr)) * PAGE_SIZE;
#if SINGLE_WORD
// Schedule a write to Flash to save the evicted line.
Transaction t = Transaction(DATA_WRITE, victim_flash_addr, NULL);
flash_queue.push_back(t);
#else
// Schedule writes for the entire page.
for(uint64_t i=0; i<PAGE_SIZE/FLASH_BURST_SIZE; i++)
{
Transaction t = Transaction(DATA_WRITE, victim_flash_addr + i*FLASH_BURST_SIZE, NULL);
flash_queue.push_back(t);
}
#endif
// No pending event schedule necessary (might add later for debugging though).
}
void HybridSystem::LineRead(Pending p)
{
if (DEBUG_CACHE)
{
cerr << currentClockCycle << ": " << "Performing LINE_READ for (" << p.flash_addr << ", " << p.cache_addr << ")\n";
cerr << "the page address was " << PAGE_ADDRESS(p.flash_addr) << endl;
}
uint64_t page_addr = PAGE_ADDRESS(p.flash_addr);
// Increment the pending set counter (this is used to ensure that the pending set entry isn't removed until both LineRead
// and VictimRead (if needed) are completely done.
contention_increment(p.flash_addr);
#if SINGLE_WORD
// Schedule a read from Flash to get the new line