loosely-coupled-dss/src/simulator.cc

139 lines
4.5 KiB
C++
Raw Normal View History

#include "simgrid/s4u.hpp"
#include <simgrid/s4u/Mailbox.hpp>
#include <string>
#include <sstream>
#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);
2021-05-06 16:25:14 +02:00
/// @brief Required by SimGrid
XBT_LOG_NEW_DEFAULT_CATEGORY(simulator, "[DAO]");
2021-05-06 16:25:14 +02:00
/// @brief For convenience
typedef unsigned int u32;
2021-05-06 16:25:14 +02:00
/**
* Data that will be exchange between the nodes
*/
2021-05-06 11:57:50 +02:00
class Payload{
2021-05-06 16:25:14 +02:00
public:
2021-05-06 16:14:57 +02:00
Payload():hint(0),duration(0),containsHint(false){}
double hint;
2021-05-06 16:14:57 +02:00
double duration;
2021-05-06 11:57:50 +02:00
bool containsHint;
};
2021-05-06 16:25:14 +02:00
/// @brief Observation node code
static void obs_node(std::vector<std::string> args);
2021-05-06 16:25:14 +02:00
/**
* No arguments are require (cf inputs.json)
*/
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);
2021-05-06 16:25:14 +02:00
// Headline
XBT_INFO("-------------------------------------------------");
XBT_INFO("Sarting loosely coupled data dissemination experiments");
XBT_INFO("-------------------------------------------------");
2021-05-06 16:25:14 +02:00
// Init all nodes actors
2021-05-06 11:57:50 +02:00
u32 nON=simgrid::s4u::Engine::get_instance()->get_host_count();
for(u32 i=0;i<nON;i++){
std::vector<std::string> args;
std::ostringstream ss;
ss<< "on" <<i;
simgrid::s4u::Actor::create("ON", simgrid::s4u::Host::by_name(ss.str()), obs_node, args);
}
// Setup/Run simulation
engine.run();
XBT_INFO("Simulation took %fs", simgrid::s4u::Engine::get_clock());
2021-05-06 16:25:14 +02:00
XBT_INFO("The simulated platform file is available in \"%s\"",PLATFORM_FILE);
return (0);
}
2021-05-06 16:25:14 +02:00
/**
* This is the brain behind each node
*/
static void obs_node(std::vector<std::string> args) {
2021-05-06 16:25:14 +02:00
// Init various variables
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());
2021-05-06 16:25:14 +02:00
// Init convenient variables
bool isSender=i.is_sender;
2021-05-06 11:57:50 +02:00
bool useHint=i.use_hint;
bool isObserver=false;
u32 data_size=i.data_size;
// Starting node
2021-05-06 16:14:57 +02:00
u32 nWakeUp=0;
u32 nDataRcv=0;
while(i.ShouldContinue()){
XBT_INFO("%s is spleeping",selfName.c_str());
2021-05-06 11:57:50 +02:00
TURN_OFF();
2021-05-06 16:14:57 +02:00
simgrid::s4u::this_actor::sleep_until(i.GetTS());
2021-05-06 11:57:50 +02:00
TURN_ON();
XBT_INFO("%s wakes up",selfName.c_str());
2021-05-06 16:14:57 +02:00
// Doing wake up stuff
try {
if(isSender){ // If I am a sender
2021-05-06 11:57:50 +02:00
Payload *p=new Payload();
2021-05-06 16:14:57 +02:00
if(useHint&&i.HasNext()){
2021-05-06 16:32:06 +02:00
p->containsHint=true;
2021-05-06 16:14:57 +02:00
p->hint=i.GetNextTS();
p->duration=i.GetNextDuration();
2021-05-06 11:57:50 +02:00
}
2021-05-06 16:14:57 +02:00
m->put(p,data_size,i.GetDuration());
XBT_INFO("%s send data successfully",selfName.c_str());
2021-05-06 11:57:50 +02:00
isObserver=true; // Do one send for now...
isSender=false;
}
2021-05-06 11:57:50 +02:00
else if (!isObserver){
2021-05-06 16:14:57 +02:00
Payload* p=m->get<Payload>(i.GetDuration());
2021-05-06 16:25:14 +02:00
nDataRcv++; // New data received
2021-05-06 11:57:50 +02:00
if(p->containsHint){
XBT_INFO("%s received and hint of %f",selfName.c_str(),p->hint);
2021-05-06 16:25:14 +02:00
i.AddEvent(p->hint, p->duration); // Schedule a new wake up time
}
else{
2021-05-06 11:57:50 +02:00
XBT_INFO("%s received data successfully and switch to forwarding mode",selfName.c_str());
2021-05-06 16:25:14 +02:00
isSender=!isSender; // Toggle isSender to start sending
}
}
2021-05-06 11:57:50 +02:00
else {
XBT_INFO("%s is observing is environment...",selfName.c_str());
2021-05-06 16:14:57 +02:00
simgrid::s4u::this_actor::sleep_until(i.GetDuration());
2021-05-06 11:57:50 +02:00
}
}
catch (...)
{
if(isSender)
XBT_INFO("%s failed to send data",selfName.c_str());
else
XBT_INFO("%s failed to receive data",selfName.c_str());
}
2021-05-06 16:25:14 +02:00
// Load next event
2021-05-06 16:14:57 +02:00
i.GotoNextEvent();
2021-05-06 16:25:14 +02:00
nWakeUp++; // Increase the number of wake up
}
// Done
2021-05-06 16:32:06 +02:00
TURN_OFF()
2021-05-06 16:14:57 +02:00
XBT_INFO("Observation node %s finished (nWakeUp:%d|nDataRcv:%d)",selfName.c_str(),nWakeUp,nDataRcv);
}