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_ddfs_stateful_init(a);
124 void MC_init_liveness_stateless(xbt_automaton_t a){
126 XBT_DEBUG("Start init mc");
128 mc_time = xbt_new0(double, simix_process_maxpid);
130 /* Initialize the data structures that must be persistent across every
131 iteration of the model-checker (in RAW memory) */
135 /* Initialize statistics */
136 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
138 XBT_DEBUG("Creating stack");
140 /* Create exploration stack */
141 mc_stack_liveness_stateless = xbt_fifo_new();
145 MC_ddfs_stateless_init(a);
151 void MC_modelcheck(void)
153 MC_init_safety_stateless();
158 void MC_modelcheck_stateful(void)
160 MC_init_safety_stateful();
165 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
166 MC_init_liveness_stateful(a);
170 void MC_modelcheck_liveness_stateless(xbt_automaton_t a){
171 MC_init_liveness_stateless(a);
175 void MC_exit_liveness(void)
177 MC_print_statistics_pairs(mc_stats_pair);
185 MC_print_statistics(mc_stats);
191 int MC_random(int min, int max)
193 /*FIXME: return mc_current_state->executed_transition->random.value;*/
198 * \brief Schedules all the process that are ready to run
200 void MC_wait_for_requests(void)
202 smx_process_t process;
206 while (xbt_dynar_length(simix_global->process_to_run)) {
207 SIMIX_process_runall();
208 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
209 req = &process->request;
210 if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
211 SIMIX_request_pre(req, 0);
216 int MC_deadlock_check()
218 int deadlock = FALSE;
219 smx_process_t process;
220 if(xbt_swag_size(simix_global->process_list)){
222 xbt_swag_foreach(process, simix_global->process_list){
223 if(process->request.call != REQ_NO_REQ
224 && MC_request_is_enabled(&process->request)){
234 * \brief Re-executes from the initial state all the transitions indicated by
235 * a given model-checker stack.
236 * \param stack The stack with the transitions to execute.
238 void MC_replay(xbt_fifo_t stack)
242 smx_req_t req = NULL, saved_req = NULL;
243 xbt_fifo_item_t item;
246 XBT_DEBUG("**** Begin Replay ****");
248 /* Restore the initial state */
249 MC_restore_snapshot(initial_snapshot);
250 /* At the moment of taking the snapshot the raw heap was set, so restoring
251 * it will set it back again, we have to unset it to continue */
254 /* Traverse the stack from the initial state and re-execute the transitions */
255 for (item = xbt_fifo_get_last_item(stack);
256 item != xbt_fifo_get_first_item(stack);
257 item = xbt_fifo_get_prev_item(item)) {
259 state = (mc_state_t) xbt_fifo_get_item_content(item);
260 saved_req = MC_state_get_executed_request(state, &value);
263 /* because we got a copy of the executed request, we have to fetch the
264 real one, pointed by the request field of the issuer process */
265 req = &saved_req->issuer->request;
267 /* Debug information */
268 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
269 req_str = MC_request_to_string(req, value);
270 XBT_DEBUG("Replay: %s (%p)", req_str, state);
275 SIMIX_request_pre(req, value);
276 MC_wait_for_requests();
278 /* Update statistics */
279 mc_stats->visited_states++;
280 mc_stats->executed_transitions++;
282 XBT_DEBUG("**** End Replay ****");
285 void MC_replay_liveness(xbt_fifo_t stack)
289 smx_req_t req = NULL, saved_req = NULL;
290 xbt_fifo_item_t item;
292 mc_pair_stateless_t pair;
295 XBT_DEBUG("**** Begin Replay ****");
297 /* Restore the initial state */
298 MC_restore_snapshot(initial_snapshot);
299 /* At the moment of taking the snapshot the raw heap was set, so restoring
300 * it will set it back again, we have to unset it to continue */
303 /* Traverse the stack from the initial state and re-execute the transitions */
304 for (item = xbt_fifo_get_last_item(stack);
305 item != xbt_fifo_get_first_item(stack);
306 item = xbt_fifo_get_prev_item(item)) {
308 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
309 state = (mc_state_t) pair->graph_state;
311 if(pair->requests > 0){
313 saved_req = MC_state_get_executed_request(state, &value);
314 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
316 if(saved_req != NULL){
317 /* because we got a copy of the executed request, we have to fetch the
318 real one, pointed by the request field of the issuer process */
319 req = &saved_req->issuer->request;
320 //XBT_DEBUG("Req->call %u", req->call);
322 /* Debug information */
323 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
324 req_str = MC_request_to_string(req, value);
325 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
331 SIMIX_request_pre(req, value);
332 MC_wait_for_requests();
337 /* Update statistics */
338 mc_stats_pair->visited_pairs++;
341 XBT_DEBUG("**** End Replay ****");
346 * \brief Dumps the contents of a model-checker's stack and shows the actual
348 * \param stack The stack to dump
350 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
354 MC_show_stack_safety_stateless(stack);
357 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
358 MC_state_delete(state);
363 void MC_show_stack_safety_stateless(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_t) (xbt_fifo_get_item_content(item)))
373 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
374 req = MC_state_get_executed_request(state, &value);
376 req_str = MC_request_to_string(req, value);
377 XBT_INFO("%s", req_str);
383 void MC_show_deadlock(smx_req_t req)
385 /*char *req_str = NULL;*/
386 XBT_INFO("**************************");
387 XBT_INFO("*** DEAD-LOCK DETECTED ***");
388 XBT_INFO("**************************");
389 XBT_INFO("Locked request:");
390 /*req_str = MC_request_to_string(req);
391 XBT_INFO("%s", req_str);
393 XBT_INFO("Counter-example execution trace:");
394 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
397 void MC_show_deadlock_stateful(smx_req_t req)
399 /*char *req_str = NULL;*/
400 XBT_INFO("**************************");
401 XBT_INFO("*** DEAD-LOCK DETECTED ***");
402 XBT_INFO("**************************");
403 XBT_INFO("Locked request:");
404 /*req_str = MC_request_to_string(req);
405 XBT_INFO("%s", req_str);
407 XBT_INFO("Counter-example execution trace:");
408 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
411 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
413 //mc_state_ws_t state;
415 MC_show_stack_safety_stateful(stack);
418 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
419 MC_state_delete(state);
424 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
428 xbt_fifo_item_t item;
430 char *req_str = NULL;
432 for (item = xbt_fifo_get_last_item(stack);
433 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
434 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
435 req = MC_state_get_executed_request(state->graph_state, &value);
437 req_str = MC_request_to_string(req, value);
438 XBT_INFO("%s", req_str);
444 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
447 xbt_fifo_item_t item;
449 char *req_str = NULL;
451 for (item = xbt_fifo_get_last_item(stack);
452 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
453 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
454 req = MC_state_get_executed_request(pair->graph_state, &value);
456 req_str = MC_request_to_string(req, value);
457 XBT_INFO("%s", req_str);
463 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
467 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
468 MC_pair_delete(pair);
472 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
474 mc_pair_stateless_t pair;
475 xbt_fifo_item_t item;
477 char *req_str = NULL;
479 for (item = xbt_fifo_get_last_item(stack);
480 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
481 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
482 req = MC_state_get_executed_request(pair->graph_state, &value);
484 if(pair->requests>0){
485 req_str = MC_request_to_string(req, value);
486 XBT_INFO("%s", req_str);
489 XBT_INFO("End of system requests but evolution in Büchi automaton");
495 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
496 mc_pair_stateless_t pair;
499 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
500 MC_pair_stateless_delete(pair);
505 void MC_print_statistics(mc_stats_t stats)
507 XBT_INFO("State space size ~= %lu", stats->state_size);
508 XBT_INFO("Expanded states = %lu", stats->expanded_states);
509 XBT_INFO("Visited states = %lu", stats->visited_states);
510 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
511 XBT_INFO("Expanded / Visited = %lf",
512 (double) stats->visited_states / stats->expanded_states);
513 /*XBT_INFO("Exploration coverage = %lf",
514 (double)stats->expanded_states / stats->state_size); */
517 void MC_print_statistics_pairs(mc_stats_pair_t stats)
519 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
520 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
521 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
522 XBT_INFO("Expanded / Visited = %lf",
523 (double) stats->visited_pairs / stats->expanded_pairs);
524 /*XBT_INFO("Exploration coverage = %lf",
525 (double)stats->expanded_states / stats->state_size); */
528 void MC_assert(int prop)
530 if (MC_IS_ENABLED && !prop) {
531 XBT_INFO("**************************");
532 XBT_INFO("*** PROPERTY NOT VALID ***");
533 XBT_INFO("**************************");
534 XBT_INFO("Counter-example execution trace:");
535 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
536 MC_print_statistics(mc_stats);
541 void MC_assert_stateful(int prop)
543 if (MC_IS_ENABLED && !prop) {
544 XBT_INFO("**************************");
545 XBT_INFO("*** PROPERTY NOT VALID ***");
546 XBT_INFO("**************************");
547 XBT_INFO("Counter-example execution trace:");
548 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
549 MC_print_statistics(mc_stats);
554 void MC_assert_pair_stateful(int prop){
555 if (MC_IS_ENABLED && !prop) {
556 XBT_INFO("**************************");
557 XBT_INFO("*** PROPERTY NOT VALID ***");
558 XBT_INFO("**************************");
559 //XBT_INFO("Counter-example execution trace:");
560 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
561 //MC_dump_snapshot_stack(mc_snapshot_stack);
562 MC_print_statistics_pairs(mc_stats_pair);
567 void MC_assert_pair_stateless(int prop){
568 if (MC_IS_ENABLED && !prop) {
569 XBT_INFO("**************************");
570 XBT_INFO("*** PROPERTY NOT VALID ***");
571 XBT_INFO("**************************");
572 //XBT_INFO("Counter-example execution trace:");
573 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
574 //MC_dump_snapshot_stack(mc_snapshot_stack);
575 MC_print_statistics_pairs(mc_stats_pair);
580 void MC_process_clock_add(smx_process_t process, double amount)
582 mc_time[process->pid] += amount;
585 double MC_process_clock_get(smx_process_t process)
588 return mc_time[process->pid];