Logo AND Algorithmique Numérique Distribuée

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