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;
33 new_state->other_num = -1;
39 /* Dichotomic search in visited_states dynar.
40 * States are ordered by the number of processes then the number of bytes used in std_heap */
42 static int is_visited_state(){
44 if(_sg_mc_visited == 0)
47 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
50 mc_visited_state_t new_state = visited_state_new();
53 if(xbt_dynar_is_empty(visited_states)){
56 xbt_dynar_push(visited_states, &new_state);
68 size_t current_bytes_used = new_state->heap_bytes_used;
69 int current_nb_processes = new_state->nb_processes;
71 unsigned int cursor = 0;
72 int previous_cursor = 0, next_cursor = 0;
74 int end = xbt_dynar_length(visited_states) - 1;
76 mc_visited_state_t state_test = NULL;
77 size_t bytes_used_test;
78 int nb_processes_test;
79 int same_processes_and_bytes_not_found = 1;
81 while(start <= end && same_processes_and_bytes_not_found){
82 cursor = (start + end) / 2;
83 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, cursor, mc_visited_state_t);
84 bytes_used_test = state_test->heap_bytes_used;
85 nb_processes_test = state_test->nb_processes;
86 if(nb_processes_test < current_nb_processes){
88 }else if(nb_processes_test > current_nb_processes){
90 }else if(nb_processes_test == current_nb_processes){
91 if(bytes_used_test < current_bytes_used)
93 if(bytes_used_test > current_bytes_used)
95 if(bytes_used_test == current_bytes_used){
96 same_processes_and_bytes_not_found = 0;
97 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
98 if(state_test->other_num == -1)
99 new_state->other_num = state_test->num;
101 new_state->other_num = state_test->other_num;
102 if(dot_output == NULL)
103 XBT_DEBUG("Next state %d already visited ! (equal to state %d)", new_state->num, state_test->num);
105 XBT_DEBUG("Next state %d already visited ! (equal to state %d (state %d in dot_output))", new_state->num, state_test->num, new_state->other_num);
106 xbt_dynar_remove_at(visited_states, cursor, NULL);
107 xbt_dynar_insert_at(visited_states, cursor, &new_state);
112 return new_state->other_num;
114 /* Search another state with same number of bytes used in std_heap */
115 previous_cursor = cursor - 1;
116 while(previous_cursor >= 0){
117 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, previous_cursor, mc_visited_state_t);
118 bytes_used_test = state_test->system_state->heap_bytes_used;
119 if(bytes_used_test != current_bytes_used)
121 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
122 if(state_test->other_num == -1)
123 new_state->other_num = state_test->num;
125 new_state->other_num = state_test->other_num;
126 if(dot_output == NULL)
127 XBT_DEBUG("Next state %d already visited ! (equal to state %d)", new_state->num, state_test->num);
129 XBT_DEBUG("Next state %d already visited ! (equal to state %d (state %d in dot_output))", new_state->num, state_test->num, new_state->other_num);
130 xbt_dynar_remove_at(visited_states, previous_cursor, NULL);
131 xbt_dynar_insert_at(visited_states, cursor, &new_state);
136 return new_state->other_num;
140 next_cursor = cursor + 1;
141 while(next_cursor < xbt_dynar_length(visited_states)){
142 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, next_cursor, mc_visited_state_t);
143 bytes_used_test = state_test->system_state->heap_bytes_used;
144 if(bytes_used_test != current_bytes_used)
146 if(snapshot_compare(new_state->system_state, state_test->system_state) == 0){
147 if(state_test->other_num == -1)
148 new_state->other_num = state_test->num;
150 new_state->other_num = state_test->other_num;
151 if(dot_output == NULL)
152 XBT_DEBUG("Next state %d already visited ! (equal to state %d)", new_state->num, state_test->num);
154 XBT_DEBUG("Next state %d already visited ! (equal to state %d (state %d in dot_output))", new_state->num, state_test->num, new_state->other_num);
155 xbt_dynar_remove_at(visited_states, next_cursor, NULL);
156 xbt_dynar_insert_at(visited_states, next_cursor, &new_state);
161 return new_state->other_num;
170 state_test = (mc_visited_state_t)xbt_dynar_get_as(visited_states, cursor, mc_visited_state_t);
171 bytes_used_test = state_test->heap_bytes_used;
173 if(bytes_used_test < current_bytes_used)
174 xbt_dynar_insert_at(visited_states, cursor + 1, &new_state);
176 xbt_dynar_insert_at(visited_states, cursor, &new_state);
178 if(xbt_dynar_length(visited_states) > _sg_mc_visited){
179 int min = mc_stats->expanded_states;
180 unsigned int cursor2 = 0;
181 unsigned int index = 0;
182 xbt_dynar_foreach(visited_states, cursor2, state_test){
183 if(state_test->num < min){
185 min = state_test->num;
188 xbt_dynar_remove_at(visited_states, index, NULL);
202 * \brief Initialize the DPOR exploration algorithm
207 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
209 mc_state_t initial_state = NULL;
210 smx_process_t process;
212 /* Create the initial state and push it into the exploration stack */
215 initial_state = MC_state_new();
216 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
218 first_enabled_state = xbt_dict_new_homogeneous(&xbt_free_f);
222 XBT_DEBUG("**************************************************");
223 XBT_DEBUG("Initial state");
225 /* Wait for requests (schedules processes) */
226 MC_wait_for_requests();
230 /* Get an enabled process and insert it in the interleave set of the initial state */
231 xbt_swag_foreach(process, simix_global->process_list){
232 if(MC_process_is_enabled(process)){
233 MC_state_interleave_process(initial_state, process);
234 if(mc_reduce_kind != e_mc_reduce_none)
239 xbt_fifo_unshift(mc_stack_safety, initial_state);
241 /* To ensure the soundness of DPOR, we have to keep a list of
242 processes which are still enabled at each step of the exploration.
243 If max depth is reached, we interleave them in the state in which they have
244 been enabled for the first time. */
245 xbt_swag_foreach(process, simix_global->process_list){
246 if(MC_process_is_enabled(process)){
247 char *key = bprintf("%lu", process->pid);
248 char *data = bprintf("%d", xbt_fifo_size(mc_stack_safety));
249 xbt_dict_set(first_enabled_state, key, data, NULL);
265 * \brief Perform the model-checking operation using a depth-first search exploration
266 * with Dynamic Partial Order Reductions
271 char *req_str = NULL;
273 smx_simcall_t req = NULL, prev_req = NULL;
274 mc_state_t state = NULL, prev_state = NULL, next_state = NULL, restore_state=NULL;
275 smx_process_t process = NULL;
276 xbt_fifo_item_t item = NULL;
279 int enabled = 0, max_depth_reached;
281 while (xbt_fifo_size(mc_stack_safety) > 0) {
283 /* Get current state */
285 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_safety));
287 XBT_DEBUG("**************************************************");
288 XBT_DEBUG("Exploration depth=%d (state=%p, num %d)(%u interleave, user_max_depth %d)",
289 xbt_fifo_size(mc_stack_safety), state, state->num,
290 MC_state_interleave_size(state), user_max_depth_reached);
292 /* Update statistics */
293 mc_stats->visited_states++;
295 /* If there are processes to interleave and the maximum depth has not been reached
296 then perform one step of the exploration algorithm */
297 if (xbt_fifo_size(mc_stack_safety) <= _sg_mc_max_depth && !user_max_depth_reached &&
298 (req = MC_state_get_request(state, &value))) {
300 /* Debug information */
301 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
302 req_str = MC_request_to_string(req, value);
303 XBT_DEBUG("Execute: %s", req_str);
308 if(dot_output != NULL)
309 req_str = MC_request_get_dot_output(req, value);
312 MC_state_set_executed_request(state, req, value);
313 mc_stats->executed_transitions++;
316 char *key = bprintf("%lu", req->issuer->pid);
317 xbt_dict_remove(first_enabled_state, key);
321 /* Answer the request */
322 SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
324 /* Wait for requests (schedules processes) */
325 MC_wait_for_requests();
327 /* Create the new expanded state */
330 next_state = MC_state_new();
332 if((visited_state = is_visited_state()) == -1){
334 /* Get an enabled process and insert it in the interleave set of the next state */
335 xbt_swag_foreach(process, simix_global->process_list){
336 if(MC_process_is_enabled(process)){
337 MC_state_interleave_process(next_state, process);
338 if(mc_reduce_kind != e_mc_reduce_none)
343 if(_sg_mc_checkpoint && ((xbt_fifo_size(mc_stack_safety) + 1) % _sg_mc_checkpoint == 0)){
344 next_state->system_state = MC_take_snapshot();
347 if(dot_output != NULL)
348 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
352 if(dot_output != NULL)
353 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state, req_str);
357 xbt_fifo_unshift(mc_stack_safety, next_state);
359 /* Insert in dict all enabled processes, if not included yet */
360 xbt_swag_foreach(process, simix_global->process_list){
361 if(MC_process_is_enabled(process)){
362 char *key = bprintf("%lu", process->pid);
363 if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
364 char *data = bprintf("%d", xbt_fifo_size(mc_stack_safety));
365 xbt_dict_set(first_enabled_state, key, data, NULL);
371 if(dot_output != NULL)
376 /* Let's loop again */
378 /* The interleave set is empty or the maximum depth is reached, let's back-track */
381 if((xbt_fifo_size(mc_stack_safety) > _sg_mc_max_depth) || user_max_depth_reached){
383 if(user_max_depth_reached)
384 XBT_WARN("User max depth reached !");
386 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
388 /* Interleave enabled processes in the state in which they have been enabled for the first time */
389 xbt_swag_foreach(process, simix_global->process_list){
390 if(MC_process_is_enabled(process)){
391 char *key = bprintf("%lu", process->pid);
392 enabled = (int)strtoul(xbt_dict_get_or_null(first_enabled_state, key), 0, 10);
394 mc_state_t state_test = NULL;
395 xbt_fifo_item_t item = NULL;
396 int cursor = xbt_fifo_size(mc_stack_safety);
397 xbt_fifo_foreach(mc_stack_safety, item, state_test, mc_state_t){
398 if(cursor-- == enabled){
399 if(!MC_state_process_is_done(state_test, process)){
400 MC_state_interleave_process(state_test, process);
408 if(MC_state_interleave_size(state) > 0){
409 max_depth_reached = 1;
411 /* Trash the current state, no longer needed */
413 xbt_fifo_shift(mc_stack_safety);
414 MC_state_delete(state);
417 max_depth_reached = 0;
423 XBT_DEBUG("There are no more processes to interleave.");
425 /* Trash the current state, no longer needed */
427 xbt_fifo_shift(mc_stack_safety);
428 MC_state_delete(state);
429 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack_safety) + 1);
432 max_depth_reached = 0;
435 /* Check for deadlocks */
436 if(MC_deadlock_check()){
437 MC_show_deadlock(NULL);
442 /* Traverse the stack backwards until a state with a non empty interleave
443 set is found, deleting all the states that have it empty in the way.
444 For each deleted state, check if the request that has generated it
445 (from it's predecesor state), depends on any other previous request
446 executed before it. If it does then add it to the interleave set of the
447 state that executed that previous request. */
449 while ((state = xbt_fifo_shift(mc_stack_safety)) != NULL) {
450 if(mc_reduce_kind != e_mc_reduce_none){
451 if(((xbt_fifo_size(mc_stack_safety) == _sg_mc_max_depth) && max_depth_reached) || user_max_depth_reached){
452 req = MC_state_get_request(state, &value);
453 MC_state_set_executed_request(state, req, value);
454 user_max_depth_reached = 0;
456 req = MC_state_get_internal_request(state);
457 xbt_fifo_foreach(mc_stack_safety, item, prev_state, mc_state_t) {
458 if(MC_request_depend(req, MC_state_get_internal_request(prev_state))){
459 if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
460 XBT_DEBUG("Dependent Transitions:");
461 prev_req = MC_state_get_executed_request(prev_state, &value);
462 req_str = MC_request_to_string(prev_req, value);
463 XBT_DEBUG("%s (state=%d)", req_str, prev_state->num);
465 prev_req = MC_state_get_executed_request(state, &value);
466 req_str = MC_request_to_string(prev_req, value);
467 XBT_DEBUG("%s (state=%d)", req_str, state->num);
471 if(!MC_state_process_is_done(prev_state, req->issuer))
472 MC_state_interleave_process(prev_state, req->issuer);
474 XBT_DEBUG("Process %p is in done set", req->issuer);
478 }else if(req->issuer == MC_state_get_internal_request(prev_state)->issuer){
480 XBT_DEBUG("Simcall %d and %d with same issuer", req->call, MC_state_get_internal_request(prev_state)->call);
485 XBT_DEBUG("Simcall %d, process %lu (state %d) and simcall %d, process %lu (state %d) are independant", req->call, req->issuer->pid, state->num, MC_state_get_internal_request(prev_state)->call, MC_state_get_internal_request(prev_state)->issuer->pid, prev_state->num);
491 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack_safety) < _sg_mc_max_depth) {
492 /* We found a back-tracking point, let's loop */
493 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack_safety) + 1);
494 if(_sg_mc_checkpoint){
495 if(state->system_state != NULL){
496 MC_restore_snapshot(state->system_state);
497 xbt_fifo_unshift(mc_stack_safety, state);
500 pos = xbt_fifo_size(mc_stack_safety);
501 item = xbt_fifo_get_first_item(mc_stack_safety);
503 restore_state = (mc_state_t) xbt_fifo_get_item_content(item);
504 if(restore_state->system_state != NULL){
507 item = xbt_fifo_get_next_item(item);
511 MC_restore_snapshot(restore_state->system_state);
512 xbt_fifo_unshift(mc_stack_safety, state);
514 MC_replay(mc_stack_safety, pos);
517 xbt_fifo_unshift(mc_stack_safety, state);
519 MC_replay(mc_stack_safety, -1);
521 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack_safety));
524 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack_safety) + 1);
525 MC_state_delete(state);
531 MC_print_statistics(mc_stats);