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/automaton_create.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 /* MC global data structures */
23 mc_state_t mc_current_state = NULL;
24 char mc_replay_mode = FALSE;
25 double *mc_time = NULL;
26 mc_snapshot_t initial_snapshot = NULL;
30 xbt_fifo_t mc_stack_safety = 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;
41 static void MC_init_liveness(xbt_automaton_t a);
42 static void MC_assert_pair(int prop);
46 * \brief Initialize the model-checker data structures
48 void MC_init_safety(void)
51 /* Check if MC is already initialized */
55 mc_time = xbt_new0(double, simix_process_maxpid);
57 /* Initialize the data structures that must be persistent across every
58 iteration of the model-checker (in RAW memory) */
61 /* Initialize statistics */
62 mc_stats = xbt_new0(s_mc_stats_t, 1);
63 mc_stats->state_size = 1;
65 /* Create exploration stack */
66 mc_stack_safety = xbt_fifo_new();
73 /* Save the initial state */
74 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
75 MC_take_snapshot(initial_snapshot);
81 static void MC_init_liveness(xbt_automaton_t a){
83 XBT_DEBUG("Start init mc");
85 mc_time = xbt_new0(double, simix_process_maxpid);
87 /* Initialize the data structures that must be persistent across every
88 iteration of the model-checker (in RAW memory) */
92 /* Initialize statistics */
93 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
95 XBT_DEBUG("Creating stack");
97 /* Create exploration stack */
98 mc_stack_liveness = xbt_fifo_new();
110 void MC_modelcheck(void)
119 void MC_modelcheck_liveness(xbt_automaton_t a){
124 void MC_exit_liveness(void)
126 MC_print_statistics_pairs(mc_stats_pair);
135 MC_print_statistics(mc_stats);
141 int MC_random(int min, int max)
143 /*FIXME: return mc_current_state->executed_transition->random.value;*/
148 * \brief Schedules all the process that are ready to run
150 void MC_wait_for_requests(void)
152 smx_process_t process;
156 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
157 SIMIX_process_runall();
158 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
159 req = &process->simcall;
160 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
161 SIMIX_simcall_pre(req, 0);
166 int MC_deadlock_check()
168 int deadlock = FALSE;
169 smx_process_t process;
170 if(xbt_swag_size(simix_global->process_list)){
172 xbt_swag_foreach(process, simix_global->process_list){
173 if(process->simcall.call != SIMCALL_NONE
174 && MC_request_is_enabled(&process->simcall)){
184 * \brief Re-executes from the start state all the transitions indicated by
185 * a given model-checker stack.
186 * \param stack The stack with the transitions to execute.
187 * \param start Start index to begin the re-execution.
189 void MC_replay(xbt_fifo_t stack, int start)
193 smx_simcall_t req = NULL, saved_req = NULL;
194 xbt_fifo_item_t item;
197 XBT_DEBUG("**** Begin Replay ****");
199 /* Restore the initial state */
200 MC_restore_snapshot(initial_snapshot);
201 /* At the moment of taking the snapshot the raw heap was set, so restoring
202 * it will set it back again, we have to unset it to continue */
205 /* Traverse the stack from the initial state and re-execute the transitions */
206 for (item = xbt_fifo_get_last_item(stack);
207 item != xbt_fifo_get_first_item(stack);
208 item = xbt_fifo_get_prev_item(item)) {
210 state = (mc_state_t) xbt_fifo_get_item_content(item);
211 saved_req = MC_state_get_executed_request(state, &value);
214 /* because we got a copy of the executed request, we have to fetch the
215 real one, pointed by the request field of the issuer process */
216 req = &saved_req->issuer->simcall;
218 /* Debug information */
219 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
220 req_str = MC_request_to_string(req, value);
221 XBT_DEBUG("Replay: %s (%p)", req_str, state);
226 SIMIX_simcall_pre(req, value);
227 MC_wait_for_requests();
229 /* Update statistics */
230 mc_stats->visited_states++;
231 mc_stats->executed_transitions++;
233 XBT_DEBUG("**** End Replay ****");
236 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
240 smx_simcall_t req = NULL, saved_req = NULL;
241 xbt_fifo_item_t item;
243 mc_pair_stateless_t pair;
246 XBT_DEBUG("**** Begin Replay ****");
248 /* Restore the initial state */
249 MC_restore_snapshot(initial_snapshot_liveness);
250 /* At the moment of taking the snapshot the raw heap was set, so restoring
251 * it will set it back again, we have to unset it to continue */
256 item = xbt_fifo_get_last_item(stack);
258 while(depth <= xbt_fifo_size(stack)){
260 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
261 state = (mc_state_t) pair->graph_state;
263 if(pair->requests > 0){
265 saved_req = MC_state_get_executed_request(state, &value);
266 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
268 if(saved_req != NULL){
269 /* because we got a copy of the executed request, we have to fetch the
270 real one, pointed by the request field of the issuer process */
271 req = &saved_req->issuer->simcall;
272 //XBT_DEBUG("Req->call %u", req->call);
274 /* Debug information */
275 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
276 req_str = MC_request_to_string(req, value);
277 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
283 SIMIX_simcall_pre(req, value);
284 MC_wait_for_requests();
289 /* Update statistics */
290 mc_stats_pair->visited_pairs++;
292 item = xbt_fifo_get_prev_item(item);
297 /* Traverse the stack from the initial state and re-execute the transitions */
298 for (item = xbt_fifo_get_last_item(stack);
299 item != xbt_fifo_get_first_item(stack);
300 item = xbt_fifo_get_prev_item(item)) {
302 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
303 state = (mc_state_t) pair->graph_state;
305 if(pair->requests > 0){
307 saved_req = MC_state_get_executed_request(state, &value);
308 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
310 if(saved_req != NULL){
311 /* because we got a copy of the executed request, we have to fetch the
312 real one, pointed by the request field of the issuer process */
313 req = &saved_req->issuer->simcall;
314 //XBT_DEBUG("Req->call %u", req->call);
316 /* Debug information */
317 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
318 req_str = MC_request_to_string(req, value);
319 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
325 SIMIX_simcall_pre(req, value);
326 MC_wait_for_requests();
331 /* Update statistics */
332 mc_stats_pair->visited_pairs++;
336 XBT_DEBUG("**** End Replay ****");
340 * \brief Dumps the contents of a model-checker's stack and shows the actual
342 * \param stack The stack to dump
344 void MC_dump_stack_safety(xbt_fifo_t stack)
347 MC_show_stack_safety(stack);
349 if(!_surf_do_mc_checkpoint){
354 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
355 MC_state_delete(state);
362 void MC_show_stack_safety(xbt_fifo_t stack)
366 xbt_fifo_item_t item;
368 char *req_str = NULL;
370 for (item = xbt_fifo_get_last_item(stack);
371 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
372 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
373 req = MC_state_get_executed_request(state, &value);
375 req_str = MC_request_to_string(req, value);
376 XBT_INFO("%s", req_str);
382 void MC_show_deadlock(smx_simcall_t req)
384 /*char *req_str = NULL;*/
385 XBT_INFO("**************************");
386 XBT_INFO("*** DEAD-LOCK DETECTED ***");
387 XBT_INFO("**************************");
388 XBT_INFO("Locked request:");
389 /*req_str = MC_request_to_string(req);
390 XBT_INFO("%s", req_str);
392 XBT_INFO("Counter-example execution trace:");
393 MC_dump_stack_safety(mc_stack_safety);
397 void MC_show_stack_liveness(xbt_fifo_t stack){
399 mc_pair_stateless_t pair;
400 xbt_fifo_item_t item;
402 char *req_str = NULL;
404 for (item = xbt_fifo_get_last_item(stack);
405 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
406 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
407 req = MC_state_get_executed_request(pair->graph_state, &value);
409 if(pair->requests>0){
410 req_str = MC_request_to_string(req, value);
411 XBT_INFO("%s", req_str);
414 XBT_INFO("End of system requests but evolution in Büchi automaton");
420 void MC_dump_stack_liveness(xbt_fifo_t stack){
421 mc_pair_stateless_t pair;
424 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
425 MC_pair_stateless_delete(pair);
430 void MC_print_statistics(mc_stats_t stats)
432 //XBT_INFO("State space size ~= %lu", stats->state_size);
433 XBT_INFO("Expanded states = %lu", stats->expanded_states);
434 XBT_INFO("Visited states = %lu", stats->visited_states);
435 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
436 XBT_INFO("Expanded / Visited = %lf",
437 (double) stats->visited_states / stats->expanded_states);
438 /*XBT_INFO("Exploration coverage = %lf",
439 (double)stats->expanded_states / stats->state_size); */
442 void MC_print_statistics_pairs(mc_stats_pair_t stats)
444 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
445 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
446 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
447 XBT_INFO("Expanded / Visited = %lf",
448 (double) stats->visited_pairs / stats->expanded_pairs);
449 /*XBT_INFO("Exploration coverage = %lf",
450 (double)stats->expanded_states / stats->state_size); */
453 void MC_assert(int prop)
455 if (MC_IS_ENABLED && !prop){
456 XBT_INFO("**************************");
457 XBT_INFO("*** PROPERTY NOT VALID ***");
458 XBT_INFO("**************************");
459 XBT_INFO("Counter-example execution trace:");
460 MC_dump_stack_safety(mc_stack_safety);
461 MC_print_statistics(mc_stats);
466 static void MC_assert_pair(int prop){
467 if (MC_IS_ENABLED && !prop) {
468 XBT_INFO("**************************");
469 XBT_INFO("*** PROPERTY NOT VALID ***");
470 XBT_INFO("**************************");
471 //XBT_INFO("Counter-example execution trace:");
472 MC_show_stack_liveness(mc_stack_liveness);
473 //MC_dump_snapshot_stack(mc_snapshot_stack);
474 MC_print_statistics_pairs(mc_stats_pair);
479 void MC_process_clock_add(smx_process_t process, double amount)
481 mc_time[process->pid] += amount;
484 double MC_process_clock_get(smx_process_t process)
487 return mc_time[process->pid];
494 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
495 MC_take_snapshot_liveness(sn);
499 XBT_INFO("Number of regions : %u", sn->num_reg);
501 for(i=0; i<sn->num_reg; i++){
503 switch(sn->regions[i]->type){
505 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
506 mmalloc_display_info_heap(sn->regions[i]->data);
508 case 1 : /* libsimgrid */
509 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
511 case 2 : /* data program */
512 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
515 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
516 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
524 xbt_automaton_t MC_create_automaton(const char *file){
526 xbt_automaton_t a = xbt_create_automaton(file);