6 #include "../surf/surf_private.h"
7 #include "../simix/smx_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 mc_stats_pair_t mc_stats_pair = NULL;
31 xbt_fifo_t mc_stack_liveness = NULL;
32 mc_snapshot_t initial_snapshot_liveness = NULL;
34 xbt_automaton_t automaton;
37 static void MC_init_liveness(xbt_automaton_t a, char *prgm);
38 static void MC_assert_pair(int prop);
42 * \brief Initialize the model-checker data structures
44 void MC_init_safety_stateless(void)
47 /* Check if MC is already initialized */
51 mc_time = xbt_new0(double, simix_process_maxpid);
53 /* Initialize the data structures that must be persistent across every
54 iteration of the model-checker (in RAW memory) */
57 /* Initialize statistics */
58 mc_stats = xbt_new0(s_mc_stats_t, 1);
59 mc_stats->state_size = 1;
61 /* Create exploration stack */
62 mc_stack_safety_stateless = xbt_fifo_new();
69 /* Save the initial state */
70 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
71 MC_take_snapshot(initial_snapshot);
75 void MC_init_safety_stateful(void){
78 /* Check if MC is already initialized */
82 mc_time = xbt_new0(double, simix_process_maxpid);
84 /* Initialize the data structures that must be persistent across every
85 iteration of the model-checker (in RAW memory) */
88 /* Initialize statistics */
89 mc_stats = xbt_new0(s_mc_stats_t, 1);
90 mc_stats->state_size = 1;
92 /* Create exploration stack */
93 mc_stack_safety_stateful = xbt_fifo_new();
97 MC_dpor_stateful_init();
102 static void MC_init_liveness(xbt_automaton_t a, char *prgm){
104 XBT_DEBUG("Start init mc");
106 mc_time = xbt_new0(double, simix_process_maxpid);
108 /* Initialize the data structures that must be persistent across every
109 iteration of the model-checker (in RAW memory) */
113 /* Initialize statistics */
114 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
116 XBT_DEBUG("Creating stack");
118 /* Create exploration stack */
119 mc_stack_liveness = xbt_fifo_new();
124 prog_name = strdup(prgm);
132 void MC_modelcheck(void)
134 MC_init_safety_stateless();
139 void MC_modelcheck_stateful(void)
141 MC_init_safety_stateful();
147 void MC_modelcheck_liveness(xbt_automaton_t a, char *prgm){
148 MC_init_liveness(a, prgm);
152 void MC_exit_liveness(void)
154 MC_print_statistics_pairs(mc_stats_pair);
163 MC_print_statistics(mc_stats);
169 int MC_random(int min, int max)
171 /*FIXME: return mc_current_state->executed_transition->random.value;*/
176 * \brief Schedules all the process that are ready to run
178 void MC_wait_for_requests(void)
180 smx_process_t process;
184 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
185 SIMIX_process_runall();
186 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
187 req = &process->simcall;
188 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
189 SIMIX_simcall_pre(req, 0);
194 int MC_deadlock_check()
196 int deadlock = FALSE;
197 smx_process_t process;
198 if(xbt_swag_size(simix_global->process_list)){
200 xbt_swag_foreach(process, simix_global->process_list){
201 if(process->simcall.call != SIMCALL_NONE
202 && MC_request_is_enabled(&process->simcall)){
212 * \brief Re-executes from the initial state all the transitions indicated by
213 * a given model-checker stack.
214 * \param stack The stack with the transitions to execute.
216 void MC_replay(xbt_fifo_t stack)
220 smx_simcall_t req = NULL, saved_req = NULL;
221 xbt_fifo_item_t item;
224 XBT_DEBUG("**** Begin Replay ****");
226 /* Restore the initial state */
227 MC_restore_snapshot(initial_snapshot);
228 /* At the moment of taking the snapshot the raw heap was set, so restoring
229 * it will set it back again, we have to unset it to continue */
232 /* Traverse the stack from the initial state and re-execute the transitions */
233 for (item = xbt_fifo_get_last_item(stack);
234 item != xbt_fifo_get_first_item(stack);
235 item = xbt_fifo_get_prev_item(item)) {
237 state = (mc_state_t) xbt_fifo_get_item_content(item);
238 saved_req = MC_state_get_executed_request(state, &value);
241 /* because we got a copy of the executed request, we have to fetch the
242 real one, pointed by the request field of the issuer process */
243 req = &saved_req->issuer->simcall;
245 /* Debug information */
246 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
247 req_str = MC_request_to_string(req, value);
248 XBT_DEBUG("Replay: %s (%p)", req_str, state);
253 SIMIX_simcall_pre(req, value);
254 MC_wait_for_requests();
256 /* Update statistics */
257 mc_stats->visited_states++;
258 mc_stats->executed_transitions++;
260 XBT_DEBUG("**** End Replay ****");
263 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
267 smx_simcall_t req = NULL, saved_req = NULL;
268 xbt_fifo_item_t item;
270 mc_pair_stateless_t pair;
273 XBT_DEBUG("**** Begin Replay ****");
275 /* Restore the initial state */
276 MC_restore_snapshot(initial_snapshot_liveness);
277 /* At the moment of taking the snapshot the raw heap was set, so restoring
278 * it will set it back again, we have to unset it to continue */
283 item = xbt_fifo_get_last_item(stack);
285 while(depth <= xbt_fifo_size(stack)){
287 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
288 state = (mc_state_t) pair->graph_state;
290 if(pair->requests > 0){
292 saved_req = MC_state_get_executed_request(state, &value);
293 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
295 if(saved_req != NULL){
296 /* because we got a copy of the executed request, we have to fetch the
297 real one, pointed by the request field of the issuer process */
298 req = &saved_req->issuer->simcall;
299 //XBT_DEBUG("Req->call %u", req->call);
301 /* Debug information */
302 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
303 req_str = MC_request_to_string(req, value);
304 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
310 SIMIX_simcall_pre(req, value);
311 MC_wait_for_requests();
316 /* Update statistics */
317 mc_stats_pair->visited_pairs++;
319 item = xbt_fifo_get_prev_item(item);
324 /* Traverse the stack from the initial state and re-execute the transitions */
325 for (item = xbt_fifo_get_last_item(stack);
326 item != xbt_fifo_get_first_item(stack);
327 item = xbt_fifo_get_prev_item(item)) {
329 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
330 state = (mc_state_t) pair->graph_state;
332 if(pair->requests > 0){
334 saved_req = MC_state_get_executed_request(state, &value);
335 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
337 if(saved_req != NULL){
338 /* because we got a copy of the executed request, we have to fetch the
339 real one, pointed by the request field of the issuer process */
340 req = &saved_req->issuer->simcall;
341 //XBT_DEBUG("Req->call %u", req->call);
343 /* Debug information */
344 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
345 req_str = MC_request_to_string(req, value);
346 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
352 SIMIX_simcall_pre(req, value);
353 MC_wait_for_requests();
358 /* Update statistics */
359 mc_stats_pair->visited_pairs++;
363 XBT_DEBUG("**** End Replay ****");
368 * \brief Dumps the contents of a model-checker's stack and shows the actual
370 * \param stack The stack to dump
372 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
376 MC_show_stack_safety_stateless(stack);
379 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
380 MC_state_delete(state);
385 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
389 xbt_fifo_item_t item;
391 char *req_str = NULL;
393 for (item = xbt_fifo_get_last_item(stack);
394 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
395 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
396 req = MC_state_get_executed_request(state, &value);
398 req_str = MC_request_to_string(req, value);
399 XBT_INFO("%s", req_str);
405 void MC_show_deadlock(smx_simcall_t req)
407 /*char *req_str = NULL;*/
408 XBT_INFO("**************************");
409 XBT_INFO("*** DEAD-LOCK DETECTED ***");
410 XBT_INFO("**************************");
411 XBT_INFO("Locked request:");
412 /*req_str = MC_request_to_string(req);
413 XBT_INFO("%s", req_str);
415 XBT_INFO("Counter-example execution trace:");
416 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
419 void MC_show_deadlock_stateful(smx_simcall_t req)
421 /*char *req_str = NULL;*/
422 XBT_INFO("**************************");
423 XBT_INFO("*** DEAD-LOCK DETECTED ***");
424 XBT_INFO("**************************");
425 XBT_INFO("Locked request:");
426 /*req_str = MC_request_to_string(req);
427 XBT_INFO("%s", req_str);
429 XBT_INFO("Counter-example execution trace:");
430 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
433 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
435 //mc_state_ws_t state;
437 MC_show_stack_safety_stateful(stack);
440 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
441 MC_state_delete(state);
446 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
450 xbt_fifo_item_t item;
452 char *req_str = NULL;
454 for (item = xbt_fifo_get_last_item(stack);
455 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
456 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
457 req = MC_state_get_executed_request(state->graph_state, &value);
459 req_str = MC_request_to_string(req, value);
460 XBT_INFO("%s", req_str);
467 void MC_show_stack_liveness(xbt_fifo_t stack){
469 mc_pair_stateless_t pair;
470 xbt_fifo_item_t item;
472 char *req_str = NULL;
474 for (item = xbt_fifo_get_last_item(stack);
475 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
476 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
477 req = MC_state_get_executed_request(pair->graph_state, &value);
479 if(pair->requests>0){
480 req_str = MC_request_to_string(req, value);
481 XBT_INFO("%s", req_str);
484 XBT_INFO("End of system requests but evolution in Büchi automaton");
490 void MC_dump_stack_liveness(xbt_fifo_t stack){
491 mc_pair_stateless_t pair;
494 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
495 MC_pair_stateless_delete(pair);
500 void MC_print_statistics(mc_stats_t stats)
502 XBT_INFO("State space size ~= %lu", stats->state_size);
503 XBT_INFO("Expanded states = %lu", stats->expanded_states);
504 XBT_INFO("Visited states = %lu", stats->visited_states);
505 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
506 XBT_INFO("Expanded / Visited = %lf",
507 (double) stats->visited_states / stats->expanded_states);
508 /*XBT_INFO("Exploration coverage = %lf",
509 (double)stats->expanded_states / stats->state_size); */
512 void MC_print_statistics_pairs(mc_stats_pair_t stats)
514 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
515 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
516 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
517 XBT_INFO("Expanded / Visited = %lf",
518 (double) stats->visited_pairs / stats->expanded_pairs);
519 /*XBT_INFO("Exploration coverage = %lf",
520 (double)stats->expanded_states / stats->state_size); */
523 void MC_assert(int prop)
525 if (MC_IS_ENABLED && !prop) {
526 XBT_INFO("**************************");
527 XBT_INFO("*** PROPERTY NOT VALID ***");
528 XBT_INFO("**************************");
529 XBT_INFO("Counter-example execution trace:");
530 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
531 MC_print_statistics(mc_stats);
536 void MC_assert_stateful(int prop)
538 if (MC_IS_ENABLED && !prop) {
539 XBT_INFO("**************************");
540 XBT_INFO("*** PROPERTY NOT VALID ***");
541 XBT_INFO("**************************");
542 XBT_INFO("Counter-example execution trace:");
543 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
544 MC_print_statistics(mc_stats);
550 static void MC_assert_pair(int prop){
551 if (MC_IS_ENABLED && !prop) {
552 XBT_INFO("**************************");
553 XBT_INFO("*** PROPERTY NOT VALID ***");
554 XBT_INFO("**************************");
555 //XBT_INFO("Counter-example execution trace:");
556 MC_show_stack_liveness(mc_stack_liveness);
557 //MC_dump_snapshot_stack(mc_snapshot_stack);
558 MC_print_statistics_pairs(mc_stats_pair);
563 void MC_process_clock_add(smx_process_t process, double amount)
565 mc_time[process->pid] += amount;
568 double MC_process_clock_get(smx_process_t process)
571 return mc_time[process->pid];
578 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
579 MC_take_snapshot_liveness(sn);
583 XBT_INFO("Number of regions : %d", sn->num_reg);
585 for(i=0; i<sn->num_reg; i++){
587 switch(sn->regions[i]->type){
589 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
590 mmalloc_display_info_heap(sn->regions[i]->data);
592 case 1 : /* libsimgrid */
593 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
595 case 2 : /* data program */
596 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
599 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
600 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);