6 #include "../surf/surf_private.h"
7 #include "../simix/private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
13 "Logging specific to MC (global)");
15 /* MC global data structures */
17 mc_state_t mc_current_state = NULL;
18 char mc_replay_mode = FALSE;
19 double *mc_time = NULL;
20 mc_snapshot_t initial_snapshot = NULL;
24 xbt_fifo_t mc_stack_safety_stateful = NULL;
25 xbt_fifo_t mc_stack_safety_stateless = NULL;
26 mc_stats_t mc_stats = NULL;
30 mc_stats_pair_t mc_stats_pair = NULL;
31 xbt_fifo_t mc_stack_liveness = NULL;
32 mc_snapshot_t initial_snapshot_liveness = NULL;
34 xbt_automaton_t automaton;
38 * \brief Initialize the model-checker data structures
40 void MC_init_safety_stateless(void)
43 /* Check if MC is already initialized */
47 mc_time = xbt_new0(double, simix_process_maxpid);
49 /* Initialize the data structures that must be persistent across every
50 iteration of the model-checker (in RAW memory) */
53 /* Initialize statistics */
54 mc_stats = xbt_new0(s_mc_stats_t, 1);
55 mc_stats->state_size = 1;
57 /* Create exploration stack */
58 mc_stack_safety_stateless = xbt_fifo_new();
65 /* Save the initial state */
66 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
67 MC_take_snapshot(initial_snapshot);
71 void MC_init_safety_stateful(void){
74 /* Check if MC is already initialized */
78 mc_time = xbt_new0(double, simix_process_maxpid);
80 /* Initialize the data structures that must be persistent across every
81 iteration of the model-checker (in RAW memory) */
84 /* Initialize statistics */
85 mc_stats = xbt_new0(s_mc_stats_t, 1);
86 mc_stats->state_size = 1;
88 /* Create exploration stack */
89 mc_stack_safety_stateful = xbt_fifo_new();
93 MC_dpor_stateful_init();
98 void MC_init_liveness(xbt_automaton_t a, char *prgm){
100 XBT_DEBUG("Start init mc");
102 mc_time = xbt_new0(double, simix_process_maxpid);
104 /* Initialize the data structures that must be persistent across every
105 iteration of the model-checker (in RAW memory) */
109 /* Initialize statistics */
110 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
112 XBT_DEBUG("Creating stack");
114 /* Create exploration stack */
115 mc_stack_liveness = xbt_fifo_new();
120 prog_name = strdup(prgm);
128 void MC_modelcheck(void)
130 MC_init_safety_stateless();
135 void MC_modelcheck_stateful(void)
137 MC_init_safety_stateful();
143 void MC_modelcheck_liveness(xbt_automaton_t a, char *prgm){
144 MC_init_liveness(a, prgm);
148 void MC_exit_liveness(void)
150 MC_print_statistics_pairs(mc_stats_pair);
159 MC_print_statistics(mc_stats);
165 int MC_random(int min, int max)
167 /*FIXME: return mc_current_state->executed_transition->random.value;*/
172 * \brief Schedules all the process that are ready to run
174 void MC_wait_for_requests(void)
176 smx_process_t process;
180 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
181 SIMIX_process_runall();
182 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
183 req = &process->request;
184 if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
185 SIMIX_request_pre(req, 0);
190 int MC_deadlock_check()
192 int deadlock = FALSE;
193 smx_process_t process;
194 if(xbt_swag_size(simix_global->process_list)){
196 xbt_swag_foreach(process, simix_global->process_list){
197 if(process->request.call != REQ_NO_REQ
198 && MC_request_is_enabled(&process->request)){
208 * \brief Re-executes from the initial state all the transitions indicated by
209 * a given model-checker stack.
210 * \param stack The stack with the transitions to execute.
212 void MC_replay(xbt_fifo_t stack)
216 smx_req_t req = NULL, saved_req = NULL;
217 xbt_fifo_item_t item;
220 XBT_DEBUG("**** Begin Replay ****");
222 /* Restore the initial state */
223 MC_restore_snapshot(initial_snapshot);
224 /* At the moment of taking the snapshot the raw heap was set, so restoring
225 * it will set it back again, we have to unset it to continue */
228 /* Traverse the stack from the initial state and re-execute the transitions */
229 for (item = xbt_fifo_get_last_item(stack);
230 item != xbt_fifo_get_first_item(stack);
231 item = xbt_fifo_get_prev_item(item)) {
233 state = (mc_state_t) xbt_fifo_get_item_content(item);
234 saved_req = MC_state_get_executed_request(state, &value);
237 /* because we got a copy of the executed request, we have to fetch the
238 real one, pointed by the request field of the issuer process */
239 req = &saved_req->issuer->request;
241 /* Debug information */
242 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
243 req_str = MC_request_to_string(req, value);
244 XBT_DEBUG("Replay: %s (%p)", req_str, state);
249 SIMIX_request_pre(req, value);
250 MC_wait_for_requests();
252 /* Update statistics */
253 mc_stats->visited_states++;
254 mc_stats->executed_transitions++;
256 XBT_DEBUG("**** End Replay ****");
259 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
263 smx_req_t req = NULL, saved_req = NULL;
264 xbt_fifo_item_t item;
266 mc_pair_stateless_t pair;
269 XBT_DEBUG("**** Begin Replay ****");
271 /* Restore the initial state */
272 MC_restore_snapshot(initial_snapshot_liveness);
273 /* At the moment of taking the snapshot the raw heap was set, so restoring
274 * it will set it back again, we have to unset it to continue */
279 item = xbt_fifo_get_last_item(stack);
281 while(depth <= xbt_fifo_size(stack)){
283 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
284 state = (mc_state_t) pair->graph_state;
286 if(pair->requests > 0){
288 saved_req = MC_state_get_executed_request(state, &value);
289 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
291 if(saved_req != NULL){
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->request;
295 //XBT_DEBUG("Req->call %u", req->call);
297 /* Debug information */
298 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
299 req_str = MC_request_to_string(req, value);
300 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
306 SIMIX_request_pre(req, value);
307 MC_wait_for_requests();
312 /* Update statistics */
313 mc_stats_pair->visited_pairs++;
315 item = xbt_fifo_get_prev_item(item);
320 /* Traverse the stack from the initial state and re-execute the transitions */
321 for (item = xbt_fifo_get_last_item(stack);
322 item != xbt_fifo_get_first_item(stack);
323 item = xbt_fifo_get_prev_item(item)) {
325 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
326 state = (mc_state_t) pair->graph_state;
328 if(pair->requests > 0){
330 saved_req = MC_state_get_executed_request(state, &value);
331 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
333 if(saved_req != NULL){
334 /* because we got a copy of the executed request, we have to fetch the
335 real one, pointed by the request field of the issuer process */
336 req = &saved_req->issuer->request;
337 //XBT_DEBUG("Req->call %u", req->call);
339 /* Debug information */
340 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
341 req_str = MC_request_to_string(req, value);
342 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
348 SIMIX_request_pre(req, value);
349 MC_wait_for_requests();
354 /* Update statistics */
355 mc_stats_pair->visited_pairs++;
359 XBT_DEBUG("**** End Replay ****");
364 * \brief Dumps the contents of a model-checker's stack and shows the actual
366 * \param stack The stack to dump
368 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
372 MC_show_stack_safety_stateless(stack);
375 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
376 MC_state_delete(state);
381 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
385 xbt_fifo_item_t item;
387 char *req_str = NULL;
389 for (item = xbt_fifo_get_last_item(stack);
390 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
391 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
392 req = MC_state_get_executed_request(state, &value);
394 req_str = MC_request_to_string(req, value);
395 XBT_INFO("%s", req_str);
401 void MC_show_deadlock(smx_req_t req)
403 /*char *req_str = NULL;*/
404 XBT_INFO("**************************");
405 XBT_INFO("*** DEAD-LOCK DETECTED ***");
406 XBT_INFO("**************************");
407 XBT_INFO("Locked request:");
408 /*req_str = MC_request_to_string(req);
409 XBT_INFO("%s", req_str);
411 XBT_INFO("Counter-example execution trace:");
412 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
415 void MC_show_deadlock_stateful(smx_req_t req)
417 /*char *req_str = NULL;*/
418 XBT_INFO("**************************");
419 XBT_INFO("*** DEAD-LOCK DETECTED ***");
420 XBT_INFO("**************************");
421 XBT_INFO("Locked request:");
422 /*req_str = MC_request_to_string(req);
423 XBT_INFO("%s", req_str);
425 XBT_INFO("Counter-example execution trace:");
426 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
429 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
431 //mc_state_ws_t state;
433 MC_show_stack_safety_stateful(stack);
436 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
437 MC_state_delete(state);
442 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
446 xbt_fifo_item_t item;
448 char *req_str = NULL;
450 for (item = xbt_fifo_get_last_item(stack);
451 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
452 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
453 req = MC_state_get_executed_request(state->graph_state, &value);
455 req_str = MC_request_to_string(req, value);
456 XBT_INFO("%s", req_str);
463 void MC_show_stack_liveness(xbt_fifo_t stack){
465 mc_pair_stateless_t pair;
466 xbt_fifo_item_t item;
468 char *req_str = NULL;
470 for (item = xbt_fifo_get_last_item(stack);
471 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
472 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
473 req = MC_state_get_executed_request(pair->graph_state, &value);
475 if(pair->requests>0){
476 req_str = MC_request_to_string(req, value);
477 XBT_INFO("%s", req_str);
480 XBT_INFO("End of system requests but evolution in Büchi automaton");
486 void MC_dump_stack_liveness(xbt_fifo_t stack){
487 mc_pair_stateless_t pair;
490 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
491 MC_pair_stateless_delete(pair);
496 void MC_print_statistics(mc_stats_t stats)
498 XBT_INFO("State space size ~= %lu", stats->state_size);
499 XBT_INFO("Expanded states = %lu", stats->expanded_states);
500 XBT_INFO("Visited states = %lu", stats->visited_states);
501 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
502 XBT_INFO("Expanded / Visited = %lf",
503 (double) stats->visited_states / stats->expanded_states);
504 /*XBT_INFO("Exploration coverage = %lf",
505 (double)stats->expanded_states / stats->state_size); */
508 void MC_print_statistics_pairs(mc_stats_pair_t stats)
510 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
511 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
512 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
513 XBT_INFO("Expanded / Visited = %lf",
514 (double) stats->visited_pairs / stats->expanded_pairs);
515 /*XBT_INFO("Exploration coverage = %lf",
516 (double)stats->expanded_states / stats->state_size); */
519 void MC_assert(int prop)
521 if (MC_IS_ENABLED && !prop) {
522 XBT_INFO("**************************");
523 XBT_INFO("*** PROPERTY NOT VALID ***");
524 XBT_INFO("**************************");
525 XBT_INFO("Counter-example execution trace:");
526 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
527 MC_print_statistics(mc_stats);
532 void MC_assert_stateful(int prop)
534 if (MC_IS_ENABLED && !prop) {
535 XBT_INFO("**************************");
536 XBT_INFO("*** PROPERTY NOT VALID ***");
537 XBT_INFO("**************************");
538 XBT_INFO("Counter-example execution trace:");
539 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
540 MC_print_statistics(mc_stats);
546 void MC_assert_pair(int prop){
547 if (MC_IS_ENABLED && !prop) {
548 XBT_INFO("**************************");
549 XBT_INFO("*** PROPERTY NOT VALID ***");
550 XBT_INFO("**************************");
551 //XBT_INFO("Counter-example execution trace:");
552 MC_show_stack_liveness(mc_stack_liveness);
553 //MC_dump_snapshot_stack(mc_snapshot_stack);
554 MC_print_statistics_pairs(mc_stats_pair);
559 void MC_process_clock_add(smx_process_t process, double amount)
561 mc_time[process->pid] += amount;
564 double MC_process_clock_get(smx_process_t process)
567 return mc_time[process->pid];