1 /* Copyright (c) 2008-2014. 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 "../simix/smx_private.h"
12 #include "mc_request.h"
13 #include "mc_private.h"
14 #include "mc_comm_pattern.h"
18 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 mc_state_t MC_state_new()
30 mc_state_t state = xbt_new0(s_mc_state_t, 1);
32 state->max_pid = MC_smx_get_maxpid();
33 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
34 state->system_state = NULL;
35 state->num = ++mc_stats->expanded_states;
36 state->in_visited_states = 0;
37 state->incomplete_comm_pattern = NULL;
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 = 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);
50 * \brief Deletes a state data structure
51 * \param trans The state to be deleted
53 void MC_state_delete(mc_state_t state, int free_snapshot){
54 if (state->system_state && free_snapshot){
55 delete state->system_state;
57 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
58 xbt_free(state->index_comm);
59 xbt_free(state->incomplete_comm_pattern);
61 xbt_free(state->proc_status);
65 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
68 state->proc_status[process->pid].state = MC_INTERLEAVE;
69 state->proc_status[process->pid].interleave_count = 0;
72 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
74 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
75 state->proc_status[process->pid].state = MC_DONE;
78 unsigned int MC_state_interleave_size(mc_state_t state)
80 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))
91 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
93 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
96 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
99 state->executed_req = *req;
100 state->req_num = value;
102 smx_process_t process = NULL;
104 /* The waitany and testany request are transformed into a wait or test request over the
105 * corresponding communication action so it can be treated later by the dependence
108 case SIMCALL_COMM_WAITANY:
109 state->internal_req.call = SIMCALL_COMM_WAIT;
110 state->internal_req.issuer = req->issuer;
111 read_element(mc_model_checker->process(),
112 &state->internal_comm, remote(simcall_comm_waitany__get__comms(req)),
113 value, sizeof(state->internal_comm));
114 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
115 simcall_comm_wait__set__timeout(&state->internal_req, 0);
118 case SIMCALL_COMM_TESTANY:
119 state->internal_req.call = SIMCALL_COMM_TEST;
120 state->internal_req.issuer = req->issuer;
123 read_element(mc_model_checker->process(),
124 &state->internal_comm, remote(simcall_comm_testany__get__comms(req)),
125 value, sizeof(state->internal_comm));
127 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
128 simcall_comm_test__set__result(&state->internal_req, value);
131 case SIMCALL_COMM_WAIT:
132 state->internal_req = *req;
133 mc_model_checker->process().read_bytes(&state->internal_comm ,
134 sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
135 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
136 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
139 case SIMCALL_COMM_TEST:
140 state->internal_req = *req;
141 mc_model_checker->process().read_bytes(&state->internal_comm,
142 sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
143 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
144 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
147 case SIMCALL_MC_RANDOM: {
148 state->internal_req = *req;
149 int random_max = simcall_mc_random__get__max(req);
150 if (value != random_max) {
151 MC_EACH_SIMIX_PROCESS(process,
152 mc_procstate_t procstate = &state->proc_status[process->pid];
153 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
154 if (process->pid == issuer->pid) {
155 procstate->state = MC_MORE_INTERLEAVE;
164 state->internal_req = *req;
169 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
171 *value = state->req_num;
172 return &state->executed_req;
175 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
177 return &state->internal_req;
180 static inline smx_simcall_t MC_state_get_request_for_process(
181 mc_state_t state, int*value, smx_process_t process)
183 mc_procstate_t procstate = &state->proc_status[process->pid];
185 if (procstate->state != MC_INTERLEAVE
186 && procstate->state != MC_MORE_INTERLEAVE)
188 if (!MC_process_is_enabled(process))
191 switch (process->simcall.call) {
193 case SIMCALL_COMM_WAITANY:
195 while (procstate->interleave_count <
196 read_length(mc_model_checker->process(),
197 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
198 if (MC_request_is_enabled_by_idx
199 (&process->simcall, procstate->interleave_count++)) {
200 *value = procstate->interleave_count - 1;
205 if (procstate->interleave_count >=
206 simgrid::mc::read_length(mc_model_checker->process(),
207 simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
208 procstate->state = MC_DONE;
211 return &process->simcall;
215 case SIMCALL_COMM_TESTANY: {
216 unsigned start_count = procstate->interleave_count;
218 while (procstate->interleave_count <
219 read_length(mc_model_checker->process(),
220 remote(simcall_comm_testany__get__comms(&process->simcall)))) {
221 if (MC_request_is_enabled_by_idx
222 (&process->simcall, procstate->interleave_count++)) {
223 *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) {
247 if (act.comm.src_proc == NULL && act.comm.type == SIMIX_COMM_READY
248 && act.comm.detached == 1)
253 procstate->state = MC_DONE;
254 return &process->simcall;
257 case SIMCALL_MC_RANDOM:
258 if (procstate->state == MC_INTERLEAVE)
259 *value = simcall_mc_random__get__min(&process->simcall);
261 if (state->req_num < simcall_mc_random__get__max(&process->simcall))
262 *value = state->req_num + 1;
264 procstate->state = MC_DONE;
265 return &process->simcall;
268 procstate->state = MC_DONE;
270 return &process->simcall;
275 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
277 smx_process_t process = NULL;
278 MC_EACH_SIMIX_PROCESS(process,
279 smx_simcall_t res = MC_state_get_request_for_process(state, value, process);