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 state at position start 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, start_item;
197 XBT_DEBUG("**** Begin Replay ****");
200 /* Restore the initial state */
201 MC_restore_snapshot(initial_snapshot);
202 /* At the moment of taking the snapshot the raw heap was set, so restoring
203 * it will set it back again, we have to unset it to continue */
207 start_item = xbt_fifo_get_last_item(stack);
210 start_item = xbt_fifo_get_prev_item(start_item);
215 /* Traverse the stack from the state at position start and re-execute the transitions */
216 for (item = start_item;
217 item != xbt_fifo_get_first_item(stack);
218 item = xbt_fifo_get_prev_item(item)) {
220 state = (mc_state_t) xbt_fifo_get_item_content(item);
221 saved_req = MC_state_get_executed_request(state, &value);
224 /* because we got a copy of the executed request, we have to fetch the
225 real one, pointed by the request field of the issuer process */
226 req = &saved_req->issuer->simcall;
228 /* Debug information */
229 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
230 req_str = MC_request_to_string(req, value);
231 XBT_DEBUG("Replay: %s (%p)", req_str, state);
236 SIMIX_simcall_pre(req, value);
237 MC_wait_for_requests();
239 /* Update statistics */
240 mc_stats->visited_states++;
241 mc_stats->executed_transitions++;
243 XBT_DEBUG("**** End Replay ****");
246 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
250 smx_simcall_t req = NULL, saved_req = NULL;
251 xbt_fifo_item_t item;
253 mc_pair_stateless_t pair;
256 XBT_DEBUG("**** Begin Replay ****");
258 /* Restore the initial state */
259 MC_restore_snapshot(initial_snapshot_liveness);
260 /* At the moment of taking the snapshot the raw heap was set, so restoring
261 * it will set it back again, we have to unset it to continue */
266 item = xbt_fifo_get_last_item(stack);
268 while(depth <= xbt_fifo_size(stack)){
270 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
271 state = (mc_state_t) pair->graph_state;
273 if(pair->requests > 0){
275 saved_req = MC_state_get_executed_request(state, &value);
276 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
278 if(saved_req != NULL){
279 /* because we got a copy of the executed request, we have to fetch the
280 real one, pointed by the request field of the issuer process */
281 req = &saved_req->issuer->simcall;
282 //XBT_DEBUG("Req->call %u", req->call);
284 /* Debug information */
285 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
286 req_str = MC_request_to_string(req, value);
287 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
293 SIMIX_simcall_pre(req, value);
294 MC_wait_for_requests();
299 /* Update statistics */
300 mc_stats_pair->visited_pairs++;
302 item = xbt_fifo_get_prev_item(item);
307 /* Traverse the stack from the initial state and re-execute the transitions */
308 for (item = xbt_fifo_get_last_item(stack);
309 item != xbt_fifo_get_first_item(stack);
310 item = xbt_fifo_get_prev_item(item)) {
312 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
313 state = (mc_state_t) pair->graph_state;
315 if(pair->requests > 0){
317 saved_req = MC_state_get_executed_request(state, &value);
318 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
320 if(saved_req != NULL){
321 /* because we got a copy of the executed request, we have to fetch the
322 real one, pointed by the request field of the issuer process */
323 req = &saved_req->issuer->simcall;
324 //XBT_DEBUG("Req->call %u", req->call);
326 /* Debug information */
327 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
328 req_str = MC_request_to_string(req, value);
329 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
335 SIMIX_simcall_pre(req, value);
336 MC_wait_for_requests();
341 /* Update statistics */
342 mc_stats_pair->visited_pairs++;
346 XBT_DEBUG("**** End Replay ****");
350 * \brief Dumps the contents of a model-checker's stack and shows the actual
352 * \param stack The stack to dump
354 void MC_dump_stack_safety(xbt_fifo_t stack)
357 MC_show_stack_safety(stack);
359 if(!_surf_do_mc_checkpoint){
364 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
365 MC_state_delete(state);
372 void MC_show_stack_safety(xbt_fifo_t stack)
376 xbt_fifo_item_t item;
378 char *req_str = NULL;
380 for (item = xbt_fifo_get_last_item(stack);
381 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
382 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
383 req = MC_state_get_executed_request(state, &value);
385 req_str = MC_request_to_string(req, value);
386 XBT_INFO("%s", req_str);
392 void MC_show_deadlock(smx_simcall_t req)
394 /*char *req_str = NULL;*/
395 XBT_INFO("**************************");
396 XBT_INFO("*** DEAD-LOCK DETECTED ***");
397 XBT_INFO("**************************");
398 XBT_INFO("Locked request:");
399 /*req_str = MC_request_to_string(req);
400 XBT_INFO("%s", req_str);
402 XBT_INFO("Counter-example execution trace:");
403 MC_dump_stack_safety(mc_stack_safety);
407 void MC_show_stack_liveness(xbt_fifo_t stack){
409 mc_pair_stateless_t pair;
410 xbt_fifo_item_t item;
412 char *req_str = NULL;
414 for (item = xbt_fifo_get_last_item(stack);
415 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
416 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
417 req = MC_state_get_executed_request(pair->graph_state, &value);
419 if(pair->requests>0){
420 req_str = MC_request_to_string(req, value);
421 XBT_INFO("%s", req_str);
424 XBT_INFO("End of system requests but evolution in Büchi automaton");
430 void MC_dump_stack_liveness(xbt_fifo_t stack){
431 mc_pair_stateless_t pair;
434 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
435 MC_pair_stateless_delete(pair);
440 void MC_print_statistics(mc_stats_t stats)
442 //XBT_INFO("State space size ~= %lu", stats->state_size);
443 XBT_INFO("Expanded states = %lu", stats->expanded_states);
444 XBT_INFO("Visited states = %lu", stats->visited_states);
445 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
446 XBT_INFO("Expanded / Visited = %lf",
447 (double) stats->visited_states / stats->expanded_states);
448 /*XBT_INFO("Exploration coverage = %lf",
449 (double)stats->expanded_states / stats->state_size); */
452 void MC_print_statistics_pairs(mc_stats_pair_t stats)
454 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
455 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
456 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
457 XBT_INFO("Expanded / Visited = %lf",
458 (double) stats->visited_pairs / stats->expanded_pairs);
459 /*XBT_INFO("Exploration coverage = %lf",
460 (double)stats->expanded_states / stats->state_size); */
463 void MC_assert(int prop)
465 if (MC_IS_ENABLED && !prop){
466 XBT_INFO("**************************");
467 XBT_INFO("*** PROPERTY NOT VALID ***");
468 XBT_INFO("**************************");
469 XBT_INFO("Counter-example execution trace:");
470 MC_dump_stack_safety(mc_stack_safety);
471 MC_print_statistics(mc_stats);
476 static void MC_assert_pair(int prop){
477 if (MC_IS_ENABLED && !prop) {
478 XBT_INFO("**************************");
479 XBT_INFO("*** PROPERTY NOT VALID ***");
480 XBT_INFO("**************************");
481 //XBT_INFO("Counter-example execution trace:");
482 MC_show_stack_liveness(mc_stack_liveness);
483 //MC_dump_snapshot_stack(mc_snapshot_stack);
484 MC_print_statistics_pairs(mc_stats_pair);
489 void MC_process_clock_add(smx_process_t process, double amount)
491 mc_time[process->pid] += amount;
494 double MC_process_clock_get(smx_process_t process)
497 return mc_time[process->pid];
504 mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
505 MC_take_snapshot_liveness(sn);
509 XBT_INFO("Number of regions : %u", sn->num_reg);
511 for(i=0; i<sn->num_reg; i++){
513 switch(sn->regions[i]->type){
515 XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
516 mmalloc_display_info_heap(sn->regions[i]->data);
518 case 1 : /* libsimgrid */
519 XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
521 case 2 : /* data program */
522 XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
525 XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
526 XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
534 xbt_automaton_t MC_create_automaton(const char *file){
536 xbt_automaton_t a = xbt_create_automaton(file);