Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make CommDet a plugin on top of Safety
[simgrid.git] / src / mc / checker / SafetyChecker.hpp
1 /* Copyright (c) 2008-2022. 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_SAFETY_CHECKER_HPP
8 #define SIMGRID_MC_SAFETY_CHECKER_HPP
9
10 #include "src/mc/VisitedState.hpp"
11 #include "src/mc/checker/Checker.hpp"
12 #include "src/mc/mc_safety.hpp"
13
14 #include <list>
15 #include <memory>
16 #include <string>
17 #include <vector>
18
19 namespace simgrid {
20 namespace mc {
21
22 class XBT_PRIVATE SafetyChecker : public Checker {
23   ReductionMode reductionMode_ = ReductionMode::unset;
24   long backtrack_count_        = 0;
25
26   static xbt::signal<void()> on_exploration_start_signal;
27   static xbt::signal<void()> on_backtracking_signal;
28
29   static xbt::signal<void(State*)> on_state_creation_signal;
30
31   static xbt::signal<void(State*)> on_restore_system_state_signal;
32   static xbt::signal<void()> on_restore_initial_state_signal;
33   static xbt::signal<void(Transition*)> on_transition_replay_signal;
34   static xbt::signal<void(Transition*)> on_transition_execute_signal;
35
36   static xbt::signal<void()> on_log_state_signal;
37
38 public:
39   explicit SafetyChecker(Session* session);
40   void run() override;
41   RecordTrace get_record_trace() override;
42   std::vector<std::string> get_textual_trace() override;
43   void log_state() override;
44
45   /** Called once when the exploration starts */
46   static void on_exploration_start(std::function<void()> f) { on_exploration_start_signal.connect(f); }
47   /** Called each time that the exploration backtracks from a exploration end */
48   static void on_backtracking(std::function<void()> f) { on_backtracking_signal.connect(f); }
49   /** Called each time that a new state is create */
50   static void on_state_creation(std::function<void(State*)> f) { on_state_creation_signal.connect(f); }
51   /** Called when rollbacking directly onto a previously checkpointed state */
52   static void on_restore_system_state(std::function<void(State*)> f) { on_restore_system_state_signal.connect(f); }
53   /** Called when the state to which we backtrack was not checkpointed state, forcing us to restore the initial state
54    * before replaying some transitions */
55   static void on_restore_initial_state(std::function<void()> f) { on_restore_initial_state_signal.connect(f); }
56   /** Called when replaying a transition that was previously executed, to reach a backtracked state */
57   static void on_transition_replay(std::function<void(Transition*)> f) { on_transition_replay_signal.connect(f); }
58   /** Called when executing a new transition */
59   static void on_transition_execute(std::function<void(Transition*)> f) { on_transition_execute_signal.connect(f); }
60   /** Called when displaying the statistics at the end of the exploration */
61   static void on_log_state(std::function<void()> f) { on_log_state_signal.connect(f); }
62
63 private:
64   void check_non_termination(const State* current_state);
65   void backtrack();
66   void restore_state();
67
68   /** Stack representing the position in the exploration graph */
69   std::list<std::unique_ptr<State>> stack_;
70   VisitedStates visited_states_;
71   std::unique_ptr<VisitedState> visited_state_;
72 };
73
74 } // namespace mc
75 } // namespace simgrid
76
77 #endif