1 /* Copyright (c) 2004-2016. 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. */
16 #include <boost/intrusive/list.hpp>
18 #include <xbt/signal.hpp>
21 #include "surf/surf_routing.h"
22 #include "surf/surf.h"
23 #include "src/surf/surf_private.h"
24 #include "src/internal_config.h"
26 #define NO_MAX_DURATION -1.0
32 /* user-visible parameters */
33 extern XBT_PRIVATE double sg_tcp_gamma;
34 extern XBT_PRIVATE double sg_sender_gap;
35 extern XBT_PRIVATE double sg_latency_factor;
36 extern XBT_PRIVATE double sg_bandwidth_factor;
37 extern XBT_PRIVATE double sg_weight_S_parameter;
38 extern XBT_PRIVATE int sg_network_crosstraffic;
39 extern XBT_PRIVATE xbt_dynar_t surf_path;
42 XBT_PUBLIC(double) surf_get_clock(void);
44 /** \ingroup SURF_simulation
45 * \brief List of hosts that have just restarted and whose autorestart process should be restarted.
47 XBT_PUBLIC_DATA(std::vector<sg_host_t>) host_that_restart;
50 extern XBT_PRIVATE double sg_sender_gap;
55 extern XBT_PRIVATE simgrid::xbt::signal<void(void)> surfExitCallbacks;
60 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
62 static inline char* sg_storage_name(sg_storage_t storage) {
70 enum heap_action_type{
80 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
81 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
87 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
89 /** \ingroup SURF_models
90 * \brief List of initialized models
92 XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
97 /** @ingroup SURF_interface
98 * @brief SURF action interface class
99 * @details An action is an event generated by a resource (e.g.: a communication for the network)
101 XBT_PUBLIC_CLASS Action {
103 boost::intrusive::list_member_hook<> action_hook;
104 boost::intrusive::list_member_hook<> action_lmm_hook;
105 typedef boost::intrusive::member_hook<
106 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
107 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
110 ready = 0, /**< Ready */
111 running, /**< Running */
112 failed, /**< Task Failure */
113 done, /**< Completed */
114 to_free, /**< Action to free in next cleanup */
115 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
120 * @brief Action constructor
122 * @param model The Model associated to this Action
123 * @param cost The cost of the Action
124 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
126 Action(simgrid::surf::Model *model, double cost, bool failed);
129 * @brief Action constructor
131 * @param model The Model associated to this Action
132 * @param cost The cost of the Action
133 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
134 * @param var The lmm variable associated to this Action if it is part of a LMM component
136 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
138 /** @brief Destructor */
141 /** @brief Mark that the action is now finished */
144 /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
145 Action::State getState(); /**< get the state*/
146 /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
147 virtual void setState(Action::State state);
149 /** @brief Get the bound of the current Action */
151 /** @brief Set the bound of the current Action */
152 void setBound(double bound);
154 /** @brief Get the start time of the current action */
155 double getStartTime();
156 /** @brief Get the finish time of the current action */
157 double getFinishTime();
159 /** @brief Get the user data associated to the current action */
160 void *getData() {return data_;}
161 /** @brief Set the user data associated to the current action */
162 void setData(void* data);
164 /** @brief Get the cost of the current action */
165 double getCost() {return cost_;}
166 /** @brief Set the cost of the current action */
167 void setCost(double cost) {cost_ = cost;}
169 /** @brief Update the maximum duration of the current action
170 * @param delta Amount to remove from the MaxDuration */
171 void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
173 /** @brief Update the remaining time of the current action
174 * @param delta Amount to remove from the remaining time */
175 void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
177 /** @brief Set the remaining time of the current action */
178 void setRemains(double value) {remains_ = value;}
179 /** @brief Get the remaining time of the current action after updating the resource */
180 virtual double getRemains();
181 /** @brief Get the remaining time of the current action without updating the resource */
182 double getRemainsNoUpdate();
184 /** @brief Set the finish time of the current action */
185 void setFinishTime(double value) {finishTime_ = value;}
187 /**@brief Add a reference to the current action (refcounting) */
189 /** @brief Unref that action (and destroy it if refcount reaches 0)
190 * @return true if the action was destroyed and false if someone still has references on it
194 /** @brief Cancel the current Action if running */
195 virtual void cancel();
197 /** @brief Suspend the current Action */
198 virtual void suspend();
200 /** @brief Resume the current Action */
201 virtual void resume();
203 /** @brief Returns true if the current action is running */
204 virtual bool isSuspended();
206 /** @brief Get the maximum duration of the current action */
207 double getMaxDuration() {return maxDuration_;}
208 /** @brief Set the maximum duration of the current Action */
209 virtual void setMaxDuration(double duration);
211 /** @brief Get the tracing category associated to the current action */
212 char *getCategory() {return category_;}
213 /** @brief Set the tracing category of the current Action */
214 void setCategory(const char *category);
216 /** @brief Get the priority of the current Action */
217 double getPriority() {return priority_;};
218 /** @brief Set the priority of the current Action */
219 virtual void setPriority(double priority);
221 /** @brief Get the state set in which the action is */
222 ActionList* getStateSet() {return stateSet_;};
224 s_xbt_swag_hookup_t stateHookup_ = {nullptr,nullptr};
226 simgrid::surf::Model *getModel() {return model_;}
229 ActionList* stateSet_;
230 double priority_ = 1.0; /**< priority (1.0 by default) */
232 double remains_; /**< How much of that cost remains to be done in the currently running task */
233 double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
234 double finishTime_ = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
237 double start_; /**< start time */
238 char *category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
241 simgrid::surf::Model *model_;
242 void *data_ = nullptr; /**< for your convenience */
246 virtual void updateRemainingLazy(double now);
247 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
248 void heapRemove(xbt_heap_t heap);
249 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
250 void updateIndexHeap(int i);
251 lmm_variable_t getVariable() {return variable_;}
252 double getLastUpdate() {return lastUpdate_;}
253 void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
254 enum heap_action_type getHat() {return hat_;}
255 bool is_linked() {return action_lmm_hook.is_linked();}
259 lmm_variable_t variable_ = nullptr;
260 double lastValue_ = 0;
261 double lastUpdate_ = 0;
264 enum heap_action_type hat_ = NOTSET;
267 typedef Action::ActionList ActionList;
269 typedef boost::intrusive::member_hook<
270 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
271 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
272 typedef ActionLmmList* ActionLmmListPtr;
278 /** @ingroup SURF_interface
279 * @brief SURF model interface class
280 * @details A model is an object which handle the interactions between its Resources and its Actions
282 XBT_PUBLIC_CLASS Model {
287 /** @brief Get the set of [actions](@ref Action) in *ready* state */
288 virtual ActionList* getReadyActionSet() {return readyActionSet_;}
290 /** @brief Get the set of [actions](@ref Action) in *running* state */
291 virtual ActionList* getRunningActionSet() {return runningActionSet_;}
293 /** @brief Get the set of [actions](@ref Action) in *failed* state */
294 virtual ActionList* getFailedActionSet() {return failedActionSet_;}
296 /** @brief Get the set of [actions](@ref Action) in *done* state */
297 virtual ActionList* getDoneActionSet() {return doneActionSet_;}
299 /** @brief Get the set of modified [actions](@ref Action) */
300 virtual ActionLmmListPtr getModifiedSet() {return modifiedSet_;}
302 /** @brief Get the maxmin system of the current Model */
303 lmm_system_t getMaxminSystem() {return maxminSystem_;}
306 * @brief Get the update mechanism of the current Model
309 e_UM_t getUpdateMechanism() {return updateMechanism_;}
311 /** @brief Get Action heap */
312 xbt_heap_t getActionHeap() {return actionHeap_;}
315 * @brief Share the resources between the actions
317 * @param now The current time of the simulation
318 * @return The delta of time till the next action will finish
320 virtual double nextOccuringEvent(double now);
321 virtual double nextOccuringEventLazy(double now);
322 virtual double nextOccuringEventFull(double now);
325 * @brief Update action to the current time
327 * @param now The current time of the simulation
328 * @param delta The delta of time since the last update
330 virtual void updateActionsState(double now, double delta);
331 virtual void updateActionsStateLazy(double now, double delta);
332 virtual void updateActionsStateFull(double now, double delta);
334 /** @brief Returns whether this model have an idempotent shareResource()
336 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
337 * so we need to call it only when the next timestamp of other sources is computed.
339 virtual bool nextOccuringEventIsIdempotent() { return true;}
342 ActionLmmListPtr modifiedSet_;
343 lmm_system_t maxminSystem_ = nullptr;
344 e_UM_t updateMechanism_ = UM_UNDEFINED;
345 bool selectiveUpdate_;
346 xbt_heap_t actionHeap_;
349 ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
350 ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
351 ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
352 ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
362 /** @ingroup SURF_interface
363 * @brief Resource which have a metric handled by a maxmin system
366 double peak; /**< The peak of the metric, ie its max value */
367 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
368 tmgr_trace_iterator_t event; /**< The associated trace event associated to the metric */
374 /** @ingroup SURF_interface
375 * @brief SURF resource interface class
376 * @details This is the ancestor class of every resources in SimGrid, such as links, CPU or storage
378 XBT_PUBLIC_CLASS Resource {
381 * @brief Constructor of LMM Resources
383 * @param model Model associated to this Resource
384 * @param name The name of the Resource
385 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
387 Resource(Model *model, const char *name, lmm_constraint_t constraint);
391 /** @brief Get the Model of the current Resource */
392 Model *getModel() const;
394 /** @brief Get the name of the current Resource */
395 const char *getName() const;
397 bool operator==(const Resource &other) const;
400 * @brief Apply an event of external load event to that resource
402 * @param event What happened
403 * @param value [TODO]
405 virtual void apply_event(tmgr_trace_iterator_t event, double value)=0;
407 /** @brief Check if the current Resource is used (if it currently serves an action) */
408 virtual bool isUsed()=0;
410 /** @brief Check if the current Resource is active */
411 virtual bool isOn() const;
412 /** @brief Check if the current Resource is shut down */
413 virtual bool isOff() const;
414 /** @brief Turn on the current Resource */
415 virtual void turnOn();
416 /** @brief Turn off the current Resource */
417 virtual void turnOff();
425 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component (or null if none) */
426 lmm_constraint_t getConstraint() const;
428 const lmm_constraint_t constraint_ = nullptr;
436 struct hash<simgrid::surf::Resource>
438 std::size_t operator()(const simgrid::surf::Resource& r) const
440 return (std::size_t) xbt_str_hash(r.getName());
445 #endif /* SURF_MODEL_H_ */