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;
33 mc_snapshot_t initial_snapshot_liveness = NULL;
37 * \brief Initialize the model-checker data structures
39 void MC_init_safety_stateless(void)
42 /* Check if MC is already initialized */
46 mc_time = xbt_new0(double, simix_process_maxpid);
48 /* Initialize the data structures that must be persistent across every
49 iteration of the model-checker (in RAW memory) */
52 /* Initialize statistics */
53 mc_stats = xbt_new0(s_mc_stats_t, 1);
54 mc_stats->state_size = 1;
56 /* Create exploration stack */
57 mc_stack_safety_stateless = xbt_fifo_new();
64 /* Save the initial state */
65 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
66 MC_take_snapshot(initial_snapshot);
70 void MC_init_safety_stateful(void){
73 /* Check if MC is already initialized */
77 mc_time = xbt_new0(double, simix_process_maxpid);
79 /* Initialize the data structures that must be persistent across every
80 iteration of the model-checker (in RAW memory) */
83 /* Initialize statistics */
84 mc_stats = xbt_new0(s_mc_stats_t, 1);
85 mc_stats->state_size = 1;
87 /* Create exploration stack */
88 mc_stack_safety_stateful = xbt_fifo_new();
92 MC_dpor_stateful_init();
97 void MC_init_liveness_stateful(xbt_automaton_t a){
99 XBT_DEBUG("Start init mc");
101 mc_time = xbt_new0(double, simix_process_maxpid);
103 /* Initialize the data structures that must be persistent across every
104 iteration of the model-checker (in RAW memory) */
108 /* Initialize statistics */
109 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
110 //mc_stats = xbt_new0(s_mc_stats_t, 1);
112 XBT_DEBUG("Creating snapshot_stack");
114 /* Create exploration stack */
115 mc_stack_liveness_stateful = xbt_fifo_new();
120 //MC_ddfs_stateful_init(a);
125 void MC_init_liveness_stateless(xbt_automaton_t a, char *prgm){
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 stack");
141 /* Create exploration stack */
142 mc_stack_liveness_stateless = xbt_fifo_new();
146 MC_ddfs_stateless_init(a, prgm);
152 void MC_modelcheck(void)
154 MC_init_safety_stateless();
159 void MC_modelcheck_stateful(void)
161 MC_init_safety_stateful();
166 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
167 MC_init_liveness_stateful(a);
171 void MC_modelcheck_liveness_stateless(xbt_automaton_t a, char *prgm){
172 MC_init_liveness_stateless(a, prgm);
176 void MC_exit_liveness(void)
178 MC_print_statistics_pairs(mc_stats_pair);
187 MC_print_statistics(mc_stats);
193 int MC_random(int min, int max)
195 /*FIXME: return mc_current_state->executed_transition->random.value;*/
200 * \brief Schedules all the process that are ready to run
202 void MC_wait_for_requests(void)
204 smx_process_t process;
208 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
209 SIMIX_process_runall();
210 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
211 req = &process->request;
212 if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
213 SIMIX_request_pre(req, 0);
218 int MC_deadlock_check()
220 int deadlock = FALSE;
221 smx_process_t process;
222 if(xbt_swag_size(simix_global->process_list)){
224 xbt_swag_foreach(process, simix_global->process_list){
225 if(process->request.call != REQ_NO_REQ
226 && MC_request_is_enabled(&process->request)){
236 * \brief Re-executes from the initial state all the transitions indicated by
237 * a given model-checker stack.
238 * \param stack The stack with the transitions to execute.
240 void MC_replay(xbt_fifo_t stack)
244 smx_req_t req = NULL, saved_req = NULL;
245 xbt_fifo_item_t item;
248 XBT_DEBUG("**** Begin Replay ****");
250 /* Restore the initial state */
251 MC_restore_snapshot(initial_snapshot);
252 /* At the moment of taking the snapshot the raw heap was set, so restoring
253 * it will set it back again, we have to unset it to continue */
256 /* Traverse the stack from the initial state and re-execute the transitions */
257 for (item = xbt_fifo_get_last_item(stack);
258 item != xbt_fifo_get_first_item(stack);
259 item = xbt_fifo_get_prev_item(item)) {
261 state = (mc_state_t) xbt_fifo_get_item_content(item);
262 saved_req = MC_state_get_executed_request(state, &value);
265 /* because we got a copy of the executed request, we have to fetch the
266 real one, pointed by the request field of the issuer process */
267 req = &saved_req->issuer->request;
269 /* Debug information */
270 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
271 req_str = MC_request_to_string(req, value);
272 XBT_DEBUG("Replay: %s (%p)", req_str, state);
277 SIMIX_request_pre(req, value);
278 MC_wait_for_requests();
280 /* Update statistics */
281 mc_stats->visited_states++;
282 mc_stats->executed_transitions++;
284 XBT_DEBUG("**** End Replay ****");
287 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
291 smx_req_t req = NULL, saved_req = NULL;
292 xbt_fifo_item_t item;
294 mc_pair_stateless_t pair;
297 XBT_DEBUG("**** Begin Replay ****");
299 /* Restore the initial state */
300 MC_restore_snapshot(initial_snapshot_liveness);
301 /* At the moment of taking the snapshot the raw heap was set, so restoring
302 * it will set it back again, we have to unset it to continue */
307 item = xbt_fifo_get_last_item(stack);
309 while(depth <= xbt_fifo_size(stack)){
311 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
312 state = (mc_state_t) pair->graph_state;
314 if(pair->requests > 0){
316 saved_req = MC_state_get_executed_request(state, &value);
317 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
319 if(saved_req != NULL){
320 /* because we got a copy of the executed request, we have to fetch the
321 real one, pointed by the request field of the issuer process */
322 req = &saved_req->issuer->request;
323 //XBT_DEBUG("Req->call %u", req->call);
325 /* Debug information */
326 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
327 req_str = MC_request_to_string(req, value);
328 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
334 SIMIX_request_pre(req, value);
335 MC_wait_for_requests();
340 /* Update statistics */
341 mc_stats_pair->visited_pairs++;
343 item = xbt_fifo_get_prev_item(item);
348 /* Traverse the stack from the initial state and re-execute the transitions */
349 for (item = xbt_fifo_get_last_item(stack);
350 item != xbt_fifo_get_first_item(stack);
351 item = xbt_fifo_get_prev_item(item)) {
353 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
354 state = (mc_state_t) pair->graph_state;
356 if(pair->requests > 0){
358 saved_req = MC_state_get_executed_request(state, &value);
359 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
361 if(saved_req != NULL){
362 /* because we got a copy of the executed request, we have to fetch the
363 real one, pointed by the request field of the issuer process */
364 req = &saved_req->issuer->request;
365 //XBT_DEBUG("Req->call %u", req->call);
367 /* Debug information */
368 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
369 req_str = MC_request_to_string(req, value);
370 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
376 SIMIX_request_pre(req, value);
377 MC_wait_for_requests();
382 /* Update statistics */
383 mc_stats_pair->visited_pairs++;
387 XBT_DEBUG("**** End Replay ****");
392 * \brief Dumps the contents of a model-checker's stack and shows the actual
394 * \param stack The stack to dump
396 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
400 MC_show_stack_safety_stateless(stack);
403 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
404 MC_state_delete(state);
409 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
413 xbt_fifo_item_t item;
415 char *req_str = NULL;
417 for (item = xbt_fifo_get_last_item(stack);
418 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
419 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
420 req = MC_state_get_executed_request(state, &value);
422 req_str = MC_request_to_string(req, value);
423 XBT_INFO("%s", req_str);
429 void MC_show_deadlock(smx_req_t req)
431 /*char *req_str = NULL;*/
432 XBT_INFO("**************************");
433 XBT_INFO("*** DEAD-LOCK DETECTED ***");
434 XBT_INFO("**************************");
435 XBT_INFO("Locked request:");
436 /*req_str = MC_request_to_string(req);
437 XBT_INFO("%s", req_str);
439 XBT_INFO("Counter-example execution trace:");
440 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
443 void MC_show_deadlock_stateful(smx_req_t req)
445 /*char *req_str = NULL;*/
446 XBT_INFO("**************************");
447 XBT_INFO("*** DEAD-LOCK DETECTED ***");
448 XBT_INFO("**************************");
449 XBT_INFO("Locked request:");
450 /*req_str = MC_request_to_string(req);
451 XBT_INFO("%s", req_str);
453 XBT_INFO("Counter-example execution trace:");
454 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
457 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
459 //mc_state_ws_t state;
461 MC_show_stack_safety_stateful(stack);
464 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
465 MC_state_delete(state);
470 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
474 xbt_fifo_item_t item;
476 char *req_str = NULL;
478 for (item = xbt_fifo_get_last_item(stack);
479 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
480 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
481 req = MC_state_get_executed_request(state->graph_state, &value);
483 req_str = MC_request_to_string(req, value);
484 XBT_INFO("%s", req_str);
490 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
493 xbt_fifo_item_t item;
495 char *req_str = NULL;
497 for (item = xbt_fifo_get_last_item(stack);
498 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
499 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
500 req = MC_state_get_executed_request(pair->graph_state, &value);
502 req_str = MC_request_to_string(req, value);
503 XBT_INFO("%s", req_str);
509 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
513 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
514 MC_pair_delete(pair);
518 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
520 mc_pair_stateless_t pair;
521 xbt_fifo_item_t item;
523 char *req_str = NULL;
525 for (item = xbt_fifo_get_last_item(stack);
526 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
527 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
528 req = MC_state_get_executed_request(pair->graph_state, &value);
530 if(pair->requests>0){
531 req_str = MC_request_to_string(req, value);
532 XBT_INFO("%s", req_str);
535 XBT_INFO("End of system requests but evolution in Büchi automaton");
541 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
542 mc_pair_stateless_t pair;
545 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
546 MC_pair_stateless_delete(pair);
551 void MC_print_statistics(mc_stats_t stats)
553 XBT_INFO("State space size ~= %lu", stats->state_size);
554 XBT_INFO("Expanded states = %lu", stats->expanded_states);
555 XBT_INFO("Visited states = %lu", stats->visited_states);
556 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
557 XBT_INFO("Expanded / Visited = %lf",
558 (double) stats->visited_states / stats->expanded_states);
559 /*XBT_INFO("Exploration coverage = %lf",
560 (double)stats->expanded_states / stats->state_size); */
563 void MC_print_statistics_pairs(mc_stats_pair_t stats)
565 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
566 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
567 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
568 XBT_INFO("Expanded / Visited = %lf",
569 (double) stats->visited_pairs / stats->expanded_pairs);
570 /*XBT_INFO("Exploration coverage = %lf",
571 (double)stats->expanded_states / stats->state_size); */
574 void MC_assert(int prop)
576 if (MC_IS_ENABLED && !prop) {
577 XBT_INFO("**************************");
578 XBT_INFO("*** PROPERTY NOT VALID ***");
579 XBT_INFO("**************************");
580 XBT_INFO("Counter-example execution trace:");
581 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
582 MC_print_statistics(mc_stats);
587 void MC_assert_stateful(int prop)
589 if (MC_IS_ENABLED && !prop) {
590 XBT_INFO("**************************");
591 XBT_INFO("*** PROPERTY NOT VALID ***");
592 XBT_INFO("**************************");
593 XBT_INFO("Counter-example execution trace:");
594 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
595 MC_print_statistics(mc_stats);
600 void MC_assert_pair_stateful(int prop){
601 if (MC_IS_ENABLED && !prop) {
602 XBT_INFO("**************************");
603 XBT_INFO("*** PROPERTY NOT VALID ***");
604 XBT_INFO("**************************");
605 //XBT_INFO("Counter-example execution trace:");
606 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
607 //MC_dump_snapshot_stack(mc_snapshot_stack);
608 MC_print_statistics_pairs(mc_stats_pair);
613 void MC_assert_pair_stateless(int prop){
614 if (MC_IS_ENABLED && !prop) {
615 XBT_INFO("**************************");
616 XBT_INFO("*** PROPERTY NOT VALID ***");
617 XBT_INFO("**************************");
618 //XBT_INFO("Counter-example execution trace:");
619 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
620 //MC_dump_snapshot_stack(mc_snapshot_stack);
621 MC_print_statistics_pairs(mc_stats_pair);
626 void MC_process_clock_add(smx_process_t process, double amount)
628 mc_time[process->pid] += amount;
631 double MC_process_clock_get(smx_process_t process)
634 return mc_time[process->pid];