1 #include "../surf/surf_private.h"
2 #include "../simix/private.h"
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
7 "Logging specific to MC (global)");
9 /* MC global data structures */
10 mc_snapshot_t initial_snapshot = NULL;
11 xbt_fifo_t mc_stack = NULL;
12 xbt_setset_t mc_setset = NULL;
13 mc_stats_t mc_stats = NULL;
14 mc_state_t mc_current_state = NULL;
15 char mc_replay_mode = FALSE;
18 * \brief Initialize the model-checker data structures
20 void MC_init(int method)
22 /* Check if MC is already initialized */
26 /* Initialize the data structures that must be persistent across every
27 iteration of the model-checker (in RAW memory) */
30 /* Initialize statistics */
31 mc_stats = xbt_new0(s_mc_stats_t, 1);
32 mc_stats->state_size = 1;
34 /* Create exploration stack */
35 mc_stack = xbt_fifo_new();
37 /* Create the container for the sets */
38 mc_setset = xbt_setset_new(20);
51 /* Save the initial state */
53 initial_snapshot = xbt_new(s_mc_snapshot_t,1);
54 MC_take_snapshot(initial_snapshot);
58 void MC_modelcheck(int method){
74 int MC_random(int min, int max)
76 MC_random_create(min,max);
77 SIMIX_process_yield();
80 return mc_current_state->executed_transition->current_value;
82 return mc_current_state->executed_transition->current_value - 1;
86 * \brief Re-executes from the initial state all the transitions indicated by
87 * a given model-checker stack.
88 * \param stack The stack with the transitions to execute.
90 void MC_replay(xbt_fifo_t stack)
93 mc_transition_t trans;
95 DEBUG0("**** Begin Replay ****");
97 /* Restore the initial state */
98 MC_restore_snapshot(initial_snapshot);
100 mc_replay_mode = TRUE;
104 /* Traverse the stack from the initial state and re-execute the transitions */
105 for(item = xbt_fifo_get_last_item(stack);
106 item != xbt_fifo_get_first_item(stack);
107 item = xbt_fifo_get_prev_item(item)){
109 mc_current_state = (mc_state_t) xbt_fifo_get_item_content(item);
110 trans = mc_current_state->executed_transition;
112 /* Update statistics */
113 mc_stats->visited_states++;
114 mc_stats->executed_transitions++;
116 DEBUG1("Executing transition %s", trans->name);
117 SIMIX_process_schedule(trans->process);
119 /* Do all surf's related black magic tricks to keep all working */
120 MC_execute_surf_actions();
122 /* Schedule every process that got enabled due to the executed transition */
123 MC_schedule_enabled_processes();
125 mc_replay_mode = FALSE;
126 DEBUG0("**** End Replay ****");
130 * \brief Dumps the contents of a model-checker's stack and shows the actual
132 * \param stack The stack to dump
134 void MC_dump_stack(xbt_fifo_t stack)
137 mc_transition_t trans;
140 while( (state = (mc_state_t)xbt_fifo_pop(stack)) != NULL ){
141 trans = state->executed_transition;
143 INFO1("%s", trans->name);
145 MC_state_delete(state);
150 void MC_show_stack(xbt_fifo_t stack)
153 mc_transition_t trans;
154 xbt_fifo_item_t item;
156 INFO0("===========================");
157 for(item=xbt_fifo_get_last_item(stack);
158 (item?(state=(mc_state_t)(xbt_fifo_get_item_content(item))):(NULL)); \
159 item=xbt_fifo_get_prev_item(item)){
160 trans = state->executed_transition;
162 INFO1("%s", trans->name);
169 * \brief Schedules all the process that are ready to run
170 * As a side effect it performs some clean-up required by SIMIX
172 void MC_schedule_enabled_processes(void)
174 smx_process_t process;
176 //SIMIX_process_empty_trash();
178 /* Schedule every process that is ready to run due to an finished action */
179 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
180 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
181 SIMIX_process_schedule(process);
185 /******************************** States **************************************/
188 * \brief Creates a state data structure used by the exploration algorithm
190 mc_state_t MC_state_new(void)
192 mc_state_t state = NULL;
194 state = xbt_new0(s_mc_state_t, 1);
195 state->transitions = xbt_setset_new_set(mc_setset);
196 state->enabled_transitions = xbt_setset_new_set(mc_setset);
197 state->interleave = xbt_setset_new_set(mc_setset);
198 state->done = xbt_setset_new_set(mc_setset);
199 state->executed_transition = NULL;
201 mc_stats->expanded_states++;
206 * \brief Deletes a state data structure
207 * \param trans The state to be deleted
209 void MC_state_delete(mc_state_t state)
211 /*if(state->executed_transition)
212 MC_transition_delete(state->executed_transition);*/
213 xbt_setset_destroy_set(state->transitions);
214 xbt_setset_destroy_set(state->enabled_transitions);
215 xbt_setset_destroy_set(state->interleave);
216 xbt_setset_destroy_set(state->done);
221 /************************** SURF Emulation ************************************/
223 /* Dirty hack, we manipulate surf's clock to simplify the integration of the
228 * \brief Executes all the actions at every model
230 void MC_execute_surf_actions(void)
233 surf_action_t action = NULL;
234 surf_model_t model = NULL;
235 smx_action_t smx_action = NULL;
237 /* Execute all the actions in every model */
238 xbt_dynar_foreach(model_list, iter, model){
239 while ((action = xbt_swag_extract(model->states.running_action_set))){
240 /* FIXME: timeouts are not calculated correctly */
241 if(NOW >= action->max_duration){
242 surf_action_state_set(action, SURF_ACTION_DONE);
243 smx_action = action->data;
244 DEBUG5("Resource [%s] (%d): Executing RUNNING action \"%s\" (%p) MaxDuration %lf",
245 model->name, xbt_swag_size(model->states.running_action_set),
246 smx_action->name, smx_action, action->max_duration);
248 /* Copy the transfered data of the completed network actions */
249 /* FIXME: be carefull it might not be an action of the network model */
250 if(smx_action && smx_action->data != NULL)
251 SIMIX_network_copy_data((smx_comm_t)smx_action->data);
254 SIMIX_action_signal_all(smx_action);
257 /*FIXME: check if this is always empty or not */
258 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
259 smx_action = action->data;
260 DEBUG4("Resource [%s] (%d): Executing FAILED action \"%s\" (%p)",
261 model->name, xbt_swag_size(model->states.running_action_set),
262 smx_action->name, smx_action);
264 SIMIX_action_signal_all(smx_action);
267 /* That's it, now go one step deeper into the model-checking process! */
268 NOW += 0.5; /* FIXME: Check time increases*/
271 /****************************** Statistics ************************************/
272 void MC_print_statistics(mc_stats_t stats)
274 INFO1("State space size ~= %lu", stats->state_size);
275 INFO1("Expanded states = %lu", stats->expanded_states);
276 INFO1("Visited states = %lu", stats->visited_states);
277 INFO1("Executed transitions = %lu", stats->executed_transitions);
278 INFO1("Expanded / Visited = %lf",
279 (double)stats->visited_states / stats->expanded_states);
280 /*INFO1("Exploration coverage = %lf",
281 (double)stats->expanded_states / stats->state_size);*/
284 /************************* Assertion Checking *********************************/
285 void MC_assert(int prop)
288 INFO0("**************************");
289 INFO0("*** PROPERTY NOT VALID ***");
290 INFO0("**************************");
291 INFO0("Counter-example execution trace:");
292 MC_dump_stack(mc_stack);
293 MC_print_statistics(mc_stats);