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"
13 #include "../xbt/mmalloc/mmprivate.h"
15 #include "mc_private.h"
16 #include "xbt/automaton.h"
18 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
20 "Logging specific to MC (global)");
22 /* Configuration support */
23 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
25 extern int _surf_init_status;
26 void _mc_cfg_cb_reduce(const char *name, int pos) {
27 if (_surf_init_status && !_surf_do_model_check) {
28 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.");
30 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
31 if (!strcasecmp(val,"none")) {
32 mc_reduce_kind = e_mc_reduce_none;
33 } else if (!strcasecmp(val,"dpor")) {
34 mc_reduce_kind = e_mc_reduce_dpor;
36 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
38 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
41 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
42 if (_surf_init_status && !_surf_do_model_check) {
43 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.");
45 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
46 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
48 void _mc_cfg_cb_property(const char *name, int pos) {
49 if (_surf_init_status && !_surf_do_model_check) {
50 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.");
52 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
53 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
57 /* MC global data structures */
59 mc_state_t mc_current_state = NULL;
60 char mc_replay_mode = FALSE;
61 double *mc_time = NULL;
62 mc_snapshot_t initial_snapshot = NULL;
67 xbt_fifo_t mc_stack_safety = NULL;
68 mc_stats_t mc_stats = NULL;
72 mc_stats_pair_t mc_stats_pair = NULL;
73 xbt_fifo_t mc_stack_liveness = NULL;
74 mc_snapshot_t initial_snapshot_liveness = NULL;
76 extern xbt_dynar_t mmalloc_ignore;
78 xbt_automaton_t _mc_property_automaton = NULL;
80 static void MC_assert_pair(int prop);
82 void MC_do_the_modelcheck_for_real() {
83 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
84 if (mc_reduce_kind==e_mc_reduce_unset)
85 mc_reduce_kind=e_mc_reduce_dpor;
87 XBT_INFO("Check a safety property");
92 if (mc_reduce_kind==e_mc_reduce_unset)
93 mc_reduce_kind=e_mc_reduce_none;
95 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
96 MC_automaton_load(_surf_mc_property_file);
97 MC_modelcheck_liveness();
102 * \brief Initialize the model-checker data structures
104 void MC_init_safety(void)
107 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
109 /* Check if MC is already initialized */
110 if (initial_snapshot)
113 mc_time = xbt_new0(double, simix_process_maxpid);
115 /* Initialize the data structures that must be persistent across every
116 iteration of the model-checker (in RAW memory) */
120 /* Initialize statistics */
121 mc_stats = xbt_new0(s_mc_stats_t, 1);
122 mc_stats->state_size = 1;
124 /* Create exploration stack */
125 mc_stack_safety = xbt_fifo_new();
132 /* Save the initial state */
133 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
134 MC_take_snapshot(initial_snapshot);
145 void MC_compare(void){
150 void MC_modelcheck(void)
157 void MC_modelcheck_liveness(){
159 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
162 XBT_DEBUG("Start init mc");
164 mc_time = xbt_new0(double, simix_process_maxpid);
166 /* mc_time refers to clock for each process -> ignore it for heap comparison */
168 for(i = 0; i<simix_process_maxpid; i++)
169 MC_ignore(&(mc_time[i]), sizeof(double));
173 /* Initialize the data structures that must be persistent across every
174 iteration of the model-checker (in RAW memory) */
178 /* Initialize statistics */
179 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
181 XBT_DEBUG("Creating stack");
183 /* Create exploration stack */
184 mc_stack_liveness = xbt_fifo_new();
192 MC_print_statistics_pairs(mc_stats_pair);
201 MC_print_statistics(mc_stats);
207 int MC_random(int min, int max)
209 /*FIXME: return mc_current_state->executed_transition->random.value;*/
214 * \brief Schedules all the process that are ready to run
216 void MC_wait_for_requests(void)
218 smx_process_t process;
222 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
223 SIMIX_process_runall();
224 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
225 req = &process->simcall;
226 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
227 SIMIX_simcall_pre(req, 0);
232 int MC_deadlock_check()
234 int deadlock = FALSE;
235 smx_process_t process;
236 if(xbt_swag_size(simix_global->process_list)){
238 xbt_swag_foreach(process, simix_global->process_list){
239 if(process->simcall.call != SIMCALL_NONE
240 && MC_request_is_enabled(&process->simcall)){
250 * \brief Re-executes from the state at position start all the transitions indicated by
251 * a given model-checker stack.
252 * \param stack The stack with the transitions to execute.
253 * \param start Start index to begin the re-execution.
255 void MC_replay(xbt_fifo_t stack, int start)
257 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
261 smx_simcall_t req = NULL, saved_req = NULL;
262 xbt_fifo_item_t item, start_item;
265 XBT_DEBUG("**** Begin Replay ****");
268 /* Restore the initial state */
269 MC_restore_snapshot(initial_snapshot);
270 /* At the moment of taking the snapshot the raw heap was set, so restoring
271 * it will set it back again, we have to unset it to continue */
275 start_item = xbt_fifo_get_last_item(stack);
278 start_item = xbt_fifo_get_prev_item(start_item);
283 /* Traverse the stack from the state at position start and re-execute the transitions */
284 for (item = start_item;
285 item != xbt_fifo_get_first_item(stack);
286 item = xbt_fifo_get_prev_item(item)) {
288 state = (mc_state_t) xbt_fifo_get_item_content(item);
289 saved_req = MC_state_get_executed_request(state, &value);
292 /* because we got a copy of the executed request, we have to fetch the
293 real one, pointed by the request field of the issuer process */
294 req = &saved_req->issuer->simcall;
296 /* Debug information */
297 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
298 req_str = MC_request_to_string(req, value);
299 XBT_DEBUG("Replay: %s (%p)", req_str, state);
304 SIMIX_simcall_pre(req, value);
305 MC_wait_for_requests();
307 /* Update statistics */
308 mc_stats->visited_states++;
309 mc_stats->executed_transitions++;
311 XBT_DEBUG("**** End Replay ****");
321 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
324 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
328 smx_simcall_t req = NULL, saved_req = NULL;
329 xbt_fifo_item_t item;
331 mc_pair_stateless_t pair;
334 XBT_DEBUG("**** Begin Replay ****");
336 /* Restore the initial state */
337 MC_restore_snapshot(initial_snapshot_liveness);
338 /* At the moment of taking the snapshot the raw heap was set, so restoring
339 * it will set it back again, we have to unset it to continue */
344 item = xbt_fifo_get_last_item(stack);
346 while(depth <= xbt_fifo_size(stack)){
348 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
349 state = (mc_state_t) pair->graph_state;
351 if(pair->requests > 0){
353 saved_req = MC_state_get_executed_request(state, &value);
354 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
356 if(saved_req != NULL){
357 /* because we got a copy of the executed request, we have to fetch the
358 real one, pointed by the request field of the issuer process */
359 req = &saved_req->issuer->simcall;
360 //XBT_DEBUG("Req->call %u", req->call);
362 /* Debug information */
363 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
364 req_str = MC_request_to_string(req, value);
365 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
371 SIMIX_simcall_pre(req, value);
372 MC_wait_for_requests();
377 /* Update statistics */
378 mc_stats_pair->visited_pairs++;
380 item = xbt_fifo_get_prev_item(item);
385 /* Traverse the stack from the initial state and re-execute the transitions */
386 for (item = xbt_fifo_get_last_item(stack);
387 item != xbt_fifo_get_first_item(stack);
388 item = xbt_fifo_get_prev_item(item)) {
390 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
391 state = (mc_state_t) pair->graph_state;
393 if(pair->requests > 0){
395 saved_req = MC_state_get_executed_request(state, &value);
396 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
398 if(saved_req != NULL){
399 /* because we got a copy of the executed request, we have to fetch the
400 real one, pointed by the request field of the issuer process */
401 req = &saved_req->issuer->simcall;
402 //XBT_DEBUG("Req->call %u", req->call);
404 /* Debug information */
405 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
406 req_str = MC_request_to_string(req, value);
407 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
413 SIMIX_simcall_pre(req, value);
414 MC_wait_for_requests();
419 /* Update statistics */
420 mc_stats_pair->visited_pairs++;
424 XBT_DEBUG("**** End Replay ****");
434 * \brief Dumps the contents of a model-checker's stack and shows the actual
436 * \param stack The stack to dump
438 void MC_dump_stack_safety(xbt_fifo_t stack)
441 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
443 MC_show_stack_safety(stack);
445 if(!_surf_mc_checkpoint){
450 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
451 MC_state_delete(state);
464 void MC_show_stack_safety(xbt_fifo_t stack)
468 xbt_fifo_item_t item;
470 char *req_str = NULL;
472 for (item = xbt_fifo_get_last_item(stack);
473 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
474 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
475 req = MC_state_get_executed_request(state, &value);
477 req_str = MC_request_to_string(req, value);
478 XBT_INFO("%s", req_str);
484 void MC_show_deadlock(smx_simcall_t req)
486 /*char *req_str = NULL;*/
487 XBT_INFO("**************************");
488 XBT_INFO("*** DEAD-LOCK DETECTED ***");
489 XBT_INFO("**************************");
490 XBT_INFO("Locked request:");
491 /*req_str = MC_request_to_string(req);
492 XBT_INFO("%s", req_str);
494 XBT_INFO("Counter-example execution trace:");
495 MC_dump_stack_safety(mc_stack_safety);
499 void MC_show_stack_liveness(xbt_fifo_t stack){
501 mc_pair_stateless_t pair;
502 xbt_fifo_item_t item;
504 char *req_str = NULL;
506 for (item = xbt_fifo_get_last_item(stack);
507 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
508 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
509 req = MC_state_get_executed_request(pair->graph_state, &value);
511 if(pair->requests>0){
512 req_str = MC_request_to_string(req, value);
513 XBT_INFO("%s", req_str);
516 XBT_INFO("End of system requests but evolution in Büchi automaton");
522 void MC_dump_stack_liveness(xbt_fifo_t stack){
524 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
526 mc_pair_stateless_t pair;
529 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
530 MC_pair_stateless_delete(pair);
541 void MC_print_statistics(mc_stats_t stats)
543 //XBT_INFO("State space size ~= %lu", stats->state_size);
544 XBT_INFO("Expanded states = %lu", stats->expanded_states);
545 XBT_INFO("Visited states = %lu", stats->visited_states);
546 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
547 XBT_INFO("Expanded / Visited = %lf",
548 (double) stats->visited_states / stats->expanded_states);
549 /*XBT_INFO("Exploration coverage = %lf",
550 (double)stats->expanded_states / stats->state_size); */
553 void MC_print_statistics_pairs(mc_stats_pair_t stats)
555 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
556 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
557 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
558 XBT_INFO("Expanded / Visited = %lf",
559 (double) stats->visited_pairs / stats->expanded_pairs);
560 /*XBT_INFO("Exploration coverage = %lf",
561 (double)stats->expanded_states / stats->state_size); */
564 void MC_assert(int prop)
566 if (MC_IS_ENABLED && !prop){
567 XBT_INFO("**************************");
568 XBT_INFO("*** PROPERTY NOT VALID ***");
569 XBT_INFO("**************************");
570 XBT_INFO("Counter-example execution trace:");
571 MC_dump_stack_safety(mc_stack_safety);
572 MC_print_statistics(mc_stats);
577 static void MC_assert_pair(int prop){
578 if (MC_IS_ENABLED && !prop) {
579 XBT_INFO("**************************");
580 XBT_INFO("*** PROPERTY NOT VALID ***");
581 XBT_INFO("**************************");
582 //XBT_INFO("Counter-example execution trace:");
583 MC_show_stack_liveness(mc_stack_liveness);
584 //MC_dump_snapshot_stack(mc_snapshot_stack);
585 MC_print_statistics_pairs(mc_stats_pair);
590 void MC_process_clock_add(smx_process_t process, double amount)
592 mc_time[process->pid] += amount;
595 double MC_process_clock_get(smx_process_t process)
598 return mc_time[process->pid];
603 void MC_automaton_load(const char *file){
605 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
609 if (_mc_property_automaton == NULL)
610 _mc_property_automaton = xbt_automaton_new();
612 xbt_automaton_load(_mc_property_automaton,file);
623 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
625 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
629 if (_mc_property_automaton == NULL)
630 _mc_property_automaton = xbt_automaton_new();
632 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
643 void MC_ignore_init(){
645 mmalloc_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
649 void MC_ignore(void *address, size_t size){
653 mc_ignore_region_t region = NULL;
654 region = xbt_new0(s_mc_ignore_region_t, 1);
655 region->address = address;
657 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
659 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
660 region->fragment = -1;
662 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
665 unsigned int cursor = 0;
666 mc_ignore_region_t current_region;
667 xbt_dynar_foreach(mmalloc_ignore, cursor, current_region){
668 if(current_region->address > address)
672 xbt_dynar_insert_at(mmalloc_ignore, cursor, ®ion);