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. */
13 #include <boost/intrusive/list.hpp>
15 #include <xbt/signal.hpp>
18 #include "surf/surf_routing.h"
19 #include "surf/surf.h"
20 #include "src/surf/surf_private.h"
21 #include "src/internal_config.h"
23 #define NO_MAX_DURATION -1.0
29 /* user-visible parameters */
30 extern XBT_PRIVATE double sg_tcp_gamma;
31 extern XBT_PRIVATE double sg_sender_gap;
32 extern XBT_PRIVATE double sg_latency_factor;
33 extern XBT_PRIVATE double sg_bandwidth_factor;
34 extern XBT_PRIVATE double sg_weight_S_parameter;
35 extern XBT_PRIVATE int sg_network_crosstraffic;
36 extern XBT_PRIVATE xbt_dynar_t surf_path;
39 XBT_PUBLIC(double) surf_get_clock(void);
42 extern XBT_PRIVATE double sg_sender_gap;
47 extern XBT_PRIVATE simgrid::xbt::signal<void(void)> surfExitCallbacks;
52 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
54 static inline char* sg_storage_name(sg_storage_t storage) {
62 enum heap_action_type{
72 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
73 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
79 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
81 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
86 /** @ingroup SURF_interface
87 * @brief SURF action interface class
88 * @details An action is an event generated by a resource (e.g.: a communication for the network)
90 XBT_PUBLIC_CLASS Action {
92 boost::intrusive::list_member_hook<> action_hook;
93 boost::intrusive::list_member_hook<> action_lmm_hook;
94 typedef boost::intrusive::member_hook<
95 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
96 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
99 ready = 0, /**< Ready */
100 running, /**< Running */
101 failed, /**< Task Failure */
102 done, /**< Completed */
103 to_free, /**< Action to free in next cleanup */
104 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
109 * @brief Common initializations for the constructors
111 void initialize(simgrid::surf::Model *model, double cost, bool failed,
112 lmm_variable_t var = NULL);
116 * @brief Action constructor
118 * @param model The Model associated to this Action
119 * @param cost The cost of the Action
120 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
122 Action(simgrid::surf::Model *model, double cost, bool failed);
125 * @brief Action constructor
127 * @param model The Model associated to this Action
128 * @param cost The cost of the Action
129 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
130 * @param var The lmm variable associated to this Action if it is part of a LMM component
132 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
134 /** @brief Destructor */
137 /** @brief Mark that the action is now finished */
140 /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
141 Action::State getState(); /**< get the state*/
142 /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
143 virtual void setState(Action::State state);
145 /** @brief Get the bound of the current Action */
147 /** @brief Set the bound of the current Action */
148 void setBound(double bound);
150 /** @brief Get the start time of the current action */
151 double getStartTime();
152 /** @brief Get the finish time of the current action */
153 double getFinishTime();
155 /** @brief Get the user data associated to the current action */
156 void *getData() {return data_;}
157 /** @brief Set the user data associated to the current action */
158 void setData(void* data);
160 /** @brief Get the cost of the current action */
161 double getCost() {return cost_;}
162 /** @brief Set the cost of the current action */
163 void setCost(double cost) {cost_ = cost;}
165 /** @brief Update the maximum duration of the current action
166 * @param delta Amount to remove from the MaxDuration */
167 void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
169 /** @brief Update the remaining time of the current action
170 * @param delta Amount to remove from the remaining time */
171 void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
173 /** @brief Set the remaining time of the current action */
174 void setRemains(double value) {remains_ = value;}
175 /** @brief Get the remaining time of the current action after updating the resource */
176 virtual double getRemains();
177 /** @brief Get the remaining time of the current action without updating the resource */
178 double getRemainsNoUpdate();
180 /** @brief Set the finish time of the current action */
181 void setFinishTime(double value) {finishTime_ = value;}
183 /**@brief Add a reference to the current action (refcounting) */
185 /** @brief Unref that action (and destroy it if refcount reaches 0)
186 * @return true if the action was destroyed and false if someone still has references on it
190 /** @brief Cancel the current Action if running */
191 virtual void cancel();
193 /** @brief Suspend the current Action */
194 virtual void suspend();
196 /** @brief Resume the current Action */
197 virtual void resume();
199 /** @brief Returns true if the current action is running */
200 virtual bool isSuspended();
202 /** @brief Get the maximum duration of the current action */
203 double getMaxDuration() {return maxDuration_;}
204 /** @brief Set the maximum duration of the current Action */
205 virtual void setMaxDuration(double duration);
207 /** @brief Get the tracing category associated to the current action */
208 char *getCategory() {return category_;}
209 /** @brief Set the tracing category of the current Action */
210 void setCategory(const char *category);
212 /** @brief Get the priority of the current Action */
213 double getPriority() {return priority_;};
214 /** @brief Set the priority of the current Action */
215 virtual void setPriority(double priority);
217 /** @brief Get the state set in which the action is */
218 ActionList* getStateSet() {return stateSet_;};
220 s_xbt_swag_hookup_t stateHookup_ = {NULL,NULL};
222 simgrid::surf::Model *getModel() {return model_;}
225 ActionList* stateSet_;
226 double priority_ = 1.0; /**< priority (1.0 by default) */
228 double remains_; /**< How much of that cost remains to be done in the currently running task */
229 double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
230 double finishTime_ = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
233 double start_; /**< start time */
234 char *category_ = NULL; /**< tracing category for categorized resource utilization monitoring */
237 simgrid::surf::Model *model_;
238 void *data_ = NULL; /**< for your convenience */
242 virtual void updateRemainingLazy(double now);
243 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
244 void heapRemove(xbt_heap_t heap);
245 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
246 void updateIndexHeap(int i);
247 lmm_variable_t getVariable() {return variable_;}
248 double getLastUpdate() {return lastUpdate_;}
249 void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
250 enum heap_action_type getHat() {return hat_;}
251 bool is_linked() {return action_lmm_hook.is_linked();}
255 lmm_variable_t variable_ = nullptr;
256 double lastValue_ = 0;
257 double lastUpdate_ = 0;
260 enum heap_action_type hat_ = NOTSET;
263 typedef Action::ActionList ActionList;
265 typedef boost::intrusive::member_hook<
266 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
267 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
268 typedef ActionLmmList* ActionLmmListPtr;
274 /** @ingroup SURF_interface
275 * @brief SURF model interface class
276 * @details A model is an object which handle the interactions between its Resources and its Actions
278 XBT_PUBLIC_CLASS Model {
283 /** @brief Get the set of [actions](@ref Action) in *ready* state */
284 virtual ActionList* getReadyActionSet() {return readyActionSet_;}
286 /** @brief Get the set of [actions](@ref Action) in *running* state */
287 virtual ActionList* getRunningActionSet() {return runningActionSet_;}
289 /** @brief Get the set of [actions](@ref Action) in *failed* state */
290 virtual ActionList* getFailedActionSet() {return failedActionSet_;}
292 /** @brief Get the set of [actions](@ref Action) in *done* state */
293 virtual ActionList* getDoneActionSet() {return doneActionSet_;}
295 /** @brief Get the set of modified [actions](@ref Action) */
296 virtual ActionLmmListPtr getModifiedSet() {return modifiedSet_;}
298 /** @brief Get the maxmin system of the current Model */
299 lmm_system_t getMaxminSystem() {return maxminSystem_;}
302 * @brief Get the update mechanism of the current Model
305 e_UM_t getUpdateMechanism() {return updateMechanism_;}
307 /** @brief Get Action heap */
308 xbt_heap_t getActionHeap() {return actionHeap_;}
311 * @brief Share the resources between the actions
313 * @param now The current time of the simulation
314 * @return The delta of time till the next action will finish
316 virtual double next_occuring_event(double now);
317 virtual double next_occuring_event_lazy(double now);
318 virtual double next_occuring_event_full(double now);
319 double shareResourcesMaxMin(ActionList* running_actions,
320 lmm_system_t sys, void (*solve) (lmm_system_t));
323 * @brief Update action to the current time
325 * @param now The current time of the simulation
326 * @param delta The delta of time since the last update
328 virtual void updateActionsState(double now, double delta);
329 virtual void updateActionsStateLazy(double now, double delta);
330 virtual void updateActionsStateFull(double now, double delta);
332 /** @brief Returns whether this model have an idempotent shareResource()
334 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
335 * so we need to call it only when the next timestamp of other sources is computed.
337 virtual bool next_occuring_event_isIdempotent()=0;
340 ActionLmmListPtr modifiedSet_;
341 lmm_system_t maxminSystem_ = nullptr;
342 e_UM_t updateMechanism_ = UM_UNDEFINED;
343 int selectiveUpdate_;
344 xbt_heap_t actionHeap_;
347 ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
348 ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
349 ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
350 ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
360 /** @ingroup SURF_interface
361 * @brief Resource which have a metric handled by a maxmin system
364 double peak; /**< The peak of the metric, ie its max value */
365 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
366 tmgr_trace_iterator_t event; /**< The associated trace event associated to the metric */
372 /** @ingroup SURF_interface
373 * @brief SURF resource interface class
374 * @details This is the ancestor class of every resources in SimGrid, such as links, CPU or storage
376 XBT_PUBLIC_CLASS Resource {
379 * @brief Constructor of non-LMM Resources
381 * @param model Model associated to this Resource
382 * @param name The name of the Resource
384 Resource(Model *model, const char *name);
387 * @brief Constructor of LMM Resources
389 * @param model Model associated to this Resource
390 * @param name The name of the Resource
391 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
393 Resource(Model *model, const char *name, lmm_constraint_t constraint);
397 /** @brief Get the Model of the current Resource */
398 Model *getModel() const;
400 /** @brief Get the name of the current Resource */
401 const char *getName() const;
403 bool operator==(const Resource &other) const;
406 * @brief Apply an event of external load event to that resource
408 * @param event What happened
409 * @param value [TODO]
411 virtual void apply_event(tmgr_trace_iterator_t event, double value)=0;
413 /** @brief Check if the current Resource is used (if it currently serves an action) */
414 virtual bool isUsed()=0;
416 /** @brief Check if the current Resource is active */
417 virtual bool isOn() const;
418 /** @brief Check if the current Resource is shut down */
419 virtual bool isOff() const;
420 /** @brief Turn on the current Resource */
421 virtual void turnOn();
422 /** @brief Turn off the current Resource */
423 virtual void turnOff();
431 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component (or null if none) */
432 lmm_constraint_t getConstraint() const;
434 lmm_constraint_t constraint_ = nullptr;
442 struct hash<simgrid::surf::Resource>
444 std::size_t operator()(const simgrid::surf::Resource& r) const
446 return (std::size_t) xbt_str_hash(r.getName());
451 #endif /* SURF_MODEL_H_ */