/* * SimulationManager.cpp * * Created on: Apr 4, 2014 * Author: jonny */ #include "Simulation.h" #include "../common/TlmRecorder.h" #include "../common/DebugManager.h" #include "../common/xmlAddressdecoder.h" #include "../controller/core/ControllerCore.h" #include #include #include #include #include "../common/Utils.h" using namespace std; namespace simulation { Simulation::Simulation(sc_module_name /*name*/, string pathToResources, string traceName, DramSetup setup, std::vector devices) : traceName(traceName), dramSetup(setup) { SC_THREAD(stop); xmlAddressDecoder::addressConfigURI = pathToResources + string("configs/amconfigs/") + setup.addressmapping; Configuration::memconfigUri = pathToResources + string("configs/memconfigs/") + setup.memconfig; Configuration::memspecUri = pathToResources + string("configs/memspecs/") + setup.memspec; setupTlmRecorder(traceName, pathToResources, setup, devices); instantiateModules(pathToResources, devices); bindSockets(); setupDebugManager(traceName); calculateNumberOfTransaction(devices, pathToResources); } void Simulation::setupDebugManager(const string& traceName) { auto& dbg = DebugManager::getInstance(); dbg.addToWhiteList(controller->name()); dbg.addToWhiteList(player2->name()); dbg.addToWhiteList(player1->name()); dbg.addToWhiteList(this->name()); dbg.addToWhiteList(Scheduler::sendername); dbg.addToWhiteList(TlmRecorder::senderName); dbg.addToWhiteList(ControllerCore::senderName); dbg.addToWhiteList(PowerDownManagerBankwise::senderName); dbg.writeToConsole = true; dbg.writeToFile = true; if(dbg.writeToFile) dbg.openDebugFile(traceName + ".txt"); } void Simulation::setupTlmRecorder(const string &traceName, const string &pathToResources, const DramSetup &setup, const std::vector &devices) { if(Configuration::getInstance().databaseRecordingEnabled) { TlmRecorder::recordingEnabled = true; TlmRecorder::dbName = traceName; TlmRecorder::sqlScriptURI = pathToResources + string("scripts/createTraceDB.sql"); TlmRecorder::getInstance().recordMemconfig(setup.memconfig); TlmRecorder::getInstance().recordMemspec(setup.memspec); TlmRecorder::getInstance().recordTracenames(devices[0].trace + "," + devices[1].trace + "," + devices[2].trace + "," + devices[3].trace); } else { TlmRecorder::recordingEnabled = false; } } void Simulation::instantiateModules(const string &pathToResources, const std::vector& devices) { dram = new Dram<>("dram"); arbiter = new Arbiter("arbiter"); controller = new Controller<>("controller"); player1 = new TracePlayer<>("player1", pathToResources + string("traces/") + devices[0].trace, devices[0].burstLength, devices[0].clkMhz, this); player2 = new TracePlayer<>("player2", pathToResources + string("traces/") + devices[1].trace, devices[1].burstLength, devices[1].clkMhz, this); player3 = new TracePlayer<>("player3", pathToResources + string("traces/") + devices[2].trace, devices[2].burstLength, devices[2].clkMhz, this); player4 = new TracePlayer<>("player4", pathToResources + string("traces/") + devices[3].trace, devices[3].burstLength, devices[3].clkMhz, this); } void Simulation::bindSockets() { player1->iSocket.bind(arbiter->tSockets[0]); player2->iSocket.bind(arbiter->tSockets[1]); player3->iSocket.bind(arbiter->tSockets[2]); player4->iSocket.bind(arbiter->tSockets[3]); arbiter->iSocket.bind(controller->tSocket); controller->iSocket.bind(dram->tSocket); } void Simulation::calculateNumberOfTransaction(std::vector devices, string pathToResources) { totalTransactions = getNumberOfLines(pathToResources + string("traces/") + devices[0].trace); totalTransactions += getNumberOfLines(pathToResources + string("traces/") + devices[1].trace); totalTransactions += getNumberOfLines(pathToResources + string("traces/") + devices[2].trace); totalTransactions += getNumberOfLines(pathToResources + string("traces/") + devices[3].trace); remainingTransactions = totalTransactions; } Simulation::~Simulation() { delete dram; delete arbiter; delete controller; delete player1; delete player2; delete player3; delete player4; } void Simulation::start() { report("\n\nStarting simulation:"); report(headline); report(" -> setup: \t\t" + getFileName(traceName)); report(" -> memspec: \t\t" + Configuration::getInstance().MemoryId); report(" -> transactions: \t" + to_string(totalTransactions)); cout << endl; simulationStartTime = clock(); player1->start(); player2->start(); player3->start(); player4->start(); sc_set_stop_mode(SC_STOP_FINISH_DELTA); sc_start(); } void inline Simulation::transactionFinished() { remainingTransactions--; loadbar(totalTransactions - remainingTransactions, totalTransactions); if (remainingTransactions == 0) { cout << endl; terminateSimulation.notify(); } } void Simulation::stop() { wait(terminateSimulation); report("\nTerminating simulation"); controller->terminateSimulation(); wait(sc_time(200, SC_NS)); TlmRecorder::getInstance().closeConnection(); sc_stop(); double elapsed_secs = double(clock() - simulationStartTime) / CLOCKS_PER_SEC; report("Simulation took " + to_string(elapsed_secs) + " seconds"); } void Simulation::report(string message) { DebugManager::getInstance().printDebugMessage(this->name(), message); cout << message << endl; } unsigned int Simulation::getNumberOfLines(string uri) { std::ifstream file(uri); // count the newlines file.unsetf(std::ios_base::skipws); unsigned lineCount = std::count(std::istream_iterator(file), std::istream_iterator(), '\n'); return lineCount; } } /* namespace simulation */