Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start snake_case()ing some private fields
[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 const 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 /** @ingroup SURF_interface
28  * @brief SURF action interface class
29  * @details An action is an event generated by a resource (e.g.: a communication for the network)
30  */
31 class XBT_PUBLIC Action {
32 public:
33   boost::intrusive::list_member_hook<> modifiedSetHook_; /* Used by the lazy update to list the actions to track */
34   bool isLinkedModifiedSet() const { return modifiedSetHook_.is_linked(); }
35
36   typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modifiedSetHook_>
37       ActionLmmOptions;
38   typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
39
40   boost::intrusive::list_member_hook<> stateSetHook_;
41   typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::stateSetHook_>
42       ActionOptions;
43   typedef boost::intrusive::list<Action, ActionOptions> ActionList;
44
45   enum class State {
46     ready = 0,        /**< Ready        */
47     running,          /**< Running      */
48     failed,           /**< Task Failure */
49     done,             /**< Completed    */
50     to_free,          /**< Action to free in next cleanup */
51     not_in_the_system /**< Not in the system anymore. Why did you ask ? */
52   };
53
54   enum class SuspendStates {
55     not_suspended = 0, /**< Action currently not suspended **/
56     suspended,
57     sleeping
58   };
59
60   enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
61
62   /**
63    * @brief Action constructor
64    *
65    * @param model The Model associated to this Action
66    * @param cost The cost of the Action
67    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
68    */
69   Action(simgrid::kernel::resource::Model* model, double cost, bool failed);
70
71   /**
72    * @brief Action constructor
73    *
74    * @param model The Model associated to this Action
75    * @param cost The cost of the Action
76    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
77    * @param var The lmm variable associated to this Action if it is part of a LMM component
78    */
79   Action(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var);
80
81   virtual ~Action();
82
83   /**
84    * @brief Mark that the action is now finished
85    *
86    * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
87    */
88   void finish(Action::State state);
89
90   /** @brief Get the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
91   Action::State getState() const; /**< get the state*/
92   /** @brief Set the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
93   virtual void setState(Action::State state);
94
95   /** @brief Get the bound of the current Action */
96   double getBound() const;
97   /** @brief Set the bound of the current Action */
98   void setBound(double bound);
99
100   /** @brief Get the start time of the current action */
101   double getStartTime() const { return start_; }
102   /** @brief Get the finish time of the current action */
103   double getFinishTime() const { return finish_time_; }
104
105   /** @brief Get the user data associated to the current action */
106   void* getData() const { return data_; }
107   /** @brief Set the user data associated to the current action */
108   void setData(void* data) { data_ = data; }
109
110   /** @brief Get the cost of the current action */
111   double getCost() const { return cost_; }
112   /** @brief Set the cost of the current action */
113   void setCost(double cost) { cost_ = cost; }
114
115   /** @brief Update the maximum duration of the current action
116    *  @param delta Amount to remove from the MaxDuration */
117   void updateMaxDuration(double delta);
118
119   /** @brief Update the remaining time of the current action
120    *  @param delta Amount to remove from the remaining time */
121   void updateRemains(double delta);
122
123   /** @brief Set the remaining time of the current action */
124   void setRemains(double value) { remains_ = value; }
125   /** @brief Get the remaining time of the current action after updating the resource */
126   virtual double getRemains();
127   /** @brief Get the remaining time of the current action without updating the resource */
128   double getRemainsNoUpdate() const { return remains_; }
129
130   /** @brief Set the finish time of the current action */
131   void setFinishTime(double value) { finish_time_ = value; }
132
133   /**@brief Add a reference to the current action (refcounting) */
134   void ref();
135   /** @brief Unref that action (and destroy it if refcount reaches 0)
136    *  @return true if the action was destroyed and false if someone still has references on it
137    */
138   virtual int unref();
139
140   /** @brief Cancel the current Action if running */
141   virtual void cancel();
142
143   /** @brief Suspend the current Action */
144   virtual void suspend();
145
146   /** @brief Resume the current Action */
147   virtual void resume();
148
149   /** @brief Returns true if the current action is running */
150   virtual bool isSuspended();
151
152   /** @brief Get the maximum duration of the current action */
153   double getMaxDuration() const { return max_duration_; }
154   /** @brief Set the maximum duration of the current Action */
155   virtual void setMaxDuration(double duration);
156
157   /** @brief Get the tracing category associated to the current action */
158   char* getCategory() const { return category_; }
159   /** @brief Set the tracing category of the current Action */
160   void setCategory(const char* category);
161
162   /** @brief Get the priority of the current Action */
163   double getPriority() const { return sharing_weight_; };
164   /** @brief Set the priority of the current Action */
165   virtual void setSharingWeight(double priority);
166   void setSharingWeightNoUpdate(double weight) { sharing_weight_ = weight; }
167
168   /** @brief Get the state set in which the action is */
169   ActionList* getStateSet() const { return state_set_; };
170
171   simgrid::kernel::resource::Model* getModel() const { return model_; }
172
173 protected:
174   ActionList* state_set_;
175   int refcount_ = 1;
176
177 private:
178   double sharing_weight_ = 1.0;             /**< priority (1.0 by default) */
179   double max_duration_   = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
180   double remains_;           /**< How much of that cost remains to be done in the currently running task */
181   double start_;             /**< start time  */
182   char* category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
183   double finish_time_ =
184       -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
185
186   double cost_;
187   simgrid::kernel::resource::Model* model_;
188   void* data_ = nullptr; /**< for your convenience */
189
190   /* LMM */
191   double last_update_                                  = 0;
192   double last_value_                                   = 0;
193   kernel::lmm::Variable* variable_                    = nullptr;
194   Action::Type type_                                  = Action::Type::NOTSET;
195   boost::optional<heap_type::handle_type> heap_handle_ = boost::none;
196
197 public:
198   virtual void updateRemainingLazy(double now) = 0;
199   void heapInsert(heap_type& heap, double key, Action::Type hat);
200   void heapRemove(heap_type& heap);
201   void heapUpdate(heap_type& heap, double key, Action::Type hat);
202   void clearHeapHandle() { heap_handle_ = boost::none; }
203   kernel::lmm::Variable* getVariable() const { return variable_; }
204   void setVariable(kernel::lmm::Variable* var) { variable_ = var; }
205   double getLastUpdate() const { return last_update_; }
206   void refreshLastUpdate();
207   double getLastValue() const { return last_value_; }
208   void setLastValue(double val) { last_value_ = val; }
209   Action::Type getType() const { return type_; }
210
211 protected:
212   Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
213 };
214
215 typedef Action::ActionList ActionList;
216 typedef Action::ActionLmmList ActionLmmList;
217 typedef Action::ActionLmmList* ActionLmmListPtr;
218 } // namespace resource
219 } // namespace kernel
220 } // namespace simgrid
221 #endif