Code refactoring.

This commit is contained in:
Lukas Steiner
2021-05-20 17:42:12 +02:00
parent 2256d03c58
commit c015a73e91
7 changed files with 84 additions and 92 deletions

View File

@@ -46,13 +46,12 @@
using namespace tlm;
TlmRecorder::TlmRecorder(std::string name, std::string dbname) :
dbName(dbname), name(name),
totalNumTransactions(1), simulationTimeCoveredByRecording(SC_ZERO_TIME)
TlmRecorder::TlmRecorder(const std::string &name, const std::string &dbName) :
name(name), totalNumTransactions(1), simulationTimeCoveredByRecording(SC_ZERO_TIME)
{
recordedData.reserve(transactionCommitRate);
setUpTransactionTerminatingPhases();
openDB(TlmRecorder::dbName.c_str());
openDB(dbName);
char *sErrMsg;
sqlite3_exec(db, "PRAGMA main.page_size = 4096", nullptr, nullptr, &sErrMsg);
sqlite3_exec(db, "PRAGMA main.cache_size=10000", nullptr, nullptr, &sErrMsg);
@@ -110,7 +109,7 @@ void TlmRecorder::recordBandwidth(double timeInSeconds, double averageBandwidth)
}
void TlmRecorder::recordPhase(tlm_generic_payload &trans,
tlm_phase phase, sc_time time)
tlm_phase phase, const sc_time &time)
{
if (currentTransactionsInSystem.count(&trans) == 0)
introduceTransactionSystem(trans);
@@ -147,7 +146,7 @@ void TlmRecorder::updateDataStrobe(const sc_time &begin, const sc_time &end,
}
void TlmRecorder::recordDebugMessage(std::string message, sc_time time)
void TlmRecorder::recordDebugMessage(const std::string &message, const sc_time &time)
{
insertDebugMessageInDB(message, time);
}
@@ -194,7 +193,7 @@ void TlmRecorder::commitRecordedDataToDB()
sqlite3_exec(db, "BEGIN;", nullptr, nullptr, nullptr);
for (Transaction &recordingData : recordedData)
{
assert(recordingData.recordedPhases.size() > 0);
assert(!recordingData.recordedPhases.empty());
insertTransactionInDB(recordingData);
for (Transaction::Phase &phaseData : recordingData.recordedPhases)
{
@@ -216,20 +215,20 @@ void TlmRecorder::commitRecordedDataToDB()
}
void TlmRecorder::Transaction::insertPhase(std::string name, sc_time begin)
void TlmRecorder::Transaction::insertPhase(const std::string &phaseName, const sc_time &begin)
{
recordedPhases.push_back(Phase(name, begin));
recordedPhases.emplace_back(phaseName, begin);
}
void TlmRecorder::Transaction::setPhaseEnd(std::string name, sc_time end)
void TlmRecorder::Transaction::setPhaseEnd(const std::string &phaseName, const sc_time &end)
{
// Find the latest recorder phase for that transaction with a matching name and update it
// Find the latest recorder phase for that transaction with a matching phaseName and update it
// Note: Transactions might have the same phase multiple times (e.g. PRE->ACT->REF->ACT->RD)
// only update the latest one that has been recorded
for (size_t i = recordedPhases.size(); i > 0; i--)
{
Phase &data = recordedPhases[i - 1];
if (data.name == name)
if (data.name == phaseName)
{
data.interval.end = end;
return;
@@ -239,18 +238,18 @@ void TlmRecorder::Transaction::setPhaseEnd(std::string name, sc_time end)
"While trying to set phase end: phaseBegin has not been recorded");
}
void TlmRecorder::openDB(std::string name)
void TlmRecorder::openDB(const std::string &dbName)
{
std::ifstream f(name.c_str());
std::ifstream f(dbName.c_str());
if (f.good())
{
if (remove(name.c_str()) != 0)
if (remove(dbName.c_str()) != 0)
{
SC_REPORT_FATAL("TlmRecorder", "Error deleting file" );
}
}
if (sqlite3_open(name.c_str(), &db) != SQLITE_OK)
if (sqlite3_open(dbName.c_str(), &db) != SQLITE_OK)
{
SC_REPORT_FATAL("Error in TraceRecorder", "Error cannot open database");
sqlite3_close(db);
@@ -262,24 +261,18 @@ void TlmRecorder::setUpTransactionTerminatingPhases()
transactionTerminatingPhases.push_back(END_RESP);
// Refresh All
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_REFA));
transactionTerminatingPhases.push_back(END_REFA);
// Refresh Bank
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_REFB));
transactionTerminatingPhases.push_back(END_REFB);
// Refresh Same Bank
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_REFSB));
transactionTerminatingPhases.push_back(END_REFSB);
// Phases for Power Down
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_PDNA));
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_PDNP));
transactionTerminatingPhases.push_back(static_cast<const tlm_phase>
(END_SREF));
transactionTerminatingPhases.push_back(END_PDNA);
transactionTerminatingPhases.push_back(END_PDNP);
transactionTerminatingPhases.push_back(END_SREF);
}
void TlmRecorder::prepareSqlStatements()
@@ -331,7 +324,7 @@ void TlmRecorder::prepareSqlStatements()
sqlite3_prepare_v2(db, insertBandwidthString.c_str(), -1, &insertBandwidthStatement, nullptr);
}
void TlmRecorder::insertDebugMessageInDB(std::string message, const sc_time &time)
void TlmRecorder::insertDebugMessageInDB(const std::string &message, const sc_time &time)
{
sqlite3_bind_int64(insertDebugMessageStatement, 1, static_cast<int64_t>(time.value()));
sqlite3_bind_text(insertDebugMessageStatement, 2, message.c_str(), static_cast<int>(message.length()), nullptr);
@@ -379,24 +372,24 @@ void TlmRecorder::insertCommandLengths()
{
const MemSpec *memSpec = Configuration::getInstance().memSpec;
sqlite3_bind_int(insertCommandLengthsStatement, 1, static_cast<int>(memSpec->getCommandLength(Command::NOP) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 2, static_cast<int>(memSpec->getCommandLength(Command::RD) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 3, static_cast<int>(memSpec->getCommandLength(Command::WR) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 4, static_cast<int>(memSpec->getCommandLength(Command::RDA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 5, static_cast<int>(memSpec->getCommandLength(Command::WRA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 6, static_cast<int>(memSpec->getCommandLength(Command::ACT) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 7, static_cast<int>(memSpec->getCommandLength(Command::PRE) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 8, static_cast<int>(memSpec->getCommandLength(Command::REFB) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 9, static_cast<int>(memSpec->getCommandLength(Command::PRESB) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 10, static_cast<int>(memSpec->getCommandLength(Command::REFSB) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 11, static_cast<int>(memSpec->getCommandLength(Command::PREA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 12, static_cast<int>(memSpec->getCommandLength(Command::REFA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 13, static_cast<int>(memSpec->getCommandLength(Command::PDEA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 14, static_cast<int>(memSpec->getCommandLength(Command::PDXA) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 15, static_cast<int>(memSpec->getCommandLength(Command::PDEP) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 16, static_cast<int>(memSpec->getCommandLength(Command::PDXP) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 17, static_cast<int>(memSpec->getCommandLength(Command::SREFEN) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 18, static_cast<int>(memSpec->getCommandLength(Command::SREFEX) / memSpec->tCK + 0.5));
sqlite3_bind_int(insertCommandLengthsStatement, 1, static_cast<int>(lround(memSpec->getCommandLength(Command::NOP) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 2, static_cast<int>(lround(memSpec->getCommandLength(Command::RD) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 3, static_cast<int>(lround(memSpec->getCommandLength(Command::WR) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 4, static_cast<int>(lround(memSpec->getCommandLength(Command::RDA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 5, static_cast<int>(lround(memSpec->getCommandLength(Command::WRA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 6, static_cast<int>(lround(memSpec->getCommandLength(Command::ACT) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 7, static_cast<int>(lround(memSpec->getCommandLength(Command::PRE) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 8, static_cast<int>(lround(memSpec->getCommandLength(Command::REFB) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 9, static_cast<int>(lround(memSpec->getCommandLength(Command::PRESB) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 10, static_cast<int>(lround(memSpec->getCommandLength(Command::REFSB) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 11, static_cast<int>(lround(memSpec->getCommandLength(Command::PREA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 12, static_cast<int>(lround(memSpec->getCommandLength(Command::REFA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 13, static_cast<int>(lround(memSpec->getCommandLength(Command::PDEA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 14, static_cast<int>(lround(memSpec->getCommandLength(Command::PDXA) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 15, static_cast<int>(lround(memSpec->getCommandLength(Command::PDEP) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 16, static_cast<int>(lround(memSpec->getCommandLength(Command::PDXP) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 17, static_cast<int>(lround(memSpec->getCommandLength(Command::SREFEN) / memSpec->tCK)));
sqlite3_bind_int(insertCommandLengthsStatement, 18, static_cast<int>(lround(memSpec->getCommandLength(Command::SREFEX) / memSpec->tCK)));
executeSqlStatement(insertCommandLengthsStatement);
}
@@ -442,7 +435,7 @@ void TlmRecorder::insertRangeInDB(uint64_t id, const sc_time &begin,
executeSqlStatement(insertRangeStatement);
}
void TlmRecorder::insertPhaseInDB(std::string phaseName, const sc_time &begin,
void TlmRecorder::insertPhaseInDB(const std::string &phaseName, const sc_time &begin,
const sc_time &end,
uint64_t transactionID)
{

View File

@@ -56,30 +56,29 @@ class TlmRecorder
{
public:
std::string sqlScriptURI;
std::string dbName;
TlmRecorder(std::string name, std::string dbname);
TlmRecorder(const std::string &name, const std::string &dbName);
~TlmRecorder();
void recordMCconfig(std::string mcconfig)
void recordMcConfig(std::string _mcconfig)
{
this->mcconfig = mcconfig;
mcconfig = _mcconfig;
}
void recordMemspec(std::string memspec)
void recordMemspec(std::string _memspec)
{
this->memspec = memspec;
memspec = _memspec;
}
void recordTracenames(std::string traces)
void recordTraceNames(std::string _traces)
{
this->traces = traces;
traces = _traces;
}
void recordPhase(tlm::tlm_generic_payload &trans, tlm::tlm_phase phase,
sc_time time);
const sc_time &time);
void recordPower(double timeInSeconds, double averagePower);
void recordBufferDepth(double timeInSeconds, const std::vector<double> &averageBufferDepth);
void recordBandwidth(double timeInSeconds, double averageBandwidth);
void recordDebugMessage(std::string message, sc_time time);
void recordDebugMessage(const std::string &message, const sc_time &time);
void updateDataStrobe(const sc_time &begin, const sc_time &end,
tlm::tlm_generic_payload &trans);
void closeConnection();
@@ -106,8 +105,8 @@ private:
};
std::vector<Phase> recordedPhases;
void insertPhase(std::string name, sc_time begin);
void setPhaseEnd(std::string name, sc_time end);
void insertPhase(const std::string &phaseName, const sc_time &begin);
void setPhaseEnd(const std::string &phaseName, const sc_time &end);
};
std::string name;
@@ -116,9 +115,9 @@ private:
void prepareSqlStatements();
void executeInitialSqlCommand();
void executeSqlStatement(sqlite3_stmt *statement);
static void executeSqlStatement(sqlite3_stmt *statement);
void openDB(std::string name);
void openDB(const std::string &dbName);
void setUpTransactionTerminatingPhases();
void introduceTransactionSystem(tlm::tlm_generic_payload &trans);
@@ -129,9 +128,9 @@ private:
void insertCommandLengths();
void insertTransactionInDB(Transaction &recordingData);
void insertRangeInDB(uint64_t id, const sc_time &begin, const sc_time &end);
void insertPhaseInDB(std::string phaseName, const sc_time &begin, const sc_time &end,
void insertPhaseInDB(const std::string &phaseName, const sc_time &begin, const sc_time &end,
uint64_t transactionID);
void insertDebugMessageInDB(std::string message, const sc_time &time);
void insertDebugMessageInDB(const std::string &message, const sc_time &time);
static const int transactionCommitRate = 1000;
std::vector<Transaction> recordedData;

View File

@@ -46,18 +46,18 @@
using namespace tlm;
using json = nlohmann::json;
bool TimeInterval::timeIsInInterval(sc_time time)
bool TimeInterval::timeIsInInterval(const sc_time &time) const
{
return (start < time && time < end);
}
bool TimeInterval::intersects(TimeInterval other)
bool TimeInterval::intersects(const TimeInterval &other) const
{
return other.timeIsInInterval(this->start)
|| this->timeIsInInterval(other.start);
}
sc_time TimeInterval::getLength()
sc_time TimeInterval::getLength() const
{
if (end > start)
return end - start;
@@ -72,7 +72,7 @@ std::string getPhaseName(tlm_phase phase)
return oss.str();
}
json parseJSON(std::string path)
json parseJSON(const std::string &path)
{
std::ifstream file(path);
if (file.is_open())
@@ -87,7 +87,7 @@ json parseJSON(std::string path)
throw std::invalid_argument("Failed to open file '" + path + "'.");
}
bool parseBool(json &obj, std::string name)
bool parseBool(json &obj, const std::string &name)
{
if (!obj.empty())
{
@@ -100,7 +100,7 @@ bool parseBool(json &obj, std::string name)
throw std::invalid_argument("Parameter '" + name + "' does not exist.");
}
unsigned int parseUint(json &obj, std::string name)
unsigned int parseUint(json &obj, const std::string &name)
{
if (!obj.empty())
{
@@ -113,7 +113,7 @@ unsigned int parseUint(json &obj, std::string name)
throw std::invalid_argument("Parameter '" + name + "' does not exist.");
}
double parseUdouble(json &obj, std::string name)
double parseUdouble(json &obj, const std::string &name)
{
if (!obj.empty())
{
@@ -126,7 +126,7 @@ double parseUdouble(json &obj, std::string name)
throw std::invalid_argument("Parameter '" + name + "' does not exist.");
}
std::string parseString(json &obj, std::string name)
std::string parseString(json &obj, const std::string &name)
{
if (!obj.empty())
{

View File

@@ -57,9 +57,9 @@ public:
TimeInterval() : start(SC_ZERO_TIME), end(SC_ZERO_TIME) {}
TimeInterval(sc_time start, sc_time end) : start(start), end(end) {}
sc_time getLength();
bool timeIsInInterval(sc_time time);
bool intersects(TimeInterval other);
sc_time getLength() const;
bool timeIsInInterval(const sc_time &time) const;
bool intersects(const TimeInterval &other) const;
};
constexpr const char headline[] =
@@ -67,11 +67,11 @@ constexpr const char headline[] =
std::string getPhaseName(tlm::tlm_phase phase);
nlohmann::json parseJSON(std::string path);
bool parseBool(nlohmann::json &obj, std::string name);
unsigned int parseUint(nlohmann::json &obj, std::string name);
double parseUdouble(nlohmann::json &obj, std::string name);
std::string parseString(nlohmann::json &obj, std::string name);
nlohmann::json parseJSON(const std::string &path);
bool parseBool(nlohmann::json &obj, const std::string &name);
unsigned int parseUint(nlohmann::json &obj, const std::string &name);
double parseUdouble(nlohmann::json &obj, const std::string &name);
std::string parseString(nlohmann::json &obj, const std::string &name);
void setUpDummy(tlm::tlm_generic_payload &payload, uint64_t channelPayloadID,
Rank rank = Rank(0), BankGroup bankGroup = BankGroup(0), Bank bank = Bank(0));

View File

@@ -55,10 +55,10 @@
using json = nlohmann::json;
std::string Configuration::memspecUri = "";
std::string Configuration::mcconfigUri = "";
std::string Configuration::memspecUri;
std::string Configuration::mcconfigUri;
enum sc_time_unit string2TimeUnit(std::string s)
enum sc_time_unit string2TimeUnit(const std::string &s)
{
if (s == "s")
return SC_SEC;
@@ -79,7 +79,7 @@ enum sc_time_unit string2TimeUnit(std::string s)
}
}
void Configuration::setParameter(std::string name, nlohmann::json value)
void Configuration::setParameter(const std::string &name, const nlohmann::json &value)
{
// MCConfig
if (name == "PagePolicy")
@@ -283,14 +283,14 @@ void Configuration::setParameter(std::string name, nlohmann::json value)
("Parameter " + name + " not defined in Configuration").c_str());
}
void Configuration::setPathToResources(std::string path)
void Configuration::setPathToResources(const std::string &path)
{
pathToResources = path;
temperatureSim.setPathToResources(path);
}
// Changes the number of bytes depeding on the ECC Controller. This function is needed for modules which get data directly or indirectly from the ECC Controller
unsigned int Configuration::adjustNumBytesAfterECC(unsigned nBytes)
unsigned int Configuration::adjustNumBytesAfterECC(unsigned nBytes) const
{
// Manipulate the number of bytes only if there is an ECC Controller selected
if (eccMode == ECCMode::Disabled)

View File

@@ -61,7 +61,7 @@ public:
private:
Configuration() {}
Configuration(const Configuration &);
Configuration & operator = (const Configuration &);
Configuration &operator = (const Configuration &);
public:
static std::string memspecUri;
@@ -107,7 +107,7 @@ public:
// MemSpec (from DRAM-Power)
const MemSpec *memSpec;
void setParameter(std::string name, nlohmann::json value);
void setParameter(const std::string &name, const nlohmann::json &value);
//Configs for Seed, csv file and StorageMode
unsigned int errorChipSeed;
@@ -117,8 +117,8 @@ public:
// Temperature Simulation related
TemperatureSimConfig temperatureSim;
unsigned int adjustNumBytesAfterECC(unsigned bytes);
void setPathToResources(std::string path);
unsigned int adjustNumBytesAfterECC(unsigned bytes) const;
void setPathToResources(const std::string &path);
void loadMCConfig(Configuration &config, std::string amconfigUri);
void loadSimConfig(Configuration &config, std::string simconfigUri);

View File

@@ -100,11 +100,11 @@ void DRAMSysRecordable::setupTlmRecorders(const std::string &traceName)
TlmRecorder *tlmRecorder =
new TlmRecorder(recorderName, dbName.c_str());
tlmRecorder->recordMCconfig(Configuration::getInstance().mcconfigUri);
tlmRecorder->recordMcConfig(Configuration::getInstance().mcconfigUri);
tlmRecorder->recordMemspec(Configuration::getInstance().memspecUri);
std::string traceNames = Configuration::getInstance().simulationName;
tlmRecorder->recordTracenames(traceNames);
tlmRecorder->recordTraceNames(traceNames);
tlmRecorders.push_back(tlmRecorder);
}