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_stats_pair_t mc_stats_pair = NULL;
19 mc_state_t mc_current_state = NULL;
20 char mc_replay_mode = FALSE;
21 double *mc_time = NULL;
22 xbt_fifo_t mc_snapshot_stack = NULL;
25 * \brief Initialize the model-checker data structures
29 /* Check if MC is already initialized */
33 mc_time = xbt_new0(double, simix_process_maxpid);
35 /* Initialize the data structures that must be persistent across every
36 iteration of the model-checker (in RAW memory) */
39 /* Initialize statistics */
40 mc_stats = xbt_new0(s_mc_stats_t, 1);
41 mc_stats->state_size = 1;
43 /* Create exploration stack */
44 mc_stack = xbt_fifo_new();
51 /* Save the initial state */
52 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
53 MC_take_snapshot(initial_snapshot);
57 void MC_init_with_automaton(xbt_automaton_t a){
59 XBT_DEBUG("Start init mc");
61 mc_time = xbt_new0(double, simix_process_maxpid);
63 /* Initialize the data structures that must be persistent across every
64 iteration of the model-checker (in RAW memory) */
68 /* Initialize statistics */
69 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
70 mc_stats = xbt_new0(s_mc_stats_t, 1);
71 //mc_stats_pair->pair_size = 1;
73 XBT_DEBUG("Creating snapshot_stack");
75 /* Create exploration stack */
76 mc_snapshot_stack = xbt_fifo_new();
80 //MC_vddfs_with_restore_snapshot_init(a);
81 //MC_ddfs_with_restore_snapshot_init(a);
86 void MC_modelcheck(void)
93 void MC_modelcheck_with_automaton(xbt_automaton_t a){
94 MC_init_with_automaton(a);
95 MC_exit_with_automaton();
98 void MC_exit_with_automaton(void)
100 MC_print_statistics_pairs(mc_stats_pair);
107 MC_print_statistics(mc_stats);
112 int MC_random(int min, int max)
114 /*FIXME: return mc_current_state->executed_transition->random.value;*/
119 * \brief Schedules all the process that are ready to run
121 void MC_wait_for_requests(void)
123 smx_process_t process;
127 while (xbt_dynar_length(simix_global->process_to_run)) {
128 SIMIX_process_runall();
129 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
130 req = &process->request;
131 if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
132 SIMIX_request_pre(req, 0);
137 int MC_deadlock_check()
139 int deadlock = FALSE;
140 smx_process_t process;
141 if(xbt_swag_size(simix_global->process_list)){
143 xbt_swag_foreach(process, simix_global->process_list){
144 if(process->request.call != REQ_NO_REQ
145 && MC_request_is_enabled(&process->request)){
155 * \brief Re-executes from the initial state all the transitions indicated by
156 * a given model-checker stack.
157 * \param stack The stack with the transitions to execute.
159 void MC_replay(xbt_fifo_t stack)
163 smx_req_t req = NULL, saved_req = NULL;
164 xbt_fifo_item_t item;
167 XBT_DEBUG("**** Begin Replay ****");
169 /* Restore the initial state */
170 MC_restore_snapshot(initial_snapshot);
171 /* At the moment of taking the snapshot the raw heap was set, so restoring
172 * it will set it back again, we have to unset it to continue */
175 /* Traverse the stack from the initial state and re-execute the transitions */
176 for (item = xbt_fifo_get_last_item(stack);
177 item != xbt_fifo_get_first_item(stack);
178 item = xbt_fifo_get_prev_item(item)) {
180 state = (mc_state_t) xbt_fifo_get_item_content(item);
181 saved_req = MC_state_get_executed_request(state, &value);
184 /* because we got a copy of the executed request, we have to fetch the
185 real one, pointed by the request field of the issuer process */
186 req = &saved_req->issuer->request;
188 /* Debug information */
189 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
190 req_str = MC_request_to_string(req, value);
191 XBT_DEBUG("Replay: %s (%p)", req_str, state);
196 SIMIX_request_pre(req, value);
197 MC_wait_for_requests();
199 /* Update statistics */
200 mc_stats->visited_states++;
201 mc_stats->executed_transitions++;
203 XBT_DEBUG("**** End Replay ****");
207 * \brief Dumps the contents of a model-checker's stack and shows the actual
209 * \param stack The stack to dump
211 void MC_dump_stack(xbt_fifo_t stack)
215 MC_show_stack(stack);
218 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
219 MC_state_delete(state);
223 void MC_show_stack(xbt_fifo_t stack)
227 xbt_fifo_item_t item;
229 char *req_str = NULL;
231 for (item = xbt_fifo_get_last_item(stack);
232 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
233 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
234 req = MC_state_get_executed_request(state, &value);
236 req_str = MC_request_to_string(req, value);
237 XBT_INFO("%s", req_str);
243 void MC_show_deadlock(smx_req_t req)
245 /*char *req_str = NULL;*/
246 XBT_INFO("**************************");
247 XBT_INFO("*** DEAD-LOCK DETECTED ***");
248 XBT_INFO("**************************");
249 XBT_INFO("Locked request:");
250 /*req_str = MC_request_to_string(req);
251 XBT_INFO("%s", req_str);
253 XBT_INFO("Counter-example execution trace:");
254 MC_dump_stack(mc_stack);
257 void MC_print_statistics(mc_stats_t stats)
259 XBT_INFO("State space size ~= %lu", stats->state_size);
260 XBT_INFO("Expanded states = %lu", stats->expanded_states);
261 XBT_INFO("Visited states = %lu", stats->visited_states);
262 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
263 XBT_INFO("Expanded / Visited = %lf",
264 (double) stats->visited_states / stats->expanded_states);
265 /*XBT_INFO("Exploration coverage = %lf",
266 (double)stats->expanded_states / stats->state_size); */
269 void MC_print_statistics_pairs(mc_stats_pair_t stats)
271 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
272 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
273 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
274 XBT_INFO("Expanded / Visited = %lf",
275 (double) stats->visited_pairs / stats->expanded_pairs);
276 /*XBT_INFO("Exploration coverage = %lf",
277 (double)stats->expanded_states / stats->state_size); */
280 void MC_assert(int prop)
282 if (MC_IS_ENABLED && !prop) {
283 XBT_INFO("**************************");
284 XBT_INFO("*** PROPERTY NOT VALID ***");
285 XBT_INFO("**************************");
286 XBT_INFO("Counter-example execution trace:");
287 MC_dump_stack(mc_stack);
288 MC_print_statistics(mc_stats);
293 void MC_assert_pair(int prop){
294 if (MC_IS_ENABLED && !prop) {
295 XBT_INFO("**************************");
296 XBT_INFO("*** PROPERTY NOT VALID ***");
297 XBT_INFO("**************************");
298 //XBT_INFO("Counter-example execution trace:");
299 MC_show_snapshot_stack(mc_snapshot_stack);
300 //MC_dump_snapshot_stack(mc_snapshot_stack);
301 MC_print_statistics_pairs(mc_stats_pair);
306 void MC_process_clock_add(smx_process_t process, double amount)
308 mc_time[process->pid] += amount;
311 double MC_process_clock_get(smx_process_t process)
314 return mc_time[process->pid];