Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d0752d25a91afd6be06885c9217539d4bc5a233f
[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.
33    *
34    * TODO: If a single transition is taken at a time in a concurrent system,
35    * then nearly all of the transitions from in a state `s'` after taking
36    * an action `t` from state `s`  (i.e. s -- t --> s') are the same
37    * sans for the new transition of the actor which just executed t.
38    * This means there may be a way to store the list once and apply differences
39    * rather than repeating elements frequently.
40    */
41   std::vector<std::unique_ptr<Transition>> pending_transitions_;
42
43   /* Possible exploration status of an actor transition in a state.
44    * Either the checker did not consider the transition, or it was considered and still to do, or considered and
45    * done.
46    */
47   enum class InterleavingType {
48     /** This actor transition is not considered by the checker (yet?) */
49     disabled = 0,
50     /** The checker algorithm decided that this actor transitions should be done at some point */
51     todo,
52     /** The checker algorithm decided that this should be done, but it was done in the meanwhile */
53     done,
54   };
55
56   /** Exploration control information */
57   InterleavingType state_ = InterleavingType::disabled;
58
59   /** The ID of that actor */
60   const aid_t aid_;
61
62   /** Number of times that the actor was considered to be executed in previous explorations of the state space */
63   unsigned int times_considered_ = 0;
64   /** Maximal amount of times that the actor can be considered for execution in this state.
65    * If times_considered==max_consider, we fully explored that part of the state space */
66   unsigned int max_consider_ = 0;
67
68   /** Whether that actor is initially enabled in this state */
69   bool enabled_;
70
71 public:
72   ActorState(aid_t aid, bool enabled, unsigned int max_consider) : ActorState(aid, enabled, max_consider, {}) {}
73
74   ActorState(aid_t aid, bool enabled, unsigned int max_consider, std::vector<std::unique_ptr<Transition>> transitions)
75       : aid_(aid), max_consider_(max_consider), enabled_(enabled), pending_transitions_(std::move(transitions))
76   {
77   }
78
79   unsigned int do_consider()
80   {
81     if (max_consider_ <= times_considered_ + 1)
82       set_done();
83     return times_considered_++;
84   }
85   unsigned int get_times_considered() const { return times_considered_; }
86   aid_t get_aid() const { return aid_; }
87
88   /* returns whether the actor is marked as enabled in the application side */
89   bool is_enabled() const { return enabled_; }
90   /* returns whether the actor is marked as disabled by the exploration algorithm */
91   bool is_disabled() const { return this->state_ == InterleavingType::disabled; }
92   bool is_done() const { return this->state_ == InterleavingType::done; }
93   bool is_todo() const { return this->state_ == InterleavingType::todo; }
94   /** Mark that we should try executing this process at some point in the future of the checker algorithm */
95   void mark_todo()
96   {
97     this->state_            = InterleavingType::todo;
98     this->times_considered_ = 0;
99   }
100   void set_done() { this->state_ = InterleavingType::done; }
101
102   Transition* get_transition(unsigned times_considered)
103   {
104     xbt_assert(times_considered <= this->pending_transitions_.size(),
105                "Actor %lu does not have a state available transition with `times_considered = %d`,\n"
106                "yet one was asked for",
107                aid_, times_considered);
108     return this->pending_transitions_[times_considered].get();
109   }
110 };
111
112 } // namespace simgrid::mc
113
114 #endif