1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "../surf/surf_private.h"
12 #include "../simix/smx_private.h"
14 #include "mc_private.h"
15 #include "xbt/automaton/automaton_create.h"
17 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
19 "Logging specific to MC (global)");
21 /* MC global data structures */
23 mc_state_t mc_current_state = NULL;
24 char mc_replay_mode = FALSE;
25 double *mc_time = NULL;
26 mc_snapshot_t initial_snapshot = NULL;
30 xbt_fifo_t mc_stack_safety_stateful = NULL;
31 xbt_fifo_t mc_stack_safety_stateless = NULL;
32 mc_stats_t mc_stats = NULL;
36 mc_stats_pair_t mc_stats_pair = NULL;
37 xbt_fifo_t mc_stack_liveness = NULL;
38 mc_snapshot_t initial_snapshot_liveness = NULL;
40 xbt_automaton_t automaton;
42 static void MC_init_liveness(xbt_automaton_t a);
43 static void MC_assert_pair(int prop);
47 * \brief Initialize the model-checker data structures
49 void MC_init_safety_stateless(void)
52 /* Check if MC is already initialized */
56 mc_time = xbt_new0(double, simix_process_maxpid);
58 /* Initialize the data structures that must be persistent across every
59 iteration of the model-checker (in RAW memory) */
62 /* Initialize statistics */
63 mc_stats = xbt_new0(s_mc_stats_t, 1);
64 mc_stats->state_size = 1;
66 /* Create exploration stack */
67 mc_stack_safety_stateless = xbt_fifo_new();
74 /* Save the initial state */
75 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
76 MC_take_snapshot(initial_snapshot);
80 void MC_init_safety_stateful(void){
83 /* Check if MC is already initialized */
87 mc_time = xbt_new0(double, simix_process_maxpid);
89 /* Initialize the data structures that must be persistent across every
90 iteration of the model-checker (in RAW memory) */
93 /* Initialize statistics */
94 mc_stats = xbt_new0(s_mc_stats_t, 1);
95 mc_stats->state_size = 1;
97 /* Create exploration stack */
98 mc_stack_safety_stateful = xbt_fifo_new();
102 MC_dpor_stateful_init();
107 static void MC_init_liveness(xbt_automaton_t a){
109 XBT_DEBUG("Start init mc");
111 mc_time = xbt_new0(double, simix_process_maxpid);
113 /* Initialize the data structures that must be persistent across every
114 iteration of the model-checker (in RAW memory) */
118 /* Initialize statistics */
119 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
121 XBT_DEBUG("Creating stack");
123 /* Create exploration stack */
124 mc_stack_liveness = xbt_fifo_new();
136 void MC_modelcheck(void)
138 MC_init_safety_stateless();
143 void MC_modelcheck_stateful(void)
145 MC_init_safety_stateful();
151 void MC_modelcheck_liveness(xbt_automaton_t a){
156 void MC_exit_liveness(void)
158 MC_print_statistics_pairs(mc_stats_pair);
167 MC_print_statistics(mc_stats);
173 int MC_random(int min, int max)
175 /*FIXME: return mc_current_state->executed_transition->random.value;*/
180 * \brief Schedules all the process that are ready to run
182 void MC_wait_for_requests(void)
184 smx_process_t process;
188 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
189 SIMIX_process_runall();
190 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
191 req = &process->simcall;
192 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
193 SIMIX_simcall_pre(req, 0);
198 int MC_deadlock_check()
200 int deadlock = FALSE;
201 smx_process_t process;
202 if(xbt_swag_size(simix_global->process_list)){
204 xbt_swag_foreach(process, simix_global->process_list){
205 if(process->simcall.call != SIMCALL_NONE
206 && MC_request_is_enabled(&process->simcall)){
216 * \brief Re-executes from the initial state all the transitions indicated by
217 * a given model-checker stack.
218 * \param stack The stack with the transitions to execute.
220 void MC_replay(xbt_fifo_t stack)
224 smx_simcall_t req = NULL, saved_req = NULL;
225 xbt_fifo_item_t item;
228 XBT_DEBUG("**** Begin Replay ****");
230 /* Restore the initial state */
231 MC_restore_snapshot(initial_snapshot);
232 /* At the moment of taking the snapshot the raw heap was set, so restoring
233 * it will set it back again, we have to unset it to continue */
236 /* Traverse the stack from the initial state and re-execute the transitions */
237 for (item = xbt_fifo_get_last_item(stack);
238 item != xbt_fifo_get_first_item(stack);
239 item = xbt_fifo_get_prev_item(item)) {
241 state = (mc_state_t) xbt_fifo_get_item_content(item);
242 saved_req = MC_state_get_executed_request(state, &value);
245 /* because we got a copy of the executed request, we have to fetch the
246 real one, pointed by the request field of the issuer process */
247 req = &saved_req->issuer->simcall;
249 /* Debug information */
250 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
251 req_str = MC_request_to_string(req, value);
252 XBT_DEBUG("Replay: %s (%p)", req_str, state);
257 SIMIX_simcall_pre(req, value);
258 MC_wait_for_requests();
260 /* Update statistics */
261 mc_stats->visited_states++;
262 mc_stats->executed_transitions++;
264 XBT_DEBUG("**** End Replay ****");
267 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
271 smx_simcall_t req = NULL, saved_req = NULL;
272 xbt_fifo_item_t item;
274 mc_pair_stateless_t pair;
277 XBT_DEBUG("**** Begin Replay ****");
279 /* Restore the initial state */
280 MC_restore_snapshot(initial_snapshot_liveness);
281 /* At the moment of taking the snapshot the raw heap was set, so restoring
282 * it will set it back again, we have to unset it to continue */
287 item = xbt_fifo_get_last_item(stack);
289 while(depth <= xbt_fifo_size(stack)){
291 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
292 state = (mc_state_t) pair->graph_state;
294 if(pair->requests > 0){
296 saved_req = MC_state_get_executed_request(state, &value);
297 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
299 if(saved_req != NULL){
300 /* because we got a copy of the executed request, we have to fetch the
301 real one, pointed by the request field of the issuer process */
302 req = &saved_req->issuer->simcall;
303 //XBT_DEBUG("Req->call %u", req->call);
305 /* Debug information */
306 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
307 req_str = MC_request_to_string(req, value);
308 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
314 SIMIX_simcall_pre(req, value);
315 MC_wait_for_requests();
320 /* Update statistics */
321 mc_stats_pair->visited_pairs++;
323 item = xbt_fifo_get_prev_item(item);
328 /* Traverse the stack from the initial state and re-execute the transitions */
329 for (item = xbt_fifo_get_last_item(stack);
330 item != xbt_fifo_get_first_item(stack);
331 item = xbt_fifo_get_prev_item(item)) {
333 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
334 state = (mc_state_t) pair->graph_state;
336 if(pair->requests > 0){
338 saved_req = MC_state_get_executed_request(state, &value);
339 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
341 if(saved_req != NULL){
342 /* because we got a copy of the executed request, we have to fetch the
343 real one, pointed by the request field of the issuer process */
344 req = &saved_req->issuer->simcall;
345 //XBT_DEBUG("Req->call %u", req->call);
347 /* Debug information */
348 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
349 req_str = MC_request_to_string(req, value);
350 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
356 SIMIX_simcall_pre(req, value);
357 MC_wait_for_requests();
362 /* Update statistics */
363 mc_stats_pair->visited_pairs++;
367 XBT_DEBUG("**** End Replay ****");
372 * \brief Dumps the contents of a model-checker's stack and shows the actual
374 * \param stack The stack to dump
376 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
380 MC_show_stack_safety_stateless(stack);
383 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
384 MC_state_delete(state);
389 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
393 xbt_fifo_item_t item;
395 char *req_str = NULL;
397 for (item = xbt_fifo_get_last_item(stack);
398 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
399 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
400 req = MC_state_get_executed_request(state, &value);
402 req_str = MC_request_to_string(req, value);
403 XBT_INFO("%s", req_str);
409 void MC_show_deadlock(smx_simcall_t req)
411 /*char *req_str = NULL;*/
412 XBT_INFO("**************************");
413 XBT_INFO("*** DEAD-LOCK DETECTED ***");
414 XBT_INFO("**************************");
415 XBT_INFO("Locked request:");
416 /*req_str = MC_request_to_string(req);
417 XBT_INFO("%s", req_str);
419 XBT_INFO("Counter-example execution trace:");
420 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
423 void MC_show_deadlock_stateful(smx_simcall_t req)
425 /*char *req_str = NULL;*/
426 XBT_INFO("**************************");
427 XBT_INFO("*** DEAD-LOCK DETECTED ***");
428 XBT_INFO("**************************");
429 XBT_INFO("Locked request:");
430 /*req_str = MC_request_to_string(req);
431 XBT_INFO("%s", req_str);
433 XBT_INFO("Counter-example execution trace:");
434 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
437 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
439 //mc_state_ws_t state;
441 MC_show_stack_safety_stateful(stack);
444 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
445 MC_state_delete(state);
450 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
454 xbt_fifo_item_t item;
456 char *req_str = NULL;
458 for (item = xbt_fifo_get_last_item(stack);
459 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
460 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
461 req = MC_state_get_executed_request(state->graph_state, &value);
463 req_str = MC_request_to_string(req, value);
464 XBT_INFO("%s", req_str);
471 void MC_show_stack_liveness(xbt_fifo_t stack){
473 mc_pair_stateless_t pair;
474 xbt_fifo_item_t item;
476 char *req_str = NULL;
478 for (item = xbt_fifo_get_last_item(stack);
479 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
480 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
481 req = MC_state_get_executed_request(pair->graph_state, &value);
483 if(pair->requests>0){
484 req_str = MC_request_to_string(req, value);
485 XBT_INFO("%s", req_str);
488 XBT_INFO("End of system requests but evolution in Büchi automaton");
494 void MC_dump_stack_liveness(xbt_fifo_t stack){
495 mc_pair_stateless_t pair;
498 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
499 MC_pair_stateless_delete(pair);
504 void MC_print_statistics(mc_stats_t stats)
506 //XBT_INFO("State space size ~= %lu", stats->state_size);
507 XBT_INFO("Expanded states = %lu", stats->expanded_states);
508 XBT_INFO("Visited states = %lu", stats->visited_states);
509 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
510 XBT_INFO("Expanded / Visited = %lf",
511 (double) stats->visited_states / stats->expanded_states);
512 /*XBT_INFO("Exploration coverage = %lf",
513 (double)stats->expanded_states / stats->state_size); */
516 void MC_print_statistics_pairs(mc_stats_pair_t stats)
518 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
519 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
520 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
521 XBT_INFO("Expanded / Visited = %lf",
522 (double) stats->visited_pairs / stats->expanded_pairs);
523 /*XBT_INFO("Exploration coverage = %lf",
524 (double)stats->expanded_states / stats->state_size); */
527 void MC_assert(int prop)
529 if (MC_IS_ENABLED && !prop){
530 XBT_INFO("**************************");
531 XBT_INFO("*** PROPERTY NOT VALID ***");
532 XBT_INFO("**************************");
533 XBT_INFO("Counter-example execution trace:");
534 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
535 MC_print_statistics(mc_stats);
540 void MC_assert_stateful(int prop)
542 if (MC_IS_ENABLED && !prop) {
543 XBT_INFO("**************************");
544 XBT_INFO("*** PROPERTY NOT VALID ***");
545 XBT_INFO("**************************");
546 XBT_INFO("Counter-example execution trace:");
547 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
548 MC_print_statistics(mc_stats);
554 static void MC_assert_pair(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(mc_stack_liveness);
561 //MC_dump_snapshot_stack(mc_snapshot_stack);
562 MC_print_statistics_pairs(mc_stats_pair);
567 void MC_process_clock_add(smx_process_t process, double amount)
569 mc_time[process->pid] += amount;
572 double MC_process_clock_get(smx_process_t process)
575 return mc_time[process->pid];
582 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
583 MC_take_snapshot_liveness(sn);
587 XBT_INFO("Number of regions : %u", sn->num_reg);
589 for(i=0; i<sn->num_reg; i++){
591 switch(sn->regions[i]->type){
593 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
594 mmalloc_display_info_heap(sn->regions[i]->data);
596 case 1 : /* libsimgrid */
597 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
599 case 2 : /* data program */
600 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
603 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
604 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
612 xbt_automaton_t MC_create_automaton(const char *file){
614 xbt_automaton_t a = xbt_create_automaton(file);