Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50307cacd1dad9b692f4321b9655a08be2320c3d
[simgrid.git] / src / mc / explo / LivenessChecker.hpp
1 /* Copyright (c) 2007-2023. 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 #ifndef SIMGRID_MC_LIVENESS_CHECKER_HPP
8 #define SIMGRID_MC_LIVENESS_CHECKER_HPP
9
10 #include "src/mc/api/State.hpp"
11 #include "src/mc/explo/Exploration.hpp"
12 #include "xbt/automaton.hpp"
13
14 #include <list>
15 #include <memory>
16 #include <vector>
17
18 namespace simgrid::mc {
19
20 class XBT_PRIVATE Pair {
21 public:
22   int num                           = 0;
23   bool search_cycle                 = false;
24   std::shared_ptr<State> app_state_ = nullptr; /* State of the application (including system state) */
25   xbt_automaton_state_t prop_state_ = nullptr; /* State of the property automaton */
26   std::shared_ptr<const std::vector<int>> atomic_propositions;
27   int requests             = 0;
28   int depth                = 0;
29   bool exploration_started = false;
30
31   explicit Pair(unsigned long expanded_pairs);
32
33   Pair(Pair const&) = delete;
34   Pair& operator=(Pair const&) = delete;
35 };
36
37 class XBT_PRIVATE VisitedPair {
38 public:
39   int num;
40   int other_num                      = 0;       /* Dot output for */
41   std::shared_ptr<State> app_state_  = nullptr; /* State of the application (including system state) */
42   xbt_automaton_state_t prop_state_;            /* State of the property automaton */
43   std::shared_ptr<const std::vector<int>> atomic_propositions;
44   std::size_t heap_bytes_used = 0;
45   int actor_count_;
46
47   VisitedPair(int pair_num, xbt_automaton_state_t prop_state,
48               std::shared_ptr<const std::vector<int>> atomic_propositions, std::shared_ptr<State> app_state);
49 };
50
51 class XBT_PRIVATE LivenessChecker : public Exploration {
52 public:
53   explicit LivenessChecker(const std::vector<char*>& args);
54   ~LivenessChecker() override;
55
56   void run() override;
57   RecordTrace get_record_trace() override;
58   std::vector<std::string> get_textual_trace() override;
59   void log_state() override;
60
61 private:
62   std::shared_ptr<const std::vector<int>> get_proposition_values() const;
63   std::shared_ptr<VisitedPair> insert_acceptance_pair(Pair* pair);
64   int insert_visited_pair(std::shared_ptr<VisitedPair> visited_pair, Pair* pair);
65   void show_acceptance_cycle(std::size_t depth);
66   void replay();
67   void remove_acceptance_pair(int pair_num);
68   void purge_visited_pairs();
69   void backtrack();
70   std::shared_ptr<Pair> create_pair(const Pair* pair, xbt_automaton_state_t state,
71                                     std::shared_ptr<const std::vector<int>> propositions);
72
73   // A stack of (application_state, automaton_state) pairs for DFS exploration:
74   std::list<std::shared_ptr<Pair>> exploration_stack_;
75   std::list<std::shared_ptr<VisitedPair>> acceptance_pairs_;
76   std::list<std::shared_ptr<VisitedPair>> visited_pairs_;
77   unsigned long visited_pairs_count_  = 0;
78   unsigned long expanded_pairs_count_ = 0;
79   int previous_pair_                  = 0;
80   std::string previous_request_;
81
82   /* The property automaton must be a static because it's sometimes used before the explorer is even created.
83    *
84    * This can happen if some symbols are created during the application's initialization process, before the first
85    * decision point for the model-checker. Since the first snapshot is taken at the first decision point and since the
86    * explorer is created after the first snapshot, this may result in some symbols being registered even before the
87    * model-checker notices that this is a LivenessChecker to create.
88    *
89    * This situation is unfortunate, but I guess that it's the best I can achieve given the state of our initialization
90    * code.
91    */
92   static xbt_automaton_t property_automaton_;
93   bool evaluate_label(const xbt_automaton_exp_label* l, std::vector<int> const& values);
94
95 public:
96   void automaton_load(const char* file) const;
97   std::vector<int> automaton_propositional_symbol_evaluate() const;
98   std::vector<xbt_automaton_state_t> get_automaton_state() const;
99   int compare_automaton_exp_label(const xbt_automaton_exp_label* l) const;
100   void set_property_automaton(xbt_automaton_state_t const& automaton_state) const;
101   xbt_automaton_exp_label_t get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const;
102   xbt_automaton_state_t get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const;
103   static void automaton_register_symbol(RemoteProcess const& remote_process, const char* name, RemotePtr<int> addr);
104 };
105
106 } // namespace simgrid::mc
107
108 #endif