From 4b321cfe80f0bbf94f3648a9fc056ae2c19a3436 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 6 May 2021 09:04:35 +0200 Subject: Improve platform management and allow energy simulation --- src/inputs.cc | 50 +++++++++++++++++++++++++ src/inputs.hpp | 22 +++++++++++ src/simulator.cc | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 src/inputs.cc create mode 100644 src/inputs.hpp create mode 100644 src/simulator.cc (limited to 'src') diff --git a/src/inputs.cc b/src/inputs.cc new file mode 100644 index 0000000..066dcb0 --- /dev/null +++ b/src/inputs.cc @@ -0,0 +1,50 @@ +#include "inputs.hpp" +#include +#include + + +Inputs::Inputs(std::string node_name){ + FILE* input_file = fopen(INPUTS_FILE, "rb"); + char input_file_buffer[65536]; + rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); + d.ParseStream(is); + fclose(input_file); + + wake_duration=d[node_name.c_str()]["wake_duration"].GetDouble(); + wake_interval=d[node_name.c_str()]["wake_interval"].GetDouble(); + startup_delay=d[node_name.c_str()]["startup_delay"].GetDouble(); + is_sender=d[node_name.c_str()]["is_sender"].GetBool(); + max_attempts=d[node_name.c_str()]["max_attemps"].GetInt(); + +} + +void Inputs::GeneratePlatform(std::string p){ + FILE* input_file = fopen(INPUTS_FILE, "rb"); + char input_file_buffer[65536]; + rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); + rapidjson::Document d; + d.ParseStream(is); + fclose(input_file); + + // Write platform file + std::ofstream pf; + pf.open (p); + pf << "\n"; + pf << "\n"; + pf << "\n \n"; + pf << " \n"; + for (Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) + { + std::string name=itr->name.GetString(); + double power_on=d[itr->name.GetString()]["power_on"].GetDouble(); + double power_off=d[itr->name.GetString()]["power_off"].GetDouble(); + + //db=d[itr->name.GetString()]["wake_interval"].GetDouble(); + pf << " \n"; + pf << " \n"; + pf << " \n \n"; + pf << " \n"; + } + pf << " \n\n"; + pf.close(); +} \ No newline at end of file diff --git a/src/inputs.hpp b/src/inputs.hpp new file mode 100644 index 0000000..92b9005 --- /dev/null +++ b/src/inputs.hpp @@ -0,0 +1,22 @@ +#include "rapidjson/document.h" +#include "rapidjson/filereadstream.h" +#include +#include + +#define INPUTS_FILE "inputs.json" + +using namespace rapidjson; + +class Inputs { + Document d; + std::string node_name; +public: + Inputs(std::string node_name); + static void GeneratePlatform(std::string p); + + double wake_duration; + double wake_interval; + double startup_delay; + bool is_sender; + int max_attempts; +}; \ No newline at end of file diff --git a/src/simulator.cc b/src/simulator.cc new file mode 100644 index 0000000..b1461d5 --- /dev/null +++ b/src/simulator.cc @@ -0,0 +1,112 @@ +#include "simgrid/s4u.hpp" +#include +#include +#include +#include "inputs.hpp" +#include "simgrid/s4u/Host.hpp" +#include "simgrid/plugins/energy.h" +#include "xbt/log.h" + +#define PLATFORM_FILE "platform.xml" +#define TURN_OFF() simgrid::s4u::this_actor::get_host()->set_pstate(0); +#define TURN_ON() simgrid::s4u::this_actor::get_host()->set_pstate(1); + + +XBT_LOG_NEW_DEFAULT_CATEGORY(simulator, "[DAO]"); + +typedef unsigned int u32; +typedef struct{ + double hint; + bool isHint; +} Payload; + +/// @brief Observation unit code +static void obs_node(std::vector args); + + +int main(int argc, char **argv) { + + // Build engine + sg_host_energy_plugin_init(); + simgrid::s4u::Engine engine(&argc, argv); + Inputs::GeneratePlatform(PLATFORM_FILE); + engine.load_platform(PLATFORM_FILE); + + XBT_INFO("-------------------------------------------------"); + XBT_INFO("Sarting loosely coupled data dissemination experiments"); + XBT_INFO("-------------------------------------------------"); + + u32 nObsUnit=simgrid::s4u::Engine::get_instance()->get_host_count(); + for(u32 i=0;i args; + std::ostringstream ss; + ss<< "on" < args) { + std::string selfName = simgrid::s4u::this_actor::get_host()->get_name(); + simgrid::s4u::this_actor::get_host()->turn_on(); + Inputs i(selfName); + simgrid::s4u::Mailbox *m = simgrid::s4u::Mailbox::by_name("medium"); + XBT_INFO("Deploying observation node %s",selfName.c_str()); + + double wake_interval=i.wake_interval; + double wake_duration=i.wake_duration; + double startup_delay=i.startup_delay; + int max_attempts=i.max_attempts; + bool isSender=i.is_sender; + + // Starting node + u32 effectiveAttemps=0; + double effective_wake_duration=wake_duration; + double effective_wake_interval=wake_interval; + simgrid::s4u::this_actor::sleep_for(startup_delay); + Payload p; + for(u32 i=0;iput(&p,0,effective_wake_duration); + XBT_INFO("%s send data successfully",selfName.c_str()); + effective_wake_interval=wake_interval; + } + else { + Payload *p=m->get(effective_wake_duration); + if(p->isHint){ + XBT_INFO("%s received and hint of %f",selfName.c_str(),p->hint); + effective_wake_interval=p->hint; + } + else{ + XBT_INFO("%s received data successfully",selfName.c_str()); + } + } + } + catch (...) + { + if(isSender) + XBT_INFO("%s failed to send data",selfName.c_str()); + else + XBT_INFO("%s failed to receive data",selfName.c_str()); + } + effectiveAttemps++; + } + + // Done + XBT_INFO("Observation node %s finished (attemps:%d)",selfName.c_str(),effectiveAttemps); +} \ No newline at end of file -- cgit v1.2.3