-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBOB.cpp
More file actions
1110 lines (949 loc) · 35.2 KB
/
BOB.cpp
File metadata and controls
1110 lines (949 loc) · 35.2 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
/*********************************************************************************
* 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.
*********************************************************************************/
//BOB source
#include <bitset>
#include <math.h>
#include "BOB.h"
using namespace std;
namespace BOBSim
{
#ifdef LOG_OUTPUT
ofstream logOutput;
#endif
unsigned LINK_CPU_CLK_RATIO;
unsigned DRAM_CPU_CLK_RATIO;
unsigned DRAM_CPU_CLK_ADJUSTMENT;
//uint64_t QEMU_MEMORY_SIZE;
BOB::BOB() : priorityPort(0),
readCounter(0),
writeCounter(0),
committedWrites(0),
clockCycleAdjustmentCounter(0),
writeIssuedCB(NULL),
rankBitWidth(log2(NUM_RANKS)),
bankBitWidth(log2(NUM_BANKS)),
rowBitWidth(log2(NUM_ROWS)),
colBitWidth(log2(NUM_COLS)),
busOffsetBitWidth(log2(BUS_ALIGNMENT_SIZE)),
channelBitWidth(log2(NUM_CHANNELS)),
cacheOffset(log2(CACHE_LINE_SIZE))
{
/*
HACK!!! - QEMU does not support the huge memory sizes BOB can simulate.
Removing bits from the row address will have the least impact on function
*/
uint qemuMemoryBitWidth = log2_64(QEMU_MEMORY_SIZE);
uint bitDifference = (busOffsetBitWidth + colBitWidth + rowBitWidth + channelBitWidth + rankBitWidth + bankBitWidth) - qemuMemoryBitWidth;
rowBitWidth = rowBitWidth - bitDifference;
//end hack
#ifdef LOG_OUTPUT
string output_filename("BOBsim");
if (getenv("SIM_DESC"))
output_filename += getenv("SIM_DESC");
output_filename += VARIANT_SUFFIX;
output_filename += ".log";
logOutput.open(output_filename.c_str());
#endif
DEBUG("!!!!!!! QEMU_MEMORY_SIZE :"<<QEMU_MEMORY_SIZE<<" qemuMemoryBitWidth : "<<qemuMemoryBitWidth<<" newRowBitWidth : "<<rowBitWidth<<"\n");
DEBUG("busoff:"<<busOffsetBitWidth<<" col:"<<colBitWidth<<" row:"<<rowBitWidth<<" rank:"<<rankBitWidth<<" bank:"<<bankBitWidth<<" chan:"<<channelBitWidth);
currentClockCycle = 0;
//Compute ratios of various clock frequencies
if(fmod(CPU_CLK_PERIOD,LINK_BUS_CLK_PERIOD)>0.00001)
{
ERROR("== ERROR - Link bus clock period ("<<LINK_BUS_CLK_PERIOD<<") must be a factor of CPU clock period ("<<CPU_CLK_PERIOD<<")");
ERROR("== fmod="<<fmod(CPU_CLK_PERIOD,LINK_BUS_CLK_PERIOD));
abort();
}
LINK_CPU_CLK_RATIO = (CPU_CLK_PERIOD / LINK_BUS_CLK_PERIOD);
cout<<"Channel to CPU ratio : "<<LINK_CPU_CLK_RATIO<<endl;
DRAM_CPU_CLK_RATIO = tCK / CPU_CLK_PERIOD;
cout<<"DRAM to CPU ratio : "<<DRAM_CPU_CLK_RATIO<<endl;
DRAM_CPU_CLK_ADJUSTMENT = tCK / fmod(tCK,CPU_CLK_PERIOD);
cout<<"DRAM_CPU_CLK_ADJUSTMENT : "<<DRAM_CPU_CLK_ADJUSTMENT<<endl;
//Ensure that parameters have been set correclty
if(NUM_CHANNELS / CHANNELS_PER_LINK_BUS != NUM_LINK_BUSES)
{
ERROR("== ERROR - Mismatch in NUM_CHANNELS and NUM_LINK_BUSES");
exit(0);
}
if(DEBUG_BOB) DEBUG("== Channel-CPU clk ratio : "<<LINK_CPU_CLK_RATIO<<" DRAM-CPU clk ratio : "<<DRAM_CPU_CLK_RATIO);
//Make port objects
for(unsigned i=0; i<NUM_PORTS; i++)
{
ports.push_back(Port(i));
}
//Initialize fields for bookkeeping
inFlightRequestLinkCountdowns = vector<unsigned>(NUM_LINK_BUSES,0);
inFlightResponseLinkCountdowns = vector<unsigned>(NUM_LINK_BUSES,0);
inFlightRequestLink = vector<Transaction *> (NUM_LINK_BUSES, (Transaction*)(NULL));
inFlightResponseLink = vector<Transaction *> (NUM_LINK_BUSES, (Transaction*)NULL);
serDesBufferRequest = vector<Transaction *> (NUM_LINK_BUSES, (Transaction*)NULL);
serDesBufferResponse = vector<Transaction *> (NUM_LINK_BUSES, (Transaction*)NULL);
responseLinkRoundRobin = vector<unsigned> (NUM_LINK_BUSES,0);
channelCounters = vector<unsigned>(NUM_CHANNELS,0);
channelCountersLifetime = vector<uint64_t>(NUM_CHANNELS,0);
portInputBufferAvg = vector<uint> (NUM_PORTS, 0);
portOutputBufferAvg = vector<uint> (NUM_PORTS, 0);
requestLinkIdle = vector<unsigned> (NUM_LINK_BUSES,0);
responseLinkIdle = vector<unsigned> (NUM_LINK_BUSES,0);
cmdQFull = vector<uint>(NUM_CHANNELS,0);
//Define callback function
Callback<BOB, void, BusPacket*, unsigned> *reportCallback = new Callback<BOB, void, BusPacket*, unsigned>(this, &BOB::ReportCallback);
//Create channels
channels.reserve(NUM_CHANNELS);
for(unsigned i=0; i<NUM_CHANNELS; i++)
{
channels.push_back(new DRAMChannel(i,reportCallback));
}
//Used for round-robin
priorityLinkBus = vector<unsigned>(NUM_PORTS,0);
}
void BOB::Update()
{
//
//STATS KEEPING
//
//keep track of how long data has been waiting in the channel for the switch to become available
for(unsigned i=0; i<pendingReads.size(); i++)
{
//look in the channel that the request was mapped to and search its return queue
for(unsigned j=0; j<channels[pendingReads[i]->mappedChannel]->readReturnQueue.size(); j++)
{
//if the transaction IDs match, then the data is waiting there to return (it might not be ready yet,
// which would not trigger this condition)
if(pendingReads[i]->transactionID==channels[pendingReads[i]->mappedChannel]->readReturnQueue[j]->transactionID)
{
pendingReads[i]->cyclesInReadReturnQ++;
}
}
}
//calculate number of transactions waiting
for(unsigned i=0; i<NUM_CHANNELS; i++)
{
for(unsigned j=0; j<channels[i]->simpleController.commandQueue.size(); j++)
{
if(channels[i]->simpleController.commandQueue[j]->busPacketType==ACTIVATE)
{
channels[i]->simpleController.commandQueue[j]->queueWaitTime++;
}
}
}
//keep track of idle link buses
for(unsigned i=0; i<NUM_LINK_BUSES; i++)
{
if(inFlightRequestLink[i]==NULL)
{
requestLinkIdle[i]++;
}
if(inFlightResponseLink[i]==NULL)
{
responseLinkIdle[i]++;
}
}
//keep track of average entries in port in/out-buffers
for(unsigned i=0; i<NUM_PORTS; i++)
{
portInputBufferAvg[i] += ports[i].inputBuffer.size();
portOutputBufferAvg[i] += ports[i].outputBuffer.size();
}
//
// DEBUG OUTPUT
//
if(DEBUG_BOB)
{
DEBUG(endl<<" --------- BOB Update Started ["<<currentClockCycle<<"] ---------");
for(unsigned i=0; i<NUM_PORTS; i++)
{
DEBUG("== Port "<<i);
DEBUG(" - Input Buffer ("<<ports[i].inputBuffer.size()<<") == Busy Countdown:"<<ports[i].inputBusyCountdown);
for(unsigned j=0; j<ports[i].inputBuffer.size(); j++)
{
DEBUG(" "<<j<<"] "<<*ports[i].inputBuffer[j]);
}
DEBUG(" - Output Buffer ("<<ports[i].outputBuffer.size()<<") == Busy Countdown:"<<ports[i].outputBusyCountdown);
for(unsigned j=0; j<ports[i].outputBuffer.size(); j++)
{
DEBUG(" "<<j<<"] "<<*ports[i].outputBuffer[j]);
}
}
DEBUG("== SerDe Buffers");
for(unsigned i=0; i<NUM_LINK_BUSES; i++)
{
DEBUGN(" "<<i<<"] request: ");
if(serDesBufferRequest[i]==NULL)
{
DEBUGN("NULL");
}
else
{
DEBUGN(*serDesBufferRequest[i]);
}
DEBUGN(" response: ");
if(serDesBufferResponse[i]==NULL)
{
DEBUG("NULL");
}
else
{
DEBUG(*serDesBufferResponse[i]);
}
}
DEBUG("== Request Link Transactions");
for(unsigned i=0; i<NUM_LINK_BUSES; i++)
{
if(inFlightRequestLink[i]!=NULL)
{
DEBUG(" "<<i<<"] "<<*inFlightRequestLink[i]<<" for "<<inFlightRequestLinkCountdowns[i]<<" cc");
}
else
{
DEBUG(" "<<i<<"] NULL");
}
}
DEBUG("== Response Link Transactions");
for(unsigned i=0; i<NUM_LINK_BUSES; i++)
{
if(inFlightResponseLink[i]!=NULL)
{
DEBUG(" "<<i<<"] "<<*inFlightResponseLink[i]<<" for "<<inFlightResponseLinkCountdowns[i]<<" cc");
}
else
{
DEBUG(" "<<i<<"] NULL");
}
}
DEBUG("== Pending Read Queue ("<<pendingReads.size()<<")");
for(unsigned i=0; i<pendingReads.size(); i++)
{
DEBUG(" "<<i<<"] "<<*pendingReads[i]);
}
}
//
// UPDATE BUSES
//
if(DEBUG_BOB)DEBUG("== Bus Movement");
//update each port's bookkeeping
for(unsigned i=0; i<NUM_PORTS; i++)
{
if(ports[i].inputBusyCountdown>0)
{
ports[i].inputBusyCountdown--;
//if the port input is done sending, erase that item
if(ports[i].inputBusyCountdown==0)
{
}
}
if(ports[i].outputBusyCountdown>0)
{
ports[i].outputBusyCountdown--;
}
}
for(unsigned i=0; i<NUM_LINK_BUSES; i++)
{
if(inFlightRequestLinkCountdowns[i]>0)
{
inFlightRequestLinkCountdowns[i]--;
if(inFlightRequestLinkCountdowns[i]==0)
{
//compute total time in serDes and travel up channel
inFlightRequestLink[i]->cyclesReqLink = currentClockCycle - inFlightRequestLink[i]->cyclesReqLink;
if(DEBUG_BOB) DEBUG(" == Adding to channel "<<inFlightRequestLink[i]->mappedChannel<<" (from link bus "<<i<<") : "<<*inFlightRequestLink[i]);
//add to channel
channels[inFlightRequestLink[i]->mappedChannel]->AddTransaction(inFlightRequestLink[i], 0); //0 is not used
//remove from channel bus
inFlightRequestLink[i] = NULL;
//remove from SerDe buffer
serDesBufferRequest[i] = NULL;
}
}
if(inFlightResponseLinkCountdowns[i]>0)
{
inFlightResponseLinkCountdowns[i]--;
if(inFlightResponseLinkCountdowns[i]==0)
{
if(serDesBufferResponse[i]!=NULL)
{
ERROR("== Error - Response SerDe Buffer "<<i<<" collision");
exit(0);
}
//note the time
inFlightResponseLink[i]->channelTimeTotal = currentClockCycle - inFlightResponseLink[i]->channelStartTime;
//remove from return queue
delete channels[inFlightResponseLink[i]->mappedChannel]->readReturnQueue[0];
channels[inFlightResponseLink[i]->mappedChannel]->readReturnQueue.erase(channels[inFlightResponseLink[i]->mappedChannel]->readReturnQueue.begin());
serDesBufferResponse[i] = inFlightResponseLink[i];
inFlightResponseLink[i] = NULL;
}
}
}
//
// NEW STUFF
//
if(DEBUG_BOB) DEBUG("== Moving from SerDe to channel bus");
for(unsigned c=0; c<NUM_LINK_BUSES; c++)
{
//
//REQUEST
//
if(serDesBufferRequest[c]!=NULL &&
inFlightRequestLinkCountdowns[c]==0)
{
//error checking
if(inFlightRequestLink[c]!=NULL)
{
ERROR("== Error - item in SerDe buffer without channel being free");
ERROR(" == Channel : "<<c);
ERROR(" == Current : "<<*inFlightRequestLink[c]);
ERROR(" == SerDe : "<<*serDesBufferRequest[c]);
exit(0);
}
//put on channel bus
inFlightRequestLink[c] = serDesBufferRequest[c];
//note the time
inFlightRequestLink[c]->channelStartTime = currentClockCycle;
//the total number of channel cycles the packet will be on the bus
unsigned totalChannelCycles;
if(inFlightRequestLink[c]->transactionType==DATA_READ)
{
//
//widths are in bits
//
totalChannelCycles = (RD_REQUEST_PACKET_OVERHEAD * 8) / REQUEST_LINK_BUS_WIDTH +
!!((RD_REQUEST_PACKET_OVERHEAD * 8) % REQUEST_LINK_BUS_WIDTH);
}
else if(inFlightRequestLink[c]->transactionType==DATA_WRITE)
{
//
//widths are in bits
//
totalChannelCycles = ((WR_REQUEST_PACKET_OVERHEAD + TRANSACTION_SIZE) * 8) / REQUEST_LINK_BUS_WIDTH +
!!(((WR_REQUEST_PACKET_OVERHEAD + TRANSACTION_SIZE) * 8) % REQUEST_LINK_BUS_WIDTH);
}
else if(inFlightRequestLink[c]->transactionType==LOGIC_OPERATION)
{
//
//widths are in bits
//
totalChannelCycles = (inFlightRequestLink[c]->transactionSize * 8) / REQUEST_LINK_BUS_WIDTH +
!!((inFlightRequestLink[c]->transactionSize * 8) % REQUEST_LINK_BUS_WIDTH);
}
else
{
ERROR("== Error - wrong type "<<*inFlightRequestLink[c]);
exit(0);
}
//if the channel uses DDR signaling, the cycles is cut in half
if(LINK_BUS_USE_DDR)
{
totalChannelCycles = totalChannelCycles / 2 + !!(totalChannelCycles % 2);
}
//since the channel is faster than the CPU, figure out how many CPU
// cycles the channel will be in use
inFlightRequestLinkCountdowns[c] = (totalChannelCycles / LINK_CPU_CLK_RATIO) +
!!(totalChannelCycles%LINK_CPU_CLK_RATIO);
//error check
if(inFlightRequestLinkCountdowns[c]==0)
{
ERROR("== Error - countdown is 0");
exit(0);
}
if(DEBUG_BOB)
{
DEBUG(" == Channel Bus "<<c<<" getting : "<<*inFlightRequestLink[c]);
DEBUG(" == CPU Clks:"<<inFlightRequestLinkCountdowns[c]<<" Channel Clks:"<< totalChannelCycles<<" DDR?:"<<LINK_BUS_USE_DDR);
}
}
}
//
//Responses
//
for(unsigned p=0; p<NUM_PORTS; p++)
{
if(ports[p].outputBusyCountdown==0)
{
//check to see if something has been received from a channel
if(serDesBufferResponse[priorityLinkBus[p]]!=NULL &&
serDesBufferResponse[priorityLinkBus[p]]->portID==p)
{
serDesBufferResponse[priorityLinkBus[p]]->cyclesRspLink = currentClockCycle - serDesBufferResponse[priorityLinkBus[p]]->cyclesRspLink;
serDesBufferResponse[priorityLinkBus[p]]->cyclesRspPort = currentClockCycle;
ports[p].outputBuffer.push_back(serDesBufferResponse[priorityLinkBus[p]]);
switch(serDesBufferResponse[priorityLinkBus[p]]->transactionType)
{
case RETURN_DATA:
ports[p].outputBusyCountdown = TRANSACTION_SIZE / PORT_WIDTH;
break;
case LOGIC_RESPONSE:
ports[p].outputBusyCountdown = 1;
break;
default:
ERROR("== ERROR - Trying to add wrong type of transaction to output port : "<<*serDesBufferResponse[priorityLinkBus[p]]);
exit(0);
break;
};
serDesBufferResponse[priorityLinkBus[p]] = NULL;
}
priorityLinkBus[p]++;
if(priorityLinkBus[p]==NUM_LINK_BUSES)priorityLinkBus[p]=0;
}
}
//
//Move from input ports to serdes
//
if(DEBUG_BOB) DEBUG("== Moving from port buffer to SerDe");
for(unsigned port=0; port<NUM_PORTS; port++)
{
unsigned p = priorityPort;
if(DEBUG_BOB) DEBUG(" == Port "<<p);
//make sure the port isn't already sending something
// and that there is an item there to send
if(ports[p].inputBusyCountdown==0 &&
ports[p].inputBuffer.size()>0)
{
//search out-of-order
for(unsigned i=0; i<ports[p].inputBuffer.size(); i++)
{
unsigned channelID = FindChannelID(ports[p].inputBuffer[i]);
unsigned linkBusID = channelID / CHANNELS_PER_LINK_BUS;
//make sure the serDe isn't busy and the queue isn't full
if(serDesBufferRequest[linkBusID]==NULL &&
channels[channelID]->simpleController.waitingACTS<CHANNEL_WORK_Q_MAX)
{
//put on channel bus
serDesBufferRequest[linkBusID] = ports[p].inputBuffer[i];
serDesBufferRequest[linkBusID]->cyclesReqLink = currentClockCycle;
serDesBufferRequest[linkBusID]->cyclesReqPort = currentClockCycle - serDesBufferRequest[linkBusID]->cyclesReqPort;
serDesBufferRequest[linkBusID]->mappedChannel = channelID;
//keep track of requests
channelCounters[channelID]++;
if(serDesBufferRequest[linkBusID]->transactionType==DATA_READ)
{
//put in pending queue
// make it a RETURN_DATA type before we put it in pending queue
pendingReads.push_back(ports[p].inputBuffer[i]);
readCounter++;
//set port busy time
ports[p].inputBusyCountdown = 1;
}
else if(serDesBufferRequest[linkBusID]->transactionType==DATA_WRITE)
{
writeCounter++;
//set port busy time
ports[p].inputBusyCountdown = serDesBufferRequest[linkBusID]->transactionSize / PORT_WIDTH;
}
else if(serDesBufferRequest[linkBusID]->transactionType==LOGIC_OPERATION)
{
logicOpCounter++;
//set port busy time
ports[p].inputBusyCountdown = serDesBufferRequest[linkBusID]->transactionSize / PORT_WIDTH;
}
else
{
ERROR("== Error - unknown transaction type going to channel : "<<*serDesBufferRequest[linkBusID]);
exit(0);
}
if(DEBUG_BOB) DEBUG(" == Request SerDes "<<linkBusID<<" getting "<<*serDesBufferRequest[linkBusID]);
priorityPort++;
if(priorityPort==NUM_PORTS) priorityPort = 0;
//remove from port input buffer
ports[p].inputBuffer.erase(ports[p].inputBuffer.begin()+i);
break;
}
else
{
if(DEBUG_BOB)
{
if(serDesBufferRequest[linkBusID]!=NULL)
{
DEBUG(" == Request SerDes Full : "<<*serDesBufferRequest[linkBusID]);
DEBUG(" Left : "<<inFlightRequestLinkCountdowns[linkBusID]);
}
if(channels[channelID]->simpleController.waitingACTS>=CHANNEL_WORK_Q_MAX)
{
cmdQFull[channelID]++;
DEBUG(" == Channel Queue Full");
}
}
}
}
}
priorityPort++;
if(priorityPort==NUM_PORTS) priorityPort = 0;
}
priorityPort++;
if(priorityPort==NUM_PORTS) priorityPort = 0;
if(DEBUG_BOB) DEBUG("== Move from channel return queue to SerDe buffer");
for(unsigned link=0; link<NUM_LINK_BUSES; link++)
{
//make sure output is not busy sending something else
if(inFlightResponseLinkCountdowns[link]==0 &&
serDesBufferResponse[link]==NULL)
{
for(unsigned z=0; z<CHANNELS_PER_LINK_BUS; z++)
{
unsigned chan = link * CHANNELS_PER_LINK_BUS + responseLinkRoundRobin[link];
//make sure something is there
if(channels[chan]->pendingLogicResponse!=NULL)
{
//calculate numbers to see how long the response is on the bus
//
//widths are in bits
unsigned totalChannelCycles = (LOGIC_RESPONSE_PACKET_OVERHEAD * 8) / RESPONSE_LINK_BUS_WIDTH +
!!((LOGIC_RESPONSE_PACKET_OVERHEAD * 8) % RESPONSE_LINK_BUS_WIDTH);
if(LINK_BUS_USE_DDR)
{
totalChannelCycles = totalChannelCycles / 2 + !!(totalChannelCycles % 2);
}
//channel countdown
inFlightResponseLinkCountdowns[link] = totalChannelCycles / LINK_CPU_CLK_RATIO +
!!(totalChannelCycles % LINK_CPU_CLK_RATIO);
//make sure computation worked
if(inFlightResponseLinkCountdowns[link]==0)
{
ERROR("== ERROR - Countdown 0 on link "<<link);
ERROR("== totalChannelCycles : "<<totalChannelCycles);
exit(0);
}
//make in-flight
if(inFlightResponseLink[link]!=NULL)
{
ERROR("== Error - Trying to set Transaction on down channel while something is there");
ERROR(" Cycle : "<<currentClockCycle);
ERROR(" Channel : "<<chan);
ERROR(" LinkBus : "<<link);
ERROR(" Incoming: "<<*(channels[chan]->pendingLogicResponse));
ERROR(" Current : "<<*inFlightResponseLink[link]);
exit(0);
}
//make in flight
inFlightResponseLink[link] = channels[chan]->pendingLogicResponse;
//remove from channel
channels[chan]->pendingLogicResponse = NULL;
if(DEBUG_BOB)
{
DEBUG(" == Link Bus "<<link<<" returning "<<*inFlightResponseLink[link]);
DEBUG(" == CPU Clks:"<<inFlightResponseLinkCountdowns[link]<<" Channel Clks:"<<totalChannelCycles<<" DDR?:"<<LINK_BUS_USE_DDR);
}
break;
}
else if(channels[chan]->readReturnQueue.size()>0)
{
//remove transaction from pending queue
for(unsigned p=0; p<pendingReads.size(); p++)
{
//find pending item in pending queue
if(pendingReads[p]->transactionID == channels[chan]->readReturnQueue[0]->transactionID)
{
//make the return packet
pendingReads[p]->transactionType = RETURN_DATA;
//calculate numbers to see how long the response is on the bus
//
//widths are in bits
unsigned totalChannelCycles = ((RD_RESPONSE_PACKET_OVERHEAD + TRANSACTION_SIZE) * 8) / RESPONSE_LINK_BUS_WIDTH +
!!(((RD_RESPONSE_PACKET_OVERHEAD+TRANSACTION_SIZE) * 8) % RESPONSE_LINK_BUS_WIDTH);
if(LINK_BUS_USE_DDR)
{
totalChannelCycles = totalChannelCycles / 2 + !!(totalChannelCycles % 2);
}
//channel countdown
inFlightResponseLinkCountdowns[link] = totalChannelCycles / LINK_CPU_CLK_RATIO
+ !!(totalChannelCycles % LINK_CPU_CLK_RATIO);
//make sure computation worked
if(inFlightResponseLinkCountdowns[link]==0)
{
ERROR("== ERROR - Countdown 0 on link "<<link);
exit(0);
}
//make in-flight
if(inFlightResponseLink[link]!=NULL)
{
ERROR("== Error - Trying to set Transaction on down channel while something is there");
ERROR(" Cycle : "<<currentClockCycle);
ERROR(" Channel : "<<chan);
ERROR(" LinkBus : "<<link);
ERROR(" Incoming: "<<*pendingReads[p]);
ERROR(" Current : "<<*inFlightResponseLink[link]);
exit(0);
}
inFlightResponseLink[link] = pendingReads[p];
//note the time
inFlightResponseLink[link]->cyclesRspLink = currentClockCycle;
if(DEBUG_BOB)
{
DEBUG(" == Link Bus "<<link<<" returning "<<*inFlightResponseLink[link]);
DEBUG(" == CPU Clks:"<<inFlightResponseLinkCountdowns[link]<<" Channel Clks:"<<totalChannelCycles<<" DDR?:"<<LINK_BUS_USE_DDR);
}
//remove pending queues
pendingReads.erase(pendingReads.begin()+p);
//delete channels[chan]->readReturnQueue[0];
//channels[chan]->readReturnQueue.erase(channels[chan]->readReturnQueue.begin());
break;
}
}
//check to see if we cound an item, and break out of the loop over chans_per_link
if(inFlightResponseLinkCountdowns[link]>0)
{
//increment round robin (increment here to go to next index before we break)
responseLinkRoundRobin[link]++;
if(responseLinkRoundRobin[link]==CHANNELS_PER_LINK_BUS)
responseLinkRoundRobin[link]=0;
break;
}
}
//increment round robin (increment here if we found nothing)
responseLinkRoundRobin[link]++;
if(responseLinkRoundRobin[link]==CHANNELS_PER_LINK_BUS)
responseLinkRoundRobin[link]=0;
}
}
}
//
//Channels update at DRAM speeds (whatever ratio is with CPU)
//
if(currentClockCycle>0 && currentClockCycle%DRAM_CPU_CLK_RATIO==0)
{
if(DEBUG_CHANNEL) DEBUG(" --------- Channel updates started ---------");
//if there is an adjustment, we need to increment counter and handle clock differences
if(DRAM_CPU_CLK_ADJUSTMENT>0)
{
if(clockCycleAdjustmentCounter<DRAM_CPU_CLK_ADJUSTMENT)
{
for(unsigned i=0; i<NUM_CHANNELS; i++)
{
channels[i]->Update();
}
}
else clockCycleAdjustmentCounter=0;
clockCycleAdjustmentCounter++;
}
//if there is no adjustment, just update normally
else
{
for(unsigned i=0; i<NUM_CHANNELS; i++)
{
channels[i]->Update();
}
}
}
if(DEBUG_BOB) DEBUG(endl<<" --------- BOB Update Ended ["<<currentClockCycle<<"] ---------");
//increment clock cycle
currentClockCycle++;
}
unsigned BOB::FindChannelID(Transaction* trans)
{
unsigned channelID = 0;
unsigned bitWidth = log2(NUM_CHANNELS);
uint64_t channelMask = 0;
unsigned channelIDOffset;// = log2(BUS_ALIGNMENT_SIZE) + CHANNEL_ID_OFFSET;
switch(mappingScheme)
{
case BK_CLH_RW_RK_CH_CLL_BY://bank:col_high:row:rank:chan:col_low:by
channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
break;
case CLH_RW_RK_BK_CH_CLL_BY://col_high:row:rank:bank:chan:col_low:byte
channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
break;
case RK_BK_RW_CLH_CH_CLL_BY://rank:bank:row:col_high:chan:col_low:by
channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
break;
case RW_BK_RK_CH_CL_BY://row:bank:rank:chan:col:byte
channelIDOffset = colBitWidth + busOffsetBitWidth;
break;
case RW_CH_BK_RK_CL_BY://row:chan:bank:rank:col:byte
channelIDOffset = busOffsetBitWidth + colBitWidth + rankBitWidth + bankBitWidth;
break;
case RW_BK_RK_CLH_CH_CLL_BY://row:bank:rank:col_high:chan:col_low:byte
channelIDOffset = cacheOffset; //bus alignment + column low bits should make the cache offset
break;
case RW_CLH_BK_RK_CH_CLL_BY://row:col_high:bank:rank:chan:col_low:byte
channelIDOffset = cacheOffset;
break;
case CH_RW_BK_RK_CL_BY: //chan:row:bank:rank:col:byte
channelIDOffset = rowBitWidth + bankBitWidth + rankBitWidth + colBitWidth + busOffsetBitWidth;
break;
default:
ERROR("== ERROR - Unknown address mapping???");
exit(1);
break;
};
if(DEBUG_BOB) DEBUGN(" == Mapping "<<*trans);
//build channel id mask
for(unsigned i=0; i<bitWidth; i++)
{
channelMask = channelMask<<1;
channelMask++;
}
channelMask = channelMask << channelIDOffset;
channelID = (trans->address & channelMask) >> channelIDOffset;
if(DEBUG_BOB) DEBUG(" to channel "<<channelID);
return channelID;
}
//This is also kind of kludgey, but essentially this function always prints the power
// stats to the output file, but supresses the print to cout until finalPrint is true
void BOB::PrintStats(ofstream &statsOut, ofstream &powerOut, bool finalPrint, unsigned elapsedCycles)
{
unsigned long dramCyclesElapsed;
//check if we are on an epoch boundary or if there are cycles left over
//
//NOTE : subtract one from currentClockCycle since BOB object Update() has already been called
// before this call to PrintStats()
if((currentClockCycle-1)%EPOCH_LENGTH==0)
{
//OLD - doesn't work for non-even ratios
//dramCyclesElapsed = EPOCH_LENGTH / DRAM_CPU_CLK_RATIO;
//NEW
dramCyclesElapsed = EPOCH_LENGTH / ((float)tCK/(float)CPU_CLK_PERIOD);
}
else
{
//same as above
dramCyclesElapsed = ((currentClockCycle-1)%EPOCH_LENGTH)/((float)tCK/(float)CPU_CLK_PERIOD);
}
//dramCyclesElapsed = channels[0]->currentClockCycle;
#define MAX_TMP_STR 512
char tmp_str[MAX_TMP_STR];
PRINT(" === BOB Print === ");
unsigned long totalRequestsAtChannels = 0L;
PRINT(" == Ports");
for(unsigned p=0; p<NUM_PORTS; p++)
{
PRINT(" -- Port "<<p<<" - inputBufferAvg : "<<(float)portInputBufferAvg[p]/elapsedCycles<<" ("<<ports[p].inputBuffer.size()<<") outputBufferAvg : "<<(float)portOutputBufferAvg[p]/elapsedCycles<<" ("<<ports[p].outputBuffer.size()<<")");
portInputBufferAvg[p]=0;
portOutputBufferAvg[p]=0;
}
//calculate possible bandwidth of part
float dataperiod = tCK/2;
float bw = (1/dataperiod)*64/8;//64-bit bus, 8 bits/byte
unsigned numDevices = 64 / DEVICE_WIDTH;
unsigned long totalBytesPerDevice = (NUM_COLS * NUM_ROWS * NUM_BANKS * DEVICE_WIDTH) / 8L; //in bytes
unsigned long gigabytesPerRank = (numDevices * totalBytesPerDevice)>>30;
//calculate peak bandwidth for link bus
float reqbwpeak = 0;
float rspbwpeak = 0;
if(LINK_BUS_USE_DDR)
{
reqbwpeak = ((REQUEST_LINK_BUS_WIDTH * 2) / LINK_BUS_CLK_PERIOD)/8; //in bytes
rspbwpeak = ((RESPONSE_LINK_BUS_WIDTH * 2) / LINK_BUS_CLK_PERIOD)/8; //in bytes
}
else
{
reqbwpeak = (REQUEST_LINK_BUS_WIDTH / LINK_BUS_CLK_PERIOD) / 8; //in bytes
rspbwpeak = (RESPONSE_LINK_BUS_WIDTH / LINK_BUS_CLK_PERIOD) / 8; //in bytes
}
PRINT(" == Link Bandwidth (!! Includes packet overhead and request packets !!)")
PRINT(" Req Link("<<reqbwpeak<<" GB/s peak) Rsp Link("<<rspbwpeak<<" GB/s peak)");
double reqtotal = 0;
double rsptotal = 0;
for(int l=0; l<NUM_LINK_BUSES; l++)
{
reqtotal += (1-(float)requestLinkIdle[l]/(float)elapsedCycles) * reqbwpeak;
rsptotal += (1-(float)responseLinkIdle[l]/(float)elapsedCycles) * rspbwpeak;
PRINTN(" "<<(1-(float)requestLinkIdle[l]/(float)elapsedCycles) * reqbwpeak<<" GB/s ");
requestLinkIdle[l]=0;
PRINT((1-(float)responseLinkIdle[l]/(float)elapsedCycles) * rspbwpeak<<" GB/s");
responseLinkIdle[l]=0;
}
PRINT(" -----------");
PRINT(" "<<reqtotal/NUM_LINK_BUSES<<" "<<rsptotal/NUM_LINK_BUSES<<" (avgs)");
PRINT(" == Channel Usage and Stats ("<<(NUM_RANKS * gigabytesPerRank)<<"GB/Chan == "<<NUM_RANKS * gigabytesPerRank * NUM_CHANNELS<<" GB total)");
PRINT(" reqs workQAvg workQMax idleBanks actBanks preBanks refBanks (totalBanks) BusIdle BW("<<bw<<") RRQMax("<<CHANNEL_RETURN_Q_MAX/TRANSACTION_SIZE<<") RRQFull lifetimeRequests");
float totalDRAMbw = 0;
for(unsigned i=0; i<NUM_CHANNELS; i++)
{
//compute each DRAM channel's BW
double DRAMBandwidth= (bw * (1 - ((double)channels[i]->DRAMBusIdleCount/(double)dramCyclesElapsed))) * 1E9 / (1<<30);
statsOut<<DRAMBandwidth<<",";
totalDRAMbw += DRAMBandwidth;
totalRequestsAtChannels+=channelCounters[i];
channelCountersLifetime[i]+=channelCounters[i];
// since trying to actually format strings with stream operators is a huge pain
snprintf(tmp_str, MAX_TMP_STR, "%d]%9d%10.4f%10d%10.4f%10.4f%10.4f%10.4f%10.4f%10.2f%10.3f%10d(%d)%10d%10ld\n",
i,
channelCounters[i],
(float)channels[i]->simpleController.commandQueueAverage/dramCyclesElapsed,
channels[i]->simpleController.commandQueueMax,
(float)channels[i]->simpleController.numIdleBanksAverage/dramCyclesElapsed,
(float)channels[i]->simpleController.numActBanksAverage/dramCyclesElapsed,
(float)channels[i]->simpleController.numPreBanksAverage/dramCyclesElapsed,
(float)channels[i]->simpleController.numRefBanksAverage/dramCyclesElapsed,
(float)(channels[i]->simpleController.numIdleBanksAverage+
channels[i]->simpleController.numActBanksAverage+
channels[i]->simpleController.numPreBanksAverage+
channels[i]->simpleController.numRefBanksAverage)/dramCyclesElapsed,
100*((float)channels[i]->DRAMBusIdleCount/(float)dramCyclesElapsed),
DRAMBandwidth,
channels[i]->readReturnQueueMax,
(int)channels[i]->readReturnQueue.size(),
channels[i]->simpleController.RRQFull,
channelCountersLifetime[i]
);
for(int r=0; r<NUM_RANKS; r++)
{
for(int b=0; b<NUM_BANKS; b++)
{
//PRINTN(channels[i]->simpleController.bankStates[r][b]);
}
}
//reset
channels[i]->simpleController.commandQueueAverage=0;
channels[i]->simpleController.numIdleBanksAverage=0;
channels[i]->simpleController.numActBanksAverage=0;
channels[i]->simpleController.numPreBanksAverage=0;
channels[i]->simpleController.numRefBanksAverage=0;
channels[i]->simpleController.commandQueueMax=0;
channels[i]->readReturnQueueMax=0;
channels[i]->DRAMBusIdleCount=0;
channelCounters[i]=0;
channels[i]->simpleController.RRQFull=0;
cmdQFull[i]=0;
PRINTN(tmp_str);
}
//separates bandwidth numbers and power numbers
statsOut<<";";
PRINT(" AVG : "<<totalDRAMbw/NUM_CHANNELS);
PRINT(" == Requests seen at Channels");
PRINT(" -- Reads : "<<readCounter);
PRINT(" -- Writes : "<<writeCounter);
PRINT(" = "<<totalRequestsAtChannels);
readCounter = 0;
writeCounter = 0;
totalRequestsAtChannels = 0;
//
//
//POWER
//
//
PRINT(" == Channel Power");
powerOut<<currentClockCycle * CPU_CLK_PERIOD / 1000000<<",";
bool detailedOutput = false;
bool shortOutput = true;
float allChanAveragePower = 0;
for(unsigned c=0; c<NUM_CHANNELS; c++)
{
float totalChannelPower = 0;
PRINTN(" -- Channel "<<c);
for(unsigned r=0; r<NUM_RANKS; r++)
{
float backgroundPower = ((float)channels[c]->simpleController.backgroundEnergy[r] / (float) dramCyclesElapsed) * Vdd / 1000;