Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case another method
[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 /** @details An action is a consumption on a resource (e.g.: a communication for the network) */
28 class XBT_PUBLIC Action {
29 public:
30   /* Lazy update needs this Set hook to maintain a list of the tracked actions */
31   boost::intrusive::list_member_hook<> modified_set_hook_;
32   bool isLinkedModifiedSet() const { return modified_set_hook_.is_linked(); }
33   typedef boost::intrusive::list<
34       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
35       ModifiedSet;
36
37   boost::intrusive::list_member_hook<> state_set_hook_;
38   typedef boost::intrusive::list<
39       Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
40       StateSet;
41
42   enum class State {
43     ready = 0,        /**< Ready        */
44     running,          /**< Running      */
45     failed,           /**< Task Failure */
46     done,             /**< Completed    */
47     to_free,          /**< Action to free in next cleanup */
48     not_in_the_system /**< Not in the system anymore. Why did you ask ? */
49   };
50
51   enum class SuspendStates {
52     not_suspended = 0, /**< Action currently not suspended **/
53     suspended,
54     sleeping
55   };
56
57   enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
58
59   /**
60    * @brief Action constructor
61    *
62    * @param model The Model associated to this Action
63    * @param cost The cost of the Action
64    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
65    */
66   Action(Model* model, double cost, bool failed);
67
68   /**
69    * @brief Action constructor
70    *
71    * @param model The Model associated to this Action
72    * @param cost The cost of the Action
73    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
74    * @param var The lmm variable associated to this Action if it is part of a LMM component
75    */
76   Action(Model* model, double cost, bool failed, lmm::Variable* var);
77
78   virtual ~Action();
79
80   /**
81    * @brief Mark that the action is now finished
82    *
83    * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
84    */
85   void finish(Action::State state);
86
87   /** @brief Get the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
88   Action::State get_state() const; /**< get the state*/
89   /** @brief Set the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
90   virtual void set_state(Action::State state);
91
92   /** @brief Get the bound of the current Action */
93   double get_bound() const;
94   /** @brief Set the bound of the current Action */
95   void set_bound(double bound);
96
97   /** @brief Get the start time of the current action */
98   double get_start_time() const { return start_time_; }
99   /** @brief Get the finish time of the current action */
100   double getFinishTime() const { return finish_time_; }
101
102   /** @brief Get the user data associated to the current action */
103   void* get_data() const { return data_; }
104   /** @brief Set the user data associated to the current action */
105   void set_data(void* data) { data_ = data; }
106
107   /** @brief Get the cost of the current action */
108   double get_cost() const { return cost_; }
109   /** @brief Set the cost of the current action */
110   void set_cost(double cost) { cost_ = cost; }
111
112   /** @brief Update the maximum duration of the current action
113    *  @param delta Amount to remove from the MaxDuration */
114   void update_max_duration(double delta);
115
116   /** @brief Update the remaining time of the current action
117    *  @param delta Amount to remove from the remaining time */
118   void update_remains(double delta);
119
120   virtual void update_remains_lazy(double now) = 0;
121
122   /** @brief Set the remaining time of the current action */
123   void set_remains(double value) { remains_ = value; }
124
125   /** @brief Get the remaining time of the current action after updating the resource */
126   virtual double get_remains();
127   /** @brief Get the remaining time of the current action without updating the resource */
128   double get_remains_no_update() const { return remains_; }
129
130   /** @brief Set the finish time of the current action */
131   void set_finish_time(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   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 get_max_duration() const { return max_duration_; }
154   /** @brief Set the maximum duration of the current Action */
155   virtual void set_max_duration(double duration);
156
157   /** @brief Get the tracing category associated to the current action */
158   char* get_category() const { return category_; }
159   /** @brief Set the tracing category of the current Action */
160   void set_category(const char* category);
161
162   /** @brief Get the priority of the current Action */
163   double get_priority() const { return sharing_priority_; };
164   /** @brief Set the priority of the current Action */
165   virtual void set_priority(double priority);
166   void set_priority_no_update(double priority) { sharing_priority_ = priority; }
167
168   /** @brief Get the state set in which the action is */
169   StateSet* get_state_set() const { return state_set_; };
170
171   simgrid::kernel::resource::Model* get_model() const { return model_; }
172
173 protected:
174   StateSet* state_set_;
175
176 private:
177   int refcount_            = 1;
178   double sharing_priority_ = 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_time_;        /**< 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   void heapInsert(heap_type& heap, double key, Action::Type hat);
199   void heapRemove(heap_type& heap);
200   void heapUpdate(heap_type& heap, double key, Action::Type hat);
201   void clearHeapHandle() { heap_handle_ = boost::none; }
202   kernel::lmm::Variable* getVariable() const { return variable_; }
203   void setVariable(kernel::lmm::Variable* var) { variable_ = var; }
204   double getLastUpdate() const { return last_update_; }
205   void refreshLastUpdate();
206   double getLastValue() const { return last_value_; }
207   void setLastValue(double val) { last_value_ = val; }
208   Action::Type getType() const { return type_; }
209
210 protected:
211   Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
212 };
213
214 } // namespace resource
215 } // namespace kernel
216 } // namespace simgrid
217 #endif