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. */
7 #include "../simix/smx_private.h"
9 #include "mc_private.h"
12 * \brief Creates a state data structure used by the exploration algorithm
14 mc_state_t MC_state_new()
16 mc_state_t state = NULL;
18 state = xbt_new0(s_mc_state_t, 1);
19 state->max_pid = simix_process_maxpid;
20 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
21 state->system_state = NULL;
22 state->num = ++mc_stats->expanded_states;
28 * \brief Deletes a state data structure
29 * \param trans The state to be deleted
31 void MC_state_delete(mc_state_t state)
33 if (state->system_state)
34 MC_free_snapshot(state->system_state);
35 xbt_free(state->proc_status);
39 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
41 state->proc_status[process->pid].state = MC_INTERLEAVE;
42 state->proc_status[process->pid].interleave_count = 0;
45 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
47 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
48 state->proc_status[process->pid].state = MC_DONE;
51 unsigned int MC_state_interleave_size(mc_state_t state)
53 unsigned int i, size = 0;
55 for (i = 0; i < state->max_pid; i++) {
56 if ((state->proc_status[i].state == MC_INTERLEAVE)
57 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
64 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
66 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
69 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
72 state->executed_req = *req;
73 state->req_num = value;
74 smx_process_t process = NULL;
75 mc_procstate_t procstate = NULL;
77 /* The waitany and testany request are transformed into a wait or test request over the
78 * corresponding communication action so it can be treated later by the dependence
81 case SIMCALL_COMM_WAITANY:
82 state->internal_req.call = SIMCALL_COMM_WAIT;
83 state->internal_req.issuer = req->issuer;
84 state->internal_comm =
85 *xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
87 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
88 simcall_comm_wait__set__timeout(&state->internal_req, 0);
91 case SIMCALL_COMM_TESTANY:
92 state->internal_req.call = SIMCALL_COMM_TEST;
93 state->internal_req.issuer = req->issuer;
96 state->internal_comm =
97 *xbt_dynar_get_as(simcall_comm_testany__get__comms(req), value,
100 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
101 simcall_comm_test__set__result(&state->internal_req, value);
104 case SIMCALL_COMM_WAIT:
105 state->internal_req = *req;
106 state->internal_comm = *(simcall_comm_wait__get__comm(req));
107 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
108 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
111 case SIMCALL_COMM_TEST:
112 state->internal_req = *req;
113 state->internal_comm = *simcall_comm_test__get__comm(req);
114 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
115 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
118 case SIMCALL_MC_RANDOM:
119 state->internal_req = *req;
120 if (value != simcall_mc_random__get__max(req)) {
121 xbt_swag_foreach(process, simix_global->process_list) {
122 procstate = &state->proc_status[process->pid];
123 if (process->pid == req->issuer->pid) {
124 procstate->state = MC_MORE_INTERLEAVE;
132 state->internal_req = *req;
137 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
139 *value = state->req_num;
140 return &state->executed_req;
143 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
145 return &state->internal_req;
148 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
150 smx_process_t process = NULL;
151 mc_procstate_t procstate = NULL;
152 unsigned int start_count;
153 smx_action_t act = NULL;
155 xbt_swag_foreach(process, simix_global->process_list) {
156 procstate = &state->proc_status[process->pid];
158 if (procstate->state == MC_INTERLEAVE
159 || procstate->state == MC_MORE_INTERLEAVE) {
160 if (MC_process_is_enabled(process)) {
161 switch (process->simcall.call) {
162 case SIMCALL_COMM_WAITANY:
164 while (procstate->interleave_count <
165 xbt_dynar_length(simcall_comm_waitany__get__comms
166 (&process->simcall))) {
167 if (MC_request_is_enabled_by_idx
168 (&process->simcall, procstate->interleave_count++)) {
169 *value = procstate->interleave_count - 1;
174 if (procstate->interleave_count >=
175 xbt_dynar_length(simcall_comm_waitany__get__comms
176 (&process->simcall)))
177 procstate->state = MC_DONE;
180 return &process->simcall;
184 case SIMCALL_COMM_TESTANY:
185 start_count = procstate->interleave_count;
187 while (procstate->interleave_count <
188 xbt_dynar_length(simcall_comm_testany__get__comms
189 (&process->simcall))) {
190 if (MC_request_is_enabled_by_idx
191 (&process->simcall, procstate->interleave_count++)) {
192 *value = procstate->interleave_count - 1;
197 if (procstate->interleave_count >=
198 xbt_dynar_length(simcall_comm_testany__get__comms
199 (&process->simcall)))
200 procstate->state = MC_DONE;
202 if (*value != -1 || start_count == 0)
203 return &process->simcall;
207 case SIMCALL_COMM_WAIT:
208 act = simcall_comm_wait__get__comm(&process->simcall);
209 if (act->comm.src_proc && act->comm.dst_proc) {
212 if (act->comm.src_proc == NULL && act->comm.type == SIMIX_COMM_READY
213 && act->comm.detached == 1)
218 procstate->state = MC_DONE;
219 return &process->simcall;
223 case SIMCALL_MC_RANDOM:
224 if (procstate->state == MC_INTERLEAVE)
227 if (state->req_num < simcall_mc_random__get__max(&process->simcall))
228 *value = state->req_num + 1;
230 procstate->state = MC_DONE;
231 return &process->simcall;
235 procstate->state = MC_DONE;
237 return &process->simcall;