1 /* Copyright (c) 2004-2017. 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. */
9 #include "xbt/signal.hpp"
10 #include "xbt/utility.hpp"
12 #include "src/surf/surf_private.hpp"
13 #include "surf/surf.hpp"
16 #include <boost/heap/pairing_heap.hpp>
17 #include <boost/intrusive/list.hpp>
18 #include <boost/optional.hpp>
22 #include <unordered_map>
24 #define NO_MAX_DURATION -1.0
30 /* user-visible parameters */
31 XBT_PUBLIC_DATA(double) sg_maxmin_precision;
32 XBT_PUBLIC_DATA(double) sg_surf_precision;
33 XBT_PUBLIC_DATA(int) sg_concurrency_limit;
35 extern XBT_PRIVATE double sg_tcp_gamma;
36 extern XBT_PRIVATE double sg_latency_factor;
37 extern XBT_PRIVATE double sg_bandwidth_factor;
38 extern XBT_PRIVATE double sg_weight_S_parameter;
39 extern XBT_PRIVATE int sg_network_crosstraffic;
40 extern XBT_PRIVATE std::vector<std::string> surf_path;
41 extern XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
42 extern XBT_PRIVATE std::set<std::string> watched_hosts;
44 static inline void double_update(double* variable, double value, double precision)
46 // printf("Updating %g -= %g +- %g\n",*variable,value,precision);
47 // xbt_assert(value==0 || value>precision);
48 // Check that precision is higher than the machine-dependent size of the mantissa. If not, brutal rounding may
49 // happen, and the precision mechanism is not active...
50 // xbt_assert(*variable< (2<<DBL_MANT_DIG)*precision && FLT_RADIX==2);
52 if (*variable < precision)
56 static inline int double_positive(double value, double precision)
58 return (value > precision);
61 static inline int double_equals(double value1, double value2, double precision)
63 return (fabs(value1 - value2) < precision);
67 XBT_PUBLIC(double) surf_get_clock();
69 /** \ingroup SURF_simulation
70 * \brief List of hosts that have just restarted and whose autorestart process should be restarted.
72 XBT_PUBLIC_DATA(std::vector<sg_host_t>) host_that_restart;
74 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
80 /** \ingroup SURF_models
81 * \brief List of initialized models
83 XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
88 typedef std::pair<double, simgrid::surf::Action*> heap_element_type;
89 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
90 boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>
93 /** @ingroup SURF_interface
94 * @brief SURF action interface class
95 * @details An action is an event generated by a resource (e.g.: a communication for the network)
97 XBT_PUBLIC_CLASS Action {
99 boost::intrusive::list_member_hook<> action_hook;
100 boost::intrusive::list_member_hook<> action_lmm_hook;
101 typedef boost::intrusive::member_hook<
102 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
103 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
106 ready = 0, /**< Ready */
107 running, /**< Running */
108 failed, /**< Task Failure */
109 done, /**< Completed */
110 to_free, /**< Action to free in next cleanup */
111 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
114 enum class SuspendStates {
115 not_suspended = 0, /**< Action currently not suspended **/
120 enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
123 * @brief Action constructor
125 * @param model The Model associated to this Action
126 * @param cost The cost of the Action
127 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
129 Action(simgrid::surf::Model* model, double cost, bool failed);
132 * @brief Action constructor
134 * @param model The Model associated to this Action
135 * @param cost The cost of the Action
136 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
137 * @param var The lmm variable associated to this Action if it is part of a LMM component
139 Action(simgrid::surf::Model * model, double cost, bool failed, kernel::lmm::Variable* var);
141 /** @brief Destructor */
145 * @brief Mark that the action is now finished
147 * @param state the new [state](\ref simgrid::surf::Action::State) of the current Action
149 void finish(Action::State state);
151 /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
152 Action::State getState() const; /**< get the state*/
153 /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
154 virtual void setState(Action::State state);
156 /** @brief Get the bound of the current Action */
157 double getBound() const;
158 /** @brief Set the bound of the current Action */
159 void setBound(double bound);
161 /** @brief Get the start time of the current action */
162 double getStartTime() const { return start_; }
163 /** @brief Get the finish time of the current action */
164 double getFinishTime() const { return finishTime_; }
166 /** @brief Get the user data associated to the current action */
167 void* getData() const { return data_; }
168 /** @brief Set the user data associated to the current action */
169 void setData(void* data) { data_ = data; }
171 /** @brief Get the cost of the current action */
172 double getCost() const { return cost_; }
173 /** @brief Set the cost of the current action */
174 void setCost(double cost) {cost_ = cost;}
176 /** @brief Update the maximum duration of the current action
177 * @param delta Amount to remove from the MaxDuration */
178 void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
180 /** @brief Update the remaining time of the current action
181 * @param delta Amount to remove from the remaining time */
182 void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
184 /** @brief Set the remaining time of the current action */
185 void setRemains(double value) {remains_ = value;}
186 /** @brief Get the remaining time of the current action after updating the resource */
187 virtual double getRemains();
188 /** @brief Get the remaining time of the current action without updating the resource */
189 double getRemainsNoUpdate() const { return remains_; }
191 /** @brief Set the finish time of the current action */
192 void setFinishTime(double value) {finishTime_ = value;}
194 /**@brief Add a reference to the current action (refcounting) */
196 /** @brief Unref that action (and destroy it if refcount reaches 0)
197 * @return true if the action was destroyed and false if someone still has references on it
201 /** @brief Cancel the current Action if running */
202 virtual void cancel();
204 /** @brief Suspend the current Action */
205 virtual void suspend();
207 /** @brief Resume the current Action */
208 virtual void resume();
210 /** @brief Returns true if the current action is running */
211 virtual bool isSuspended();
213 /** @brief Get the maximum duration of the current action */
214 double getMaxDuration() const { return maxDuration_; }
215 /** @brief Set the maximum duration of the current Action */
216 virtual void setMaxDuration(double duration);
218 /** @brief Get the tracing category associated to the current action */
219 char* getCategory() const { return category_; }
220 /** @brief Set the tracing category of the current Action */
221 void setCategory(const char *category);
223 /** @brief Get the priority of the current Action */
224 double getPriority() const { return sharingWeight_; };
225 /** @brief Set the priority of the current Action */
226 virtual void setSharingWeight(double priority);
227 void setSharingWeightNoUpdate(double weight) { sharingWeight_ = weight; }
229 /** @brief Get the state set in which the action is */
230 ActionList* getStateSet() const { return stateSet_; };
232 simgrid::surf::Model* getModel() const { return model_; }
235 ActionList* stateSet_;
239 double sharingWeight_ = 1.0; /**< priority (1.0 by default) */
240 double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
241 double remains_; /**< How much of that cost remains to be done in the currently running task */
242 double start_; /**< start time */
243 char *category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
245 -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
248 simgrid::surf::Model *model_;
249 void *data_ = nullptr; /**< for your convenience */
252 double lastUpdate_ = 0;
253 double lastValue_ = 0;
254 kernel::lmm::Variable* variable_ = nullptr;
255 Action::Type hat_ = Action::Type::NOTSET;
256 boost::optional<heap_type::handle_type> heapHandle_ = boost::none;
259 virtual void updateRemainingLazy(double now) { THROW_IMPOSSIBLE; };
260 void heapInsert(heap_type & heap, double key, Action::Type hat);
261 void heapRemove(heap_type& heap);
262 void heapUpdate(heap_type & heap, double key, Action::Type hat);
263 void clearHeapHandle() { heapHandle_ = boost::none; }
264 kernel::lmm::Variable* getVariable() const { return variable_; }
265 void setVariable(kernel::lmm::Variable * var) { variable_ = var; }
266 double getLastUpdate() const { return lastUpdate_; }
267 void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
268 double getLastValue() const { return lastValue_; }
269 void setLastValue(double val) { lastValue_ = val; }
270 Action::Type getHat() const { return hat_; }
271 bool is_linked() const { return action_lmm_hook.is_linked(); }
273 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
276 typedef Action::ActionList ActionList;
278 typedef boost::intrusive::member_hook<
279 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
280 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
281 typedef ActionLmmList* ActionLmmListPtr;
287 /** @ingroup SURF_interface
288 * @brief SURF model interface class
289 * @details A model is an object which handle the interactions between its Resources and its Actions
291 XBT_PUBLIC_CLASS Model {
296 /** @brief Get the set of [actions](@ref Action) in *ready* state */
297 virtual ActionList* getReadyActionSet() const { return readyActionSet_; }
299 /** @brief Get the set of [actions](@ref Action) in *running* state */
300 virtual ActionList* getRunningActionSet() const { return runningActionSet_; }
302 /** @brief Get the set of [actions](@ref Action) in *failed* state */
303 virtual ActionList* getFailedActionSet() const { return failedActionSet_; }
305 /** @brief Get the set of [actions](@ref Action) in *done* state */
306 virtual ActionList* getDoneActionSet() const { return doneActionSet_; }
308 /** @brief Get the set of modified [actions](@ref Action) */
309 virtual ActionLmmListPtr getModifiedSet() const { return modifiedSet_; }
311 /** @brief Get the maxmin system of the current Model */
312 lmm_system_t getMaxminSystem() const { return maxminSystem_; }
315 * @brief Get the update mechanism of the current Model
318 e_UM_t getUpdateMechanism() const { return updateMechanism_; }
319 void setUpdateMechanism(e_UM_t mechanism) { updateMechanism_ = mechanism; }
321 /** @brief Get Action heap */
322 heap_type& getActionHeap() { return actionHeap_; }
324 double actionHeapTopDate() const { return actionHeap_.top().first; }
325 Action* actionHeapPop();
326 bool actionHeapIsEmpty() const { return actionHeap_.empty(); }
329 * @brief Share the resources between the actions
331 * @param now The current time of the simulation
332 * @return The delta of time till the next action will finish
334 virtual double nextOccuringEvent(double now);
335 virtual double nextOccuringEventLazy(double now);
336 virtual double nextOccuringEventFull(double now);
339 * @brief Update action to the current time
341 * @param now The current time of the simulation
342 * @param delta The delta of time since the last update
344 virtual void updateActionsState(double now, double delta);
345 virtual void updateActionsStateLazy(double now, double delta);
346 virtual void updateActionsStateFull(double now, double delta);
348 /** @brief Returns whether this model have an idempotent shareResource()
350 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
351 * so we need to call it only when the next timestamp of other sources is computed.
353 virtual bool nextOccuringEventIsIdempotent() { return true;}
356 ActionLmmListPtr modifiedSet_;
357 lmm_system_t maxminSystem_ = nullptr;
358 bool selectiveUpdate_;
361 e_UM_t updateMechanism_ = UM_UNDEFINED;
362 ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
363 ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
364 ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
365 ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
366 heap_type actionHeap_;
377 #endif /* SURF_MODEL_H_ */