Logo AND Algorithmique Numérique Distribuée

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