Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a9dd2c43d02c784f88c879c7f64151c1c8ee4f48
[simgrid.git] / src / mc / api / State.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_MC_STATE_HPP
7 #define SIMGRID_MC_STATE_HPP
8
9 #include "src/mc/api/ActorState.hpp"
10 #include "src/mc/api/RemoteApp.hpp"
11 #include "src/mc/api/guide/GuidedState.hpp"
12 #include "src/mc/sosp/Snapshot.hpp"
13 #include "src/mc/transition/Transition.hpp"
14
15 namespace simgrid::mc {
16
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 */
20
21   /**
22    * @brief An empty transition that leads to this state by default
23    */
24   const std::unique_ptr<Transition> default_transition_ = std::make_unique<Transition>();
25
26   /**
27    * @brief The outgoing transition: what was the last transition that
28    * we took to leave this state?
29    *
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
32    * set
33    */
34   Transition* transition_ = default_transition_.get();
35
36   /** Sequential state ID (used for debugging) */
37   long num_ = 0;
38
39   /** Snapshot of system state (if needed) */
40   std::shared_ptr<Snapshot> system_state_;
41
42   /** Unique parent of this state. Required both for sleep set computation
43       and for guided model-checking */
44   const State* parent_state_;
45
46   std::unique_ptr<GuidedState> guide;
47
48   /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
49    * set. With this information, it is check whether it should be removed from it or not when exploring a new
50    * transition */
51   std::map<aid_t, Transition> sleep_set_;
52
53 public:
54   explicit State(RemoteApp& remote_app);
55   explicit State(RemoteApp& remote_app, const State* parent_state);
56   /* Returns a positive number if there is another transition to pick, or -1 if not */
57   aid_t next_transition() const;
58
59   /* Explore a new path on the remote app; the parameter 'next' must be the result of a previous call to
60    * next_transition() */
61   void execute_next(aid_t next, RemoteApp& app);
62
63   long get_num() const { return num_; }
64   std::size_t count_todo() const;
65   void mark_todo(aid_t actor) { guide->actors_to_run_.at(actor).mark_todo(); }
66   void mark_all_enabled_todo();
67   bool is_actor_done(aid_t actor) const { return guide->actors_to_run_.at(actor).is_done(); }
68   Transition* get_transition() const;
69   void set_transition(Transition* t) { transition_ = t; }
70   std::map<aid_t, ActorState> const& get_actors_list() const { return guide->actors_to_run_; }
71
72   unsigned long get_actor_count() const { return guide->actors_to_run_.size(); }
73   bool is_actor_enabled(aid_t actor) { return guide->actors_to_run_.at(actor).is_enabled(); }
74
75   Snapshot* get_system_state() const { return system_state_.get(); }
76   void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
77
78   std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
79   void add_sleep_set(Transition* t)
80   {
81     sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_));
82   }
83
84   /* Returns the total amount of states created so far (for statistics) */
85   static long get_expanded_states() { return expended_states_; }
86 };
87 } // namespace simgrid::mc
88
89 #endif