Logo AND Algorithmique Numérique Distribuée

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