1 /* Copyright (c) 2004-2018. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_KERNEL_RESOURCE_ACTION_HPP
7 #define SIMGRID_KERNEL_RESOURCE_ACTION_HPP
9 #include "src/surf/surf_interface.hpp"
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>>>
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)
24 XBT_PUBLIC_CLASS Action
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(); }
30 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modifiedSetHook_>
32 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
34 boost::intrusive::list_member_hook<> stateSetHook_;
35 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::stateSetHook_>
37 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
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 ? */
48 enum class SuspendStates {
49 not_suspended = 0, /**< Action currently not suspended **/
54 enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
57 * @brief Action constructor
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)
63 Action(simgrid::kernel::resource::Model * model, double cost, bool failed);
66 * @brief Action constructor
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
73 Action(simgrid::kernel::resource::Model * model, double cost, bool failed, kernel::lmm::Variable* var);
78 * @brief Mark that the action is now finished
80 * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
82 void finish(Action::State state);
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);
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);
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_; }
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; }
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; }
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); }
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); }
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_; }
124 /** @brief Set the finish time of the current action */
125 void setFinishTime(double value) { finishTime_ = value; }
127 /**@brief Add a reference to the current action (refcounting) */
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
134 /** @brief Cancel the current Action if running */
135 virtual void cancel();
137 /** @brief Suspend the current Action */
138 virtual void suspend();
140 /** @brief Resume the current Action */
141 virtual void resume();
143 /** @brief Returns true if the current action is running */
144 virtual bool isSuspended();
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);
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);
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; }
162 /** @brief Get the state set in which the action is */
163 ActionList* getStateSet() const { return stateSet_; };
165 simgrid::kernel::resource::Model* getModel() const { return model_; }
168 ActionList* stateSet_;
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 */
178 -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
181 simgrid::kernel::resource::Model* model_;
182 void* data_ = nullptr; /**< for your convenience */
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;
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_; }
206 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
209 typedef Action::ActionList ActionList;
210 typedef Action::ActionLmmList ActionLmmList;
211 typedef Action::ActionLmmList* ActionLmmListPtr;
213 } // namespace simgrid
214 } // namespace simgrid