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 class XBT_PUBLIC Action {
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(); }
29 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modifiedSetHook_>
31 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
33 boost::intrusive::list_member_hook<> stateSetHook_;
34 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::stateSetHook_>
36 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
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 ? */
47 enum class SuspendStates {
48 not_suspended = 0, /**< Action currently not suspended **/
53 enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
56 * @brief Action constructor
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)
62 Action(simgrid::kernel::resource::Model * model, double cost, bool failed);
65 * @brief Action constructor
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
72 Action(simgrid::kernel::resource::Model * model, double cost, bool failed, kernel::lmm::Variable* var);
77 * @brief Mark that the action is now finished
79 * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
81 void finish(Action::State state);
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);
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);
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_; }
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; }
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; }
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); }
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); }
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_; }
123 /** @brief Set the finish time of the current action */
124 void setFinishTime(double value) { finishTime_ = value; }
126 /**@brief Add a reference to the current action (refcounting) */
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
133 /** @brief Cancel the current Action if running */
134 virtual void cancel();
136 /** @brief Suspend the current Action */
137 virtual void suspend();
139 /** @brief Resume the current Action */
140 virtual void resume();
142 /** @brief Returns true if the current action is running */
143 virtual bool isSuspended();
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);
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);
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; }
161 /** @brief Get the state set in which the action is */
162 ActionList* getStateSet() const { return stateSet_; };
164 simgrid::kernel::resource::Model* getModel() const { return model_; }
167 ActionList* stateSet_;
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 */
177 -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
180 simgrid::kernel::resource::Model* model_;
181 void* data_ = nullptr; /**< for your convenience */
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;
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_; }
205 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
208 typedef Action::ActionList ActionList;
209 typedef Action::ActionLmmList ActionLmmList;
210 typedef Action::ActionLmmList* ActionLmmListPtr;
212 } // namespace simgrid
213 } // namespace simgrid