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. */
10 #include <xbt/sysdep.h>
12 #include "src/simix/smx_private.h"
13 #include "src/mc/mc_state.h"
14 #include "src/mc/mc_request.h"
15 #include "src/mc/mc_private.h"
16 #include "src/mc/mc_comm_pattern.h"
17 #include "src/mc/mc_smx.h"
18 #include "src/mc/mc_xbt.hpp"
20 using simgrid::mc::remote;
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
23 "Logging specific to MC (state)");
26 * \brief Creates a state data structure used by the exploration algorithm
28 simgrid::mc::State* MC_state_new()
30 simgrid::mc::State* state = new simgrid::mc::State();
31 std::memset(&state->internal_comm, 0, sizeof(state->internal_comm));
32 std::memset(&state->internal_req, 0, sizeof(state->internal_req));
33 std::memset(&state->executed_req, 0, sizeof(state->executed_req));
35 state->max_pid = MC_smx_get_maxpid();
36 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
37 state->num = ++mc_stats->expanded_states;
38 /* Stateful model checking */
39 if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) || _sg_mc_termination){
40 state->system_state = simgrid::mc::take_snapshot(state->num);
41 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
42 MC_state_copy_incomplete_communications_pattern(state);
43 MC_state_copy_index_communications_pattern(state);
54 std::memset(&this->internal_comm, 0, sizeof(this->internal_comm));
55 std::memset(&this->internal_req, 0, sizeof(this->internal_req));
56 std::memset(&this->executed_req, 0, sizeof(this->executed_req));
61 xbt_free(this->index_comm);
62 xbt_free(this->incomplete_comm_pattern);
63 xbt_free(this->proc_status);
70 * \brief Deletes a state data structure
71 * \param trans The state to be deleted
73 void MC_state_delete(simgrid::mc::State* state, int free_snapshot)
78 void MC_state_interleave_process(simgrid::mc::State* state, smx_process_t process)
81 state->proc_status[process->pid].state = MC_INTERLEAVE;
82 state->proc_status[process->pid].interleave_count = 0;
85 void MC_state_remove_interleave_process(simgrid::mc::State* state, smx_process_t process)
87 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
88 state->proc_status[process->pid].state = MC_DONE;
91 unsigned int MC_state_interleave_size(simgrid::mc::State* state)
93 unsigned int i, size = 0;
94 for (i = 0; i < state->max_pid; i++)
95 if ((state->proc_status[i].state == MC_INTERLEAVE)
96 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
101 int MC_state_process_is_done(simgrid::mc::State* state, smx_process_t process)
103 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
106 void MC_state_set_executed_request(simgrid::mc::State* state, smx_simcall_t req,
109 state->executed_req = *req;
110 state->req_num = value;
112 /* The waitany and testany request are transformed into a wait or test request over the
113 * corresponding communication action so it can be treated later by the dependence
116 case SIMCALL_COMM_WAITANY: {
117 state->internal_req.call = SIMCALL_COMM_WAIT;
118 state->internal_req.issuer = req->issuer;
119 smx_synchro_t remote_comm;
120 read_element(mc_model_checker->process(),
121 &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
122 value, sizeof(remote_comm));
123 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
124 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
125 simcall_comm_wait__set__timeout(&state->internal_req, 0);
129 case SIMCALL_COMM_TESTANY:
130 state->internal_req.call = SIMCALL_COMM_TEST;
131 state->internal_req.issuer = req->issuer;
134 smx_synchro_t remote_comm;
135 read_element(mc_model_checker->process(),
136 &remote_comm, remote(simcall_comm_testany__get__comms(req)),
137 value, sizeof(remote_comm));
138 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
141 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
142 simcall_comm_test__set__result(&state->internal_req, value);
145 case SIMCALL_COMM_WAIT:
146 state->internal_req = *req;
147 mc_model_checker->process().read_bytes(&state->internal_comm ,
148 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
149 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
150 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
153 case SIMCALL_COMM_TEST:
154 state->internal_req = *req;
155 mc_model_checker->process().read_bytes(&state->internal_comm,
156 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
157 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
158 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
161 case SIMCALL_MC_RANDOM: {
162 state->internal_req = *req;
163 int random_max = simcall_mc_random__get__max(req);
164 if (value != random_max)
165 for (auto& p : mc_model_checker->process().simix_processes()) {
166 mc_procstate_t procstate = &state->proc_status[p.copy.pid];
167 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
168 if (p.copy.pid == issuer->pid) {
169 procstate->state = MC_MORE_INTERLEAVE;
177 state->internal_req = *req;
182 smx_simcall_t MC_state_get_executed_request(simgrid::mc::State* state, int *value)
184 *value = state->req_num;
185 return &state->executed_req;
188 smx_simcall_t MC_state_get_internal_request(simgrid::mc::State* state)
190 return &state->internal_req;
193 static inline smx_simcall_t MC_state_get_request_for_process(
194 simgrid::mc::State* state, int*value, smx_process_t process)
196 mc_procstate_t procstate = &state->proc_status[process->pid];
198 if (procstate->state != MC_INTERLEAVE
199 && procstate->state != MC_MORE_INTERLEAVE)
201 if (!simgrid::mc::process_is_enabled(process))
204 switch (process->simcall.call) {
206 case SIMCALL_COMM_WAITANY:
208 while (procstate->interleave_count <
209 read_length(mc_model_checker->process(),
210 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
211 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
212 procstate->interleave_count++)) {
213 *value = procstate->interleave_count - 1;
218 if (procstate->interleave_count >=
219 simgrid::mc::read_length(mc_model_checker->process(),
220 simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
221 procstate->state = MC_DONE;
224 return &process->simcall;
228 case SIMCALL_COMM_TESTANY: {
229 unsigned start_count = procstate->interleave_count;
231 while (procstate->interleave_count <
232 read_length(mc_model_checker->process(),
233 remote(simcall_comm_testany__get__comms(&process->simcall))))
234 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
235 procstate->interleave_count++)) {
236 *value = procstate->interleave_count - 1;
240 if (procstate->interleave_count >=
241 read_length(mc_model_checker->process(),
242 remote(simcall_comm_testany__get__comms(&process->simcall))))
243 procstate->state = MC_DONE;
245 if (*value != -1 || start_count == 0)
246 return &process->simcall;
251 case SIMCALL_COMM_WAIT: {
252 smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
254 mc_model_checker->process().read_bytes(
255 &act, sizeof(act), remote(remote_act));
256 if (act.comm.src_proc && act.comm.dst_proc)
258 else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
259 && act.comm.detached == 1)
263 procstate->state = MC_DONE;
264 return &process->simcall;
267 case SIMCALL_MC_RANDOM:
268 if (procstate->state == MC_INTERLEAVE)
269 *value = simcall_mc_random__get__min(&process->simcall);
270 else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
271 *value = state->req_num + 1;
272 procstate->state = MC_DONE;
273 return &process->simcall;
276 procstate->state = MC_DONE;
278 return &process->simcall;
283 smx_simcall_t MC_state_get_request(simgrid::mc::State* state, int *value)
285 for (auto& p : mc_model_checker->process().simix_processes()) {
286 smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);