Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename SIMGRID_HAVE_MC into SIMGRID_HAVE_STATEFUL_MC (so that MC can be optional...
[simgrid.git] / src / mc / explo / DFSExplorer.hpp
1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_SAFETY_CHECKER_HPP
7 #define SIMGRID_MC_SAFETY_CHECKER_HPP
8
9 #include "src/mc/api/State.hpp"
10 #include "src/mc/explo/Exploration.hpp"
11
12 #if SIMGRID_HAVE_STATEFUL_MC
13 #include "src/mc/VisitedState.hpp"
14 #endif
15
16 #include <list>
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 namespace simgrid::mc {
22
23 typedef std::list<std::shared_ptr<State>> stack_t;
24
25 /* Used to compare two stacks and decide which one is better to backtrack,
26  * regarding the chosen guide in the last state. */
27 class OpenedStatesCompare {
28 public:
29   bool operator()(std::shared_ptr<State> const& lhs, std::shared_ptr<State> const& rhs)
30   {
31     return lhs->next_transition_guided().second < rhs->next_transition_guided().second;
32   }
33 };
34
35 class XBT_PRIVATE DFSExplorer : public Exploration {
36
37   XBT_DECLARE_ENUM_CLASS(ReductionMode, none, dpor);
38
39   ReductionMode reduction_mode_;
40   unsigned long backtrack_count_      = 0; // for statistics
41   unsigned long visited_states_count_ = 0; // for statistics
42
43   static xbt::signal<void(RemoteApp&)> on_exploration_start_signal;
44   static xbt::signal<void(RemoteApp&)> on_backtracking_signal;
45
46   static xbt::signal<void(State*, RemoteApp&)> on_state_creation_signal;
47
48   static xbt::signal<void(State*, RemoteApp&)> on_restore_system_state_signal;
49   static xbt::signal<void(RemoteApp&)> on_restore_initial_state_signal;
50   static xbt::signal<void(Transition*, RemoteApp&)> on_transition_replay_signal;
51   static xbt::signal<void(Transition*, RemoteApp&)> on_transition_execute_signal;
52
53   static xbt::signal<void(RemoteApp&)> on_log_state_signal;
54
55 public:
56   explicit DFSExplorer(const std::vector<char*>& args, bool with_dpor, bool need_memory_info = false);
57   void run() override;
58   RecordTrace get_record_trace() override;
59   std::vector<std::string> get_textual_trace() override;
60   void log_state() override;
61
62   /** Called once when the exploration starts */
63   static void on_exploration_start(std::function<void(RemoteApp& remote_app)> const& f)
64   {
65     on_exploration_start_signal.connect(f);
66   }
67   /** Called each time that the exploration backtracks from a exploration end */
68   static void on_backtracking(std::function<void(RemoteApp& remote_app)> const& f)
69   {
70     on_backtracking_signal.connect(f);
71   }
72   /** Called each time that a new state is create */
73   static void on_state_creation(std::function<void(State*, RemoteApp& remote_app)> const& f)
74   {
75     on_state_creation_signal.connect(f);
76   }
77   /** Called when rollbacking directly onto a previously checkpointed state */
78   static void on_restore_system_state(std::function<void(State*, RemoteApp& remote_app)> const& f)
79   {
80     on_restore_system_state_signal.connect(f);
81   }
82   /** Called when the state to which we backtrack was not checkpointed state, forcing us to restore the initial state
83    * before replaying some transitions */
84   static void on_restore_initial_state(std::function<void(RemoteApp& remote_app)> const& f)
85   {
86     on_restore_initial_state_signal.connect(f);
87   }
88   /** Called when replaying a transition that was previously executed, to reach a backtracked state */
89   static void on_transition_replay(std::function<void(Transition*, RemoteApp& remote_app)> const& f)
90   {
91     on_transition_replay_signal.connect(f);
92   }
93   /** Called when executing a new transition */
94   static void on_transition_execute(std::function<void(Transition*, RemoteApp& remote_app)> const& f)
95   {
96     on_transition_execute_signal.connect(f);
97   }
98   /** Called when displaying the statistics at the end of the exploration */
99   static void on_log_state(std::function<void(RemoteApp&)> const& f) { on_log_state_signal.connect(f); }
100
101 private:
102   void check_non_termination(const State* current_state);
103   void backtrack();
104
105   /** Stack representing the position in the exploration graph */
106   stack_t stack_;
107 #if SIMGRID_HAVE_STATEFUL_MC
108   VisitedStates visited_states_;
109   std::unique_ptr<VisitedState> visited_state_;
110 #else
111   void* visited_state_ = nullptr; /* The code uses it to detect whether we are doing stateful MC */
112 #endif
113
114   /** Opened states are states that still contains todo actors.
115    *  When backtracking, we pick a state from it*/
116   std::priority_queue<std::shared_ptr<State>, std::vector<std::shared_ptr<State>>, OpenedStatesCompare> opened_states_;
117
118   /** Change current stack_ value to correspond to the one we would have
119    *  had if we executed transition to get to state. This is required when
120    *  backtracking, and achieved thanks to the fact states save their parent.*/
121   void restore_stack(std::shared_ptr<State> state);
122
123   RecordTrace get_record_trace_of_stack(stack_t stack);
124 };
125
126 } // namespace simgrid::mc
127
128 #endif