Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8be40448363bfa20d11d16846c8d5cf08582bc1c
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2011-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <unistd.h>
8 #include <sys/wait.h>
9
10 #include <xbt/dynar.h>
11 #include <xbt/automaton.h>
12
13 #include "mc_request.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
16 #include "mc_record.h"
17 #include "mc_smx.h"
18 #include "mc_client.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
21                                 "Logging specific to algorithms for liveness properties verification");
22
23 /********* Global variables *********/
24
25 xbt_dynar_t acceptance_pairs;
26 xbt_parmap_t parmap;
27
28 /********* Static functions *********/
29
30 static xbt_dynar_t get_atomic_propositions_values()
31 {
32   unsigned int cursor = 0;
33   xbt_automaton_propositional_symbol_t ps = NULL;
34   xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
35   xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
36     int res = xbt_automaton_propositional_symbol_evaluate(ps);
37     xbt_dynar_push_as(values, int, res);
38   }
39
40   return values;
41 }
42
43 static mc_visited_pair_t is_reached_acceptance_pair(mc_pair_t pair) {
44   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
45
46   mc_visited_pair_t new_pair = NULL;
47   new_pair = MC_visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
48   new_pair->acceptance_pair = 1;
49
50   if (xbt_dynar_is_empty(acceptance_pairs)) {
51
52     xbt_dynar_push(acceptance_pairs, &new_pair);
53
54   } else {
55
56     int min = -1, max = -1, index;
57     //int res;
58     mc_visited_pair_t pair_test;
59     int cursor;
60
61     index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
62
63     if (min != -1 && max != -1) {       // Acceptance pair with same number of processes and same heap bytes used exists
64
65       // Parallell implementation
66       /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
67          if(res != -1){
68          if(!raw_mem_set)
69          MC_SET_STD_HEAP;
70          return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
71          } */
72
73       cursor = min;
74       if(pair->search_cycle == 1){
75         while (cursor <= max) {
76           pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor, mc_visited_pair_t);
77           if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
78             if (xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, new_pair->atomic_propositions) == 0) {
79               if (snapshot_compare(pair_test, new_pair) == 0) {
80                 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
81                 xbt_fifo_shift(mc_stack);
82                 if (dot_output != NULL)
83                   fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
84                 mmalloc_set_current_heap(heap);
85                 return NULL;
86               }
87             }
88           }
89           cursor++;
90         }
91       }
92       xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
93     } else {
94       pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index, mc_visited_pair_t);
95       if (pair_test->nb_processes < new_pair->nb_processes) {
96         xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
97       } else {
98         if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
99           xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
100         else
101           xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
102       }
103     }
104
105   }
106   mmalloc_set_current_heap(heap);
107   return new_pair;
108 }
109
110 static void remove_acceptance_pair(int pair_num)
111 {
112   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
113
114   unsigned int cursor = 0;
115   mc_visited_pair_t pair_test = NULL;
116   int pair_found = 0;
117
118   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
119     if (pair_test->num == pair_num) {
120        pair_found = 1;
121       break;
122     }
123   }
124
125   if(pair_found == 1) {
126     xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
127
128     pair_test->acceptance_removed = 1;
129
130     if (_sg_mc_visited == 0 || pair_test->visited_removed == 1) 
131       MC_visited_pair_delete(pair_test);
132
133   }
134
135   mmalloc_set_current_heap(heap);
136 }
137
138
139 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
140                                        xbt_dynar_t atomic_propositions_values)
141 {
142
143   switch (l->type) {
144   case 0:{
145       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
146       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
147       return (left_res || right_res);
148     }
149   case 1:{
150       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
151       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
152       return (left_res && right_res);
153     }
154   case 2:{
155       int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
156       return (!res);
157     }
158   case 3:{
159       unsigned int cursor = 0;
160       xbt_automaton_propositional_symbol_t p = NULL;
161       xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p) {
162         if (strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
163           return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
164       }
165       return -1;
166     }
167   case 4:{
168       return 2;
169     }
170   default:
171     return -1;
172   }
173 }
174
175 static void MC_modelcheck_liveness_main(void);
176
177 static void MC_pre_modelcheck_liveness(void)
178 {
179   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
180
181   mc_pair_t initial_pair = NULL;
182   smx_process_t process;
183
184   // TODO, fix this
185   MC_wait_for_requests();
186   MC_wait_for_requests();
187
188   MC_SET_MC_HEAP;
189
190   acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
191   if(_sg_mc_visited > 0)
192     visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
193
194   initial_global_state->snapshot = MC_take_snapshot(0);
195   initial_global_state->prev_pair = 0;
196
197   unsigned int cursor = 0;
198   xbt_automaton_state_t automaton_state;
199   
200   xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
201     if (automaton_state->type == -1) {  /* Initial automaton state */
202
203       initial_pair = MC_pair_new();
204       initial_pair->automaton_state = automaton_state;
205       initial_pair->graph_state = MC_state_new();
206       initial_pair->atomic_propositions = get_atomic_propositions_values();
207       initial_pair->depth = 1;
208
209       /* Get enabled processes and insert them in the interleave set of the graph_state */
210       MC_EACH_SIMIX_PROCESS(process,
211         if (MC_process_is_enabled(process)) {
212           MC_state_interleave_process(initial_pair->graph_state, process);
213         }
214       );
215
216       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
217       initial_pair->search_cycle = 0;
218
219       xbt_fifo_unshift(mc_stack, initial_pair);
220     }
221   }
222
223   MC_SET_STD_HEAP;
224   
225   MC_modelcheck_liveness_main();
226
227   if (initial_global_state->raw_mem_set)
228     MC_SET_MC_HEAP;
229 }
230
231 static void MC_modelcheck_liveness_main(void)
232 {
233   smx_process_t process = NULL;
234   mc_pair_t current_pair = NULL;
235   int value, res, visited_num = -1;
236   smx_simcall_t req = NULL;
237   xbt_automaton_transition_t transition_succ = NULL;
238   int cursor = 0;
239   mc_pair_t next_pair = NULL;
240   xbt_dynar_t prop_values = NULL;
241   mc_visited_pair_t reached_pair = NULL;
242   
243   while(xbt_fifo_size(mc_stack) > 0){
244
245     /* Get current pair */
246     current_pair = (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
247
248     /* Update current state in buchi automaton */
249     _mc_property_automaton->current_state = current_pair->automaton_state;
250
251     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
252        current_pair->depth, current_pair->search_cycle,
253        MC_state_interleave_size(current_pair->graph_state), current_pair->num,
254        current_pair->requests);
255
256     if (current_pair->requests > 0) {
257
258       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
259         /* If new acceptance pair, return new pair */
260         if ((reached_pair = is_reached_acceptance_pair(current_pair)) == NULL) {
261           int counter_example_depth = current_pair->depth;
262           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
263           XBT_INFO("|             ACCEPTANCE CYCLE            |");
264           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
265           XBT_INFO("Counter-example that violates formula :");
266           MC_show_stack_liveness(mc_stack);
267           MC_dump_stack_liveness(mc_stack);
268           MC_print_statistics(mc_stats);
269           XBT_INFO("Counter-example depth : %d", counter_example_depth);
270           xbt_abort();
271         }
272       }
273
274       /* Pair already visited ? stop the exploration on the current path */
275       if ((current_pair->exploration_started == 0) && (visited_num = is_visited_pair(reached_pair, current_pair)) != -1) {
276
277         if (dot_output != NULL){
278           MC_SET_MC_HEAP;
279           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
280           fflush(dot_output);
281           MC_SET_STD_HEAP;
282         }
283
284         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
285         MC_SET_MC_HEAP;
286         current_pair->requests = 0;
287         MC_SET_STD_HEAP;
288         goto backtracking;
289         
290       }else{
291
292         req = MC_state_get_request(current_pair->graph_state, &value);
293
294          if (dot_output != NULL) {
295            MC_SET_MC_HEAP;
296            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
297              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
298              xbt_free(initial_global_state->prev_req);
299            }
300            initial_global_state->prev_pair = current_pair->num;
301            initial_global_state->prev_req = MC_request_get_dot_output(req, value);
302            if (current_pair->search_cycle)
303              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
304            fflush(dot_output);
305            MC_SET_STD_HEAP;
306          }
307
308          char* req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX); 
309          XBT_DEBUG("Execute: %s", req_str);
310          xbt_free(req_str);
311
312          /* Set request as executed */
313          MC_state_set_executed_request(current_pair->graph_state, req, value);
314
315          /* Update mc_stats */
316          mc_stats->executed_transitions++;
317          if(current_pair->exploration_started == 0)
318            mc_stats->visited_pairs++;
319
320          /* Answer the request */
321          MC_simcall_handle(req, value);
322          
323          /* Wait for requests (schedules processes) */
324          MC_wait_for_requests();
325
326          MC_SET_MC_HEAP;
327
328          current_pair->requests--;
329          current_pair->exploration_started = 1;
330
331          /* Get values of atomic propositions (variables used in the property formula) */ 
332          prop_values = get_atomic_propositions_values();
333
334          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
335          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
336          while (cursor >= 0) {
337            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
338            res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
339            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
340               next_pair = MC_pair_new();
341               next_pair->graph_state = MC_state_new();
342               next_pair->automaton_state = transition_succ->dst;
343               next_pair->atomic_propositions = get_atomic_propositions_values();
344               next_pair->depth = current_pair->depth + 1;
345               /* Get enabled processes and insert them in the interleave set of the next graph_state */
346               MC_EACH_SIMIX_PROCESS(process,
347                 if (MC_process_is_enabled(process)) {
348                   MC_state_interleave_process(next_pair->graph_state, process);
349                 }
350               );
351
352               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
353
354               /* FIXME : get search_cycle value for each acceptant state */
355               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
356                 next_pair->search_cycle = 1;
357
358               /* Add new pair to the exploration stack */
359               xbt_fifo_unshift(mc_stack, next_pair);
360
361            }
362            cursor--;
363          }
364          
365          MC_SET_STD_HEAP;
366         
367       } /* End of visited_pair test */
368       
369     } else {
370
371     backtracking:
372       if(visited_num == -1)
373         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
374     
375       MC_SET_MC_HEAP;
376     
377       xbt_dynar_free(&prop_values);
378     
379       /* Traverse the stack backwards until a pair with a non empty interleave
380          set is found, deleting all the pairs that have it empty in the way. */
381       while ((current_pair = xbt_fifo_shift(mc_stack)) != NULL) {
382         if (current_pair->requests > 0) {
383           /* We found a backtracking point */
384           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
385           xbt_fifo_unshift(mc_stack, current_pair);
386           MC_SET_STD_HEAP;
387           MC_replay_liveness(mc_stack);
388           XBT_DEBUG("Backtracking done");
389           break;
390         }else{
391           /* Delete pair */
392           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
393           if (current_pair->automaton_state->type == 1) 
394             remove_acceptance_pair(current_pair->num);
395           MC_pair_delete(current_pair);
396         }
397       }
398     
399       MC_SET_STD_HEAP;
400     } /* End of if (current_pair->requests > 0) else ... */
401     
402   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
403   
404 }
405
406 void MC_modelcheck_liveness(void)
407 {
408   XBT_DEBUG("Starting the liveness algorithm");
409   xbt_assert(mc_mode == MC_MODE_SERVER);
410   _sg_mc_liveness = 1;
411
412   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
413
414   /* Create exploration stack */
415   mc_stack = xbt_fifo_new();
416
417   /* Create the initial state */
418   initial_global_state = xbt_new0(s_mc_global_t, 1);
419
420   MC_SET_STD_HEAP;
421
422   MC_pre_modelcheck_liveness();
423
424   /* We're done */
425   MC_print_statistics(mc_stats);
426   xbt_free(mc_time);
427
428   mmalloc_set_current_heap(heap);
429
430 }