Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert msg/replay to modern replay things even in the tracing parts (sorry)
[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   
53   MC_dpor_init();
54
55   /* Save the initial state */
56   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
57   MC_take_snapshot(initial_snapshot);
58   MC_UNSET_RAW_MEM;
59 }
60
61 void MC_modelcheck(void)
62 {
63   MC_init();
64   MC_dpor();
65   MC_exit();
66 }
67
68 void MC_exit(void)
69 {
70   MC_memory_exit();
71 }
72
73 int MC_random(int min, int max)
74 {
75   /*FIXME: return mc_current_state->executed_transition->random.value;*/
76   return 0;
77 }
78
79 /**
80  * \brief Re-executes from the initial state all the transitions indicated by
81  *        a given model-checker stack.
82  * \param stack The stack with the transitions to execute.
83 */
84 void MC_replay(xbt_fifo_t stack)
85 {
86   char *req_str;
87   smx_req_t req = NULL, saved_req = NULL;
88   xbt_fifo_item_t item;
89   mc_state_t state;
90
91   DEBUG0("**** Begin Replay ****");
92
93   /* Restore the initial state */
94   MC_restore_snapshot(initial_snapshot);
95
96   /* Traverse the stack from the initial state and re-execute the transitions */
97   for (item = xbt_fifo_get_last_item(stack);
98        item != xbt_fifo_get_first_item(stack);
99        item = xbt_fifo_get_prev_item(item)) {
100
101     state = (mc_state_t) xbt_fifo_get_item_content(item);
102     saved_req = MC_state_get_executed_request(state);
103    
104     if(saved_req){
105       /* because we got a copy of the executed request, we have to fetch the  
106          real one, pointed by the request field of the issuer process */
107       req = &saved_req->issuer->request;
108
109       /* Debug information */
110       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
111         req_str = MC_request_to_string(req); 
112         DEBUG1("Replay: %s", req_str);
113         xbt_free(req_str);
114       }
115     }
116          
117     SIMIX_request_pre(req);
118     MC_wait_for_requests();
119          
120     /* Update statistics */
121     mc_stats->visited_states++;
122     mc_stats->executed_transitions++;
123   }
124   DEBUG0("**** End Replay ****");
125 }
126
127 /**
128  * \brief Dumps the contents of a model-checker's stack and shows the actual
129  *        execution trace
130  * \param stack The stack to dump
131 */
132 void MC_dump_stack(xbt_fifo_t stack)
133 {
134   mc_state_t state;
135
136   MC_show_stack(stack);
137
138   MC_SET_RAW_MEM;
139   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
140     MC_state_delete(state);
141   MC_UNSET_RAW_MEM;
142 }
143
144 void MC_show_stack(xbt_fifo_t stack)
145 {
146   mc_state_t state;
147   xbt_fifo_item_t item;
148   smx_req_t req;
149   char *req_str = NULL;
150   
151   for (item = xbt_fifo_get_last_item(stack);
152        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
153         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
154     req = MC_state_get_executed_request(state);
155     if(req){
156       req_str = MC_request_to_string(req); 
157       INFO1("%s", req_str);
158       xbt_free(req_str);
159     }
160   }
161 }
162
163 /**
164  * \brief Schedules all the process that are ready to run
165  */
166 void MC_wait_for_requests(void)
167 {
168   char *req_str = NULL;
169   smx_req_t req = NULL;
170
171   do {
172     SIMIX_context_runall(simix_global->process_to_run);
173     while((req = SIMIX_request_pop())){
174       if(!SIMIX_request_is_visible(req))
175         SIMIX_request_pre(req);
176       else if(req->call == REQ_COMM_WAITANY)
177         THROW_UNIMPLEMENTED;
178       else if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
179         req_str = MC_request_to_string(req);
180         DEBUG1("Got: %s", req_str);
181         xbt_free(req_str);
182       }
183     }
184   } while (xbt_dynar_length(simix_global->process_to_run));
185 }
186
187 /****************************** Statistics ************************************/
188 void MC_print_statistics(mc_stats_t stats)
189 {
190   INFO1("State space size ~= %lu", stats->state_size);
191   INFO1("Expanded states = %lu", stats->expanded_states);
192   INFO1("Visited states = %lu", stats->visited_states);
193   INFO1("Executed transitions = %lu", stats->executed_transitions);
194   INFO1("Expanded / Visited = %lf",
195         (double) stats->visited_states / stats->expanded_states);
196   /*INFO1("Exploration coverage = %lf", 
197      (double)stats->expanded_states / stats->state_size); */
198 }
199
200 /************************* Assertion Checking *********************************/
201 void MC_assert(int prop)
202 {
203   if (MC_IS_ENABLED && !prop) {
204     INFO0("**************************");
205     INFO0("*** PROPERTY NOT VALID ***");
206     INFO0("**************************");
207     INFO0("Counter-example execution trace:");
208     MC_dump_stack(mc_stack);
209     MC_print_statistics(mc_stats);
210     xbt_abort();
211   }
212 }
213
214 void MC_show_deadlock(smx_req_t req)
215 {
216   char *req_str = NULL;
217   INFO0("**************************");
218   INFO0("*** DEAD-LOCK DETECTED ***");
219   INFO0("**************************");
220   INFO0("Locked request:");
221   req_str = MC_request_to_string(req);
222   INFO1("%s", req_str);
223   xbt_free(req_str);
224   INFO0("Counter-example execution trace:");
225   MC_dump_stack(mc_stack);
226 }