1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <boost/range/algorithm.hpp>
12 #include <xbt/sysdep.h>
14 #include "src/simix/smx_private.h"
15 #include "src/mc/mc_state.h"
16 #include "src/mc/mc_request.h"
17 #include "src/mc/mc_private.h"
18 #include "src/mc/mc_comm_pattern.h"
19 #include "src/mc/mc_smx.h"
20 #include "src/mc/mc_xbt.hpp"
21 #include "src/mc/Transition.hpp"
23 using simgrid::mc::remote;
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
26 "Logging specific to MC (state)");
29 * \brief Creates a state data structure used by the exploration algorithm
31 simgrid::mc::State* MC_state_new(unsigned long state_number)
33 simgrid::mc::State* state = new simgrid::mc::State();
34 state->processStates.resize(MC_smx_get_maxpid());
35 state->num = state_number;
36 /* Stateful model checking */
37 if((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination){
38 state->system_state = simgrid::mc::take_snapshot(state->num);
39 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
40 MC_state_copy_incomplete_communications_pattern(state);
41 MC_state_copy_index_communications_pattern(state);
52 this->internal_comm.clear();
53 std::memset(&this->internal_req, 0, sizeof(this->internal_req));
54 std::memset(&this->executed_req, 0, sizeof(this->executed_req));
57 std::size_t State::interleaveSize() const
59 return boost::range::count_if(this->processStates,
60 [](simgrid::mc::ProcessState const& p) { return p.isToInterleave(); });
63 Transition State::getTransition() const
65 return this->transition;
71 static inline smx_simcall_t MC_state_get_request_for_process(
72 simgrid::mc::State* state, smx_process_t process)
74 simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
75 state->transition.pid = -1;
76 state->transition.argument = -1;
78 if (!procstate->isToInterleave())
80 if (!simgrid::mc::process_is_enabled(process))
83 smx_simcall_t req = nullptr;
84 switch (process->simcall.call) {
85 case SIMCALL_COMM_WAITANY:
86 state->transition.argument = -1;
87 while (procstate->interleave_count <
88 read_length(mc_model_checker->process(),
89 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
90 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
91 procstate->interleave_count++)) {
92 state->transition.argument = procstate->interleave_count - 1;
97 if (procstate->interleave_count >=
98 simgrid::mc::read_length(mc_model_checker->process(),
99 simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
100 procstate->setDone();
101 if (state->transition.argument != -1)
102 req = &process->simcall;
105 case SIMCALL_COMM_TESTANY: {
106 unsigned start_count = procstate->interleave_count;
107 state->transition.argument = -1;
108 while (procstate->interleave_count <
109 read_length(mc_model_checker->process(),
110 remote(simcall_comm_testany__get__comms(&process->simcall))))
111 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
112 procstate->interleave_count++)) {
113 state->transition.argument = procstate->interleave_count - 1;
117 if (procstate->interleave_count >=
118 read_length(mc_model_checker->process(),
119 remote(simcall_comm_testany__get__comms(&process->simcall))))
120 procstate->setDone();
122 if (state->transition.argument != -1 || start_count == 0)
123 req = &process->simcall;
128 case SIMCALL_COMM_WAIT: {
129 simgrid::mc::RemotePtr<simgrid::simix::Comm> remote_act = remote(
130 static_cast<simgrid::simix::Comm*>(simcall_comm_wait__get__comm(&process->simcall)));
131 simgrid::mc::Remote<simgrid::simix::Comm> temp_act;
132 mc_model_checker->process().read(temp_act, remote_act);
133 simgrid::simix::Comm* act = temp_act.getBuffer();
134 if (act->src_proc && act->dst_proc)
135 state->transition.argument = 0;
136 else if (act->src_proc == nullptr && act->type == SIMIX_COMM_READY
137 && act->detached == 1)
138 state->transition.argument = 0;
140 state->transition.argument = -1;
141 procstate->setDone();
142 req = &process->simcall;
146 case SIMCALL_MC_RANDOM: {
147 int min_value = simcall_mc_random__get__min(&process->simcall);
148 state->transition.argument = procstate->interleave_count + min_value;
149 procstate->interleave_count++;
150 if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
151 procstate->setDone();
152 req = &process->simcall;
157 procstate->setDone();
158 state->transition.argument = 0;
159 req = &process->simcall;
165 // Fetch the data of the request and translate it:
167 state->transition.pid = process->pid;
169 state->executed_req = *req;
171 /* The waitany and testany request are transformed into a wait or test request
172 * over the corresponding communication action so it can be treated later by
173 * the dependence function. */
175 case SIMCALL_COMM_WAITANY: {
176 state->internal_req.call = SIMCALL_COMM_WAIT;
177 state->internal_req.issuer = req->issuer;
178 smx_synchro_t remote_comm;
179 read_element(mc_model_checker->process(),
180 &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
181 state->transition.argument, sizeof(remote_comm));
182 mc_model_checker->process().read(state->internal_comm, remote(
183 static_cast<simgrid::simix::Comm*>(remote_comm)));
184 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
185 simcall_comm_wait__set__timeout(&state->internal_req, 0);
189 case SIMCALL_COMM_TESTANY:
190 state->internal_req.call = SIMCALL_COMM_TEST;
191 state->internal_req.issuer = req->issuer;
193 if (state->transition.argument > 0) {
194 smx_synchro_t remote_comm;
195 read_element(mc_model_checker->process(),
196 &remote_comm, remote(simcall_comm_testany__get__comms(req)),
197 state->transition.argument, sizeof(remote_comm));
198 mc_model_checker->process().read(state->internal_comm, remote(
199 static_cast<simgrid::simix::Comm*>(remote_comm)));
202 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
203 simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
206 case SIMCALL_COMM_WAIT:
207 state->internal_req = *req;
208 mc_model_checker->process().read_bytes(&state->internal_comm ,
209 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
210 simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
211 simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
214 case SIMCALL_COMM_TEST:
215 state->internal_req = *req;
216 mc_model_checker->process().read_bytes(&state->internal_comm,
217 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
218 simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
219 simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
223 state->internal_req = *req;
230 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
232 for (auto& p : mc_model_checker->process().simix_processes()) {
233 smx_simcall_t res = MC_state_get_request_for_process(state, p.copy.getBuffer());