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. */
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 std::memset(&this->internal_comm, 0, sizeof(this->internal_comm));
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 std::count_if(this->processStates.begin(), this->processStates.end(),
60 [](simgrid::mc::ProcessState const& state) { return state.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 smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
131 mc_model_checker->process().read_bytes(
132 &act, sizeof(act), remote(remote_act));
133 if (act.comm.src_proc && act.comm.dst_proc)
134 state->transition.argument = 0;
135 else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
136 && act.comm.detached == 1)
137 state->transition.argument = 0;
139 state->transition.argument = -1;
140 procstate->setDone();
141 req = &process->simcall;
145 case SIMCALL_MC_RANDOM: {
146 int min_value = simcall_mc_random__get__min(&process->simcall);
147 state->transition.argument = procstate->interleave_count + min_value;
148 procstate->interleave_count++;
149 if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
150 procstate->setDone();
151 req = &process->simcall;
156 procstate->setDone();
157 state->transition.argument = 0;
158 req = &process->simcall;
164 // Fetch the data of the request and translate it:
166 state->transition.pid = process->pid;
168 state->executed_req = *req;
170 /* The waitany and testany request are transformed into a wait or test request
171 * over the corresponding communication action so it can be treated later by
172 * the dependence function. */
174 case SIMCALL_COMM_WAITANY: {
175 state->internal_req.call = SIMCALL_COMM_WAIT;
176 state->internal_req.issuer = req->issuer;
177 smx_synchro_t remote_comm;
178 read_element(mc_model_checker->process(),
179 &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
180 state->transition.argument, sizeof(remote_comm));
181 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
182 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
183 simcall_comm_wait__set__timeout(&state->internal_req, 0);
187 case SIMCALL_COMM_TESTANY:
188 state->internal_req.call = SIMCALL_COMM_TEST;
189 state->internal_req.issuer = req->issuer;
191 if (state->transition.argument > 0) {
192 smx_synchro_t remote_comm;
193 read_element(mc_model_checker->process(),
194 &remote_comm, remote(simcall_comm_testany__get__comms(req)),
195 state->transition.argument, sizeof(remote_comm));
196 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
199 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
200 simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
203 case SIMCALL_COMM_WAIT:
204 state->internal_req = *req;
205 mc_model_checker->process().read_bytes(&state->internal_comm ,
206 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
207 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
208 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
211 case SIMCALL_COMM_TEST:
212 state->internal_req = *req;
213 mc_model_checker->process().read_bytes(&state->internal_comm,
214 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
215 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
216 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
220 state->internal_req = *req;
227 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
229 for (auto& p : mc_model_checker->process().simix_processes()) {
230 smx_simcall_t res = MC_state_get_request_for_process(state, &p.copy);