1 /* Copyright (c) 2007-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_STATE_HPP
7 #define SIMGRID_MC_STATE_HPP
9 #include "src/mc/api/ActorState.hpp"
10 #include "src/mc/api/RemoteApp.hpp"
11 #include "src/mc/api/strategy/Strategy.hpp"
12 #include "src/mc/transition/Transition.hpp"
14 #if SIMGRID_HAVE_STATEFUL_MC
15 #include "src/mc/sosp/Snapshot.hpp"
18 namespace simgrid::mc {
20 /* A node in the exploration graph (kind-of) */
21 class XBT_PRIVATE State : public xbt::Extendable<State> {
22 static long expended_states_; /* Count total amount of states, for stats */
25 * @brief An empty transition that leads to this state by default
27 const std::unique_ptr<Transition> default_transition_ = std::make_unique<Transition>();
30 * @brief The outgoing transition: what was the last transition that
31 * we took to leave this state?
33 * The owner of the transition is the `ActorState` instance which exists in this state,
34 * or a reference to the internal default transition `Transition()` if no transition has been
37 Transition* transition_ = default_transition_.get();
39 /** @brief A list of transition to be replayed in order to get in this state. */
40 std::list<Transition*> recipe_;
42 /** Sequential state ID (used for debugging) */
45 /** Snapshot of system state (if needed) */
46 std::shared_ptr<Snapshot> system_state_;
48 /** Unique parent of this state. Required both for sleep set computation
49 and for guided model-checking */
50 std::shared_ptr<State> parent_state_ = nullptr;
52 std::shared_ptr<Strategy> strategy_;
54 /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
55 * set. With this information, it is check whether it should be removed from it or not when exploring a new
57 std::map<aid_t, Transition> sleep_set_;
60 explicit State(RemoteApp& remote_app);
61 explicit State(RemoteApp& remote_app, std::shared_ptr<State> parent_state);
62 /* Returns a positive number if there is another transition to pick, or -1 if not */
63 aid_t next_transition() const; // this function should disapear as it is redundant with the next one
65 /* Same as next_transition, but choice is now guided, and an integer corresponding to the
66 internal cost of the transition is returned */
67 std::pair<aid_t, int> next_transition_guided() const;
70 * @brief Explore a new path on the remote app; the parameter 'next' must be the result of a previous call to
73 std::shared_ptr<Transition> execute_next(aid_t next, RemoteApp& app);
75 long get_num() const { return num_; }
76 std::size_t count_todo() const;
77 std::size_t count_todo_multiples() const;
79 /* Marking as TODO some actor in this state:
80 * + consider_one mark aid actor (and assert it is possible)
81 * + consider_best ensure one actor is marked by eventually marking the best regarding its guiding methode
82 * + conside_all mark all enabled actor that are not done yet */
83 void consider_one(aid_t aid) { strategy_->consider_one(aid); }
84 void consider_best() { strategy_->consider_best(); }
85 unsigned long consider_all() { return strategy_->consider_all(); }
87 bool is_actor_done(aid_t actor) const { return strategy_->actors_to_run_.at(actor).is_done(); }
88 Transition* get_transition() const;
89 void set_transition(Transition* t) { transition_ = t; }
90 std::shared_ptr<State> get_parent_state() { return parent_state_; }
91 std::list<Transition*> get_recipe() const { return recipe_; }
93 std::map<aid_t, ActorState> const& get_actors_list() const { return strategy_->actors_to_run_; }
95 unsigned long get_actor_count() const { return strategy_->actors_to_run_.size(); }
96 bool is_actor_enabled(aid_t actor) { return strategy_->actors_to_run_.at(actor).is_enabled(); }
98 Snapshot* get_system_state() const { return system_state_.get(); }
99 void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
101 std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
102 void add_sleep_set(const Transition* t)
104 sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_));
107 /* Returns the total amount of states created so far (for statistics) */
108 static long get_expanded_states() { return expended_states_; }
110 } // namespace simgrid::mc