Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7faf1f1260eec543b809843e0bfe65751b7138a9
[simgrid.git] / src / mc / mc_state.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "../simix/smx_private.h"
7 #include "xbt/fifo.h"
8 #include "mc_private.h"
9
10 /**
11  * \brief Creates a state data structure used by the exploration algorithm
12  */
13 mc_state_t MC_state_new()
14 {
15   mc_state_t state = NULL;
16   
17   state = xbt_new0(s_mc_state_t, 1);
18   state->max_pid = simix_process_maxpid;
19   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
20   state->system_state = NULL;
21
22   mc_stats->expanded_states++;
23   return state;
24 }
25
26 mc_state_t MC_state_pair_new(void)
27 {
28   mc_state_t state = NULL;
29   
30   state = xbt_new0(s_mc_state_t, 1);
31   state->max_pid = simix_process_maxpid;
32   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
33   
34   //mc_stats->expanded_states++;
35   return state;
36 }
37
38 /**
39  * \brief Deletes a state data structure
40  * \param trans The state to be deleted
41  */
42 void MC_state_delete(mc_state_t state)
43 {
44   xbt_free(state->proc_status);
45   xbt_free(state);
46 }
47
48 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
49 {
50   state->proc_status[process->pid].state = MC_INTERLEAVE;
51   state->proc_status[process->pid].interleave_count = 0;
52 }
53
54 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
55 {
56   if(state->proc_status[process->pid].state == MC_INTERLEAVE)
57     state->proc_status[process->pid].state = MC_DONE;
58 }
59
60 unsigned int MC_state_interleave_size(mc_state_t state)
61 {
62   unsigned int i, size=0;
63
64   for(i=0; i < state->max_pid; i++){
65     if(state->proc_status[i].state == MC_INTERLEAVE)
66       size++;
67   }
68
69   return size;
70 }
71
72 int MC_state_process_is_done(mc_state_t state, smx_process_t process){
73   return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
74 }
75
76 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value)
77 {
78   state->executed_req = *req;
79   state->req_num = value;
80
81   /* The waitany and testany request are transformed into a wait or test request over the
82    * corresponding communication action so it can be treated later by the dependence
83    * function. */
84   switch(req->call){
85     case SIMCALL_COMM_WAITANY:
86       state->internal_req.call = SIMCALL_COMM_WAIT;
87       state->internal_req.issuer = req->issuer;
88       state->internal_comm = *xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_action_t);
89       simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
90       simcall_comm_wait__set__timeout(&state->internal_req, 0);
91       break;
92
93     case SIMCALL_COMM_TESTANY:
94       state->internal_req.call = SIMCALL_COMM_TEST;
95       state->internal_req.issuer = req->issuer;
96
97       if(value > 0)
98         state->internal_comm = *xbt_dynar_get_as(simcall_comm_testany__get__comms(req), value, smx_action_t);
99
100       simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
101       simcall_comm_test__set__result(&state->internal_req, value);
102       break;
103
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);
109       break;
110
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);
116       break;
117
118     default:
119       state->internal_req = *req;
120       break;
121   }
122 }
123
124 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
125 {
126   *value = state->req_num;
127   return &state->executed_req;
128 }
129
130 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
131 {
132   return &state->internal_req;
133 }
134
135 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
136 {
137   smx_process_t process = NULL;
138   mc_procstate_t procstate = NULL;
139   unsigned int start_count;
140
141   xbt_swag_foreach(process, simix_global->process_list){
142     procstate = &state->proc_status[process->pid];
143
144     if(procstate->state == MC_INTERLEAVE){
145       if(MC_process_is_enabled(process)){
146         switch(process->simcall.call){
147           case SIMCALL_COMM_WAITANY:
148             *value = -1;
149             while(procstate->interleave_count < xbt_dynar_length(simcall_comm_waitany__get__comms(&process->simcall))){
150               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
151                 *value = procstate->interleave_count-1;
152                 break;
153               }
154             }
155
156             if(procstate->interleave_count >= xbt_dynar_length(simcall_comm_waitany__get__comms(&process->simcall)))
157               procstate->state = MC_DONE;
158
159             if(*value != -1)
160               return &process->simcall;
161
162             break;
163
164           case SIMCALL_COMM_TESTANY:
165             start_count = procstate->interleave_count;
166             *value = -1;
167             while(procstate->interleave_count < xbt_dynar_length(simcall_comm_testany__get__comms(&process->simcall))){
168               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
169                 *value = procstate->interleave_count - 1;
170                 break;
171               }
172             }
173
174             if(procstate->interleave_count >= xbt_dynar_length(simcall_comm_testany__get__comms(&process->simcall)))
175               procstate->state = MC_DONE;
176
177             if(*value != -1 || start_count == 0)
178               return &process->simcall;
179
180             break;
181
182           case SIMCALL_COMM_WAIT:
183             if(simcall_comm_wait__get__comm(&process->simcall)->comm.src_proc
184                && simcall_comm_wait__get__comm(&process->simcall)->comm.dst_proc){
185               *value = 0;
186             }else{
187               *value = -1;
188             }
189             procstate->state = MC_DONE;
190             return &process->simcall;
191
192             break;
193
194           default:
195             procstate->state = MC_DONE;
196             *value = 0;
197             return &process->simcall;
198             break;
199         }
200       }
201     }
202   }
203
204   return NULL;
205 }