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/sosp/Snapshot.hpp"
12 #include "src/mc/transition/Transition.hpp"
14 namespace simgrid::mc {
16 /* A node in the exploration graph (kind-of) */
17 class XBT_PRIVATE State : public xbt::Extendable<State> {
18 static long expended_states_; /* Count total amount of states, for stats */
21 * @brief An empty transition that leads to this state by default
23 const std::unique_ptr<Transition> default_transition_ = std::make_unique<Transition>();
26 * @brief The outgoing transition: what was the last transition that
27 * we took to leave this state?
29 * The owner of the transition is the `ActorState` instance which exists in this state,
30 * or a reference to the internal default transition `Transition()` if no transition has been
33 Transition* transition_ = default_transition_.get();
35 /** Sequential state ID (used for debugging) */
38 /** State's exploration status by actor. Not all the actors are there, only the ones that are ready-to-run in this
40 std::map<aid_t, ActorState> actors_to_run_;
42 /** Snapshot of system state (if needed) */
43 std::shared_ptr<Snapshot> system_state_;
45 /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
46 * set. With this information, it is check whether it should be removed from it or not when exploring a new
48 std::map<aid_t, Transition> sleep_set_;
51 explicit State(const RemoteApp& remote_app);
52 explicit State(const RemoteApp& remote_app, const State* previous_state);
53 /* Returns a positive number if there is another transition to pick, or -1 if not */
54 aid_t next_transition() const;
56 /* Explore a new path; the parameter must be the result of a previous call to next_transition() */
57 void execute_next(aid_t next);
59 long get_num() const { return num_; }
60 std::size_t count_todo() const;
61 void mark_todo(aid_t actor) { actors_to_run_.at(actor).mark_todo(); }
62 void mark_all_enabled_todo();
63 bool is_actor_done(aid_t actor) const { return actors_to_run_.at(actor).is_done(); }
64 Transition* get_transition() const;
65 void set_transition(Transition* t) { transition_ = t; }
66 std::map<aid_t, ActorState> const& get_actors_list() const { return actors_to_run_; }
68 unsigned long get_actor_count() const { return actors_to_run_.size(); }
69 bool is_actor_enabled(aid_t actor) { return actors_to_run_.at(actor).is_enabled(); }
71 Snapshot* get_system_state() const { return system_state_.get(); }
72 void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
74 std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
75 void add_sleep_set(Transition* t) {sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_)); }
77 /* Returns the total amount of states created so far (for statistics) */
78 static long get_expanded_states() { return expended_states_; }
80 } // namespace simgrid::mc