From: Martin Quinson Date: Sat, 12 Feb 2022 18:10:43 +0000 (+0100) Subject: Simplify the serialization protocol to implement TestAny & WaitAny in a moment X-Git-Tag: v3.31~424 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4118c5ef84abd079c5b785e9e72bb5a1a9ac129d Simplify the serialization protocol to implement TestAny & WaitAny in a moment --- diff --git a/src/kernel/actor/SimcallObserver.cpp b/src/kernel/actor/SimcallObserver.cpp index 569eca1212..b230c668f9 100644 --- a/src/kernel/actor/SimcallObserver.cpp +++ b/src/kernel/actor/SimcallObserver.cpp @@ -19,6 +19,10 @@ namespace simgrid { namespace kernel { namespace actor { +void SimcallObserver::serialize(std::stringstream& stream) const +{ + stream << (short)mc::Transition::Type::UNKNOWN; +} bool SimcallObserver::depends(SimcallObserver* other) { THROW_UNIMPLEMENTED; @@ -28,9 +32,9 @@ bool RandomSimcall::depends(SimcallObserver* other) { return get_issuer() == other->get_issuer(); } -void RandomSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream) +void RandomSimcall::serialize(std::stringstream& stream) const { - type = mc::Transition::Type::RANDOM; + stream << (short)mc::Transition::Type::RANDOM << ' '; stream << min_ << ' ' << max_; } @@ -132,29 +136,29 @@ std::string ActivityTestanySimcall::to_string(int times_considered) const return res; }*/ -void ActivityWaitSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream) +void ActivityWaitSimcall::serialize(std::stringstream& stream) const { if (auto* comm = dynamic_cast(activity_)) { - type = mc::Transition::Type::COMM_WAIT; + stream << (short)mc::Transition::Type::COMM_WAIT << ' '; stream << (timeout_ > 0) << ' ' << comm; stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1); stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1); stream << ' ' << comm->get_mailbox_id(); stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_; } else { - type = mc::Transition::Type::UNKNOWN; + stream << (short)mc::Transition::Type::UNKNOWN; } } -void ActivityTestSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream) +void ActivityTestSimcall::serialize(std::stringstream& stream) const { if (auto* comm = dynamic_cast(activity_)) { - type = mc::Transition::Type::COMM_TEST; + stream << (short)mc::Transition::Type::COMM_TEST << ' '; stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1); stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1); stream << ' ' << comm->get_mailbox_id(); stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_; } else { - type = mc::Transition::Type::UNKNOWN; + stream << (short)mc::Transition::Type::UNKNOWN; } } @@ -206,17 +210,17 @@ void ActivityWaitanySimcall::prepare(int times_considered) next_value_ = times_considered; } -void CommIsendSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream) +void CommIsendSimcall::serialize(std::stringstream& stream) const { - type = mc::Transition::Type::COMM_SEND; + stream << (short)mc::Transition::Type::COMM_SEND << ' '; stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_; XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_); } -void CommIrecvSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream) +void CommIrecvSimcall::serialize(std::stringstream& stream) const { - type = mc::Transition::Type::COMM_RECV; - stream << mbox_->get_id() << dst_buff_; + stream << (short)mc::Transition::Type::COMM_RECV << ' '; + stream << mbox_->get_id() << ' ' << dst_buff_; } } // namespace actor diff --git a/src/kernel/actor/SimcallObserver.hpp b/src/kernel/actor/SimcallObserver.hpp index 4bbfa8a075..54169d5ec4 100644 --- a/src/kernel/actor/SimcallObserver.hpp +++ b/src/kernel/actor/SimcallObserver.hpp @@ -54,10 +54,7 @@ public: virtual bool depends(SimcallObserver* other); /** Serialize to the given string buffer */ - virtual void serialize(mc::Transition::Type& type, std::stringstream& stream) - { - type = mc::Transition::Type::UNKNOWN; - } + virtual void serialize(std::stringstream& stream) const; /** Some simcalls may only be observable under some conditions. * Most simcalls are not visible from the MC because they don't have an observer at all. */ @@ -84,7 +81,7 @@ public: { xbt_assert(min < max); } - void serialize(mc::Transition::Type& type, std::stringstream& stream) override; + void serialize(std::stringstream& stream) const override; int get_max_consider() const override; void prepare(int times_considered) override; int get_value() const { return next_value_; } @@ -158,7 +155,7 @@ public: } bool is_visible() const override { return true; } activity::ActivityImpl* get_activity() const { return activity_; } - void serialize(mc::Transition::Type& type, std::stringstream& stream) override; + void serialize(std::stringstream& stream) const override; }; class ActivityTestanySimcall : public ResultingSimcall { @@ -186,7 +183,7 @@ public: : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout) { } - void serialize(mc::Transition::Type& type, std::stringstream& stream) override; + void serialize(std::stringstream& stream) const override; bool is_visible() const override { return true; } bool is_enabled() const override; activity::ActivityImpl* get_activity() const { return activity_; } @@ -245,7 +242,7 @@ public: , copy_data_fun_(copy_data_fun) { } - void serialize(mc::Transition::Type& type, std::stringstream& stream) override; + void serialize(std::stringstream& stream) const override; bool is_visible() const override { return true; } activity::MailboxImpl* get_mailbox() const { return mbox_; } double get_payload_size() const { return payload_size_; } @@ -280,7 +277,7 @@ public: , copy_data_fun_(copy_data_fun) { } - void serialize(mc::Transition::Type& type, std::stringstream& stream) override; + void serialize(std::stringstream& stream) const override; bool is_visible() const override { return true; } activity::MailboxImpl* get_mailbox() const { return mbox_; } double get_rate() const { return rate_; } diff --git a/src/mc/ModelChecker.cpp b/src/mc/ModelChecker.cpp index fdae4fe19d..4fdb59de4a 100644 --- a/src/mc/ModelChecker.cpp +++ b/src/mc/ModelChecker.cpp @@ -332,7 +332,7 @@ Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool n checker_side_.dispatch(); // The app may send messages while processing the transition if (new_transition) - return recv_transition(aid, times_considered, answer.simcall, answer.buffer); + return recv_transition(aid, times_considered, answer.buffer); else return nullptr; } diff --git a/src/mc/api/Transition.cpp b/src/mc/api/Transition.cpp index 0af324b945..5e63923ade 100644 --- a/src/mc/api/Transition.cpp +++ b/src/mc/api/Transition.cpp @@ -46,10 +46,9 @@ std::string RandomTransition::to_string(bool verbose) const return xbt::string_printf("Random([%d;%d] ~> %d)", min_, max_, times_considered_); } -RandomTransition::RandomTransition(aid_t issuer, int times_considered, char* buffer) +RandomTransition::RandomTransition(aid_t issuer, int times_considered, std::stringstream& stream) : Transition(Type::RANDOM, issuer, times_considered) { - std::stringstream stream(buffer); stream >> min_ >> max_; } std::string RandomTransition::dot_label() const diff --git a/src/mc/api/Transition.hpp b/src/mc/api/Transition.hpp index 9aa49f8cba..8b8458bce3 100644 --- a/src/mc/api/Transition.hpp +++ b/src/mc/api/Transition.hpp @@ -10,6 +10,7 @@ #include "simgrid/forward.h" // aid_t #include "xbt/utility.hpp" // XBT_DECLARE_ENUM_CLASS +#include #include namespace simgrid { @@ -74,7 +75,7 @@ class RandomTransition : public Transition { public: std::string to_string(bool verbose) const override; std::string dot_label() const override; - RandomTransition(aid_t issuer, int times_considered, char* buffer); + RandomTransition(aid_t issuer, int times_considered, std::stringstream& stream); bool depends(const Transition* other) const override { return false; } // Independent with any other transition }; diff --git a/src/mc/api/TransitionComm.cpp b/src/mc/api/TransitionComm.cpp index dc14fe9226..2f86ceaa44 100644 --- a/src/mc/api/TransitionComm.cpp +++ b/src/mc/api/TransitionComm.cpp @@ -20,10 +20,9 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_comm, mc_transition, namespace simgrid { namespace mc { -CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, char* buffer) +CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, std::stringstream& stream) : Transition(Type::COMM_WAIT, issuer, times_considered) { - std::stringstream stream(buffer); stream >> timeout_ >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_; XBT_DEBUG("CommWaitTransition %s comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu", (timeout_ ? "timeout" : "no-timeout"), comm_, sender_, receiver_, mbox_, src_buff_, dst_buff_, size_); @@ -69,10 +68,9 @@ bool CommWaitTransition::depends(const Transition* other) const return true; } -CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, char* buffer) +CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream) : Transition(Type::COMM_TEST, issuer, times_considered) { - std::stringstream stream(buffer); stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_; XBT_DEBUG("CommTestTransition comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu", comm_, sender_, receiver_, mbox_, src_buff_, dst_buff_, size_); @@ -115,10 +113,9 @@ bool CommTestTransition::depends(const Transition* other) const return true; } -CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, char* buffer) +CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream) : Transition(Type::COMM_RECV, issuer, times_considered) { - std::stringstream stream(buffer); stream >> mbox_ >> dst_buff_; } std::string CommRecvTransition::to_string(bool verbose) const @@ -165,10 +162,9 @@ bool CommRecvTransition::depends(const Transition* other) const return true; } -CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, char* buffer) +CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream) : Transition(Type::COMM_SEND, issuer, times_considered) { - std::stringstream stream(buffer); stream >> mbox_ >> src_buff_ >> size_; XBT_DEBUG("SendTransition mbox:%u buff:%p size:%zu", mbox_, src_buff_, size_); } @@ -217,20 +213,25 @@ bool CommSendTransition::depends(const Transition* other) const return true; } -Transition* recv_transition(aid_t issuer, int times_considered, Transition::Type simcall, char* buffer) +Transition* recv_transition(aid_t issuer, int times_considered, char* buffer) { + std::stringstream stream(buffer); + short type; + stream >> type; + Transition::Type simcall = static_cast(type); + switch (simcall) { case Transition::Type::COMM_RECV: - return new CommRecvTransition(issuer, times_considered, buffer); + return new CommRecvTransition(issuer, times_considered, stream); case Transition::Type::COMM_SEND: - return new CommSendTransition(issuer, times_considered, buffer); + return new CommSendTransition(issuer, times_considered, stream); case Transition::Type::COMM_TEST: - return new CommTestTransition(issuer, times_considered, buffer); + return new CommTestTransition(issuer, times_considered, stream); case Transition::Type::COMM_WAIT: - return new CommWaitTransition(issuer, times_considered, buffer); + return new CommWaitTransition(issuer, times_considered, stream); case Transition::Type::RANDOM: - return new RandomTransition(issuer, times_considered, buffer); + return new RandomTransition(issuer, times_considered, stream); case Transition::Type::UNKNOWN: return new Transition(Transition::Type::UNKNOWN, issuer, times_considered); diff --git a/src/mc/api/TransitionComm.hpp b/src/mc/api/TransitionComm.hpp index 0d1264bb23..29ebf679f0 100644 --- a/src/mc/api/TransitionComm.hpp +++ b/src/mc/api/TransitionComm.hpp @@ -10,6 +10,7 @@ #include "src/kernel/actor/SimcallObserver.hpp" #include "src/mc/api/Transition.hpp" +#include #include namespace simgrid { @@ -33,7 +34,7 @@ class CommWaitTransition : public Transition { friend CommTestTransition; public: - CommWaitTransition(aid_t issuer, int times_considered, char* buffer); + CommWaitTransition(aid_t issuer, int times_considered, std::stringstream& stream); std::string to_string(bool verbose) const override; bool depends(const Transition* other) const override; }; @@ -49,7 +50,7 @@ class CommTestTransition : public Transition { friend CommRecvTransition; public: - CommTestTransition(aid_t issuer, int times_considered, char* buffer); + CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream); std::string to_string(bool verbose) const override; bool depends(const Transition* other) const override; }; @@ -59,7 +60,7 @@ class CommRecvTransition : public Transition { void* dst_buff_; public: - CommRecvTransition(aid_t issuer, int times_considered, char* buffer); + CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream); std::string to_string(bool verbose) const override; bool depends(const Transition* other) const override; }; @@ -70,13 +71,13 @@ class CommSendTransition : public Transition { size_t size_; public: - CommSendTransition(aid_t issuer, int times_considered, char* buffer); + CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream); std::string to_string(bool verbose) const override; bool depends(const Transition* other) const override; }; /** Make a new transition from serialized description */ -Transition* recv_transition(aid_t issuer, int times_considered, Transition::Type simcall, char* buffer); +Transition* recv_transition(aid_t issuer, int times_considered, char* buffer); } // namespace mc } // namespace simgrid diff --git a/src/mc/remote/AppSide.cpp b/src/mc/remote/AppSide.cpp index 496c5bc533..e062682e31 100644 --- a/src/mc/remote/AppSide.cpp +++ b/src/mc/remote/AppSide.cpp @@ -103,18 +103,17 @@ void AppSide::handle_simcall_execute(const s_mc_message_simcall_execute_t* messa s_mc_message_simcall_execute_answer_t answer; memset(&answer, 0, sizeof(answer)); answer.type = MessageType::SIMCALL_EXECUTE_ANSWER; + std::stringstream stream; if (actor->simcall_.observer_ != nullptr) { - std::stringstream stream; - actor->simcall_.observer_->serialize(answer.simcall, stream); - xbt_assert(stream.str().size() < sizeof(answer.buffer) - 1, - "The serialized simcall is too large for the buffer. Please fix the code."); - strncpy(answer.buffer, stream.str().c_str(), sizeof(answer.buffer) - 1); + actor->simcall_.observer_->serialize(stream); } else { - answer.simcall = mc::Transition::Type::UNKNOWN; + stream << (short)mc::Transition::Type::UNKNOWN; } + xbt_assert(stream.str().size() < sizeof(answer.buffer) - 1, + "The serialized simcall is too large for the buffer. Please fix the code."); + strncpy(answer.buffer, stream.str().c_str(), sizeof(answer.buffer) - 1); - XBT_DEBUG("send SIMCALL_EXECUTE_ANSWER(%s) ~> %s '%s'", actor->get_cname(), mc::Transition::to_c_str(answer.simcall), - answer.buffer); + XBT_DEBUG("send SIMCALL_EXECUTE_ANSWER(%s) ~> '%s'", actor->get_cname(), answer.buffer); xbt_assert(channel_.send(answer) == 0, "Could not send response"); // The client may send some messages to the server while processing the transition diff --git a/src/mc/remote/mc_protocol.h b/src/mc/remote/mc_protocol.h index d8915e5aca..b933feaccc 100644 --- a/src/mc/remote/mc_protocol.h +++ b/src/mc/remote/mc_protocol.h @@ -105,7 +105,6 @@ struct s_mc_message_simcall_execute_t { }; struct s_mc_message_simcall_execute_answer_t { simgrid::mc::MessageType type; - simgrid::mc::Transition::Type simcall; char buffer[SIMCALL_SERIALIZATION_BUFFER_SIZE]; };