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.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 /* Configuration support */
22 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
24 extern int _surf_init_status;
25 void _mc_cfg_cb_reduce(const char *name, int pos) {
26 if (_surf_init_status && !_surf_do_model_check) {
27 xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
29 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
30 if (!strcasecmp(val,"none")) {
31 mc_reduce_kind = e_mc_reduce_none;
32 } else if (!strcasecmp(val,"dpor")) {
33 mc_reduce_kind = e_mc_reduce_dpor;
35 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
37 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
40 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
41 if (_surf_init_status && !_surf_do_model_check) {
42 xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
44 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
45 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
47 void _mc_cfg_cb_property(const char *name, int pos) {
48 if (_surf_init_status && !_surf_do_model_check) {
49 xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
51 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
52 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
56 /* MC global data structures */
58 mc_state_t mc_current_state = NULL;
59 char mc_replay_mode = FALSE;
60 double *mc_time = NULL;
61 mc_snapshot_t initial_snapshot = NULL;
66 xbt_fifo_t mc_stack_safety = NULL;
67 mc_stats_t mc_stats = NULL;
71 mc_stats_pair_t mc_stats_pair = NULL;
72 xbt_fifo_t mc_stack_liveness = NULL;
73 mc_snapshot_t initial_snapshot_liveness = NULL;
75 xbt_automaton_t _mc_property_automaton = NULL;
77 static void MC_assert_pair(int prop);
79 void MC_do_the_modelcheck_for_real() {
80 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
81 if (mc_reduce_kind==e_mc_reduce_unset)
82 mc_reduce_kind=e_mc_reduce_dpor;
84 XBT_INFO("Check a safety property");
89 if (mc_reduce_kind==e_mc_reduce_unset)
90 mc_reduce_kind=e_mc_reduce_none;
92 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
93 MC_automaton_load(_surf_mc_property_file);
94 MC_modelcheck_liveness();
99 * \brief Initialize the model-checker data structures
101 void MC_init_safety(void)
104 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
106 /* Check if MC is already initialized */
107 if (initial_snapshot)
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 = xbt_new0(s_mc_stats_t, 1);
119 mc_stats->state_size = 1;
121 /* Create exploration stack */
122 mc_stack_safety = xbt_fifo_new();
129 /* Save the initial state */
130 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
131 MC_take_snapshot(initial_snapshot);
143 void MC_modelcheck(void)
150 void MC_modelcheck_liveness(){
152 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
155 XBT_DEBUG("Start init mc");
157 mc_time = xbt_new0(double, simix_process_maxpid);
159 /* Initialize the data structures that must be persistent across every
160 iteration of the model-checker (in RAW memory) */
164 /* Initialize statistics */
165 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
167 XBT_DEBUG("Creating stack");
169 /* Create exploration stack */
170 mc_stack_liveness = xbt_fifo_new();
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->simcall;
212 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
213 SIMIX_simcall_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->simcall.call != SIMCALL_NONE
226 && MC_request_is_enabled(&process->simcall)){
236 * \brief Re-executes from the state at position start all the transitions indicated by
237 * a given model-checker stack.
238 * \param stack The stack with the transitions to execute.
239 * \param start Start index to begin the re-execution.
241 void MC_replay(xbt_fifo_t stack, int start)
243 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
247 smx_simcall_t req = NULL, saved_req = NULL;
248 xbt_fifo_item_t item, start_item;
251 XBT_DEBUG("**** Begin Replay ****");
254 /* Restore the initial state */
255 MC_restore_snapshot(initial_snapshot);
256 /* At the moment of taking the snapshot the raw heap was set, so restoring
257 * it will set it back again, we have to unset it to continue */
261 start_item = xbt_fifo_get_last_item(stack);
264 start_item = xbt_fifo_get_prev_item(start_item);
269 /* Traverse the stack from the state at position start and re-execute the transitions */
270 for (item = start_item;
271 item != xbt_fifo_get_first_item(stack);
272 item = xbt_fifo_get_prev_item(item)) {
274 state = (mc_state_t) xbt_fifo_get_item_content(item);
275 saved_req = MC_state_get_executed_request(state, &value);
278 /* because we got a copy of the executed request, we have to fetch the
279 real one, pointed by the request field of the issuer process */
280 req = &saved_req->issuer->simcall;
282 /* Debug information */
283 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
284 req_str = MC_request_to_string(req, value);
285 XBT_DEBUG("Replay: %s (%p)", req_str, state);
290 SIMIX_simcall_pre(req, value);
291 MC_wait_for_requests();
293 /* Update statistics */
294 mc_stats->visited_states++;
295 mc_stats->executed_transitions++;
297 XBT_DEBUG("**** End Replay ****");
307 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
310 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
314 smx_simcall_t req = NULL, saved_req = NULL;
315 xbt_fifo_item_t item;
317 mc_pair_stateless_t pair;
320 XBT_DEBUG("**** Begin Replay ****");
322 /* Restore the initial state */
323 MC_restore_snapshot(initial_snapshot_liveness);
324 /* At the moment of taking the snapshot the raw heap was set, so restoring
325 * it will set it back again, we have to unset it to continue */
330 item = xbt_fifo_get_last_item(stack);
332 while(depth <= xbt_fifo_size(stack)){
334 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
335 state = (mc_state_t) pair->graph_state;
337 if(pair->requests > 0){
339 saved_req = MC_state_get_executed_request(state, &value);
340 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
342 if(saved_req != NULL){
343 /* because we got a copy of the executed request, we have to fetch the
344 real one, pointed by the request field of the issuer process */
345 req = &saved_req->issuer->simcall;
346 //XBT_DEBUG("Req->call %u", req->call);
348 /* Debug information */
349 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
350 req_str = MC_request_to_string(req, value);
351 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
357 SIMIX_simcall_pre(req, value);
358 MC_wait_for_requests();
363 /* Update statistics */
364 mc_stats_pair->visited_pairs++;
366 item = xbt_fifo_get_prev_item(item);
371 /* Traverse the stack from the initial state and re-execute the transitions */
372 for (item = xbt_fifo_get_last_item(stack);
373 item != xbt_fifo_get_first_item(stack);
374 item = xbt_fifo_get_prev_item(item)) {
376 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
377 state = (mc_state_t) pair->graph_state;
379 if(pair->requests > 0){
381 saved_req = MC_state_get_executed_request(state, &value);
382 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
384 if(saved_req != NULL){
385 /* because we got a copy of the executed request, we have to fetch the
386 real one, pointed by the request field of the issuer process */
387 req = &saved_req->issuer->simcall;
388 //XBT_DEBUG("Req->call %u", req->call);
390 /* Debug information */
391 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
392 req_str = MC_request_to_string(req, value);
393 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
399 SIMIX_simcall_pre(req, value);
400 MC_wait_for_requests();
405 /* Update statistics */
406 mc_stats_pair->visited_pairs++;
410 XBT_DEBUG("**** End Replay ****");
420 * \brief Dumps the contents of a model-checker's stack and shows the actual
422 * \param stack The stack to dump
424 void MC_dump_stack_safety(xbt_fifo_t stack)
427 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
429 MC_show_stack_safety(stack);
431 if(!_surf_mc_checkpoint){
436 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
437 MC_state_delete(state);
450 void MC_show_stack_safety(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_t) (xbt_fifo_get_item_content(item)))
460 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
461 req = MC_state_get_executed_request(state, &value);
463 req_str = MC_request_to_string(req, value);
464 XBT_INFO("%s", req_str);
470 void MC_show_deadlock(smx_simcall_t req)
472 /*char *req_str = NULL;*/
473 XBT_INFO("**************************");
474 XBT_INFO("*** DEAD-LOCK DETECTED ***");
475 XBT_INFO("**************************");
476 XBT_INFO("Locked request:");
477 /*req_str = MC_request_to_string(req);
478 XBT_INFO("%s", req_str);
480 XBT_INFO("Counter-example execution trace:");
481 MC_dump_stack_safety(mc_stack_safety);
485 void MC_show_stack_liveness(xbt_fifo_t stack){
487 mc_pair_stateless_t pair;
488 xbt_fifo_item_t item;
490 char *req_str = NULL;
492 for (item = xbt_fifo_get_last_item(stack);
493 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
494 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
495 req = MC_state_get_executed_request(pair->graph_state, &value);
497 if(pair->requests>0){
498 req_str = MC_request_to_string(req, value);
499 XBT_INFO("%s", req_str);
502 XBT_INFO("End of system requests but evolution in Büchi automaton");
508 void MC_dump_stack_liveness(xbt_fifo_t stack){
510 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
512 mc_pair_stateless_t pair;
515 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
516 MC_pair_stateless_delete(pair);
527 void MC_print_statistics(mc_stats_t stats)
529 //XBT_INFO("State space size ~= %lu", stats->state_size);
530 XBT_INFO("Expanded states = %lu", stats->expanded_states);
531 XBT_INFO("Visited states = %lu", stats->visited_states);
532 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
533 XBT_INFO("Expanded / Visited = %lf",
534 (double) stats->visited_states / stats->expanded_states);
535 /*XBT_INFO("Exploration coverage = %lf",
536 (double)stats->expanded_states / stats->state_size); */
539 void MC_print_statistics_pairs(mc_stats_pair_t stats)
541 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
542 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
543 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
544 XBT_INFO("Expanded / Visited = %lf",
545 (double) stats->visited_pairs / stats->expanded_pairs);
546 /*XBT_INFO("Exploration coverage = %lf",
547 (double)stats->expanded_states / stats->state_size); */
550 void MC_assert(int prop)
552 if (MC_IS_ENABLED && !prop){
553 XBT_INFO("**************************");
554 XBT_INFO("*** PROPERTY NOT VALID ***");
555 XBT_INFO("**************************");
556 XBT_INFO("Counter-example execution trace:");
557 MC_dump_stack_safety(mc_stack_safety);
558 MC_print_statistics(mc_stats);
563 static void MC_assert_pair(int prop){
564 if (MC_IS_ENABLED && !prop) {
565 XBT_INFO("**************************");
566 XBT_INFO("*** PROPERTY NOT VALID ***");
567 XBT_INFO("**************************");
568 //XBT_INFO("Counter-example execution trace:");
569 MC_show_stack_liveness(mc_stack_liveness);
570 //MC_dump_snapshot_stack(mc_snapshot_stack);
571 MC_print_statistics_pairs(mc_stats_pair);
576 void MC_process_clock_add(smx_process_t process, double amount)
578 mc_time[process->pid] += amount;
581 double MC_process_clock_get(smx_process_t process)
584 return mc_time[process->pid];
591 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
592 MC_take_snapshot_liveness(sn);
596 XBT_INFO("Number of regions : %u", sn->num_reg);
598 for(i=0; i<sn->num_reg; i++){
600 switch(sn->regions[i]->type){
602 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
604 case 1 : /* libsimgrid */
605 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
607 case 2 : /* data program */
608 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
611 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
612 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
620 void MC_automaton_load(const char *file){
622 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
626 if (_mc_property_automaton == NULL)
627 _mc_property_automaton = xbt_automaton_new();
629 xbt_automaton_load(_mc_property_automaton,file);
640 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
642 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
646 if (_mc_property_automaton == NULL)
647 _mc_property_automaton = xbt_automaton_new();
649 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);