1 /* Copyright (c) 2008-2017. 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. */
8 #include <boost/range/algorithm.hpp>
11 #include "xbt/sysdep.h"
13 #include "src/mc/Transition.hpp"
14 #include "src/mc/mc_comm_pattern.hpp"
15 #include "src/mc/mc_private.hpp"
16 #include "src/mc/mc_request.hpp"
17 #include "src/mc/mc_smx.hpp"
18 #include "src/mc/mc_state.hpp"
19 #include "src/mc/mc_xbt.hpp"
20 #include "src/simix/smx_private.hpp"
22 using simgrid::mc::remote;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC (state)");
29 State::State(unsigned long state_number) : num(state_number)
31 this->internal_comm.clear();
32 std::memset(&this->internal_req, 0, sizeof(this->internal_req));
33 std::memset(&this->executed_req, 0, sizeof(this->executed_req));
35 actorStates.resize(MC_smx_get_maxpid());
36 /* Stateful model checking */
37 if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
38 system_state = simgrid::mc::take_snapshot(num);
39 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
40 MC_state_copy_incomplete_communications_pattern(this);
41 MC_state_copy_index_communications_pattern(this);
46 std::size_t State::interleaveSize() const
48 return boost::range::count_if(this->actorStates, [](simgrid::mc::ProcessState const& p) { return p.isTodo(); });
51 Transition State::getTransition() const
53 return this->transition;
59 /* Search an enabled transition for the given process.
61 * This can be seen as an iterator returning the next transition of the process.
63 * We only consider the processes that are both
64 * - marked "to be interleaved" in their ProcessState (controlled by the checker algorithm).
65 * - which simcall can currently be executed (like a comm where the other partner is already known)
66 * Once we returned the last enabled transition of a process, it is marked done.
68 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
69 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
71 static inline smx_simcall_t MC_state_get_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
73 /* reset the outgoing transition */
74 simgrid::mc::ProcessState* procstate = &state->actorStates[actor->pid];
75 state->transition.pid = -1;
76 state->transition.argument = -1;
77 state->executed_req.call = SIMCALL_NONE;
79 if (not simgrid::mc::actor_is_enabled(actor))
80 return nullptr; // Not executable in the application
82 smx_simcall_t req = nullptr;
83 switch (actor->simcall.call) {
84 case SIMCALL_COMM_WAITANY:
85 state->transition.argument = -1;
86 while (procstate->times_considered <
87 read_length(mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(&actor->simcall)))) {
88 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
89 state->transition.argument = procstate->times_considered - 1;
94 if (procstate->times_considered >=
95 simgrid::mc::read_length(mc_model_checker->process(),
96 simgrid::mc::remote(simcall_comm_waitany__get__comms(&actor->simcall))))
98 if (state->transition.argument != -1)
99 req = &actor->simcall;
102 case SIMCALL_COMM_TESTANY: {
103 unsigned start_count = procstate->times_considered;
104 state->transition.argument = -1;
105 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall))
106 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
107 state->transition.argument = procstate->times_considered - 1;
111 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall))
112 procstate->setDone();
114 if (state->transition.argument != -1 || start_count == 0)
115 req = &actor->simcall;
120 case SIMCALL_COMM_WAIT: {
121 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
122 remote(static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(&actor->simcall)));
123 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
124 mc_model_checker->process().read(temp_act, remote_act);
125 simgrid::kernel::activity::CommImpl* act = temp_act.getBuffer();
126 if (act->src_proc && act->dst_proc)
127 state->transition.argument = 0;
128 else if (act->src_proc == nullptr && act->type == SIMIX_COMM_READY && act->detached == 1)
129 state->transition.argument = 0;
131 state->transition.argument = -1;
132 procstate->setDone();
133 req = &actor->simcall;
137 case SIMCALL_MC_RANDOM: {
138 int min_value = simcall_mc_random__get__min(&actor->simcall);
139 state->transition.argument = procstate->times_considered + min_value;
140 procstate->times_considered++;
141 if (state->transition.argument == simcall_mc_random__get__max(&actor->simcall))
142 procstate->setDone();
143 req = &actor->simcall;
148 procstate->setDone();
149 state->transition.argument = 0;
150 req = &actor->simcall;
156 state->transition.pid = actor->pid;
157 state->executed_req = *req;
158 // Fetch the data of the request and translate it:
159 state->internal_req = *req;
161 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
162 * action so it can be treated later by the dependence function. */
164 case SIMCALL_COMM_WAITANY: {
165 state->internal_req.call = SIMCALL_COMM_WAIT;
166 simgrid::kernel::activity::ActivityImpl* remote_comm;
167 read_element(mc_model_checker->process(), &remote_comm, remote(simcall_comm_waitany__getraw__comms(req)),
168 state->transition.argument, sizeof(remote_comm));
169 mc_model_checker->process().read(state->internal_comm,
170 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_comm)));
171 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
172 simcall_comm_wait__set__timeout(&state->internal_req, 0);
176 case SIMCALL_COMM_TESTANY:
177 state->internal_req.call = SIMCALL_COMM_TEST;
179 if (state->transition.argument > 0) {
180 simgrid::kernel::activity::ActivityImpl* remote_comm = mc_model_checker->process().read(
181 remote(simcall_comm_testany__getraw__comms(req) + state->transition.argument));
182 mc_model_checker->process().read(state->internal_comm,
183 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_comm)));
186 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
187 simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
190 case SIMCALL_COMM_WAIT:
191 mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
192 remote(simcall_comm_wait__getraw__comm(req)));
193 simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
194 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
197 case SIMCALL_COMM_TEST:
198 mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
199 remote(simcall_comm_test__getraw__comm(req)));
200 simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
201 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
205 /* No translation needed */
212 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
214 for (auto& actor : mc_model_checker->process().actors()) {
215 /* Only consider the actors that were marked as interleaving by the checker algorithm */
216 if (not state->actorStates[actor.copy.getBuffer()->pid].isTodo())
219 smx_simcall_t res = MC_state_get_request_for_process(state, actor.copy.getBuffer());