Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop using default mpi_call
[simgrid.git] / include / simgrid / kernel / resource / Action.hpp
1 /* Copyright (c) 2004-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_KERNEL_RESOURCE_ACTION_HPP
7 #define SIMGRID_KERNEL_RESOURCE_ACTION_HPP
8
9 #include <simgrid/forward.h>
10 #include <xbt/signal.hpp>
11 #include <xbt/utility.hpp>
12
13 #include <boost/heap/pairing_heap.hpp>
14 #include <boost/optional.hpp>
15 #include <string>
16 #include <string_view>
17
18 static constexpr double NO_MAX_DURATION = -1.0;
19
20 namespace simgrid::kernel::resource {
21
22 using heap_element_type = std::pair<double, Action*>;
23 using heap_type =
24     boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
25                               boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>;
26
27 class XBT_PUBLIC ActionHeap : public heap_type {
28   friend Action;
29
30 public:
31   enum class Type {
32     latency = 100, /* this is a heap entry to warn us when the latency is paid */
33     max_duration,  /* this is a heap entry to warn us when the max_duration limit (timeout) is reached */
34     normal,        /* this is a normal heap entry stating the date to finish transmitting */
35     unset
36   };
37
38   double top_date() const;
39   void insert(Action* action, double date, ActionHeap::Type type);
40   void update(Action* action, double date, ActionHeap::Type type);
41   void remove(Action* action);
42   Action* pop();
43 };
44
45 /** @details An action is a consumption on a resource (e.g.: a communication for the network).
46  *
47  * It is related (but still different) from activities, that are the stuff on which an actor can be blocked.
48  *
49  * - A sequential execution activity encompasses 2 actions: one for the exec itself,
50  *   and a time-limited sleep used as timeout detector.
51  * - A point-to-point communication activity encompasses 3 actions: one for the comm itself
52  *   (which spans on all links of the path), and one infinite sleep used as failure detector
53  *   on both sender and receiver hosts.
54  * - Synchronization activities may possibly be connected to no action.
55
56  */
57 class XBT_PUBLIC Action {
58   friend ActionHeap;
59
60   int refcount_           = 1;
61   double sharing_penalty_ = 1.0;             /**< priority (1.0 by default) */
62   double max_duration_    = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
63   double remains_;          /**< How much of that cost remains to be done in the currently running task */
64   double start_time_;       /**< start time  */
65   double finish_time_ = -1; /**< finish time (may fluctuate until the task is completed) */
66   std::string category_;    /**< tracing category for categorized resource utilization monitoring */
67
68   double cost_;
69   Model* model_;
70   void* data_                       = nullptr; /**< for your convenience - XBT_ATTRIB_DEPRECATED_v334 */
71   activity::ActivityImpl* activity_ = nullptr;
72
73   /* LMM */
74   double factor_           = 1.0; /**< Factor for effective rate = var->get_value() * factor_ */
75   double last_update_      = 0;
76   double last_value_       = 0;
77   lmm::Variable* variable_ = nullptr;
78   double user_bound_       = -1;
79
80   ActionHeap::Type type_                              = ActionHeap::Type::unset;
81   boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
82   boost::intrusive::list_member_hook<> modified_set_hook_;
83   boost::intrusive::list_member_hook<> state_set_hook_;
84
85 public:
86   /* Lazy update needs this Set hook to maintain a list of the tracked actions */
87   bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
88   using ModifiedSet = boost::intrusive::list<
89       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>;
90
91   using StateSet = boost::intrusive::list<
92       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>;
93
94   enum class State {
95     INITED,   /**< Created, but not started yet */
96     STARTED,  /**< Currently running */
97     FAILED,   /**< either the resource failed, or the action was canceled */
98     FINISHED, /**< Successfully completed  */
99     IGNORED   /**< e.g. failure detectors: infinite sleep actions that are put on resources which failure should get
100                  noticed  */
101   };
102
103   enum class SuspendStates {
104     RUNNING = 0, /**< Action currently not suspended **/
105     SUSPENDED,
106     SLEEPING
107   };
108
109 private:
110   StateSet* state_set_;
111   Action::SuspendStates suspended_ = Action::SuspendStates::RUNNING;
112
113 public:
114   /**
115    * @brief Action constructor
116    *
117    * @param model The Model associated to this Action
118    * @param cost The cost of the Action
119    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
120    */
121   Action(Model* model, double cost, bool failed);
122
123   /**
124    * @brief Action constructor
125    *
126    * @param model The Model associated to this Action
127    * @param cost The cost of the Action
128    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
129    * @param var The lmm variable associated to this Action if it is part of a LMM component
130    */
131   Action(Model* model, double cost, bool failed, lmm::Variable* var);
132   Action(const Action&) = delete;
133   Action& operator=(const Action&) = delete;
134
135   virtual ~Action();
136
137   /**
138    * @brief Mark that the action is now finished
139    *
140    * @param state the new [state](@ref simgrid::kernel::resource::Action::State) of the current Action
141    */
142   void finish(Action::State state);
143
144   /** @brief Get the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
145   Action::State get_state() const; /**< get the state*/
146   /** @brief Set the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
147   virtual void set_state(Action::State state);
148
149   /** @brief Get the bound of the current Action */
150   double get_bound() const;
151   /** @brief Set the bound of the current Action */
152   void set_bound(double bound);
153
154   /** @brief Get the start time of the current action */
155   double get_start_time() const { return start_time_; }
156   /** @brief Get the finish time of the current action */
157   double get_finish_time() const { return finish_time_; }
158
159   /** @brief Get the user data associated to the current action */
160   XBT_ATTRIB_DEPRECATED_v334("Please manifest if you actually need this function") void* get_data() const
161   {
162     return data_;
163   }
164   /** @brief Set the user data associated to the current action */
165   XBT_ATTRIB_DEPRECATED_v334("Please manifest if you actually need this function") void set_data(void* data)
166   {
167     data_ = data;
168   }
169
170   /** @brief Get the user data associated to the current action */
171   activity::ActivityImpl* get_activity() const { return activity_; }
172   /** @brief Set the user data associated to the current action */
173   void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
174
175   /** @brief Get the cost of the current action */
176   double get_cost() const { return cost_; }
177   /** @brief Set the cost of the current action */
178   void set_cost(double cost) { cost_ = cost; }
179
180   /** @brief Update the maximum duration of the current action
181    *  @param delta Amount to remove from the MaxDuration */
182   void update_max_duration(double delta);
183
184   /** @brief Update the remaining time of the current action
185    *  @param delta Amount to remove from the remaining time */
186   void update_remains(double delta);
187
188   virtual void update_remains_lazy(double now) = 0;
189
190   /** @brief Set the remaining time of the current action */
191   void set_remains(double value) { remains_ = value; }
192
193   /** @brief Get the remaining time of the current action after updating the resource */
194   virtual double get_remains();
195   /** @brief Get the remaining time of the current action without updating the resource */
196   double get_remains_no_update() const { return remains_; }
197
198   /** @brief Set the finish time of the current action */
199   void set_finish_time(double value) { finish_time_ = value; }
200
201   /**@brief Add a reference to the current action (refcounting) */
202   void ref();
203   /** @brief Unref that action (and destroy it if refcount reaches 0)
204    *  @return true if the action was destroyed and false if someone still has references on it */
205   bool unref();
206
207   /** @brief Cancel the current Action if running */
208   virtual void cancel();
209
210   /** @brief Suspend the current Action */
211   virtual void suspend();
212
213   /** @brief Resume the current Action */
214   virtual void resume();
215
216   /** @brief Returns true if the current action is suspended */
217   bool is_suspended() const { return suspended_ == SuspendStates::SUSPENDED; }
218   /** @brief Returns true if the current action is running */
219   bool is_running() const { return suspended_ == SuspendStates::RUNNING; }
220
221   /** @brief Get the maximum duration of the current action */
222   double get_max_duration() const { return max_duration_; }
223   /** @brief Set the maximum duration of the current Action */
224   virtual void set_max_duration(double duration);
225
226   /** @brief Get the tracing category associated to the current action */
227   const std::string& get_category() const { return category_; }
228   /** @brief Set the tracing category of the current Action */
229   void set_category(std::string_view category) { category_ = category; }
230
231   /** @brief Get the sharing_penalty (RTT or 1/thread_count) of the current Action */
232   double get_sharing_penalty() const { return sharing_penalty_; };
233   /** @brief Set the sharing_penalty (RTT or 1/thread_count) of the current Action */
234   virtual void set_sharing_penalty(double sharing_penalty);
235   void set_sharing_penalty_no_update(double sharing_penalty) { sharing_penalty_ = sharing_penalty; }
236
237   /** @brief Get the state set in which the action is */
238   StateSet* get_state_set() const { return state_set_; };
239
240   Model* get_model() const { return model_; }
241
242   ActionHeap::Type get_type() const { return type_; }
243
244   lmm::Variable* get_variable() const { return variable_; }
245   void set_variable(lmm::Variable* var) { variable_ = var; }
246
247   double get_user_bound() const { return user_bound_; }
248   void set_user_bound(double bound) { user_bound_ = bound; }
249
250   double get_last_update() const { return last_update_; }
251   void set_last_update();
252
253   /**
254    * @brief Set a factor for this action
255    *
256    * Defines a multiplicative factor for the consumption of the underlying resource.
257    *
258    * @param factor Multiplicative factor for this action (e.g. 0.97)
259    */
260   void set_rate_factor(double factor) { factor_ = factor; }
261   /**
262    * @brief Get the effective consumption rate of the resource
263    *
264    * The rate is based on the sharing given by the maxmin system underneath.
265    * However, it depends on the factor defined for this action.
266    *
267    * So, the effective rate is equal to var->get_value() * factor_
268    */
269   double get_rate() const;
270   double get_last_value() const { return last_value_; }
271   void set_last_value(double val) { last_value_ = val; }
272   void set_suspend_state(Action::SuspendStates state) { suspended_ = state; }
273 };
274
275 } // namespace simgrid::kernel::resource
276 #endif