forked from dramninjasUMD/DRAMSim2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryController.cpp
978 lines (865 loc) · 30.5 KB
/
MemoryController.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
/*********************************************************************************
* Copyright (c) 2010-2011, Elliott Cooper-Balis
* Paul Rosenfeld
* Bruce Jacob
* University of Maryland
* dramninjas [at] gmail [dot] com
* 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.
*********************************************************************************/
//MemoryController.cpp
//
//Class file for memory controller object
//
#include "MemoryController.h"
#include "MemorySystem.h"
#include "AddressMapping.h"
#define SEQUENTIAL(rank,bank) (rank*NUM_BANKS)+bank
/* Power computations are localized to MemoryController.cpp */
extern unsigned IDD0;
extern unsigned IDD1;
extern unsigned IDD2P;
extern unsigned IDD2Q;
extern unsigned IDD2N;
extern unsigned IDD3Pf;
extern unsigned IDD3Ps;
extern unsigned IDD3N;
extern unsigned IDD4W;
extern unsigned IDD4R;
extern unsigned IDD5;
extern unsigned IDD6;
extern unsigned IDD6L;
extern unsigned IDD7;
extern float Vdd;
using namespace DRAMSim;
MemoryController::MemoryController(MemorySystem *parent, CSVWriter &csvOut_, ostream &dramsim_log_) :
dramsim_log(dramsim_log_),
bankStates(NUM_RANKS, vector<BankState>(NUM_BANKS, dramsim_log)),
commandQueue(bankStates, dramsim_log_),
poppedBusPacket(NULL),
csvOut(csvOut_),
totalTransactions(0),
refreshRank(0)
{
//get handle on parent
parentMemorySystem = parent;
//bus related fields
outgoingCmdPacket = NULL;
outgoingDataPacket = NULL;
dataCyclesLeft = 0;
cmdCyclesLeft = 0;
//set here to avoid compile errors
currentClockCycle = 0;
//reserve memory for vectors
transactionQueue.reserve(TRANS_QUEUE_DEPTH);
powerDown = vector<bool>(NUM_RANKS,false);
grandTotalBankAccesses = vector<uint64_t>(NUM_RANKS*NUM_BANKS,0);
totalReadsPerBank = vector<uint64_t>(NUM_RANKS*NUM_BANKS,0);
totalWritesPerBank = vector<uint64_t>(NUM_RANKS*NUM_BANKS,0);
totalReadsPerRank = vector<uint64_t>(NUM_RANKS,0);
totalWritesPerRank = vector<uint64_t>(NUM_RANKS,0);
writeDataCountdown.reserve(NUM_RANKS);
writeDataToSend.reserve(NUM_RANKS);
refreshCountdown.reserve(NUM_RANKS);
//Power related packets
backgroundEnergy = vector <uint64_t >(NUM_RANKS,0);
burstEnergy = vector <uint64_t> (NUM_RANKS,0);
actpreEnergy = vector <uint64_t> (NUM_RANKS,0);
refreshEnergy = vector <uint64_t> (NUM_RANKS,0);
totalEpochLatency = vector<uint64_t> (NUM_RANKS*NUM_BANKS,0);
//staggers when each rank is due for a refresh
for (size_t i=0;i<NUM_RANKS;i++)
{
refreshCountdown.push_back((int)((REFRESH_PERIOD/tCK)/NUM_RANKS)*(i+1));
}
}
//get a bus packet from either data or cmd bus
void MemoryController::receiveFromBus(BusPacket *bpacket)
{
if (bpacket->busPacketType != DATA)
{
ERROR("== Error - Memory Controller received a non-DATA bus packet from rank");
bpacket->print();
exit(0);
}
if (DEBUG_BUS)
{
PRINTN(" -- MC Receiving From Data Bus : ");
bpacket->print();
}
//add to return read data queue
returnTransaction.push_back(new Transaction(RETURN_DATA, bpacket->physicalAddress, bpacket->data));
totalReadsPerBank[SEQUENTIAL(bpacket->rank,bpacket->bank)]++;
// this delete statement saves a mindboggling amount of memory
delete(bpacket);
}
//sends read data back to the CPU
void MemoryController::returnReadData(const Transaction *trans)
{
if (parentMemorySystem->ReturnReadData!=NULL)
{
(*parentMemorySystem->ReturnReadData)(parentMemorySystem->systemID, trans->address, currentClockCycle);
}
}
//gives the memory controller a handle on the rank objects
void MemoryController::attachRanks(vector<Rank *> *ranks)
{
this->ranks = ranks;
}
//memory controller update
void MemoryController::update()
{
//PRINT(" ------------------------- [" << currentClockCycle << "] -------------------------");
//update bank states
for (size_t i=0;i<NUM_RANKS;i++)
{
for (size_t j=0;j<NUM_BANKS;j++)
{
if (bankStates[i][j].stateChangeCountdown>0)
{
//decrement counters
bankStates[i][j].stateChangeCountdown--;
//if counter has reached 0, change state
if (bankStates[i][j].stateChangeCountdown == 0)
{
switch (bankStates[i][j].lastCommand)
{
//only these commands have an implicit state change
case WRITE_P:
case READ_P:
bankStates[i][j].currentBankState = Precharging;
bankStates[i][j].lastCommand = PRECHARGE;
bankStates[i][j].stateChangeCountdown = tRP;
break;
case REFRESH:
case PRECHARGE:
bankStates[i][j].currentBankState = Idle;
break;
default:
break;
}
}
}
}
}
//check for outgoing command packets and handle countdowns
if (outgoingCmdPacket != NULL)
{
cmdCyclesLeft--;
if (cmdCyclesLeft == 0) //packet is ready to be received by rank
{
(*ranks)[outgoingCmdPacket->rank]->receiveFromBus(outgoingCmdPacket);
outgoingCmdPacket = NULL;
}
}
//check for outgoing data packets and handle countdowns
if (outgoingDataPacket != NULL)
{
dataCyclesLeft--;
if (dataCyclesLeft == 0)
{
//inform upper levels that a write is done
if (parentMemorySystem->WriteDataDone!=NULL)
{
(*parentMemorySystem->WriteDataDone)(parentMemorySystem->systemID,outgoingDataPacket->physicalAddress, currentClockCycle);
}
(*ranks)[outgoingDataPacket->rank]->receiveFromBus(outgoingDataPacket);
outgoingDataPacket=NULL;
}
}
//if any outstanding write data needs to be sent
//and the appropriate amount of time has passed (WL)
//then send data on bus
//
//write data held in fifo vector along with countdowns
if (writeDataCountdown.size() > 0)
{
for (size_t i=0;i<writeDataCountdown.size();i++)
{
writeDataCountdown[i]--;
}
if (writeDataCountdown[0]==0)
{
//send to bus and print debug stuff
if (DEBUG_BUS)
{
PRINTN(" -- MC Issuing On Data Bus : ");
writeDataToSend[0]->print();
}
// queue up the packet to be sent
if (outgoingDataPacket != NULL)
{
ERROR("== Error - Data Bus Collision");
exit(-1);
}
outgoingDataPacket = writeDataToSend[0];
dataCyclesLeft = BL/2;
totalTransactions++;
totalWritesPerBank[SEQUENTIAL(writeDataToSend[0]->rank,writeDataToSend[0]->bank)]++;
writeDataCountdown.erase(writeDataCountdown.begin());
writeDataToSend.erase(writeDataToSend.begin());
}
}
//if its time for a refresh issue a refresh
// else pop from command queue if it's not empty
if (refreshCountdown[refreshRank]==0)
{
commandQueue.needRefresh(refreshRank);
(*ranks)[refreshRank]->refreshWaiting = true;
refreshCountdown[refreshRank] = REFRESH_PERIOD/tCK;
refreshRank++;
if (refreshRank == NUM_RANKS)
{
refreshRank = 0;
}
}
//if a rank is powered down, make sure we power it up in time for a refresh
else if (powerDown[refreshRank] && refreshCountdown[refreshRank] <= tXP)
{
(*ranks)[refreshRank]->refreshWaiting = true;
}
//pass a pointer to a poppedBusPacket
//function returns true if there is something valid in poppedBusPacket
if (commandQueue.pop(&poppedBusPacket))
{
if (poppedBusPacket->busPacketType == WRITE || poppedBusPacket->busPacketType == WRITE_P)
{
writeDataToSend.push_back(new BusPacket(DATA, poppedBusPacket->physicalAddress, poppedBusPacket->column,
poppedBusPacket->row, poppedBusPacket->rank, poppedBusPacket->bank,
poppedBusPacket->data, dramsim_log));
writeDataCountdown.push_back(WL);
}
//
//update each bank's state based on the command that was just popped out of the command queue
//
//for readability's sake
unsigned rank = poppedBusPacket->rank;
unsigned bank = poppedBusPacket->bank;
switch (poppedBusPacket->busPacketType)
{
case READ_P:
case READ:
//add energy to account for total
if (DEBUG_POWER)
{
PRINT(" ++ Adding Read energy to total energy");
}
burstEnergy[rank] += (IDD4R - IDD3N) * BL/2 * NUM_DEVICES;
if (poppedBusPacket->busPacketType == READ_P)
{
//Don't bother setting next read or write times because the bank is no longer active
//bankStates[rank][bank].currentBankState = Idle;
bankStates[rank][bank].nextActivate = max(currentClockCycle + READ_AUTOPRE_DELAY,
bankStates[rank][bank].nextActivate);
bankStates[rank][bank].lastCommand = READ_P;
bankStates[rank][bank].stateChangeCountdown = READ_TO_PRE_DELAY;
}
else if (poppedBusPacket->busPacketType == READ)
{
bankStates[rank][bank].nextPrecharge = max(currentClockCycle + READ_TO_PRE_DELAY,
bankStates[rank][bank].nextPrecharge);
bankStates[rank][bank].lastCommand = READ;
}
for (size_t i=0;i<NUM_RANKS;i++)
{
for (size_t j=0;j<NUM_BANKS;j++)
{
if (i!=poppedBusPacket->rank)
{
//check to make sure it is active before trying to set (save's time?)
if (bankStates[i][j].currentBankState == RowActive)
{
bankStates[i][j].nextRead = max(currentClockCycle + BL/2 + tRTRS, bankStates[i][j].nextRead);
bankStates[i][j].nextWrite = max(currentClockCycle + READ_TO_WRITE_DELAY,
bankStates[i][j].nextWrite);
}
}
else
{
bankStates[i][j].nextRead = max(currentClockCycle + max(tCCD, BL/2), bankStates[i][j].nextRead);
bankStates[i][j].nextWrite = max(currentClockCycle + READ_TO_WRITE_DELAY,
bankStates[i][j].nextWrite);
}
}
}
if (poppedBusPacket->busPacketType == READ_P)
{
//set read and write to nextActivate so the state table will prevent a read or write
// being issued (in cq.isIssuable())before the bank state has been changed because of the
// auto-precharge associated with this command
bankStates[rank][bank].nextRead = bankStates[rank][bank].nextActivate;
bankStates[rank][bank].nextWrite = bankStates[rank][bank].nextActivate;
}
break;
case WRITE_P:
case WRITE:
if (poppedBusPacket->busPacketType == WRITE_P)
{
bankStates[rank][bank].nextActivate = max(currentClockCycle + WRITE_AUTOPRE_DELAY,
bankStates[rank][bank].nextActivate);
bankStates[rank][bank].lastCommand = WRITE_P;
bankStates[rank][bank].stateChangeCountdown = WRITE_TO_PRE_DELAY;
}
else if (poppedBusPacket->busPacketType == WRITE)
{
bankStates[rank][bank].nextPrecharge = max(currentClockCycle + WRITE_TO_PRE_DELAY,
bankStates[rank][bank].nextPrecharge);
bankStates[rank][bank].lastCommand = WRITE;
}
//add energy to account for total
if (DEBUG_POWER)
{
PRINT(" ++ Adding Write energy to total energy");
}
burstEnergy[rank] += (IDD4W - IDD3N) * BL/2 * NUM_DEVICES;
for (size_t i=0;i<NUM_RANKS;i++)
{
for (size_t j=0;j<NUM_BANKS;j++)
{
if (i!=poppedBusPacket->rank)
{
if (bankStates[i][j].currentBankState == RowActive)
{
bankStates[i][j].nextWrite = max(currentClockCycle + BL/2 + tRTRS, bankStates[i][j].nextWrite);
bankStates[i][j].nextRead = max(currentClockCycle + WRITE_TO_READ_DELAY_R,
bankStates[i][j].nextRead);
}
}
else
{
bankStates[i][j].nextWrite = max(currentClockCycle + max(BL/2, tCCD), bankStates[i][j].nextWrite);
bankStates[i][j].nextRead = max(currentClockCycle + WRITE_TO_READ_DELAY_B,
bankStates[i][j].nextRead);
}
}
}
//set read and write to nextActivate so the state table will prevent a read or write
// being issued (in cq.isIssuable())before the bank state has been changed because of the
// auto-precharge associated with this command
if (poppedBusPacket->busPacketType == WRITE_P)
{
bankStates[rank][bank].nextRead = bankStates[rank][bank].nextActivate;
bankStates[rank][bank].nextWrite = bankStates[rank][bank].nextActivate;
}
break;
case ACTIVATE:
//add energy to account for total
if (DEBUG_POWER)
{
PRINT(" ++ Adding Activate and Precharge energy to total energy");
}
actpreEnergy[rank] += ((IDD0 * tRC) - ((IDD3N * tRAS) + (IDD2N * (tRC - tRAS)))) * NUM_DEVICES;
bankStates[rank][bank].currentBankState = RowActive;
bankStates[rank][bank].lastCommand = ACTIVATE;
bankStates[rank][bank].openRowAddress = poppedBusPacket->row;
bankStates[rank][bank].nextActivate = max(currentClockCycle + tRC, bankStates[rank][bank].nextActivate);
bankStates[rank][bank].nextPrecharge = max(currentClockCycle + tRAS, bankStates[rank][bank].nextPrecharge);
//if we are using posted-CAS, the next column access can be sooner than normal operation
bankStates[rank][bank].nextRead = max(currentClockCycle + (tRCD-AL), bankStates[rank][bank].nextRead);
bankStates[rank][bank].nextWrite = max(currentClockCycle + (tRCD-AL), bankStates[rank][bank].nextWrite);
for (size_t i=0;i<NUM_BANKS;i++)
{
if (i!=poppedBusPacket->bank)
{
bankStates[rank][i].nextActivate = max(currentClockCycle + tRRD, bankStates[rank][i].nextActivate);
}
}
break;
case PRECHARGE:
bankStates[rank][bank].currentBankState = Precharging;
bankStates[rank][bank].lastCommand = PRECHARGE;
bankStates[rank][bank].stateChangeCountdown = tRP;
bankStates[rank][bank].nextActivate = max(currentClockCycle + tRP, bankStates[rank][bank].nextActivate);
break;
case REFRESH:
//add energy to account for total
if (DEBUG_POWER)
{
PRINT(" ++ Adding Refresh energy to total energy");
}
refreshEnergy[rank] += (IDD5 - IDD3N) * tRFC * NUM_DEVICES;
for (size_t i=0;i<NUM_BANKS;i++)
{
bankStates[rank][i].nextActivate = currentClockCycle + tRFC;
bankStates[rank][i].currentBankState = Refreshing;
bankStates[rank][i].lastCommand = REFRESH;
bankStates[rank][i].stateChangeCountdown = tRFC;
}
break;
default:
ERROR("== Error - Popped a command we shouldn't have of type : " << poppedBusPacket->busPacketType);
exit(0);
}
//issue on bus and print debug
if (DEBUG_BUS)
{
PRINTN(" -- MC Issuing On Command Bus : ");
poppedBusPacket->print();
}
//check for collision on bus
if (outgoingCmdPacket != NULL)
{
ERROR("== Error - Command Bus Collision");
exit(-1);
}
outgoingCmdPacket = poppedBusPacket;
cmdCyclesLeft = tCMD;
}
for (size_t i=0;i<transactionQueue.size();i++)
{
//pop off top transaction from queue
//
// assuming simple scheduling at the moment
// will eventually add policies here
Transaction *transaction = transactionQueue[i];
//map address to rank,bank,row,col
unsigned newTransactionChan, newTransactionRank, newTransactionBank, newTransactionRow, newTransactionColumn;
// pass these in as references so they get set by the addressMapping function
addressMapping(transaction->address, newTransactionChan, newTransactionRank, newTransactionBank, newTransactionRow, newTransactionColumn);
//if we have room, break up the transaction into the appropriate commands
//and add them to the command queue
if (commandQueue.hasRoomFor(2, newTransactionRank, newTransactionBank))
{
if (DEBUG_ADDR_MAP)
{
PRINTN("== New Transaction - Mapping Address [0x" << hex << transaction->address << dec << "]");
if (transaction->transactionType == DATA_READ)
{
PRINT(" (Read)");
}
else
{
PRINT(" (Write)");
}
PRINT(" Rank : " << newTransactionRank);
PRINT(" Bank : " << newTransactionBank);
PRINT(" Row : " << newTransactionRow);
PRINT(" Col : " << newTransactionColumn);
}
//now that we know there is room in the command queue, we can remove from the transaction queue
transactionQueue.erase(transactionQueue.begin()+i);
//create activate command to the row we just translated
BusPacket *ACTcommand = new BusPacket(ACTIVATE, transaction->address,
newTransactionColumn, newTransactionRow, newTransactionRank,
newTransactionBank, 0, dramsim_log);
//create read or write command and enqueue it
BusPacketType bpType = transaction->getBusPacketType();
BusPacket *command = new BusPacket(bpType, transaction->address,
newTransactionColumn, newTransactionRow, newTransactionRank,
newTransactionBank, transaction->data, dramsim_log);
commandQueue.enqueue(ACTcommand);
commandQueue.enqueue(command);
// If we have a read, save the transaction so when the data comes back
// in a bus packet, we can staple it back into a transaction and return it
if (transaction->transactionType == DATA_READ)
{
pendingReadTransactions.push_back(transaction);
}
else
{
// just delete the transaction now that it's a buspacket
delete transaction;
}
/* only allow one transaction to be scheduled per cycle -- this should
* be a reasonable assumption considering how much logic would be
* required to schedule multiple entries per cycle (parallel data
* lines, switching logic, decision logic)
*/
break;
}
else // no room, do nothing this cycle
{
//PRINT( "== Warning - No room in command queue" << endl;
}
}
//calculate power
// this is done on a per-rank basis, since power characterization is done per device (not per bank)
for (size_t i=0;i<NUM_RANKS;i++)
{
if (USE_LOW_POWER)
{
//if there are no commands in the queue and that particular rank is not waiting for a refresh...
if (commandQueue.isEmpty(i) && !(*ranks)[i]->refreshWaiting)
{
//check to make sure all banks are idle
bool allIdle = true;
for (size_t j=0;j<NUM_BANKS;j++)
{
if (bankStates[i][j].currentBankState != Idle)
{
allIdle = false;
break;
}
}
//if they ARE all idle, put in power down mode and set appropriate fields
if (allIdle)
{
powerDown[i] = true;
(*ranks)[i]->powerDown();
for (size_t j=0;j<NUM_BANKS;j++)
{
bankStates[i][j].currentBankState = PowerDown;
bankStates[i][j].nextPowerUp = currentClockCycle + tCKE;
}
}
}
//if there IS something in the queue or there IS a refresh waiting (and we can power up), do it
else if (currentClockCycle >= bankStates[i][0].nextPowerUp && powerDown[i]) //use 0 since theyre all the same
{
powerDown[i] = false;
(*ranks)[i]->powerUp();
for (size_t j=0;j<NUM_BANKS;j++)
{
bankStates[i][j].currentBankState = Idle;
bankStates[i][j].nextActivate = currentClockCycle + tXP;
}
}
}
//check for open bank
bool bankOpen = false;
for (size_t j=0;j<NUM_BANKS;j++)
{
if (bankStates[i][j].currentBankState == Refreshing ||
bankStates[i][j].currentBankState == RowActive)
{
bankOpen = true;
break;
}
}
//background power is dependent on whether or not a bank is open or not
if (bankOpen)
{
if (DEBUG_POWER)
{
PRINT(" ++ Adding IDD3N to total energy [from rank "<< i <<"]");
}
backgroundEnergy[i] += IDD3N * NUM_DEVICES;
}
else
{
//if we're in power-down mode, use the correct current
if (powerDown[i])
{
if (DEBUG_POWER)
{
PRINT(" ++ Adding IDD2P to total energy [from rank " << i << "]");
}
backgroundEnergy[i] += IDD2P * NUM_DEVICES;
}
else
{
if (DEBUG_POWER)
{
PRINT(" ++ Adding IDD2N to total energy [from rank " << i << "]");
}
backgroundEnergy[i] += IDD2N * NUM_DEVICES;
}
}
}
//check for outstanding data to return to the CPU
if (returnTransaction.size()>0)
{
if (DEBUG_BUS)
{
PRINTN(" -- MC Issuing to CPU bus : " << *returnTransaction[0]);
}
totalTransactions++;
bool foundMatch=false;
//find the pending read transaction to calculate latency
for (size_t i=0;i<pendingReadTransactions.size();i++)
{
if (pendingReadTransactions[i]->address == returnTransaction[0]->address)
{
//if(currentClockCycle - pendingReadTransactions[i]->timeAdded > 2000)
// {
// pendingReadTransactions[i]->print();
// exit(0);
// }
unsigned chan,rank,bank,row,col;
addressMapping(returnTransaction[0]->address,chan,rank,bank,row,col);
insertHistogram(currentClockCycle-pendingReadTransactions[i]->timeAdded,rank,bank);
//return latency
returnReadData(pendingReadTransactions[i]);
delete pendingReadTransactions[i];
pendingReadTransactions.erase(pendingReadTransactions.begin()+i);
foundMatch=true;
break;
}
}
if (!foundMatch)
{
ERROR("Can't find a matching transaction for 0x"<<hex<<returnTransaction[0]->address<<dec);
abort();
}
delete returnTransaction[0];
returnTransaction.erase(returnTransaction.begin());
}
//decrement refresh counters
for (size_t i=0;i<NUM_RANKS;i++)
{
refreshCountdown[i]--;
}
//
//print debug
//
if (DEBUG_TRANS_Q)
{
PRINT("== Printing transaction queue");
for (size_t i=0;i<transactionQueue.size();i++)
{
PRINTN(" " << i << "] "<< *transactionQueue[i]);
}
}
if (DEBUG_BANKSTATE)
{
//TODO: move this to BankState.cpp
PRINT("== Printing bank states (According to MC)");
for (size_t i=0;i<NUM_RANKS;i++)
{
for (size_t j=0;j<NUM_BANKS;j++)
{
if (bankStates[i][j].currentBankState == RowActive)
{
PRINTN("[" << bankStates[i][j].openRowAddress << "] ");
}
else if (bankStates[i][j].currentBankState == Idle)
{
PRINTN("[idle] ");
}
else if (bankStates[i][j].currentBankState == Precharging)
{
PRINTN("[pre] ");
}
else if (bankStates[i][j].currentBankState == Refreshing)
{
PRINTN("[ref] ");
}
else if (bankStates[i][j].currentBankState == PowerDown)
{
PRINTN("[lowp] ");
}
}
PRINT(""); // effectively just cout<<endl;
}
}
if (DEBUG_CMD_Q)
{
commandQueue.print();
}
commandQueue.step();
}
bool MemoryController::WillAcceptTransaction()
{
return transactionQueue.size() < TRANS_QUEUE_DEPTH;
}
//allows outside source to make request of memory system
bool MemoryController::addTransaction(Transaction *trans)
{
if (WillAcceptTransaction())
{
trans->timeAdded = currentClockCycle;
transactionQueue.push_back(trans);
return true;
}
else
{
return false;
}
}
void MemoryController::resetStats()
{
for (size_t i=0; i<NUM_RANKS; i++)
{
for (size_t j=0; j<NUM_BANKS; j++)
{
//XXX: this means the bank list won't be printed for partial epochs
grandTotalBankAccesses[SEQUENTIAL(i,j)] += totalReadsPerBank[SEQUENTIAL(i,j)] + totalWritesPerBank[SEQUENTIAL(i,j)];
totalReadsPerBank[SEQUENTIAL(i,j)] = 0;
totalWritesPerBank[SEQUENTIAL(i,j)] = 0;
totalEpochLatency[SEQUENTIAL(i,j)] = 0;
}
burstEnergy[i] = 0;
actpreEnergy[i] = 0;
refreshEnergy[i] = 0;
backgroundEnergy[i] = 0;
totalReadsPerRank[i] = 0;
totalWritesPerRank[i] = 0;
}
}
//prints statistics at the end of an epoch or simulation
void MemoryController::printStats(bool finalStats)
{
unsigned myChannel = parentMemorySystem->systemID;
//if we are not at the end of the epoch, make sure to adjust for the actual number of cycles elapsed
uint64_t cyclesElapsed = (currentClockCycle % EPOCH_LENGTH == 0) ? EPOCH_LENGTH : currentClockCycle % EPOCH_LENGTH;
unsigned bytesPerTransaction = (JEDEC_DATA_BUS_BITS*BL)/8;
uint64_t totalBytesTransferred = totalTransactions * bytesPerTransaction;
double secondsThisEpoch = (double)cyclesElapsed * tCK * 1E-9;
// only per rank
vector<double> backgroundPower = vector<double>(NUM_RANKS,0.0);
vector<double> burstPower = vector<double>(NUM_RANKS,0.0);
vector<double> refreshPower = vector<double>(NUM_RANKS,0.0);
vector<double> actprePower = vector<double>(NUM_RANKS,0.0);
vector<double> averagePower = vector<double>(NUM_RANKS,0.0);
// per bank variables
vector<double> averageLatency = vector<double>(NUM_RANKS*NUM_BANKS,0.0);
vector<double> bandwidth = vector<double>(NUM_RANKS*NUM_BANKS,0.0);
double totalBandwidth=0.0;
for (size_t i=0;i<NUM_RANKS;i++)
{
for (size_t j=0; j<NUM_BANKS; j++)
{
bandwidth[SEQUENTIAL(i,j)] = (((double)(totalReadsPerBank[SEQUENTIAL(i,j)]+totalWritesPerBank[SEQUENTIAL(i,j)]) * (double)bytesPerTransaction)/(1024.0*1024.0*1024.0)) / secondsThisEpoch;
averageLatency[SEQUENTIAL(i,j)] = ((float)totalEpochLatency[SEQUENTIAL(i,j)] / (float)(totalReadsPerBank[SEQUENTIAL(i,j)])) * tCK;
totalBandwidth+=bandwidth[SEQUENTIAL(i,j)];
totalReadsPerRank[i] += totalReadsPerBank[SEQUENTIAL(i,j)];
totalWritesPerRank[i] += totalWritesPerBank[SEQUENTIAL(i,j)];
}
}
#ifdef LOG_OUTPUT
dramsim_log.precision(3);
dramsim_log.setf(ios::fixed,ios::floatfield);
#else
cout.precision(3);
cout.setf(ios::fixed,ios::floatfield);
#endif
PRINT( " =======================================================" );
PRINT( " ============== Printing Statistics [id:"<<parentMemorySystem->systemID<<"]==============" );
PRINTN( " Total Return Transactions : " << totalTransactions );
PRINT( " ("<<totalBytesTransferred <<" bytes) aggregate average bandwidth "<<totalBandwidth<<"GB/s");
double totalAggregateBandwidth = 0.0;
for (size_t r=0;r<NUM_RANKS;r++)
{
PRINT( " -Rank "<<r<<" : ");
PRINTN( " -Reads : " << totalReadsPerRank[r]);
PRINT( " ("<<totalReadsPerRank[r] * bytesPerTransaction<<" bytes)");
PRINTN( " -Writes : " << totalWritesPerRank[r]);
PRINT( " ("<<totalWritesPerRank[r] * bytesPerTransaction<<" bytes)");
for (size_t j=0;j<NUM_BANKS;j++)
{
PRINT( " -Bandwidth / Latency (Bank " <<j<<"): " <<bandwidth[SEQUENTIAL(r,j)] << " GB/s\t\t" <<averageLatency[SEQUENTIAL(r,j)] << " ns");
}
// factor of 1000 at the end is to account for the fact that totalEnergy is accumulated in mJ since IDD values are given in mA
backgroundPower[r] = ((double)backgroundEnergy[r] / (double)(cyclesElapsed)) * Vdd / 1000.0;
burstPower[r] = ((double)burstEnergy[r] / (double)(cyclesElapsed)) * Vdd / 1000.0;
refreshPower[r] = ((double) refreshEnergy[r] / (double)(cyclesElapsed)) * Vdd / 1000.0;
actprePower[r] = ((double)actpreEnergy[r] / (double)(cyclesElapsed)) * Vdd / 1000.0;
averagePower[r] = ((backgroundEnergy[r] + burstEnergy[r] + refreshEnergy[r] + actpreEnergy[r]) / (double)cyclesElapsed) * Vdd / 1000.0;
if ((*parentMemorySystem->ReportPower)!=NULL)
{
(*parentMemorySystem->ReportPower)(backgroundPower[r],burstPower[r],refreshPower[r],actprePower[r]);
}
PRINT( " == Power Data for Rank " << r );
PRINT( " Average Power (watts) : " << averagePower[r] );
PRINT( " -Background (watts) : " << backgroundPower[r] );
PRINT( " -Act/Pre (watts) : " << actprePower[r] );
PRINT( " -Burst (watts) : " << burstPower[r]);
PRINT( " -Refresh (watts) : " << refreshPower[r] );
if (VIS_FILE_OUTPUT)
{
// cout << "c="<<myChannel<< " r="<<r<<"writing to csv out on cycle "<< currentClockCycle<<endl;
// write the vis file output
csvOut << CSVWriter::IndexedName("Background_Power",myChannel,r) <<backgroundPower[r];
csvOut << CSVWriter::IndexedName("ACT_PRE_Power",myChannel,r) << actprePower[r];
csvOut << CSVWriter::IndexedName("Burst_Power",myChannel,r) << burstPower[r];
csvOut << CSVWriter::IndexedName("Refresh_Power",myChannel,r) << refreshPower[r];
double totalRankBandwidth=0.0;
for (size_t b=0; b<NUM_BANKS; b++)
{
csvOut << CSVWriter::IndexedName("Bandwidth",myChannel,r,b) << bandwidth[SEQUENTIAL(r,b)];
totalRankBandwidth += bandwidth[SEQUENTIAL(r,b)];
totalAggregateBandwidth += bandwidth[SEQUENTIAL(r,b)];
csvOut << CSVWriter::IndexedName("Average_Latency",myChannel,r,b) << averageLatency[SEQUENTIAL(r,b)];
}
csvOut << CSVWriter::IndexedName("Rank_Aggregate_Bandwidth",myChannel,r) << totalRankBandwidth;
csvOut << CSVWriter::IndexedName("Rank_Average_Bandwidth",myChannel,r) << totalRankBandwidth/NUM_RANKS;
}
}
if (VIS_FILE_OUTPUT)
{
csvOut << CSVWriter::IndexedName("Aggregate_Bandwidth",myChannel) << totalAggregateBandwidth;
csvOut << CSVWriter::IndexedName("Average_Bandwidth",myChannel) << totalAggregateBandwidth / (NUM_RANKS*NUM_BANKS);
}
// only print the latency histogram at the end of the simulation since it clogs the output too much to print every epoch
if (finalStats)
{
PRINT( " --- Latency list ("<<latencies.size()<<")");
PRINT( " [lat] : #");
if (VIS_FILE_OUTPUT)
{
csvOut.getOutputStream() << "!!HISTOGRAM_DATA"<<endl;
}
map<unsigned,unsigned>::iterator it; //
for (it=latencies.begin(); it!=latencies.end(); it++)
{
PRINT( " ["<< it->first <<"-"<<it->first+(HISTOGRAM_BIN_SIZE-1)<<"] : "<< it->second );
if (VIS_FILE_OUTPUT)
{
csvOut.getOutputStream() << it->first <<"="<< it->second << endl;
}
}
if (currentClockCycle % EPOCH_LENGTH == 0)
{
PRINT( " --- Grand Total Bank usage list");
for (size_t i=0;i<NUM_RANKS;i++)
{
PRINT("Rank "<<i<<":");
for (size_t j=0;j<NUM_BANKS;j++)
{
PRINT( " b"<<j<<": "<<grandTotalBankAccesses[SEQUENTIAL(i,j)]);
}
}
}
}
PRINT(endl<< " == Pending Transactions : "<<pendingReadTransactions.size()<<" ("<<currentClockCycle<<")==");
/*
for(size_t i=0;i<pendingReadTransactions.size();i++)
{
PRINT( i << "] I've been waiting for "<<currentClockCycle-pendingReadTransactions[i].timeAdded<<endl;
}
*/
#ifdef LOG_OUTPUT
dramsim_log.flush();
#endif
resetStats();
}
MemoryController::~MemoryController()
{
//ERROR("MEMORY CONTROLLER DESTRUCTOR");
//abort();
for (size_t i=0; i<pendingReadTransactions.size(); i++)
{
delete pendingReadTransactions[i];
}
for (size_t i=0; i<returnTransaction.size(); i++)
{
delete returnTransaction[i];
}
}
//inserts a latency into the latency histogram
void MemoryController::insertHistogram(unsigned latencyValue, unsigned rank, unsigned bank)
{
totalEpochLatency[SEQUENTIAL(rank,bank)] += latencyValue;
//poor man's way to bin things.
latencies[(latencyValue/HISTOGRAM_BIN_SIZE)*HISTOGRAM_BIN_SIZE]++;
}