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