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 void _mc_cfg_cb_reduce(const char *name, int pos) {
25 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
26 if (!strcasecmp(val,"none")) {
27 mc_reduce_kind = e_mc_reduce_none;
28 } else if (!strcasecmp(val,"dpor")) {
29 mc_reduce_kind = e_mc_reduce_dpor;
31 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
33 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
36 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
37 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
38 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
40 void _mc_cfg_cb_property(const char *name, int pos) {
41 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
42 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
46 /* MC global data structures */
48 mc_state_t mc_current_state = NULL;
49 char mc_replay_mode = FALSE;
50 double *mc_time = NULL;
51 mc_snapshot_t initial_snapshot = NULL;
55 xbt_fifo_t mc_stack_safety = NULL;
56 mc_stats_t mc_stats = NULL;
60 mc_stats_pair_t mc_stats_pair = NULL;
61 xbt_fifo_t mc_stack_liveness = NULL;
62 mc_snapshot_t initial_snapshot_liveness = NULL;
64 xbt_automaton_t _mc_property_automaton = NULL;
66 static void MC_assert_pair(int prop);
68 void MC_do_the_modelcheck_for_real() {
69 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
70 if (mc_reduce_kind==e_mc_reduce_unset)
71 mc_reduce_kind=e_mc_reduce_dpor;
73 XBT_INFO("Check a safety property");
78 if (mc_reduce_kind==e_mc_reduce_unset)
79 mc_reduce_kind=e_mc_reduce_none;
81 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
82 MC_automaton_load(_surf_mc_property_file);
83 MC_modelcheck_liveness();
88 * \brief Initialize the model-checker data structures
90 void MC_init_safety(void)
93 /* Check if MC is already initialized */
97 mc_time = xbt_new0(double, simix_process_maxpid);
99 /* Initialize the data structures that must be persistent across every
100 iteration of the model-checker (in RAW memory) */
103 /* Initialize statistics */
104 mc_stats = xbt_new0(s_mc_stats_t, 1);
105 mc_stats->state_size = 1;
107 /* Create exploration stack */
108 mc_stack_safety = xbt_fifo_new();
115 /* Save the initial state */
116 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
117 MC_take_snapshot(initial_snapshot);
123 void MC_modelcheck(void)
130 void MC_modelcheck_liveness(){
132 XBT_DEBUG("Start init mc");
134 mc_time = xbt_new0(double, simix_process_maxpid);
136 /* Initialize the data structures that must be persistent across every
137 iteration of the model-checker (in RAW memory) */
141 /* Initialize statistics */
142 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
144 XBT_DEBUG("Creating stack");
146 /* Create exploration stack */
147 mc_stack_liveness = xbt_fifo_new();
155 MC_print_statistics_pairs(mc_stats_pair);
164 MC_print_statistics(mc_stats);
170 int MC_random(int min, int max)
172 /*FIXME: return mc_current_state->executed_transition->random.value;*/
177 * \brief Schedules all the process that are ready to run
179 void MC_wait_for_requests(void)
181 smx_process_t process;
185 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
186 SIMIX_process_runall();
187 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
188 req = &process->simcall;
189 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
190 SIMIX_simcall_pre(req, 0);
195 int MC_deadlock_check()
197 int deadlock = FALSE;
198 smx_process_t process;
199 if(xbt_swag_size(simix_global->process_list)){
201 xbt_swag_foreach(process, simix_global->process_list){
202 if(process->simcall.call != SIMCALL_NONE
203 && MC_request_is_enabled(&process->simcall)){
213 * \brief Re-executes from the state at position start all the transitions indicated by
214 * a given model-checker stack.
215 * \param stack The stack with the transitions to execute.
216 * \param start Start index to begin the re-execution.
218 void MC_replay(xbt_fifo_t stack, int start)
222 smx_simcall_t req = NULL, saved_req = NULL;
223 xbt_fifo_item_t item, start_item;
226 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 */
236 start_item = xbt_fifo_get_last_item(stack);
239 start_item = xbt_fifo_get_prev_item(start_item);
244 /* Traverse the stack from the state at position start and re-execute the transitions */
245 for (item = start_item;
246 item != xbt_fifo_get_first_item(stack);
247 item = xbt_fifo_get_prev_item(item)) {
249 state = (mc_state_t) xbt_fifo_get_item_content(item);
250 saved_req = MC_state_get_executed_request(state, &value);
253 /* because we got a copy of the executed request, we have to fetch the
254 real one, pointed by the request field of the issuer process */
255 req = &saved_req->issuer->simcall;
257 /* Debug information */
258 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
259 req_str = MC_request_to_string(req, value);
260 XBT_DEBUG("Replay: %s (%p)", req_str, state);
265 SIMIX_simcall_pre(req, value);
266 MC_wait_for_requests();
268 /* Update statistics */
269 mc_stats->visited_states++;
270 mc_stats->executed_transitions++;
272 XBT_DEBUG("**** End Replay ****");
275 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
279 smx_simcall_t req = NULL, saved_req = NULL;
280 xbt_fifo_item_t item;
282 mc_pair_stateless_t pair;
285 XBT_DEBUG("**** Begin Replay ****");
287 /* Restore the initial state */
288 MC_restore_snapshot(initial_snapshot_liveness);
289 /* At the moment of taking the snapshot the raw heap was set, so restoring
290 * it will set it back again, we have to unset it to continue */
295 item = xbt_fifo_get_last_item(stack);
297 while(depth <= xbt_fifo_size(stack)){
299 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
300 state = (mc_state_t) pair->graph_state;
302 if(pair->requests > 0){
304 saved_req = MC_state_get_executed_request(state, &value);
305 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
307 if(saved_req != NULL){
308 /* because we got a copy of the executed request, we have to fetch the
309 real one, pointed by the request field of the issuer process */
310 req = &saved_req->issuer->simcall;
311 //XBT_DEBUG("Req->call %u", req->call);
313 /* Debug information */
314 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
315 req_str = MC_request_to_string(req, value);
316 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
322 SIMIX_simcall_pre(req, value);
323 MC_wait_for_requests();
328 /* Update statistics */
329 mc_stats_pair->visited_pairs++;
331 item = xbt_fifo_get_prev_item(item);
336 /* Traverse the stack from the initial state and re-execute the transitions */
337 for (item = xbt_fifo_get_last_item(stack);
338 item != xbt_fifo_get_first_item(stack);
339 item = xbt_fifo_get_prev_item(item)) {
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++;
375 XBT_DEBUG("**** End Replay ****");
379 * \brief Dumps the contents of a model-checker's stack and shows the actual
381 * \param stack The stack to dump
383 void MC_dump_stack_safety(xbt_fifo_t stack)
386 MC_show_stack_safety(stack);
388 if(!_surf_mc_checkpoint){
393 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
394 MC_state_delete(state);
401 void MC_show_stack_safety(xbt_fifo_t stack)
405 xbt_fifo_item_t item;
407 char *req_str = NULL;
409 for (item = xbt_fifo_get_last_item(stack);
410 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
411 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
412 req = MC_state_get_executed_request(state, &value);
414 req_str = MC_request_to_string(req, value);
415 XBT_INFO("%s", req_str);
421 void MC_show_deadlock(smx_simcall_t req)
423 /*char *req_str = NULL;*/
424 XBT_INFO("**************************");
425 XBT_INFO("*** DEAD-LOCK DETECTED ***");
426 XBT_INFO("**************************");
427 XBT_INFO("Locked request:");
428 /*req_str = MC_request_to_string(req);
429 XBT_INFO("%s", req_str);
431 XBT_INFO("Counter-example execution trace:");
432 MC_dump_stack_safety(mc_stack_safety);
436 void MC_show_stack_liveness(xbt_fifo_t stack){
438 mc_pair_stateless_t pair;
439 xbt_fifo_item_t item;
441 char *req_str = NULL;
443 for (item = xbt_fifo_get_last_item(stack);
444 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
445 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
446 req = MC_state_get_executed_request(pair->graph_state, &value);
448 if(pair->requests>0){
449 req_str = MC_request_to_string(req, value);
450 XBT_INFO("%s", req_str);
453 XBT_INFO("End of system requests but evolution in Büchi automaton");
459 void MC_dump_stack_liveness(xbt_fifo_t stack){
460 mc_pair_stateless_t pair;
463 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
464 MC_pair_stateless_delete(pair);
469 void MC_print_statistics(mc_stats_t stats)
471 //XBT_INFO("State space size ~= %lu", stats->state_size);
472 XBT_INFO("Expanded states = %lu", stats->expanded_states);
473 XBT_INFO("Visited states = %lu", stats->visited_states);
474 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
475 XBT_INFO("Expanded / Visited = %lf",
476 (double) stats->visited_states / stats->expanded_states);
477 /*XBT_INFO("Exploration coverage = %lf",
478 (double)stats->expanded_states / stats->state_size); */
481 void MC_print_statistics_pairs(mc_stats_pair_t stats)
483 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
484 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
485 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
486 XBT_INFO("Expanded / Visited = %lf",
487 (double) stats->visited_pairs / stats->expanded_pairs);
488 /*XBT_INFO("Exploration coverage = %lf",
489 (double)stats->expanded_states / stats->state_size); */
492 void MC_assert(int prop)
494 if (MC_IS_ENABLED && !prop){
495 XBT_INFO("**************************");
496 XBT_INFO("*** PROPERTY NOT VALID ***");
497 XBT_INFO("**************************");
498 XBT_INFO("Counter-example execution trace:");
499 MC_dump_stack_safety(mc_stack_safety);
500 MC_print_statistics(mc_stats);
505 static void MC_assert_pair(int prop){
506 if (MC_IS_ENABLED && !prop) {
507 XBT_INFO("**************************");
508 XBT_INFO("*** PROPERTY NOT VALID ***");
509 XBT_INFO("**************************");
510 //XBT_INFO("Counter-example execution trace:");
511 MC_show_stack_liveness(mc_stack_liveness);
512 //MC_dump_snapshot_stack(mc_snapshot_stack);
513 MC_print_statistics_pairs(mc_stats_pair);
518 void MC_process_clock_add(smx_process_t process, double amount)
520 mc_time[process->pid] += amount;
523 double MC_process_clock_get(smx_process_t process)
526 return mc_time[process->pid];
533 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
534 MC_take_snapshot_liveness(sn);
538 XBT_INFO("Number of regions : %u", sn->num_reg);
540 for(i=0; i<sn->num_reg; i++){
542 switch(sn->regions[i]->type){
544 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
545 mmalloc_display_info_heap(sn->regions[i]->data);
547 case 1 : /* libsimgrid */
548 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
550 case 2 : /* data program */
551 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
554 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
555 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
563 void MC_automaton_load(const char *file){
566 if (_mc_property_automaton == NULL)
567 _mc_property_automaton = xbt_automaton_new();
568 xbt_automaton_load(_mc_property_automaton,file);
572 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
574 if (_mc_property_automaton == NULL)
575 _mc_property_automaton = xbt_automaton_new();
576 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);