Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
99ca5be7b3b3e62d0c3d6b3943b4c8c2240a6720
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2008-2012 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 #include <unistd.h>
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
10                                 "Logging specific to algorithms for liveness properties verification");
11
12 xbt_dynar_t reached_pairs;
13 xbt_dynar_t reached_pairs_hash;
14 xbt_dynar_t visited_pairs;
15 xbt_dynar_t visited_pairs_hash;
16 xbt_dynar_t successors;
17
18 xbt_dynar_t hosts_table;
19
20
21 /* fast implementation of djb2 algorithm */
22 unsigned int hash_region(char *str, int str_len){
23
24   int c;
25   register unsigned int hash = 5381;
26
27   while (str_len--) {
28     c = *str++;
29     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
30   }
31
32   return hash;
33
34 }
35
36 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
37
38   
39   if(s1->num_reg != s2->num_reg){
40     XBT_DEBUG("Different num_reg (s1 = %u, s2 = %u)", s1->num_reg, s2->num_reg);
41     return 1;
42   }
43
44   int i;
45   int errors = 0;
46
47   for(i=0 ; i< s1->num_reg ; i++){
48
49     if(s1->regions[i]->type != s2->regions[i]->type){
50       if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
51         XBT_DEBUG("Different type of region");
52         errors++;
53       }else{
54         return 1;
55       }
56     }
57
58     switch(s1->regions[i]->type){
59       case 0:
60       if(s1->regions[i]->size != s2->regions[i]->size){
61         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
62           XBT_DEBUG("Different size of heap (s1 = %zu, s2 = %zu)", s1->regions[i]->size, s2->regions[i]->size);
63           errors++;
64         }else{
65           return 1;
66         }
67       }
68       if(s1->regions[i]->start_addr != s2->regions[i]->start_addr){
69         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
70           XBT_DEBUG("Different start addr of heap (s1 = %p, s2 = %p)", s1->regions[i]->start_addr, s2->regions[i]->start_addr);
71           errors++;
72         }else{
73           return 1;
74         }
75       }
76       if(mmalloc_compare_heap(s1->regions[i]->data, s2->regions[i]->data)){
77         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
78           XBT_DEBUG("Different heap (mmalloc_compare)");
79           errors++; 
80         }else{
81           return 1;
82         }
83       }
84       break;
85       /*case 1 :
86       if(s1->regions[i]->size != s2->regions[i]->size){
87         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
88           XBT_DEBUG("Different size of libsimgrid (s1 = %zu, s2 = %zu)", s1->regions[i]->size, s2->regions[i]->size);
89           errors++;
90         }else{
91           return 1;
92         }
93       }
94       if(s1->regions[i]->start_addr != s2->regions[i]->start_addr){
95         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
96           XBT_DEBUG("Different start addr of libsimgrid (s1 = %p, s2 = %p)", s1->regions[i]->start_addr, s2->regions[i]->start_addr);
97           errors++;
98         }else{
99           return 1;
100         }
101       }
102       if(memcmp(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
103         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
104           XBT_DEBUG("Different memcmp for data in libsimgrid");
105           XBT_DEBUG("Size : %zu", s1->regions[i]->size);
106           errors++;
107         }else{
108           return 1;
109         }
110       }
111       break;*/
112     default:
113       break;
114     }
115   }
116
117   return (errors > 0);
118   
119   
120 }
121
122 int reached(xbt_state_t st){
123
124
125   if(xbt_dynar_is_empty(reached_pairs)){
126
127     return 0;
128
129   }else{
130
131     MC_SET_RAW_MEM;
132
133     mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
134     MC_take_snapshot_liveness(sn);    
135     
136     xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
137     int res;
138     int (*f)();
139
140     /* Get values of propositional symbols */
141     unsigned int cursor = 0;
142     xbt_propositional_symbol_t ps = NULL;
143     xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
144       f = ps->function;
145       res = (*f)();
146       xbt_dynar_push_as(prop_ato, int, res);
147     }
148
149     cursor = 0;
150     mc_pair_reached_t pair_test = NULL;
151
152     //xbt_dict_t current_rdv_points = SIMIX_get_rdv_points();
153      
154     xbt_dynar_foreach(reached_pairs, cursor, pair_test){
155       XBT_DEBUG("Pair reached #%u", cursor+1);
156       if(automaton_state_compare(pair_test->automaton_state, st) == 0){
157         if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
158           //XBT_DEBUG("Rdv points size %d - %d", xbt_dict_length(pair_test->rdv_points), xbt_dict_length(current_rdv_points));
159           //if(xbt_dict_length(pair_test->rdv_points) == xbt_dict_length(current_rdv_points)){
160           //if(rdv_points_compare(pair_test->rdv_points, current_rdv_points) == 0){
161               if(snapshot_compare(pair_test->system_state, sn) == 0){
162                 MC_free_snapshot(sn);
163                 xbt_dynar_reset(prop_ato);
164                 xbt_free(prop_ato);
165                 MC_UNSET_RAW_MEM;
166                 return 1;
167               }
168               /* }
169           }else{
170             XBT_DEBUG("Different size of rdv points (%d - %d)",xbt_dict_length(pair_test->rdv_points), xbt_dict_length(current_rdv_points) );
171             }*/
172         }else{
173           XBT_DEBUG("Different values of propositional symbols");
174         }
175       }else{
176         XBT_DEBUG("Different automaton state");
177       }
178     }
179
180     MC_free_snapshot(sn);
181     xbt_dynar_reset(prop_ato);
182     xbt_free(prop_ato);
183     MC_UNSET_RAW_MEM;
184     return 0;
185     
186   }
187 }
188
189 int rdv_points_compare(xbt_dict_t d1, xbt_dict_t d2){ /* d1 = pair_test, d2 = current_pair */ 
190   
191    xbt_dict_cursor_t cursor_dict = NULL;
192    char *key;
193    char *data;
194    smx_rdv_t rdv1, rdv2;
195    xbt_fifo_item_t item1, item2;
196    smx_action_t action1, action2;
197    xbt_fifo_item_t item_req1, item_req2;
198    smx_simcall_t req1, req2;
199    int i=0;
200    int j=0;
201
202    xbt_dict_foreach(d1, cursor_dict, key, data){
203      rdv1 = (smx_rdv_t)data;
204      rdv2 = xbt_dict_get_or_null(d2, rdv1->name);
205      if(rdv2 == NULL){
206        XBT_DEBUG("Rdv point unknown");
207        return 1;
208      }else{
209        if(xbt_fifo_size(rdv1->comm_fifo) != xbt_fifo_size(rdv2->comm_fifo)){
210          XBT_DEBUG("Different total of actions in mailbox \"%s\" (%d - %d)", rdv1->name, xbt_fifo_size(rdv1->comm_fifo),xbt_fifo_size(rdv2->comm_fifo) );
211          return 1;
212        }else{
213          
214          XBT_DEBUG("Total of actions in mailbox \"%s\" : %d", rdv1->name, xbt_fifo_size(rdv1->comm_fifo)); 
215          
216          item1 = xbt_fifo_get_first_item(rdv1->comm_fifo);      
217          item2 = xbt_fifo_get_first_item(rdv2->comm_fifo);
218
219          while(i<xbt_fifo_size(rdv1->comm_fifo)){
220            action1 = (smx_action_t) xbt_fifo_get_item_content(item1);
221            action2 = (smx_action_t) xbt_fifo_get_item_content(item2);
222
223            if(action1->type != action2->type){
224              XBT_DEBUG("Different type of action");
225              return 1;
226            }
227
228            if(action1->state != action2->state){
229              XBT_DEBUG("Different state of action");
230              return 1;
231            }
232
233            if(xbt_fifo_size(action1->simcalls) != xbt_fifo_size(action2->simcalls)){
234              XBT_DEBUG("Different size of simcall list (%d - %d", xbt_fifo_size(action1->simcalls), xbt_fifo_size(action2->simcalls));
235              return 1;
236            }else{
237
238              item_req1 = xbt_fifo_get_first_item(action1->simcalls);    
239              item_req2 = xbt_fifo_get_first_item(action2->simcalls);
240
241              while(j<xbt_fifo_size(action1->simcalls)){
242
243                req1 = (smx_simcall_t) xbt_fifo_get_item_content(item_req1);
244                req2 = (smx_simcall_t) xbt_fifo_get_item_content(item_req2);
245                
246                if(req1->call != req2->call){
247                  XBT_DEBUG("Different simcall call in simcalls of action (%d - %d)", (int)req1->call, (int)req2->call);
248                  return 1;
249                }
250                if(req1->issuer->pid != req2->issuer->pid){
251                  XBT_DEBUG("Different simcall issuer in simcalls of action (%lu- %lu)", req1->issuer->pid, req2->issuer->pid);
252                  return 1;
253                }
254
255                item_req1 = xbt_fifo_get_next_item(item_req1);   
256                item_req2 = xbt_fifo_get_next_item(item_req2);
257                j++;
258                
259              }
260            }
261
262            switch(action1->type){
263            case 0: /* execution */
264            case 1: /* parallel execution */
265              if(strcmp(action1->execution.host->name, action2->execution.host->name) != 0)
266                return 1;
267              break;
268            case 2: /* comm */
269              if(action1->comm.type != action2->comm.type)
270                return 1;
271              //XBT_DEBUG("Type of comm : %d", action1->comm.type);
272              
273              switch(action1->comm.type){
274              case 0: /* SEND */
275                if(action1->comm.src_proc->pid != action2->comm.src_proc->pid)
276                  return 1;
277                if(strcmp(action1->comm.src_proc->smx_host->name, action2->comm.src_proc->smx_host->name) != 0)
278                  return 1;
279                break;
280              case 1: /* RECEIVE */
281                if(action1->comm.dst_proc->pid != action2->comm.dst_proc->pid)
282                  return 1;
283                if(strcmp(action1->comm.dst_proc->smx_host->name, action2->comm.dst_proc->smx_host->name) != 0)
284                  return 1;
285                break;
286              case 2: /* READY */
287                if(action1->comm.src_proc->pid != action2->comm.src_proc->pid)
288                  return 1;
289                if(strcmp(action1->comm.src_proc->smx_host->name, action2->comm.src_proc->smx_host->name) != 0)
290                  return 1;
291                if(action1->comm.dst_proc->pid != action2->comm.dst_proc->pid)
292                  return 1;
293                if(strcmp(action1->comm.dst_proc->smx_host->name, action2->comm.dst_proc->smx_host->name) != 0)
294                  return 1;
295                break;
296              case 3: /* DONE */
297                if(action1->comm.src_proc->pid != action2->comm.src_proc->pid)
298                  return 1;
299                if(strcmp(action1->comm.src_proc->smx_host->name, action2->comm.src_proc->smx_host->name) != 0)
300                  return 1;
301                if(action1->comm.dst_proc->pid != action2->comm.dst_proc->pid)
302                  return 1;
303                if(strcmp(action1->comm.dst_proc->smx_host->name, action2->comm.dst_proc->smx_host->name) != 0)
304                  return 1;
305                break;
306                
307              } /* end of switch on type of comm */
308              
309              if(action1->comm.refcount != action2->comm.refcount)
310                return 1;
311              if(action1->comm.detached != action2->comm.detached)
312                return 1;
313              if(action1->comm.rate != action2->comm.rate)
314                return 1;
315              if(action1->comm.task_size != action2->comm.task_size)
316                return 1;
317              if(action1->comm.src_buff != action2->comm.src_buff)
318                return 1;
319              if(action1->comm.dst_buff != action2->comm.dst_buff)
320                return 1;
321              if(action1->comm.src_data != action2->comm.src_data)
322                return 1;
323              if(action1->comm.dst_data != action2->comm.dst_data)
324                return 1;
325              
326              break;
327            case 3: /* sleep */
328              if(strcmp(action1->sleep.host->name, action2->sleep.host->name) != 0)
329                return 1;
330              break;
331            case 4: /* synchro */
332              
333              break;
334            default:
335              break;
336            }
337
338            item1 = xbt_fifo_get_next_item(item1);       
339            item2 = xbt_fifo_get_next_item(item2);
340            i++;
341          }
342        }
343      }
344    }
345
346    return 0;
347    
348 }
349
350 void set_pair_reached(xbt_state_t st){
351
352  
353   MC_SET_RAW_MEM;
354   
355   mc_pair_reached_t pair = NULL;
356   pair = xbt_new0(s_mc_pair_reached_t, 1);
357   pair->automaton_state = st;
358   pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
359   pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
360   //pair->rdv_points = xbt_dict_new();  
361   MC_take_snapshot_liveness(pair->system_state);
362
363   /* Get values of propositional symbols */
364   unsigned int cursor = 0;
365   xbt_propositional_symbol_t ps = NULL;
366   int res;
367   int (*f)(); 
368
369   xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
370     f = ps->function;
371     res = (*f)();
372     xbt_dynar_push_as(pair->prop_ato, int, res);
373   }
374
375   /*xbt_dict_t rdv_points = SIMIX_get_rdv_points();
376
377   xbt_dict_cursor_t cursor_dict = NULL;
378   char *key;
379   char *data;
380   xbt_fifo_item_t item;
381   smx_action_t action;
382
383   xbt_dict_foreach(rdv_points, cursor_dict, key, data){
384     smx_rdv_t new_rdv = xbt_new0(s_smx_rvpoint_t, 1);
385     new_rdv->name = strdup(((smx_rdv_t)data)->name);
386     new_rdv->comm_fifo = xbt_fifo_new();
387     xbt_fifo_foreach(((smx_rdv_t)data)->comm_fifo, item, action, smx_action_t) {
388       smx_action_t a = xbt_new0(s_smx_action_t, 1);
389       memcpy(a, action, sizeof(s_smx_action_t));
390       xbt_fifo_push(new_rdv->comm_fifo, a);
391       XBT_DEBUG("New action (type = %d, state = %d) in mailbox \"%s\"", action->type, action->state, key);
392       if(action->type==2)
393         XBT_DEBUG("Type of communication : %d, Ref count = %d", action->comm.type, action->comm.refcount);
394     }
395     //new_rdv->comm_fifo = xbt_fifo_copy(((smx_rdv_t)data)->comm_fifo);
396     xbt_dict_set(pair->rdv_points, new_rdv->name, new_rdv, NULL);
397     }*/
398  
399   xbt_dynar_push(reached_pairs, &pair); 
400
401   MC_UNSET_RAW_MEM;
402   
403 }
404
405
406 int reached_hash(xbt_state_t st){
407
408
409   if(xbt_dynar_is_empty(reached_pairs_hash)){
410
411     return 0;
412
413   }else{
414
415     MC_SET_RAW_MEM;
416
417     mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
418     MC_take_snapshot_liveness(sn);
419
420     int j;
421     unsigned int hash_regions[sn->num_reg];
422     for(j=0; j<sn->num_reg; j++){
423       hash_regions[j] = hash_region(sn->regions[j]->data, sn->regions[j]->size);
424     }
425
426
427     /* Get values of propositional symbols */
428     xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
429     unsigned int cursor = 0;
430     xbt_propositional_symbol_t ps = NULL;
431     int res;
432     int (*f)();
433
434     xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
435       f = ps->function;
436       res = (*f)();
437       xbt_dynar_push_as(prop_ato, int, res);
438     }
439
440     mc_pair_reached_hash_t pair_test = NULL;
441
442     int region_diff = 0;
443
444     cursor = 0;
445
446     xbt_dynar_foreach(reached_pairs_hash, cursor, pair_test){
447
448       if(automaton_state_compare(pair_test->automaton_state, st) == 0){
449         if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
450           for(j=0 ; j< sn->num_reg ; j++){
451             if(hash_regions[j] != pair_test->hash_regions[j]){
452               region_diff++;
453             }
454           }
455           if(region_diff == 0){
456             MC_free_snapshot(sn);
457             xbt_dynar_reset(prop_ato);
458             xbt_free(prop_ato);
459             MC_UNSET_RAW_MEM;
460             return 1;
461           }else{
462             XBT_DEBUG("Different snapshot");
463           }
464         }else{
465           XBT_DEBUG("Different values of propositional symbols");
466         }
467       }else{
468         XBT_DEBUG("Different automaton state");
469       }
470
471       region_diff = 0;
472     }
473     
474     MC_free_snapshot(sn);
475     xbt_dynar_reset(prop_ato);
476     xbt_free(prop_ato);
477     MC_UNSET_RAW_MEM;
478     return 0;
479     
480   }
481 }
482
483 void set_pair_reached_hash(xbt_state_t st){
484  
485   MC_SET_RAW_MEM;
486
487   mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
488   MC_take_snapshot_liveness(sn);
489  
490   mc_pair_reached_hash_t pair = NULL;
491   pair = xbt_new0(s_mc_pair_reached_hash_t, 1);
492   pair->automaton_state = st;
493   pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
494   pair->hash_regions = malloc(sizeof(unsigned int) * sn->num_reg);
495   
496   int i;
497
498   for(i=0 ; i< sn->num_reg ; i++){
499     pair->hash_regions[i] = hash_region(sn->regions[i]->data, sn->regions[i]->size);
500   }
501   
502   /* Get values of propositional symbols */
503   unsigned int cursor = 0;
504   xbt_propositional_symbol_t ps = NULL;
505   int res;
506   int (*f)();
507
508   xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
509     f = ps->function;
510     res = (*f)();
511     xbt_dynar_push_as(pair->prop_ato, int, res);
512   }
513   
514   xbt_dynar_push(reached_pairs_hash, &pair);
515
516   MC_free_snapshot(sn);
517   
518   MC_UNSET_RAW_MEM;
519     
520 }
521
522
523 int visited(xbt_state_t st, int sc){
524
525
526   if(xbt_dynar_is_empty(visited_pairs)){
527
528     return 0;
529
530   }else{
531
532     MC_SET_RAW_MEM;
533
534     mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
535     MC_take_snapshot_liveness(sn);
536
537     xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
538
539     /* Get values of propositional symbols */
540     unsigned int cursor = 0;
541     xbt_propositional_symbol_t ps = NULL;
542     int res;
543     int (*f)();
544
545     xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
546       f = ps->function;
547       res = (*f)();
548       xbt_dynar_push_as(prop_ato, int, res);
549     }
550
551     cursor = 0;
552     mc_pair_visited_t pair_test = NULL;
553
554     xbt_dynar_foreach(visited_pairs, cursor, pair_test){
555       if(pair_test->search_cycle == sc) {
556         if(automaton_state_compare(pair_test->automaton_state, st) == 0){
557           if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
558             if(snapshot_compare(pair_test->system_state, sn) == 0){
559             
560               MC_free_snapshot(sn);
561               xbt_dynar_reset(prop_ato);
562               xbt_free(prop_ato);
563               MC_UNSET_RAW_MEM;
564                 
565               return 1;
566         
567             }else{
568               XBT_DEBUG("Different snapshot");
569             }
570           }else{
571             XBT_DEBUG("Different values of propositional symbols"); 
572           }
573         }else{
574           XBT_DEBUG("Different automaton state");
575         }
576       }else{
577         XBT_DEBUG("Different value of search_cycle");
578       }
579     }
580
581
582     MC_free_snapshot(sn);
583     xbt_dynar_reset(prop_ato);
584     xbt_free(prop_ato);
585     MC_UNSET_RAW_MEM;
586     return 0;
587     
588   }
589 }
590
591
592 int visited_hash(xbt_state_t st, int sc){
593
594
595   if(xbt_dynar_is_empty(visited_pairs_hash)){
596
597     return 0;
598
599   }else{
600
601     MC_SET_RAW_MEM;
602
603     mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
604     MC_take_snapshot_liveness(sn);
605
606     int j;
607     unsigned int hash_regions[sn->num_reg];
608     for(j=0; j<sn->num_reg; j++){
609       hash_regions[j] = hash_region(sn->regions[j]->data, sn->regions[j]->size);
610     }
611
612     
613     /* Get values of propositional symbols */
614     xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
615     unsigned int cursor = 0;
616     xbt_propositional_symbol_t ps = NULL;
617     int res;
618     int (*f)();
619
620     xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
621       f = ps->function;
622       res = (*f)();
623       xbt_dynar_push_as(prop_ato, int, res);
624     }
625
626     mc_pair_visited_hash_t pair_test = NULL;
627
628     int region_diff = 0;
629     cursor = 0;
630
631     xbt_dynar_foreach(visited_pairs_hash, cursor, pair_test){
632   
633       if(pair_test->search_cycle == sc) {
634         if(automaton_state_compare(pair_test->automaton_state, st) == 0){
635           if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
636             for(j=0 ; j< sn->num_reg ; j++){
637               if(hash_regions[j] != pair_test->hash_regions[j]){
638                 region_diff++;
639               }
640             }
641             if(region_diff == 0){
642               MC_free_snapshot(sn);
643               xbt_dynar_reset(prop_ato);
644               xbt_free(prop_ato);
645               MC_UNSET_RAW_MEM;
646               return 1;
647             }else{
648               XBT_DEBUG("Different snapshot");
649             }
650           }else{
651             XBT_DEBUG("Different values of propositional symbols"); 
652           }
653         }else{
654           XBT_DEBUG("Different automaton state");
655         }
656       }else{
657         XBT_DEBUG("Different value of search_cycle");
658       }
659       
660       region_diff = 0;
661     }
662     
663     MC_free_snapshot(sn);
664     xbt_dynar_reset(prop_ato);
665     xbt_free(prop_ato);
666     MC_UNSET_RAW_MEM;
667     return 0;
668     
669   }
670 }
671
672 void set_pair_visited_hash(xbt_state_t st, int sc){
673  
674   MC_SET_RAW_MEM;
675
676   mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
677   MC_take_snapshot_liveness(sn);
678  
679   mc_pair_visited_hash_t pair = NULL;
680   pair = xbt_new0(s_mc_pair_visited_hash_t, 1);
681   pair->automaton_state = st;
682   pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
683   pair->search_cycle = sc;
684   pair->hash_regions = malloc(sizeof(unsigned int) * sn->num_reg);
685   
686   int i;
687
688   for(i=0 ; i< sn->num_reg ; i++){
689     pair->hash_regions[i] = hash_region(sn->regions[i]->data, sn->regions[i]->size);
690   }
691   
692   /* Get values of propositional symbols */
693   unsigned int cursor = 0;
694   xbt_propositional_symbol_t ps = NULL;
695   int res;
696   int (*f)();
697
698   xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
699     f = ps->function;
700     res = (*f)();
701     xbt_dynar_push_as(pair->prop_ato, int, res);
702   }
703   
704   xbt_dynar_push(visited_pairs_hash, &pair);
705
706   MC_free_snapshot(sn);
707   
708   MC_UNSET_RAW_MEM;
709     
710 }
711
712 void set_pair_visited(xbt_state_t st, int sc){
713
714  
715   MC_SET_RAW_MEM;
716  
717   mc_pair_visited_t pair = NULL;
718   pair = xbt_new0(s_mc_pair_visited_t, 1);
719   pair->automaton_state = st;
720   pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
721   pair->search_cycle = sc;
722   pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
723   MC_take_snapshot_liveness(pair->system_state);
724
725
726   /* Get values of propositional symbols */
727   unsigned int cursor = 0;
728   xbt_propositional_symbol_t ps = NULL;
729   int res;
730   int (*f)();
731
732   xbt_dynar_foreach(automaton->propositional_symbols, cursor, ps){
733     f = ps->function;
734     res = (*f)();
735     xbt_dynar_push_as(pair->prop_ato, int, res);
736   }
737   
738   xbt_dynar_push(visited_pairs, &pair);
739   
740   MC_UNSET_RAW_MEM;
741   
742   
743 }
744
745 void MC_pair_delete(mc_pair_t pair){
746   xbt_free(pair->graph_state->proc_status);
747   xbt_free(pair->graph_state);
748   xbt_free(pair);
749 }
750
751
752
753 int MC_automaton_evaluate_label(xbt_exp_label_t l){
754   
755   switch(l->type){
756   case 0 : {
757     int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
758     int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
759     return (left_res || right_res);
760     break;
761   }
762   case 1 : {
763     int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
764     int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
765     return (left_res && right_res);
766     break;
767   }
768   case 2 : {
769     int res = MC_automaton_evaluate_label(l->u.exp_not);
770     return (!res);
771     break;
772   }
773   case 3 : { 
774     unsigned int cursor = 0;
775     xbt_propositional_symbol_t p = NULL;
776     int (*f)();
777     xbt_dynar_foreach(automaton->propositional_symbols, cursor, p){
778       if(strcmp(p->pred, l->u.predicat) == 0){
779         f = p->function;
780         return (*f)();
781       }
782     }
783     return -1;
784     break;
785   }
786   case 4 : {
787     return 2;
788     break;
789   }
790   default : 
791     return -1;
792     break;
793   }
794 }
795
796
797 /********************* Double-DFS stateless *******************/
798
799 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
800   xbt_free(pair->graph_state->proc_status);
801   xbt_free(pair->graph_state);
802   xbt_free(pair);
803 }
804
805 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
806   mc_pair_stateless_t p = NULL;
807   p = xbt_new0(s_mc_pair_stateless_t, 1);
808   p->automaton_state = st;
809   p->graph_state = sg;
810   p->requests = r;
811   mc_stats_pair->expanded_pairs++;
812   return p;
813 }
814
815 void MC_ddfs_init(void){
816
817   XBT_DEBUG("**************************************************");
818   XBT_DEBUG("Double-DFS init");
819   XBT_DEBUG("**************************************************");
820
821   mc_pair_stateless_t mc_initial_pair = NULL;
822   mc_state_t initial_graph_state = NULL;
823   smx_process_t process; 
824  
825   MC_wait_for_requests();
826
827   MC_SET_RAW_MEM;
828
829   initial_graph_state = MC_state_pair_new();
830   xbt_swag_foreach(process, simix_global->process_list){
831     if(MC_process_is_enabled(process)){
832       MC_state_interleave_process(initial_graph_state, process);
833     }
834   }
835
836   reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
837   //reached_pairs_hash = xbt_dynar_new(sizeof(mc_pair_reached_hash_t), NULL);
838   //visited_pairs = xbt_dynar_new(sizeof(mc_pair_visited_t), NULL);
839   visited_pairs_hash = xbt_dynar_new(sizeof(mc_pair_visited_hash_t), NULL);
840   successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
841
842   /* Save the initial state */
843   initial_snapshot_liveness = xbt_new0(s_mc_snapshot_t, 1);
844   MC_take_snapshot_liveness(initial_snapshot_liveness);
845
846   MC_UNSET_RAW_MEM; 
847
848   unsigned int cursor = 0;
849   xbt_state_t state;
850
851   xbt_dynar_foreach(automaton->states, cursor, state){
852     if(state->type == -1){
853       
854       MC_SET_RAW_MEM;
855       mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
856       xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
857       MC_UNSET_RAW_MEM;
858       
859       if(cursor != 0){
860         MC_restore_snapshot(initial_snapshot_liveness);
861         MC_UNSET_RAW_MEM;
862       }
863
864       MC_ddfs(0);
865
866     }else{
867       if(state->type == 2){
868       
869         MC_SET_RAW_MEM;
870         mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
871         xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
872         MC_UNSET_RAW_MEM;
873
874         set_pair_reached(state);
875         //set_pair_reached_hash(state);
876
877         if(cursor != 0){
878           MC_restore_snapshot(initial_snapshot_liveness);
879           MC_UNSET_RAW_MEM;
880         }
881         
882         MC_ddfs(1);
883         
884       }
885     }
886   } 
887
888 }
889
890
891 void MC_ddfs(int search_cycle){
892
893   smx_process_t process;
894   mc_pair_stateless_t current_pair = NULL;
895
896   if(xbt_fifo_size(mc_stack_liveness) == 0)
897     return;
898
899
900   /* Get current pair */
901   current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
902
903   /* Update current state in buchi automaton */
904   automaton->current_state = current_pair->automaton_state;
905
906  
907   XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
908   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));
909  
910   mc_stats_pair->visited_pairs++;
911
912   sleep(1);
913
914   int value;
915   mc_state_t next_graph_state = NULL;
916   smx_simcall_t req = NULL;
917   char *req_str;
918
919   xbt_transition_t transition_succ;
920   unsigned int cursor = 0;
921   int res;
922
923   mc_pair_stateless_t next_pair = NULL;
924   mc_pair_stateless_t pair_succ;
925
926   if(xbt_fifo_size(mc_stack_liveness) < MAX_DEPTH_LIVENESS){
927
928     //set_pair_visited(current_pair->automaton_state, search_cycle);
929     set_pair_visited_hash(current_pair->automaton_state, search_cycle);
930
931     //XBT_DEBUG("Visited pairs : %lu", xbt_dynar_length(visited_pairs));
932     XBT_DEBUG("Visited pairs : %lu", xbt_dynar_length(visited_pairs_hash));
933
934     if(current_pair->requests > 0){
935
936       while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
937    
938         /* Debug information */
939         if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
940           req_str = MC_request_to_string(req, value);
941           XBT_DEBUG("Execute: %s", req_str);
942           xbt_free(req_str);
943         }
944
945         MC_state_set_executed_request(current_pair->graph_state, req, value);   
946     
947         /* Answer the request */
948         SIMIX_simcall_pre(req, value);
949
950         /* Wait for requests (schedules processes) */
951         MC_wait_for_requests();
952
953
954         MC_SET_RAW_MEM;
955
956         /* Create the new expanded graph_state */
957         next_graph_state = MC_state_pair_new();
958
959         /* Get enabled process and insert it in the interleave set of the next graph_state */
960         xbt_swag_foreach(process, simix_global->process_list){
961           if(MC_process_is_enabled(process)){
962             MC_state_interleave_process(next_graph_state, process);
963           }
964         }
965
966         xbt_dynar_reset(successors);
967
968         MC_UNSET_RAW_MEM;
969
970
971         cursor= 0;
972         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
973
974           res = MC_automaton_evaluate_label(transition_succ->label);
975
976           if(res == 1){ // enabled transition in automaton
977             MC_SET_RAW_MEM;
978             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
979             xbt_dynar_push(successors, &next_pair);
980             MC_UNSET_RAW_MEM;
981           }
982
983         }
984
985         cursor = 0;
986    
987         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
988       
989           res = MC_automaton_evaluate_label(transition_succ->label);
990         
991           if(res == 2){ // true transition in automaton
992             MC_SET_RAW_MEM;
993             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
994             xbt_dynar_push(successors, &next_pair);
995             MC_UNSET_RAW_MEM;
996           }
997
998         }
999
1000         cursor = 0; 
1001         
1002         xbt_dynar_foreach(successors, cursor, pair_succ){
1003
1004           if(search_cycle == 1){
1005
1006             if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){ 
1007                       
1008               if(reached(pair_succ->automaton_state)){
1009                 //if(reached_hash(pair_succ->automaton_state)){
1010               
1011                 XBT_DEBUG("Next pair (depth = %d, %u interleave) already reached !", xbt_fifo_size(mc_stack_liveness) + 1, MC_state_interleave_size(pair_succ->graph_state));
1012
1013                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
1014                 XBT_INFO("|             ACCEPTANCE CYCLE            |");
1015                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
1016                 XBT_INFO("Counter-example that violates formula :");
1017                 MC_show_stack_liveness(mc_stack_liveness);
1018                 MC_dump_stack_liveness(mc_stack_liveness);
1019                 MC_print_statistics_pairs(mc_stats_pair);
1020                 exit(0);
1021
1022               }else{
1023
1024                 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
1025               
1026                 set_pair_reached(pair_succ->automaton_state);
1027                 //set_pair_reached_hash(pair_succ->automaton_state);
1028
1029                 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
1030                 //XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs_hash));
1031
1032                 MC_SET_RAW_MEM;
1033                 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1034                 MC_UNSET_RAW_MEM;
1035                 
1036                 MC_ddfs(search_cycle);
1037
1038               }
1039
1040             }else{
1041
1042               if(!visited_hash(pair_succ->automaton_state, search_cycle)){
1043                 //if(!visited(pair_succ->automaton_state, search_cycle)){
1044                 
1045                 MC_SET_RAW_MEM;
1046                 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1047                 MC_UNSET_RAW_MEM;
1048                 
1049                 MC_ddfs(search_cycle);
1050                 
1051               }else{
1052
1053                 XBT_DEBUG("Next pair already visited ! ");
1054
1055               }
1056               
1057             }
1058
1059           }else{
1060           
1061             if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
1062
1063               XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
1064             
1065               set_pair_reached(pair_succ->automaton_state); 
1066               //set_pair_reached_hash(pair_succ->automaton_state);
1067
1068               search_cycle = 1;
1069
1070               XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
1071               //XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs_hash));
1072
1073             }
1074
1075             if(!visited_hash(pair_succ->automaton_state, search_cycle)){
1076               //if(!visited(pair_succ->automaton_state, search_cycle)){
1077               
1078               MC_SET_RAW_MEM;
1079               xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1080               MC_UNSET_RAW_MEM;
1081               
1082               MC_ddfs(search_cycle);
1083               
1084             }else{
1085
1086               XBT_DEBUG("Next pair already visited ! ");
1087
1088             }
1089
1090           }
1091
1092          
1093           /* Restore system before checking others successors */
1094           if(cursor != (xbt_dynar_length(successors) - 1))
1095             MC_replay_liveness(mc_stack_liveness, 1);
1096         
1097           
1098         }
1099
1100         if(MC_state_interleave_size(current_pair->graph_state) > 0){
1101           XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
1102           MC_replay_liveness(mc_stack_liveness, 0);
1103         }
1104       }
1105
1106  
1107     }else{  /*No request to execute, search evolution in Büchi automaton */
1108
1109       MC_SET_RAW_MEM;
1110
1111       /* Create the new expanded graph_state */
1112       next_graph_state = MC_state_pair_new();
1113
1114       xbt_dynar_reset(successors);
1115
1116       MC_UNSET_RAW_MEM;
1117
1118
1119       cursor= 0;
1120       xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
1121
1122         res = MC_automaton_evaluate_label(transition_succ->label);
1123
1124         if(res == 1){ // enabled transition in automaton
1125           MC_SET_RAW_MEM;
1126           next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
1127           xbt_dynar_push(successors, &next_pair);
1128           MC_UNSET_RAW_MEM;
1129         }
1130
1131       }
1132
1133       cursor = 0;
1134    
1135       xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
1136       
1137         res = MC_automaton_evaluate_label(transition_succ->label);
1138         
1139         if(res == 2){ // true transition in automaton
1140           MC_SET_RAW_MEM;
1141           next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
1142           xbt_dynar_push(successors, &next_pair);
1143           MC_UNSET_RAW_MEM;
1144         }
1145
1146       }
1147
1148       cursor = 0; 
1149      
1150       xbt_dynar_foreach(successors, cursor, pair_succ){
1151
1152         if(search_cycle == 1){
1153
1154           if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){ 
1155
1156             if(reached(pair_succ->automaton_state)){
1157               //if(reached_hash(pair_succ->automaton_state)){
1158
1159               XBT_DEBUG("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness) + 1);
1160
1161               XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
1162               XBT_INFO("|             ACCEPTANCE CYCLE            |");
1163               XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
1164               XBT_INFO("Counter-example that violates formula :");
1165               MC_show_stack_liveness(mc_stack_liveness);
1166               MC_dump_stack_liveness(mc_stack_liveness);
1167               MC_print_statistics_pairs(mc_stats_pair);
1168               exit(0);
1169
1170             }else{
1171
1172               XBT_DEBUG("Next pair (depth = %d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
1173               
1174               set_pair_reached(pair_succ->automaton_state);
1175               //set_pair_reached_hash(pair_succ->automaton_state);
1176                 
1177               XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
1178               //XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs_hash));
1179
1180               MC_SET_RAW_MEM;
1181               xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1182               MC_UNSET_RAW_MEM;
1183               
1184               MC_ddfs(search_cycle);
1185
1186             }
1187
1188           }else{
1189
1190             if(!visited_hash(pair_succ->automaton_state, search_cycle)){
1191               //if(!visited(pair_succ->automaton_state, search_cycle)){
1192
1193               MC_SET_RAW_MEM;
1194               xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1195               MC_UNSET_RAW_MEM;
1196               
1197               MC_ddfs(search_cycle);
1198               
1199             }else{
1200
1201               XBT_DEBUG("Next pair already visited ! ");
1202
1203             }
1204           }
1205             
1206
1207         }else{
1208             
1209           if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
1210
1211             set_pair_reached(pair_succ->automaton_state);
1212             //set_pair_reached_hash(pair_succ->automaton_state);
1213                     
1214             search_cycle = 1;
1215
1216             XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
1217             //XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs_hash));
1218
1219           }
1220
1221           if(!visited_hash(pair_succ->automaton_state, search_cycle)){
1222             //if(!visited(pair_succ->automaton_state, search_cycle)){
1223
1224             MC_SET_RAW_MEM;
1225             xbt_fifo_unshift(mc_stack_liveness, pair_succ);
1226             MC_UNSET_RAW_MEM;
1227             
1228             MC_ddfs(search_cycle);
1229             
1230           }else{
1231
1232             XBT_DEBUG("Next pair already visited ! ");
1233
1234           }
1235
1236         }
1237
1238         /* Restore system before checking others successors */
1239         if(cursor != xbt_dynar_length(successors) - 1)
1240           MC_replay_liveness(mc_stack_liveness, 1);
1241
1242          
1243       }
1244            
1245     }
1246     
1247   }else{
1248     
1249     XBT_DEBUG("Max depth reached");
1250
1251   }
1252
1253   if(xbt_fifo_size(mc_stack_liveness) == MAX_DEPTH_LIVENESS ){
1254     XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack, maximum depth reached", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
1255   }else{
1256     XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
1257   }
1258
1259   
1260   MC_SET_RAW_MEM;
1261   xbt_fifo_shift(mc_stack_liveness);
1262   if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
1263     xbt_dynar_pop(reached_pairs, NULL);
1264     //xbt_dynar_pop(reached_pairs_hash, NULL);
1265   }
1266   MC_UNSET_RAW_MEM;
1267   
1268   
1269
1270 }