6 #include "../surf/surf_private.h"
7 #include "../simix/private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
13 "Logging specific to MC (global)");
15 /* MC global data structures */
17 mc_state_t mc_current_state = NULL;
18 char mc_replay_mode = FALSE;
19 double *mc_time = NULL;
20 mc_snapshot_t initial_snapshot = NULL;
24 xbt_fifo_t mc_stack_safety_stateful = NULL;
25 xbt_fifo_t mc_stack_safety_stateless = NULL;
26 mc_stats_t mc_stats = NULL;
30 xbt_fifo_t mc_stack_liveness_stateful = NULL;
31 mc_stats_pair_t mc_stats_pair = NULL;
32 xbt_fifo_t mc_stack_liveness_stateless = NULL;
36 * \brief Initialize the model-checker data structures
38 void MC_init_safety_stateless(void)
41 /* Check if MC is already initialized */
45 mc_time = xbt_new0(double, simix_process_maxpid);
47 /* Initialize the data structures that must be persistent across every
48 iteration of the model-checker (in RAW memory) */
51 /* Initialize statistics */
52 mc_stats = xbt_new0(s_mc_stats_t, 1);
53 mc_stats->state_size = 1;
55 /* Create exploration stack */
56 mc_stack_safety_stateless = xbt_fifo_new();
63 /* Save the initial state */
64 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
65 MC_take_snapshot(initial_snapshot);
69 void MC_init_safety_stateful(void){
72 /* Check if MC is already initialized */
76 mc_time = xbt_new0(double, simix_process_maxpid);
78 /* Initialize the data structures that must be persistent across every
79 iteration of the model-checker (in RAW memory) */
82 /* Initialize statistics */
83 mc_stats = xbt_new0(s_mc_stats_t, 1);
84 mc_stats->state_size = 1;
86 /* Create exploration stack */
87 mc_stack_safety_stateful = xbt_fifo_new();
91 MC_dpor_stateful_init();
96 void MC_init_liveness_stateful(xbt_automaton_t a){
98 XBT_DEBUG("Start init mc");
100 mc_time = xbt_new0(double, simix_process_maxpid);
102 /* Initialize the data structures that must be persistent across every
103 iteration of the model-checker (in RAW memory) */
107 /* Initialize statistics */
108 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
109 mc_stats = xbt_new0(s_mc_stats_t, 1);
111 XBT_DEBUG("Creating snapshot_stack");
113 /* Create exploration stack */
114 mc_stack_liveness_stateful = xbt_fifo_new();
119 //MC_vddfs_stateful_init(a);
120 MC_ddfs_stateful_init(a);
125 void MC_init_liveness_stateless(xbt_automaton_t a){
127 XBT_DEBUG("Start init mc");
129 mc_time = xbt_new0(double, simix_process_maxpid);
131 /* Initialize the data structures that must be persistent across every
132 iteration of the model-checker (in RAW memory) */
136 /* Initialize statistics */
137 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
139 XBT_DEBUG("Creating snapshot_stack");
141 /* Create exploration stack */
142 mc_stack_liveness_stateless = xbt_fifo_new();
146 MC_ddfs_stateless_init(a);
150 void MC_modelcheck(void)
152 MC_init_safety_stateless();
157 void MC_modelcheck_stateful(void)
159 MC_init_safety_stateful();
164 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
165 MC_init_liveness_stateful(a);
169 void MC_modelcheck_liveness_stateless(xbt_automaton_t a){
170 MC_init_liveness_stateless(a);
174 void MC_exit_liveness(void)
176 MC_print_statistics_pairs(mc_stats_pair);
184 MC_print_statistics(mc_stats);
190 int MC_random(int min, int max)
192 /*FIXME: return mc_current_state->executed_transition->random.value;*/
197 * \brief Schedules all the process that are ready to run
199 void MC_wait_for_requests(void)
201 smx_process_t process;
205 while (xbt_dynar_length(simix_global->process_to_run)) {
206 SIMIX_process_runall();
207 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
208 req = &process->request;
209 if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
210 SIMIX_request_pre(req, 0);
215 int MC_deadlock_check()
217 int deadlock = FALSE;
218 smx_process_t process;
219 if(xbt_swag_size(simix_global->process_list)){
221 xbt_swag_foreach(process, simix_global->process_list){
222 if(process->request.call != REQ_NO_REQ
223 && MC_request_is_enabled(&process->request)){
233 * \brief Re-executes from the initial state all the transitions indicated by
234 * a given model-checker stack.
235 * \param stack The stack with the transitions to execute.
237 void MC_replay(xbt_fifo_t stack)
241 smx_req_t req = NULL, saved_req = NULL;
242 xbt_fifo_item_t item;
245 XBT_DEBUG("**** Begin Replay ****");
247 /* Restore the initial state */
248 MC_restore_snapshot(initial_snapshot);
249 /* At the moment of taking the snapshot the raw heap was set, so restoring
250 * it will set it back again, we have to unset it to continue */
253 /* Traverse the stack from the initial state and re-execute the transitions */
254 for (item = xbt_fifo_get_last_item(stack);
255 item != xbt_fifo_get_first_item(stack);
256 item = xbt_fifo_get_prev_item(item)) {
258 state = (mc_state_t) xbt_fifo_get_item_content(item);
259 saved_req = MC_state_get_executed_request(state, &value);
262 /* because we got a copy of the executed request, we have to fetch the
263 real one, pointed by the request field of the issuer process */
264 req = &saved_req->issuer->request;
266 /* Debug information */
267 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
268 req_str = MC_request_to_string(req, value);
269 XBT_DEBUG("Replay: %s (%p)", req_str, state);
274 SIMIX_request_pre(req, value);
275 MC_wait_for_requests();
277 /* Update statistics */
278 mc_stats->visited_states++;
279 mc_stats->executed_transitions++;
281 XBT_DEBUG("**** End Replay ****");
285 * \brief Dumps the contents of a model-checker's stack and shows the actual
287 * \param stack The stack to dump
289 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
293 MC_show_stack_safety_stateless(stack);
296 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
297 MC_state_delete(state);
302 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
306 xbt_fifo_item_t item;
308 char *req_str = NULL;
310 for (item = xbt_fifo_get_last_item(stack);
311 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
312 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
313 req = MC_state_get_executed_request(state, &value);
315 req_str = MC_request_to_string(req, value);
316 XBT_INFO("%s", req_str);
322 void MC_show_deadlock(smx_req_t req)
324 /*char *req_str = NULL;*/
325 XBT_INFO("**************************");
326 XBT_INFO("*** DEAD-LOCK DETECTED ***");
327 XBT_INFO("**************************");
328 XBT_INFO("Locked request:");
329 /*req_str = MC_request_to_string(req);
330 XBT_INFO("%s", req_str);
332 XBT_INFO("Counter-example execution trace:");
333 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
336 void MC_show_deadlock_stateful(smx_req_t req)
338 /*char *req_str = NULL;*/
339 XBT_INFO("**************************");
340 XBT_INFO("*** DEAD-LOCK DETECTED ***");
341 XBT_INFO("**************************");
342 XBT_INFO("Locked request:");
343 /*req_str = MC_request_to_string(req);
344 XBT_INFO("%s", req_str);
346 XBT_INFO("Counter-example execution trace:");
347 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
350 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
352 //mc_state_ws_t state;
354 MC_show_stack_safety_stateful(stack);
357 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
358 MC_state_delete(state);
363 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
367 xbt_fifo_item_t item;
369 char *req_str = NULL;
371 for (item = xbt_fifo_get_last_item(stack);
372 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
373 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
374 req = MC_state_get_executed_request(state->graph_state, &value);
376 req_str = MC_request_to_string(req, value);
377 XBT_INFO("%s", req_str);
383 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
386 xbt_fifo_item_t item;
388 char *req_str = NULL;
390 for (item = xbt_fifo_get_last_item(stack);
391 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
392 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
393 req = MC_state_get_executed_request(pair->graph_state, &value);
395 req_str = MC_request_to_string(req, value);
396 XBT_INFO("%s", req_str);
402 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
406 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
407 MC_pair_delete(pair);
411 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
413 mc_pair_stateless_t pair;
414 xbt_fifo_item_t item;
416 char *req_str = NULL;
418 for (item = xbt_fifo_get_last_item(stack);
419 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
420 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
421 req = MC_state_get_executed_request(pair->graph_state, &value);
423 req_str = MC_request_to_string(req, value);
424 XBT_INFO("%s", req_str);
430 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
431 mc_pair_stateless_t pair;
434 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
435 MC_pair_stateless_delete(pair);
440 void MC_print_statistics(mc_stats_t stats)
442 XBT_INFO("State space size ~= %lu", stats->state_size);
443 XBT_INFO("Expanded states = %lu", stats->expanded_states);
444 XBT_INFO("Visited states = %lu", stats->visited_states);
445 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
446 XBT_INFO("Expanded / Visited = %lf",
447 (double) stats->visited_states / stats->expanded_states);
448 /*XBT_INFO("Exploration coverage = %lf",
449 (double)stats->expanded_states / stats->state_size); */
452 void MC_print_statistics_pairs(mc_stats_pair_t stats)
454 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
455 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
456 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
457 XBT_INFO("Expanded / Visited = %lf",
458 (double) stats->visited_pairs / stats->expanded_pairs);
459 /*XBT_INFO("Exploration coverage = %lf",
460 (double)stats->expanded_states / stats->state_size); */
463 void MC_assert(int prop)
465 if (MC_IS_ENABLED && !prop) {
466 XBT_INFO("**************************");
467 XBT_INFO("*** PROPERTY NOT VALID ***");
468 XBT_INFO("**************************");
469 XBT_INFO("Counter-example execution trace:");
470 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
471 MC_print_statistics(mc_stats);
476 void MC_assert_stateful(int prop)
478 if (MC_IS_ENABLED && !prop) {
479 XBT_INFO("**************************");
480 XBT_INFO("*** PROPERTY NOT VALID ***");
481 XBT_INFO("**************************");
482 XBT_INFO("Counter-example execution trace:");
483 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
484 MC_print_statistics(mc_stats);
489 void MC_assert_pair_stateful(int prop){
490 if (MC_IS_ENABLED && !prop) {
491 XBT_INFO("**************************");
492 XBT_INFO("*** PROPERTY NOT VALID ***");
493 XBT_INFO("**************************");
494 //XBT_INFO("Counter-example execution trace:");
495 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
496 //MC_dump_snapshot_stack(mc_snapshot_stack);
497 MC_print_statistics_pairs(mc_stats_pair);
502 void MC_assert_pair_stateless(int prop){
503 if (MC_IS_ENABLED && !prop) {
504 XBT_INFO("**************************");
505 XBT_INFO("*** PROPERTY NOT VALID ***");
506 XBT_INFO("**************************");
507 //XBT_INFO("Counter-example execution trace:");
508 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
509 //MC_dump_snapshot_stack(mc_snapshot_stack);
510 MC_print_statistics_pairs(mc_stats_pair);
515 void MC_process_clock_add(smx_process_t process, double amount)
517 mc_time[process->pid] += amount;
520 double MC_process_clock_get(smx_process_t process)
523 return mc_time[process->pid];