-
Notifications
You must be signed in to change notification settings - Fork 150
/
MemorySystem.cpp
264 lines (205 loc) · 8.19 KB
/
MemorySystem.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
/*********************************************************************************
* 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.
*********************************************************************************/
//MemorySystem.cpp
//
//Class file for JEDEC memory system wrapper
//
#include "MemorySystem.h"
#include "IniReader.h"
#include <unistd.h>
using namespace std;
ofstream cmd_verify_out; //used in Rank.cpp and MemoryController.cpp if VERIFICATION_OUTPUT is set
unsigned NUM_DEVICES;
unsigned NUM_RANKS;
unsigned NUM_RANKS_LOG;
namespace DRAMSim {
powerCallBack_t MemorySystem::ReportPower = NULL;
MemorySystem::MemorySystem(unsigned id, unsigned int megsOfMemory, CSVWriter &csvOut_, ostream &dramsim_log_) :
dramsim_log(dramsim_log_),
ReturnReadData(NULL),
WriteDataDone(NULL),
systemID(id),
csvOut(csvOut_)
{
currentClockCycle = 0;
DEBUG("===== MemorySystem "<<systemID<<" =====");
//calculate the total storage based on the devices the user selected and the number of
//calculate number of devices
/************************
This code has always been problematic even though it's pretty simple. I'll try to explain it
for my own sanity.
There are two main variables here that we could let the user choose:
NUM_RANKS or TOTAL_STORAGE. Since the density and width of the part is
fixed by the device ini file, the only variable that is really
controllable is the number of ranks. Users care more about choosing the
total amount of storage, but with a fixed device they might choose a total
storage that isn't possible. In that sense it's not as good to allow them
to choose TOTAL_STORAGE (because any NUM_RANKS value >1 will be valid).
However, users don't care (or know) about ranks, they care about total
storage, so maybe it's better to let them choose and just throw an error
if they choose something invalid.
A bit of background:
Each column contains DEVICE_WIDTH bits. A row contains NUM_COLS columns.
Each bank contains NUM_ROWS rows. Therefore, the total storage per DRAM device is:
PER_DEVICE_STORAGE = NUM_ROWS*NUM_COLS*DEVICE_WIDTH*NUM_BANKS (in bits)
A rank *must* have a 64 bit output bus (JEDEC standard), so each rank must have:
NUM_DEVICES_PER_RANK = 64/DEVICE_WIDTH
(note: if you have multiple channels ganged together, the bus width is
effectively NUM_CHANS * 64/DEVICE_WIDTH)
If we multiply these two numbers to get the storage per rank (in bits), we get:
PER_RANK_STORAGE = PER_DEVICE_STORAGE*NUM_DEVICES_PER_RANK = NUM_ROWS*NUM_COLS*NUM_BANKS*64
Finally, to get TOTAL_STORAGE, we need to multiply by NUM_RANKS
TOTAL_STORAGE = PER_RANK_STORAGE*NUM_RANKS (total storage in bits)
So one could compute this in reverse -- compute NUM_DEVICES,
PER_DEVICE_STORAGE, and PER_RANK_STORAGE first since all these parameters
are set by the device ini. Then, TOTAL_STORAGE/PER_RANK_STORAGE = NUM_RANKS
The only way this could run into problems is if TOTAL_STORAGE < PER_RANK_STORAGE,
which could happen for very dense parts.
*********************/
// number of bytes per rank
unsigned long megsOfStoragePerRank = ((((long long)NUM_ROWS * (NUM_COLS * DEVICE_WIDTH) * NUM_BANKS) * ((long long)JEDEC_DATA_BUS_BITS / DEVICE_WIDTH)) / 8) >> 20;
// If this is set, effectively override the number of ranks
if (megsOfMemory != 0)
{
NUM_RANKS = megsOfMemory / megsOfStoragePerRank;
NUM_RANKS_LOG = dramsim_log2(NUM_RANKS);
if (NUM_RANKS == 0)
{
PRINT("WARNING: Cannot create memory system with "<<megsOfMemory<<"MB, defaulting to minimum size of "<<megsOfStoragePerRank<<"MB");
NUM_RANKS=1;
}
}
NUM_DEVICES = JEDEC_DATA_BUS_BITS/DEVICE_WIDTH;
TOTAL_STORAGE = (NUM_RANKS * megsOfStoragePerRank);
DEBUG("CH. " <<systemID<<" TOTAL_STORAGE : "<< TOTAL_STORAGE << "MB | "<<NUM_RANKS<<" Ranks | "<< NUM_DEVICES <<" Devices per rank");
memoryController = new MemoryController(this, csvOut, dramsim_log);
// TODO: change to other vector constructor?
ranks = new vector<Rank *>();
for (size_t i=0; i<NUM_RANKS; i++)
{
Rank *r = new Rank(dramsim_log);
r->setId(i);
r->attachMemoryController(memoryController);
ranks->push_back(r);
}
memoryController->attachRanks(ranks);
}
MemorySystem::~MemorySystem()
{
/* the MemorySystem should exist for all time, nothing should be destroying it */
// ERROR("MEMORY SYSTEM DESTRUCTOR with ID "<<systemID);
// abort();
delete(memoryController);
for (size_t i=0; i<NUM_RANKS; i++)
{
delete (*ranks)[i];
}
ranks->clear();
delete(ranks);
if (VERIFICATION_OUTPUT)
{
cmd_verify_out.flush();
cmd_verify_out.close();
}
}
bool MemorySystem::WillAcceptTransaction()
{
return memoryController->WillAcceptTransaction();
}
bool MemorySystem::addTransaction(bool isWrite, uint64_t addr)
{
TransactionType type = isWrite ? DATA_WRITE : DATA_READ;
Transaction *trans = new Transaction(type,addr,NULL);
// push_back in memoryController will make a copy of this during
// addTransaction so it's kosher for the reference to be local
if (memoryController->WillAcceptTransaction())
{
return memoryController->addTransaction(trans);
}
else
{
pendingTransactions.push_back(trans);
return true;
}
}
bool MemorySystem::addTransaction(Transaction *trans)
{
return memoryController->addTransaction(trans);
}
//prints statistics
void MemorySystem::printStats(bool finalStats)
{
memoryController->printStats(finalStats);
}
//update the memory systems state
void MemorySystem::update()
{
//PRINT(" ----------------- Memory System Update ------------------");
//updates the state of each of the objects
// NOTE - do not change order
for (size_t i=0;i<NUM_RANKS;i++)
{
(*ranks)[i]->update();
}
//pendingTransactions will only have stuff in it if MARSS is adding stuff
if (pendingTransactions.size() > 0 && memoryController->WillAcceptTransaction())
{
memoryController->addTransaction(pendingTransactions.front());
pendingTransactions.pop_front();
}
memoryController->update();
//simply increments the currentClockCycle field for each object
for (size_t i=0;i<NUM_RANKS;i++)
{
(*ranks)[i]->step();
}
memoryController->step();
this->step();
//PRINT("\n"); // two new lines
}
void MemorySystem::RegisterCallbacks( Callback_t* readCB, Callback_t* writeCB,
void (*reportPower)(double bgpower, double burstpower,
double refreshpower, double actprepower))
{
ReturnReadData = readCB;
WriteDataDone = writeCB;
ReportPower = reportPower;
}
} /*namespace DRAMSim */
// This function can be used by autoconf AC_CHECK_LIB since
// apparently it can't detect C++ functions.
// Basically just an entry in the symbol table
extern "C"
{
void libdramsim_is_present(void)
{
;
}
}