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)");
28 * \brief Creates a state data structure used by the exploration algorithm
30 mc_state_t MC_state_new()
32 mc_state_t state = xbt_new0(s_mc_state_t, 1);
34 state->max_pid = MC_smx_get_maxpid();
35 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
36 state->system_state = nullptr;
37 state->num = ++mc_stats->expanded_states;
38 state->in_visited_states = 0;
39 state->incomplete_comm_pattern = nullptr;
40 /* Stateful model checking */
41 if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) || _sg_mc_termination){
42 state->system_state = simgrid::mc::take_snapshot(state->num);
43 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
44 MC_state_copy_incomplete_communications_pattern(state);
45 MC_state_copy_index_communications_pattern(state);
52 * \brief Deletes a state data structure
53 * \param trans The state to be deleted
55 void MC_state_delete(mc_state_t state, int free_snapshot){
56 if (state->system_state && free_snapshot)
57 delete state->system_state;
58 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
59 xbt_free(state->index_comm);
60 xbt_free(state->incomplete_comm_pattern);
62 xbt_free(state->proc_status);
66 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
69 state->proc_status[process->pid].state = MC_INTERLEAVE;
70 state->proc_status[process->pid].interleave_count = 0;
73 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
75 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
76 state->proc_status[process->pid].state = MC_DONE;
79 unsigned int MC_state_interleave_size(mc_state_t state)
81 unsigned int i, size = 0;
82 for (i = 0; i < state->max_pid; i++)
83 if ((state->proc_status[i].state == MC_INTERLEAVE)
84 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
89 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
91 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
94 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
97 state->executed_req = *req;
98 state->req_num = value;
100 /* The waitany and testany request are transformed into a wait or test request over the
101 * corresponding communication action so it can be treated later by the dependence
104 case SIMCALL_COMM_WAITANY: {
105 state->internal_req.call = SIMCALL_COMM_WAIT;
106 state->internal_req.issuer = req->issuer;
107 smx_synchro_t remote_comm;
108 read_element(mc_model_checker->process(),
109 &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
110 value, sizeof(remote_comm));
111 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
112 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
113 simcall_comm_wait__set__timeout(&state->internal_req, 0);
117 case SIMCALL_COMM_TESTANY:
118 state->internal_req.call = SIMCALL_COMM_TEST;
119 state->internal_req.issuer = req->issuer;
122 smx_synchro_t remote_comm;
123 read_element(mc_model_checker->process(),
124 &remote_comm, remote(simcall_comm_testany__get__comms(req)),
125 value, sizeof(remote_comm));
126 mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
129 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
130 simcall_comm_test__set__result(&state->internal_req, value);
133 case SIMCALL_COMM_WAIT:
134 state->internal_req = *req;
135 mc_model_checker->process().read_bytes(&state->internal_comm ,
136 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
137 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
138 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
141 case SIMCALL_COMM_TEST:
142 state->internal_req = *req;
143 mc_model_checker->process().read_bytes(&state->internal_comm,
144 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
145 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
146 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
149 case SIMCALL_MC_RANDOM: {
150 state->internal_req = *req;
151 int random_max = simcall_mc_random__get__max(req);
152 if (value != random_max)
153 for (auto& p : mc_model_checker->process().simix_processes()) {
154 mc_procstate_t procstate = &state->proc_status[p.copy.pid];
155 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
156 if (p.copy.pid == issuer->pid) {
157 procstate->state = MC_MORE_INTERLEAVE;
165 state->internal_req = *req;
170 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
172 *value = state->req_num;
173 return &state->executed_req;
176 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
178 return &state->internal_req;
181 static inline smx_simcall_t MC_state_get_request_for_process(
182 mc_state_t state, int*value, smx_process_t process)
184 mc_procstate_t procstate = &state->proc_status[process->pid];
186 if (procstate->state != MC_INTERLEAVE
187 && procstate->state != MC_MORE_INTERLEAVE)
189 if (!simgrid::mc::process_is_enabled(process))
192 switch (process->simcall.call) {
194 case SIMCALL_COMM_WAITANY:
196 while (procstate->interleave_count <
197 read_length(mc_model_checker->process(),
198 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
199 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
200 procstate->interleave_count++)) {
201 *value = procstate->interleave_count - 1;
206 if (procstate->interleave_count >=
207 simgrid::mc::read_length(mc_model_checker->process(),
208 simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
209 procstate->state = MC_DONE;
212 return &process->simcall;
216 case SIMCALL_COMM_TESTANY: {
217 unsigned start_count = procstate->interleave_count;
219 while (procstate->interleave_count <
220 read_length(mc_model_checker->process(),
221 remote(simcall_comm_testany__get__comms(&process->simcall))))
222 if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
223 procstate->interleave_count++)) {
224 *value = procstate->interleave_count - 1;
228 if (procstate->interleave_count >=
229 read_length(mc_model_checker->process(),
230 remote(simcall_comm_testany__get__comms(&process->simcall))))
231 procstate->state = MC_DONE;
233 if (*value != -1 || start_count == 0)
234 return &process->simcall;
239 case SIMCALL_COMM_WAIT: {
240 smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
242 mc_model_checker->process().read_bytes(
243 &act, sizeof(act), remote(remote_act));
244 if (act.comm.src_proc && act.comm.dst_proc)
246 else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
247 && act.comm.detached == 1)
251 procstate->state = MC_DONE;
252 return &process->simcall;
255 case SIMCALL_MC_RANDOM:
256 if (procstate->state == MC_INTERLEAVE)
257 *value = simcall_mc_random__get__min(&process->simcall);
258 else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
259 *value = state->req_num + 1;
260 procstate->state = MC_DONE;
261 return &process->simcall;
264 procstate->state = MC_DONE;
266 return &process->simcall;
271 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
273 for (auto& p : mc_model_checker->process().simix_processes()) {
274 smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);