X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/41966a6d30f870c0c7cc5ad5e64c0263e63970a8..a7cc502f224afb96f0203b94759cd0dabebf8de5:/src/mc/api/RemoteApp.cpp diff --git a/src/mc/api/RemoteApp.cpp b/src/mc/api/RemoteApp.cpp index 2ddd5c0876..dc6c7d0d54 100644 --- a/src/mc/api/RemoteApp.cpp +++ b/src/mc/api/RemoteApp.cpp @@ -17,11 +17,12 @@ #include #include +#include #include #include #include - #include +#include #include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session"); @@ -29,22 +30,50 @@ XBT_LOG_EXTERNAL_CATEGORY(mc_global); namespace simgrid::mc { -RemoteApp::RemoteApp(const std::vector& args) -{ - checker_side_ = std::make_unique(args); - - initial_snapshot_ = std::make_shared(0, page_store_, checker_side_->get_remote_memory()); -} +static std::string master_socket_name; -RemoteApp::~RemoteApp() +RemoteApp::RemoteApp(const std::vector& args) : app_args_(args) { - initial_snapshot_ = nullptr; - shutdown(); + master_socket_ = socket(AF_UNIX, +#ifdef __APPLE__ + SOCK_STREAM, /* Mac OSX does not have AF_UNIX + SOCK_SEQPACKET, even if that's faster */ +#else + SOCK_SEQPACKET, +#endif + 0); + xbt_assert(master_socket_ != -1, "Cannot create the master socket: %s", strerror(errno)); + + master_socket_name = "/tmp/simgrid-mc-" + std::to_string(getpid()); + master_socket_name.resize(MC_SOCKET_NAME_LEN); // truncate socket name if it's too long + master_socket_name.back() = '\0'; // ensure the data are null-terminated +#ifdef __linux__ + master_socket_name[0] = '\0'; // abstract socket, automatically removed after close +#else + unlink(master_socket_name.c_str()); // remove possible stale socket before bind + atexit([]() { + if (not master_socket_name.empty()) + unlink(master_socket_name.c_str()); + master_socket_name.clear(); + }); +#endif + + struct sockaddr_un serv_addr = {}; + serv_addr.sun_family = AF_UNIX; + master_socket_name.copy(serv_addr.sun_path, MC_SOCKET_NAME_LEN); + + xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, sizeof serv_addr) >= 0, + "Cannot bind the master socket to %c%s: %s.", (serv_addr.sun_path[0] ? serv_addr.sun_path[0] : '@'), + serv_addr.sun_path + 1, strerror(errno)); + + xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno)); + + application_factory_ = std::make_unique(app_args_); + checker_side_ = application_factory_->clone(master_socket_, master_socket_name); } -void RemoteApp::restore_initial_state() const +void RemoteApp::restore_initial_state() { - this->initial_snapshot_->restore(checker_side_->get_remote_memory()); + checker_side_ = application_factory_->clone(master_socket_, master_socket_name); } unsigned long RemoteApp::get_maxpid() const @@ -70,26 +99,26 @@ void RemoteApp::get_actors_status(std::map& whereto) const // // CheckerSide AppSide // send ACTORS_STATUS ----> - // <----- send ACTORS_STATUS_REPLY - // <----- send `N` `s_mc_message_actors_status_one_t` structs - // <----- send `M` `s_mc_message_simcall_probe_one_t` structs + // <----- send ACTORS_STATUS_REPLY_COUNT + // <----- send `N` ACTORS_STATUS_REPLY_TRANSITION (s_mc_message_actors_status_one_t) + // <----- send `M` ACTORS_STATUS_REPLY_SIMCALL (s_mc_message_simcall_probe_one_t) + // + // Note that we also receive disabled transitions, because the guiding strategies need them to decide what could + // unlock actors. + checker_side_->get_channel().send(MessageType::ACTORS_STATUS); s_mc_message_actors_status_answer_t answer; ssize_t answer_size = checker_side_->get_channel().receive(answer); xbt_assert(answer_size != -1, "Could not receive message"); xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer); - xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY, - "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)", - to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY); + xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY_COUNT, + "%d Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY_COUNT (%i)", getpid(), + to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY_COUNT); // Message sanity checks - xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count); - xbt_assert(answer.transition_count >= 0, "Received an ACTOR_STATUS_REPLY message with transition_count '%d' < 0", - answer.transition_count); - xbt_assert(answer.transition_count == 0 || answer.count >= 0, - "Received an ACTOR_STATUS_REPLY message with no actor data " - "but with transition data nonetheless"); + xbt_assert(answer.count >= 0, "Received an ACTORS_STATUS_REPLY_COUNT message with an actor count of '%d' < 0", + answer.count); std::vector status(answer.count); if (answer.count > 0) { @@ -98,43 +127,25 @@ void RemoteApp::get_actors_status(std::map& whereto) const xbt_assert(static_cast(received) == size); } - // Ensures that each actor sends precisely `answer.transition_count` transitions. While technically - // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead - // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide - const auto expected_transitions = std::accumulate( - status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; }); - xbt_assert(expected_transitions == answer.transition_count, - "Expected to receive %d transition(s) but was only notified of %d by the app side", expected_transitions, - answer.transition_count); - - std::vector probes(answer.transition_count); - if (answer.transition_count > 0) { - for (auto& probe : probes) { + whereto.clear(); + + for (const auto& actor : status) { + std::vector> actor_transitions; + int n_transitions = actor.max_considered; + for (int times_considered = 0; times_considered < n_transitions; times_considered++) { + s_mc_message_simcall_probe_one_t probe; ssize_t received = checker_side_->get_channel().receive(probe); xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno)); xbt_assert(static_cast(received) == sizeof probe, "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected", received, sizeof probe); - } - } - whereto.clear(); - std::move_iterator probes_iter(probes.begin()); - - for (const auto& actor : status) { - xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered, - "If any transitions are serialized for an actor, it must match the " - "total number of transitions that can be considered for the actor " - "(currently %d), but only %d transition(s) was/were said to be encoded", - actor.max_considered, actor.n_transitions); - - std::vector> actor_transitions; - for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, probes_iter++) { - std::stringstream stream((*probes_iter).buffer.data()); + std::stringstream stream(probe.buffer.data()); actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream)); } - XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid); + XBT_DEBUG("Received %zu transitions for actor %ld. The first one is %s", actor_transitions.size(), actor.aid, + (actor_transitions.size() > 0 ? actor_transitions[0]->to_string().c_str() : "null")); whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions)); } } @@ -159,7 +170,7 @@ void RemoteApp::check_deadlock() const "--cfg=model-check/replay:'%s'", explo->get_record_trace().to_string().c_str()); explo->log_state(); - throw DeadlockError(); + throw McError(ExitStatus::DEADLOCK); } } @@ -168,18 +179,6 @@ void RemoteApp::wait_for_requests() checker_side_->wait_for_requests(); } -void RemoteApp::shutdown() -{ - XBT_DEBUG("Shutting down model-checker"); - - if (checker_side_->running()) { - XBT_DEBUG("Killing process"); - finalize_app(true); - kill(checker_side_->get_pid(), SIGKILL); - checker_side_->terminate(); - } -} - Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition) { s_mc_message_simcall_execute_t m = {}; @@ -188,17 +187,16 @@ Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_ m.times_considered_ = times_considered; checker_side_->get_channel().send(m); - get_remote_process_memory().clear_cache(); if (checker_side_->running()) checker_side_->dispatch_events(); // The app may send messages while processing the transition s_mc_message_simcall_execute_answer_t answer; ssize_t s = checker_side_->get_channel().receive(answer); xbt_assert(s != -1, "Could not receive message"); + xbt_assert(s > 0 && answer.type == MessageType::SIMCALL_EXECUTE_REPLY, + "%d Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_REPLY (%i)", getpid(), + to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_REPLY); xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer); - xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER, - "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)", - to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER); if (new_transition) { std::stringstream stream(answer.buffer.data()); @@ -209,18 +207,7 @@ Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_ void RemoteApp::finalize_app(bool terminate_asap) { - s_mc_message_int_t m = {}; - m.type = MessageType::FINALIZE; - m.value = terminate_asap; - xbt_assert(checker_side_->get_channel().send(m) == 0, "Could not ask the app to finalize on need"); - - s_mc_message_t answer; - ssize_t s = checker_side_->get_channel().receive(answer); - xbt_assert(s != -1, "Could not receive answer to FINALIZE"); - xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer); - xbt_assert(answer.type == MessageType::FINALIZE_REPLY, - "Received unexpected message %s (%i); expected MessageType::FINALIZE_REPLY (%i)", to_c_str(answer.type), - (int)answer.type, (int)MessageType::FINALIZE_REPLY); + checker_side_->finalize(terminate_asap); } } // namespace simgrid::mc