Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix missing proto for PID functions
[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   if(state->system_state)
45     MC_free_snapshot(state->system_state);
46   xbt_free(state->proc_status);
47   xbt_free(state);
48 }
49
50 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
51 {
52   state->proc_status[process->pid].state = MC_INTERLEAVE;
53   state->proc_status[process->pid].interleave_count = 0;
54 }
55
56 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
57 {
58   if(state->proc_status[process->pid].state == MC_INTERLEAVE)
59     state->proc_status[process->pid].state = MC_DONE;
60 }
61
62 unsigned int MC_state_interleave_size(mc_state_t state)
63 {
64   unsigned int i, size=0;
65
66   for(i=0; i < state->max_pid; i++){
67     if(state->proc_status[i].state == MC_INTERLEAVE)
68       size++;
69   }
70
71   return size;
72 }
73
74 int MC_state_process_is_done(mc_state_t state, smx_process_t process){
75   return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
76 }
77
78 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value)
79 {
80   state->executed_req = *req;
81   state->req_num = value;
82
83   /* The waitany and testany request are transformed into a wait or test request over the
84    * corresponding communication action so it can be treated later by the dependence
85    * function. */
86   switch(req->call){
87     case SIMCALL_COMM_WAITANY:
88       state->internal_req.call = SIMCALL_COMM_WAIT;
89       state->internal_req.issuer = req->issuer;
90       state->internal_comm = *xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_action_t);
91       simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
92       simcall_comm_wait__set__timeout(&state->internal_req, 0);
93       break;
94
95     case SIMCALL_COMM_TESTANY:
96       state->internal_req.call = SIMCALL_COMM_TEST;
97       state->internal_req.issuer = req->issuer;
98
99       if(value > 0)
100         state->internal_comm = *xbt_dynar_get_as(simcall_comm_testany__get__comms(req), value, smx_action_t);
101
102       simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
103       simcall_comm_test__set__result(&state->internal_req, value);
104       break;
105
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);
111       break;
112
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);
118       break;
119
120     default:
121       state->internal_req = *req;
122       break;
123   }
124 }
125
126 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
127 {
128   *value = state->req_num;
129   return &state->executed_req;
130 }
131
132 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
133 {
134   return &state->internal_req;
135 }
136
137 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
138 {
139   smx_process_t process = NULL;
140   mc_procstate_t procstate = NULL;
141   unsigned int start_count;
142
143   xbt_swag_foreach(process, simix_global->process_list){
144     procstate = &state->proc_status[process->pid];
145
146     if(procstate->state == MC_INTERLEAVE){
147       if(MC_process_is_enabled(process)){
148         switch(process->simcall.call){
149           case SIMCALL_COMM_WAITANY:
150             *value = -1;
151             while(procstate->interleave_count < xbt_dynar_length(simcall_comm_waitany__get__comms(&process->simcall))){
152               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
153                 *value = procstate->interleave_count-1;
154                 break;
155               }
156             }
157
158             if(procstate->interleave_count >= xbt_dynar_length(simcall_comm_waitany__get__comms(&process->simcall)))
159               procstate->state = MC_DONE;
160
161             if(*value != -1)
162               return &process->simcall;
163
164             break;
165
166           case SIMCALL_COMM_TESTANY:
167             start_count = procstate->interleave_count;
168             *value = -1;
169             while(procstate->interleave_count < xbt_dynar_length(simcall_comm_testany__get__comms(&process->simcall))){
170               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
171                 *value = procstate->interleave_count - 1;
172                 break;
173               }
174             }
175
176             if(procstate->interleave_count >= xbt_dynar_length(simcall_comm_testany__get__comms(&process->simcall)))
177               procstate->state = MC_DONE;
178
179             if(*value != -1 || start_count == 0)
180               return &process->simcall;
181
182             break;
183
184           case SIMCALL_COMM_WAIT:
185             if(simcall_comm_wait__get__comm(&process->simcall)->comm.src_proc
186                && simcall_comm_wait__get__comm(&process->simcall)->comm.dst_proc){
187               *value = 0;
188             }else{
189               *value = -1;
190             }
191             procstate->state = MC_DONE;
192             return &process->simcall;
193
194             break;
195
196           default:
197             procstate->state = MC_DONE;
198             *value = 0;
199             return &process->simcall;
200             break;
201         }
202       }
203     }
204   }
205
206   return NULL;
207 }