Repository navigation
Expand file tree
/
Copy pathModule.cc
More file actions
1369 lines (1160 loc) · 32.8 KB
/
Copy pathModule.cc
File metadata and controls
1369 lines (1160 loc) · 32.8 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
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
/*
* Multi2Sim
* Copyright (C) 2012 Rafael Ubal ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <climits>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "Frame.h"
#include "Module.h"
#include "System.h"
namespace mem
{
Module::Module(const std::string &name,
Type type,
int num_ports,
int block_size,
int data_latency)
:
name(name),
type(type),
block_size(block_size),
data_latency(data_latency),
num_ports(num_ports)
{
// Create 'num_ports' in vector of ports
ports.resize(num_ports);
// Block size
assert(!(block_size & (block_size - 1)) && block_size >= 4);
log_block_size = misc::LogBase2(block_size);
}
bool Module::ServesAddress(unsigned address) const
{
// Address bounds
if (range_type == RangeBounds)
return address >= range.bounds.low &&
address <= range.bounds.high;
// Interleaved addresses
if (range_type == RangeInterleaved)
return (address / range.interleaved.div) %
range.interleaved.mod ==
range.interleaved.eq;
// Invalid
throw misc::Panic("Invalid range type");
}
Module *Module::getLowModuleServingAddress(unsigned address) const
{
// The address must be served by the current module
assert(ServesAddress(address));
// Main memory does not have a low module
if (type == TypeMainMemory)
{
assert(!low_modules.size());
return nullptr;
}
// Check which low module serves address
Module *server_module = nullptr;
for (Module *low_module : low_modules)
{
// Skip if this low module doesn't serve address
if (!low_module->ServesAddress(address))
continue;
// Address served by more than one module
if (server_module)
throw Error(misc::fmt("%s: low modules '%s' "
"and '%s' both serve address 0x%x",
name.c_str(),
server_module->getName().c_str(),
low_module->getName().c_str(),
address));
// Assign serving module
server_module = low_module;
}
// Error if no low module serves address
if (!server_module)
throw Error(misc::fmt("Module %s: no lower module "
"serves address 0x%x",
name.c_str(),
address));
// Return server module
return server_module;
}
Module *Module::getOwner(int set_id, int way_id, int sub_block_id) const
{
// Get directory entry
assert(directory.get());
Directory::Entry *entry = directory->getEntry(set_id,
way_id,
sub_block_id);
// Get owner ID
assert(entry);
int owner = entry->getOwner();
// No owner
if (owner == Directory::NoOwner)
return nullptr;
// Get node
net::Node *node = high_network->getNode(owner);
assert(node);
// Return owner module
return (Module *) node->getUserData();
}
bool Module::canAccess(int address) const
{
// There must be a free port
assert(num_locked_ports <= num_ports);
if (num_locked_ports == num_ports)
return false;
// If no MSHR is given, module can be accessed
if (!mshr_size)
return true;
// Module can be accessed if number of non-coalesced in-flight accesses
// is smaller than the MSHR size.
int num_non_coalesced_accesses = accesses.size() -
num_coalesced_accesses;
return num_non_coalesced_accesses < mshr_size;
}
long long Module::Access(AccessType access_type,
unsigned address,
int *witness,
esim::Event *return_event)
{
// Create a new event frame
auto frame = misc::new_shared<Frame>(
Frame::getNewId(),
this,
address);
frame->witness = witness;
// Select initial event type
esim::Event *event;
switch (type)
{
case TypeCache:
case TypeMainMemory:
switch (access_type)
{
case AccessLoad:
event = System::event_load;
break;
case AccessStore:
event = System::event_store;
break;
case AccessNCStore:
event = System::event_nc_store;
break;
default:
throw misc::Panic("Invalid access type");
}
break;
case TypeLocalMemory:
switch (access_type)
{
case AccessLoad:
event = System::event_local_load;
break;
case AccessStore:
event = System::event_local_store;
break;
default:
throw misc::Panic("Invalid access type");
}
break;
default:
throw misc::Panic("Invalid module type");
}
// Schedule event
esim::Engine *esim_engine = esim::Engine::getInstance();
esim_engine->Call(event, frame, return_event);
// Return frame ID
return frame->getId();
}
void Module::StartAccess(Frame *frame, AccessType access_type)
{
// Record access type
frame->access_type = access_type;
// Insert in access list
frame->accesses_iterator = accesses.insert(accesses.end(),
frame);
// Insert in write access list
if (access_type == AccessStore)
frame->write_accesses_iterator = write_accesses.insert(
write_accesses.end(),
frame);
// Insert in hash table of block addresses
unsigned block_address = frame->getAddress() >> log_block_size;
in_flight_block_addresses.emplace(std::make_pair(block_address, frame));
// Insert in set of access identifiers
in_flight_access_ids.emplace(frame->getId());
}
void Module::FinishAccess(Frame *frame)
{
// Remove from access list
accesses.erase(frame->accesses_iterator);
frame->accesses_iterator = accesses.end();
// Remove from write access list
assert(frame->access_type);
if (frame->access_type == Module::AccessStore)
{
write_accesses.erase(frame->write_accesses_iterator);
frame->write_accesses_iterator = write_accesses.end();
}
// Remove from hash table
unsigned block_address = frame->getAddress() >> log_block_size;
auto range = in_flight_block_addresses.equal_range(block_address);
bool found = false;
for (auto it = range.first; it != range.second; ++it)
{
Frame *other_frame = it->second;
if (frame == other_frame)
{
found = true;
in_flight_block_addresses.erase(it);
break;
}
}
// Must have been removed
if (!found)
throw misc::Panic("Frame not found");
// Remove from set of in-flight access identifiers
in_flight_access_ids.erase(frame->getId());
// If this was a coalesced access, update counter
if (frame->coalesced)
{
assert(num_coalesced_accesses > 0);
num_coalesced_accesses--;
}
// When a frame finishes its access, we need to check each of the frames
// in the access list and make sure that no remaining frames use the
// finished frame as a master frame. If they are, their master frame
// pointer is resest to null. This is to prevent a situation where the
// finishing frame makes a later access and adopts a master frame from
// a frame in the access list which points to itself.
for (auto it = accesses.begin(); it != accesses.end(); it++)
{
Frame *list_frame = *it;
if (list_frame->master_frame == frame)
list_frame->master_frame = nullptr;
}
// Wake up dependent accesses
frame->queue.WakeupAll();
}
Frame *Module::getInFlightAddress(unsigned address,
Frame *older_than_frame)
{
// Look for address
unsigned block_address = address >> log_block_size;
auto range = in_flight_block_addresses.equal_range(block_address);
for (auto it = range.first; it != range.second; ++it)
{
// Get frame
Frame *frame = it->second;
// This frame is not older than 'older_than_frame'
if (older_than_frame && frame->getId() >=
older_than_frame->getId())
continue;
// Block address matches
assert(frame->getAddress() >> log_block_size ==
address >> log_block_size);
return frame;
}
// Not found
return nullptr;
}
Frame *Module::getInFlightWrite(Frame *older_than_frame)
{
// No 'older_than_frame' given, return youngest write, or nullptr if
// there is no in-flight write.
if (!older_than_frame)
return write_accesses.size() ?
write_accesses.back() :
nullptr;
// Search
auto it = older_than_frame->accesses_iterator;
while (it != accesses.begin())
{
// Rewind
--it;
// Check if it is a write
Frame *frame = *it;
if (frame->access_type == AccessStore)
return frame;
}
// Not found
return nullptr;
}
bool Module::isInFlightAddress(unsigned address)
{
unsigned block_address = address >> log_block_size;
auto it = in_flight_block_addresses.find(block_address);
return it != in_flight_block_addresses.end();
}
bool Module::isInFlightAccess(long long id)
{
auto it = in_flight_access_ids.find(id);
return it != in_flight_access_ids.end();
}
void Module::Dump(std::ostream &os) const
{
// First we have to calculate the largest string that will be presented
// in the table, and create the size of elements of the table according
// to this string. Initial element size is the string "Module <name>"
unsigned element_size = name.length() + 7;
// Get the number of sets and ways
unsigned sets = cache->getNumSets();
unsigned ways = cache->getNumWays();
int sub_blocks = directory->getNumSubBlocks();
// Also we create a list with integers for the maximum number of
// sharers per set
std::vector<int> max_sharers;
max_sharers.resize(cache->getNumSets());
for (unsigned set = 0; set < sets; set++)
{
for (unsigned way = 0; way < ways; way++)
{
// Get the block
Cache::Block *block = cache->getBlock(set,way);
// If block state is invalid continue
if (!block->getState())
continue;
// Start the element size with the tag of the block
if (element_size < std::to_string(
block->getTag()).length())
element_size = std::to_string(
block->getTag()).length();
// Sharers field size
unsigned entry_text_size = 0;
// find modules that are sharers/owner of the
// directory entries.
for (int sub_block_id = 0;
sub_block_id < sub_blocks;
sub_block_id++)
{
// Number of sharers per sub_block
int sharers = 0;
// Add the name of the sharer + two spaces
for (auto &high_module : high_modules)
{
int index = getSharerIndex(high_module);
if (directory->isSharer((int) set, (int) way,
sub_block_id, index))
{
if (entry_text_size < high_module->
getName().length())
entry_text_size = high_module->
getName().
length();
sharers++;
}
}
// Increase the maximum number of sharers
// per set if it is required
if (max_sharers[set] < sharers)
max_sharers[set] = sharers;
}
// Check if this string is bigger than the previous
// maximum value
if (element_size < entry_text_size)
element_size = entry_text_size;
}
}
// Based on the element size construct the dump table
// No matter what, we would add extra space to the element size
element_size += 2;
// We start drawing the table based on the element size
// ******************************************
// * Module <name> *
// ******************************************
os << "(The Dump is best viewed when text "
"wrapping is disabled)\n";
os << std::string((ways * sub_blocks + 2)* element_size, '=') <<
"\n";
os << "| Module " << name.c_str() <<
std::string((ways * sub_blocks + 2) * element_size - 10 -
name.length(), ' ') << "|" << "\n";
os << std::string((ways * sub_blocks + 2)* element_size, '=') <<
"\n";
// Here we start printing
int pad_size = 0;
// The first line is the number of ways. The first two elements
// in this row are empty.
os << "|" << std::string(element_size * 2 - 2 , ' ') << "|";
for (unsigned i = 0; i < ways; i++)
{
// Print the way index
std::string way_id = "Way " + std::to_string(i);
// Empty space excluding | and the way index
pad_size = (sub_blocks * element_size) - 2 - way_id.length();
os << "|" <<
std::string(pad_size/2, ' ') <<
way_id.c_str() <<
std::string((sub_blocks * element_size) - 2
- way_id.length() - pad_size/2, ' ') <<
"|";
}
// Separating the first row
os <<"\n";
os << std::string((sub_blocks * ways + 2)* element_size, '=') <<
"\n";
for (unsigned set = 0; set < sets; set++)
{
// The row information
std::string set_id = "Set " + std::to_string(set);
// Print the set index
os << "| " <<
std::left <<
std::setw(element_size - 3) <<
std::setfill(' ') <<
set_id.c_str() <<
"|";
// The first row is the state (which is 5 letters)
pad_size = element_size - 2 - 5;
os << "|" <<
std::string(pad_size/2, ' ') <<
"State" <<
std::string(element_size - 7 -
pad_size/2, ' ') <<
"|";
// Printing the state of the block
for (unsigned way = 0; way < ways; way++)
{
// Get the block
Cache::Block *block = cache->getBlock(set, way);
// Print the state
pad_size = sub_blocks * element_size - 3;
os << "|" <<
std::string(pad_size/2, ' ') <<
Cache::BlockStateMap[block->getState()] <<
std::string(sub_blocks * element_size - 3 -
pad_size/2, ' ') <<
"|";
}
// Internal separator
os << "\n" << "|" << std::string(element_size - 1, ' ')
<< std::string(element_size *
(ways * sub_blocks + 1),'-') << "\n";
// The next row is the tag but the first column is empty
os << "|" << std::string(element_size - 2 , ' ') << "|";
// The second column prints Tag(which is 3 words)
pad_size = element_size - 2 - 3;
os << "|" <<
std::string(pad_size/2, ' ') <<
"Tag" <<
std::string(element_size - 5 -
pad_size/2, ' ') <<
"|";
// Printing the tag of the block
for (unsigned way = 0; way < ways; way++)
{
// Get the block
Cache::Block *block = cache->getBlock(set, way);
// If the block is invalid print empty
if (!block->getState())
{
os << "|" << std::string(sub_blocks * element_size - 2, ' ')
<< "|";
}
else
{
// Conduct the tag in Hex
std::stringstream stream;
stream << std::hex << block->getTag();
std::string tag = "0x" + stream.str();
// Print the tag
pad_size = sub_blocks * element_size -
2 - tag.length();
os << "|" <<
std::string(pad_size/2, ' ') <<
tag.c_str() <<
std::string(sub_blocks *
element_size - 2 -
tag.length() - pad_size/2,
' ') <<
"|";
}
}
// Internal separator
os << "\n" << "|" << std::string(element_size - 1, ' ')
<< std::string(element_size *
(sub_blocks * ways + 1),'-') << "\n";
// Start printing sharers of the sub-blocks one sharer
// per line
std::vector<int> printed_counter;
printed_counter.resize(cache->getNumWays() * directory->
getNumSubBlocks());
// Minimum number of rows for sharer is 1
int sharer_rows = 1;
if (sharer_rows < max_sharers[set])
sharer_rows = max_sharers[set];
for (int sharer_row = 0; sharer_row < sharer_rows;
sharer_row++)
{
// The next row is the sharers but the first column is empty
os << "|" << std::string(element_size - 2 , ' ') << "|";
// The second column prints Sharers(which is 7 words)
if (sharer_row == 0)
{
pad_size = element_size - 2 - 7;
os << "|" <<
std::string(pad_size/2, ' ') <<
"Sharers" <<
std::string(element_size - 9 -
pad_size/2, ' ') <<
"|";
}
else
{
os << "|" << std::string(element_size - 2, ' ')
<< "|";
}
// For each entry
for (unsigned way = 0; way < ways; way++)
{
for (int sub_block = 0; sub_block < sub_blocks;
sub_block++)
{
// If the sub_block doesn't have a sharer
// print empty and continue
if (!getNumSharers(set, way, sub_block))
{
os << "|" << std::string(
element_size - 2, ' ') << "|";
continue;
}
// Counting the modules that are
// sharers
int counting = 0;
for (auto &high_module : high_modules)
{
// Get the number of printed sharer
int printed = printed_counter[
way * directory->
getNumSubBlocks() +
sub_block];
// Find a sharer module
int index = getSharerIndex(high_module);
if (directory->isSharer((int) set, (int) way,
sub_block,
index))
{
// If the sharer module is
// already printed continue
// otherwise print and
// exit the loop for
// this sub-block
if (printed > counting)
{
counting++;
continue;
}
else
{
// Increase
// printed
// counter
printed_counter[
way * directory->
getNumSubBlocks() +
sub_block]++;
// Print
int length =
high_module->
getName().
length();
pad_size = element_size -
2 -
length;
os << "|" <<
std::string(pad_size/2, ' ') <<
high_module->getName() <<
std::string(element_size - 2 -
length - pad_size/2, ' ') <<
"|";
break;
}
}
}
}
}
// Internal separator
os << "\n";
}
// Internal separator
os << "|" << std::string(element_size - 1, ' ')
<< std::string(element_size *
(sub_blocks * ways + 1),'-') << "\n";
// The next row is the owner but the first column is empty
os << "|" << std::string(element_size - 2 , ' ') << "|";
// The second column prints Owner(which is 5 letters)
pad_size = element_size - 2 - 5;
os << "|" <<
std::string(pad_size/2, ' ') <<
"Owner" <<
std::string(element_size - 7 -
pad_size/2, ' ') <<
"|";
// For each entry
for (unsigned way = 0; way < ways; way++)
{
for (int sub_block = 0; sub_block < sub_blocks;
sub_block++)
{
// If the sub_block doesn't have a owner
// print empty
Module *owner = getOwner(set, way, sub_block);
if (!owner)
{
os << "|" << std::string(
element_size - 2, ' ') << "|";
}
else
{
// Print
int length = owner->getName().length();
pad_size = element_size - 2 - length;
os << "|" <<
std::string(pad_size/2,
' ') <<
owner->getName() <<
std::string(element_size -
2 - length -
pad_size/2, ' ') <<
"|";
}
}
}
// Set separator
os << "\n" << "|" << std::string((2 + ways * sub_blocks) *
element_size - 2, '=') << "|"
<< "\n";
}
}
void Module::DumpReport(std::ostream &os) const
{
// Dumping module's name
os << misc::fmt("[ %s ]\n\n", name.c_str());
// Dump the cache related information
if (type == TypeCache)
{
os << misc::fmt("Sets = %d\n", cache->getNumSets());
os << misc::fmt("Ways = %d\n", cache->getNumWays());
os << "ReplacementPolicy = " <<
cache->ReplacementPolicyMap.MapValue(
cache->getReplacementPolicy()) << "\n";
os << "WritePolicy = " << cache->WritePolicyMap.MapValue(
cache->getWritePolicy()) << "\n";
}
// Dump the module information
os << misc::fmt("BlockSize = %d\n", block_size);
os << misc::fmt("DataLatency = %d\n", data_latency);
os << misc::fmt("Ports = %d\n", num_ports);
os << "\n";
// Statistics - Accesses
os << misc::fmt("Accesses = %lld\n", num_accesses);
os << misc::fmt("CoalescedAccesses = %lld\n", num_coalesced_reads +
num_coalesced_writes + num_coalesced_nc_writes);
os << misc::fmt("RetriedAccesses = %lld\n", num_retry_accesses);
os << misc::fmt("Evictions = %lld\n", num_evictions);
// Statistics - Hits and misses
long long int num_hits = num_read_hits + num_write_hits
+ num_nc_write_hits;
os << misc::fmt("Hits = %lld\n", num_hits);
os << misc::fmt("Misses = %lld\n", num_accesses - num_hits);
os << misc::fmt("HitRatio = %.4g\n", num_accesses ?
(double) num_hits / num_accesses : 0.0);
os << "\n";
// Statistics breakdown - Reads
os << misc::fmt("Reads = %lld\n", num_reads);
os << misc::fmt("CoalescedReads = %lld\n", num_coalesced_reads);
os << misc::fmt("ReadHits = %lld\n", num_read_hits);
os << misc::fmt("ReadMisses = %lld\n", num_reads - num_read_hits);
os << misc::fmt("ReadRetries = %lld\n", num_retry_reads);
os << misc::fmt("ReadRetryHits = %lld\n", num_retry_read_hits);
os << misc::fmt("ReadRetryMisses = %lld\n", num_retry_read_misses);
os << misc::fmt("BlockingReads = %lld\n", num_blocking_reads);
os << misc::fmt("NonBlockingReads = %lld\n", num_non_blocking_reads);
os << "\n";
// Statistics breakdown - Writes
os << misc::fmt("Writes = %lld\n", num_writes);
os << misc::fmt("CoalescedWrites = %lld\n", num_coalesced_writes);
os << misc::fmt("WriteHits = %lld\n", num_write_hits);
os << misc::fmt("WriteMisses = %lld\n", num_writes - num_write_hits);
os << misc::fmt("WriteRetries = %lld\n", num_retry_writes);
os << misc::fmt("WriteRetryHits = %lld\n", num_retry_write_hits);
os << misc::fmt("WriteRetryMisses = %lld\n", num_retry_write_misses);
os << misc::fmt("BlockingWrites = %lld\n", num_blocking_writes);
os << misc::fmt("NonBlockingWrites = %lld\n", num_non_blocking_writes);
os << "\n";
// Statistics breakdown - Non-coherent Writes
os << misc::fmt("NCWrites = %lld\n", num_nc_writes);
os << misc::fmt("CoalescedNCWrites = %lld\n", num_coalesced_nc_writes);
os << misc::fmt("NCWriteHits = %lld\n", num_nc_write_hits);
os << misc::fmt("NCWriteMisses = %lld\n",
num_nc_writes - num_nc_write_hits);
os << misc::fmt("NCWriteRetries = %lld\n", num_retry_nc_writes);
os << misc::fmt("NCWriteRetryHits = %lld\n", num_retry_nc_write_hits);
os << misc::fmt("NCWriteRetryMisses = %lld\n",
num_retry_nc_write_misses);
os << misc::fmt("BlockingNCWrites = %lld\n", num_blocking_nc_writes);
os << misc::fmt("NonBlockingNCWrites = %lld\n",
num_non_blocking_nc_writes);
os << "\n";
// Statistics - Conflicts
os << misc::fmt("DirectoryEntryConflicts = %lld\n",
num_directory_entry_conflicts);
os << misc::fmt("RetryDirectoryEntryConflicts = %lld\n",
num_retry_directory_entry_conflicts);
if (type == TypeCache)
os << misc::fmt("ConflictInvalidation = %lld\n",
num_conflict_invalidations);
// Separating line between modules
os << "\n\n";
}
void Module::DumpInFlightAddresses(std::ostream &os)
{
esim::Engine *engine = esim::Engine::getInstance();
os << misc::fmt("[%s] In-flight blocks in cycle %lld:\n",
name.c_str(), engine->getCycle());
for (auto &pair : in_flight_block_addresses)
{
unsigned block_address = pair.first;
Frame *frame = pair.second;
os << misc::fmt("\tkey (block_address) = 0x%x: "
"id = %lld, "
"address = 0x%x, "
"block_address = 0x%x\n",
block_address,
frame->getId(),
frame->getAddress(),
frame->getAddress() >> log_block_size);
frame->CheckMagic();
if (block_address != frame->getAddress() >> log_block_size)
throw misc::Panic("Invalid block address");
}
}
Frame *Module::canCoalesce(AccessType access_type,
unsigned address,
Frame *older_than_frame)
{
// Nothing if there is no in-flight access
if (!accesses.size())
return nullptr;
// For efficiency, first check in the hash table of accesses
// whether there is an access in flight to the same block.
assert(access_type);
if (!getInFlightAddress(address, older_than_frame))
return nullptr;
// Nothing if 'older_than_frame' is in the head of the in-flight
// access list (i.e., there is nothing older).
if (older_than_frame->accesses_iterator == accesses.begin())
return nullptr;
// Get iterator to youngest access older than 'older_than_frame', or an
// iterator to the overall youngest access if 'older_than_frame' is
// null.
auto tail = older_than_frame ?
older_than_frame->accesses_iterator :
accesses.end();
assert(tail != accesses.begin());
--tail;
// Coalesce depending on access type
switch (access_type)
{
case AccessLoad:
{
auto it = tail;
while (true)
{
// Only coalesce with groups of reads at the tail
Frame *frame = *it;
if (frame->access_type != AccessLoad)
return nullptr;
// Same block address, coalesce
if (frame->getAddress() >> log_block_size ==
address >> log_block_size)
{
assert(!frame->master_frame ||
!frame->master_frame->master_frame);
return frame->master_frame ?
frame->master_frame :
frame;
}
// Done when head reached
if (it == accesses.begin())
break;
// Previous
--it;
}
break;
}
case AccessStore:
{
// Only coalesce with last access if it is a write
Frame *frame = *tail;
if (frame->access_type != AccessStore)
return nullptr;
// Only if it is an access to the same block
if (frame->getAddress() >> log_block_size !=
address >> log_block_size)
return nullptr;
// Only if previous write has not started yet
if (frame->port_locked)
return nullptr;
// Coalesce
assert(!frame->master_frame ||
!frame->master_frame->master_frame);
return frame->master_frame ?
frame->master_frame :
frame;
}
case AccessNCStore:
{
// Only coalesce with last access if it is a non-coherent write
Frame *frame = *tail;
if (frame->access_type != AccessNCStore)
return nullptr;
// Only if it is an access to the same block
if (frame->getAddress() >> log_block_size !=
address >> log_block_size)
return nullptr;
// Only if previous write has not started yet
if (frame->port_locked)
return nullptr;
// Coalesce
assert(!frame->master_frame ||
!frame->master_frame->master_frame);
return frame->master_frame ?
frame->master_frame :
frame;
}
default:
throw misc::Panic("Invalid access type");
}