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"
17 static void copy_incomplete_communications_pattern(mc_state_t state) {
19 xbt_dynar_t incomplete_process_comms;
20 mc_comm_pattern_t comm;
22 state->incomplete_comm_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
23 for (i=0; i < MC_smx_get_maxpid(); i++) {
24 incomplete_process_comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
25 xbt_dynar_t incomplete_process_comms_copy = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
26 xbt_dynar_foreach(incomplete_process_comms, cursor, comm) {
27 mc_comm_pattern_t copy_comm = xbt_new0(s_mc_comm_pattern_t, 1);
28 copy_comm->index = comm->index;
29 copy_comm->type = comm->type;
30 copy_comm->comm = comm->comm;
31 copy_comm->rdv = strdup(comm->rdv);
32 copy_comm->data_size = -1;
33 copy_comm->data = NULL;
34 if(comm->type == SIMIX_COMM_SEND){
35 copy_comm->src_proc = comm->src_proc;
36 copy_comm->src_host = comm->src_host;
37 if (comm->data != NULL) {
38 copy_comm->data_size = comm->data_size;
39 copy_comm->data = xbt_malloc0(comm->data_size);
40 memcpy(copy_comm->data, comm->data, comm->data_size);
43 copy_comm->dst_proc = comm->dst_proc;
44 copy_comm->dst_host = comm->dst_host;
46 xbt_dynar_push(incomplete_process_comms_copy, ©_comm);
48 xbt_dynar_insert_at(state->incomplete_comm_pattern, i, &incomplete_process_comms_copy);
52 static void copy_index_communications_pattern(mc_state_t state) {
54 state->index_comm = xbt_dynar_new(sizeof(unsigned int), NULL);
55 mc_list_comm_pattern_t list_process_comm;
57 xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
58 xbt_dynar_push_as(state->index_comm, unsigned int, list_process_comm->index_comm);
63 * \brief Creates a state data structure used by the exploration algorithm
65 mc_state_t MC_state_new()
67 mc_state_t state = xbt_new0(s_mc_state_t, 1);
68 state->max_pid = MC_smx_get_maxpid();
69 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
70 state->system_state = NULL;
71 state->num = ++mc_stats->expanded_states;
72 state->in_visited_states = 0;
73 state->incomplete_comm_pattern = NULL;
74 /* Stateful model checking */
75 if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) || _sg_mc_termination){
76 state->system_state = MC_take_snapshot(state->num);
77 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
78 copy_incomplete_communications_pattern(state);
79 copy_index_communications_pattern(state);
86 * \brief Deletes a state data structure
87 * \param trans The state to be deleted
89 void MC_state_delete(mc_state_t state, int free_snapshot){
90 if (state->system_state && free_snapshot){
91 MC_free_snapshot(state->system_state);
93 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
94 xbt_free(state->index_comm);
95 xbt_free(state->incomplete_comm_pattern);
97 xbt_free(state->proc_status);
101 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
104 state->proc_status[process->pid].state = MC_INTERLEAVE;
105 state->proc_status[process->pid].interleave_count = 0;
108 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
110 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
111 state->proc_status[process->pid].state = MC_DONE;
114 unsigned int MC_state_interleave_size(mc_state_t state)
116 unsigned int i, size = 0;
118 for (i = 0; i < state->max_pid; i++) {
119 if ((state->proc_status[i].state == MC_INTERLEAVE)
120 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
127 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
129 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
132 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
135 state->executed_req = *req;
136 state->req_num = value;
137 smx_process_t process = NULL;
138 mc_procstate_t procstate = NULL;
140 /* The waitany and testany request are transformed into a wait or test request over the
141 * corresponding communication action so it can be treated later by the dependence
144 case SIMCALL_COMM_WAITANY:
145 state->internal_req.call = SIMCALL_COMM_WAIT;
146 state->internal_req.issuer = req->issuer;
147 // FIXME, read from remote process
148 state->internal_comm =
149 *xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
151 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
152 simcall_comm_wait__set__timeout(&state->internal_req, 0);
155 case SIMCALL_COMM_TESTANY:
156 state->internal_req.call = SIMCALL_COMM_TEST;
157 state->internal_req.issuer = req->issuer;
160 // FIXME, read from remote process
161 state->internal_comm =
162 *xbt_dynar_get_as(simcall_comm_testany__get__comms(req), value,
165 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
166 simcall_comm_test__set__result(&state->internal_req, value);
169 case SIMCALL_COMM_WAIT:
170 state->internal_req = *req;
171 // FIXME, read from remote process
172 state->internal_comm = *(simcall_comm_wait__get__comm(req));
173 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
174 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
177 case SIMCALL_COMM_TEST:
178 state->internal_req = *req;
179 // FIXME, read from remote process
180 state->internal_comm = *simcall_comm_test__get__comm(req);
181 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
182 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
185 case SIMCALL_MC_RANDOM:
186 state->internal_req = *req;
187 int random_max = simcall_mc_random__get__max(req);
188 if (value != random_max) {
189 MC_EACH_SIMIX_PROCESS(process,
190 procstate = &state->proc_status[process->pid];
191 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
192 if (process->pid == issuer->pid) {
193 procstate->state = MC_MORE_INTERLEAVE;
201 state->internal_req = *req;
206 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
208 *value = state->req_num;
209 return &state->executed_req;
212 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
214 return &state->internal_req;
217 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
219 smx_process_t process = NULL;
220 mc_procstate_t procstate = NULL;
221 unsigned int start_count;
222 smx_synchro_t act = NULL;
224 MC_EACH_SIMIX_PROCESS(process,
225 procstate = &state->proc_status[process->pid];
227 if (procstate->state != MC_INTERLEAVE
228 && procstate->state != MC_MORE_INTERLEAVE)
230 if (!MC_process_is_enabled(process))
233 switch (process->simcall.call) {
234 case SIMCALL_COMM_WAITANY:
236 while (procstate->interleave_count <
237 xbt_dynar_length(simcall_comm_waitany__get__comms
238 (&process->simcall))) {
239 if (MC_request_is_enabled_by_idx
240 (&process->simcall, procstate->interleave_count++)) {
241 *value = procstate->interleave_count - 1;
246 if (procstate->interleave_count >=
247 xbt_dynar_length(simcall_comm_waitany__get__comms
248 (&process->simcall)))
249 procstate->state = MC_DONE;
252 return &process->simcall;
256 case SIMCALL_COMM_TESTANY:
257 start_count = procstate->interleave_count;
259 while (procstate->interleave_count <
260 xbt_dynar_length(simcall_comm_testany__get__comms
261 (&process->simcall))) {
262 if (MC_request_is_enabled_by_idx
263 (&process->simcall, procstate->interleave_count++)) {
264 *value = procstate->interleave_count - 1;
269 if (procstate->interleave_count >=
270 xbt_dynar_length(simcall_comm_testany__get__comms
271 (&process->simcall)))
272 procstate->state = MC_DONE;
274 if (*value != -1 || start_count == 0)
275 return &process->simcall;
279 case SIMCALL_COMM_WAIT:
280 act = simcall_comm_wait__get__comm(&process->simcall);
281 if (act->comm.src_proc && act->comm.dst_proc) {
284 if (act->comm.src_proc == NULL && act->comm.type == SIMIX_COMM_READY
285 && act->comm.detached == 1)
290 procstate->state = MC_DONE;
291 return &process->simcall;
295 case SIMCALL_MC_RANDOM:
296 if (procstate->state == MC_INTERLEAVE)
299 if (state->req_num < simcall_mc_random__get__max(&process->simcall))
300 *value = state->req_num + 1;
302 procstate->state = MC_DONE;
303 return &process->simcall;
307 procstate->state = MC_DONE;
309 return &process->simcall;