Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
203b7aa8c75da45c4e07271dda952f7d983bcf0a
[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 mc_stats_t mc_stats = NULL;
18 mc_state_t mc_current_state = NULL;
19 char mc_replay_mode = FALSE;
20 double *mc_time = NULL;
21 xbt_fifo_t mc_snapshot_stack = NULL;
22
23 /**
24  *  \brief Initialize the model-checker data structures
25  */
26 void MC_init(void)
27 {
28   /* Check if MC is already initialized */
29   if (initial_snapshot)
30     return;
31
32   mc_time = xbt_new0(double, simix_process_maxpid);
33
34   /* Initialize the data structures that must be persistent across every
35      iteration of the model-checker (in RAW memory) */
36   MC_SET_RAW_MEM;
37
38   /* Initialize statistics */
39   mc_stats = xbt_new0(s_mc_stats_t, 1);
40   mc_stats->state_size = 1;
41
42   /* Create exploration stack */
43   mc_stack = xbt_fifo_new();
44
45   MC_UNSET_RAW_MEM;
46
47   MC_dpor_init();
48
49   MC_SET_RAW_MEM;
50   /* Save the initial state */
51   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
52   MC_take_snapshot(initial_snapshot);
53   MC_UNSET_RAW_MEM;
54 }
55
56 void MC_init_with_automaton(xbt_automaton_t a){
57   
58   mc_time = xbt_new0(double, simix_process_maxpid);
59
60   /* Initialize the data structures that must be persistent across every
61      iteration of the model-checker (in RAW memory) */
62   MC_SET_RAW_MEM;
63
64   /* Initialize statistics */
65   mc_stats = xbt_new0(s_mc_stats_t, 1);
66   mc_stats->state_size = 1;
67
68   XBT_DEBUG("Creating snapshot_stack");
69
70  /* Create exploration stack */
71   mc_snapshot_stack = xbt_fifo_new();
72
73   MC_UNSET_RAW_MEM;
74
75   MC_dfs_init(a);
76 }
77
78
79 void MC_modelcheck(void)
80 {
81   MC_init();
82   MC_dpor();
83   MC_exit();
84 }
85
86 void MC_modelcheck_with_automaton(xbt_automaton_t a){
87   MC_init_with_automaton(a);
88   MC_exit();
89 }
90
91 void MC_exit(void)
92 {
93   MC_print_statistics(mc_stats);
94   xbt_free(mc_time);
95   MC_memory_exit();
96 }
97
98 int MC_random(int min, int max)
99 {
100   /*FIXME: return mc_current_state->executed_transition->random.value;*/
101   return 0;
102 }
103
104 /**
105  * \brief Schedules all the process that are ready to run
106  */
107 void MC_wait_for_requests(void)
108 {
109   smx_process_t process;
110   smx_req_t req;
111   unsigned int iter;
112
113   while (xbt_dynar_length(simix_global->process_to_run)) {
114     SIMIX_process_runall();
115     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
116       req = &process->request;
117       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
118           SIMIX_request_pre(req, 0);
119     }
120   }
121 }
122
123 int MC_deadlock_check()
124 {
125   int deadlock = FALSE;
126   smx_process_t process;
127   if(xbt_swag_size(simix_global->process_list)){
128     deadlock = TRUE;
129     xbt_swag_foreach(process, simix_global->process_list){
130       if(process->request.call != REQ_NO_REQ
131          && MC_request_is_enabled(&process->request)){
132         deadlock = FALSE;
133         break;
134       }
135     }
136   }
137   return deadlock;
138 }
139
140 /**
141  * \brief Re-executes from the initial state all the transitions indicated by
142  *        a given model-checker stack.
143  * \param stack The stack with the transitions to execute.
144 */
145 void MC_replay(xbt_fifo_t stack)
146 {
147   int value;
148   char *req_str;
149   smx_req_t req = NULL, saved_req = NULL;
150   xbt_fifo_item_t item;
151   mc_state_t state;
152
153   XBT_DEBUG("**** Begin Replay ****");
154
155   /* Restore the initial state */
156   MC_restore_snapshot(initial_snapshot);
157   /* At the moment of taking the snapshot the raw heap was set, so restoring
158    * it will set it back again, we have to unset it to continue  */
159   MC_UNSET_RAW_MEM;
160
161   /* Traverse the stack from the initial state and re-execute the transitions */
162   for (item = xbt_fifo_get_last_item(stack);
163        item != xbt_fifo_get_first_item(stack);
164        item = xbt_fifo_get_prev_item(item)) {
165
166     state = (mc_state_t) xbt_fifo_get_item_content(item);
167     saved_req = MC_state_get_executed_request(state, &value);
168    
169     if(saved_req){
170       /* because we got a copy of the executed request, we have to fetch the  
171          real one, pointed by the request field of the issuer process */
172       req = &saved_req->issuer->request;
173
174       /* Debug information */
175       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
176         req_str = MC_request_to_string(req, value);
177         XBT_DEBUG("Replay: %s (%p)", req_str, state);
178         xbt_free(req_str);
179       }
180     }
181          
182     SIMIX_request_pre(req, value);
183     MC_wait_for_requests();
184          
185     /* Update statistics */
186     mc_stats->visited_states++;
187     mc_stats->executed_transitions++;
188   }
189   XBT_DEBUG("**** End Replay ****");
190 }
191
192 /**
193  * \brief Dumps the contents of a model-checker's stack and shows the actual
194  *        execution trace
195  * \param stack The stack to dump
196 */
197 void MC_dump_stack(xbt_fifo_t stack)
198 {
199   mc_state_t state;
200
201   MC_show_stack(stack);
202
203   MC_SET_RAW_MEM;
204   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
205     MC_state_delete(state);
206   MC_UNSET_RAW_MEM;
207 }
208
209 void MC_show_stack(xbt_fifo_t stack)
210 {
211   int value;
212   mc_state_t state;
213   xbt_fifo_item_t item;
214   smx_req_t req;
215   char *req_str = NULL;
216   
217   for (item = xbt_fifo_get_last_item(stack);
218        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
219         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
220     req = MC_state_get_executed_request(state, &value);
221     if(req){
222       req_str = MC_request_to_string(req, value);
223       XBT_INFO("%s", req_str);
224       xbt_free(req_str);
225     }
226   }
227 }
228
229 void MC_show_deadlock(smx_req_t req)
230 {
231   /*char *req_str = NULL;*/
232   XBT_INFO("**************************");
233   XBT_INFO("*** DEAD-LOCK DETECTED ***");
234   XBT_INFO("**************************");
235   XBT_INFO("Locked request:");
236   /*req_str = MC_request_to_string(req);
237   XBT_INFO("%s", req_str);
238   xbt_free(req_str);*/
239   XBT_INFO("Counter-example execution trace:");
240   MC_dump_stack(mc_stack);
241 }
242
243 void MC_print_statistics(mc_stats_t stats)
244 {
245   XBT_INFO("State space size ~= %lu", stats->state_size);
246   XBT_INFO("Expanded states = %lu", stats->expanded_states);
247   XBT_INFO("Visited states = %lu", stats->visited_states);
248   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
249   XBT_INFO("Expanded / Visited = %lf",
250         (double) stats->visited_states / stats->expanded_states);
251   /*XBT_INFO("Exploration coverage = %lf",
252      (double)stats->expanded_states / stats->state_size); */
253 }
254
255 void MC_assert(int prop)
256 {
257   if (MC_IS_ENABLED && !prop) {
258     XBT_INFO("**************************");
259     XBT_INFO("*** PROPERTY NOT VALID ***");
260     XBT_INFO("**************************");
261     XBT_INFO("Counter-example execution trace:");
262     MC_dump_stack(mc_stack);
263     MC_print_statistics(mc_stats);
264     xbt_abort();
265   }
266 }
267
268 void MC_process_clock_add(smx_process_t process, double amount)
269 {
270   mc_time[process->pid] += amount;
271 }
272
273 double MC_process_clock_get(smx_process_t process)
274 {
275   if(mc_time)
276     return mc_time[process->pid];
277   else
278     return 0;
279 }