1 /* Copyright (c) 2008-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/api/State.hpp"
7 #include "src/mc/Session.hpp"
8 #include "src/mc/api.hpp"
9 #include "src/mc/mc_config.hpp"
11 #include <boost/range/algorithm.hpp>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC states");
15 using simgrid::mc::remote;
16 using api = simgrid::mc::Api;
21 long State::expended_states_ = 0;
23 State::State() : num_(++expended_states_)
25 const unsigned long maxpid = api::get().get_maxpid();
26 actor_states_.resize(maxpid);
27 transition_.reset(new Transition());
28 /* Stateful model checking */
29 if ((_sg_mc_checkpoint > 0 && (num_ % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
30 auto snapshot_ptr = api::get().take_snapshot(num_);
31 system_state_ = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
32 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
33 copy_incomplete_comm_pattern();
34 copy_index_comm_pattern();
39 std::size_t State::count_todo() const
41 return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
44 Transition* State::get_transition() const
46 return transition_.get();
49 int State::next_transition() const
51 std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
52 XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
53 for (unsigned int i = 0; i < actors.size(); i++) {
54 aid_t aid = actors[i].copy.get_buffer()->get_pid();
55 const ActorState* actor_state = &actor_states_[aid];
57 /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
58 if (not actor_state->is_todo() || not simgrid::mc::session_singleton->actor_is_enabled(aid))
65 void State::execute_next(int next)
67 std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
69 kernel::actor::ActorImpl* actor = actors[next].copy.get_buffer();
70 aid_t aid = actor->get_pid();
73 simgrid::mc::ActorState* actor_state = &actor_states_[aid];
74 /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
75 if (actor->simcall_.observer_ != nullptr) {
76 times_considered = actor_state->get_times_considered_and_inc();
77 if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
78 actor_state->set_done();
81 actor_state->set_done();
84 transition_->init(aid, times_considered);
85 executed_req_ = actor->simcall_;
87 XBT_DEBUG("Let's run actor %ld (times_considered = %d)", aid, times_considered);
89 Transition::executed_transitions_++;
91 transition_.reset(mc_model_checker->handle_simcall(*transition_, true));
92 mc_model_checker->wait_for_requests();
95 void State::copy_incomplete_comm_pattern()
97 incomplete_comm_pattern_.clear();
98 const unsigned long maxpid = api::get().get_maxpid();
99 for (unsigned long i = 0; i < maxpid; i++) {
100 std::vector<simgrid::mc::PatternCommunication> res;
101 for (auto const& comm : incomplete_communications_pattern[i])
102 res.push_back(comm->dup());
103 incomplete_comm_pattern_.push_back(std::move(res));
107 void State::copy_index_comm_pattern()
109 communication_indices_.clear();
110 for (auto const& list_process_comm : initial_communications_pattern)
111 this->communication_indices_.push_back(list_process_comm.index_comm);
115 } // namespace simgrid