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"
10 #include "mc_request.h"
11 #include "mc_private.h"
14 * \brief Creates a state data structure used by the exploration algorithm
16 mc_state_t MC_state_new()
18 mc_state_t state = NULL;
20 state = xbt_new0(s_mc_state_t, 1);
21 state->max_pid = simix_process_maxpid;
22 state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
23 state->system_state = NULL;
24 state->num = ++mc_stats->expanded_states;
30 * \brief Deletes a state data structure
31 * \param trans The state to be deleted
33 void MC_state_delete(mc_state_t state)
35 if (state->system_state)
36 MC_free_snapshot(state->system_state);
37 xbt_free(state->proc_status);
41 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
43 state->proc_status[process->pid].state = MC_INTERLEAVE;
44 state->proc_status[process->pid].interleave_count = 0;
47 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
49 if (state->proc_status[process->pid].state == MC_INTERLEAVE)
50 state->proc_status[process->pid].state = MC_DONE;
53 unsigned int MC_state_interleave_size(mc_state_t state)
55 unsigned int i, size = 0;
57 for (i = 0; i < state->max_pid; i++) {
58 if ((state->proc_status[i].state == MC_INTERLEAVE)
59 || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
66 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
68 return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
71 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
74 state->executed_req = *req;
75 state->req_num = value;
76 smx_process_t process = NULL;
77 mc_procstate_t procstate = NULL;
79 /* The waitany and testany request are transformed into a wait or test request over the
80 * corresponding communication action so it can be treated later by the dependence
83 case SIMCALL_COMM_WAITANY:
84 state->internal_req.call = SIMCALL_COMM_WAIT;
85 state->internal_req.issuer = req->issuer;
86 state->internal_comm =
87 *xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
89 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
90 simcall_comm_wait__set__timeout(&state->internal_req, 0);
93 case SIMCALL_COMM_TESTANY:
94 state->internal_req.call = SIMCALL_COMM_TEST;
95 state->internal_req.issuer = req->issuer;
98 state->internal_comm =
99 *xbt_dynar_get_as(simcall_comm_testany__get__comms(req), value,
102 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
103 simcall_comm_test__set__result(&state->internal_req, value);
106 case SIMCALL_COMM_WAIT:
107 state->internal_req = *req;
108 state->internal_comm = *(simcall_comm_wait__get__comm(req));
109 simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
110 simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
113 case SIMCALL_COMM_TEST:
114 state->internal_req = *req;
115 state->internal_comm = *simcall_comm_test__get__comm(req);
116 simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
117 simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
120 case SIMCALL_MC_RANDOM:
121 state->internal_req = *req;
122 if (value != simcall_mc_random__get__max(req)) {
123 xbt_swag_foreach(process, simix_global->process_list) {
124 procstate = &state->proc_status[process->pid];
125 if (process->pid == req->issuer->pid) {
126 procstate->state = MC_MORE_INTERLEAVE;
134 state->internal_req = *req;
139 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
141 *value = state->req_num;
142 return &state->executed_req;
145 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
147 return &state->internal_req;
150 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
152 smx_process_t process = NULL;
153 mc_procstate_t procstate = NULL;
154 unsigned int start_count;
155 smx_synchro_t act = NULL;
157 xbt_swag_foreach(process, simix_global->process_list) {
158 procstate = &state->proc_status[process->pid];
160 if (procstate->state == MC_INTERLEAVE
161 || procstate->state == MC_MORE_INTERLEAVE) {
162 if (MC_process_is_enabled(process)) {
163 switch (process->simcall.call) {
164 case SIMCALL_COMM_WAITANY:
166 while (procstate->interleave_count <
167 xbt_dynar_length(simcall_comm_waitany__get__comms
168 (&process->simcall))) {
169 if (MC_request_is_enabled_by_idx
170 (&process->simcall, procstate->interleave_count++)) {
171 *value = procstate->interleave_count - 1;
176 if (procstate->interleave_count >=
177 xbt_dynar_length(simcall_comm_waitany__get__comms
178 (&process->simcall)))
179 procstate->state = MC_DONE;
182 return &process->simcall;
186 case SIMCALL_COMM_TESTANY:
187 start_count = procstate->interleave_count;
189 while (procstate->interleave_count <
190 xbt_dynar_length(simcall_comm_testany__get__comms
191 (&process->simcall))) {
192 if (MC_request_is_enabled_by_idx
193 (&process->simcall, procstate->interleave_count++)) {
194 *value = procstate->interleave_count - 1;
199 if (procstate->interleave_count >=
200 xbt_dynar_length(simcall_comm_testany__get__comms
201 (&process->simcall)))
202 procstate->state = MC_DONE;
204 if (*value != -1 || start_count == 0)
205 return &process->simcall;
209 case SIMCALL_COMM_WAIT:
210 act = simcall_comm_wait__get__comm(&process->simcall);
211 if (act->comm.src_proc && act->comm.dst_proc) {
214 if (act->comm.src_proc == NULL && act->comm.type == SIMIX_COMM_READY
215 && act->comm.detached == 1)
220 procstate->state = MC_DONE;
221 return &process->simcall;
225 case SIMCALL_MC_RANDOM:
226 if (procstate->state == MC_INTERLEAVE)
229 if (state->req_num < simcall_mc_random__get__max(&process->simcall))
230 *value = state->req_num + 1;
232 procstate->state = MC_DONE;
233 return &process->simcall;
237 procstate->state = MC_DONE;
239 return &process->simcall;