Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9fa095a3e374b1ac14c5983017f199ed21a56b07
[simgrid.git] / src / mc / mc_global.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4
5 #include "../surf/surf_private.h"
6 #include "../simix/private.h"
7 #include "xbt/fifo.h"
8 #include "private.h"
9
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
12                                 "Logging specific to MC (global)");
13
14 /* MC global data structures */
15 mc_snapshot_t initial_snapshot = NULL;
16 xbt_fifo_t mc_stack = NULL;
17 xbt_setset_t mc_setset = NULL;
18 mc_stats_t mc_stats = NULL;
19 mc_state_t mc_current_state = NULL;
20 char mc_replay_mode = FALSE;
21
22 /**
23  *  \brief Initialize the model-checker data structures
24  */
25 void MC_init(void)
26 {
27   smx_process_t process;
28   
29   /* Check if MC is already initialized */
30   if (initial_snapshot)
31     return;
32
33   /* Initialize the data structures that must be persistent across every
34      iteration of the model-checker (in RAW memory) */
35   MC_SET_RAW_MEM;
36
37   /* Initialize statistics */
38   mc_stats = xbt_new0(s_mc_stats_t, 1);
39   mc_stats->state_size = 1;
40
41   /* Create exploration stack */
42   mc_stack = xbt_fifo_new();
43
44   /* Create the container for the sets */
45   mc_setset = xbt_setset_new(20);
46
47   /* Add the existing processes to mc's setset so they have an ID designated */
48   /* FIXME: check what happen if there are processes created during the exploration */
49   xbt_swag_foreach(process, simix_global->process_list){
50     xbt_setset_elm_add(mc_setset, process);
51   }
52   MC_UNSET_RAW_MEM;
53
54   MC_dpor_init();
55
56   MC_SET_RAW_MEM;
57   /* Save the initial state */
58   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
59   MC_take_snapshot(initial_snapshot);
60   MC_UNSET_RAW_MEM;
61 }
62
63 void MC_modelcheck(void)
64 {
65   MC_init();
66   MC_dpor();
67   MC_exit();
68 }
69
70 void MC_exit(void)
71 {
72   MC_memory_exit();
73 }
74
75 int MC_random(int min, int max)
76 {
77   /*FIXME: return mc_current_state->executed_transition->random.value;*/
78   return 0;
79 }
80
81 /**
82  * \brief Re-executes from the initial state all the transitions indicated by
83  *        a given model-checker stack.
84  * \param stack The stack with the transitions to execute.
85 */
86 void MC_replay(xbt_fifo_t stack)
87 {
88   char *req_str;
89   smx_req_t req = NULL, saved_req = NULL;
90   xbt_fifo_item_t item;
91   mc_state_t state;
92
93   DEBUG0("**** Begin Replay ****");
94
95   /* Restore the initial state */
96   MC_restore_snapshot(initial_snapshot);
97   /* At the moment of taking the snapshot the raw heap was set, so restoring
98    * it will set it back again, we have to unset it to continue  */
99   MC_UNSET_RAW_MEM;
100
101   /* Traverse the stack from the initial state and re-execute the transitions */
102   for (item = xbt_fifo_get_last_item(stack);
103        item != xbt_fifo_get_first_item(stack);
104        item = xbt_fifo_get_prev_item(item)) {
105
106     state = (mc_state_t) xbt_fifo_get_item_content(item);
107     saved_req = MC_state_get_executed_request(state);
108    
109     if(saved_req){
110       /* because we got a copy of the executed request, we have to fetch the  
111          real one, pointed by the request field of the issuer process */
112       req = &saved_req->issuer->request;
113
114       /* Debug information */
115       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
116         req_str = MC_request_to_string(req); 
117         DEBUG2("Replay: %s (%p)", req_str, state);
118         xbt_free(req_str);
119       }
120     }
121          
122     SIMIX_request_pre(req);
123     MC_wait_for_requests();
124          
125     /* Update statistics */
126     mc_stats->visited_states++;
127     mc_stats->executed_transitions++;
128   }
129   DEBUG0("**** End Replay ****");
130 }
131
132 /**
133  * \brief Dumps the contents of a model-checker's stack and shows the actual
134  *        execution trace
135  * \param stack The stack to dump
136 */
137 void MC_dump_stack(xbt_fifo_t stack)
138 {
139   mc_state_t state;
140
141   MC_show_stack(stack);
142
143   MC_SET_RAW_MEM;
144   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
145     MC_state_delete(state);
146   MC_UNSET_RAW_MEM;
147 }
148
149 void MC_show_stack(xbt_fifo_t stack)
150 {
151   mc_state_t state;
152   xbt_fifo_item_t item;
153   smx_req_t req;
154   char *req_str = NULL;
155   
156   for (item = xbt_fifo_get_last_item(stack);
157        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
158         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
159     req = MC_state_get_executed_request(state);
160     if(req){
161       req_str = MC_request_to_string(req); 
162       INFO1("%s", req_str);
163       xbt_free(req_str);
164     }
165   }
166 }
167
168 /**
169  * \brief Schedules all the process that are ready to run
170  */
171 void MC_wait_for_requests(void)
172 {
173   char *req_str = NULL;
174   smx_req_t req = NULL;
175
176   do {
177     SIMIX_context_runall(simix_global->process_to_run);
178     while((req = SIMIX_request_pop())){
179       if(!SIMIX_request_is_visible(req))
180         SIMIX_request_pre(req);
181       else if(req->call == REQ_COMM_WAITANY)
182         THROW_UNIMPLEMENTED;
183       else if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
184         req_str = MC_request_to_string(req);
185         DEBUG1("Got: %s", req_str);
186         xbt_free(req_str);
187       }
188     }
189   } while (xbt_dynar_length(simix_global->process_to_run));
190 }
191
192 /****************************** Statistics ************************************/
193 void MC_print_statistics(mc_stats_t stats)
194 {
195   INFO1("State space size ~= %lu", stats->state_size);
196   INFO1("Expanded states = %lu", stats->expanded_states);
197   INFO1("Visited states = %lu", stats->visited_states);
198   INFO1("Executed transitions = %lu", stats->executed_transitions);
199   INFO1("Expanded / Visited = %lf",
200         (double) stats->visited_states / stats->expanded_states);
201   /*INFO1("Exploration coverage = %lf", 
202      (double)stats->expanded_states / stats->state_size); */
203 }
204
205 /************************* Assertion Checking *********************************/
206 void MC_assert(int prop)
207 {
208   if (MC_IS_ENABLED && !prop) {
209     INFO0("**************************");
210     INFO0("*** PROPERTY NOT VALID ***");
211     INFO0("**************************");
212     INFO0("Counter-example execution trace:");
213     MC_dump_stack(mc_stack);
214     MC_print_statistics(mc_stats);
215     xbt_abort();
216   }
217 }
218
219 void MC_show_deadlock(smx_req_t req)
220 {
221   char *req_str = NULL;
222   INFO0("**************************");
223   INFO0("*** DEAD-LOCK DETECTED ***");
224   INFO0("**************************");
225   INFO0("Locked request:");
226   req_str = MC_request_to_string(req);
227   INFO1("%s", req_str);
228   xbt_free(req_str);
229   INFO0("Counter-example execution trace:");
230   MC_dump_stack(mc_stack);
231 }