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"
16 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
18 "Logging specific to MC (global)");
20 /* MC global data structures */
22 mc_state_t mc_current_state = NULL;
23 char mc_replay_mode = FALSE;
24 double *mc_time = NULL;
25 mc_snapshot_t initial_snapshot = NULL;
29 xbt_fifo_t mc_stack_safety_stateful = NULL;
30 xbt_fifo_t mc_stack_safety_stateless = NULL;
31 mc_stats_t mc_stats = NULL;
35 mc_stats_pair_t mc_stats_pair = NULL;
36 xbt_fifo_t mc_stack_liveness = NULL;
37 mc_snapshot_t initial_snapshot_liveness = NULL;
39 xbt_automaton_t automaton;
42 static void MC_init_liveness(xbt_automaton_t a, char *prgm);
43 static void MC_assert_pair(int prop);
47 * \brief Initialize the model-checker data structures
49 void MC_init_safety_stateless(void)
52 /* Check if MC is already initialized */
56 mc_time = xbt_new0(double, simix_process_maxpid);
58 /* Initialize the data structures that must be persistent across every
59 iteration of the model-checker (in RAW memory) */
62 /* Initialize statistics */
63 mc_stats = xbt_new0(s_mc_stats_t, 1);
64 mc_stats->state_size = 1;
66 /* Create exploration stack */
67 mc_stack_safety_stateless = xbt_fifo_new();
74 /* Save the initial state */
75 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
76 MC_take_snapshot(initial_snapshot);
80 void MC_init_safety_stateful(void){
83 /* Check if MC is already initialized */
87 mc_time = xbt_new0(double, simix_process_maxpid);
89 /* Initialize the data structures that must be persistent across every
90 iteration of the model-checker (in RAW memory) */
93 /* Initialize statistics */
94 mc_stats = xbt_new0(s_mc_stats_t, 1);
95 mc_stats->state_size = 1;
97 /* Create exploration stack */
98 mc_stack_safety_stateful = xbt_fifo_new();
102 MC_dpor_stateful_init();
107 static void MC_init_liveness(xbt_automaton_t a, char *prgm){
109 XBT_DEBUG("Start init mc");
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_pair = xbt_new0(s_mc_stats_pair_t, 1);
121 XBT_DEBUG("Creating stack");
123 /* Create exploration stack */
124 mc_stack_liveness = xbt_fifo_new();
129 prog_name = strdup(prgm);
137 void MC_modelcheck(void)
139 MC_init_safety_stateless();
144 void MC_modelcheck_stateful(void)
146 MC_init_safety_stateful();
152 void MC_modelcheck_liveness(xbt_automaton_t a, char *prgm){
153 MC_init_liveness(a, prgm);
157 void MC_exit_liveness(void)
159 MC_print_statistics_pairs(mc_stats_pair);
168 MC_print_statistics(mc_stats);
174 int MC_random(int min, int max)
176 /*FIXME: return mc_current_state->executed_transition->random.value;*/
181 * \brief Schedules all the process that are ready to run
183 void MC_wait_for_requests(void)
185 smx_process_t process;
189 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
190 SIMIX_process_runall();
191 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
192 req = &process->simcall;
193 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
194 SIMIX_simcall_pre(req, 0);
199 int MC_deadlock_check()
201 int deadlock = FALSE;
202 smx_process_t process;
203 if(xbt_swag_size(simix_global->process_list)){
205 xbt_swag_foreach(process, simix_global->process_list){
206 if(process->simcall.call != SIMCALL_NONE
207 && MC_request_is_enabled(&process->simcall)){
217 * \brief Re-executes from the initial state all the transitions indicated by
218 * a given model-checker stack.
219 * \param stack The stack with the transitions to execute.
221 void MC_replay(xbt_fifo_t stack)
225 smx_simcall_t req = NULL, saved_req = NULL;
226 xbt_fifo_item_t item;
229 XBT_DEBUG("**** Begin Replay ****");
231 /* Restore the initial state */
232 MC_restore_snapshot(initial_snapshot);
233 /* At the moment of taking the snapshot the raw heap was set, so restoring
234 * it will set it back again, we have to unset it to continue */
237 /* Traverse the stack from the initial state and re-execute the transitions */
238 for (item = xbt_fifo_get_last_item(stack);
239 item != xbt_fifo_get_first_item(stack);
240 item = xbt_fifo_get_prev_item(item)) {
242 state = (mc_state_t) xbt_fifo_get_item_content(item);
243 saved_req = MC_state_get_executed_request(state, &value);
246 /* because we got a copy of the executed request, we have to fetch the
247 real one, pointed by the request field of the issuer process */
248 req = &saved_req->issuer->simcall;
250 /* Debug information */
251 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
252 req_str = MC_request_to_string(req, value);
253 XBT_DEBUG("Replay: %s (%p)", req_str, state);
258 SIMIX_simcall_pre(req, value);
259 MC_wait_for_requests();
261 /* Update statistics */
262 mc_stats->visited_states++;
263 mc_stats->executed_transitions++;
265 XBT_DEBUG("**** End Replay ****");
268 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
272 smx_simcall_t req = NULL, saved_req = NULL;
273 xbt_fifo_item_t item;
275 mc_pair_stateless_t pair;
278 XBT_DEBUG("**** Begin Replay ****");
280 /* Restore the initial state */
281 MC_restore_snapshot(initial_snapshot_liveness);
282 /* At the moment of taking the snapshot the raw heap was set, so restoring
283 * it will set it back again, we have to unset it to continue */
288 item = xbt_fifo_get_last_item(stack);
290 while(depth <= xbt_fifo_size(stack)){
292 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
293 state = (mc_state_t) pair->graph_state;
295 if(pair->requests > 0){
297 saved_req = MC_state_get_executed_request(state, &value);
298 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
300 if(saved_req != NULL){
301 /* because we got a copy of the executed request, we have to fetch the
302 real one, pointed by the request field of the issuer process */
303 req = &saved_req->issuer->simcall;
304 //XBT_DEBUG("Req->call %u", req->call);
306 /* Debug information */
307 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
308 req_str = MC_request_to_string(req, value);
309 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
315 SIMIX_simcall_pre(req, value);
316 MC_wait_for_requests();
321 /* Update statistics */
322 mc_stats_pair->visited_pairs++;
324 item = xbt_fifo_get_prev_item(item);
329 /* Traverse the stack from the initial state and re-execute the transitions */
330 for (item = xbt_fifo_get_last_item(stack);
331 item != xbt_fifo_get_first_item(stack);
332 item = xbt_fifo_get_prev_item(item)) {
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++;
368 XBT_DEBUG("**** End Replay ****");
373 * \brief Dumps the contents of a model-checker's stack and shows the actual
375 * \param stack The stack to dump
377 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
381 MC_show_stack_safety_stateless(stack);
384 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
385 MC_state_delete(state);
390 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
394 xbt_fifo_item_t item;
396 char *req_str = NULL;
398 for (item = xbt_fifo_get_last_item(stack);
399 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
400 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
401 req = MC_state_get_executed_request(state, &value);
403 req_str = MC_request_to_string(req, value);
404 XBT_INFO("%s", req_str);
410 void MC_show_deadlock(smx_simcall_t req)
412 /*char *req_str = NULL;*/
413 XBT_INFO("**************************");
414 XBT_INFO("*** DEAD-LOCK DETECTED ***");
415 XBT_INFO("**************************");
416 XBT_INFO("Locked request:");
417 /*req_str = MC_request_to_string(req);
418 XBT_INFO("%s", req_str);
420 XBT_INFO("Counter-example execution trace:");
421 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
424 void MC_show_deadlock_stateful(smx_simcall_t req)
426 /*char *req_str = NULL;*/
427 XBT_INFO("**************************");
428 XBT_INFO("*** DEAD-LOCK DETECTED ***");
429 XBT_INFO("**************************");
430 XBT_INFO("Locked request:");
431 /*req_str = MC_request_to_string(req);
432 XBT_INFO("%s", req_str);
434 XBT_INFO("Counter-example execution trace:");
435 MC_show_stack_safety_stateful(mc_stack_safety_stateful);
438 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
440 //mc_state_ws_t state;
442 MC_show_stack_safety_stateful(stack);
445 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
446 MC_state_delete(state);
451 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
455 xbt_fifo_item_t item;
457 char *req_str = NULL;
459 for (item = xbt_fifo_get_last_item(stack);
460 (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
461 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
462 req = MC_state_get_executed_request(state->graph_state, &value);
464 req_str = MC_request_to_string(req, value);
465 XBT_INFO("%s", req_str);
472 void MC_show_stack_liveness(xbt_fifo_t stack){
474 mc_pair_stateless_t pair;
475 xbt_fifo_item_t item;
477 char *req_str = NULL;
479 for (item = xbt_fifo_get_last_item(stack);
480 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
481 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
482 req = MC_state_get_executed_request(pair->graph_state, &value);
484 if(pair->requests>0){
485 req_str = MC_request_to_string(req, value);
486 XBT_INFO("%s", req_str);
489 XBT_INFO("End of system requests but evolution in Büchi automaton");
495 void MC_dump_stack_liveness(xbt_fifo_t stack){
496 mc_pair_stateless_t pair;
499 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
500 MC_pair_stateless_delete(pair);
505 void MC_print_statistics(mc_stats_t stats)
507 XBT_INFO("State space size ~= %lu", stats->state_size);
508 XBT_INFO("Expanded states = %lu", stats->expanded_states);
509 XBT_INFO("Visited states = %lu", stats->visited_states);
510 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
511 XBT_INFO("Expanded / Visited = %lf",
512 (double) stats->visited_states / stats->expanded_states);
513 /*XBT_INFO("Exploration coverage = %lf",
514 (double)stats->expanded_states / stats->state_size); */
517 void MC_print_statistics_pairs(mc_stats_pair_t stats)
519 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
520 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
521 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
522 XBT_INFO("Expanded / Visited = %lf",
523 (double) stats->visited_pairs / stats->expanded_pairs);
524 /*XBT_INFO("Exploration coverage = %lf",
525 (double)stats->expanded_states / stats->state_size); */
528 void MC_assert(int prop)
530 if (MC_IS_ENABLED && !prop) {
531 XBT_INFO("**************************");
532 XBT_INFO("*** PROPERTY NOT VALID ***");
533 XBT_INFO("**************************");
534 XBT_INFO("Counter-example execution trace:");
535 MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
536 MC_print_statistics(mc_stats);
541 void MC_assert_stateful(int prop)
543 if (MC_IS_ENABLED && !prop) {
544 XBT_INFO("**************************");
545 XBT_INFO("*** PROPERTY NOT VALID ***");
546 XBT_INFO("**************************");
547 XBT_INFO("Counter-example execution trace:");
548 MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
549 MC_print_statistics(mc_stats);
555 static void MC_assert_pair(int prop){
556 if (MC_IS_ENABLED && !prop) {
557 XBT_INFO("**************************");
558 XBT_INFO("*** PROPERTY NOT VALID ***");
559 XBT_INFO("**************************");
560 //XBT_INFO("Counter-example execution trace:");
561 MC_show_stack_liveness(mc_stack_liveness);
562 //MC_dump_snapshot_stack(mc_snapshot_stack);
563 MC_print_statistics_pairs(mc_stats_pair);
568 void MC_process_clock_add(smx_process_t process, double amount)
570 mc_time[process->pid] += amount;
573 double MC_process_clock_get(smx_process_t process)
576 return mc_time[process->pid];
583 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
584 MC_take_snapshot_liveness(sn);
588 XBT_INFO("Number of regions : %u", sn->num_reg);
590 for(i=0; i<sn->num_reg; i++){
592 switch(sn->regions[i]->type){
594 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
595 mmalloc_display_info_heap(sn->regions[i]->data);
597 case 1 : /* libsimgrid */
598 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
600 case 2 : /* data program */
601 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
604 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
605 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);