Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Add SIMIX_context_get_thread_id to the context factory interface.
[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   xbt_free(mc_time);
64   MC_memory_exit();
65 }
66
67 int MC_random(int min, int max)
68 {
69   /*FIXME: return mc_current_state->executed_transition->random.value;*/
70   return 0;
71 }
72
73 /**
74  * \brief Schedules all the process that are ready to run
75  */
76 void MC_wait_for_requests(void)
77 {
78   smx_req_t req = NULL;
79
80   do {
81     SIMIX_context_runall(simix_global->process_to_run);
82     while((req = SIMIX_request_pop())){
83       if(!MC_request_is_visible(req))
84         SIMIX_request_pre(req, 0);
85     }
86   } while (xbt_dynar_length(simix_global->process_to_run));
87 }
88
89 int MC_deadlock_check()
90 {
91   int deadlock = FALSE;
92   smx_process_t process;
93   if(xbt_swag_size(simix_global->process_list)){
94     deadlock = TRUE;
95     xbt_swag_foreach(process, simix_global->process_list){
96       if(process->request.call != REQ_NO_REQ
97          && MC_request_is_enabled(&process->request)){
98         deadlock = FALSE;
99         break;
100       }
101     }
102   }
103   return deadlock;
104 }
105
106 /**
107  * \brief Re-executes from the initial state all the transitions indicated by
108  *        a given model-checker stack.
109  * \param stack The stack with the transitions to execute.
110 */
111 void MC_replay(xbt_fifo_t stack)
112 {
113   int value;
114   char *req_str;
115   smx_req_t req = NULL, saved_req = NULL;
116   xbt_fifo_item_t item;
117   mc_state_t state;
118
119   DEBUG0("**** Begin Replay ****");
120
121   /* Restore the initial state */
122   MC_restore_snapshot(initial_snapshot);
123   /* At the moment of taking the snapshot the raw heap was set, so restoring
124    * it will set it back again, we have to unset it to continue  */
125   MC_UNSET_RAW_MEM;
126
127   /* Traverse the stack from the initial state and re-execute the transitions */
128   for (item = xbt_fifo_get_last_item(stack);
129        item != xbt_fifo_get_first_item(stack);
130        item = xbt_fifo_get_prev_item(item)) {
131
132     state = (mc_state_t) xbt_fifo_get_item_content(item);
133     saved_req = MC_state_get_executed_request(state, &value);
134    
135     if(saved_req){
136       /* because we got a copy of the executed request, we have to fetch the  
137          real one, pointed by the request field of the issuer process */
138       req = &saved_req->issuer->request;
139
140       /* Debug information */
141       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
142         req_str = MC_request_to_string(req, value);
143         DEBUG2("Replay: %s (%p)", req_str, state);
144         xbt_free(req_str);
145       }
146     }
147          
148     SIMIX_request_pre(req, value);
149     MC_wait_for_requests();
150          
151     /* Update statistics */
152     mc_stats->visited_states++;
153     mc_stats->executed_transitions++;
154   }
155   DEBUG0("**** End Replay ****");
156 }
157
158 /**
159  * \brief Dumps the contents of a model-checker's stack and shows the actual
160  *        execution trace
161  * \param stack The stack to dump
162 */
163 void MC_dump_stack(xbt_fifo_t stack)
164 {
165   mc_state_t state;
166
167   MC_show_stack(stack);
168
169   MC_SET_RAW_MEM;
170   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
171     MC_state_delete(state);
172   MC_UNSET_RAW_MEM;
173 }
174
175 void MC_show_stack(xbt_fifo_t stack)
176 {
177   int value;
178   mc_state_t state;
179   xbt_fifo_item_t item;
180   smx_req_t req;
181   char *req_str = NULL;
182   
183   for (item = xbt_fifo_get_last_item(stack);
184        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
185         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
186     req = MC_state_get_executed_request(state, &value);
187     if(req){
188       req_str = MC_request_to_string(req, value);
189       INFO1("%s", req_str);
190       xbt_free(req_str);
191     }
192   }
193 }
194
195 void MC_show_deadlock(smx_req_t req)
196 {
197   /*char *req_str = NULL;*/
198   INFO0("**************************");
199   INFO0("*** DEAD-LOCK DETECTED ***");
200   INFO0("**************************");
201   INFO0("Locked request:");
202   /*req_str = MC_request_to_string(req);
203   INFO1("%s", req_str);
204   xbt_free(req_str);*/
205   INFO0("Counter-example execution trace:");
206   MC_dump_stack(mc_stack);
207 }
208
209 void MC_print_statistics(mc_stats_t stats)
210 {
211   INFO1("State space size ~= %lu", stats->state_size);
212   INFO1("Expanded states = %lu", stats->expanded_states);
213   INFO1("Visited states = %lu", stats->visited_states);
214   INFO1("Executed transitions = %lu", stats->executed_transitions);
215   INFO1("Expanded / Visited = %lf",
216         (double) stats->visited_states / stats->expanded_states);
217   /*INFO1("Exploration coverage = %lf", 
218      (double)stats->expanded_states / stats->state_size); */
219 }
220
221 void MC_assert(int prop)
222 {
223   if (MC_IS_ENABLED && !prop) {
224     INFO0("**************************");
225     INFO0("*** PROPERTY NOT VALID ***");
226     INFO0("**************************");
227     INFO0("Counter-example execution trace:");
228     MC_dump_stack(mc_stack);
229     MC_print_statistics(mc_stats);
230     xbt_abort();
231   }
232 }
233
234 void MC_process_clock_add(smx_process_t process, double amount)
235 {
236   mc_time[process->pid] += amount;
237 }
238
239 double MC_process_clock_get(smx_process_t process)
240 {
241   if(mc_time)
242     return mc_time[process->pid];
243   else
244     return 0;
245 }