-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMemorySystem.cpp
executable file
·530 lines (446 loc) · 14.1 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
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
/****************************************************************************
* DRAMSim2: A Cycle Accurate DRAM simulator
*
* Copyright (C) 2010 Elliott Cooper-Balis
* Paul Rosenfeld
* Bruce Jacob
* University of Maryland
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
//MemorySystem.cpp
//
//Class file for JEDEC memory system wrapper
//
#include "MemorySystem.h"
#include "IniReader.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sstream> //stringstream
#include <stdlib.h> // getenv()
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;
namespace DRAMSim {
#ifdef LOG_OUTPUT
ofstream dramsim_log;
#endif
powerCallBack_t MemorySystem::ReportPower = NULL;
MemorySystem::MemorySystem(uint id, string deviceIniFilename, string systemIniFilename, string pwd,
string traceFilename, unsigned int megsOfMemory) :
ReturnReadData(NULL),
WriteDataDone(NULL),
systemID(0),
deviceIniFilename(deviceIniFilename),
systemIniFilename(systemIniFilename),
traceFilename(traceFilename),
pwd(pwd)
{
currentClockCycle = 0;
//set ID
systemID = id;
DEBUG("===== MemorySystem "<<systemID<<" =====");
if (pwd.length() > 0)
{
//ignore the pwd argument if the argument is an absolute path
if (deviceIniFilename[0] != '/')
{
deviceIniFilename = pwd + "/" + deviceIniFilename;
}
if (systemIniFilename[0] != '/')
{
systemIniFilename = pwd + "/" + systemIniFilename;
}
}
DEBUG("== Loading device model file '"<<deviceIniFilename<<"' == ");
IniReader::ReadIniFile(deviceIniFilename, false);
DEBUG("== Loading system model file '"<<systemIniFilename<<"' == ");
IniReader::ReadIniFile(systemIniFilename, true);
//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;
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("TOTAL_STORAGE : "<< TOTAL_STORAGE << "MB | "<<NUM_RANKS<<" Ranks | "<< NUM_DEVICES <<" Devices per rank");
IniReader::InitEnumsFromStrings();
if (!IniReader::CheckIfAllSet())
{
exit(-1);
}
memoryController = new MemoryController(this, &visDataOut);
// TODO: change to other vector constructor?
ranks = new vector<Rank>();
for (size_t i=0; i<NUM_RANKS; i++)
{
Rank r = Rank();
r.setId(i);
r.attachMemoryController(memoryController);
ranks->push_back(r);
}
memoryController->attachRanks(ranks);
}
void MemorySystem::overrideSystemParam(string key, string value)
{
cerr << "Override key " <<key<<"="<<value<<endl;
IniReader::SetKey(key, value, true);
}
void MemorySystem::overrideSystemParam(string keyValuePair)
{
size_t equalsign=-1;
string overrideKey, overrideVal;
//FIXME: could use some error checks on the string
if ((equalsign = keyValuePair.find_first_of('=')) != string::npos) {
overrideKey = keyValuePair.substr(0,equalsign);
overrideVal = keyValuePair.substr(equalsign+1);
overrideSystemParam(overrideKey, overrideVal);
}
}
MemorySystem::~MemorySystem()
{
/* the MemorySystem should exist for all time, nothing should be destroying it */
// ERROR("MEMORY SYSTEM DESTRUCTOR with ID "<<systemID);
// abort();
delete(memoryController);
ranks->clear();
delete(ranks);
if (VIS_FILE_OUTPUT)
{
visDataOut.flush();
visDataOut.close();
}
if (VERIFICATION_OUTPUT)
{
cmd_verify_out.flush();
cmd_verify_out.close();
}
#ifdef LOG_OUTPUT
dramsim_log.flush();
dramsim_log.close();
#endif
}
bool fileExists(string path)
{
struct stat stat_buf;
if (stat(path.c_str(), &stat_buf) != 0)
{
if (errno == ENOENT)
{
return false;
}
}
return true;
}
string MemorySystem::SetOutputFileName(string traceFilename)
{
size_t lastSlash;
string deviceName, dramsimLogFilename;
size_t deviceIniFilenameLength = deviceIniFilename.length();
char *sim_description = NULL;
string sim_description_str;
sim_description = getenv("SIM_DESC");
#ifdef LOG_OUTPUT
dramsimLogFilename = "dramsim";
if (sim_description != NULL)
{
sim_description_str = string(sim_description);
dramsimLogFilename += "."+sim_description_str;
}
dramsimLogFilename += ".log";
dramsim_log.open(dramsimLogFilename.c_str(), ios_base::out | ios_base::trunc );
if (!dramsim_log)
{
ERROR("Cannot open "<< dramsimLogFilename);
exit(-1);
}
#endif
// create a properly named verification output file if need be
if (VERIFICATION_OUTPUT)
{
string basefilename = deviceIniFilename.substr(deviceIniFilename.find_last_of("/")+1);
string verify_filename = "sim_out_"+basefilename;
if (sim_description != NULL)
{
verify_filename += "."+sim_description_str;
}
verify_filename += ".tmp";
cmd_verify_out.open(verify_filename.c_str());
if (!cmd_verify_out)
{
ERROR("Cannot open "<< verify_filename);
exit(-1);
}
}
// TODO: move this to its own function or something?
if (VIS_FILE_OUTPUT)
{
// chop off the .ini if it's there
if (deviceIniFilename.substr(deviceIniFilenameLength-4) == ".ini")
{
deviceName = deviceIniFilename.substr(0,deviceIniFilenameLength-4);
deviceIniFilenameLength -= 4;
}
cout << deviceName << endl;
// chop off everything past the last / (i.e. leave filename only)
if ((lastSlash = deviceName.find_last_of("/")) != string::npos)
{
deviceName = deviceName.substr(lastSlash+1,deviceIniFilenameLength-lastSlash-1);
}
// working backwards, chop off the next piece of the directory
if ((lastSlash = traceFilename.find_last_of("/")) != string::npos)
{
traceFilename = traceFilename.substr(lastSlash+1,traceFilename.length()-lastSlash-1);
}
if (sim_description != NULL)
{
traceFilename += "."+sim_description_str;
}
string rest;
stringstream out,tmpNum,tmpSystemID;
string path = "results/";
string filename;
if (pwd.length() > 0)
{
path = pwd + "/" + path;
}
// create the directories if they don't exist
mkdirIfNotExist(path);
path = path + traceFilename + "/";
mkdirIfNotExist(path);
path = path + deviceName + "/";
mkdirIfNotExist(path);
// finally, figure out the filename
string sched = "BtR";
string queue = "pRank";
if (schedulingPolicy == RankThenBankRoundRobin)
{
sched = "RtB";
}
if (queuingStructure == PerRankPerBank)
{
queue = "pRankpBank";
}
/* I really don't see how "the C++ way" is better than snprintf() */
out << (TOTAL_STORAGE>>10) << "GB." << NUM_CHANS << "Ch." << NUM_RANKS <<"R." <<ADDRESS_MAPPING_SCHEME<<"."<<ROW_BUFFER_POLICY<<"."<< TRANS_QUEUE_DEPTH<<"TQ."<<CMD_QUEUE_DEPTH<<"CQ."<<sched<<"."<<queue;
if (sim_description)
{
out << "." << sim_description;
}
//filename so far, without .vis extension, see if it exists already
filename = out.str();
for (int i=0; i<100; i++)
{
if (fileExists(path+filename+tmpNum.str()+".vis"))
{
tmpNum.seekp(0);
tmpNum << "." << i;
}
else
{
filename = filename+tmpNum.str()+".vis";
break;
}
}
if (systemID!=0)
{
tmpSystemID<<"."<<systemID;
}
path.append(filename+tmpSystemID.str());
return path;
}
return string("");
}
void MemorySystem::mkdirIfNotExist(string path)
{
struct stat stat_buf;
// dwxr-xr-x on the results directories
if (stat(path.c_str(), &stat_buf) != 0)
{
if (errno == ENOENT)
{
DEBUG("\t directory doesn't exist, trying to create ...");
mode_t mode = (S_IXOTH | S_IXGRP | S_IXUSR | S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR) ;
if (mkdir(path.c_str(), mode) != 0)
{
perror("Error Has occurred while trying to make directory: ");
cerr << path << endl;
abort();
}
}
}
else
{
if (!S_ISDIR(stat_buf.st_mode))
{
ERROR(path << "is not a directory");
abort();
}
}
}
bool MemorySystem::WillAcceptTransaction()
{
return true;
// return memoryController->WillAcceptTransaction();
}
bool MemorySystem::addTransaction(bool isWrite, uint64_t addr, uint64_t txID)
{
TransactionType type = isWrite ? DATA_WRITE : DATA_READ;
Transaction trans(type,addr,NULL, txID);
// 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); // will be true
}
else
{
pendingTransactions.push_back(trans);
return true;
}
}
bool MemorySystem::addTransaction(Transaction &trans)
{
return memoryController->addTransaction(trans);
}
//prints statistics
void MemorySystem::printStats()
{
memoryController->printStats(true);
}
void MemorySystem::printStats(bool)
{
printStats();
}
//update the memory systems state
void MemorySystem::update()
{
if (currentClockCycle == 0)
{
string visOutputFilename = SetOutputFileName(traceFilename);
if (VIS_FILE_OUTPUT)
{
cerr << "writing vis file to " <<visOutputFilename<<endl;
visDataOut.open(visOutputFilename.c_str());
if (!visDataOut)
{
ERROR("Cannot open '"<<visOutputFilename<<"'");
exit(-1);
}
//write out the ini config values for the visualizer tool
IniReader::WriteValuesOut(visDataOut);
}
}
//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
}
#if (USE_SCIC_MEMSYS_QUERY == 1)
void MemorySystem::RegisterCallbacks( Callback_t* readCB, Callback_t* writeCB, CB_HIST* histgramCB,
void (*reportPower)(double bgpower, double burstpower,
double refreshpower, double actprepower))
{
ReturnReadData = readCB;
WriteDataDone = writeCB;
ReportPower = reportPower;
_callbackforHistogram = histgramCB;
}
#endif
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;
}
// static allocator for the library interface
MemorySystem *getMemorySystemInstance(uint id, string dev, string sys, string pwd, string trc, unsigned megsOfMemory)
{
return new MemorySystem(id, dev, sys, pwd, trc, megsOfMemory);
}
} /*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)
{
;
}
}