Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar (protected fields) and coding standards (uppercase enum)
[simgrid.git] / include / simgrid / kernel / resource / Action.hpp
1 /* Copyright (c) 2004-2019. 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 int NO_MAX_DURATION = -1.0;
18
19 namespace simgrid {
20 namespace kernel {
21 namespace resource {
22
23 typedef std::pair<double, simgrid::kernel::resource::Action*> heap_element_type;
24 typedef 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     heap_type;
27
28 typedef std::pair<double, simgrid::kernel::resource::Action*> heap_element_type;
29 class XBT_PUBLIC ActionHeap : public heap_type {
30   friend Action;
31
32 public:
33   enum class Type {
34     latency = 100, /* this is a heap entry to warn us when the latency is payed */
35     max_duration,  /* this is a heap entry to warn us when the max_duration limit (timeout) is reached */
36     normal,        /* this is a normal heap entry stating the date to finish transmitting */
37     unset
38   };
39
40   double top_date() const;
41   void insert(Action* action, double date, ActionHeap::Type type);
42   void update(Action* action, double date, ActionHeap::Type type);
43   void remove(Action* action);
44   Action* pop();
45 };
46
47 /** @details An action is a consumption on a resource (e.g.: a communication for the network).
48  *
49  * It is related (but still different) from activities, that are the stuff on which an actor can be blocked.
50  * See simgrid::s4u::Activity for more details.
51  */
52 class XBT_PUBLIC Action {
53   friend ActionHeap;
54
55 public:
56   /* Lazy update needs this Set hook to maintain a list of the tracked actions */
57   boost::intrusive::list_member_hook<> modified_set_hook_;
58   bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
59   typedef boost::intrusive::list<
60       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
61       ModifiedSet;
62
63   boost::intrusive::list_member_hook<> state_set_hook_;
64   typedef boost::intrusive::list<
65       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
66       StateSet;
67
68   enum class State {
69     INITED,   /**< Created, but not started yet */
70     STARTED,  /**< Currently running */
71     FAILED,   /**< either the resource failed, or the action was canceled */
72     FINISHED, /**< Successfully completed  */
73     IGNORED   /**< e.g. failure detectors: infinite sleep actions that are put on resources which failure should get
74                  noticed  */
75   };
76
77   enum class SuspendStates {
78     RUNNING = 0, /**< Action currently not suspended **/
79     SUSPENDED,
80     SLEEPING
81   };
82
83   /**
84    * @brief Action constructor
85    *
86    * @param model The Model associated to this Action
87    * @param cost The cost of the Action
88    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
89    */
90   Action(Model* model, double cost, bool failed);
91
92   /**
93    * @brief Action constructor
94    *
95    * @param model The Model associated to this Action
96    * @param cost The cost of the Action
97    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
98    * @param var The lmm variable associated to this Action if it is part of a LMM component
99    */
100   Action(Model* model, double cost, bool failed, lmm::Variable* var);
101   Action(const Action&) = delete;
102   Action& operator=(const Action&) = delete;
103
104   virtual ~Action();
105
106   /**
107    * @brief Mark that the action is now finished
108    *
109    * @param state the new [state](@ref simgrid::kernel::resource::Action::State) of the current Action
110    */
111   void finish(Action::State state);
112
113   /** @brief Get the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
114   Action::State get_state() const; /**< get the state*/
115   /** @brief Set the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
116   virtual void set_state(Action::State state);
117
118   /** @brief Get the bound of the current Action */
119   double get_bound() const;
120   /** @brief Set the bound of the current Action */
121   void set_bound(double bound);
122
123   /** @brief Get the start time of the current action */
124   double get_start_time() const { return start_time_; }
125   /** @brief Get the finish time of the current action */
126   double get_finish_time() const { return finish_time_; }
127
128   /** @brief Get the user data associated to the current action */
129   void* get_data() const { return data_; }
130   /** @brief Set the user data associated to the current action */
131   void set_data(void* data) { data_ = data; }
132
133   /** @brief Get the user data associated to the current action */
134   activity::ActivityImpl* get_activity() const { return activity_; }
135   /** @brief Set the user data associated to the current action */
136   void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
137
138   /** @brief Get the cost of the current action */
139   double get_cost() const { return cost_; }
140   /** @brief Set the cost of the current action */
141   void set_cost(double cost) { cost_ = cost; }
142
143   /** @brief Update the maximum duration of the current action
144    *  @param delta Amount to remove from the MaxDuration */
145   void update_max_duration(double delta);
146
147   /** @brief Update the remaining time of the current action
148    *  @param delta Amount to remove from the remaining time */
149   void update_remains(double delta);
150
151   virtual void update_remains_lazy(double now) = 0;
152
153   /** @brief Set the remaining time of the current action */
154   void set_remains(double value) { remains_ = value; }
155
156   /** @brief Get the remaining time of the current action after updating the resource */
157   virtual double get_remains();
158   /** @brief Get the remaining time of the current action without updating the resource */
159   double get_remains_no_update() const { return remains_; }
160
161   /** @brief Set the finish time of the current action */
162   void set_finish_time(double value) { finish_time_ = value; }
163
164   /**@brief Add a reference to the current action (refcounting) */
165   void ref();
166   /** @brief Unref that action (and destroy it if refcount reaches 0)
167    *  @return true if the action was destroyed and false if someone still has references on it */
168   bool unref();
169
170   /** @brief Cancel the current Action if running */
171   virtual void cancel();
172
173   /** @brief Suspend the current Action */
174   virtual void suspend();
175
176   /** @brief Resume the current Action */
177   virtual void resume();
178
179   /** @brief Returns true if the current action is suspended */
180   bool is_suspended() const { return suspended_ == SuspendStates::SUSPENDED; }
181   /** @brief Returns true if the current action is running */
182   bool is_running() const { return suspended_ == SuspendStates::RUNNING; }
183
184   /** @brief Get the maximum duration of the current action */
185   double get_max_duration() const { return max_duration_; }
186   /** @brief Set the maximum duration of the current Action */
187   virtual void set_max_duration(double duration);
188
189   /** @brief Get the tracing category associated to the current action */
190   const std::string& get_category() const { return category_; }
191   /** @brief Set the tracing category of the current Action */
192   void set_category(const std::string& category) { category_ = category; }
193
194   /** @brief Get the priority of the current Action */
195   double get_priority() const { return sharing_priority_; };
196   /** @brief Set the priority of the current Action */
197   virtual void set_priority(double priority);
198   void set_priority_no_update(double priority) { sharing_priority_ = priority; }
199
200   /** @brief Get the state set in which the action is */
201   StateSet* get_state_set() const { return state_set_; };
202
203   simgrid::kernel::resource::Model* get_model() const { return model_; }
204
205 private:
206   StateSet* state_set_;
207   Action::SuspendStates suspended_ = Action::SuspendStates::RUNNING;
208   int refcount_            = 1;
209   double sharing_priority_ = 1.0;             /**< priority (1.0 by default) */
210   double max_duration_   = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
211   double remains_;           /**< How much of that cost remains to be done in the currently running task */
212   double start_time_;        /**< start time  */
213   double finish_time_ = -1;  /**< finish time (may fluctuate until the task is completed) */
214   std::string category_;     /**< tracing category for categorized resource utilization monitoring */
215
216   double cost_;
217   simgrid::kernel::resource::Model* model_;
218   void* data_                       = nullptr; /**< for your convenience */
219   activity::ActivityImpl* activity_ = nullptr;
220
221   /* LMM */
222   double last_update_                                = 0;
223   double last_value_                                 = 0;
224   kernel::lmm::Variable* variable_                   = nullptr;
225
226   ActionHeap::Type type_                              = ActionHeap::Type::unset;
227   boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
228
229 public:
230   ActionHeap::Type get_type() const { return type_; }
231
232   lmm::Variable* get_variable() const { return variable_; }
233   void set_variable(lmm::Variable* var) { variable_ = var; }
234
235   double get_last_update() const { return last_update_; }
236   void set_last_update();
237
238   double get_last_value() const { return last_value_; }
239   void set_last_value(double val) { last_value_ = val; }
240   void set_suspend_state(Action::SuspendStates state) { suspended_ = state; }
241
242 protected:
243 };
244
245 } // namespace resource
246 } // namespace kernel
247 } // namespace simgrid
248 #endif