Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix a -Werror,-Wsign-compare
[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   auto visited_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(simgrid::mc::visited_pairs);
320
321   auto range = std::equal_range(visited_pairs.begin(), visited_pairs.end(),
322     new_visited_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
323
324   for (auto i = range.first; i != range.second; ++i) {
325     simgrid::mc::VisitedPair* pair_test = *i;
326     std::size_t cursor = i - visited_pairs.begin();
327     if (xbt_automaton_state_compare(pair_test->automaton_state, new_visited_pair->automaton_state) == 0) {
328       if (xbt_automaton_propositional_symbols_compare_value(
329           pair_test->atomic_propositions.get(),
330           new_visited_pair->atomic_propositions.get()) == 0) {
331         if (snapshot_compare(pair_test, new_visited_pair) == 0) {
332           if (pair_test->other_num == -1)
333             new_visited_pair->other_num = pair_test->num;
334           else
335             new_visited_pair->other_num = pair_test->other_num;
336           if (dot_output == nullptr)
337             XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", new_visited_pair->num, pair_test->num);
338           else
339             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);
340           xbt_dynar_remove_at(simgrid::mc::visited_pairs, cursor, &pair_test);
341           xbt_dynar_insert_at(simgrid::mc::visited_pairs, cursor, &new_visited_pair);
342           pair_test->visited_removed = 1;
343           if (!pair_test->acceptance_pair
344               || pair_test->acceptance_removed == 1)
345             delete pair_test;
346           return new_visited_pair->other_num;
347         }
348       }
349     }
350   }
351
352   xbt_dynar_insert_at(simgrid::mc::visited_pairs, range.first - visited_pairs.begin(), &new_visited_pair);
353
354   if ((ssize_t) visited_pairs.size() > _sg_mc_visited) {
355     int min2 = mc_stats->expanded_pairs;
356     unsigned int index2 = 0;
357     for (std::size_t i = 0; i != (std::size_t) visited_pairs.size(); ++i) {
358       simgrid::mc::VisitedPair* pair_test = visited_pairs[i];
359       if (!mc_model_checker->is_important_snapshot(*pair_test->graph_state->system_state)
360           && pair_test->num < min2) {
361         index2 = i;
362         min2 = pair_test->num;
363       }
364     }
365     simgrid::mc::VisitedPair* pair_test = nullptr;
366     xbt_dynar_remove_at(simgrid::mc::visited_pairs, index2, &pair_test);
367     pair_test->visited_removed = 1;
368     if (!pair_test->acceptance_pair || pair_test->acceptance_removed)
369       delete pair_test;
370   }
371
372   return -1;
373 }
374
375 static int MC_modelcheck_liveness_main(void)
376 {
377   simgrid::mc::Pair* current_pair = nullptr;
378   int value, res, visited_num = -1;
379   smx_simcall_t req = nullptr;
380   xbt_automaton_transition_t transition_succ = nullptr;
381   int cursor = 0;
382   simgrid::mc::Pair* next_pair = nullptr;
383   simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
384   simgrid::mc::VisitedPair* reached_pair = nullptr;
385   
386   while(xbt_fifo_size(mc_stack) > 0){
387
388     /* Get current pair */
389     current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
390
391     /* Update current state in buchi automaton */
392     simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
393
394     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
395        current_pair->depth, current_pair->search_cycle,
396        MC_state_interleave_size(current_pair->graph_state), current_pair->num,
397        current_pair->requests);
398
399     if (current_pair->requests > 0) {
400
401       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
402         /* If new acceptance pair, return new pair */
403         if ((reached_pair = is_reached_acceptance_pair(current_pair)) == nullptr) {
404           int counter_example_depth = current_pair->depth;
405           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
406           XBT_INFO("|             ACCEPTANCE CYCLE            |");
407           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
408           XBT_INFO("Counter-example that violates formula :");
409           MC_record_dump_path(mc_stack);
410           simgrid::mc::show_stack_liveness(mc_stack);
411           simgrid::mc::dump_stack_liveness(mc_stack);
412           MC_print_statistics(mc_stats);
413           XBT_INFO("Counter-example depth : %d", counter_example_depth);
414           return SIMGRID_MC_EXIT_LIVENESS;
415         }
416       }
417
418       /* Pair already visited ? stop the exploration on the current path */
419       if ((current_pair->exploration_started == 0)
420         && (visited_num = simgrid::mc::is_visited_pair(
421           reached_pair, current_pair)) != -1) {
422
423         if (dot_output != nullptr){
424           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
425           fflush(dot_output);
426         }
427
428         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
429         current_pair->requests = 0;
430         goto backtracking;
431         
432       }else{
433
434         req = MC_state_get_request(current_pair->graph_state, &value);
435
436          if (dot_output != nullptr) {
437            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
438              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
439              xbt_free(initial_global_state->prev_req);
440            }
441            initial_global_state->prev_pair = current_pair->num;
442            initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
443            if (current_pair->search_cycle)
444              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
445            fflush(dot_output);
446          }
447
448          char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
449          XBT_DEBUG("Execute: %s", req_str);
450          xbt_free(req_str);
451
452          /* Set request as executed */
453          MC_state_set_executed_request(current_pair->graph_state, req, value);
454
455          /* Update mc_stats */
456          mc_stats->executed_transitions++;
457          if(current_pair->exploration_started == 0)
458            mc_stats->visited_pairs++;
459
460          /* Answer the request */
461          simgrid::mc::handle_simcall(req, value);
462          
463          /* Wait for requests (schedules processes) */
464          mc_model_checker->wait_for_requests();
465
466          current_pair->requests--;
467          current_pair->exploration_started = 1;
468
469          /* Get values of atomic propositions (variables used in the property formula) */ 
470          prop_values = get_atomic_propositions_values();
471
472          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
473          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
474          while (cursor >= 0) {
475            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
476            res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
477            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
478               next_pair = new Pair();
479               next_pair->graph_state = MC_state_new();
480               next_pair->automaton_state = transition_succ->dst;
481               next_pair->atomic_propositions = get_atomic_propositions_values();
482               next_pair->depth = current_pair->depth + 1;
483               /* Get enabled processes and insert them in the interleave set of the next graph_state */
484               for (auto& p : mc_model_checker->process().simix_processes())
485                 if (simgrid::mc::process_is_enabled(&p.copy))
486                   MC_state_interleave_process(next_pair->graph_state, &p.copy);
487
488               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
489
490               /* FIXME : get search_cycle value for each acceptant state */
491               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
492                 next_pair->search_cycle = 1;
493
494               /* Add new pair to the exploration stack */
495               xbt_fifo_unshift(mc_stack, next_pair);
496
497            }
498            cursor--;
499          }
500
501       } /* End of visited_pair test */
502       
503     } else {
504
505     backtracking:
506       if(visited_num == -1)
507         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
508     
509       prop_values.reset();
510     
511       /* Traverse the stack backwards until a pair with a non empty interleave
512          set is found, deleting all the pairs that have it empty in the way. */
513       while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
514         if (current_pair->requests > 0) {
515           /* We found a backtracking point */
516           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
517           xbt_fifo_unshift(mc_stack, current_pair);
518           MC_replay_liveness(mc_stack);
519           XBT_DEBUG("Backtracking done");
520           break;
521         }else{
522           /* Delete pair */
523           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
524           if (current_pair->automaton_state->type == 1) 
525             remove_acceptance_pair(current_pair->num);
526           delete current_pair;
527         }
528       }
529
530     } /* End of if (current_pair->requests > 0) else ... */
531     
532   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
533
534   XBT_INFO("No property violation found.");
535   MC_print_statistics(mc_stats);
536   return SIMGRID_MC_EXIT_SUCCESS;
537 }
538
539 int modelcheck_liveness(void)
540 {
541   XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
542   MC_automaton_load(_sg_mc_property_file);
543   mc_model_checker->wait_for_requests();
544
545   XBT_DEBUG("Starting the liveness algorithm");
546   _sg_mc_liveness = 1;
547
548   /* Create exploration stack */
549   mc_stack = xbt_fifo_new();
550
551   /* Create the initial state */
552   initial_global_state = xbt_new0(s_mc_global_t, 1);
553
554   MC_pre_modelcheck_liveness();
555   int res = MC_modelcheck_liveness_main();
556
557   /* We're done */
558   simgrid::mc::processes_time.clear();
559
560   return res;
561 }
562
563 }
564 }