Logo AND Algorithmique Numérique Distribuée

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