Logo AND Algorithmique Numérique Distribuée

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