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. */
15 #include <boost/function.hpp>
16 #include <boost/intrusive/list.hpp>
18 #include <xbt/signal.hpp>
21 #include "surf/surf_routing.h"
22 #include "simgrid/platf_interface.h"
23 #include "surf/surf.h"
24 #include "src/surf/surf_private.h"
25 #include "src/internal_config.h"
27 #define NO_MAX_DURATION -1.0
33 /* user-visible parameters */
34 extern XBT_PRIVATE double sg_tcp_gamma;
35 extern XBT_PRIVATE double sg_sender_gap;
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 xbt_dynar_t surf_path;
43 XBT_PUBLIC(double) surf_get_clock(void);
46 extern XBT_PRIVATE double sg_sender_gap;
51 extern XBT_PRIVATE simgrid::xbt::signal<void(void)> surfExitCallbacks;
56 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
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 * @brief Common initializations for the constructors
101 void initialize(simgrid::surf::Model *model, double cost, bool failed,
102 lmm_variable_t var = NULL);
106 * @brief Action constructor
108 * @param model The Model associated to this Action
109 * @param cost The cost of the Action
110 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
112 Action(simgrid::surf::Model *model, double cost, bool failed);
115 * @brief Action constructor
117 * @param model The Model associated to this Action
118 * @param cost The cost of the Action
119 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
120 * @param var The lmm variable associated to this Action if it is part of a LMM component
122 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
124 /** @brief Destructor */
127 /** @brief Mark that the action is now finished */
130 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
131 e_surf_action_state_t getState(); /**< get the state*/
132 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
133 virtual void setState(e_surf_action_state_t state);
135 /** @brief Get the bound of the current Action */
137 /** @brief Set the bound of the current Action */
138 void setBound(double bound);
140 /** @brief Get the start time of the current action */
141 double getStartTime();
142 /** @brief Get the finish time of the current action */
143 double getFinishTime();
145 /** @brief Get the user data associated to the current action */
146 void *getData() {return p_data;}
147 /** @brief Set the user data associated to the current action */
148 void setData(void* data);
150 /** @brief Get the cost of the current action */
151 double getCost() {return m_cost;}
152 /** @brief Set the cost of the current action */
153 void setCost(double cost) {m_cost = cost;}
155 /** @brief Update the maximum duration of the current action
156 * @param delta Amount to remove from the MaxDuration */
157 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
159 /** @brief Update the remaining time of the current action
160 * @param delta Amount to remove from the remaining time */
161 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
163 /** @brief Set the remaining time of the current action */
164 void setRemains(double value) {m_remains = value;}
165 /** @brief Get the remaining time of the current action after updating the resource */
166 virtual double getRemains();
167 /** @brief Get the remaining time of the current action without updating the resource */
168 double getRemainsNoUpdate();
170 /** @brief Set the finish time of the current action */
171 void setFinishTime(double value) {m_finish = value;}
173 /**@brief Add a reference to the current action (refcounting) */
175 /** @brief Unref that action (and destroy it if refcount reaches 0)
176 * @return true if the action was destroyed and false if someone still has references on it
180 /** @brief Cancel the current Action if running */
181 virtual void cancel();
183 /** @brief Suspend the current Action */
184 virtual void suspend();
186 /** @brief Resume the current Action */
187 virtual void resume();
189 /** @brief Returns true if the current action is running */
190 virtual bool isSuspended();
192 /** @brief Get the maximum duration of the current action */
193 double getMaxDuration() {return m_maxDuration;}
194 /** @brief Set the maximum duration of the current Action */
195 virtual void setMaxDuration(double duration);
197 /** @brief Get the tracing category associated to the current action */
198 char *getCategory() {return p_category;}
199 /** @brief Set the tracing category of the current Action */
200 void setCategory(const char *category);
202 /** @brief Get the priority of the current Action */
203 double getPriority() {return m_priority;};
204 /** @brief Set the priority of the current Action */
205 virtual void setPriority(double priority);
207 /** @brief Get the state set in which the action is */
208 ActionList* getStateSet() {return p_stateSet;};
210 s_xbt_swag_hookup_t p_stateHookup = {NULL,NULL};
212 simgrid::surf::Model *getModel() {return p_model;}
215 ActionList* p_stateSet;
216 double m_priority = 1.0; /**< priority (1.0 by default) */
218 double m_remains; /**< How much of that cost remains to be done in the currently running task */
219 double m_maxDuration = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
220 double m_finish = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
223 double m_start; /**< start time */
224 char *p_category = NULL; /**< tracing category for categorized resource utilization monitoring */
226 #ifdef HAVE_LATENCY_BOUND_TRACKING
227 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
230 simgrid::surf::Model *p_model;
231 void *p_data = NULL; /**< for your convenience */
235 virtual void updateRemainingLazy(double now);
236 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
237 void heapRemove(xbt_heap_t heap);
238 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
239 void updateIndexHeap(int i);
240 lmm_variable_t getVariable() {return p_variable;}
241 double getLastUpdate() {return m_lastUpdate;}
242 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
243 enum heap_action_type getHat() {return m_hat;}
244 bool is_linked() {return action_lmm_hook.is_linked();}
248 lmm_variable_t p_variable;
249 double m_lastValue = 0;
250 double m_lastUpdate = 0;
253 enum heap_action_type m_hat = NOTSET;
256 typedef Action::ActionList ActionList;
258 typedef boost::intrusive::member_hook<
259 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
260 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
261 typedef ActionLmmList* ActionLmmListPtr;
267 /** @ingroup SURF_interface
268 * @brief SURF model interface class
269 * @details A model is an object which handle the interactions between its Resources and its Actions
271 XBT_PUBLIC_CLASS Model {
276 /** @brief Get the set of [actions](@ref Action) in *ready* state */
277 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
279 /** @brief Get the set of [actions](@ref Action) in *running* state */
280 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
282 /** @brief Get the set of [actions](@ref Action) in *failed* state */
283 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
285 /** @brief Get the set of [actions](@ref Action) in *done* state */
286 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
288 /** @brief Get the set of modified [actions](@ref Action) */
289 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
291 /** @brief Get the maxmin system of the current Model */
292 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
295 * @brief Get the update mechanism of the current Model
298 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
300 /** @brief Get Action heap */
301 xbt_heap_t getActionHeap() {return p_actionHeap;}
304 * @brief Share the resources between the actions
306 * @param now The current time of the simulation
307 * @return The delta of time till the next action will finish
309 virtual double next_occuring_event(double now);
310 virtual double next_occuring_event_lazy(double now);
311 virtual double next_occuring_event_full(double now);
312 double shareResourcesMaxMin(ActionList* running_actions,
313 lmm_system_t sys, void (*solve) (lmm_system_t));
316 * @brief Update action to the current time
318 * @param now The current time of the simulation
319 * @param delta The delta of time since the last update
321 virtual void updateActionsState(double now, double delta);
322 virtual void updateActionsStateLazy(double now, double delta);
323 virtual void updateActionsStateFull(double now, double delta);
325 /** @brief Returns whether this model have an idempotent shareResource()
327 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
328 * so we need to call it only when the next timestamp of other sources is computed.
330 virtual bool next_occuring_event_isIdempotent()=0;
333 ActionLmmListPtr p_modifiedSet;
334 lmm_system_t p_maxminSystem = nullptr;
335 e_UM_t p_updateMechanism = UM_UNDEFINED;
336 int m_selectiveUpdate;
337 xbt_heap_t p_actionHeap;
340 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
341 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
342 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
343 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
353 /** @ingroup SURF_interface
354 * @brief Resource which have a metric handled by a maxmin system
357 double peak; /**< The peak of the metric, ie its max value */
358 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
359 tmgr_trace_iterator_t event; /**< The associated trace event associated to the metric */
365 /** @ingroup SURF_interface
366 * @brief SURF resource interface class
367 * @details A resource represent an element of a component (e.g.: a link for the network)
369 XBT_PUBLIC_CLASS Resource {
372 * @brief Constructor of non-LMM Resources
374 * @param model Model associated to this Resource
375 * @param name The name of the Resource
377 Resource(Model *model, const char *name);
380 * @brief Constructor of LMM Resources
382 * @param model Model associated to this Resource
383 * @param name The name of the Resource
384 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
386 Resource(Model *model, const char *name, lmm_constraint_t constraint);
388 Resource(Model *model, const char *name, lmm_constraint_t constraint, int initiallyOn);
391 * @brief Resource constructor
393 * @param model Model associated to this Resource
394 * @param name The name of the Resource
395 * @param initiallyOn the initial state of the Resource
397 Resource(Model *model, const char *name, int initiallyOn);
401 /** @brief Get the Model of the current Resource */
404 /** @brief Get the name of the current Resource */
405 const char *getName();
408 * @brief Apply an event of external load event to that storage
410 * @param event What happened
411 * @param value [TODO]
413 virtual void apply_event(tmgr_trace_iterator_t event, double value)=0;
415 /** @brief Check if the current Resource is used (if it currently serves an action) */
416 virtual bool isUsed()=0;
418 /** @brief Check if the current Resource is active */
420 /** @brief Check if the current Resource is shut down */
421 virtual bool isOff();
422 /** @brief Turn on the current Resource */
423 virtual void turnOn();
424 /** @brief Turn off the current Resource */
425 virtual void turnOff();
433 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
434 lmm_constraint_t getConstraint();
436 lmm_constraint_t p_constraint = nullptr;
442 #endif /* SURF_MODEL_H_ */