1 /* Copyright (c) 2008-2013. 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;
12 xbt_dict_t first_enabled_state;
14 static void visited_state_free(mc_visited_state_t state){
16 MC_free_snapshot(state->system_state);
21 static void visited_state_free_voidp(void *s){
22 visited_state_free((mc_visited_state_t) * (void **) s);
25 static mc_visited_state_t visited_state_new(){
27 mc_visited_state_t new_state = NULL;
28 new_state = xbt_new0(s_mc_visited_state_t, 1);
29 new_state->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
30 new_state->nb_processes = xbt_swag_size(simix_global->process_list);
31 new_state->system_state = MC_take_snapshot();
32 new_state->num = mc_stats->expanded_states - 1;
38 /* Dichotomic search in visited_states dynar.
39 * States are ordered by the number of processes then the number of bytes used in std_heap */
41 static int is_visited_state(){
43 if(_sg_mc_visited == 0)
46 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
49 mc_visited_state_t new_state = visited_state_new();
52 if(xbt_dynar_is_empty(visited_states)){
55 xbt_dynar_push(visited_states, &new_state);
67 size_t current_bytes_used = new_state->heap_bytes_used;
68 int current_nb_processes = new_state->nb_processes;
70 unsigned int cursor = 0;
71 int previous_cursor = 0, next_cursor = 0;
73 int end = xbt_dynar_length(visited_states) - 1;
75 mc_visited_state_t state_test = NULL;
76 size_t bytes_used_test;
77 int nb_processes_test;
78 int same_processes_and_bytes_not_found = 1;
80 while(start <= end && same_processes_and_bytes_not_found){
81 cursor = (start + end) / 2;
82 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, cursor, mc_visited_state_t);
83 bytes_used_test = state_test->heap_bytes_used;
84 nb_processes_test = state_test->nb_processes;
85 if(nb_processes_test < current_nb_processes)
87 if(nb_processes_test > current_nb_processes)
89 if(nb_processes_test == current_nb_processes){
90 if(bytes_used_test < current_bytes_used)
92 if(bytes_used_test > current_bytes_used)
94 if(bytes_used_test == current_bytes_used){
95 same_processes_and_bytes_not_found = 0;
96 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
97 xbt_dynar_remove_at(visited_states, cursor, NULL);
98 xbt_dynar_insert_at(visited_states, cursor, &new_state);
99 XBT_DEBUG("State %d already visited ! (equal to state %d)", new_state->num, state_test->num);
104 return state_test->num;
106 /* Search another state with same number of bytes used in std_heap */
107 previous_cursor = cursor - 1;
108 while(previous_cursor >= 0){
109 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, previous_cursor, mc_visited_state_t);
110 bytes_used_test = state_test->system_state->heap_bytes_used;
111 if(bytes_used_test != current_bytes_used)
113 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
114 xbt_dynar_remove_at(visited_states, previous_cursor, NULL);
115 xbt_dynar_insert_at(visited_states, previous_cursor, &new_state);
116 XBT_DEBUG("State %d already visited ! (equal to state %d)", new_state->num, state_test->num);
121 return state_test->num;
125 next_cursor = cursor + 1;
126 while(next_cursor < xbt_dynar_length(visited_states)){
127 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, next_cursor, mc_visited_state_t);
128 bytes_used_test = state_test->system_state->heap_bytes_used;
129 if(bytes_used_test != current_bytes_used)
131 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
132 xbt_dynar_remove_at(visited_states, next_cursor, NULL);
133 xbt_dynar_insert_at(visited_states, next_cursor, &new_state);
134 XBT_DEBUG("State %d already visited ! (equal to state %d)", new_state->num, state_test->num);
139 return state_test->num;
148 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, cursor, mc_visited_state_t);
149 bytes_used_test = state_test->heap_bytes_used;
151 if(bytes_used_test < current_bytes_used)
152 xbt_dynar_insert_at(visited_states, cursor + 1, &new_state);
154 xbt_dynar_insert_at(visited_states, cursor, &new_state);
156 if(xbt_dynar_length(visited_states) > _sg_mc_visited){
157 int min = mc_stats->expanded_states;
158 unsigned int cursor2 = 0;
159 unsigned int index = 0;
160 xbt_dynar_foreach(visited_states, cursor2, state_test){
161 if(state_test->num < min){
163 min = state_test->num;
166 xbt_dynar_remove_at(visited_states, index, NULL);
180 * \brief Initialize the DPOR exploration algorithm
185 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
187 mc_state_t initial_state = NULL;
188 smx_process_t process;
190 /* Create the initial state and push it into the exploration stack */
193 initial_state = MC_state_new();
194 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
196 first_enabled_state = xbt_dict_new_homogeneous(&xbt_free_f);
200 XBT_DEBUG("**************************************************");
201 XBT_DEBUG("Initial state");
203 /* Wait for requests (schedules processes) */
204 MC_wait_for_requests();
208 /* Get an enabled process and insert it in the interleave set of the initial state */
209 xbt_swag_foreach(process, simix_global->process_list){
210 if(MC_process_is_enabled(process)){
211 MC_state_interleave_process(initial_state, process);
212 if(mc_reduce_kind != e_mc_reduce_none)
217 xbt_fifo_unshift(mc_stack_safety, initial_state);
219 /* To ensure the soundness of DPOR, we have to keep a list of
220 processes which are still enabled at each step of the exploration.
221 If max depth is reached, we interleave them in the state in which they have
222 been enabled for the first time. */
223 xbt_swag_foreach(process, simix_global->process_list){
224 if(MC_process_is_enabled(process)){
225 char *key = bprintf("%lu", process->pid);
226 char *data = bprintf("%d", xbt_fifo_size(mc_stack_safety));
227 xbt_dict_set(first_enabled_state, key, data, NULL);
243 * \brief Perform the model-checking operation using a depth-first search exploration
244 * with Dynamic Partial Order Reductions
251 smx_simcall_t req = NULL, prev_req = NULL, req2 = NULL;
252 s_smx_simcall_t req3;
253 mc_state_t state = NULL, prev_state = NULL, next_state = NULL, restore_state=NULL;
254 smx_process_t process = NULL;
255 xbt_fifo_item_t item = NULL;
259 while (xbt_fifo_size(mc_stack_safety) > 0) {
261 /* Get current state */
263 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_safety));
265 XBT_DEBUG("**************************************************");
266 XBT_DEBUG("Exploration depth=%d (state=%p)(%u interleave)",
267 xbt_fifo_size(mc_stack_safety), state,
268 MC_state_interleave_size(state));
270 /* Update statistics */
271 mc_stats->visited_states++;
273 /* If there are processes to interleave and the maximum depth has not been reached
274 then perform one step of the exploration algorithm */
275 if (xbt_fifo_size(mc_stack_safety) <= _sg_mc_max_depth &&
276 (req = MC_state_get_request(state, &value))) {
278 /* Debug information */
279 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
280 req_str = MC_request_to_string(req, value);
281 XBT_DEBUG("Execute: %s", req_str);
285 req_str = MC_request_get_dot_output(req, value);
287 MC_state_set_executed_request(state, req, value);
288 mc_stats->executed_transitions++;
291 char *key = bprintf("%lu", req->issuer->pid);
292 xbt_dict_remove(first_enabled_state, key);
296 MC_state_set_executed_request(state, req, value);
298 /* Answer the request */
299 SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
301 /* Wait for requests (schedules processes) */
302 MC_wait_for_requests();
304 /* Create the new expanded state */
307 next_state = MC_state_new();
309 if((visited_state = is_visited_state()) == -1){
311 /* Get an enabled process and insert it in the interleave set of the next state */
312 xbt_swag_foreach(process, simix_global->process_list){
313 if(MC_process_is_enabled(process)){
314 MC_state_interleave_process(next_state, process);
315 if(mc_reduce_kind != e_mc_reduce_none)
320 if(_sg_mc_checkpoint && ((xbt_fifo_size(mc_stack_safety) + 1) % _sg_mc_checkpoint == 0)){
321 next_state->system_state = MC_take_snapshot();
324 if(dot_output != NULL)
325 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
329 if(dot_output != NULL)
330 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state, req_str);
334 xbt_fifo_unshift(mc_stack_safety, next_state);
336 /* Insert in dict all enabled processes, if not included yet */
337 xbt_swag_foreach(process, simix_global->process_list){
338 if(MC_process_is_enabled(process)){
339 char *key = bprintf("%lu", process->pid);
340 if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
341 char *data = bprintf("%d", xbt_fifo_size(mc_stack_safety));
342 xbt_dict_set(first_enabled_state, key, data, NULL);
352 /* Let's loop again */
354 /* The interleave set is empty or the maximum depth is reached, let's back-track */
357 if(xbt_fifo_size(mc_stack_safety) == _sg_mc_max_depth)
358 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
360 XBT_DEBUG("There are no more processes to interleave.");
362 /* Trash the current state, no longer needed */
364 xbt_fifo_shift(mc_stack_safety);
365 MC_state_delete(state);
368 /* Check for deadlocks */
369 if(MC_deadlock_check()){
370 MC_show_deadlock(NULL);
375 /* Traverse the stack backwards until a state with a non empty interleave
376 set is found, deleting all the states that have it empty in the way.
377 For each deleted state, check if the request that has generated it
378 (from it's predecesor state), depends on any other previous request
379 executed before it. If it does then add it to the interleave set of the
380 state that executed that previous request. */
382 while ((state = xbt_fifo_shift(mc_stack_safety)) != NULL) {
383 if(mc_reduce_kind != e_mc_reduce_none){
384 req = MC_state_get_internal_request(state);
385 /* If max_depth reached, check only for the last state if the request that has generated
386 it, depends on any other processes still enabled when max_depth reached */
387 if(xbt_fifo_size(mc_stack_safety) == _sg_mc_max_depth - 1){
389 xbt_swag_foreach(process, simix_global->process_list){
390 if(MC_process_is_enabled(process) && !MC_state_process_is_done(state, process)){
391 MC_state_interleave_process(state, process);
392 req2 = MC_state_get_request(state, &value2);
393 MC_state_set_executed_request(state, req2, value2);
394 req2 = MC_state_get_internal_request(state);
395 if(MC_request_depend(&req3, req2)){
396 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
397 XBT_DEBUG("Dependent Transitions:");
398 req_str = MC_request_to_string(&req3, value);
399 XBT_DEBUG("%s (state=%p)", req_str, state);
401 req_str = MC_request_to_string(req2, value);
402 XBT_DEBUG("%s (state=%p)", req_str, state);
405 MC_state_interleave_process(state, process);
411 xbt_fifo_foreach(mc_stack_safety, item, prev_state, mc_state_t) {
412 if(MC_request_depend(req, MC_state_get_internal_request(prev_state))){
413 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
414 XBT_DEBUG("Dependent Transitions:");
415 prev_req = MC_state_get_executed_request(prev_state, &value);
416 req_str = MC_request_to_string(prev_req, value);
417 XBT_DEBUG("%s (state=%p)", req_str, prev_state);
419 prev_req = MC_state_get_executed_request(state, &value);
420 req_str = MC_request_to_string(prev_req, value);
421 XBT_DEBUG("%s (state=%p)", req_str, state);
425 if(!MC_state_process_is_done(prev_state, req->issuer))
426 MC_state_interleave_process(prev_state, req->issuer);
428 XBT_DEBUG("Process %p is in done set", req->issuer);
432 }else if(req->issuer == MC_state_get_internal_request(prev_state)->issuer){
434 XBT_DEBUG("Simcall %d and %d with same issuer", req->call, MC_state_get_internal_request(prev_state)->call);
441 if (MC_state_interleave_size(state)) {
442 /* We found a back-tracking point, let's loop */
443 if(_sg_mc_checkpoint){
444 if(state->system_state != NULL){
445 MC_restore_snapshot(state->system_state);
446 xbt_fifo_unshift(mc_stack_safety, state);
449 pos = xbt_fifo_size(mc_stack_safety);
450 item = xbt_fifo_get_first_item(mc_stack_safety);
452 restore_state = (mc_state_t) xbt_fifo_get_item_content(item);
453 if(restore_state->system_state != NULL){
456 item = xbt_fifo_get_next_item(item);
460 MC_restore_snapshot(restore_state->system_state);
461 xbt_fifo_unshift(mc_stack_safety, state);
463 MC_replay(mc_stack_safety, pos);
466 xbt_fifo_unshift(mc_stack_safety, state);
468 MC_replay(mc_stack_safety, -1);
470 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety));
473 MC_state_delete(state);
479 MC_print_statistics(mc_stats);