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;
76 xbt_automaton_t _mc_property_automaton = NULL;
78 static void MC_assert_pair(int prop);
80 void MC_do_the_modelcheck_for_real() {
81 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
82 if (mc_reduce_kind==e_mc_reduce_unset)
83 mc_reduce_kind=e_mc_reduce_dpor;
85 XBT_INFO("Check a safety property");
90 if (mc_reduce_kind==e_mc_reduce_unset)
91 mc_reduce_kind=e_mc_reduce_none;
93 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
94 MC_automaton_load(_surf_mc_property_file);
95 MC_modelcheck_liveness();
100 * \brief Initialize the model-checker data structures
102 void MC_init_safety(void)
105 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
107 /* Check if MC is already initialized */
108 if (initial_snapshot)
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 = xbt_new0(s_mc_stats_t, 1);
120 mc_stats->state_size = 1;
122 /* Create exploration stack */
123 mc_stack_safety = xbt_fifo_new();
130 /* Save the initial state */
131 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
132 MC_take_snapshot(initial_snapshot);
143 void MC_compare(void){
148 void MC_modelcheck(void)
155 void MC_modelcheck_liveness(){
157 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
160 XBT_DEBUG("Start init mc");
162 mc_time = xbt_new0(double, simix_process_maxpid);
166 /* Initialize the data structures that must be persistent across every
167 iteration of the model-checker (in RAW memory) */
171 /* Initialize statistics */
172 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
174 XBT_DEBUG("Creating stack");
176 /* Create exploration stack */
177 mc_stack_liveness = xbt_fifo_new();
185 MC_print_statistics_pairs(mc_stats_pair);
194 MC_print_statistics(mc_stats);
200 int MC_random(int min, int max)
202 /*FIXME: return mc_current_state->executed_transition->random.value;*/
207 * \brief Schedules all the process that are ready to run
209 void MC_wait_for_requests(void)
211 smx_process_t process;
215 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
216 SIMIX_process_runall();
217 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
218 req = &process->simcall;
219 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
220 SIMIX_simcall_pre(req, 0);
225 int MC_deadlock_check()
227 int deadlock = FALSE;
228 smx_process_t process;
229 if(xbt_swag_size(simix_global->process_list)){
231 xbt_swag_foreach(process, simix_global->process_list){
232 if(process->simcall.call != SIMCALL_NONE
233 && MC_request_is_enabled(&process->simcall)){
243 * \brief Re-executes from the state at position start all the transitions indicated by
244 * a given model-checker stack.
245 * \param stack The stack with the transitions to execute.
246 * \param start Start index to begin the re-execution.
248 void MC_replay(xbt_fifo_t stack, int start)
250 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
254 smx_simcall_t req = NULL, saved_req = NULL;
255 xbt_fifo_item_t item, start_item;
258 XBT_DEBUG("**** Begin Replay ****");
261 /* Restore the initial state */
262 MC_restore_snapshot(initial_snapshot);
263 /* At the moment of taking the snapshot the raw heap was set, so restoring
264 * it will set it back again, we have to unset it to continue */
268 start_item = xbt_fifo_get_last_item(stack);
271 start_item = xbt_fifo_get_prev_item(start_item);
276 /* Traverse the stack from the state at position start and re-execute the transitions */
277 for (item = start_item;
278 item != xbt_fifo_get_first_item(stack);
279 item = xbt_fifo_get_prev_item(item)) {
281 state = (mc_state_t) xbt_fifo_get_item_content(item);
282 saved_req = MC_state_get_executed_request(state, &value);
285 /* because we got a copy of the executed request, we have to fetch the
286 real one, pointed by the request field of the issuer process */
287 req = &saved_req->issuer->simcall;
289 /* Debug information */
290 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
291 req_str = MC_request_to_string(req, value);
292 XBT_DEBUG("Replay: %s (%p)", req_str, state);
297 SIMIX_simcall_pre(req, value);
298 MC_wait_for_requests();
300 /* Update statistics */
301 mc_stats->visited_states++;
302 mc_stats->executed_transitions++;
304 XBT_DEBUG("**** End Replay ****");
314 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
317 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
321 smx_simcall_t req = NULL, saved_req = NULL;
322 xbt_fifo_item_t item;
324 mc_pair_stateless_t pair;
327 XBT_DEBUG("**** Begin Replay ****");
329 /* Restore the initial state */
330 MC_restore_snapshot(initial_snapshot_liveness);
331 /* At the moment of taking the snapshot the raw heap was set, so restoring
332 * it will set it back again, we have to unset it to continue */
337 item = xbt_fifo_get_last_item(stack);
339 while(depth <= xbt_fifo_size(stack)){
341 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
342 state = (mc_state_t) pair->graph_state;
344 if(pair->requests > 0){
346 saved_req = MC_state_get_executed_request(state, &value);
347 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
349 if(saved_req != NULL){
350 /* because we got a copy of the executed request, we have to fetch the
351 real one, pointed by the request field of the issuer process */
352 req = &saved_req->issuer->simcall;
353 //XBT_DEBUG("Req->call %u", req->call);
355 /* Debug information */
356 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
357 req_str = MC_request_to_string(req, value);
358 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
364 SIMIX_simcall_pre(req, value);
365 MC_wait_for_requests();
370 /* Update statistics */
371 mc_stats_pair->visited_pairs++;
373 item = xbt_fifo_get_prev_item(item);
378 /* Traverse the stack from the initial state and re-execute the transitions */
379 for (item = xbt_fifo_get_last_item(stack);
380 item != xbt_fifo_get_first_item(stack);
381 item = xbt_fifo_get_prev_item(item)) {
383 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
384 state = (mc_state_t) pair->graph_state;
386 if(pair->requests > 0){
388 saved_req = MC_state_get_executed_request(state, &value);
389 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
391 if(saved_req != NULL){
392 /* because we got a copy of the executed request, we have to fetch the
393 real one, pointed by the request field of the issuer process */
394 req = &saved_req->issuer->simcall;
395 //XBT_DEBUG("Req->call %u", req->call);
397 /* Debug information */
398 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
399 req_str = MC_request_to_string(req, value);
400 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
406 SIMIX_simcall_pre(req, value);
407 MC_wait_for_requests();
412 /* Update statistics */
413 mc_stats_pair->visited_pairs++;
417 XBT_DEBUG("**** End Replay ****");
427 * \brief Dumps the contents of a model-checker's stack and shows the actual
429 * \param stack The stack to dump
431 void MC_dump_stack_safety(xbt_fifo_t stack)
434 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
436 MC_show_stack_safety(stack);
438 if(!_surf_mc_checkpoint){
443 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
444 MC_state_delete(state);
457 void MC_show_stack_safety(xbt_fifo_t stack)
461 xbt_fifo_item_t item;
463 char *req_str = NULL;
465 for (item = xbt_fifo_get_last_item(stack);
466 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
467 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
468 req = MC_state_get_executed_request(state, &value);
470 req_str = MC_request_to_string(req, value);
471 XBT_INFO("%s", req_str);
477 void MC_show_deadlock(smx_simcall_t req)
479 /*char *req_str = NULL;*/
480 XBT_INFO("**************************");
481 XBT_INFO("*** DEAD-LOCK DETECTED ***");
482 XBT_INFO("**************************");
483 XBT_INFO("Locked request:");
484 /*req_str = MC_request_to_string(req);
485 XBT_INFO("%s", req_str);
487 XBT_INFO("Counter-example execution trace:");
488 MC_dump_stack_safety(mc_stack_safety);
492 void MC_show_stack_liveness(xbt_fifo_t stack){
494 mc_pair_stateless_t pair;
495 xbt_fifo_item_t item;
497 char *req_str = NULL;
499 for (item = xbt_fifo_get_last_item(stack);
500 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
501 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
502 req = MC_state_get_executed_request(pair->graph_state, &value);
504 if(pair->requests>0){
505 req_str = MC_request_to_string(req, value);
506 XBT_INFO("%s", req_str);
509 XBT_INFO("End of system requests but evolution in Büchi automaton");
515 void MC_dump_stack_liveness(xbt_fifo_t stack){
517 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
519 mc_pair_stateless_t pair;
522 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
523 MC_pair_stateless_delete(pair);
534 void MC_print_statistics(mc_stats_t stats)
536 //XBT_INFO("State space size ~= %lu", stats->state_size);
537 XBT_INFO("Expanded states = %lu", stats->expanded_states);
538 XBT_INFO("Visited states = %lu", stats->visited_states);
539 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
540 XBT_INFO("Expanded / Visited = %lf",
541 (double) stats->visited_states / stats->expanded_states);
542 /*XBT_INFO("Exploration coverage = %lf",
543 (double)stats->expanded_states / stats->state_size); */
546 void MC_print_statistics_pairs(mc_stats_pair_t stats)
548 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
549 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
550 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
551 XBT_INFO("Expanded / Visited = %lf",
552 (double) stats->visited_pairs / stats->expanded_pairs);
553 /*XBT_INFO("Exploration coverage = %lf",
554 (double)stats->expanded_states / stats->state_size); */
557 void MC_assert(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_dump_stack_safety(mc_stack_safety);
565 MC_print_statistics(mc_stats);
570 static void MC_assert_pair(int prop){
571 if (MC_IS_ENABLED && !prop) {
572 XBT_INFO("**************************");
573 XBT_INFO("*** PROPERTY NOT VALID ***");
574 XBT_INFO("**************************");
575 //XBT_INFO("Counter-example execution trace:");
576 MC_show_stack_liveness(mc_stack_liveness);
577 //MC_dump_snapshot_stack(mc_snapshot_stack);
578 MC_print_statistics_pairs(mc_stats_pair);
583 void MC_process_clock_add(smx_process_t process, double amount)
585 mc_time[process->pid] += amount;
588 double MC_process_clock_get(smx_process_t process)
591 return mc_time[process->pid];
596 void MC_automaton_load(const char *file){
598 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
602 if (_mc_property_automaton == NULL)
603 _mc_property_automaton = xbt_automaton_new();
605 xbt_automaton_load(_mc_property_automaton,file);
616 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
618 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
622 if (_mc_property_automaton == NULL)
623 _mc_property_automaton = xbt_automaton_new();
625 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);