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 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;
22 * \brief Initialize the model-checker data structures
26 /* Check if MC is already initialized */
30 mc_time = xbt_new0(double, simix_process_maxpid);
32 /* Initialize the data structures that must be persistent across every
33 iteration of the model-checker (in RAW memory) */
36 /* Initialize statistics */
37 mc_stats = xbt_new0(s_mc_stats_t, 1);
38 mc_stats->state_size = 1;
40 /* Create exploration stack */
41 mc_stack = xbt_fifo_new();
48 /* Save the initial state */
49 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
50 MC_take_snapshot(initial_snapshot);
54 void MC_modelcheck(void)
63 MC_print_statistics(mc_stats);
68 int MC_random(int min, int max)
70 /*FIXME: return mc_current_state->executed_transition->random.value;*/
75 * \brief Schedules all the process that are ready to run
77 void MC_wait_for_requests(void)
82 SIMIX_context_runall(simix_global->process_to_run);
83 while((req = SIMIX_request_pop())){
84 if(!MC_request_is_visible(req))
85 SIMIX_request_pre(req, 0);
87 } while (xbt_dynar_length(simix_global->process_to_run));
90 int MC_deadlock_check()
93 smx_process_t process;
94 if(xbt_swag_size(simix_global->process_list)){
96 xbt_swag_foreach(process, simix_global->process_list){
97 if(process->request.call != REQ_NO_REQ
98 && MC_request_is_enabled(&process->request)){
108 * \brief Re-executes from the initial state all the transitions indicated by
109 * a given model-checker stack.
110 * \param stack The stack with the transitions to execute.
112 void MC_replay(xbt_fifo_t stack)
116 smx_req_t req = NULL, saved_req = NULL;
117 xbt_fifo_item_t item;
120 XBT_DEBUG("**** Begin Replay ****");
122 /* Restore the initial state */
123 MC_restore_snapshot(initial_snapshot);
124 /* At the moment of taking the snapshot the raw heap was set, so restoring
125 * it will set it back again, we have to unset it to continue */
128 /* Traverse the stack from the initial state and re-execute the transitions */
129 for (item = xbt_fifo_get_last_item(stack);
130 item != xbt_fifo_get_first_item(stack);
131 item = xbt_fifo_get_prev_item(item)) {
133 state = (mc_state_t) xbt_fifo_get_item_content(item);
134 saved_req = MC_state_get_executed_request(state, &value);
137 /* because we got a copy of the executed request, we have to fetch the
138 real one, pointed by the request field of the issuer process */
139 req = &saved_req->issuer->request;
141 /* Debug information */
142 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
143 req_str = MC_request_to_string(req, value);
144 XBT_DEBUG("Replay: %s (%p)", req_str, state);
149 SIMIX_request_pre(req, value);
150 MC_wait_for_requests();
152 /* Update statistics */
153 mc_stats->visited_states++;
154 mc_stats->executed_transitions++;
156 XBT_DEBUG("**** End Replay ****");
160 * \brief Dumps the contents of a model-checker's stack and shows the actual
162 * \param stack The stack to dump
164 void MC_dump_stack(xbt_fifo_t stack)
168 MC_show_stack(stack);
171 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
172 MC_state_delete(state);
176 void MC_show_stack(xbt_fifo_t stack)
180 xbt_fifo_item_t item;
182 char *req_str = NULL;
184 for (item = xbt_fifo_get_last_item(stack);
185 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
186 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
187 req = MC_state_get_executed_request(state, &value);
189 req_str = MC_request_to_string(req, value);
190 XBT_INFO("%s", req_str);
196 void MC_show_deadlock(smx_req_t req)
198 /*char *req_str = NULL;*/
199 XBT_INFO("**************************");
200 XBT_INFO("*** DEAD-LOCK DETECTED ***");
201 XBT_INFO("**************************");
202 XBT_INFO("Locked request:");
203 /*req_str = MC_request_to_string(req);
204 XBT_INFO("%s", req_str);
206 XBT_INFO("Counter-example execution trace:");
207 MC_dump_stack(mc_stack);
210 void MC_print_statistics(mc_stats_t stats)
212 XBT_INFO("State space size ~= %lu", stats->state_size);
213 XBT_INFO("Expanded states = %lu", stats->expanded_states);
214 XBT_INFO("Visited states = %lu", stats->visited_states);
215 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
216 XBT_INFO("Expanded / Visited = %lf",
217 (double) stats->visited_states / stats->expanded_states);
218 /*XBT_INFO("Exploration coverage = %lf",
219 (double)stats->expanded_states / stats->state_size); */
222 void MC_assert(int prop)
224 if (MC_IS_ENABLED && !prop) {
225 XBT_INFO("**************************");
226 XBT_INFO("*** PROPERTY NOT VALID ***");
227 XBT_INFO("**************************");
228 XBT_INFO("Counter-example execution trace:");
229 MC_dump_stack(mc_stack);
230 MC_print_statistics(mc_stats);
235 void MC_process_clock_add(smx_process_t process, double amount)
237 mc_time[process->pid] += amount;
240 double MC_process_clock_get(smx_process_t process)
243 return mc_time[process->pid];