Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::equal_range in is_reached_acceptance_pair()
[simgrid.git] / src / mc / mc_liveness.cpp
1 /* Copyright (c) 2011-2015. 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 <cstring>
8
9 #include <algorithm>
10
11 #include <unistd.h>
12 #include <sys/wait.h>
13
14 #include <xbt/automaton.h>
15 #include <xbt/dynar.h>
16 #include <xbt/dynar.hpp>
17 #include <xbt/fifo.h>
18 #include <xbt/log.h>
19 #include <xbt/sysdep.h>
20
21 #include "src/mc/mc_request.h"
22 #include "src/mc/mc_liveness.h"
23 #include "src/mc/mc_private.h"
24 #include "src/mc/mc_record.h"
25 #include "src/mc/mc_smx.h"
26 #include "src/mc/Client.hpp"
27 #include "src/mc/mc_private.h"
28 #include "src/mc/mc_replay.h"
29 #include "src/mc/mc_safety.h"
30 #include "src/mc/mc_exit.h"
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
33                                 "Logging specific to algorithms for liveness properties verification");
34
35 /********* Global variables *********/
36
37 xbt_dynar_t acceptance_pairs;
38
39 /********* Static functions *********/
40
41 namespace simgrid {
42 namespace mc {
43
44 static xbt_dynar_t visited_pairs;
45
46 Pair::Pair() : num(++mc_stats->expanded_pairs),
47   visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
48 {}
49
50 Pair::~Pair() {
51   if (this->visited_pair_removed)
52     MC_state_delete(this->graph_state, 1);
53 }
54
55 static  void show_stack_liveness(xbt_fifo_t stack)
56 {
57   int value;
58   simgrid::mc::Pair* pair;
59   xbt_fifo_item_t item;
60   smx_simcall_t req;
61   char *req_str = nullptr;
62
63   for (item = xbt_fifo_get_last_item(stack);
64        item; item = xbt_fifo_get_prev_item(item)) {
65     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
66     req = MC_state_get_executed_request(pair->graph_state, &value);
67     if (req && req->call != SIMCALL_NONE) {
68       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
69       XBT_INFO("%s", req_str);
70       xbt_free(req_str);
71     }
72   }
73 }
74
75 static void dump_stack_liveness(xbt_fifo_t stack)
76 {
77   simgrid::mc::Pair* pair;
78   while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
79     delete pair;
80 }
81
82 static simgrid::xbt::unique_ptr<s_xbt_dynar_t> get_atomic_propositions_values()
83 {
84   unsigned int cursor = 0;
85   xbt_automaton_propositional_symbol_t ps = nullptr;
86   simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
87   xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
88     int res = xbt_automaton_propositional_symbol_evaluate(ps);
89     xbt_dynar_push_as(values.get(), int, res);
90   }
91   return std::move(values);
92 }
93
94 static int snapshot_compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
95 {
96   simgrid::mc::Snapshot* s1 = state1->graph_state->system_state;
97   simgrid::mc::Snapshot* s2 = state2->graph_state->system_state;
98   int num1 = state1->num;
99   int num2 = state2->num;
100   return snapshot_compare(num1, s1, num2, s2);
101 }
102
103 static simgrid::mc::VisitedPair* is_reached_acceptance_pair(simgrid::mc::Pair* pair)
104 {
105   auto acceptance_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(::acceptance_pairs);
106
107   simgrid::mc::VisitedPair* new_pair = new VisitedPair(
108     pair->num, pair->automaton_state, pair->atomic_propositions.get(),
109     pair->graph_state);
110   new_pair->acceptance_pair = 1;
111
112   auto res = std::equal_range(acceptance_pairs.begin(), acceptance_pairs.end(),
113     new_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
114
115   if (pair->search_cycle == 1)
116     for (auto i = res.first; i != res.second; ++i) {
117       simgrid::mc::VisitedPair* pair_test = *i;
118       if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
119         if (xbt_automaton_propositional_symbols_compare_value(
120             pair_test->atomic_propositions.get(),
121             new_pair->atomic_propositions.get()) == 0) {
122           if (snapshot_compare(pair_test, new_pair) == 0) {
123             XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
124             xbt_fifo_shift(mc_stack);
125             if (dot_output != nullptr)
126               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
127             return nullptr;
128           }
129         }
130       }
131     }
132
133   xbt_dynar_insert_at(
134     ::acceptance_pairs, res.first - acceptance_pairs.begin(), &new_pair);
135   return new_pair;
136 }
137
138 static void remove_acceptance_pair(int pair_num)
139 {
140   unsigned int cursor = 0;
141   simgrid::mc::VisitedPair* pair_test = nullptr;
142   int pair_found = 0;
143
144   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
145     if (pair_test->num == pair_num) {
146        pair_found = 1;
147       break;
148     }
149
150   if(pair_found == 1) {
151     xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
152
153     pair_test->acceptance_removed = 1;
154
155     if (_sg_mc_visited == 0 || pair_test->visited_removed == 1) 
156       delete pair_test;
157
158   }
159 }
160
161
162 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
163                                        xbt_dynar_t atomic_propositions_values)
164 {
165
166   switch (l->type) {
167   case 0:{
168       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
169       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
170       return (left_res || right_res);
171     }
172   case 1:{
173       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
174       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
175       return (left_res && right_res);
176     }
177   case 2:{
178       int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
179       return (!res);
180     }
181   case 3:{
182       unsigned int cursor = 0;
183       xbt_automaton_propositional_symbol_t p = nullptr;
184       xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
185         if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
186           return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
187       }
188       return -1;
189     }
190   case 4:
191     return 2;
192   default:
193     return -1;
194   }
195 }
196
197 static void MC_pre_modelcheck_liveness(void)
198 {
199   simgrid::mc::Pair* initial_pair = nullptr;
200   mc_model_checker->wait_for_requests();
201   acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
202   if(_sg_mc_visited > 0)
203     simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
204
205   initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
206   initial_global_state->prev_pair = 0;
207
208   unsigned int cursor = 0;
209   xbt_automaton_state_t automaton_state;
210   
211   xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
212     if (automaton_state->type == -1) {  /* Initial automaton state */
213
214       initial_pair = new Pair();
215       initial_pair->automaton_state = automaton_state;
216       initial_pair->graph_state = MC_state_new();
217       initial_pair->atomic_propositions = get_atomic_propositions_values();
218       initial_pair->depth = 1;
219
220       /* Get enabled processes and insert them in the interleave set of the graph_state */
221       for (auto& p : mc_model_checker->process().simix_processes())
222         if (simgrid::mc::process_is_enabled(&p.copy))
223           MC_state_interleave_process(initial_pair->graph_state, &p.copy);
224
225       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
226       initial_pair->search_cycle = 0;
227
228       xbt_fifo_unshift(mc_stack, initial_pair);
229     }
230   }
231 }
232
233 static void MC_replay_liveness(xbt_fifo_t stack)
234 {
235   xbt_fifo_item_t item;
236   simgrid::mc::Pair* pair = nullptr;
237   mc_state_t state = nullptr;
238   smx_simcall_t req = nullptr, saved_req = NULL;
239   int value, depth = 1;
240   char *req_str;
241
242   XBT_DEBUG("**** Begin Replay ****");
243
244   /* Intermediate backtracking */
245   if(_sg_mc_checkpoint > 0) {
246     item = xbt_fifo_get_first_item(stack);
247     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
248     if(pair->graph_state->system_state){
249       simgrid::mc::restore_snapshot(pair->graph_state->system_state);
250       return;
251     }
252   }
253
254   /* Restore the initial state */
255   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
256
257     /* Traverse the stack from the initial state and re-execute the transitions */
258     for (item = xbt_fifo_get_last_item(stack);
259          item != xbt_fifo_get_first_item(stack);
260          item = xbt_fifo_get_prev_item(item)) {
261
262       pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
263
264       state = (mc_state_t) pair->graph_state;
265
266       if (pair->exploration_started) {
267
268         saved_req = MC_state_get_executed_request(state, &value);
269
270         if (saved_req != nullptr) {
271           /* because we got a copy of the executed request, we have to fetch the
272              real one, pointed by the request field of the issuer process */
273           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
274           req = &issuer->simcall;
275
276           /* Debug information */
277           if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
278             req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
279             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
280             xbt_free(req_str);
281           }
282
283         }
284
285         simgrid::mc::handle_simcall(req, value);
286         mc_model_checker->wait_for_requests();
287       }
288
289       /* Update statistics */
290       mc_stats->visited_pairs++;
291       mc_stats->executed_transitions++;
292
293       depth++;
294
295     }
296
297   XBT_DEBUG("**** End Replay ****");
298 }
299
300 /**
301  * \brief Checks whether a given pair has already been visited by the algorithm.
302  */
303 static
304 int is_visited_pair(simgrid::mc::VisitedPair* visited_pair, simgrid::mc::Pair* pair)
305 {
306   if (_sg_mc_visited == 0)
307     return -1;
308
309   simgrid::mc::VisitedPair* new_visited_pair = nullptr;
310   if (visited_pair == nullptr)
311     new_visited_pair = new simgrid::mc::VisitedPair(
312       pair->num, pair->automaton_state, pair->atomic_propositions.get(),
313       pair->graph_state);
314   else
315     new_visited_pair = visited_pair;
316
317   if (xbt_dynar_is_empty(visited_pairs)) {
318     xbt_dynar_push(visited_pairs, &new_visited_pair);
319     return -1;
320   }
321
322     int min = -1, max = -1, index;
323     //int res;
324     simgrid::mc::VisitedPair* pair_test;
325     int cursor;
326
327     index = get_search_interval(
328       (simgrid::mc::VisitedPair**) xbt_dynar_get_ptr(visited_pairs, 0),
329       xbt_dynar_length(visited_pairs), new_visited_pair, &min, &max);
330
331     if (min != -1 && max != -1) {       // Visited pair with same number of processes and same heap bytes used exists
332       cursor = min;
333       while (cursor <= max) {
334         pair_test = (simgrid::mc::VisitedPair*) xbt_dynar_get_as(visited_pairs, cursor, simgrid::mc::VisitedPair*);
335         if (xbt_automaton_state_compare(pair_test->automaton_state, new_visited_pair->automaton_state) == 0) {
336           if (xbt_automaton_propositional_symbols_compare_value(
337               pair_test->atomic_propositions.get(),
338               new_visited_pair->atomic_propositions.get()) == 0) {
339             if (snapshot_compare(pair_test, new_visited_pair) == 0) {
340               if (pair_test->other_num == -1)
341                 new_visited_pair->other_num = pair_test->num;
342               else
343                 new_visited_pair->other_num = pair_test->other_num;
344               if (dot_output == nullptr)
345                 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", new_visited_pair->num, pair_test->num);
346               else
347                 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))", new_visited_pair->num, pair_test->num, new_visited_pair->other_num);
348               xbt_dynar_remove_at(visited_pairs, cursor, &pair_test);
349               xbt_dynar_insert_at(visited_pairs, cursor, &new_visited_pair);
350               pair_test->visited_removed = 1;
351               if (!pair_test->acceptance_pair
352                   || pair_test->acceptance_removed == 1)
353                 delete pair_test;
354               return new_visited_pair->other_num;
355             }
356           }
357         }
358         cursor++;
359       }
360       xbt_dynar_insert_at(visited_pairs, min, &new_visited_pair);
361     } else {
362       xbt_dynar_insert_at(visited_pairs, index, &new_visited_pair);
363     }
364
365     if ((ssize_t) xbt_dynar_length(visited_pairs) > _sg_mc_visited) {
366       int min2 = mc_stats->expanded_pairs;
367       unsigned int cursor2 = 0;
368       unsigned int index2 = 0;
369       xbt_dynar_foreach(visited_pairs, cursor2, pair_test) {
370         if (!mc_model_checker->is_important_snapshot(*pair_test->graph_state->system_state)
371             && pair_test->num < min2) {
372           index2 = cursor2;
373           min2 = pair_test->num;
374         }
375       }
376       xbt_dynar_remove_at(visited_pairs, index2, &pair_test);
377       pair_test->visited_removed = 1;
378       if (!pair_test->acceptance_pair || pair_test->acceptance_removed)
379         delete pair_test;
380     }
381   return -1;
382 }
383
384 static int MC_modelcheck_liveness_main(void)
385 {
386   simgrid::mc::Pair* current_pair = nullptr;
387   int value, res, visited_num = -1;
388   smx_simcall_t req = nullptr;
389   xbt_automaton_transition_t transition_succ = nullptr;
390   int cursor = 0;
391   simgrid::mc::Pair* next_pair = nullptr;
392   simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
393   simgrid::mc::VisitedPair* reached_pair = nullptr;
394   
395   while(xbt_fifo_size(mc_stack) > 0){
396
397     /* Get current pair */
398     current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
399
400     /* Update current state in buchi automaton */
401     simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
402
403     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
404        current_pair->depth, current_pair->search_cycle,
405        MC_state_interleave_size(current_pair->graph_state), current_pair->num,
406        current_pair->requests);
407
408     if (current_pair->requests > 0) {
409
410       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
411         /* If new acceptance pair, return new pair */
412         if ((reached_pair = is_reached_acceptance_pair(current_pair)) == nullptr) {
413           int counter_example_depth = current_pair->depth;
414           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
415           XBT_INFO("|             ACCEPTANCE CYCLE            |");
416           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
417           XBT_INFO("Counter-example that violates formula :");
418           MC_record_dump_path(mc_stack);
419           simgrid::mc::show_stack_liveness(mc_stack);
420           simgrid::mc::dump_stack_liveness(mc_stack);
421           MC_print_statistics(mc_stats);
422           XBT_INFO("Counter-example depth : %d", counter_example_depth);
423           return SIMGRID_MC_EXIT_LIVENESS;
424         }
425       }
426
427       /* Pair already visited ? stop the exploration on the current path */
428       if ((current_pair->exploration_started == 0)
429         && (visited_num = simgrid::mc::is_visited_pair(
430           reached_pair, current_pair)) != -1) {
431
432         if (dot_output != nullptr){
433           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
434           fflush(dot_output);
435         }
436
437         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
438         current_pair->requests = 0;
439         goto backtracking;
440         
441       }else{
442
443         req = MC_state_get_request(current_pair->graph_state, &value);
444
445          if (dot_output != nullptr) {
446            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
447              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
448              xbt_free(initial_global_state->prev_req);
449            }
450            initial_global_state->prev_pair = current_pair->num;
451            initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
452            if (current_pair->search_cycle)
453              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
454            fflush(dot_output);
455          }
456
457          char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
458          XBT_DEBUG("Execute: %s", req_str);
459          xbt_free(req_str);
460
461          /* Set request as executed */
462          MC_state_set_executed_request(current_pair->graph_state, req, value);
463
464          /* Update mc_stats */
465          mc_stats->executed_transitions++;
466          if(current_pair->exploration_started == 0)
467            mc_stats->visited_pairs++;
468
469          /* Answer the request */
470          simgrid::mc::handle_simcall(req, value);
471          
472          /* Wait for requests (schedules processes) */
473          mc_model_checker->wait_for_requests();
474
475          current_pair->requests--;
476          current_pair->exploration_started = 1;
477
478          /* Get values of atomic propositions (variables used in the property formula) */ 
479          prop_values = get_atomic_propositions_values();
480
481          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
482          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
483          while (cursor >= 0) {
484            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
485            res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
486            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
487               next_pair = new Pair();
488               next_pair->graph_state = MC_state_new();
489               next_pair->automaton_state = transition_succ->dst;
490               next_pair->atomic_propositions = get_atomic_propositions_values();
491               next_pair->depth = current_pair->depth + 1;
492               /* Get enabled processes and insert them in the interleave set of the next graph_state */
493               for (auto& p : mc_model_checker->process().simix_processes())
494                 if (simgrid::mc::process_is_enabled(&p.copy))
495                   MC_state_interleave_process(next_pair->graph_state, &p.copy);
496
497               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
498
499               /* FIXME : get search_cycle value for each acceptant state */
500               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
501                 next_pair->search_cycle = 1;
502
503               /* Add new pair to the exploration stack */
504               xbt_fifo_unshift(mc_stack, next_pair);
505
506            }
507            cursor--;
508          }
509
510       } /* End of visited_pair test */
511       
512     } else {
513
514     backtracking:
515       if(visited_num == -1)
516         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
517     
518       prop_values.reset();
519     
520       /* Traverse the stack backwards until a pair with a non empty interleave
521          set is found, deleting all the pairs that have it empty in the way. */
522       while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
523         if (current_pair->requests > 0) {
524           /* We found a backtracking point */
525           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
526           xbt_fifo_unshift(mc_stack, current_pair);
527           MC_replay_liveness(mc_stack);
528           XBT_DEBUG("Backtracking done");
529           break;
530         }else{
531           /* Delete pair */
532           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
533           if (current_pair->automaton_state->type == 1) 
534             remove_acceptance_pair(current_pair->num);
535           delete current_pair;
536         }
537       }
538
539     } /* End of if (current_pair->requests > 0) else ... */
540     
541   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
542
543   XBT_INFO("No property violation found.");
544   MC_print_statistics(mc_stats);
545   return SIMGRID_MC_EXIT_SUCCESS;
546 }
547
548 int modelcheck_liveness(void)
549 {
550   XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
551   MC_automaton_load(_sg_mc_property_file);
552   mc_model_checker->wait_for_requests();
553
554   XBT_DEBUG("Starting the liveness algorithm");
555   _sg_mc_liveness = 1;
556
557   /* Create exploration stack */
558   mc_stack = xbt_fifo_new();
559
560   /* Create the initial state */
561   initial_global_state = xbt_new0(s_mc_global_t, 1);
562
563   MC_pre_modelcheck_liveness();
564   int res = MC_modelcheck_liveness_main();
565
566   /* We're done */
567   simgrid::mc::processes_time.clear();
568
569   return res;
570 }
571
572 }
573 }