Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : add name of program in arguments of function MSG_main_liveness_statel...
[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_ddfs_stateful_init(a);
120   //MC_dpor2_init(a);
121   //MC_dpor3_init(a);
122 }
123
124 void MC_init_liveness_stateless(xbt_automaton_t a, char *prgm){
125
126   XBT_DEBUG("Start init mc");
127   
128   mc_time = xbt_new0(double, simix_process_maxpid);
129
130   /* Initialize the data structures that must be persistent across every
131      iteration of the model-checker (in RAW memory) */
132
133   MC_SET_RAW_MEM;
134
135   /* Initialize statistics */
136   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
137
138   XBT_DEBUG("Creating stack");
139
140  /* Create exploration stack */
141   mc_stack_liveness_stateless = xbt_fifo_new();
142
143   MC_UNSET_RAW_MEM;
144
145   MC_ddfs_stateless_init(a, prgm);
146
147   
148 }
149
150
151 void MC_modelcheck(void)
152 {
153   MC_init_safety_stateless();
154   MC_dpor();
155   MC_exit();
156 }
157
158 void MC_modelcheck_stateful(void)
159 {
160   MC_init_safety_stateful();
161   MC_dpor_stateful();
162   MC_exit();
163 }
164
165 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
166   MC_init_liveness_stateful(a);
167   MC_exit_liveness();
168 }
169
170 void MC_modelcheck_liveness_stateless(xbt_automaton_t a, char *prgm){
171   MC_init_liveness_stateless(a, prgm);
172   MC_exit_liveness();
173 }
174
175 void MC_exit_liveness(void)
176 {
177   MC_print_statistics_pairs(mc_stats_pair);
178   //xbt_free(mc_time);
179   MC_memory_exit();
180 }
181
182
183 void MC_exit(void)
184 {
185   MC_print_statistics(mc_stats);
186   xbt_free(mc_time);
187   MC_memory_exit();
188 }
189
190
191 int MC_random(int min, int max)
192 {
193   /*FIXME: return mc_current_state->executed_transition->random.value;*/
194   return 0;
195 }
196
197 /**
198  * \brief Schedules all the process that are ready to run
199  */
200 void MC_wait_for_requests(void)
201 {
202   smx_process_t process;
203   smx_req_t req;
204   unsigned int iter;
205
206   while (xbt_dynar_length(simix_global->process_to_run)) {
207     SIMIX_process_runall();
208     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
209       req = &process->request;
210       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
211           SIMIX_request_pre(req, 0);
212     }
213   }
214 }
215
216 int MC_deadlock_check()
217 {
218   int deadlock = FALSE;
219   smx_process_t process;
220   if(xbt_swag_size(simix_global->process_list)){
221     deadlock = TRUE;
222     xbt_swag_foreach(process, simix_global->process_list){
223       if(process->request.call != REQ_NO_REQ
224          && MC_request_is_enabled(&process->request)){
225         deadlock = FALSE;
226         break;
227       }
228     }
229   }
230   return deadlock;
231 }
232
233 /**
234  * \brief Re-executes from the initial state all the transitions indicated by
235  *        a given model-checker stack.
236  * \param stack The stack with the transitions to execute.
237 */
238 void MC_replay(xbt_fifo_t stack)
239 {
240   int value;
241   char *req_str;
242   smx_req_t req = NULL, saved_req = NULL;
243   xbt_fifo_item_t item;
244   mc_state_t state;
245
246   XBT_DEBUG("**** Begin Replay ****");
247
248   /* Restore the initial state */
249   MC_restore_snapshot(initial_snapshot);
250   /* At the moment of taking the snapshot the raw heap was set, so restoring
251    * it will set it back again, we have to unset it to continue  */
252   MC_UNSET_RAW_MEM;
253
254   /* Traverse the stack from the initial state and re-execute the transitions */
255   for (item = xbt_fifo_get_last_item(stack);
256        item != xbt_fifo_get_first_item(stack);
257        item = xbt_fifo_get_prev_item(item)) {
258
259     state = (mc_state_t) xbt_fifo_get_item_content(item);
260     saved_req = MC_state_get_executed_request(state, &value);
261    
262     if(saved_req){
263       /* because we got a copy of the executed request, we have to fetch the  
264          real one, pointed by the request field of the issuer process */
265       req = &saved_req->issuer->request;
266
267       /* Debug information */
268       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
269         req_str = MC_request_to_string(req, value);
270         XBT_DEBUG("Replay: %s (%p)", req_str, state);
271         xbt_free(req_str);
272       }
273     }
274          
275     SIMIX_request_pre(req, value);
276     MC_wait_for_requests();
277          
278     /* Update statistics */
279     mc_stats->visited_states++;
280     mc_stats->executed_transitions++;
281   }
282   XBT_DEBUG("**** End Replay ****");
283 }
284
285 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
286 {
287   int value;
288   char *req_str;
289   smx_req_t req = NULL, saved_req = NULL;
290   xbt_fifo_item_t item;
291   mc_state_t state;
292   mc_pair_stateless_t pair;
293   int depth = 1;
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   if(all_stack){
304
305     item = xbt_fifo_get_last_item(stack);
306
307     while(depth <= xbt_fifo_size(stack)){
308
309       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
310       state = (mc_state_t) pair->graph_state;
311
312       if(pair->requests > 0){
313    
314         saved_req = MC_state_get_executed_request(state, &value);
315         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
316       
317         if(saved_req != NULL){
318           /* because we got a copy of the executed request, we have to fetch the  
319              real one, pointed by the request field of the issuer process */
320           req = &saved_req->issuer->request;
321           //XBT_DEBUG("Req->call %u", req->call);
322         
323           /* Debug information */
324           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
325             req_str = MC_request_to_string(req, value);
326             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
327             xbt_free(req_str);
328           }
329         
330         }
331  
332         SIMIX_request_pre(req, value);
333         MC_wait_for_requests();
334       }
335
336       depth++;
337     
338       /* Update statistics */
339       mc_stats_pair->visited_pairs++;
340
341       item = xbt_fifo_get_prev_item(item);
342     }
343
344   }else{
345
346     /* Traverse the stack from the initial state and re-execute the transitions */
347     for (item = xbt_fifo_get_last_item(stack);
348          item != xbt_fifo_get_first_item(stack);
349          item = xbt_fifo_get_prev_item(item)) {
350
351       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
352       state = (mc_state_t) pair->graph_state;
353
354       if(pair->requests > 0){
355    
356         saved_req = MC_state_get_executed_request(state, &value);
357         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
358       
359         if(saved_req != NULL){
360           /* because we got a copy of the executed request, we have to fetch the  
361              real one, pointed by the request field of the issuer process */
362           req = &saved_req->issuer->request;
363           //XBT_DEBUG("Req->call %u", req->call);
364         
365           /* Debug information */
366           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
367             req_str = MC_request_to_string(req, value);
368             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
369             xbt_free(req_str);
370           }
371         
372         }
373  
374         SIMIX_request_pre(req, value);
375         MC_wait_for_requests();
376       }
377
378       depth++;
379     
380       /* Update statistics */
381       mc_stats_pair->visited_pairs++;
382     }
383   }  
384
385   XBT_DEBUG("**** End Replay ****");
386 }
387
388
389 /**
390  * \brief Dumps the contents of a model-checker's stack and shows the actual
391  *        execution trace
392  * \param stack The stack to dump
393 */
394 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
395 {
396   mc_state_t state;
397
398   MC_show_stack_safety_stateless(stack);
399
400   MC_SET_RAW_MEM;
401   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
402     MC_state_delete(state);
403   MC_UNSET_RAW_MEM;
404 }
405
406
407 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
408 {
409   int value;
410   mc_state_t state;
411   xbt_fifo_item_t item;
412   smx_req_t req;
413   char *req_str = NULL;
414   
415   for (item = xbt_fifo_get_last_item(stack);
416        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
417         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
418     req = MC_state_get_executed_request(state, &value);
419     if(req){
420       req_str = MC_request_to_string(req, value);
421       XBT_INFO("%s", req_str);
422       xbt_free(req_str);
423     }
424   }
425 }
426
427 void MC_show_deadlock(smx_req_t req)
428 {
429   /*char *req_str = NULL;*/
430   XBT_INFO("**************************");
431   XBT_INFO("*** DEAD-LOCK DETECTED ***");
432   XBT_INFO("**************************");
433   XBT_INFO("Locked request:");
434   /*req_str = MC_request_to_string(req);
435   XBT_INFO("%s", req_str);
436   xbt_free(req_str);*/
437   XBT_INFO("Counter-example execution trace:");
438   MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
439 }
440
441 void MC_show_deadlock_stateful(smx_req_t req)
442 {
443   /*char *req_str = NULL;*/
444   XBT_INFO("**************************");
445   XBT_INFO("*** DEAD-LOCK DETECTED ***");
446   XBT_INFO("**************************");
447   XBT_INFO("Locked request:");
448   /*req_str = MC_request_to_string(req);
449   XBT_INFO("%s", req_str);
450   xbt_free(req_str);*/
451   XBT_INFO("Counter-example execution trace:");
452   MC_show_stack_safety_stateful(mc_stack_safety_stateful);
453 }
454
455 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
456 {
457   //mc_state_ws_t state;
458
459   MC_show_stack_safety_stateful(stack);
460
461   /*MC_SET_RAW_MEM;
462   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
463     MC_state_delete(state);
464     MC_UNSET_RAW_MEM;*/
465 }
466
467
468 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
469 {
470   int value;
471   mc_state_ws_t state;
472   xbt_fifo_item_t item;
473   smx_req_t req;
474   char *req_str = NULL;
475   
476   for (item = xbt_fifo_get_last_item(stack);
477        (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
478         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
479     req = MC_state_get_executed_request(state->graph_state, &value);
480     if(req){
481       req_str = MC_request_to_string(req, value);
482       XBT_INFO("%s", req_str);
483       xbt_free(req_str);
484     }
485   }
486 }
487
488 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
489   int value;
490   mc_pair_t pair;
491   xbt_fifo_item_t item;
492   smx_req_t req;
493   char *req_str = NULL;
494   
495   for (item = xbt_fifo_get_last_item(stack);
496        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
497         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
498     req = MC_state_get_executed_request(pair->graph_state, &value);
499     if(req){
500       req_str = MC_request_to_string(req, value);
501       XBT_INFO("%s", req_str);
502       xbt_free(req_str);
503     }
504   }
505 }
506
507 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
508   mc_pair_t pair;
509
510   MC_SET_RAW_MEM;
511   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
512     MC_pair_delete(pair);
513   MC_UNSET_RAW_MEM;
514 }
515
516 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
517   int value;
518   mc_pair_stateless_t pair;
519   xbt_fifo_item_t item;
520   smx_req_t req;
521   char *req_str = NULL;
522   
523   for (item = xbt_fifo_get_last_item(stack);
524        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
525         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
526     req = MC_state_get_executed_request(pair->graph_state, &value);
527     if(req){
528       if(pair->requests>0){
529         req_str = MC_request_to_string(req, value);
530         XBT_INFO("%s", req_str);
531         xbt_free(req_str);
532       }else{
533         XBT_INFO("End of system requests but evolution in Büchi automaton");
534       }
535     }
536   }
537 }
538
539 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
540   mc_pair_stateless_t pair;
541
542   MC_SET_RAW_MEM;
543   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
544     MC_pair_stateless_delete(pair);
545   MC_UNSET_RAW_MEM;
546 }
547
548
549 void MC_print_statistics(mc_stats_t stats)
550 {
551   XBT_INFO("State space size ~= %lu", stats->state_size);
552   XBT_INFO("Expanded states = %lu", stats->expanded_states);
553   XBT_INFO("Visited states = %lu", stats->visited_states);
554   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
555   XBT_INFO("Expanded / Visited = %lf",
556         (double) stats->visited_states / stats->expanded_states);
557   /*XBT_INFO("Exploration coverage = %lf",
558      (double)stats->expanded_states / stats->state_size); */
559 }
560
561 void MC_print_statistics_pairs(mc_stats_pair_t stats)
562 {
563   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
564   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
565   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
566   XBT_INFO("Expanded / Visited = %lf",
567         (double) stats->visited_pairs / stats->expanded_pairs);
568   /*XBT_INFO("Exploration coverage = %lf",
569      (double)stats->expanded_states / stats->state_size); */
570 }
571
572 void MC_assert(int prop)
573 {
574   if (MC_IS_ENABLED && !prop) {
575     XBT_INFO("**************************");
576     XBT_INFO("*** PROPERTY NOT VALID ***");
577     XBT_INFO("**************************");
578     XBT_INFO("Counter-example execution trace:");
579     MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
580     MC_print_statistics(mc_stats);
581     xbt_abort();
582   }
583 }
584
585 void MC_assert_stateful(int prop)
586 {
587   if (MC_IS_ENABLED && !prop) {
588     XBT_INFO("**************************");
589     XBT_INFO("*** PROPERTY NOT VALID ***");
590     XBT_INFO("**************************");
591     XBT_INFO("Counter-example execution trace:");
592     MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
593     MC_print_statistics(mc_stats);
594     xbt_abort();
595   }
596 }
597
598 void MC_assert_pair_stateful(int prop){
599   if (MC_IS_ENABLED && !prop) {
600     XBT_INFO("**************************");
601     XBT_INFO("*** PROPERTY NOT VALID ***");
602     XBT_INFO("**************************");
603     //XBT_INFO("Counter-example execution trace:");
604     MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
605     //MC_dump_snapshot_stack(mc_snapshot_stack);
606     MC_print_statistics_pairs(mc_stats_pair);
607     xbt_abort();
608   }
609 }
610
611 void MC_assert_pair_stateless(int prop){
612   if (MC_IS_ENABLED && !prop) {
613     XBT_INFO("**************************");
614     XBT_INFO("*** PROPERTY NOT VALID ***");
615     XBT_INFO("**************************");
616     //XBT_INFO("Counter-example execution trace:");
617     MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
618     //MC_dump_snapshot_stack(mc_snapshot_stack);
619     MC_print_statistics_pairs(mc_stats_pair);
620     xbt_abort();
621   }
622 }
623
624 void MC_process_clock_add(smx_process_t process, double amount)
625 {
626   mc_time[process->pid] += amount;
627 }
628
629 double MC_process_clock_get(smx_process_t process)
630 {
631   if(mc_time)
632     return mc_time[process->pid];
633   else
634     return 0;
635 }