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>
20 #include "surf/trace_mgr.h"
22 #include "surf/surf_routing.h"
23 #include "simgrid/platf_interface.h"
24 #include "surf/surf.h"
25 #include "src/surf/surf_private.h"
26 #include "src/internal_config.h"
28 extern XBT_PRIVATE tmgr_history_t history;
29 #define NO_MAX_DURATION -1.0
35 /* user-visible parameters */
36 extern XBT_PRIVATE double sg_tcp_gamma;
37 extern XBT_PRIVATE double sg_sender_gap;
38 extern XBT_PRIVATE double sg_latency_factor;
39 extern XBT_PRIVATE double sg_bandwidth_factor;
40 extern XBT_PRIVATE double sg_weight_S_parameter;
41 extern XBT_PRIVATE int sg_network_crosstraffic;
42 extern XBT_PRIVATE xbt_dynar_t surf_path;
45 XBT_PUBLIC(double) surf_get_clock(void);
48 extern XBT_PRIVATE double sg_sender_gap;
53 extern XBT_PRIVATE simgrid::xbt::signal<void(void)> surfExitCallbacks;
58 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
64 enum heap_action_type{
74 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
75 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
76 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
77 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
78 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
79 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
80 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
86 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
88 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
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 * @brief Common initializations for the constructors
108 void initialize(simgrid::surf::Model *model, double cost, bool failed,
109 lmm_variable_t var = NULL);
113 * @brief Action constructor
115 * @param model The Model associated to this Action
116 * @param cost The cost of the Action
117 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
119 Action(simgrid::surf::Model *model, double cost, bool failed);
122 * @brief Action constructor
124 * @param model The Model associated to this Action
125 * @param cost The cost of the Action
126 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
127 * @param var The lmm variable associated to this Action if it is part of a LMM component
129 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
131 /** @brief Destructor */
134 /** @brief Mark that the action is now finished */
137 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
138 e_surf_action_state_t getState(); /**< get the state*/
139 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
140 virtual void setState(e_surf_action_state_t state);
142 /** @brief Get the bound of the current Action */
144 /** @brief Set the bound of the current Action */
145 void setBound(double bound);
147 /** @brief Get the start time of the current action */
148 double getStartTime();
149 /** @brief Get the finish time of the current action */
150 double getFinishTime();
152 /** @brief Get the user data associated to the current action */
153 void *getData() {return p_data;}
154 /** @brief Set the user data associated to the current action */
155 void setData(void* data);
157 /** @brief Get the cost of the current action */
158 double getCost() {return m_cost;}
159 /** @brief Set the cost of the current action */
160 void setCost(double cost) {m_cost = cost;}
162 /** @brief Update the maximum duration of the current action
163 * @param delta Amount to remove from the MaxDuration */
164 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
166 /** @brief Update the remaining time of the current action
167 * @param delta Amount to remove from the remaining time */
168 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
170 /** @brief Set the remaining time of the current action */
171 void setRemains(double value) {m_remains = value;}
172 /** @brief Get the remaining time of the current action after updating the resource */
173 virtual double getRemains();
174 /** @brief Get the remaining time of the current action without updating the resource */
175 double getRemainsNoUpdate();
177 /** @brief Set the finish time of the current action */
178 void setFinishTime(double value) {m_finish = value;}
180 /**@brief Add a reference to the current action (refcounting) */
182 /** @brief Unref that action (and destroy it if refcount reaches 0)
183 * @return true if the action was destroyed and false if someone still has references on it
187 /** @brief Cancel the current Action if running */
188 virtual void cancel();
190 /** @brief Suspend the current Action */
191 virtual void suspend();
193 /** @brief Resume the current Action */
194 virtual void resume();
196 /** @brief Returns true if the current action is running */
197 virtual bool isSuspended();
199 /** @brief Get the maximum duration of the current action */
200 double getMaxDuration() {return m_maxDuration;}
201 /** @brief Set the maximum duration of the current Action */
202 virtual void setMaxDuration(double duration);
204 /** @brief Get the tracing category associated to the current action */
205 char *getCategory() {return p_category;}
206 /** @brief Set the tracing category of the current Action */
207 void setCategory(const char *category);
209 /** @brief Get the priority of the current Action */
210 double getPriority() {return m_priority;};
211 /** @brief Set the priority of the current Action */
212 virtual void setPriority(double priority);
214 /** @brief Get the state set in which the action is */
215 ActionList* getStateSet() {return p_stateSet;};
217 s_xbt_swag_hookup_t p_stateHookup = {NULL,NULL};
219 simgrid::surf::Model *getModel() {return p_model;}
222 ActionList* p_stateSet;
223 double m_priority = 1.0; /**< priority (1.0 by default) */
225 double m_remains; /**< How much of that cost remains to be done in the currently running task */
226 double m_maxDuration = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
227 double m_finish = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
230 double m_start; /**< start time */
231 char *p_category = NULL; /**< tracing category for categorized resource utilization monitoring */
233 #ifdef HAVE_LATENCY_BOUND_TRACKING
234 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
237 simgrid::surf::Model *p_model;
238 void *p_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 p_variable;}
248 double getLastUpdate() {return m_lastUpdate;}
249 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
250 enum heap_action_type getHat() {return m_hat;}
251 bool is_linked() {return action_lmm_hook.is_linked();}
255 lmm_variable_t p_variable;
256 double m_lastValue = 0;
257 double m_lastUpdate = 0;
260 enum heap_action_type m_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 virtual void addTraces() =0;
285 /** @brief Get the set of [actions](@ref Action) in *ready* state */
286 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
288 /** @brief Get the set of [actions](@ref Action) in *running* state */
289 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
291 /** @brief Get the set of [actions](@ref Action) in *failed* state */
292 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
294 /** @brief Get the set of [actions](@ref Action) in *done* state */
295 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
297 /** @brief Get the set of modified [actions](@ref Action) */
298 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
300 /** @brief Get the maxmin system of the current Model */
301 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
304 * @brief Get the update mechanism of the current Model
307 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
309 /** @brief Get Action heap */
310 xbt_heap_t getActionHeap() {return p_actionHeap;}
313 * @brief Share the resources between the actions
315 * @param now The current time of the simulation
316 * @return The delta of time till the next action will finish
318 virtual double shareResources(double now);
319 virtual double shareResourcesLazy(double now);
320 virtual double shareResourcesFull(double now);
321 double shareResourcesMaxMin(ActionList* running_actions,
323 void (*solve) (lmm_system_t));
326 * @brief Update action to the current time
328 * @param now The current time of the simulation
329 * @param delta The delta of time since the last update
331 virtual void updateActionsState(double now, double delta);
332 virtual void updateActionsStateLazy(double now, double delta);
333 virtual void updateActionsStateFull(double now, double delta);
335 /** @brief Returns whether this model have an idempotent shareResource()
337 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
338 * so we need to call it only when the next timestamp of other sources is computed.
340 virtual bool shareResourcesIsIdempotent()=0;
343 ActionLmmListPtr p_modifiedSet;
344 lmm_system_t p_maxminSystem = nullptr;
345 e_UM_t p_updateMechanism = UM_UNDEFINED;
346 int m_selectiveUpdate;
347 xbt_heap_t p_actionHeap;
350 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
351 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
352 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
353 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
363 /** @ingroup SURF_interface
364 * @brief Resource which have a metric handled by a maxmin system
367 double peak; /**< The peak of the metric, ie its max value */
368 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
369 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
375 /** @ingroup SURF_interface
376 * @brief SURF resource interface class
377 * @details A resource represent an element of a component (e.g.: a link for the network)
379 XBT_PUBLIC_CLASS Resource {
382 * @brief Constructor of non-LMM Resources
384 * @param model Model associated to this Resource
385 * @param name The name of the Resource
387 Resource(Model *model, const char *name);
390 * @brief Constructor of LMM Resources
392 * @param model Model associated to this Resource
393 * @param name The name of the Resource
394 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
396 Resource(Model *model, const char *name, lmm_constraint_t constraint);
398 Resource(Model *model, const char *name, lmm_constraint_t constraint, int initiallyOn);
401 * @brief Resource constructor
403 * @param model Model associated to this Resource
404 * @param name The name of the Resource
405 * @param initiallyOn the initial state of the Resource
407 Resource(Model *model, const char *name, int initiallyOn);
411 /** @brief Get the Model of the current Resource */
414 /** @brief Get the name of the current Resource */
415 const char *getName();
418 * @brief Update the state of the current Resource
421 * @param event_type [TODO]
422 * @param value [TODO]
425 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
427 /** @brief Check if the current Resource is used (if it currently serves an action) */
428 virtual bool isUsed()=0;
430 /** @brief Check if the current Resource is active */
432 /** @brief Check if the current Resource is shut down */
433 virtual bool isOff();
434 /** @brief Turn on the current Resource */
435 virtual void turnOn();
436 /** @brief Turn off the current Resource */
437 virtual void turnOff();
445 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
446 lmm_constraint_t getConstraint();
448 lmm_constraint_t p_constraint = nullptr;
454 #endif /* SURF_MODEL_H_ */