Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1e70dcd4a396f1824acf84f1eae1c05907a47c54
[simgrid.git] / src / mc / mc_liveness.c
1 #include "private.h"
2 #include "unistd.h"
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
5                                 "Logging specific to algorithms for liveness properties verification");
6
7 xbt_dynar_t initial_pairs = NULL;
8 xbt_fifo_t reached_pairs;
9 xbt_dynar_t successors = NULL;
10 extern mc_snapshot_t initial_snapshot;
11
12 /* Global variables for stateless algorithm */
13 mc_snapshot_t snapshot = NULL;
14
15 /* Global variables for stateful algorithm */
16 mc_snapshot_t next_snapshot = NULL;
17 mc_snapshot_t current_snapshot = NULL;
18
19
20 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
21
22   XBT_DEBUG("Compare snapshot");
23   
24   if(s1->num_reg != s2->num_reg)
25     return 1;
26
27   int i;
28   int errors = 0;
29
30   for(i=0 ; i< s1->num_reg ; i++){
31     
32     if(s1->regions[i]->size != s2->regions[i]->size)
33       return 1;
34     
35     if(s1->regions[i]->start_addr != s2->regions[i]->start_addr)
36       return 1;
37     
38     if(s1->regions[i]->type != s2->regions[i]->type)
39       return 1;
40
41     //XBT_DEBUG("Size of region : %Zu", s1->regions[i]->size);
42
43     if(s1->regions[i]->type == 0){ 
44       if(mmalloc_compare_heap(s1->regions[i]->start_addr, s2->regions[i]->start_addr)){
45         XBT_DEBUG("Different heap (mmalloc_compare)");
46         //sleep(1);
47         errors++; 
48       }
49     }else{
50       if(memcmp(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
51         XBT_DEBUG("Different memcmp for data in libsimgrid or program");
52         //sleep(1);
53         errors++;
54       }
55     }
56     
57     
58   }
59
60   return (errors>0);
61
62 }
63
64 int reached(xbt_automaton_t a, xbt_state_t st, mc_snapshot_t s){
65
66
67   if(xbt_fifo_size(reached_pairs) == 0){
68     return 0;
69   }else{
70     MC_SET_RAW_MEM;
71     
72     xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
73
74     /* Get values of propositional symbols */
75     unsigned int cursor = 0;
76     xbt_propositional_symbol_t ps = NULL;
77     xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
78       int (*f)() = ps->function;
79       int res = (*f)();
80       xbt_dynar_push_as(prop_ato, int, res);
81     }
82     
83     mc_pair_reached_t pair_test;
84     xbt_fifo_item_t item = xbt_fifo_get_last_item(reached_pairs);
85     int i=0;
86
87     while(i< xbt_fifo_size(reached_pairs)){
88
89       pair_test = (mc_pair_reached_t) xbt_fifo_get_item_content(item);
90       
91       if(automaton_state_compare(pair_test->automaton_state, st) == 0){
92         if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
93           if(snapshot_compare(pair_test->system_state, s) == 0){
94             MC_UNSET_RAW_MEM;
95             return 1;
96           }
97         }
98       }
99
100       item = xbt_fifo_get_prev_item(item);
101
102     }
103
104     MC_UNSET_RAW_MEM;
105     return 0;
106     
107   }
108 }
109
110 void set_pair_reached(xbt_automaton_t a, xbt_state_t st, mc_snapshot_t sn){
111
112  
113   MC_SET_RAW_MEM;
114   
115   mc_pair_reached_t pair = NULL;
116   pair = xbt_new0(s_mc_pair_reached_t, 1);
117   pair->automaton_state = st;
118   pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
119   pair->system_state = sn;
120   
121   /* Get values of propositional symbols */
122   unsigned int cursor = 0;
123   xbt_propositional_symbol_t ps = NULL;
124   xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
125     int (*f)() = ps->function;
126     int res = (*f)();
127     xbt_dynar_push_as(pair->prop_ato, int, res);
128   }
129   
130   xbt_fifo_unshift(reached_pairs, pair); 
131   
132   MC_UNSET_RAW_MEM;
133   
134   
135 }
136
137 void MC_pair_delete(mc_pair_t pair){
138   xbt_free(pair->graph_state->proc_status);
139   xbt_free(pair->graph_state);
140   //xbt_free(pair->automaton_state); -> FIXME : à implémenter
141   xbt_free(pair);
142 }
143
144
145
146 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
147   
148   switch(l->type){
149   case 0 : {
150     int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
151     int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
152     return (left_res || right_res);
153     break;
154   }
155   case 1 : {
156     int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
157     int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
158     return (left_res && right_res);
159     break;
160   }
161   case 2 : {
162     int res = MC_automaton_evaluate_label(a, l->u.exp_not);
163     return (!res);
164     break;
165   }
166   case 3 : { 
167     unsigned int cursor = 0;
168     xbt_propositional_symbol_t p = NULL;
169     xbt_dynar_foreach(a->propositional_symbols, cursor, p){
170       if(strcmp(p->pred, l->u.predicat) == 0){
171         int (*f)() = p->function;
172         return (*f)();
173       }
174     }
175     return -1;
176     break;
177   }
178   case 4 : {
179     return 2;
180     break;
181   }
182   default : 
183     return -1;
184   }
185 }
186
187
188
189
190
191 /********************* Double-DFS stateless *******************/
192
193 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
194   xbt_free(pair->graph_state->proc_status);
195   xbt_free(pair->graph_state);
196   //xbt_free(pair->automaton_state); -> FIXME : à implémenter
197   xbt_free(pair);
198 }
199
200 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
201   mc_pair_stateless_t p = NULL;
202   p = xbt_new0(s_mc_pair_stateless_t, 1);
203   p->automaton_state = st;
204   p->graph_state = sg;
205   p->requests = r;
206   mc_stats_pair->expanded_pairs++;
207   return p;
208 }
209
210
211
212 void MC_ddfs_stateless_init(xbt_automaton_t a, char *prgm){
213
214   XBT_DEBUG("**************************************************");
215   XBT_DEBUG("Double-DFS stateless init");
216   XBT_DEBUG("**************************************************");
217  
218   mc_pair_stateless_t mc_initial_pair = NULL;
219   mc_state_t initial_graph_state = NULL;
220   smx_process_t process; 
221  
222   MC_wait_for_requests();
223
224   MC_SET_RAW_MEM;
225
226   initial_graph_state = MC_state_pair_new();
227   xbt_swag_foreach(process, simix_global->process_list){
228     if(MC_process_is_enabled(process)){
229       MC_state_interleave_process(initial_graph_state, process);
230     }
231   }
232
233   reached_pairs = xbt_fifo_new(); 
234   successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
235   snapshot = xbt_new0(s_mc_snapshot_t, 1);
236
237   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
238   MC_take_snapshot_liveness(initial_snapshot, prgm);
239
240   MC_UNSET_RAW_MEM; 
241
242   unsigned int cursor = 0;
243   xbt_state_t state;
244
245   xbt_dynar_foreach(a->states, cursor, state){
246     if(state->type == -1){
247       
248       MC_SET_RAW_MEM;
249       mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
250       xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
251       MC_UNSET_RAW_MEM;
252       
253       if(cursor != 0){
254         MC_restore_snapshot(initial_snapshot);
255         MC_UNSET_RAW_MEM;
256       }
257
258       MC_ddfs_stateless(a, 0, 0, prgm);
259
260     }else{
261       if(state->type == 2){
262       
263         MC_SET_RAW_MEM;
264         mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
265         xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
266         MC_UNSET_RAW_MEM;
267
268         set_pair_reached(a, state, initial_snapshot);
269         
270         if(cursor != 0){
271           MC_restore_snapshot(initial_snapshot);
272           MC_UNSET_RAW_MEM;
273         }
274         
275         MC_ddfs_stateless(a, 1, 0, prgm);
276         
277       }
278     }
279   } 
280
281 }
282
283
284 void MC_ddfs_stateless(xbt_automaton_t a, int search_cycle, int replay, char *prgm){
285
286   smx_process_t process;
287   mc_pair_stateless_t current_pair = NULL;
288
289   if(xbt_fifo_size(mc_stack_liveness_stateless) == 0)
290     return;
291
292   if(replay == 1){
293     MC_replay_liveness(mc_stack_liveness_stateless, 0);
294     current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
295     xbt_swag_foreach(process, simix_global->process_list){
296       if(MC_process_is_enabled(process)){
297         MC_state_interleave_process(current_pair->graph_state, process);
298       }
299     }
300   }
301
302   /* Get current pair */
303   current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
304
305   /* Update current state in buchi automaton */
306   a->current_state = current_pair->automaton_state;
307
308  
309   XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateless), search_cycle);
310   XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id, MC_state_interleave_size(current_pair->graph_state));
311  
312   mc_stats_pair->visited_pairs++;
313
314   int value;
315   mc_state_t next_graph_state = NULL;
316   smx_req_t req = NULL;
317   char *req_str;
318
319   xbt_transition_t transition_succ;
320   unsigned int cursor = 0;
321   int res;
322
323   mc_pair_stateless_t next_pair = NULL;
324   mc_pair_stateless_t pair_succ;
325
326   if(xbt_fifo_size(mc_stack_liveness_stateless) < MAX_DEPTH_LIVENESS){
327
328     if(current_pair->requests > 0){
329
330       while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
331    
332         /* Debug information */
333         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
334           req_str = MC_request_to_string(req, value);
335           XBT_DEBUG("Execute: %s", req_str);
336           xbt_free(req_str);
337         }
338
339         //sleep(1);
340
341         MC_state_set_executed_request(current_pair->graph_state, req, value);   
342     
343         /* Answer the request */
344         SIMIX_request_pre(req, value);
345
346         /* Wait for requests (schedules processes) */
347         MC_wait_for_requests();
348
349         unsigned int curs = 0;
350         xbt_propositional_symbol_t p = NULL;
351         xbt_dynar_foreach(a->propositional_symbols, curs, p){
352           int (*f)() = p->function;
353           int res = (*f)();
354           if(strcmp(p->pred, "p") == 0){
355             XBT_DEBUG("p=%d",res);
356           }else{
357             XBT_DEBUG("q=%d",res);
358           }
359         }
360   
361
362
363         MC_SET_RAW_MEM;
364
365         /* Create the new expanded graph_state */
366         next_graph_state = MC_state_pair_new();
367
368         /* Get enabled process and insert it in the interleave set of the next graph_state */
369         xbt_swag_foreach(process, simix_global->process_list){
370           if(MC_process_is_enabled(process)){
371             MC_state_interleave_process(next_graph_state, process);
372           }
373         }
374
375         xbt_dynar_reset(successors);
376
377         MC_UNSET_RAW_MEM;
378
379
380         cursor= 0;
381         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
382
383           res = MC_automaton_evaluate_label(a, transition_succ->label);
384
385           if(res == 1){ // enabled transition in automaton
386             MC_SET_RAW_MEM;
387             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
388             xbt_dynar_push(successors, &next_pair);
389             MC_UNSET_RAW_MEM;
390           }
391
392         }
393
394         cursor = 0;
395    
396         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
397       
398           res = MC_automaton_evaluate_label(a, transition_succ->label);
399         
400           if(res == 2){ // true transition in automaton
401             MC_SET_RAW_MEM;
402             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
403             xbt_dynar_push(successors, &next_pair);
404             MC_UNSET_RAW_MEM;
405           }
406
407         }
408
409    
410         if(xbt_dynar_length(successors) == 0){
411           MC_SET_RAW_MEM;
412           next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state, MC_state_interleave_size(next_graph_state));
413           xbt_dynar_push(successors, &next_pair);
414           MC_UNSET_RAW_MEM;
415         }
416
417         /*MC_SET_RAW_MEM;
418           MC_take_snapshot(snapshot);
419           MC_UNSET_RAW_MEM;*/
420
421         cursor = 0; 
422
423         XBT_DEBUG("Successors length : %lu", xbt_dynar_length(successors));
424         //sleep(1);
425       
426         xbt_dynar_foreach(successors, cursor, pair_succ){
427
428           if(search_cycle == 1){
429
430             if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){ 
431
432               MC_SET_RAW_MEM;
433               MC_take_snapshot_liveness(snapshot, prgm);
434               MC_UNSET_RAW_MEM;
435         
436               if(reached(a, pair_succ->automaton_state, snapshot) == 1){
437
438                 XBT_DEBUG("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness_stateless) + 1);
439
440                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
441                 XBT_INFO("|             ACCEPTANCE CYCLE            |");
442                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
443                 XBT_INFO("Counter-example that violates formula :");
444                 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
445                 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
446                 MC_print_statistics_pairs(mc_stats_pair);
447                 exit(0);
448
449               }else{
450
451                 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
452               
453                 set_pair_reached(a, pair_succ->automaton_state, snapshot); 
454
455               }
456
457             }
458
459           }else{
460           
461             if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
462
463               XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
464
465               MC_SET_RAW_MEM;
466               MC_take_snapshot_liveness(snapshot, prgm);
467               MC_UNSET_RAW_MEM;
468             
469               set_pair_reached(a, pair_succ->automaton_state, snapshot); 
470
471               search_cycle = 1;
472
473             }
474
475           }
476
477           MC_SET_RAW_MEM;
478           xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
479           MC_UNSET_RAW_MEM;
480
481           MC_ddfs_stateless(a, search_cycle, 0, prgm);
482
483           /* Restore system before checking others successors */
484           if(cursor != (xbt_dynar_length(successors) - 1))
485             MC_replay_liveness(mc_stack_liveness_stateless, 1);
486         
487         }
488
489         if(MC_state_interleave_size(current_pair->graph_state) > 0){
490           XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
491           MC_replay_liveness(mc_stack_liveness_stateless, 0);
492         }
493       }
494
495  
496     }else{  /*No request to execute, search evolution in Büchi automaton */
497
498       MC_SET_RAW_MEM;
499
500       /* Create the new expanded graph_state */
501       next_graph_state = MC_state_pair_new();
502
503       xbt_dynar_reset(successors);
504
505       MC_UNSET_RAW_MEM;
506
507
508       cursor= 0;
509       xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
510
511         res = MC_automaton_evaluate_label(a, transition_succ->label);
512
513         if(res == 1){ // enabled transition in automaton
514           MC_SET_RAW_MEM;
515           next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
516           xbt_dynar_push(successors, &next_pair);
517           MC_UNSET_RAW_MEM;
518         }
519
520       }
521
522       cursor = 0;
523    
524       xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
525       
526         res = MC_automaton_evaluate_label(a, transition_succ->label);
527         
528         if(res == 2){ // true transition in automaton
529           MC_SET_RAW_MEM;
530           next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
531           xbt_dynar_push(successors, &next_pair);
532           MC_UNSET_RAW_MEM;
533         }
534
535       }
536
537    
538       if(xbt_dynar_length(successors) == 0){
539         MC_SET_RAW_MEM;
540         next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state, MC_state_interleave_size(next_graph_state));
541         xbt_dynar_push(successors, &next_pair);
542         MC_UNSET_RAW_MEM;
543       }
544
545       cursor = 0; 
546
547       xbt_dynar_foreach(successors, cursor, pair_succ){
548
549         if(search_cycle == 1){
550
551           if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){ 
552
553             MC_SET_RAW_MEM;
554             MC_take_snapshot_liveness(snapshot, prgm);
555             MC_UNSET_RAW_MEM;
556         
557             if(reached(a, pair_succ->automaton_state, snapshot) == 1){
558
559               XBT_DEBUG("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness_stateless) + 1);
560
561               XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
562               XBT_INFO("|             ACCEPTANCE CYCLE            |");
563               XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
564               XBT_INFO("Counter-example that violates formula :");
565               MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
566               MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
567               MC_print_statistics_pairs(mc_stats_pair);
568               exit(0);
569
570             }else{
571
572               XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
573               
574               set_pair_reached(a, pair_succ->automaton_state, snapshot); 
575
576             }
577
578           }
579
580         }else{
581           
582           if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) && (xbt_fifo_size(mc_stack_liveness_stateless) < (MAX_DEPTH_LIVENESS - 1))){
583
584             MC_SET_RAW_MEM;
585             MC_take_snapshot_liveness(snapshot, prgm);
586             MC_UNSET_RAW_MEM;
587             
588             set_pair_reached(a, pair_succ->automaton_state, snapshot); 
589
590             search_cycle = 1;
591
592           }
593
594         }
595
596         MC_SET_RAW_MEM;
597         xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
598         MC_UNSET_RAW_MEM;
599
600         MC_ddfs_stateless(a, search_cycle, 0, prgm);
601
602         /* Restore system before checking others successors */
603         if(cursor != xbt_dynar_length(successors) - 1)
604           MC_replay_liveness(mc_stack_liveness_stateless, 1);
605
606       }
607      
608     }
609   }
610
611   if(xbt_fifo_size(mc_stack_liveness_stateless) == MAX_DEPTH_LIVENESS ){
612     XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack, maximum depth reached", current_pair->graph_state, current_pair->automaton_state, search_cycle);
613   }else{
614     XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);
615   }
616
617   
618   MC_SET_RAW_MEM;
619   xbt_fifo_shift(mc_stack_liveness_stateless);
620   if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2))
621     xbt_fifo_shift(reached_pairs);
622   MC_UNSET_RAW_MEM;
623
624 }
625