Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix MC+gcc builds
[simgrid.git] / src / mc / api / ActorState.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_PATTERN_H
7 #define SIMGRID_MC_PATTERN_H
8
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
11
12 #include <exception>
13 #include <vector>
14
15 namespace simgrid::mc {
16
17 /* On every state, each actor has an entry of the following type.
18  * This usually represents both the actor and its transition because
19  * most of the time an actor cannot have more than one enabled transition
20  * at a given time. However, certain transitions have multiple "paths"
21  * that can be followed, which means that a given actor may be able
22  * to do more than one thing at a time.
23  *
24  * Formally, at this state multiple transitions would exist all of
25  * which happened to be executed by the same actor. This distinction
26  * is important in cases
27  */
28 class ActorState {
29
30   /**
31    * @brief The transitions that the actor is allowed to execute from this
32    * state, viz. those that are enabled for this actor
33    *
34    * Most actors can take only a single action from any given state.
35    * However, when an actor executes a transition with multiple
36    * possible variations (e.g. an MC_Random() [see class: RandomTransition]
37    * for more details]), multiple enabled actions are defined
38    *
39    * @invariant The transitions are arranged such that an actor
40    * with multiple possible paths of execution will contain all
41    * such transitions such that `pending_transitions_[i]` represents
42    * the variation of the transition with `times_considered = i`.
43    *
44    * TODO: If only a subset of transitions of an actor that can
45    * take multiple transitions in some state are truly enabled,
46    * we would instead need to map `times_considered` to a transition,
47    * as the map is currently implicit in the ordering of the transitions
48    * in the vector
49    *
50    * TODO: If a single transition is taken at a time in a concurrent system,
51    * then nearly all of the transitions from in a state `s'` after taking
52    * an action `t` from state `s`  (i.e. s -- t --> s') are the same
53    * sans for the new transition of the actor which just executed t.
54    * This means there may be a way to store the list once and apply differences
55    * rather than repeating elements frequently.
56    */
57   std::vector<std::unique_ptr<Transition>> pending_transitions_;
58
59   /* Possible exploration status of an actor transition in a state.
60    * Either the checker did not consider the transition, or it was considered and still to do, or considered and
61    * done.
62    */
63   enum class InterleavingType {
64     /** This actor transition is not considered by the checker (yet?) */
65     disabled = 0,
66     /** The checker algorithm decided that this actor transitions should be done at some point */
67     todo,
68     /** The checker algorithm decided that this should be done, but it was done in the meanwhile */
69     done,
70   };
71
72   /** Exploration control information */
73   InterleavingType state_ = InterleavingType::disabled;
74
75   /** The ID of that actor */
76   const aid_t aid_;
77
78   /** Number of times that the actor was considered to be executed in previous explorations of the state space */
79   unsigned int times_considered_ = 0;
80   /** Maximal amount of times that the actor can be considered for execution in this state.
81    * If times_considered==max_consider, we fully explored that part of the state space */
82   unsigned int max_consider_ = 0;
83
84   /** Whether that actor is initially enabled in this state */
85   bool enabled_;
86
87 public:
88   ActorState(aid_t aid, bool enabled, unsigned int max_consider) : ActorState(aid, enabled, max_consider, {}) {}
89
90   ActorState(aid_t aid, bool enabled, unsigned int max_consider, std::vector<std::unique_ptr<Transition>> transitions)
91       : pending_transitions_(std::move(transitions)), aid_(aid), max_consider_(max_consider), enabled_(enabled)
92   {
93   }
94
95   unsigned int do_consider()
96   {
97     if (max_consider_ <= times_considered_ + 1)
98       set_done();
99     return times_considered_++;
100   }
101   unsigned int get_times_considered() const { return times_considered_; }
102   aid_t get_aid() const { return aid_; }
103
104   /* returns whether the actor is marked as enabled in the application side */
105   bool is_enabled() const { return enabled_; }
106   /* returns whether the actor is marked as disabled by the exploration algorithm */
107   bool is_disabled() const { return this->state_ == InterleavingType::disabled; }
108   bool is_done() const { return this->state_ == InterleavingType::done; }
109   bool is_todo() const { return this->state_ == InterleavingType::todo; }
110   /** Mark that we should try executing this process at some point in the future of the checker algorithm */
111   void mark_todo()
112   {
113     this->state_            = InterleavingType::todo;
114     this->times_considered_ = 0;
115   }
116   void set_done() { this->state_ = InterleavingType::done; }
117
118   inline Transition* get_transition(unsigned times_considered)
119   {
120     xbt_assert(times_considered < this->pending_transitions_.size(),
121                "Actor %ld does not have a state available transition with `times_considered = %u`,\n"
122                "yet one was asked for",
123                aid_, times_considered);
124     return this->pending_transitions_[times_considered].get();
125   }
126
127   inline void set_transition(std::unique_ptr<Transition> t, unsigned times_considered)
128   {
129     xbt_assert(times_considered < this->pending_transitions_.size(),
130                "Actor %ld does not have a state available transition with `times_considered = %u`, "
131                "yet one was attempted to be set",
132                aid_, times_considered);
133     this->pending_transitions_[times_considered] = std::move(t);
134   }
135 };
136
137 } // namespace simgrid::mc
138
139 #endif