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"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
20 "Logging specific to MC (state)");
23 * \brief Creates a state data structure used by the exploration algorithm
25 mc_state_t MC_state_new()
27 mc_state_t state = xbt_new0(s_mc_state_t, 1);
29 state->max_pid = MC_smx_get_maxpid();
30 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
31 state->system_state = NULL;
32 state->num = ++mc_stats->expanded_states;
33 state->in_visited_states = 0;
34 state->incomplete_comm_pattern = NULL;
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 = 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);
47 * \brief Deletes a state data structure
48 * \param trans The state to be deleted
50 void MC_state_delete(mc_state_t state, int free_snapshot){
51 if (state->system_state && free_snapshot){
52 MC_free_snapshot(state->system_state);
54 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
55 xbt_free(state->index_comm);
56 xbt_free(state->incomplete_comm_pattern);
58 xbt_free(state->proc_status);
62 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
65 state->proc_status[process->pid].state = MC_INTERLEAVE;
66 state->proc_status[process->pid].interleave_count = 0;
69 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
71 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
72 state->proc_status[process->pid].state = MC_DONE;
75 unsigned int MC_state_interleave_size(mc_state_t state)
77 unsigned int i, size = 0;
79 for (i = 0; i < state->max_pid; i++) {
80 if ((state->proc_status[i].state == MC_INTERLEAVE)
81 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
88 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
90 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
93 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
96 state->executed_req = *req;
97 state->req_num = value;
99 smx_process_t process = NULL;
101 /* The waitany and testany request are transformed into a wait or test request over the
102 * corresponding communication action so it can be treated later by the dependence
105 case SIMCALL_COMM_WAITANY:
106 state->internal_req.call = SIMCALL_COMM_WAIT;
107 state->internal_req.issuer = req->issuer;
108 MC_process_read_dynar_element(&mc_model_checker->process,
109 &state->internal_comm, simcall_comm_waitany__get__comms(req),
110 value, sizeof(state->internal_comm));
111 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
112 simcall_comm_wait__set__timeout(&state->internal_req, 0);
115 case SIMCALL_COMM_TESTANY:
116 state->internal_req.call = SIMCALL_COMM_TEST;
117 state->internal_req.issuer = req->issuer;
120 MC_process_read_dynar_element(&mc_model_checker->process,
121 &state->internal_comm, simcall_comm_testany__get__comms(req),
122 value, sizeof(state->internal_comm));
124 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
125 simcall_comm_test__set__result(&state->internal_req, value);
128 case SIMCALL_COMM_WAIT:
129 state->internal_req = *req;
130 MC_process_read_simple(&mc_model_checker->process, &state->internal_comm ,
131 simcall_comm_wait__get__comm(req), sizeof(state->internal_comm));
132 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
133 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
136 case SIMCALL_COMM_TEST:
137 state->internal_req = *req;
138 MC_process_read_simple(&mc_model_checker->process, &state->internal_comm,
139 simcall_comm_test__get__comm(req), sizeof(state->internal_comm));
140 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
141 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
144 case SIMCALL_MC_RANDOM: {
145 state->internal_req = *req;
146 int random_max = simcall_mc_random__get__max(req);
147 if (value != random_max) {
148 MC_EACH_SIMIX_PROCESS(process,
149 mc_procstate_t procstate = &state->proc_status[process->pid];
150 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
151 if (process->pid == issuer->pid) {
152 procstate->state = MC_MORE_INTERLEAVE;
161 state->internal_req = *req;
166 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
168 *value = state->req_num;
169 return &state->executed_req;
172 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
174 return &state->internal_req;
177 static inline smx_simcall_t MC_state_get_request_for_process(
178 mc_state_t state, int*value, smx_process_t process)
180 mc_procstate_t procstate = &state->proc_status[process->pid];
182 if (procstate->state != MC_INTERLEAVE
183 && procstate->state != MC_MORE_INTERLEAVE)
185 if (!MC_process_is_enabled(process))
188 switch (process->simcall.call) {
190 case SIMCALL_COMM_WAITANY:
192 while (procstate->interleave_count <
193 MC_process_read_dynar_length(&mc_model_checker->process,
194 simcall_comm_waitany__get__comms(&process->simcall))) {
195 if (MC_request_is_enabled_by_idx
196 (&process->simcall, procstate->interleave_count++)) {
197 *value = procstate->interleave_count - 1;
202 if (procstate->interleave_count >=
203 MC_process_read_dynar_length(&mc_model_checker->process,
204 simcall_comm_waitany__get__comms(&process->simcall)))
205 procstate->state = MC_DONE;
208 return &process->simcall;
212 case SIMCALL_COMM_TESTANY: {
213 unsigned start_count = procstate->interleave_count;
215 while (procstate->interleave_count <
216 MC_process_read_dynar_length(&mc_model_checker->process,
217 simcall_comm_testany__get__comms(&process->simcall))) {
218 if (MC_request_is_enabled_by_idx
219 (&process->simcall, procstate->interleave_count++)) {
220 *value = procstate->interleave_count - 1;
225 if (procstate->interleave_count >=
226 MC_process_read_dynar_length(&mc_model_checker->process,
227 simcall_comm_testany__get__comms(&process->simcall)))
228 procstate->state = MC_DONE;
230 if (*value != -1 || start_count == 0)
231 return &process->simcall;
236 case SIMCALL_COMM_WAIT: {
237 smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
239 MC_process_read_simple(&mc_model_checker->process,
240 &act, remote_act, sizeof(act));
241 if (act.comm.src_proc && act.comm.dst_proc) {
244 if (act.comm.src_proc == NULL && act.comm.type == SIMIX_COMM_READY
245 && act.comm.detached == 1)
250 procstate->state = MC_DONE;
251 return &process->simcall;
254 case SIMCALL_MC_RANDOM:
255 if (procstate->state == MC_INTERLEAVE)
256 *value = simcall_mc_random__get__min(&process->simcall);
258 if (state->req_num < simcall_mc_random__get__max(&process->simcall))
259 *value = state->req_num + 1;
261 procstate->state = MC_DONE;
262 return &process->simcall;
265 procstate->state = MC_DONE;
267 return &process->simcall;
272 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
274 smx_process_t process = NULL;
275 MC_EACH_SIMIX_PROCESS(process,
276 smx_simcall_t res = MC_state_get_request_for_process(state, value, process);