Logo AND Algorithmique Numérique Distribuée

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