Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that we never have a 0 transition at the end of the stack
[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/strategy/Strategy.hpp"
12 #include "src/mc/transition/Transition.hpp"
13
14 #if SIMGRID_HAVE_STATEFUL_MC
15 #include "src/mc/sosp/Snapshot.hpp"
16 #endif
17
18 namespace simgrid::mc {
19
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 */
23
24   /**
25    * @brief The outgoing transition: what was the last transition that we took to leave this state?
26    *
27    * The owner of the transition is the `ActorState` instance which exists in this state.
28    */
29   Transition* transition_ = nullptr;
30
31   /** @brief A list of transition to be replayed in order to get in this state. */
32   std::list<Transition*> recipe_;
33
34   /** Sequential state ID (used for debugging) */
35   long num_ = 0;
36
37   /** Snapshot of system state (if needed) */
38   std::shared_ptr<Snapshot> system_state_;
39
40   /** Unique parent of this state. Required both for sleep set computation
41       and for guided model-checking */
42   std::shared_ptr<State> parent_state_ = nullptr;
43
44   std::shared_ptr<Strategy> strategy_;
45
46   /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
47    * set. With this information, it is check whether it should be removed from it or not when exploring a new
48    * transition */
49   std::map<aid_t, Transition> sleep_set_;
50
51 public:
52   explicit State(RemoteApp& remote_app);
53   explicit State(RemoteApp& remote_app, std::shared_ptr<State> parent_state);
54   /* Returns a positive number if there is another transition to pick, or -1 if not */
55   aid_t next_transition() const; // this function should disapear as it is redundant with the next one
56
57   /* Same as next_transition, but choice is now guided, and an integer corresponding to the
58    internal cost of the transition is returned */
59   std::pair<aid_t, int> next_transition_guided() const;
60
61   /* Explore a new path on the remote app; the parameter 'next' must be the result of a previous call to
62    * next_transition() */
63   void execute_next(aid_t next, RemoteApp& app);
64
65   long get_num() const { return num_; }
66   std::size_t count_todo() const;
67   std::size_t count_todo_multiples() const;
68
69   /* Marking as TODO some actor in this state:
70    *  + consider_one mark aid actor (and assert it is possible)
71    *  + consider_best ensure one actor is marked by eventually marking the best regarding its guiding methode
72    *  + conside_all mark all enabled actor that are not done yet */
73   void consider_one(aid_t aid) const { strategy_->consider_one(aid); }
74   void consider_best() const { strategy_->consider_best(); }
75   unsigned long consider_all() const { return strategy_->consider_all(); }
76
77   bool is_actor_done(aid_t actor) const { return strategy_->actors_to_run_.at(actor).is_done(); }
78   Transition* get_transition() const;
79   void set_transition(Transition* t) { transition_ = t; }
80   std::shared_ptr<State> get_parent_state() const { return parent_state_; }
81   std::list<Transition*> get_recipe() const { return recipe_; }
82
83   std::map<aid_t, ActorState> const& get_actors_list() const { return strategy_->actors_to_run_; }
84
85   unsigned long get_actor_count() const { return strategy_->actors_to_run_.size(); }
86   bool is_actor_enabled(aid_t actor) const { return strategy_->actors_to_run_.at(actor).is_enabled(); }
87
88   Snapshot* get_system_state() const { return system_state_.get(); }
89   void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
90
91   std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
92   void add_sleep_set(const Transition* t)
93   {
94     sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_));
95   }
96
97   /* Returns the total amount of states created so far (for statistics) */
98   static long get_expanded_states() { return expended_states_; }
99 };
100 } // namespace simgrid::mc
101
102 #endif