1 /* Copyright (c) 2008-2019. 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();
22 this->internal_req = s_smx_simcall();
23 this->executed_req_ = s_smx_simcall();
25 actor_states_.resize(MC_smx_get_maxpid());
26 /* Stateful model checking */
27 if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
28 system_state = std::make_shared<simgrid::mc::Snapshot>(num_);
29 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
30 MC_state_copy_incomplete_communications_pattern(this);
31 MC_state_copy_index_communications_pattern(this);
36 std::size_t State::interleave_size() const
38 return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
41 Transition State::get_transition() const
43 return this->transition_;
49 /* Search an enabled transition for the given process.
51 * This can be seen as an iterator returning the next transition of the process.
53 * We only consider the processes that are both
54 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
55 * - which simcall can currently be executed (like a comm where the other partner is already known)
56 * Once we returned the last enabled transition of a process, it is marked done.
58 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
59 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
61 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
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(static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(&actor->simcall)));
115 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
116 mc_model_checker->process().read(temp_act, remote_act);
117 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->process().read(
161 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
162 mc_model_checker->process().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->process().read(
173 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
174 mc_model_checker->process().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->process().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->process().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->process().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());