1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_SAFETY_CHECKER_HPP
7 #define SIMGRID_MC_SAFETY_CHECKER_HPP
9 #include "src/mc/api/ClockVector.hpp"
10 #include "src/mc/api/State.hpp"
11 #include "src/mc/explo/Exploration.hpp"
13 #if SIMGRID_HAVE_STATEFUL_MC
14 #include "src/mc/VisitedState.hpp"
21 #include <unordered_map>
24 namespace simgrid::mc {
26 using stack_t = std::list<std::shared_ptr<State>>;
28 class XBT_PRIVATE DFSExplorer : public Exploration {
29 XBT_DECLARE_ENUM_CLASS(ReductionMode, none, dpor, sdpor);
31 ReductionMode reduction_mode_;
32 unsigned long backtrack_count_ = 0; // for statistics
33 unsigned long visited_states_count_ = 0; // for statistics
35 static xbt::signal<void(RemoteApp&)> on_exploration_start_signal;
36 static xbt::signal<void(RemoteApp&)> on_backtracking_signal;
38 static xbt::signal<void(State*, RemoteApp&)> on_state_creation_signal;
40 static xbt::signal<void(State*, RemoteApp&)> on_restore_system_state_signal;
41 static xbt::signal<void(RemoteApp&)> on_restore_initial_state_signal;
42 static xbt::signal<void(Transition*, RemoteApp&)> on_transition_replay_signal;
43 static xbt::signal<void(Transition*, RemoteApp&)> on_transition_execute_signal;
45 static xbt::signal<void(RemoteApp&)> on_log_state_signal;
48 explicit DFSExplorer(const std::vector<char*>& args, bool with_dpor, bool need_memory_info = false);
50 RecordTrace get_record_trace() override;
51 void log_state() override;
53 /** Called once when the exploration starts */
54 static void on_exploration_start(std::function<void(RemoteApp& remote_app)> const& f)
56 on_exploration_start_signal.connect(f);
58 /** Called each time that the exploration backtracks from a exploration end */
59 static void on_backtracking(std::function<void(RemoteApp& remote_app)> const& f)
61 on_backtracking_signal.connect(f);
63 /** Called each time that a new state is create */
64 static void on_state_creation(std::function<void(State*, RemoteApp& remote_app)> const& f)
66 on_state_creation_signal.connect(f);
68 /** Called when rollbacking directly onto a previously checkpointed state */
69 static void on_restore_system_state(std::function<void(State*, RemoteApp& remote_app)> const& f)
71 on_restore_system_state_signal.connect(f);
73 /** Called when the state to which we backtrack was not checkpointed state, forcing us to restore the initial state
74 * before replaying some transitions */
75 static void on_restore_initial_state(std::function<void(RemoteApp& remote_app)> const& f)
77 on_restore_initial_state_signal.connect(f);
79 /** Called when replaying a transition that was previously executed, to reach a backtracked state */
80 static void on_transition_replay(std::function<void(Transition*, RemoteApp& remote_app)> const& f)
82 on_transition_replay_signal.connect(f);
84 /** Called when executing a new transition */
85 static void on_transition_execute(std::function<void(Transition*, RemoteApp& remote_app)> const& f)
87 on_transition_execute_signal.connect(f);
89 /** Called when displaying the statistics at the end of the exploration */
90 static void on_log_state(std::function<void(RemoteApp&)> const& f) { on_log_state_signal.connect(f); }
93 void check_non_termination(const State* current_state);
96 /** Stack representing the position in the exploration graph */
99 /** Per-actor clock vectors used to compute the "happens-before" relation */
100 std::unordered_map<aid_t, ClockVector> per_actor_clocks_;
102 #if SIMGRID_HAVE_STATEFUL_MC
103 VisitedStates visited_states_;
104 std::unique_ptr<VisitedState> visited_state_;
106 void* visited_state_ = nullptr; /* The code uses it to detect whether we are doing stateful MC */
109 /** Opened states are states that still contains todo actors.
110 * When backtracking, we pick a state from it*/
111 std::vector<std::shared_ptr<State>> opened_states_;
112 std::shared_ptr<State> best_opened_state();
114 /** Change current stack_ value to correspond to the one we would have
115 * had if we executed transition to get to state. This is required when
116 * backtracking, and achieved thanks to the fact states save their parent.*/
117 void restore_stack(std::shared_ptr<State> state);
119 RecordTrace get_record_trace_of_stack(stack_t stack);
122 } // namespace simgrid::mc