Logo AND Algorithmique Numérique Distribuée

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