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. */
6 #include "mc_private.h"
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dpor, mc,
9 "Logging specific to MC DPOR exploration");
11 xbt_dynar_t visited_states;
13 static int is_visited_state(void);
15 static int is_visited_state(){
17 if(_surf_mc_visited == 0)
20 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
24 mc_snapshot_t new_state = NULL;
25 new_state = MC_take_snapshot();
29 if(xbt_dynar_is_empty(visited_states)){
32 xbt_dynar_push(visited_states, &new_state);
44 unsigned int cursor = 0;
45 mc_snapshot_t state_test = NULL;
47 xbt_dynar_foreach(visited_states, cursor, state_test){
48 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug))
49 XBT_DEBUG("****** Pair visited #%d ******", cursor + 1);
50 if(snapshot_compare(new_state, state_test, NULL, NULL) == 0){
60 if(xbt_dynar_length(visited_states) == _surf_mc_visited){
61 mc_snapshot_t state_to_remove = NULL;
62 xbt_dynar_shift(visited_states, &state_to_remove);
63 MC_free_snapshot(state_to_remove);
66 xbt_dynar_push(visited_states, &new_state);
79 * \brief Initialize the DPOR exploration algorithm
84 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
86 mc_state_t initial_state = NULL;
87 smx_process_t process;
89 /* Create the initial state and push it into the exploration stack */
92 initial_state = MC_state_new();
93 visited_states = xbt_dynar_new(sizeof(mc_snapshot_t), NULL);
97 XBT_DEBUG("**************************************************");
98 XBT_DEBUG("Initial state");
100 /* Wait for requests (schedules processes) */
101 MC_wait_for_requests();
105 xbt_swag_foreach(process, simix_global->process_list){
106 if(MC_process_is_enabled(process)){
107 XBT_DEBUG("Process %lu enabled with simcall : %d", process->pid, (&process->simcall)->call);
111 /* Get an enabled process and insert it in the interleave set of the initial state */
112 xbt_swag_foreach(process, simix_global->process_list){
113 if(MC_process_is_enabled(process)){
114 MC_state_interleave_process(initial_state, process);
119 xbt_fifo_unshift(mc_stack_safety, initial_state);
129 /* FIXME: Update Statistics
130 mc_stats->state_size +=
131 xbt_setset_set_size(initial_state->enabled_transitions); */
136 * \brief Perform the model-checking operation using a depth-first search exploration
137 * with Dynamic Partial Order Reductions
144 smx_simcall_t req = NULL, prev_req = NULL;
145 mc_state_t state = NULL, prev_state = NULL, next_state = NULL, restore_state=NULL;
146 smx_process_t process = NULL;
147 xbt_fifo_item_t item = NULL;
150 while (xbt_fifo_size(mc_stack_safety) > 0) {
152 /* Get current state */
154 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_safety));
156 XBT_DEBUG("**************************************************");
157 XBT_DEBUG("Exploration depth=%d (state=%p)(%u interleave)",
158 xbt_fifo_size(mc_stack_safety), state,
159 MC_state_interleave_size(state));
161 /* Update statistics */
162 mc_stats->visited_states++;
164 /* If there are processes to interleave and the maximum depth has not been reached
165 then perform one step of the exploration algorithm */
166 if (xbt_fifo_size(mc_stack_safety) < _surf_mc_max_depth &&
167 (req = MC_state_get_request(state, &value))) {
169 /* Debug information */
170 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
171 req_str = MC_request_to_string(req, value);
172 XBT_DEBUG("Execute: %s", req_str);
176 MC_state_set_executed_request(state, req, value);
177 mc_stats->executed_transitions++;
179 /* Answer the request */
180 SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
182 /* Wait for requests (schedules processes) */
183 MC_wait_for_requests();
185 /* Create the new expanded state */
188 next_state = MC_state_new();
190 if(!is_visited_state()){
192 /* Get an enabled process and insert it in the interleave set of the next state */
193 xbt_swag_foreach(process, simix_global->process_list){
194 if(MC_process_is_enabled(process)){
195 MC_state_interleave_process(next_state, process);
196 XBT_DEBUG("Process %lu enabled with simcall : %d", process->pid, (&process->simcall)->call);
200 if(_surf_mc_checkpoint && ((xbt_fifo_size(mc_stack_safety) + 1) % _surf_mc_checkpoint == 0)){
201 next_state->system_state = MC_take_snapshot();
206 XBT_DEBUG("State already visited !");
210 xbt_fifo_unshift(mc_stack_safety, next_state);
213 /* FIXME: Update Statistics
214 mc_stats->state_size +=
215 xbt_setset_set_size(next_state->enabled_transitions);*/
217 /* Let's loop again */
219 /* The interleave set is empty or the maximum depth is reached, let's back-track */
222 if(xbt_fifo_size(mc_stack_safety) == _surf_mc_max_depth){
224 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
226 XBT_WARN("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ ");
227 XBT_WARN("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
232 XBT_DEBUG("There are no more processes to interleave.");
236 /* Trash the current state, no longer needed */
238 xbt_fifo_shift(mc_stack_safety);
239 MC_state_delete(state);
242 /* Check for deadlocks */
243 if(MC_deadlock_check()){
244 MC_show_deadlock(NULL);
249 /* Traverse the stack backwards until a state with a non empty interleave
250 set is found, deleting all the states that have it empty in the way.
251 For each deleted state, check if the request that has generated it
252 (from it's predecesor state), depends on any other previous request
253 executed before it. If it does then add it to the interleave set of the
254 state that executed that previous request. */
256 while ((state = xbt_fifo_shift(mc_stack_safety)) != NULL) {
257 if(MC_state_interleave_size(state) == 0){
258 req = MC_state_get_internal_request(state);
259 xbt_fifo_foreach(mc_stack_safety, item, prev_state, mc_state_t) {
260 if(MC_request_depend(req, MC_state_get_internal_request(prev_state))){
261 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
262 XBT_DEBUG("Dependent Transitions:");
263 prev_req = MC_state_get_executed_request(prev_state, &value);
264 req_str = MC_request_to_string(prev_req, value);
265 XBT_DEBUG("%s (state=%p)", req_str, prev_state);
267 prev_req = MC_state_get_executed_request(state, &value);
268 req_str = MC_request_to_string(prev_req, value);
269 XBT_DEBUG("%s (state=%p)", req_str, state);
275 }else if(req->issuer == MC_state_get_executed_request(prev_state, &value)->issuer){
281 MC_state_remove_interleave_process(prev_state, req->issuer);
286 if (MC_state_interleave_size(state)) {
287 /* We found a back-tracking point, let's loop */
288 if(_surf_mc_checkpoint){
289 if(state->system_state != NULL){
290 MC_restore_snapshot(state->system_state);
291 xbt_fifo_unshift(mc_stack_safety, state);
292 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety));
295 pos = xbt_fifo_size(mc_stack_safety);
296 item = xbt_fifo_get_first_item(mc_stack_safety);
298 restore_state = (mc_state_t) xbt_fifo_get_item_content(item);
299 if(restore_state->system_state != NULL){
302 item = xbt_fifo_get_next_item(item);
306 MC_restore_snapshot(restore_state->system_state);
307 xbt_fifo_unshift(mc_stack_safety, state);
308 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety));
310 MC_replay(mc_stack_safety, pos);
313 xbt_fifo_unshift(mc_stack_safety, state);
314 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety));
316 MC_replay(mc_stack_safety, -1);
320 MC_state_delete(state);
326 MC_print_statistics(mc_stats);