Logo AND Algorithmique Numérique Distribuée

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