Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : DPOR (independant transitions) algorithm for liveness properties
[simgrid.git] / src / mc / mc_global.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4
5 #include "../surf/surf_private.h"
6 #include "../simix/private.h"
7 #include "xbt/fifo.h"
8 #include "private.h"
9
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
12                                 "Logging specific to MC (global)");
13
14 /* MC global data structures */
15 mc_snapshot_t initial_snapshot = NULL;
16 xbt_fifo_t mc_stack = NULL;
17 mc_stats_t mc_stats = NULL;
18 mc_stats_pair_t mc_stats_pair = NULL;
19 mc_state_t mc_current_state = NULL;
20 char mc_replay_mode = FALSE;
21 double *mc_time = NULL;
22 xbt_fifo_t mc_snapshot_stack = NULL;
23
24 /**
25  *  \brief Initialize the model-checker data structures
26  */
27 void MC_init(void)
28 {
29   /* Check if MC is already initialized */
30   if (initial_snapshot)
31     return;
32
33   mc_time = xbt_new0(double, simix_process_maxpid);
34
35   /* Initialize the data structures that must be persistent across every
36      iteration of the model-checker (in RAW memory) */
37   MC_SET_RAW_MEM;
38
39   /* Initialize statistics */
40   mc_stats = xbt_new0(s_mc_stats_t, 1);
41   mc_stats->state_size = 1;
42
43   /* Create exploration stack */
44   mc_stack = xbt_fifo_new();
45
46   MC_UNSET_RAW_MEM;
47
48   MC_dpor_init();
49
50   MC_SET_RAW_MEM;
51   /* Save the initial state */
52   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
53   MC_take_snapshot(initial_snapshot);
54   MC_UNSET_RAW_MEM;
55 }
56
57 void MC_init_with_automaton(xbt_automaton_t a){
58
59   XBT_DEBUG("Start init mc");
60   
61   mc_time = xbt_new0(double, simix_process_maxpid);
62
63   /* Initialize the data structures that must be persistent across every
64      iteration of the model-checker (in RAW memory) */
65
66   MC_SET_RAW_MEM;
67
68   /* Initialize statistics */
69   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
70   mc_stats = xbt_new0(s_mc_stats_t, 1);
71   //mc_stats_pair->pair_size = 1;
72
73   XBT_DEBUG("Creating snapshot_stack");
74
75  /* Create exploration stack */
76   mc_snapshot_stack = xbt_fifo_new();
77
78   MC_UNSET_RAW_MEM;
79
80   //MC_vddfs_with_restore_snapshot_init(a);
81   //MC_ddfs_with_restore_snapshot_init(a);
82   MC_dpor2_init(a);
83 }
84
85
86 void MC_modelcheck(void)
87 {
88   MC_init();
89   MC_dpor();
90   MC_exit();
91 }
92
93 void MC_modelcheck_with_automaton(xbt_automaton_t a){
94   MC_init_with_automaton(a);
95   MC_exit_with_automaton();
96 }
97
98 void MC_exit_with_automaton(void)
99 {
100   MC_print_statistics_pairs(mc_stats_pair);
101   xbt_free(mc_time);
102   MC_memory_exit();
103 }
104
105 void MC_exit(void)
106 {
107   MC_print_statistics(mc_stats);
108   xbt_free(mc_time);
109   MC_memory_exit();
110 }
111
112 int MC_random(int min, int max)
113 {
114   /*FIXME: return mc_current_state->executed_transition->random.value;*/
115   return 0;
116 }
117
118 /**
119  * \brief Schedules all the process that are ready to run
120  */
121 void MC_wait_for_requests(void)
122 {
123   smx_process_t process;
124   smx_req_t req;
125   unsigned int iter;
126
127   while (xbt_dynar_length(simix_global->process_to_run)) {
128     SIMIX_process_runall();
129     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
130       req = &process->request;
131       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
132           SIMIX_request_pre(req, 0);
133     }
134   }
135 }
136
137 int MC_deadlock_check()
138 {
139   int deadlock = FALSE;
140   smx_process_t process;
141   if(xbt_swag_size(simix_global->process_list)){
142     deadlock = TRUE;
143     xbt_swag_foreach(process, simix_global->process_list){
144       if(process->request.call != REQ_NO_REQ
145          && MC_request_is_enabled(&process->request)){
146         deadlock = FALSE;
147         break;
148       }
149     }
150   }
151   return deadlock;
152 }
153
154 /**
155  * \brief Re-executes from the initial state all the transitions indicated by
156  *        a given model-checker stack.
157  * \param stack The stack with the transitions to execute.
158 */
159 void MC_replay(xbt_fifo_t stack)
160 {
161   int value;
162   char *req_str;
163   smx_req_t req = NULL, saved_req = NULL;
164   xbt_fifo_item_t item;
165   mc_state_t state;
166
167   XBT_DEBUG("**** Begin Replay ****");
168
169   /* Restore the initial state */
170   MC_restore_snapshot(initial_snapshot);
171   /* At the moment of taking the snapshot the raw heap was set, so restoring
172    * it will set it back again, we have to unset it to continue  */
173   MC_UNSET_RAW_MEM;
174
175   /* Traverse the stack from the initial state and re-execute the transitions */
176   for (item = xbt_fifo_get_last_item(stack);
177        item != xbt_fifo_get_first_item(stack);
178        item = xbt_fifo_get_prev_item(item)) {
179
180     state = (mc_state_t) xbt_fifo_get_item_content(item);
181     saved_req = MC_state_get_executed_request(state, &value);
182    
183     if(saved_req){
184       /* because we got a copy of the executed request, we have to fetch the  
185          real one, pointed by the request field of the issuer process */
186       req = &saved_req->issuer->request;
187
188       /* Debug information */
189       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
190         req_str = MC_request_to_string(req, value);
191         XBT_DEBUG("Replay: %s (%p)", req_str, state);
192         xbt_free(req_str);
193       }
194     }
195          
196     SIMIX_request_pre(req, value);
197     MC_wait_for_requests();
198          
199     /* Update statistics */
200     mc_stats->visited_states++;
201     mc_stats->executed_transitions++;
202   }
203   XBT_DEBUG("**** End Replay ****");
204 }
205
206 /**
207  * \brief Dumps the contents of a model-checker's stack and shows the actual
208  *        execution trace
209  * \param stack The stack to dump
210 */
211 void MC_dump_stack(xbt_fifo_t stack)
212 {
213   mc_state_t state;
214
215   MC_show_stack(stack);
216
217   MC_SET_RAW_MEM;
218   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
219     MC_state_delete(state);
220   MC_UNSET_RAW_MEM;
221 }
222
223 void MC_show_stack(xbt_fifo_t stack)
224 {
225   int value;
226   mc_state_t state;
227   xbt_fifo_item_t item;
228   smx_req_t req;
229   char *req_str = NULL;
230   
231   for (item = xbt_fifo_get_last_item(stack);
232        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
233         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
234     req = MC_state_get_executed_request(state, &value);
235     if(req){
236       req_str = MC_request_to_string(req, value);
237       XBT_INFO("%s", req_str);
238       xbt_free(req_str);
239     }
240   }
241 }
242
243 void MC_show_deadlock(smx_req_t req)
244 {
245   /*char *req_str = NULL;*/
246   XBT_INFO("**************************");
247   XBT_INFO("*** DEAD-LOCK DETECTED ***");
248   XBT_INFO("**************************");
249   XBT_INFO("Locked request:");
250   /*req_str = MC_request_to_string(req);
251   XBT_INFO("%s", req_str);
252   xbt_free(req_str);*/
253   XBT_INFO("Counter-example execution trace:");
254   MC_dump_stack(mc_stack);
255 }
256
257 void MC_print_statistics(mc_stats_t stats)
258 {
259   XBT_INFO("State space size ~= %lu", stats->state_size);
260   XBT_INFO("Expanded states = %lu", stats->expanded_states);
261   XBT_INFO("Visited states = %lu", stats->visited_states);
262   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
263   XBT_INFO("Expanded / Visited = %lf",
264         (double) stats->visited_states / stats->expanded_states);
265   /*XBT_INFO("Exploration coverage = %lf",
266      (double)stats->expanded_states / stats->state_size); */
267 }
268
269 void MC_print_statistics_pairs(mc_stats_pair_t stats)
270 {
271   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
272   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
273   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
274   XBT_INFO("Expanded / Visited = %lf",
275         (double) stats->visited_pairs / stats->expanded_pairs);
276   /*XBT_INFO("Exploration coverage = %lf",
277      (double)stats->expanded_states / stats->state_size); */
278 }
279
280 void MC_assert(int prop)
281 {
282   if (MC_IS_ENABLED && !prop) {
283     XBT_INFO("**************************");
284     XBT_INFO("*** PROPERTY NOT VALID ***");
285     XBT_INFO("**************************");
286     XBT_INFO("Counter-example execution trace:");
287     MC_dump_stack(mc_stack);
288     MC_print_statistics(mc_stats);
289     xbt_abort();
290   }
291 }
292
293 void MC_assert_pair(int prop){
294   if (MC_IS_ENABLED && !prop) {
295     XBT_INFO("**************************");
296     XBT_INFO("*** PROPERTY NOT VALID ***");
297     XBT_INFO("**************************");
298     //XBT_INFO("Counter-example execution trace:");
299     MC_show_snapshot_stack(mc_snapshot_stack);
300     //MC_dump_snapshot_stack(mc_snapshot_stack);
301     MC_print_statistics_pairs(mc_stats_pair);
302     xbt_abort();
303   }
304 }
305
306 void MC_process_clock_add(smx_process_t process, double amount)
307 {
308   mc_time[process->pid] += amount;
309 }
310
311 double MC_process_clock_get(smx_process_t process)
312 {
313   if(mc_time)
314     return mc_time[process->pid];
315   else
316     return 0;
317 }