Logo AND Algorithmique Numérique Distribuée

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