Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
556fa707fbf53e8d14b8c2d2f6dce755a9ad717a
[simgrid.git] / src / mc / mc_global.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <sys/time.h>
5
6 #include "../surf/surf_private.h"
7 #include "../simix/private.h"
8 #include "xbt/fifo.h"
9 #include "private.h"
10
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
13                                 "Logging specific to MC (global)");
14
15 /* MC global data structures */
16
17 mc_state_t mc_current_state = NULL;
18 char mc_replay_mode = FALSE;
19 double *mc_time = NULL;
20 mc_snapshot_t initial_snapshot = NULL;
21
22 /* Safety */
23
24 xbt_fifo_t mc_stack_safety_stateful = NULL;
25 xbt_fifo_t mc_stack_safety_stateless = NULL;
26 mc_stats_t mc_stats = NULL;
27
28 /* Liveness */
29
30 xbt_fifo_t mc_stack_liveness_stateful = NULL;
31 mc_stats_pair_t mc_stats_pair = NULL;
32 xbt_fifo_t mc_stack_liveness_stateless = NULL;
33
34
35 /**
36  *  \brief Initialize the model-checker data structures
37  */
38 void MC_init_safety_stateless(void)
39 {
40
41   /* Check if MC is already initialized */
42   if (initial_snapshot)
43     return;
44
45   mc_time = xbt_new0(double, simix_process_maxpid);
46
47   /* Initialize the data structures that must be persistent across every
48      iteration of the model-checker (in RAW memory) */
49   MC_SET_RAW_MEM;
50
51   /* Initialize statistics */
52   mc_stats = xbt_new0(s_mc_stats_t, 1);
53   mc_stats->state_size = 1;
54
55   /* Create exploration stack */
56   mc_stack_safety_stateless = xbt_fifo_new();
57
58   MC_UNSET_RAW_MEM;
59
60   MC_dpor_init();
61
62   MC_SET_RAW_MEM;
63   /* Save the initial state */
64   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
65   MC_take_snapshot(initial_snapshot);
66   MC_UNSET_RAW_MEM;
67 }
68
69 void MC_init_safety_stateful(void){
70
71   
72    /* Check if MC is already initialized */
73   if (initial_snapshot)
74     return;
75
76   mc_time = xbt_new0(double, simix_process_maxpid);
77
78   /* Initialize the data structures that must be persistent across every
79      iteration of the model-checker (in RAW memory) */
80   MC_SET_RAW_MEM;
81
82   /* Initialize statistics */
83   mc_stats = xbt_new0(s_mc_stats_t, 1);
84   mc_stats->state_size = 1;
85
86   /* Create exploration stack */
87   mc_stack_safety_stateful = xbt_fifo_new();
88
89   MC_UNSET_RAW_MEM;
90
91   MC_dpor_stateful_init();
92
93
94 }
95
96 void MC_init_liveness_stateful(xbt_automaton_t a){
97
98   XBT_DEBUG("Start init mc");
99   
100   mc_time = xbt_new0(double, simix_process_maxpid);
101
102   /* Initialize the data structures that must be persistent across every
103      iteration of the model-checker (in RAW memory) */
104
105   MC_SET_RAW_MEM;
106
107   /* Initialize statistics */
108   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
109   //mc_stats = xbt_new0(s_mc_stats_t, 1);
110
111   XBT_DEBUG("Creating snapshot_stack");
112
113  /* Create exploration stack */
114   mc_stack_liveness_stateful = xbt_fifo_new();
115
116
117   MC_UNSET_RAW_MEM;
118
119   MC_vddfs_stateful_init(a);
120   //MC_ddfs_stateful_init(a);
121   //MC_dpor2_init(a);
122   //MC_dpor3_init(a);
123 }
124
125 void MC_init_liveness_stateless(xbt_automaton_t a){
126
127   XBT_DEBUG("Start init mc");
128   
129   mc_time = xbt_new0(double, simix_process_maxpid);
130
131   /* Initialize the data structures that must be persistent across every
132      iteration of the model-checker (in RAW memory) */
133
134   MC_SET_RAW_MEM;
135
136   /* Initialize statistics */
137   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
138
139   XBT_DEBUG("Creating stack");
140
141  /* Create exploration stack */
142   mc_stack_liveness_stateless = xbt_fifo_new();
143
144   MC_UNSET_RAW_MEM;
145
146   MC_ddfs_stateless_init(a);
147
148   
149 }
150
151
152 void MC_modelcheck(void)
153 {
154   MC_init_safety_stateless();
155   MC_dpor();
156   MC_exit();
157 }
158
159 void MC_modelcheck_stateful(void)
160 {
161   MC_init_safety_stateful();
162   MC_dpor_stateful();
163   MC_exit();
164 }
165
166 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
167   MC_init_liveness_stateful(a);
168   MC_exit_liveness();
169 }
170
171 void MC_modelcheck_liveness_stateless(xbt_automaton_t a){
172   MC_init_liveness_stateless(a);
173   MC_exit_liveness();
174 }
175
176 void MC_exit_liveness(void)
177 {
178   MC_print_statistics_pairs(mc_stats_pair);
179   xbt_free(mc_time);
180   MC_memory_exit();
181 }
182
183
184 void MC_exit(void)
185 {
186   MC_print_statistics(mc_stats);
187   xbt_free(mc_time);
188   MC_memory_exit();
189 }
190
191
192 int MC_random(int min, int max)
193 {
194   /*FIXME: return mc_current_state->executed_transition->random.value;*/
195   return 0;
196 }
197
198 /**
199  * \brief Schedules all the process that are ready to run
200  */
201 void MC_wait_for_requests(void)
202 {
203   smx_process_t process;
204   smx_req_t req;
205   unsigned int iter;
206
207   while (xbt_dynar_length(simix_global->process_to_run)) {
208     SIMIX_process_runall();
209     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
210       req = &process->request;
211       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
212           SIMIX_request_pre(req, 0);
213     }
214   }
215 }
216
217 int MC_deadlock_check()
218 {
219   int deadlock = FALSE;
220   smx_process_t process;
221   if(xbt_swag_size(simix_global->process_list)){
222     deadlock = TRUE;
223     xbt_swag_foreach(process, simix_global->process_list){
224       if(process->request.call != REQ_NO_REQ
225          && MC_request_is_enabled(&process->request)){
226         deadlock = FALSE;
227         break;
228       }
229     }
230   }
231   return deadlock;
232 }
233
234 /**
235  * \brief Re-executes from the initial state all the transitions indicated by
236  *        a given model-checker stack.
237  * \param stack The stack with the transitions to execute.
238 */
239 void MC_replay(xbt_fifo_t stack)
240 {
241   int value;
242   char *req_str;
243   smx_req_t req = NULL, saved_req = NULL;
244   xbt_fifo_item_t item;
245   mc_state_t state;
246
247   XBT_DEBUG("**** Begin Replay ****");
248
249   /* Restore the initial state */
250   MC_restore_snapshot(initial_snapshot);
251   /* At the moment of taking the snapshot the raw heap was set, so restoring
252    * it will set it back again, we have to unset it to continue  */
253   MC_UNSET_RAW_MEM;
254
255   /* Traverse the stack from the initial state and re-execute the transitions */
256   for (item = xbt_fifo_get_last_item(stack);
257        item != xbt_fifo_get_first_item(stack);
258        item = xbt_fifo_get_prev_item(item)) {
259
260     state = (mc_state_t) xbt_fifo_get_item_content(item);
261     saved_req = MC_state_get_executed_request(state, &value);
262    
263     if(saved_req){
264       /* because we got a copy of the executed request, we have to fetch the  
265          real one, pointed by the request field of the issuer process */
266       req = &saved_req->issuer->request;
267
268       /* Debug information */
269       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
270         req_str = MC_request_to_string(req, value);
271         XBT_DEBUG("Replay: %s (%p)", req_str, state);
272         xbt_free(req_str);
273       }
274     }
275          
276     SIMIX_request_pre(req, value);
277     MC_wait_for_requests();
278          
279     /* Update statistics */
280     mc_stats->visited_states++;
281     mc_stats->executed_transitions++;
282   }
283   XBT_DEBUG("**** End Replay ****");
284 }
285
286 void MC_replay_liveness(xbt_fifo_t stack)
287 {
288   int value;
289   char *req_str;
290   smx_req_t req = NULL, saved_req = NULL;
291   xbt_fifo_item_t item;
292   mc_state_t state;
293   mc_pair_stateless_t pair;
294
295   XBT_DEBUG("**** Begin Replay ****");
296
297   /* Restore the initial state */
298   MC_restore_snapshot(initial_snapshot);
299   /* At the moment of taking the snapshot the raw heap was set, so restoring
300    * it will set it back again, we have to unset it to continue  */
301   MC_UNSET_RAW_MEM;
302
303   /* Traverse the stack from the initial state and re-execute the transitions */
304   for (item = xbt_fifo_get_last_item(stack);
305        item != xbt_fifo_get_first_item(stack);
306        item = xbt_fifo_get_prev_item(item)) {
307
308     pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
309     state = (mc_state_t) pair->graph_state;
310     saved_req = MC_state_get_executed_request(state, &value);
311     //XBT_DEBUG("SavedReq->call %u", saved_req->call);
312    
313     if(saved_req != NULL){
314       /* because we got a copy of the executed request, we have to fetch the  
315          real one, pointed by the request field of the issuer process */
316       req = &saved_req->issuer->request;
317       //XBT_DEBUG("Req->call %u", req->call);
318
319       /* Debug information */
320       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
321         req_str = MC_request_to_string(req, value);
322         XBT_DEBUG("Replay: %s (%p)", req_str, state);
323         xbt_free(req_str);
324       }
325     }
326          
327     SIMIX_request_pre(req, value);
328     MC_wait_for_requests();
329          
330     /* Update statistics */
331     mc_stats_pair->visited_pairs++;
332   }
333   XBT_DEBUG("**** End Replay ****");
334 }
335
336
337 /**
338  * \brief Dumps the contents of a model-checker's stack and shows the actual
339  *        execution trace
340  * \param stack The stack to dump
341 */
342 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
343 {
344   mc_state_t state;
345
346   MC_show_stack_safety_stateless(stack);
347
348   MC_SET_RAW_MEM;
349   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
350     MC_state_delete(state);
351   MC_UNSET_RAW_MEM;
352 }
353
354
355 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
356 {
357   int value;
358   mc_state_t state;
359   xbt_fifo_item_t item;
360   smx_req_t req;
361   char *req_str = NULL;
362   
363   for (item = xbt_fifo_get_last_item(stack);
364        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
365         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
366     req = MC_state_get_executed_request(state, &value);
367     if(req){
368       req_str = MC_request_to_string(req, value);
369       XBT_INFO("%s", req_str);
370       xbt_free(req_str);
371     }
372   }
373 }
374
375 void MC_show_deadlock(smx_req_t req)
376 {
377   /*char *req_str = NULL;*/
378   XBT_INFO("**************************");
379   XBT_INFO("*** DEAD-LOCK DETECTED ***");
380   XBT_INFO("**************************");
381   XBT_INFO("Locked request:");
382   /*req_str = MC_request_to_string(req);
383   XBT_INFO("%s", req_str);
384   xbt_free(req_str);*/
385   XBT_INFO("Counter-example execution trace:");
386   MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
387 }
388
389 void MC_show_deadlock_stateful(smx_req_t req)
390 {
391   /*char *req_str = NULL;*/
392   XBT_INFO("**************************");
393   XBT_INFO("*** DEAD-LOCK DETECTED ***");
394   XBT_INFO("**************************");
395   XBT_INFO("Locked request:");
396   /*req_str = MC_request_to_string(req);
397   XBT_INFO("%s", req_str);
398   xbt_free(req_str);*/
399   XBT_INFO("Counter-example execution trace:");
400   MC_show_stack_safety_stateful(mc_stack_safety_stateful);
401 }
402
403 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
404 {
405   //mc_state_ws_t state;
406
407   MC_show_stack_safety_stateful(stack);
408
409   /*MC_SET_RAW_MEM;
410   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
411     MC_state_delete(state);
412     MC_UNSET_RAW_MEM;*/
413 }
414
415
416 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
417 {
418   int value;
419   mc_state_ws_t state;
420   xbt_fifo_item_t item;
421   smx_req_t req;
422   char *req_str = NULL;
423   
424   for (item = xbt_fifo_get_last_item(stack);
425        (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
426         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
427     req = MC_state_get_executed_request(state->graph_state, &value);
428     if(req){
429       req_str = MC_request_to_string(req, value);
430       XBT_INFO("%s", req_str);
431       xbt_free(req_str);
432     }
433   }
434 }
435
436 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
437   int value;
438   mc_pair_t pair;
439   xbt_fifo_item_t item;
440   smx_req_t req;
441   char *req_str = NULL;
442   
443   for (item = xbt_fifo_get_last_item(stack);
444        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
445         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
446     req = MC_state_get_executed_request(pair->graph_state, &value);
447     if(req){
448       req_str = MC_request_to_string(req, value);
449       XBT_INFO("%s", req_str);
450       xbt_free(req_str);
451     }
452   }
453 }
454
455 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
456   mc_pair_t pair;
457
458   MC_SET_RAW_MEM;
459   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
460     MC_pair_delete(pair);
461   MC_UNSET_RAW_MEM;
462 }
463
464 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
465   int value;
466   mc_pair_stateless_t pair;
467   xbt_fifo_item_t item;
468   smx_req_t req;
469   char *req_str = NULL;
470   
471   for (item = xbt_fifo_get_last_item(stack);
472        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
473         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
474     req = MC_state_get_executed_request(pair->graph_state, &value);
475     if(req){
476       req_str = MC_request_to_string(req, value);
477       XBT_INFO("%s", req_str);
478       xbt_free(req_str);
479     }
480   }
481 }
482
483 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
484   mc_pair_stateless_t pair;
485
486   MC_SET_RAW_MEM;
487   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
488     MC_pair_stateless_delete(pair);
489   MC_UNSET_RAW_MEM;
490 }
491
492
493 void MC_print_statistics(mc_stats_t stats)
494 {
495   XBT_INFO("State space size ~= %lu", stats->state_size);
496   XBT_INFO("Expanded states = %lu", stats->expanded_states);
497   XBT_INFO("Visited states = %lu", stats->visited_states);
498   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
499   XBT_INFO("Expanded / Visited = %lf",
500         (double) stats->visited_states / stats->expanded_states);
501   /*XBT_INFO("Exploration coverage = %lf",
502      (double)stats->expanded_states / stats->state_size); */
503 }
504
505 void MC_print_statistics_pairs(mc_stats_pair_t stats)
506 {
507   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
508   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
509   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
510   XBT_INFO("Expanded / Visited = %lf",
511         (double) stats->visited_pairs / stats->expanded_pairs);
512   /*XBT_INFO("Exploration coverage = %lf",
513      (double)stats->expanded_states / stats->state_size); */
514 }
515
516 void MC_assert(int prop)
517 {
518   if (MC_IS_ENABLED && !prop) {
519     XBT_INFO("**************************");
520     XBT_INFO("*** PROPERTY NOT VALID ***");
521     XBT_INFO("**************************");
522     XBT_INFO("Counter-example execution trace:");
523     MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
524     MC_print_statistics(mc_stats);
525     xbt_abort();
526   }
527 }
528
529 void MC_assert_stateful(int prop)
530 {
531   if (MC_IS_ENABLED && !prop) {
532     XBT_INFO("**************************");
533     XBT_INFO("*** PROPERTY NOT VALID ***");
534     XBT_INFO("**************************");
535     XBT_INFO("Counter-example execution trace:");
536     MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
537     MC_print_statistics(mc_stats);
538     xbt_abort();
539   }
540 }
541
542 void MC_assert_pair_stateful(int prop){
543   if (MC_IS_ENABLED && !prop) {
544     XBT_INFO("**************************");
545     XBT_INFO("*** PROPERTY NOT VALID ***");
546     XBT_INFO("**************************");
547     //XBT_INFO("Counter-example execution trace:");
548     MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
549     //MC_dump_snapshot_stack(mc_snapshot_stack);
550     MC_print_statistics_pairs(mc_stats_pair);
551     xbt_abort();
552   }
553 }
554
555 void MC_assert_pair_stateless(int prop){
556   if (MC_IS_ENABLED && !prop) {
557     XBT_INFO("**************************");
558     XBT_INFO("*** PROPERTY NOT VALID ***");
559     XBT_INFO("**************************");
560     //XBT_INFO("Counter-example execution trace:");
561     MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
562     //MC_dump_snapshot_stack(mc_snapshot_stack);
563     MC_print_statistics_pairs(mc_stats_pair);
564     xbt_abort();
565   }
566 }
567
568 void MC_process_clock_add(smx_process_t process, double amount)
569 {
570   mc_time[process->pid] += amount;
571 }
572
573 double MC_process_clock_get(smx_process_t process)
574 {
575   if(mc_time)
576     return mc_time[process->pid];
577   else
578     return 0;
579 }