Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / src / mc / mc_dpor.c
1 /* Copyright (c) 2008-2013. Da SimGrid Team. All rights reserved.           */
2
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. */
5
6 #include "mc_private.h"
7
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dpor, mc,
9                                 "Logging specific to MC DPOR exploration");
10
11 xbt_dynar_t visited_states;
12 xbt_dict_t first_enabled_state;
13
14 static void visited_state_free(mc_visited_state_t state){
15   if(state){
16     MC_free_snapshot(state->system_state);
17     xbt_free(state);
18   }
19 }
20
21 static void visited_state_free_voidp(void *s){
22   visited_state_free((mc_visited_state_t) * (void **) s);
23 }
24
25 static mc_visited_state_t visited_state_new(){
26
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;
33
34   return new_state;
35   
36 }
37
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 */
40
41 static int is_visited_state(){
42
43   if(_sg_mc_visited == 0)
44     return -1;
45
46   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
47
48   MC_SET_RAW_MEM;
49   mc_visited_state_t new_state = visited_state_new();
50   MC_UNSET_RAW_MEM;
51   
52   if(xbt_dynar_is_empty(visited_states)){
53
54     MC_SET_RAW_MEM;
55     xbt_dynar_push(visited_states, &new_state); 
56     MC_UNSET_RAW_MEM;
57
58     if(raw_mem_set)
59       MC_SET_RAW_MEM;
60
61     return -1;
62
63   }else{
64
65     MC_SET_RAW_MEM;
66     
67     size_t current_bytes_used = new_state->heap_bytes_used;
68     int current_nb_processes = new_state->nb_processes;
69
70     unsigned int cursor = 0;
71     int previous_cursor = 0, next_cursor = 0;
72     int start = 0;
73     int end = xbt_dynar_length(visited_states) - 1;
74
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;
79
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)
86         start = cursor + 1;
87       if(nb_processes_test > current_nb_processes)
88         end = cursor - 1; 
89       if(nb_processes_test == current_nb_processes){
90         if(bytes_used_test < current_bytes_used)
91           start = cursor + 1;
92         if(bytes_used_test > current_bytes_used)
93           end = cursor - 1;
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);
100             if(raw_mem_set)
101               MC_SET_RAW_MEM;
102             else
103               MC_UNSET_RAW_MEM;
104             return state_test->num;
105           }else{
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)
112                 break;
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);
117                 if(raw_mem_set)
118                   MC_SET_RAW_MEM;
119                 else
120                   MC_UNSET_RAW_MEM;
121                 return state_test->num;
122               }
123               previous_cursor--;
124             }
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)
130                 break;
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);
135                 if(raw_mem_set)
136                   MC_SET_RAW_MEM;
137                 else
138                   MC_UNSET_RAW_MEM;
139                 return state_test->num;
140               }
141               next_cursor++;
142             }
143           }   
144         }
145       }
146     }
147
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;
150
151     if(bytes_used_test < current_bytes_used)
152       xbt_dynar_insert_at(visited_states, cursor + 1, &new_state);
153     else
154       xbt_dynar_insert_at(visited_states, cursor, &new_state);
155
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){
162           index = cursor2;
163           min = state_test->num;
164         }
165       }
166       xbt_dynar_remove_at(visited_states, index, NULL);
167     }
168     
169     MC_UNSET_RAW_MEM;
170
171     if(raw_mem_set)
172       MC_SET_RAW_MEM;
173     
174     return -1;
175     
176   }
177 }
178
179 /**
180  *  \brief Initialize the DPOR exploration algorithm
181  */
182 void MC_dpor_init()
183 {
184   
185   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
186
187   mc_state_t initial_state = NULL;
188   smx_process_t process;
189   
190   /* Create the initial state and push it into the exploration stack */
191   MC_SET_RAW_MEM;
192
193   initial_state = MC_state_new();
194   visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
195
196   first_enabled_state = xbt_dict_new_homogeneous(&xbt_free_f);
197
198   MC_UNSET_RAW_MEM;
199
200   XBT_DEBUG("**************************************************");
201   XBT_DEBUG("Initial state");
202
203   /* Wait for requests (schedules processes) */
204   MC_wait_for_requests();
205
206   MC_SET_RAW_MEM;
207  
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)
213         break;
214     }
215   }
216
217   xbt_fifo_unshift(mc_stack_safety, initial_state);
218
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);
228       xbt_free(key);
229     }
230   }
231
232   MC_UNSET_RAW_MEM;
233
234   if(raw_mem_set)
235     MC_SET_RAW_MEM;
236   else
237     MC_UNSET_RAW_MEM;
238   
239 }
240
241
242 /**
243  *   \brief Perform the model-checking operation using a depth-first search exploration
244  *         with Dynamic Partial Order Reductions
245  */
246 void MC_dpor(void)
247 {
248
249   char *req_str;
250   int value;
251   smx_simcall_t req = NULL, prev_req = NULL;
252   mc_state_t state = NULL, prev_state = NULL, next_state = NULL, restore_state=NULL;
253   smx_process_t process = NULL;
254   xbt_fifo_item_t item = NULL;
255   int pos;
256   int visited_state;
257   int enabled = 0, max_depth_reached = 0;
258
259   while (xbt_fifo_size(mc_stack_safety) > 0) {
260
261     /* Get current state */
262     state = (mc_state_t) 
263       xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_safety));
264
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));
269
270     /* Update statistics */
271     mc_stats->visited_states++;
272
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))) {
277
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);
282         xbt_free(req_str);
283       }
284         
285       if(dot_output != NULL)
286         req_str = MC_request_get_dot_output(req, value);
287
288       MC_state_set_executed_request(state, req, value);
289       mc_stats->executed_transitions++;
290
291       MC_SET_RAW_MEM;
292       char *key = bprintf("%lu", req->issuer->pid);
293       xbt_dict_remove(first_enabled_state, key); 
294       xbt_free(key);
295       MC_UNSET_RAW_MEM;
296
297       /* Answer the request */
298       SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
299
300       /* Wait for requests (schedules processes) */
301       MC_wait_for_requests();
302
303       /* Create the new expanded state */
304       MC_SET_RAW_MEM;
305
306       next_state = MC_state_new();
307
308       if((visited_state = is_visited_state()) == -1){
309      
310         /* Get an enabled process and insert it in the interleave set of the next state */
311         xbt_swag_foreach(process, simix_global->process_list){
312           if(MC_process_is_enabled(process)){
313             MC_state_interleave_process(next_state, process);
314             if(mc_reduce_kind != e_mc_reduce_none)
315               break;
316           }
317         }
318
319         if(_sg_mc_checkpoint && ((xbt_fifo_size(mc_stack_safety) + 1) % _sg_mc_checkpoint == 0)){
320           next_state->system_state = MC_take_snapshot();
321         }
322
323         if(dot_output != NULL)
324           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
325
326       }else{
327
328         if(dot_output != NULL)
329           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state, req_str);
330
331       }
332
333       xbt_fifo_unshift(mc_stack_safety, next_state);
334
335       /* Insert in dict all enabled processes, if not included yet */
336       xbt_swag_foreach(process, simix_global->process_list){
337         if(MC_process_is_enabled(process)){
338           char *key = bprintf("%lu", process->pid);
339           if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
340             char *data = bprintf("%d", xbt_fifo_size(mc_stack_safety));
341             xbt_dict_set(first_enabled_state, key, data, NULL); 
342           }
343           xbt_free(key);
344         }
345       }
346
347       MC_UNSET_RAW_MEM;
348
349       if(dot_output != NULL)
350         xbt_free(req_str);
351
352       /* Let's loop again */
353
354       /* The interleave set is empty or the maximum depth is reached, let's back-track */
355     } else {
356
357       if(xbt_fifo_size(mc_stack_safety) > _sg_mc_max_depth){  
358
359         XBT_WARN("/!\\ Max depth reached ! /!\\ ");
360
361         /* Interleave enabled processes in the state in which they have been enabled for the first time */
362         xbt_swag_foreach(process, simix_global->process_list){
363           if(MC_process_is_enabled(process)){
364             char *key = bprintf("%lu", process->pid);
365             enabled = (int)strtoul(xbt_dict_get_or_null(first_enabled_state, key), 0, 10);
366             xbt_free(key);
367             mc_state_t state_test = NULL;
368             xbt_fifo_item_t item = NULL;
369             int cursor = xbt_fifo_size(mc_stack_safety);
370             xbt_fifo_foreach(mc_stack_safety, item, state_test, mc_state_t){
371               if(cursor-- == enabled){ 
372                 if(!MC_state_process_is_done(state_test, process)){ 
373                   MC_state_interleave_process(state_test, process);
374                   break;
375                 }
376               }
377             } 
378           }
379         }
380
381         max_depth_reached = 1;
382
383       }else{
384
385         XBT_DEBUG("There are no more processes to interleave.");
386
387         /* Trash the current state, no longer needed */
388         MC_SET_RAW_MEM;
389         xbt_fifo_shift(mc_stack_safety);
390         MC_state_delete(state);
391         MC_UNSET_RAW_MEM;
392
393         max_depth_reached = 0;
394       }
395       
396       /* Check for deadlocks */
397       if(MC_deadlock_check()){
398         MC_show_deadlock(NULL);
399         return;
400       }
401
402       MC_SET_RAW_MEM;
403       /* Traverse the stack backwards until a state with a non empty interleave
404          set is found, deleting all the states that have it empty in the way.
405          For each deleted state, check if the request that has generated it 
406          (from it's predecesor state), depends on any other previous request 
407          executed before it. If it does then add it to the interleave set of the
408          state that executed that previous request. */
409       
410       while ((state = xbt_fifo_shift(mc_stack_safety)) != NULL) {
411         if(mc_reduce_kind != e_mc_reduce_none){
412           if((xbt_fifo_size(mc_stack_safety) == _sg_mc_max_depth) && max_depth_reached){
413             req = MC_state_get_request(state, &value);
414             MC_state_set_executed_request(state, req, value);
415           }
416           req = MC_state_get_internal_request(state);
417           xbt_fifo_foreach(mc_stack_safety, item, prev_state, mc_state_t) {
418             if(MC_request_depend(req, MC_state_get_internal_request(prev_state))){
419               if(XBT_LOG_ISENABLED(mc_dpor, xbt_log_priority_debug)){
420                 XBT_DEBUG("Dependent Transitions:");
421                 prev_req = MC_state_get_executed_request(prev_state, &value);
422                 req_str = MC_request_to_string(prev_req, value);
423                 XBT_DEBUG("%s (state=%p)", req_str, prev_state);
424                 xbt_free(req_str);
425                 prev_req = MC_state_get_executed_request(state, &value);
426                 req_str = MC_request_to_string(prev_req, value);
427                 XBT_DEBUG("%s (state=%p)", req_str, state);
428                 xbt_free(req_str);              
429               }
430
431               if(!MC_state_process_is_done(prev_state, req->issuer))
432                 MC_state_interleave_process(prev_state, req->issuer);
433               else
434                 XBT_DEBUG("Process %p is in done set", req->issuer);
435
436               break;
437
438             }else if(req->issuer == MC_state_get_internal_request(prev_state)->issuer){
439
440               XBT_DEBUG("Simcall %d and %d with same issuer", req->call, MC_state_get_internal_request(prev_state)->call);
441               break;
442
443             }
444           }
445         }
446              
447         if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack_safety) < _sg_mc_max_depth) {
448           /* We found a back-tracking point, let's loop */
449           XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety) + 1);
450           if(_sg_mc_checkpoint){
451             if(state->system_state != NULL){
452               MC_restore_snapshot(state->system_state);
453               xbt_fifo_unshift(mc_stack_safety, state);
454               MC_UNSET_RAW_MEM;
455             }else{
456               pos = xbt_fifo_size(mc_stack_safety);
457               item = xbt_fifo_get_first_item(mc_stack_safety);
458               while(pos>0){
459                 restore_state = (mc_state_t) xbt_fifo_get_item_content(item);
460                 if(restore_state->system_state != NULL){
461                   break;
462                 }else{
463                   item = xbt_fifo_get_next_item(item);
464                   pos--;
465                 }
466               }
467               MC_restore_snapshot(restore_state->system_state);
468               xbt_fifo_unshift(mc_stack_safety, state);
469               MC_UNSET_RAW_MEM;
470               MC_replay(mc_stack_safety, pos);
471             }
472           }else{
473             xbt_fifo_unshift(mc_stack_safety, state);
474             MC_UNSET_RAW_MEM;
475             MC_replay(mc_stack_safety, -1);
476           }
477           XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_stack_safety));
478           break;
479         } else {
480           MC_state_delete(state);
481         }
482       }
483       MC_UNSET_RAW_MEM;
484     }
485   }
486   MC_print_statistics(mc_stats);
487   MC_UNSET_RAW_MEM;  
488
489   return;
490 }
491
492
493
494