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_PATTERN_H
7 #define SIMGRID_MC_PATTERN_H
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
15 namespace simgrid::mc {
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.
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
30 * @brief The transitions that the actor is allowed to execute from this
31 * state, viz. those that are enabled for this actor
33 * Most actors can take only a single action from any given state.
34 * However, when an actor executes a transition with multiple
35 * possible variations (e.g. an MC_Random() [see class: RandomTransition]
36 * for more details]), multiple enabled actions are defined
38 * @invariant The transitions are arranged such that an actor
39 * with multiple possible paths of execution will contain all
40 * such transitions such that `pending_transitions_[i]` represents
41 * the variation of the transition with `times_considered = i`.
43 * @note: If only a subset of transitions of an actor that can
44 * take multiple transitions in some state are truly enabled,
45 * we would instead need to map `times_considered` to a transition,
46 * as the map is currently implicit in the ordering of the transitions
49 * TODO: If a single transition is taken at a time in a concurrent system,
50 * then nearly all of the transitions from in a state `s'` after taking
51 * an action `t` from state `s` (i.e. s -- t --> s') are the same
52 * sans for the new transition of the actor which just executed t.
53 * This means there may be a way to store the list once and apply differences
54 * rather than repeating elements frequently.
56 std::vector<std::shared_ptr<Transition>> pending_transitions_;
58 /* Possible exploration status of an actor transition in a state.
59 * Either the checker did not consider the transition, or it was considered and still to do, or considered and
62 enum class InterleavingType {
63 /** This actor transition is not considered by the checker (yet?) */
65 /** The checker algorithm decided that this actor transitions should be done at some point */
67 /** The checker algorithm decided that this should be done, but it was done in the meanwhile */
71 /** Exploration control information */
72 InterleavingType state_ = InterleavingType::disabled;
74 /** The ID of that actor */
77 /** Number of times that the actor was considered to be executed in previous explorations of the state space */
78 unsigned int times_considered_ = 0;
79 /** Maximal amount of times that the actor can be considered for execution in this state.
80 * If times_considered==max_consider, we fully explored that part of the state space */
81 unsigned int max_consider_ = 0;
83 /** Whether that actor is initially enabled in this state */
87 ActorState(aid_t aid, bool enabled, unsigned int max_consider) : ActorState(aid, enabled, max_consider, {}) {}
89 ActorState(aid_t aid, bool enabled, unsigned int max_consider, std::vector<std::shared_ptr<Transition>> transitions)
90 : pending_transitions_(std::move(transitions)), aid_(aid), max_consider_(max_consider), enabled_(enabled)
94 unsigned int do_consider()
96 if (max_consider_ <= times_considered_ + 1)
98 return times_considered_++;
100 unsigned int get_times_considered() const { return times_considered_; }
101 unsigned int get_times_not_considered() const { return max_consider_ - times_considered_; }
102 aid_t get_aid() const { return aid_; }
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 */
113 this->state_ = InterleavingType::todo;
114 this->times_considered_ = 0;
116 void mark_done() { this->state_ = InterleavingType::done; }
118 inline Transition* get_transition(unsigned times_considered) const
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();
127 inline void set_transition(std::shared_ptr<Transition> t, unsigned times_considered)
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);
136 const std::vector<std::shared_ptr<Transition>>& get_enabled_transitions() const
138 return this->pending_transitions_;
142 } // namespace simgrid::mc