Logo AND Algorithmique Numérique Distribuée

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