loosely-coupled-dss-extended/src/simulator.cc

270 lines
11 KiB
C++
Raw Normal View History

2021-05-06 17:46:34 +02:00
#include <simgrid/s4u.hpp>
#include <simgrid/s4u/Mailbox.hpp>
2021-05-06 17:46:34 +02:00
#include <simgrid/s4u/Host.hpp>
#include <simgrid/plugins/energy.h>
#include <xbt/log.h>
#include <string>
#include <sstream>
2021-05-06 17:46:34 +02:00
#include "Inputs.hpp"
#define PLATFORM_FILE "platform.xml"
#define MODE_OFF() simgrid::s4u::this_actor::get_host()->set_pstate(0);
#define MODE_ON() simgrid::s4u::this_actor::get_host()->set_pstate(1);
#define MODE_RX() simgrid::s4u::this_actor::get_host()->set_pstate(2);
#define MODE_TX() simgrid::s4u::this_actor::get_host()->set_pstate(3);
2021-05-12 10:05:19 +02:00
#define CLOCK (simgrid::s4u::Engine::get_clock())
2021-05-12 12:07:00 +02:00
#define CNAME (selfName.c_str())
2021-05-12 10:05:19 +02:00
#define TRACK_UPTIME(instruction) \
{ \
double uptimeTrack=CLOCK; \
instruction; \
uptimeTrack=CLOCK-uptimeTrack; \
uptime-=uptimeTrack; \
uptime=uptime > 0 ? uptime : 0; \
2021-05-14 10:25:51 +02:00
}
/// @brief Note that we need to simulate latency our self since we need to send instantaneous messages
#define SEND(instruction) \
{ \
TRACK_UPTIME(simgrid::s4u::this_actor::sleep_for(i.latency > uptime ? uptime : i.latency)); \
2021-05-14 10:25:51 +02:00
instruction; \
}
#define FORWARD_HINT(TRY_FORWARD_DURING) \
{ \
if(hint_forward!=NULL && CLOCK<hint_forward->hint){ \
hint_forward->HisForward=true; \
hint_forward->DedicatedMailbox="hint_forward"; \
try { \
XBT_INFO("%s try to forward a hint",CNAME); \
TRACK_UPTIME(m->put(hint_forward,0,TRY_FORWARD_DURING)); \
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(hint_forward->DedicatedMailbox); \
MODE_TX(); \
SEND(m_ded->put(hint_forward,0,uptime)); \
} \
catch(...){ \
XBT_INFO("%s fail to forward a hint",CNAME); \
} \
} \
}
2021-05-06 16:25:14 +02:00
/// @brief Required by SimGrid
2021-05-12 12:07:00 +02:00
XBT_LOG_NEW_DEFAULT_CATEGORY(simulator, "[DAO] Loosely Coupled DSS");
2021-05-07 09:07:20 +02:00
/// @brief For convenience sake
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:
Payload():hint(0),duration(0),HasHint(false),HisForward(false){}
Payload(Payload &p):hint(p.hint),duration(p.duration),HasHint(p.HasHint),DedicatedMailbox(p.DedicatedMailbox),HisForward(p.HisForward){}
2021-05-12 12:07:00 +02:00
double hint; // The timestamp that should be used by the receiver
double duration; // The duration that should be used by the receiver
2021-05-12 10:05:19 +02:00
bool HasHint;
bool HisForward;
2021-05-12 12:07:00 +02:00
std::string DedicatedMailbox; // Dedicated mailbox used by the sender/receiver
2021-05-06 11:57:50 +02:00
};
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);
}
2021-05-07 09:07:20 +02:00
// Launch the 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");
2021-05-12 12:07:00 +02:00
XBT_INFO("Deploying observation node %s",CNAME);
2021-05-06 17:46:34 +02:00
// Starting node
2021-05-12 10:05:19 +02:00
bool isObserver=false;
2021-05-06 16:14:57 +02:00
u32 nWakeUp=0;
u32 nDataRcv=0;
u32 nSendFail=0;
u32 nRcvFail=0;
2021-05-09 11:07:16 +02:00
u32 nSend=0;
2021-05-20 09:48:32 +02:00
u32 hint_added=0;
2021-05-10 16:03:46 +02:00
double totalUptime=0;
Payload *hint_forward=NULL;
2021-05-06 16:14:57 +02:00
while(i.ShouldContinue()){
2021-05-12 10:05:19 +02:00
// Start by sleeping
2021-05-12 12:07:00 +02:00
XBT_INFO("%s is spleeping",CNAME);
MODE_OFF();
2021-05-06 16:14:57 +02:00
simgrid::s4u::this_actor::sleep_until(i.GetTS());
MODE_ON();
2021-05-12 12:07:00 +02:00
XBT_INFO("%s wakes up",CNAME);
2021-05-06 16:14:57 +02:00
// Doing wake up stuff
2021-05-09 11:07:16 +02:00
double uptime=i.GetDuration();
2021-05-12 10:05:19 +02:00
double upsince=simgrid::s4u::Engine::get_clock();
double upuntil=i.GetTS()+i.GetDuration();
bool forward_mode=false;
2021-05-12 12:07:00 +02:00
while(uptime>0)
2021-05-12 10:05:19 +02:00
{
if(i.is_sender){
Payload *p=new Payload();
p->DedicatedMailbox="dedicated"+selfName;
2021-05-12 10:22:34 +02:00
// Add hint informations to the payload
2021-05-12 10:05:19 +02:00
if(i.use_hint && i.HasNext()){
2021-05-12 12:07:00 +02:00
p->HasHint=true;
2021-05-12 10:05:19 +02:00
p->duration=i.GetNextDuration();
p->hint=i.GetNextTS();
2021-05-06 11:57:50 +02:00
}
2021-05-12 10:05:19 +02:00
try {
2021-05-12 10:22:34 +02:00
// First we send and instantaneous message
// This allow first to detect if their is a receiver
// (to not cause deadlock for the extended mode) and second
// to inform the receiver if he should get a hint first
TRACK_UPTIME(m->put(p,0,uptime));
2021-05-12 10:05:19 +02:00
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
// First send hint if it is required
if(p->HasHint){
2021-05-14 10:25:51 +02:00
TRACK_UPTIME(SEND(m_ded->put(p,i.hint_size,uptime)));
2021-05-12 12:07:00 +02:00
XBT_INFO("%s sent a hint successfully",CNAME);
2021-05-09 11:07:16 +02:00
}
MODE_TX();
2021-05-12 10:05:19 +02:00
// Then try sending the data
2021-05-14 10:25:51 +02:00
if(i.extended){
SEND(m_ded->put(p,i.data_size));
}
else{
SEND(m_ded->put(p,i.data_size,uptime));
}
2021-05-12 10:22:34 +02:00
// If we reach here, data has been sent successfully
2021-05-12 12:07:00 +02:00
XBT_INFO("%s sent data successfully",CNAME);
2021-05-12 10:05:19 +02:00
nSend++;
i.is_sender=(nSend<(i.n_nodes-1));
isObserver=!i.is_sender;
}
2021-05-12 10:05:19 +02:00
catch(...){
2021-05-12 12:07:00 +02:00
XBT_INFO("%s could not send any data",CNAME);
2021-05-09 11:07:16 +02:00
nSendFail++;
2021-05-12 10:05:19 +02:00
}
}
else if(!isObserver){
// Here we try to forward hint for 1 sec and try to receive data for 5 secs
double try_for=forward_mode ? 1 : 1;
try_for=try_for>uptime ? uptime : try_for; // Ensure we do not exceed uptime
// Forward hint mode
if(forward_mode){
if(hint_forward!=NULL && CLOCK < hint_forward->hint){
FORWARD_HINT(try_for); // Try forward for 5 seconds then switch to received mode
}
}
else { // Receiving mode
Payload *p; // Received data
Payload *hint; // To Save the received hint
bool hintReceived=false; // In case of error during data rx this will be use to check if we could use the *hint Payload object
try {
// Get the instantaneous message
do {
TRACK_UPTIME(p=m->get<Payload>(try_for));
if(p->HisForward){
if(hint_forward==NULL || (hint_forward !=NULL && p->hint!=hint_forward->hint)){
simgrid::s4u::Mailbox *m_ded=simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
MODE_RX();
TRACK_UPTIME(p=m_ded->get<Payload>(uptime));
MODE_ON();
XBT_INFO("%s received a forwarded hint successfully",CNAME);
if(CLOCK < p->hint){
i.AddEvent(p->hint, p->duration);
hint_forward=new Payload(*p);
2021-05-20 09:48:32 +02:00
hint_added++;
}
}
}
} while(p->HisForward);
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
// Start receiving data
MODE_RX();
if(p->HasHint){
TRACK_UPTIME(p=m_ded->get<Payload>(uptime));
XBT_INFO("%s received a hint successfully",CNAME);
hint=new Payload(*p); // Save hint
hint_forward=new Payload(*p); // Enable hint forwarding
hintReceived=true;
}
if(i.extended)
p=m_ded->get<Payload>(); // Fetch data until sended
else
p=m_ded->get<Payload>(uptime); // Fetch data until sended or uptime expire
// If we reach here, data has been received successfully
XBT_INFO("%s received data successfully",CNAME);
nDataRcv++;
isObserver=true;
i.is_sender=false;
}catch(...){
XBT_INFO("%s could not receive any data",CNAME);
nRcvFail++;
2021-05-20 09:48:32 +02:00
if(hintReceived){
i.AddEvent(hint->hint, hint->duration); // Add the hint to the event list
2021-05-20 09:48:32 +02:00
hint_added++;
}
2021-05-09 11:07:16 +02:00
}
2021-05-12 10:05:19 +02:00
}
forward_mode=!forward_mode; // Toggle mode (go back and forth between receiving and forwarding)
}
2021-05-12 10:05:19 +02:00
else {
2021-05-12 12:07:00 +02:00
XBT_INFO("%s is observing his environment...",CNAME);
2021-05-12 10:05:19 +02:00
MODE_ON();
if(hint_forward!=NULL && CLOCK < hint_forward->hint){
FORWARD_HINT(uptime);
}
else{
simgrid::s4u::this_actor::sleep_for(uptime);
}
2021-05-12 10:05:19 +02:00
}
uptime=upuntil-CLOCK; // Note that uptime can be < 0 in extended mode
}
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
2021-05-12 10:05:19 +02:00
totalUptime+=CLOCK-upsince;
}
// Done
MODE_OFF()
2021-05-20 09:48:32 +02:00
XBT_INFO("Observation node %s finished [LOG2PARSE](node:%s|isSender:%d|nSend:%d|nWakeUp:%d|nDataRcv:%d|nSendFail:%d|nRcvFail:%d|totalUptime:%f|seed:%d|hint_added:%d)",CNAME,CNAME,i.is_sender,nSend,nWakeUp,nDataRcv,nSendFail,nRcvFail,totalUptime,i.seed,hint_added);
}