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 /* reset the outgoing transition */
62 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
63 state->transition_.pid_ = -1;
64 state->transition_.argument_ = -1;
65 state->executed_req_.call_ = SIMCALL_NONE;
67 if (not simgrid::mc::actor_is_enabled(actor))
68 return nullptr; // Not executable in the application
70 smx_simcall_t req = nullptr;
71 switch (actor->simcall_.call_) {
72 case SIMCALL_COMM_WAITANY:
73 state->transition_.argument_ = -1;
74 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
75 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
76 state->transition_.argument_ = procstate->times_considered;
77 ++procstate->times_considered;
80 ++procstate->times_considered;
83 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
84 procstate->set_done();
85 if (state->transition_.argument_ != -1)
86 req = &actor->simcall_;
89 case SIMCALL_COMM_TESTANY: {
90 unsigned start_count = procstate->times_considered;
91 state->transition_.argument_ = -1;
92 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
93 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
94 state->transition_.argument_ = procstate->times_considered;
95 ++procstate->times_considered;
98 ++procstate->times_considered;
101 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
102 procstate->set_done();
104 if (state->transition_.argument_ != -1 || start_count == 0)
105 req = &actor->simcall_;
110 case SIMCALL_COMM_WAIT: {
111 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
112 remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
113 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
114 mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
115 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
116 if (act->src_actor_.get() && act->dst_actor_.get())
117 state->transition_.argument_ = 0; // OK
118 else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
120 state->transition_.argument_ = 0; // OK
122 state->transition_.argument_ = -1; // timeout
123 procstate->set_done();
124 req = &actor->simcall_;
128 case SIMCALL_MC_RANDOM: {
129 int min_value = simcall_mc_random__get__min(&actor->simcall_);
130 state->transition_.argument_ = procstate->times_considered + min_value;
131 procstate->times_considered++;
132 if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
133 procstate->set_done();
134 req = &actor->simcall_;
139 procstate->set_done();
140 state->transition_.argument_ = 0;
141 req = &actor->simcall_;
147 state->transition_.pid_ = actor->get_pid();
148 state->executed_req_ = *req;
149 // Fetch the data of the request and translate it:
150 state->internal_req_ = *req;
152 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
153 * action so it can be treated later by the dependence function. */
154 switch (req->call_) {
155 case SIMCALL_COMM_WAITANY: {
156 state->internal_req_.call_ = SIMCALL_COMM_WAIT;
157 simgrid::kernel::activity::CommImpl* remote_comm;
158 remote_comm = mc_model_checker->get_remote_simulation().read(
159 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
160 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
161 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
162 simcall_comm_wait__set__timeout(&state->internal_req_, 0);
166 case SIMCALL_COMM_TESTANY:
167 state->internal_req_.call_ = SIMCALL_COMM_TEST;
169 if (state->transition_.argument_ > 0) {
170 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
171 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
172 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
175 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
176 simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
179 case SIMCALL_COMM_WAIT:
180 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
181 remote(simcall_comm_wait__getraw__comm(req)));
182 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
183 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
186 case SIMCALL_COMM_TEST:
187 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
188 remote(simcall_comm_test__getraw__comm(req)));
189 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
190 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
194 /* No translation needed */
201 smx_simcall_t MC_state_choose_request(simgrid::mc::State* state)
203 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
204 /* Only consider the actors that were marked as interleaving by the checker algorithm */
205 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
208 smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());