Logo AND Algorithmique Numérique Distribuée

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