Logo AND Algorithmique Numérique Distribuée

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