Logo AND Algorithmique Numérique Distribuée

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