Logo AND Algorithmique Numérique Distribuée

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