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"
22 using simgrid::mc::remote;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
25 "Logging specific to MC (state)");
28 * \brief Creates a state data structure used by the exploration algorithm
30 simgrid::mc::State* MC_state_new()
32 simgrid::mc::State* state = new simgrid::mc::State();
33 state->processStates.resize(MC_smx_get_maxpid());
34 state->num = ++mc_stats->expanded_states;
35 /* Stateful model checking */
36 if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) || _sg_mc_termination){
37 state->system_state = simgrid::mc::take_snapshot(state->num);
38 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
39 MC_state_copy_incomplete_communications_pattern(state);
40 MC_state_copy_index_communications_pattern(state);
51 std::memset(&this->internal_comm, 0, sizeof(this->internal_comm));
52 std::memset(&this->internal_req, 0, sizeof(this->internal_req));
53 std::memset(&this->executed_req, 0, sizeof(this->executed_req));
58 xbt_free(this->incomplete_comm_pattern);
61 std::size_t State::interleaveSize() const
63 return std::count_if(this->processStates.begin(), this->processStates.end(),
64 [](simgrid::mc::ProcessState const& state) { return state.interleave(); });
70 void MC_state_interleave_process(simgrid::mc::State* state, smx_process_t process)
73 state->processStates[process->pid].state = simgrid::mc::ProcessInterleaveState::interleave;
74 state->processStates[process->pid].interleave_count = 0;
77 void MC_state_remove_interleave_process(simgrid::mc::State* state, smx_process_t process)
79 if (state->processStates[process->pid].state == simgrid::mc::ProcessInterleaveState::interleave)
80 state->processStates[process->pid].state = simgrid::mc::ProcessInterleaveState::done;
83 void MC_state_set_executed_request(simgrid::mc::State* state, smx_simcall_t req,
86 state->executed_req = *req;
87 state->req_num = value;
89 /* The waitany and testany request are transformed into a wait or test request over the
90 * corresponding communication action so it can be treated later by the dependence
93 case SIMCALL_COMM_WAITANY: {
94 state->internal_req.call = SIMCALL_COMM_WAIT;
95 state->internal_req.issuer = req->issuer;
96 smx_synchro_t remote_comm;
97 read_element(mc_model_checker->process(),
98 &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
99 value, sizeof(remote_comm));
100 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
101 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
102 simcall_comm_wait__set__timeout(&state->internal_req, 0);
106 case SIMCALL_COMM_TESTANY:
107 state->internal_req.call = SIMCALL_COMM_TEST;
108 state->internal_req.issuer = req->issuer;
111 smx_synchro_t remote_comm;
112 read_element(mc_model_checker->process(),
113 &remote_comm, remote(simcall_comm_testany__get__comms(req)),
114 value, sizeof(remote_comm));
115 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
118 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
119 simcall_comm_test__set__result(&state->internal_req, value);
122 case SIMCALL_COMM_WAIT:
123 state->internal_req = *req;
124 mc_model_checker->process().read_bytes(&state->internal_comm ,
125 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
126 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
127 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
130 case SIMCALL_COMM_TEST:
131 state->internal_req = *req;
132 mc_model_checker->process().read_bytes(&state->internal_comm,
133 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
134 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
135 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
138 case SIMCALL_MC_RANDOM: {
139 state->internal_req = *req;
140 int random_max = simcall_mc_random__get__max(req);
141 if (value != random_max)
142 for (auto& p : mc_model_checker->process().simix_processes()) {
143 simgrid::mc::ProcessState* procstate = &state->processStates[p.copy.pid];
144 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
145 if (p.copy.pid == issuer->pid) {
146 procstate->state = simgrid::mc::ProcessInterleaveState::more_interleave;
154 state->internal_req = *req;
159 smx_simcall_t MC_state_get_executed_request(simgrid::mc::State* state, int *value)
161 *value = state->req_num;
162 return &state->executed_req;
165 smx_simcall_t MC_state_get_internal_request(simgrid::mc::State* state)
167 return &state->internal_req;
170 static inline smx_simcall_t MC_state_get_request_for_process(
171 simgrid::mc::State* state, int*value, smx_process_t process)
173 simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
175 if (!procstate->interleave())
177 if (!simgrid::mc::process_is_enabled(process))
180 switch (process->simcall.call) {
182 case SIMCALL_COMM_WAITANY:
184 while (procstate->interleave_count <
185 read_length(mc_model_checker->process(),
186 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
187 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
188 procstate->interleave_count++)) {
189 *value = procstate->interleave_count - 1;
194 if (procstate->interleave_count >=
195 simgrid::mc::read_length(mc_model_checker->process(),
196 simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
197 procstate->state = simgrid::mc::ProcessInterleaveState::done;
200 return &process->simcall;
204 case SIMCALL_COMM_TESTANY: {
205 unsigned start_count = procstate->interleave_count;
207 while (procstate->interleave_count <
208 read_length(mc_model_checker->process(),
209 remote(simcall_comm_testany__get__comms(&process->simcall))))
210 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
211 procstate->interleave_count++)) {
212 *value = procstate->interleave_count - 1;
216 if (procstate->interleave_count >=
217 read_length(mc_model_checker->process(),
218 remote(simcall_comm_testany__get__comms(&process->simcall))))
219 procstate->state = simgrid::mc::ProcessInterleaveState::done;
221 if (*value != -1 || start_count == 0)
222 return &process->simcall;
227 case SIMCALL_COMM_WAIT: {
228 smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
230 mc_model_checker->process().read_bytes(
231 &act, sizeof(act), remote(remote_act));
232 if (act.comm.src_proc && act.comm.dst_proc)
234 else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
235 && act.comm.detached == 1)
239 procstate->state = simgrid::mc::ProcessInterleaveState::done;
240 return &process->simcall;
243 case SIMCALL_MC_RANDOM:
244 if (procstate->state == simgrid::mc::ProcessInterleaveState::interleave)
245 *value = simcall_mc_random__get__min(&process->simcall);
246 else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
247 *value = state->req_num + 1;
248 procstate->state = simgrid::mc::ProcessInterleaveState::done;
249 return &process->simcall;
252 procstate->state = simgrid::mc::ProcessInterleaveState::done;
254 return &process->simcall;
259 smx_simcall_t MC_state_get_request(simgrid::mc::State* state, int *value)
261 for (auto& p : mc_model_checker->process().simix_processes()) {
262 smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);