Skip to content

Commit

Permalink
Fixed up CSVWriter warnings/memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dramninjasUMD committed Apr 9, 2012
1 parent 2408e29 commit 0b3ee67
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
96 changes: 56 additions & 40 deletions CSVWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>

#include <string.h>

using std::vector;
using std::ostream;
using std::string;
/*
* CSVWriter: Writes CSV data with headers to an underlying ofstream
* This wrapper is meant to look like an ofstream, but it captures
Expand Down Expand Up @@ -75,46 +78,51 @@ using std::ostream;


namespace DRAMSim {
// Single index
static char *indexStr(const char *baseName, unsigned channel)
{
if (strlen(baseName)+SINGLE_INDEX_LEN > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
abort();
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u]", baseName, channel);
return strndup(tmp_str, MAX_TMP_STR);
}

// two indices
static char *indexStr(const char *baseName, unsigned channel, unsigned rank)
{
if (strlen(baseName)+(2*SINGLE_INDEX_LEN) > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
abort();
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u][%u]", baseName, channel, rank);
return strndup(tmp_str, MAX_TMP_STR);
}

// three indices
static char *indexStr(const char *baseName, unsigned channel, unsigned rank, unsigned bank)
{
if (strlen(baseName)+(3*SINGLE_INDEX_LEN) > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
exit(-1);
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u][%u][%u]", baseName, channel, rank, bank);
return strndup(tmp_str, MAX_TMP_STR);
}

class CSVWriter {
public :
struct IndexedName {
char *str;
IndexedName(const char *baseName, unsigned channel)
{
if (strlen(baseName)+(1*SINGLE_INDEX_LEN) > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
exit(-1);
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u]", baseName, channel);
str = strndup(tmp_str, MAX_TMP_STR);
}
IndexedName(const char *baseName, unsigned channel, unsigned rank)
{
if (strlen(baseName)+(2*SINGLE_INDEX_LEN) > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
exit(-1);
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u][%u]", baseName, channel, rank);
str = strndup(tmp_str, MAX_TMP_STR);
}
IndexedName(const char *baseName, unsigned channel, unsigned rank, unsigned bank)
{
if (strlen(baseName)+(3*SINGLE_INDEX_LEN) > MAX_TMP_STR)
{
ERROR("Your string is too long for the stats, increase MAX_TMP_STR");
exit(-1);
}
char tmp_str[MAX_TMP_STR];
snprintf(tmp_str, MAX_TMP_STR,"%s[%u][%u][%u]", baseName, channel, rank, bank);
str = strndup(tmp_str, MAX_TMP_STR);
}

virtual ~IndexedName()
{
delete str;
}

};
// where the output will eventually go
ostream &output;
vector<string> fieldNames;
Expand Down Expand Up @@ -168,8 +176,17 @@ namespace DRAMSim {
}
return *this;
}

CSVWriter &operator<<(const IndexedName &indexedName)
{
if (!finalized)
{
fieldNames.push_back(string(indexedName.str));
}
return *this;
}

// Insertion operators for types
// Insertion operators for value types
// All of the other types just need to pass through to the underlying
// ofstream, so just write this small wrapper function to make the
// whole thing less verbose
Expand All @@ -190,7 +207,6 @@ namespace DRAMSim {
ADD_TYPE(uint64_t);
ADD_TYPE(float);
ADD_TYPE(double);

}; // class CSVWriter


Expand Down
21 changes: 11 additions & 10 deletions MemoryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,29 +863,30 @@ void MemoryController::printStats(bool finalStats)
PRINT( " -Act/Pre (watts) : " << actprePower[r] );
PRINT( " -Burst (watts) : " << burstPower[r]);
PRINT( " -Refresh (watts) : " << refreshPower[r] );

if (VIS_FILE_OUTPUT)
{
// write the vis file output
csvOut << indexStr("Background_Power",myChannel,r) <<backgroundPower[r];
csvOut << indexStr("ACT_PRE_Power",myChannel,r) << actprePower[r];
csvOut << indexStr("Burst_Power",myChannel,r) << burstPower[r];
csvOut << indexStr("Refresh_Power",myChannel,r) << refreshPower[r];
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 << indexStr("Bandwidth",myChannel,r,b) << bandwidth[SEQUENTIAL(r,b)];
csvOut << CSVWriter::IndexedName("Bandwidth",myChannel,r,b) << bandwidth[SEQUENTIAL(r,b)];
totalRankBandwidth += bandwidth[SEQUENTIAL(r,b)];
totalAggregateBandwidth += bandwidth[SEQUENTIAL(r,b)];
csvOut << indexStr("Average_Latency",myChannel,r,b) << averageLatency[SEQUENTIAL(r,b)];
csvOut << CSVWriter::IndexedName("Average_Latency",myChannel,r,b) << averageLatency[SEQUENTIAL(r,b)];
}
csvOut << indexStr("Rank_Aggregate_Bandwidth",myChannel,r) << totalRankBandwidth;
csvOut << indexStr("Rank_Average_Bandwidth",myChannel,r) << totalRankBandwidth/NUM_RANKS;
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 << indexStr("Aggregate_Bandwidth",myChannel) << totalAggregateBandwidth;
csvOut << indexStr("Average_Bandwidth",myChannel) << totalAggregateBandwidth / (NUM_RANKS*NUM_BANKS);
csvOut << CSVWriter::IndexedName("Aggregate_Bandwidth",myChannel) << totalAggregateBandwidth;
csvOut << CSVWriter::IndexedName("Average_Bandwidth",myChannel) << totalAggregateBandwidth / (NUM_RANKS*NUM_BANKS);
csvOut.finalize();
}

Expand Down
8 changes: 2 additions & 6 deletions MemorySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ powerCallBack_t MemorySystem::ReportPower = NULL;
MemorySystem::MemorySystem(unsigned id, unsigned int megsOfMemory, ofstream &visDataOut_) :
ReturnReadData(NULL),
WriteDataDone(NULL),
visDataOut(visDataOut_),
systemID(id)
systemID(id),
visDataOut(visDataOut_)
{
currentClockCycle = 0;

Expand Down Expand Up @@ -167,10 +167,6 @@ MemorySystem::~MemorySystem()
}
}





bool MemorySystem::WillAcceptTransaction()
{
return memoryController->WillAcceptTransaction();
Expand Down
2 changes: 1 addition & 1 deletion MemorySystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class MemorySystem : public SimulatorObject
Callback_t* WriteDataDone;
//TODO: make this a functor as well?
static powerCallBack_t ReportPower;

unsigned systemID;

private:
ofstream &visDataOut;
};
Expand Down

0 comments on commit 0b3ee67

Please sign in to comment.