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 ****");
341 * \brief Dumps the contents of a model-checker's stack and shows the actual
343 * \param stack The stack to dump
345 void MC_dump_stack_safety(xbt_fifo_t stack)
348 MC_show_stack_safety(stack);
350 if(!_surf_do_mc_checkpoint){
355 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
356 MC_state_delete(state);
363 void MC_show_stack_safety(xbt_fifo_t stack)
367 xbt_fifo_item_t item;
369 char *req_str = NULL;
371 for (item = xbt_fifo_get_last_item(stack);
372 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
373 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
374 req = MC_state_get_executed_request(state, &value);
376 req_str = MC_request_to_string(req, value);
377 XBT_INFO("%s", req_str);
383 void MC_show_deadlock(smx_simcall_t req)
385 /*char *req_str = NULL;*/
386 XBT_INFO("**************************");
387 XBT_INFO("*** DEAD-LOCK DETECTED ***");
388 XBT_INFO("**************************");
389 XBT_INFO("Locked request:");
390 /*req_str = MC_request_to_string(req);
391 XBT_INFO("%s", req_str);
393 XBT_INFO("Counter-example execution trace:");
394 MC_dump_stack_safety(mc_stack_safety);
398 void MC_show_stack_liveness(xbt_fifo_t stack){
400 mc_pair_stateless_t pair;
401 xbt_fifo_item_t item;
403 char *req_str = NULL;
405 for (item = xbt_fifo_get_last_item(stack);
406 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
407 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
408 req = MC_state_get_executed_request(pair->graph_state, &value);
410 if(pair->requests>0){
411 req_str = MC_request_to_string(req, value);
412 XBT_INFO("%s", req_str);
415 XBT_INFO("End of system requests but evolution in Büchi automaton");
421 void MC_dump_stack_liveness(xbt_fifo_t stack){
422 mc_pair_stateless_t pair;
425 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
426 MC_pair_stateless_delete(pair);
431 void MC_print_statistics(mc_stats_t stats)
433 //XBT_INFO("State space size ~= %lu", stats->state_size);
434 XBT_INFO("Expanded states = %lu", stats->expanded_states);
435 XBT_INFO("Visited states = %lu", stats->visited_states);
436 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
437 XBT_INFO("Expanded / Visited = %lf",
438 (double) stats->visited_states / stats->expanded_states);
439 /*XBT_INFO("Exploration coverage = %lf",
440 (double)stats->expanded_states / stats->state_size); */
443 void MC_print_statistics_pairs(mc_stats_pair_t stats)
445 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
446 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
447 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
448 XBT_INFO("Expanded / Visited = %lf",
449 (double) stats->visited_pairs / stats->expanded_pairs);
450 /*XBT_INFO("Exploration coverage = %lf",
451 (double)stats->expanded_states / stats->state_size); */
454 void MC_assert(int prop)
456 if (MC_IS_ENABLED && !prop){
457 XBT_INFO("**************************");
458 XBT_INFO("*** PROPERTY NOT VALID ***");
459 XBT_INFO("**************************");
460 XBT_INFO("Counter-example execution trace:");
461 MC_dump_stack_safety(mc_stack_safety);
462 MC_print_statistics(mc_stats);
467 static void MC_assert_pair(int prop){
468 if (MC_IS_ENABLED && !prop) {
469 XBT_INFO("**************************");
470 XBT_INFO("*** PROPERTY NOT VALID ***");
471 XBT_INFO("**************************");
472 //XBT_INFO("Counter-example execution trace:");
473 MC_show_stack_liveness(mc_stack_liveness);
474 //MC_dump_snapshot_stack(mc_snapshot_stack);
475 MC_print_statistics_pairs(mc_stats_pair);
480 void MC_process_clock_add(smx_process_t process, double amount)
482 mc_time[process->pid] += amount;
485 double MC_process_clock_get(smx_process_t process)
488 return mc_time[process->pid];
495 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
496 MC_take_snapshot_liveness(sn);
500 XBT_INFO("Number of regions : %u", sn->num_reg);
502 for(i=0; i<sn->num_reg; i++){
504 switch(sn->regions[i]->type){
506 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
507 mmalloc_display_info_heap(sn->regions[i]->data);
509 case 1 : /* libsimgrid */
510 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
512 case 2 : /* data program */
513 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
516 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
517 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
525 xbt_automaton_t MC_create_automaton(const char *file){
527 xbt_automaton_t a = xbt_create_automaton(file);