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"
16 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
18 "Logging specific to MC (global)");
20 /* MC global data structures */
22 mc_state_t mc_current_state = NULL;
23 char mc_replay_mode = FALSE;
24 double *mc_time = NULL;
25 mc_snapshot_t initial_snapshot = NULL;
29 xbt_fifo_t mc_stack_safety_stateful = NULL;
30 xbt_fifo_t mc_stack_safety_stateless = NULL;
31 mc_stats_t mc_stats = NULL;
35 mc_stats_pair_t mc_stats_pair = NULL;
36 xbt_fifo_t mc_stack_liveness = NULL;
37 mc_snapshot_t initial_snapshot_liveness = NULL;
39 xbt_automaton_t automaton;
41 static void MC_init_liveness(xbt_automaton_t a);
42 static void MC_assert_pair(int prop);
46 * \brief Initialize the model-checker data structures
48 void MC_init_safety_stateless(void)
51 /* Check if MC is already initialized */
55 mc_time = xbt_new0(double, simix_process_maxpid);
57 /* Initialize the data structures that must be persistent across every
58 iteration of the model-checker (in RAW memory) */
61 /* Initialize statistics */
62 mc_stats = xbt_new0(s_mc_stats_t, 1);
63 mc_stats->state_size = 1;
65 /* Create exploration stack */
66 mc_stack_safety_stateless = xbt_fifo_new();
73 /* Save the initial state */
74 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
75 MC_take_snapshot(initial_snapshot);
79 void MC_init_safety_stateful(void){
82 /* Check if MC is already initialized */
86 mc_time = xbt_new0(double, simix_process_maxpid);
88 /* Initialize the data structures that must be persistent across every
89 iteration of the model-checker (in RAW memory) */
92 /* Initialize statistics */
93 mc_stats = xbt_new0(s_mc_stats_t, 1);
94 mc_stats->state_size = 1;
96 /* Create exploration stack */
97 mc_stack_safety_stateful = xbt_fifo_new();
101 MC_dpor_stateful_init();
106 static void MC_init_liveness(xbt_automaton_t a){
108 XBT_DEBUG("Start init mc");
110 mc_time = xbt_new0(double, simix_process_maxpid);
112 /* Initialize the data structures that must be persistent across every
113 iteration of the model-checker (in RAW memory) */
117 /* Initialize statistics */
118 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
120 XBT_DEBUG("Creating stack");
122 /* Create exploration stack */
123 mc_stack_liveness = xbt_fifo_new();
135 void MC_modelcheck(void)
137 MC_init_safety_stateless();
142 void MC_modelcheck_stateful(void)
144 MC_init_safety_stateful();
150 void MC_modelcheck_liveness(xbt_automaton_t a){
155 void MC_exit_liveness(void)
157 MC_print_statistics_pairs(mc_stats_pair);
166 MC_print_statistics(mc_stats);
172 int MC_random(int min, int max)
174 /*FIXME: return mc_current_state->executed_transition->random.value;*/
179 * \brief Schedules all the process that are ready to run
181 void MC_wait_for_requests(void)
183 smx_process_t process;
187 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
188 SIMIX_process_runall();
189 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
190 req = &process->simcall;
191 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
192 SIMIX_simcall_pre(req, 0);
197 int MC_deadlock_check()
199 int deadlock = FALSE;
200 smx_process_t process;
201 if(xbt_swag_size(simix_global->process_list)){
203 xbt_swag_foreach(process, simix_global->process_list){
204 if(process->simcall.call != SIMCALL_NONE
205 && MC_request_is_enabled(&process->simcall)){
215 * \brief Re-executes from the initial state all the transitions indicated by
216 * a given model-checker stack.
217 * \param stack The stack with the transitions to execute.
219 void MC_replay(xbt_fifo_t stack)
223 smx_simcall_t req = NULL, saved_req = NULL;
224 xbt_fifo_item_t item;
227 XBT_DEBUG("**** Begin Replay ****");
229 /* Restore the initial state */
230 MC_restore_snapshot(initial_snapshot);
231 /* At the moment of taking the snapshot the raw heap was set, so restoring
232 * it will set it back again, we have to unset it to continue */
235 /* Traverse the stack from the initial state and re-execute the transitions */
236 for (item = xbt_fifo_get_last_item(stack);
237 item != xbt_fifo_get_first_item(stack);
238 item = xbt_fifo_get_prev_item(item)) {
240 state = (mc_state_t) xbt_fifo_get_item_content(item);
241 saved_req = MC_state_get_executed_request(state, &value);
244 /* because we got a copy of the executed request, we have to fetch the
245 real one, pointed by the request field of the issuer process */
246 req = &saved_req->issuer->simcall;
248 /* Debug information */
249 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
250 req_str = MC_request_to_string(req, value);
251 XBT_DEBUG("Replay: %s (%p)", req_str, state);
256 SIMIX_simcall_pre(req, value);
257 MC_wait_for_requests();
259 /* Update statistics */
260 mc_stats->visited_states++;
261 mc_stats->executed_transitions++;
263 XBT_DEBUG("**** End Replay ****");
266 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
270 smx_simcall_t req = NULL, saved_req = NULL;
271 xbt_fifo_item_t item;
273 mc_pair_stateless_t pair;
276 XBT_DEBUG("**** Begin Replay ****");
278 /* Restore the initial state */
279 MC_restore_snapshot(initial_snapshot_liveness);
280 /* At the moment of taking the snapshot the raw heap was set, so restoring
281 * it will set it back again, we have to unset it to continue */
286 item = xbt_fifo_get_last_item(stack);
288 while(depth <= xbt_fifo_size(stack)){
290 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
291 state = (mc_state_t) pair->graph_state;
293 if(pair->requests > 0){
295 saved_req = MC_state_get_executed_request(state, &value);
296 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
298 if(saved_req != NULL){
299 /* because we got a copy of the executed request, we have to fetch the
300 real one, pointed by the request field of the issuer process */
301 req = &saved_req->issuer->simcall;
302 //XBT_DEBUG("Req->call %u", req->call);
304 /* Debug information */
305 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
306 req_str = MC_request_to_string(req, value);
307 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
313 SIMIX_simcall_pre(req, value);
314 MC_wait_for_requests();
319 /* Update statistics */
320 mc_stats_pair->visited_pairs++;
322 item = xbt_fifo_get_prev_item(item);
327 /* Traverse the stack from the initial state and re-execute the transitions */
328 for (item = xbt_fifo_get_last_item(stack);
329 item != xbt_fifo_get_first_item(stack);
330 item = xbt_fifo_get_prev_item(item)) {
332 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
333 state = (mc_state_t) pair->graph_state;
335 if(pair->requests > 0){
337 saved_req = MC_state_get_executed_request(state, &value);
338 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
340 if(saved_req != NULL){
341 /* because we got a copy of the executed request, we have to fetch the
342 real one, pointed by the request field of the issuer process */
343 req = &saved_req->issuer->simcall;
344 //XBT_DEBUG("Req->call %u", req->call);
346 /* Debug information */
347 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
348 req_str = MC_request_to_string(req, value);
349 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
355 SIMIX_simcall_pre(req, value);
356 MC_wait_for_requests();
361 /* Update statistics */
362 mc_stats_pair->visited_pairs++;
366 XBT_DEBUG("**** End Replay ****");
371 * \brief Dumps the contents of a model-checker's stack and shows the actual
373 * \param stack The stack to dump
375 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
379 MC_show_stack_safety_stateless(stack);
382 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
383 MC_state_delete(state);
388 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
392 xbt_fifo_item_t item;
394 char *req_str = NULL;
396 for (item = xbt_fifo_get_last_item(stack);
397 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
398 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
399 req = MC_state_get_executed_request(state, &value);
401 req_str = MC_request_to_string(req, value);
402 XBT_INFO("%s", req_str);
408 void MC_show_deadlock(smx_simcall_t req)
410 /*char *req_str = NULL;*/
411 XBT_INFO("**************************");
412 XBT_INFO("*** DEAD-LOCK DETECTED ***");
413 XBT_INFO("**************************");
414 XBT_INFO("Locked request:");
415 /*req_str = MC_request_to_string(req);
416 XBT_INFO("%s", req_str);
418 XBT_INFO("Counter-example execution trace:");
419 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
422 void MC_show_deadlock_stateful(smx_simcall_t req)
424 /*char *req_str = NULL;*/
425 XBT_INFO("**************************");
426 XBT_INFO("*** DEAD-LOCK DETECTED ***");
427 XBT_INFO("**************************");
428 XBT_INFO("Locked request:");
429 /*req_str = MC_request_to_string(req);
430 XBT_INFO("%s", req_str);
432 XBT_INFO("Counter-example execution trace:");
433 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
436 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
438 //mc_state_ws_t state;
440 MC_show_stack_safety_stateful(stack);
443 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
444 MC_state_delete(state);
449 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
453 xbt_fifo_item_t item;
455 char *req_str = NULL;
457 for (item = xbt_fifo_get_last_item(stack);
458 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
459 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
460 req = MC_state_get_executed_request(state->graph_state, &value);
462 req_str = MC_request_to_string(req, value);
463 XBT_INFO("%s", req_str);
470 void MC_show_stack_liveness(xbt_fifo_t stack){
472 mc_pair_stateless_t pair;
473 xbt_fifo_item_t item;
475 char *req_str = NULL;
477 for (item = xbt_fifo_get_last_item(stack);
478 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
479 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
480 req = MC_state_get_executed_request(pair->graph_state, &value);
482 if(pair->requests>0){
483 req_str = MC_request_to_string(req, value);
484 XBT_INFO("%s", req_str);
487 XBT_INFO("End of system requests but evolution in Büchi automaton");
493 void MC_dump_stack_liveness(xbt_fifo_t stack){
494 mc_pair_stateless_t pair;
497 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
498 MC_pair_stateless_delete(pair);
503 void MC_print_statistics(mc_stats_t stats)
505 //XBT_INFO("State space size ~= %lu", stats->state_size);
506 XBT_INFO("Expanded states = %lu", stats->expanded_states);
507 XBT_INFO("Visited states = %lu", stats->visited_states);
508 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
509 XBT_INFO("Expanded / Visited = %lf",
510 (double) stats->visited_states / stats->expanded_states);
511 /*XBT_INFO("Exploration coverage = %lf",
512 (double)stats->expanded_states / stats->state_size); */
515 void MC_print_statistics_pairs(mc_stats_pair_t stats)
517 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
518 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
519 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
520 XBT_INFO("Expanded / Visited = %lf",
521 (double) stats->visited_pairs / stats->expanded_pairs);
522 /*XBT_INFO("Exploration coverage = %lf",
523 (double)stats->expanded_states / stats->state_size); */
526 void MC_assert(int 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);
538 MC_print_statistics(mc_stats);
544 void MC_assert_stateful(int prop)
546 if (MC_IS_ENABLED && !prop) {
547 XBT_INFO("**************************");
548 XBT_INFO("*** PROPERTY NOT VALID ***");
549 XBT_INFO("**************************");
550 XBT_INFO("Counter-example execution trace:");
551 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
552 MC_print_statistics(mc_stats);
558 static void MC_assert_pair(int prop){
559 if (MC_IS_ENABLED && !prop) {
560 XBT_INFO("**************************");
561 XBT_INFO("*** PROPERTY NOT VALID ***");
562 XBT_INFO("**************************");
563 //XBT_INFO("Counter-example execution trace:");
564 MC_show_stack_liveness(mc_stack_liveness);
565 //MC_dump_snapshot_stack(mc_snapshot_stack);
566 MC_print_statistics_pairs(mc_stats_pair);
571 void MC_process_clock_add(smx_process_t process, double amount)
573 mc_time[process->pid] += amount;
576 double MC_process_clock_get(smx_process_t process)
579 return mc_time[process->pid];
586 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
587 MC_take_snapshot_liveness(sn);
591 XBT_INFO("Number of regions : %u", sn->num_reg);
593 for(i=0; i<sn->num_reg; i++){
595 switch(sn->regions[i]->type){
597 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
598 mmalloc_display_info_heap(sn->regions[i]->data);
600 case 1 : /* libsimgrid */
601 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
603 case 2 : /* data program */
604 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
607 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
608 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);