1 /* Copyright (c) 2008-2020. 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/mc_state.hpp"
7 #include "src/mc/mc_comm_pattern.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_request.hpp"
10 #include "src/mc/mc_smx.hpp"
12 #include <boost/range/algorithm.hpp>
14 using simgrid::mc::remote;
19 State::State(unsigned long state_number) : num_(state_number)
21 this->internal_comm_.clear();
23 actor_states_.resize(MC_smx_get_maxpid());
24 /* Stateful model checking */
25 if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
26 system_state_ = std::make_shared<simgrid::mc::Snapshot>(num_);
27 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
28 MC_state_copy_incomplete_communications_pattern(this);
29 MC_state_copy_index_communications_pattern(this);
34 std::size_t State::interleave_size() const
36 return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
39 Transition State::get_transition() const
41 return this->transition_;
47 /* Search an enabled transition for the given process.
49 * This can be seen as an iterator returning the next transition of the process.
51 * We only consider the processes that are both
52 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
53 * - which simcall can currently be executed (like a comm where the other partner is already known)
54 * Once we returned the last enabled transition of a process, it is marked done.
56 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
57 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
59 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
61 using simgrid::simix::Simcall;
63 /* reset the outgoing transition */
64 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
65 state->transition_.pid_ = -1;
66 state->transition_.argument_ = -1;
67 state->executed_req_.call_ = Simcall::NONE;
69 if (not simgrid::mc::actor_is_enabled(actor))
70 return nullptr; // Not executable in the application
72 smx_simcall_t req = nullptr;
73 switch (actor->simcall_.call_) {
74 case Simcall::COMM_WAITANY:
75 state->transition_.argument_ = -1;
76 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
77 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
78 state->transition_.argument_ = procstate->times_considered;
79 ++procstate->times_considered;
82 ++procstate->times_considered;
85 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
86 procstate->set_done();
87 if (state->transition_.argument_ != -1)
88 req = &actor->simcall_;
91 case Simcall::COMM_TESTANY: {
92 unsigned start_count = procstate->times_considered;
93 state->transition_.argument_ = -1;
94 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
95 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
96 state->transition_.argument_ = procstate->times_considered;
97 ++procstate->times_considered;
100 ++procstate->times_considered;
103 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
104 procstate->set_done();
106 if (state->transition_.argument_ != -1 || start_count == 0)
107 req = &actor->simcall_;
112 case Simcall::COMM_WAIT: {
113 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
114 remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
115 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
116 mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
117 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
118 if (act->src_actor_.get() && act->dst_actor_.get())
119 state->transition_.argument_ = 0; // OK
120 else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
122 state->transition_.argument_ = 0; // OK
124 state->transition_.argument_ = -1; // timeout
125 procstate->set_done();
126 req = &actor->simcall_;
130 case Simcall::MC_RANDOM: {
131 int min_value = simcall_mc_random__get__min(&actor->simcall_);
132 state->transition_.argument_ = procstate->times_considered + min_value;
133 procstate->times_considered++;
134 if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
135 procstate->set_done();
136 req = &actor->simcall_;
141 procstate->set_done();
142 state->transition_.argument_ = 0;
143 req = &actor->simcall_;
149 state->transition_.pid_ = actor->get_pid();
150 state->executed_req_ = *req;
151 // Fetch the data of the request and translate it:
152 state->internal_req_ = *req;
154 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
155 * action so it can be treated later by the dependence function. */
156 switch (req->call_) {
157 case Simcall::COMM_WAITANY: {
158 state->internal_req_.call_ = Simcall::COMM_WAIT;
159 simgrid::kernel::activity::CommImpl* remote_comm;
160 remote_comm = mc_model_checker->get_remote_simulation().read(
161 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
162 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
163 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
164 simcall_comm_wait__set__timeout(&state->internal_req_, 0);
168 case Simcall::COMM_TESTANY:
169 state->internal_req_.call_ = Simcall::COMM_TEST;
171 if (state->transition_.argument_ > 0) {
172 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
173 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
174 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
177 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
178 simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
181 case Simcall::COMM_WAIT:
182 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
183 remote(simcall_comm_wait__getraw__comm(req)));
184 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
185 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
188 case Simcall::COMM_TEST:
189 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
190 remote(simcall_comm_test__getraw__comm(req)));
191 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
192 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
196 /* No translation needed */
203 smx_simcall_t MC_state_choose_request(simgrid::mc::State* state)
205 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
206 /* Only consider the actors that were marked as interleaving by the checker algorithm */
207 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
210 smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());