5 #include "../surf/surf_private.h"
6 #include "../simix/private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
12 "Logging specific to MC (global)");
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;
23 * \brief Initialize the model-checker data structures
27 smx_process_t process;
29 /* Check if MC is already initialized */
33 /* Initialize the data structures that must be persistent across every
34 iteration of the model-checker (in RAW memory) */
37 /* Initialize statistics */
38 mc_stats = xbt_new0(s_mc_stats_t, 1);
39 mc_stats->state_size = 1;
41 /* Create exploration stack */
42 mc_stack = xbt_fifo_new();
44 /* Create the container for the sets */
45 mc_setset = xbt_setset_new(20);
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);
55 /* Save the initial state */
56 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
57 MC_take_snapshot(initial_snapshot);
61 void MC_modelcheck(void)
73 int MC_random(int min, int max)
75 /*FIXME: return mc_current_state->executed_transition->random.value;*/
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.
84 void MC_replay(xbt_fifo_t stack)
87 smx_req_t req = NULL, saved_req = NULL;
91 DEBUG0("**** Begin Replay ****");
93 /* Restore the initial state */
94 MC_restore_snapshot(initial_snapshot);
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)) {
101 state = (mc_state_t) xbt_fifo_get_item_content(item);
102 saved_req = MC_state_get_executed_request(state);
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;
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);
117 SIMIX_request_pre(req);
118 MC_wait_for_requests();
120 /* Update statistics */
121 mc_stats->visited_states++;
122 mc_stats->executed_transitions++;
124 DEBUG0("**** End Replay ****");
128 * \brief Dumps the contents of a model-checker's stack and shows the actual
130 * \param stack The stack to dump
132 void MC_dump_stack(xbt_fifo_t stack)
136 MC_show_stack(stack);
139 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
140 MC_state_delete(state);
144 void MC_show_stack(xbt_fifo_t stack)
147 xbt_fifo_item_t item;
149 char *req_str = NULL;
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);
156 req_str = MC_request_to_string(req);
157 INFO1("%s", req_str);
164 * \brief Schedules all the process that are ready to run
166 void MC_wait_for_requests(void)
168 char *req_str = NULL;
169 smx_req_t req = NULL;
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)
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);
184 } while (xbt_swag_size(simix_global->process_to_run));
187 /****************************** Statistics ************************************/
188 void MC_print_statistics(mc_stats_t stats)
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); */
200 /************************* Assertion Checking *********************************/
201 void MC_assert(int prop)
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);
214 void MC_show_deadlock(smx_req_t req)
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);
224 INFO0("Counter-example execution trace:");
225 MC_dump_stack(mc_stack);