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;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC (state)");
21 State::State(unsigned long state_number) : num_(state_number)
23 this->internal_comm.clear();
24 this->internal_req = s_smx_simcall();
25 this->executed_req_ = s_smx_simcall();
27 actor_states_.resize(MC_smx_get_maxpid());
28 /* Stateful model checking */
29 if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
30 system_state = std::make_shared<simgrid::mc::Snapshot>(num_);
31 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
32 MC_state_copy_incomplete_communications_pattern(this);
33 MC_state_copy_index_communications_pattern(this);
38 std::size_t State::interleave_size() const
40 return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
43 Transition State::get_transition() const
45 return this->transition_;
51 /* Search an enabled transition for the given process.
53 * This can be seen as an iterator returning the next transition of the process.
55 * We only consider the processes that are both
56 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
57 * - which simcall can currently be executed (like a comm where the other partner is already known)
58 * Once we returned the last enabled transition of a process, it is marked done.
60 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
61 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
63 static inline smx_simcall_t MC_state_get_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
65 /* reset the outgoing transition */
66 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
67 state->transition_.pid_ = -1;
68 state->transition_.argument_ = -1;
69 state->executed_req_.call_ = SIMCALL_NONE;
71 if (not simgrid::mc::actor_is_enabled(actor))
72 return nullptr; // Not executable in the application
74 smx_simcall_t req = nullptr;
75 switch (actor->simcall.call_) {
76 case SIMCALL_COMM_WAITANY:
77 state->transition_.argument_ = -1;
78 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall)) {
79 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered)) {
80 state->transition_.argument_ = procstate->times_considered;
81 ++procstate->times_considered;
84 ++procstate->times_considered;
87 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall))
88 procstate->set_done();
89 if (state->transition_.argument_ != -1)
90 req = &actor->simcall;
93 case SIMCALL_COMM_TESTANY: {
94 unsigned start_count = procstate->times_considered;
95 state->transition_.argument_ = -1;
96 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall)) {
97 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered)) {
98 state->transition_.argument_ = procstate->times_considered;
99 ++procstate->times_considered;
102 ++procstate->times_considered;
105 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall))
106 procstate->set_done();
108 if (state->transition_.argument_ != -1 || start_count == 0)
109 req = &actor->simcall;
114 case SIMCALL_COMM_WAIT: {
115 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
116 remote(static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(&actor->simcall)));
117 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
118 mc_model_checker->process().read(temp_act, remote_act);
119 simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
120 if (act->src_actor_.get() && act->dst_actor_.get())
121 state->transition_.argument_ = 0; // OK
122 else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
124 state->transition_.argument_ = 0; // OK
126 state->transition_.argument_ = -1; // timeout
127 procstate->set_done();
128 req = &actor->simcall;
132 case SIMCALL_MC_RANDOM: {
133 int min_value = simcall_mc_random__get__min(&actor->simcall);
134 state->transition_.argument_ = procstate->times_considered + min_value;
135 procstate->times_considered++;
136 if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall))
137 procstate->set_done();
138 req = &actor->simcall;
143 procstate->set_done();
144 state->transition_.argument_ = 0;
145 req = &actor->simcall;
151 state->transition_.pid_ = actor->get_pid();
152 state->executed_req_ = *req;
153 // Fetch the data of the request and translate it:
154 state->internal_req = *req;
156 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
157 * action so it can be treated later by the dependence function. */
158 switch (req->call_) {
159 case SIMCALL_COMM_WAITANY: {
160 state->internal_req.call_ = SIMCALL_COMM_WAIT;
161 simgrid::kernel::activity::CommImpl* remote_comm;
162 remote_comm = mc_model_checker->process().read(
163 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
164 mc_model_checker->process().read(state->internal_comm, remote(remote_comm));
165 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.get_buffer());
166 simcall_comm_wait__set__timeout(&state->internal_req, 0);
170 case SIMCALL_COMM_TESTANY:
171 state->internal_req.call_ = SIMCALL_COMM_TEST;
173 if (state->transition_.argument_ > 0) {
174 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->process().read(
175 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
176 mc_model_checker->process().read(state->internal_comm, remote(remote_comm));
179 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.get_buffer());
180 simcall_comm_test__set__result(&state->internal_req, state->transition_.argument_);
183 case SIMCALL_COMM_WAIT:
184 mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
185 remote(simcall_comm_wait__getraw__comm(req)));
186 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm.get_buffer());
187 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.get_buffer());
190 case SIMCALL_COMM_TEST:
191 mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
192 remote(simcall_comm_test__getraw__comm(req)));
193 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm.get_buffer());
194 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.get_buffer());
198 /* No translation needed */
205 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
207 for (auto& actor : mc_model_checker->process().actors()) {
208 /* Only consider the actors that were marked as interleaving by the checker algorithm */
209 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
212 smx_simcall_t res = MC_state_get_request_for_process(state, actor.copy.get_buffer());