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/surf/trace_mgr.hpp"
26 #include "src/internal_config.h"
28 extern XBT_PRIVATE sg_future_evt_set_t future_evt_set;
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_speed;
77 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
78 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_bw;
79 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_lat;
85 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
87 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
92 /** @ingroup SURF_interface
93 * @brief SURF action interface class
94 * @details An action is an event generated by a resource (e.g.: a communication for the network)
96 XBT_PUBLIC_CLASS Action {
98 boost::intrusive::list_member_hook<> action_hook;
99 boost::intrusive::list_member_hook<> action_lmm_hook;
100 typedef boost::intrusive::member_hook<
101 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
102 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
105 * @brief Common initializations for the constructors
107 void initialize(simgrid::surf::Model *model, double cost, bool failed,
108 lmm_variable_t var = NULL);
112 * @brief Action constructor
114 * @param model The Model associated to this Action
115 * @param cost The cost of the Action
116 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
118 Action(simgrid::surf::Model *model, double cost, bool failed);
121 * @brief Action constructor
123 * @param model The Model associated to this Action
124 * @param cost The cost of the Action
125 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
126 * @param var The lmm variable associated to this Action if it is part of a LMM component
128 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
130 /** @brief Destructor */
133 /** @brief Mark that the action is now finished */
136 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
137 e_surf_action_state_t getState(); /**< get the state*/
138 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
139 virtual void setState(e_surf_action_state_t state);
141 /** @brief Get the bound of the current Action */
143 /** @brief Set the bound of the current Action */
144 void setBound(double bound);
146 /** @brief Get the start time of the current action */
147 double getStartTime();
148 /** @brief Get the finish time of the current action */
149 double getFinishTime();
151 /** @brief Get the user data associated to the current action */
152 void *getData() {return p_data;}
153 /** @brief Set the user data associated to the current action */
154 void setData(void* data);
156 /** @brief Get the cost of the current action */
157 double getCost() {return m_cost;}
158 /** @brief Set the cost of the current action */
159 void setCost(double cost) {m_cost = cost;}
161 /** @brief Update the maximum duration of the current action
162 * @param delta Amount to remove from the MaxDuration */
163 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
165 /** @brief Update the remaining time of the current action
166 * @param delta Amount to remove from the remaining time */
167 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
169 /** @brief Set the remaining time of the current action */
170 void setRemains(double value) {m_remains = value;}
171 /** @brief Get the remaining time of the current action after updating the resource */
172 virtual double getRemains();
173 /** @brief Get the remaining time of the current action without updating the resource */
174 double getRemainsNoUpdate();
176 /** @brief Set the finish time of the current action */
177 void setFinishTime(double value) {m_finish = value;}
179 /**@brief Add a reference to the current action (refcounting) */
181 /** @brief Unref that action (and destroy it if refcount reaches 0)
182 * @return true if the action was destroyed and false if someone still has references on it
186 /** @brief Cancel the current Action if running */
187 virtual void cancel();
189 /** @brief Suspend the current Action */
190 virtual void suspend();
192 /** @brief Resume the current Action */
193 virtual void resume();
195 /** @brief Returns true if the current action is running */
196 virtual bool isSuspended();
198 /** @brief Get the maximum duration of the current action */
199 double getMaxDuration() {return m_maxDuration;}
200 /** @brief Set the maximum duration of the current Action */
201 virtual void setMaxDuration(double duration);
203 /** @brief Get the tracing category associated to the current action */
204 char *getCategory() {return p_category;}
205 /** @brief Set the tracing category of the current Action */
206 void setCategory(const char *category);
208 /** @brief Get the priority of the current Action */
209 double getPriority() {return m_priority;};
210 /** @brief Set the priority of the current Action */
211 virtual void setPriority(double priority);
213 /** @brief Get the state set in which the action is */
214 ActionList* getStateSet() {return p_stateSet;};
216 s_xbt_swag_hookup_t p_stateHookup = {NULL,NULL};
218 simgrid::surf::Model *getModel() {return p_model;}
221 ActionList* p_stateSet;
222 double m_priority = 1.0; /**< priority (1.0 by default) */
224 double m_remains; /**< How much of that cost remains to be done in the currently running task */
225 double m_maxDuration = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
226 double m_finish = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
229 double m_start; /**< start time */
230 char *p_category = NULL; /**< tracing category for categorized resource utilization monitoring */
232 #ifdef HAVE_LATENCY_BOUND_TRACKING
233 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
236 simgrid::surf::Model *p_model;
237 void *p_data = NULL; /**< for your convenience */
241 virtual void updateRemainingLazy(double now);
242 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
243 void heapRemove(xbt_heap_t heap);
244 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
245 void updateIndexHeap(int i);
246 lmm_variable_t getVariable() {return p_variable;}
247 double getLastUpdate() {return m_lastUpdate;}
248 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
249 enum heap_action_type getHat() {return m_hat;}
250 bool is_linked() {return action_lmm_hook.is_linked();}
254 lmm_variable_t p_variable;
255 double m_lastValue = 0;
256 double m_lastUpdate = 0;
259 enum heap_action_type m_hat = NOTSET;
262 typedef Action::ActionList ActionList;
264 typedef boost::intrusive::member_hook<
265 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
266 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
267 typedef ActionLmmList* ActionLmmListPtr;
273 /** @ingroup SURF_interface
274 * @brief SURF model interface class
275 * @details A model is an object which handle the interactions between its Resources and its Actions
277 XBT_PUBLIC_CLASS Model {
282 virtual void addTraces() =0;
284 /** @brief Get the set of [actions](@ref Action) in *ready* state */
285 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
287 /** @brief Get the set of [actions](@ref Action) in *running* state */
288 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
290 /** @brief Get the set of [actions](@ref Action) in *failed* state */
291 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
293 /** @brief Get the set of [actions](@ref Action) in *done* state */
294 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
296 /** @brief Get the set of modified [actions](@ref Action) */
297 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
299 /** @brief Get the maxmin system of the current Model */
300 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
303 * @brief Get the update mechanism of the current Model
306 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
308 /** @brief Get Action heap */
309 xbt_heap_t getActionHeap() {return p_actionHeap;}
312 * @brief Share the resources between the actions
314 * @param now The current time of the simulation
315 * @return The delta of time till the next action will finish
317 virtual double shareResources(double now);
318 virtual double shareResourcesLazy(double now);
319 virtual double shareResourcesFull(double now);
320 double shareResourcesMaxMin(ActionList* running_actions,
322 void (*solve) (lmm_system_t));
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 shareResourcesIsIdempotent()=0;
342 ActionLmmListPtr p_modifiedSet;
343 lmm_system_t p_maxminSystem = nullptr;
344 e_UM_t p_updateMechanism = UM_UNDEFINED;
345 int m_selectiveUpdate;
346 xbt_heap_t p_actionHeap;
349 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
350 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
351 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
352 ActionList* p_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 A resource represent an element of a component (e.g.: a link for the network)
378 XBT_PUBLIC_CLASS Resource {
381 * @brief Constructor of non-LMM Resources
383 * @param model Model associated to this Resource
384 * @param name The name of the Resource
386 Resource(Model *model, const char *name);
389 * @brief Constructor of LMM Resources
391 * @param model Model associated to this Resource
392 * @param name The name of the Resource
393 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
395 Resource(Model *model, const char *name, lmm_constraint_t constraint);
397 Resource(Model *model, const char *name, lmm_constraint_t constraint, int initiallyOn);
400 * @brief Resource constructor
402 * @param model Model associated to this Resource
403 * @param name The name of the Resource
404 * @param initiallyOn the initial state of the Resource
406 Resource(Model *model, const char *name, int initiallyOn);
410 /** @brief Get the Model of the current Resource */
413 /** @brief Get the name of the current Resource */
414 const char *getName();
417 * @brief Update the state of the current Resource
420 * @param event_type [TODO]
421 * @param value [TODO]
424 virtual void updateState(tmgr_trace_iterator_t event_type, double value, double date)=0;
426 /** @brief Check if the current Resource is used (if it currently serves an action) */
427 virtual bool isUsed()=0;
429 /** @brief Check if the current Resource is active */
431 /** @brief Check if the current Resource is shut down */
432 virtual bool isOff();
433 /** @brief Turn on the current Resource */
434 virtual void turnOn();
435 /** @brief Turn off the current Resource */
436 virtual void turnOff();
444 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
445 lmm_constraint_t getConstraint();
447 lmm_constraint_t p_constraint = nullptr;
453 #endif /* SURF_MODEL_H_ */