Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into MC_LTL
[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 mc_snapshot_t initial_snapshot_liveness = NULL;
34
35
36 /**
37  *  \brief Initialize the model-checker data structures
38  */
39 void MC_init_safety_stateless(void)
40 {
41
42   /* Check if MC is already initialized */
43   if (initial_snapshot)
44     return;
45
46   mc_time = xbt_new0(double, simix_process_maxpid);
47
48   /* Initialize the data structures that must be persistent across every
49      iteration of the model-checker (in RAW memory) */
50   MC_SET_RAW_MEM;
51
52   /* Initialize statistics */
53   mc_stats = xbt_new0(s_mc_stats_t, 1);
54   mc_stats->state_size = 1;
55
56   /* Create exploration stack */
57   mc_stack_safety_stateless = xbt_fifo_new();
58
59   MC_UNSET_RAW_MEM;
60
61   MC_dpor_init();
62
63   MC_SET_RAW_MEM;
64   /* Save the initial state */
65   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
66   MC_take_snapshot(initial_snapshot);
67   MC_UNSET_RAW_MEM;
68 }
69
70 void MC_init_safety_stateful(void){
71
72   
73    /* Check if MC is already initialized */
74   if (initial_snapshot)
75     return;
76
77   mc_time = xbt_new0(double, simix_process_maxpid);
78
79   /* Initialize the data structures that must be persistent across every
80      iteration of the model-checker (in RAW memory) */
81   MC_SET_RAW_MEM;
82
83   /* Initialize statistics */
84   mc_stats = xbt_new0(s_mc_stats_t, 1);
85   mc_stats->state_size = 1;
86
87   /* Create exploration stack */
88   mc_stack_safety_stateful = xbt_fifo_new();
89
90   MC_UNSET_RAW_MEM;
91
92   MC_dpor_stateful_init();
93
94
95 }
96
97 void MC_init_liveness_stateful(xbt_automaton_t a){
98
99   XBT_DEBUG("Start init mc");
100   
101   mc_time = xbt_new0(double, simix_process_maxpid);
102
103   /* Initialize the data structures that must be persistent across every
104      iteration of the model-checker (in RAW memory) */
105
106   MC_SET_RAW_MEM;
107
108   /* Initialize statistics */
109   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
110   //mc_stats = xbt_new0(s_mc_stats_t, 1);
111
112   XBT_DEBUG("Creating snapshot_stack");
113
114  /* Create exploration stack */
115   mc_stack_liveness_stateful = xbt_fifo_new();
116
117
118   MC_UNSET_RAW_MEM;
119
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, char *prgm){
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, prgm);
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, char *prgm){
172   MC_init_liveness_stateless(a, prgm);
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   exit(0);
182 }
183
184
185 void MC_exit(void)
186 {
187   MC_print_statistics(mc_stats);
188   xbt_free(mc_time);
189   MC_memory_exit();
190 }
191
192
193 int MC_random(int min, int max)
194 {
195   /*FIXME: return mc_current_state->executed_transition->random.value;*/
196   return 0;
197 }
198
199 /**
200  * \brief Schedules all the process that are ready to run
201  */
202 void MC_wait_for_requests(void)
203 {
204   smx_process_t process;
205   smx_req_t req;
206   unsigned int iter;
207
208   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
209     SIMIX_process_runall();
210     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
211       req = &process->request;
212       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
213           SIMIX_request_pre(req, 0);
214     }
215   }
216 }
217
218 int MC_deadlock_check()
219 {
220   int deadlock = FALSE;
221   smx_process_t process;
222   if(xbt_swag_size(simix_global->process_list)){
223     deadlock = TRUE;
224     xbt_swag_foreach(process, simix_global->process_list){
225       if(process->request.call != REQ_NO_REQ
226          && MC_request_is_enabled(&process->request)){
227         deadlock = FALSE;
228         break;
229       }
230     }
231   }
232   return deadlock;
233 }
234
235 /**
236  * \brief Re-executes from the initial state all the transitions indicated by
237  *        a given model-checker stack.
238  * \param stack The stack with the transitions to execute.
239 */
240 void MC_replay(xbt_fifo_t stack)
241 {
242   int value;
243   char *req_str;
244   smx_req_t req = NULL, saved_req = NULL;
245   xbt_fifo_item_t item;
246   mc_state_t state;
247
248   XBT_DEBUG("**** Begin Replay ****");
249
250   /* Restore the initial state */
251   MC_restore_snapshot(initial_snapshot);
252   /* At the moment of taking the snapshot the raw heap was set, so restoring
253    * it will set it back again, we have to unset it to continue  */
254   MC_UNSET_RAW_MEM;
255
256   /* Traverse the stack from the initial state and re-execute the transitions */
257   for (item = xbt_fifo_get_last_item(stack);
258        item != xbt_fifo_get_first_item(stack);
259        item = xbt_fifo_get_prev_item(item)) {
260
261     state = (mc_state_t) xbt_fifo_get_item_content(item);
262     saved_req = MC_state_get_executed_request(state, &value);
263    
264     if(saved_req){
265       /* because we got a copy of the executed request, we have to fetch the  
266          real one, pointed by the request field of the issuer process */
267       req = &saved_req->issuer->request;
268
269       /* Debug information */
270       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
271         req_str = MC_request_to_string(req, value);
272         XBT_DEBUG("Replay: %s (%p)", req_str, state);
273         xbt_free(req_str);
274       }
275     }
276          
277     SIMIX_request_pre(req, value);
278     MC_wait_for_requests();
279          
280     /* Update statistics */
281     mc_stats->visited_states++;
282     mc_stats->executed_transitions++;
283   }
284   XBT_DEBUG("**** End Replay ****");
285 }
286
287 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
288 {
289   int value;
290   char *req_str;
291   smx_req_t req = NULL, saved_req = NULL;
292   xbt_fifo_item_t item;
293   mc_state_t state;
294   mc_pair_stateless_t pair;
295   int depth = 1;
296
297   XBT_DEBUG("**** Begin Replay ****");
298
299   /* Restore the initial state */
300   MC_restore_snapshot(initial_snapshot_liveness);
301   /* At the moment of taking the snapshot the raw heap was set, so restoring
302    * it will set it back again, we have to unset it to continue  */
303   MC_UNSET_RAW_MEM;
304
305   if(all_stack){
306
307     item = xbt_fifo_get_last_item(stack);
308
309     while(depth <= xbt_fifo_size(stack)){
310
311       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
312       state = (mc_state_t) pair->graph_state;
313
314       if(pair->requests > 0){
315    
316         saved_req = MC_state_get_executed_request(state, &value);
317         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
318       
319         if(saved_req != NULL){
320           /* because we got a copy of the executed request, we have to fetch the  
321              real one, pointed by the request field of the issuer process */
322           req = &saved_req->issuer->request;
323           //XBT_DEBUG("Req->call %u", req->call);
324         
325           /* Debug information */
326           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
327             req_str = MC_request_to_string(req, value);
328             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
329             xbt_free(req_str);
330           }
331         
332         }
333  
334         SIMIX_request_pre(req, value);
335         MC_wait_for_requests();
336       }
337
338       depth++;
339     
340       /* Update statistics */
341       mc_stats_pair->visited_pairs++;
342
343       item = xbt_fifo_get_prev_item(item);
344     }
345
346   }else{
347
348     /* Traverse the stack from the initial state and re-execute the transitions */
349     for (item = xbt_fifo_get_last_item(stack);
350          item != xbt_fifo_get_first_item(stack);
351          item = xbt_fifo_get_prev_item(item)) {
352
353       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
354       state = (mc_state_t) pair->graph_state;
355
356       if(pair->requests > 0){
357    
358         saved_req = MC_state_get_executed_request(state, &value);
359         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
360       
361         if(saved_req != NULL){
362           /* because we got a copy of the executed request, we have to fetch the  
363              real one, pointed by the request field of the issuer process */
364           req = &saved_req->issuer->request;
365           //XBT_DEBUG("Req->call %u", req->call);
366         
367           /* Debug information */
368           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
369             req_str = MC_request_to_string(req, value);
370             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
371             xbt_free(req_str);
372           }
373         
374         }
375  
376         SIMIX_request_pre(req, value);
377         MC_wait_for_requests();
378       }
379
380       depth++;
381     
382       /* Update statistics */
383       mc_stats_pair->visited_pairs++;
384     }
385   }  
386
387   XBT_DEBUG("**** End Replay ****");
388 }
389
390
391 /**
392  * \brief Dumps the contents of a model-checker's stack and shows the actual
393  *        execution trace
394  * \param stack The stack to dump
395 */
396 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
397 {
398   mc_state_t state;
399
400   MC_show_stack_safety_stateless(stack);
401
402   MC_SET_RAW_MEM;
403   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
404     MC_state_delete(state);
405   MC_UNSET_RAW_MEM;
406 }
407
408
409 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
410 {
411   int value;
412   mc_state_t state;
413   xbt_fifo_item_t item;
414   smx_req_t req;
415   char *req_str = NULL;
416   
417   for (item = xbt_fifo_get_last_item(stack);
418        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
419         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
420     req = MC_state_get_executed_request(state, &value);
421     if(req){
422       req_str = MC_request_to_string(req, value);
423       XBT_INFO("%s", req_str);
424       xbt_free(req_str);
425     }
426   }
427 }
428
429 void MC_show_deadlock(smx_req_t req)
430 {
431   /*char *req_str = NULL;*/
432   XBT_INFO("**************************");
433   XBT_INFO("*** DEAD-LOCK DETECTED ***");
434   XBT_INFO("**************************");
435   XBT_INFO("Locked request:");
436   /*req_str = MC_request_to_string(req);
437   XBT_INFO("%s", req_str);
438   xbt_free(req_str);*/
439   XBT_INFO("Counter-example execution trace:");
440   MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
441 }
442
443 void MC_show_deadlock_stateful(smx_req_t req)
444 {
445   /*char *req_str = NULL;*/
446   XBT_INFO("**************************");
447   XBT_INFO("*** DEAD-LOCK DETECTED ***");
448   XBT_INFO("**************************");
449   XBT_INFO("Locked request:");
450   /*req_str = MC_request_to_string(req);
451   XBT_INFO("%s", req_str);
452   xbt_free(req_str);*/
453   XBT_INFO("Counter-example execution trace:");
454   MC_show_stack_safety_stateful(mc_stack_safety_stateful);
455 }
456
457 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
458 {
459   //mc_state_ws_t state;
460
461   MC_show_stack_safety_stateful(stack);
462
463   /*MC_SET_RAW_MEM;
464   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
465     MC_state_delete(state);
466     MC_UNSET_RAW_MEM;*/
467 }
468
469
470 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
471 {
472   int value;
473   mc_state_ws_t state;
474   xbt_fifo_item_t item;
475   smx_req_t req;
476   char *req_str = NULL;
477   
478   for (item = xbt_fifo_get_last_item(stack);
479        (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
480         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
481     req = MC_state_get_executed_request(state->graph_state, &value);
482     if(req){
483       req_str = MC_request_to_string(req, value);
484       XBT_INFO("%s", req_str);
485       xbt_free(req_str);
486     }
487   }
488 }
489
490 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
491   int value;
492   mc_pair_t pair;
493   xbt_fifo_item_t item;
494   smx_req_t req;
495   char *req_str = NULL;
496   
497   for (item = xbt_fifo_get_last_item(stack);
498        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
499         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
500     req = MC_state_get_executed_request(pair->graph_state, &value);
501     if(req){
502       req_str = MC_request_to_string(req, value);
503       XBT_INFO("%s", req_str);
504       xbt_free(req_str);
505     }
506   }
507 }
508
509 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
510   mc_pair_t pair;
511
512   MC_SET_RAW_MEM;
513   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
514     MC_pair_delete(pair);
515   MC_UNSET_RAW_MEM;
516 }
517
518 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
519   int value;
520   mc_pair_stateless_t pair;
521   xbt_fifo_item_t item;
522   smx_req_t req;
523   char *req_str = NULL;
524   
525   for (item = xbt_fifo_get_last_item(stack);
526        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
527         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
528     req = MC_state_get_executed_request(pair->graph_state, &value);
529     if(req){
530       if(pair->requests>0){
531         req_str = MC_request_to_string(req, value);
532         XBT_INFO("%s", req_str);
533         xbt_free(req_str);
534       }else{
535         XBT_INFO("End of system requests but evolution in Büchi automaton");
536       }
537     }
538   }
539 }
540
541 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
542   mc_pair_stateless_t pair;
543
544   MC_SET_RAW_MEM;
545   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
546     MC_pair_stateless_delete(pair);
547   MC_UNSET_RAW_MEM;
548 }
549
550
551 void MC_print_statistics(mc_stats_t stats)
552 {
553   XBT_INFO("State space size ~= %lu", stats->state_size);
554   XBT_INFO("Expanded states = %lu", stats->expanded_states);
555   XBT_INFO("Visited states = %lu", stats->visited_states);
556   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
557   XBT_INFO("Expanded / Visited = %lf",
558         (double) stats->visited_states / stats->expanded_states);
559   /*XBT_INFO("Exploration coverage = %lf",
560      (double)stats->expanded_states / stats->state_size); */
561 }
562
563 void MC_print_statistics_pairs(mc_stats_pair_t stats)
564 {
565   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
566   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
567   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
568   XBT_INFO("Expanded / Visited = %lf",
569         (double) stats->visited_pairs / stats->expanded_pairs);
570   /*XBT_INFO("Exploration coverage = %lf",
571      (double)stats->expanded_states / stats->state_size); */
572 }
573
574 void MC_assert(int prop)
575 {
576   if (MC_IS_ENABLED && !prop) {
577     XBT_INFO("**************************");
578     XBT_INFO("*** PROPERTY NOT VALID ***");
579     XBT_INFO("**************************");
580     XBT_INFO("Counter-example execution trace:");
581     MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
582     MC_print_statistics(mc_stats);
583     xbt_abort();
584   }
585 }
586
587 void MC_assert_stateful(int prop)
588 {
589   if (MC_IS_ENABLED && !prop) {
590     XBT_INFO("**************************");
591     XBT_INFO("*** PROPERTY NOT VALID ***");
592     XBT_INFO("**************************");
593     XBT_INFO("Counter-example execution trace:");
594     MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
595     MC_print_statistics(mc_stats);
596     xbt_abort();
597   }
598 }
599
600 void MC_assert_pair_stateful(int prop){
601   if (MC_IS_ENABLED && !prop) {
602     XBT_INFO("**************************");
603     XBT_INFO("*** PROPERTY NOT VALID ***");
604     XBT_INFO("**************************");
605     //XBT_INFO("Counter-example execution trace:");
606     MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
607     //MC_dump_snapshot_stack(mc_snapshot_stack);
608     MC_print_statistics_pairs(mc_stats_pair);
609     xbt_abort();
610   }
611 }
612
613 void MC_assert_pair_stateless(int prop){
614   if (MC_IS_ENABLED && !prop) {
615     XBT_INFO("**************************");
616     XBT_INFO("*** PROPERTY NOT VALID ***");
617     XBT_INFO("**************************");
618     //XBT_INFO("Counter-example execution trace:");
619     MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
620     //MC_dump_snapshot_stack(mc_snapshot_stack);
621     MC_print_statistics_pairs(mc_stats_pair);
622     xbt_abort();
623   }
624 }
625
626 void MC_process_clock_add(smx_process_t process, double amount)
627 {
628   mc_time[process->pid] += amount;
629 }
630
631 double MC_process_clock_get(smx_process_t process)
632 {
633   if(mc_time)
634     return mc_time[process->pid];
635   else
636     return 0;
637 }