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 char mc_replay_mode = FALSE;
17 * \brief Initialize the model-checker data structures
19 void MC_init(int method)
21 /* Check if MC is already initialized */
25 /* Initialize the data structures that must be persistent across every
26 iteration of the model-checker (in RAW memory) */
29 /* Initialize statistics */
30 mc_stats = xbt_new0(s_mc_stats_t, 1);
31 mc_stats->state_size = 1;
33 /* Create exploration stack */
34 mc_stack = xbt_fifo_new();
36 /* Create the container for the sets */
37 mc_setset = xbt_setset_new(20);
50 /* Save the initial state */
52 initial_snapshot = xbt_new(s_mc_snapshot_t,1);
53 MC_take_snapshot(initial_snapshot);
57 void MC_modelcheck(int method){
75 * \brief Re-executes from the initial state all the transitions indicated by
76 * a given model-checker stack.
77 * \param stack The stack with the transitions to execute.
79 void MC_replay(xbt_fifo_t stack)
83 mc_transition_t trans;
85 DEBUG0("**** Begin Replay ****");
87 /* Restore the initial state */
88 MC_restore_snapshot(initial_snapshot);
90 mc_replay_mode = TRUE;
94 /* Traverse the stack from the initial state and re-execute the transitions */
95 for(item = xbt_fifo_get_last_item(stack);
96 item != xbt_fifo_get_first_item(stack);
97 item = xbt_fifo_get_prev_item(item)){
99 state = (mc_state_t) xbt_fifo_get_item_content(item);
100 trans = state->executed_transition;
102 /* Update statistics */
103 mc_stats->visited_states++;
104 mc_stats->executed_transitions++;
106 DEBUG1("Executing transition %s", trans->name);
107 SIMIX_process_schedule(trans->process);
109 /* Do all surf's related black magic tricks to keep all working */
110 MC_execute_surf_actions();
112 /* Schedule every process that got enabled due to the executed transition */
113 MC_schedule_enabled_processes();
115 mc_replay_mode = FALSE;
116 DEBUG0("**** End Replay ****");
120 * \brief Dumps the contents of a model-checker's stack and shows the actual
122 * \param stack The stack to dump
124 void MC_dump_stack(xbt_fifo_t stack)
127 mc_transition_t trans;
130 while( (state = (mc_state_t)xbt_fifo_pop(stack)) != NULL ){
131 trans = state->executed_transition;
133 INFO1("%s", trans->name);
135 MC_state_delete(state);
140 void MC_show_stack(xbt_fifo_t stack)
143 mc_transition_t trans;
144 xbt_fifo_item_t item;
146 INFO0("===========================");
147 for(item=xbt_fifo_get_last_item(stack);
148 (item?(state=(mc_state_t)(xbt_fifo_get_item_content(item))):(NULL)); \
149 item=xbt_fifo_get_prev_item(item)){
150 trans = state->executed_transition;
152 INFO1("%s", trans->name);
159 * \brief Schedules all the process that are ready to run
160 * As a side effect it performs some clean-up required by SIMIX
162 void MC_schedule_enabled_processes(void)
164 smx_process_t process;
166 //SIMIX_process_empty_trash();
168 /* Schedule every process that is ready to run due to an finished action */
169 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
170 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
171 SIMIX_process_schedule(process);
175 /******************************** States **************************************/
178 * \brief Creates a state data structure used by the exploration algorithm
180 mc_state_t MC_state_new(void)
182 mc_state_t state = NULL;
184 state = xbt_new0(s_mc_state_t, 1);
185 state->transitions = xbt_setset_new_set(mc_setset);
186 state->enabled_transitions = xbt_setset_new_set(mc_setset);
187 state->interleave = xbt_setset_new_set(mc_setset);
188 state->done = xbt_setset_new_set(mc_setset);
189 state->executed_transition = NULL;
191 mc_stats->expanded_states++;
196 * \brief Deletes a state data structure
197 * \param trans The state to be deleted
199 void MC_state_delete(mc_state_t state)
201 /*if(state->executed_transition)
202 MC_transition_delete(state->executed_transition);*/
203 xbt_setset_destroy_set(state->transitions);
204 xbt_setset_destroy_set(state->enabled_transitions);
205 xbt_setset_destroy_set(state->interleave);
206 xbt_setset_destroy_set(state->done);
211 /************************** SURF Emulation ************************************/
213 /* Dirty hack, we manipulate surf's clock to simplify the integration of the
218 * \brief Executes all the actions at every model
220 void MC_execute_surf_actions(void)
223 surf_action_t action = NULL;
224 surf_model_t model = NULL;
225 smx_action_t smx_action = NULL;
227 /* Execute all the actions in every model */
228 xbt_dynar_foreach(model_list, iter, model){
229 while ((action = xbt_swag_extract(model->states.running_action_set))){
230 /* FIXME: timeouts are not calculated correctly */
231 if(NOW >= action->max_duration){
232 surf_action_state_set(action, SURF_ACTION_DONE);
233 smx_action = action->data;
234 DEBUG5("Resource [%s] (%d): Executing RUNNING action \"%s\" (%p) MaxDuration %lf",
235 model->name, xbt_swag_size(model->states.running_action_set),
236 smx_action->name, smx_action, action->max_duration);
238 /* Copy the transfered data of the completed network actions */
239 /* FIXME: be carefull it might not be an action of the network model */
240 if(smx_action && smx_action->data != NULL)
241 SIMIX_network_copy_data((smx_comm_t)smx_action->data);
244 SIMIX_action_signal_all(smx_action);
247 /*FIXME: check if this is always empty or not */
248 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
249 smx_action = action->data;
250 DEBUG4("Resource [%s] (%d): Executing FAILED action \"%s\" (%p)",
251 model->name, xbt_swag_size(model->states.running_action_set),
252 smx_action->name, smx_action);
254 SIMIX_action_signal_all(smx_action);
257 /* That's it, now go one step deeper into the model-checking process! */
258 NOW += 0.5; /* FIXME: Check time increases*/
261 /****************************** Statistics ************************************/
262 void MC_print_statistics(mc_stats_t stats)
264 INFO1("State space size ~= %lu", stats->state_size);
265 INFO1("Expanded states = %lu", stats->expanded_states);
266 INFO1("Visited states = %lu", stats->visited_states);
267 INFO1("Executed transitions = %lu", stats->executed_transitions);
268 INFO1("Expanded / Visited = %lf",
269 (double)stats->visited_states / stats->expanded_states);
270 /*INFO1("Exploration coverage = %lf",
271 (double)stats->expanded_states / stats->state_size);*/
274 /************************* Assertion Checking *********************************/
275 void MC_assert(int prop)
278 INFO0("**************************");
279 INFO0("*** PROPERTY NOT VALID ***");
280 INFO0("**************************");
281 INFO0("Counter-example execution trace:");
282 MC_dump_stack(mc_stack);
283 MC_print_statistics(mc_stats);