1 /* Copyright (c) 2004-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
16 #include <boost/function.hpp>
17 #include <boost/intrusive/list.hpp>
18 #include "surf/trace_mgr.h"
20 #include "surf/surf_routing.h"
21 #include "simgrid/platf_interface.h"
22 #include "surf/surf.h"
23 #include "src/surf/surf_private.h"
24 #include "src/internal_config.h"
27 #include <sigc++/sigc++.h>
30 // Wraps sigc++ signals with the interface of boost::signals2:
31 template<class T> class signal;
32 template<class R, class... P>
33 class signal<R(P...)> {
35 sigc::signal<R, P...> sig_;
37 template<class T> XBT_ALWAYS_INLINE
38 void connect(T&& slot)
40 sig_.connect(std::forward<T>(slot));
42 template<class Res, class... Args> XBT_ALWAYS_INLINE
43 void connect(Res(*slot)(Args...))
45 sig_.connect(sigc::ptr_fun(slot));
47 template<class... Args>
48 R operator()(Args&&... args) const
50 return sig_.emit(std::forward<Args>(args)...);
56 #include <boost/signals2.hpp>
60 using signal = ::boost::signals2::signal<T>;
66 #pragma warning( disable : 4251)
67 // 4251: needs to have dll-interface to be used by clients of class
70 extern XBT_PRIVATE tmgr_history_t history;
71 #define NO_MAX_DURATION -1.0
77 /* user-visible parameters */
78 extern XBT_PRIVATE double sg_tcp_gamma;
79 extern XBT_PRIVATE double sg_sender_gap;
80 extern XBT_PRIVATE double sg_latency_factor;
81 extern XBT_PRIVATE double sg_bandwidth_factor;
82 extern XBT_PRIVATE double sg_weight_S_parameter;
83 extern XBT_PRIVATE int sg_network_crosstraffic;
84 extern XBT_PRIVATE xbt_dynar_t surf_path;
87 XBT_PUBLIC(double) surf_get_clock(void);
90 extern XBT_PRIVATE double sg_sender_gap;
95 extern XBT_PRIVATE simgrid::surf::signal<void(void)> surfExitCallbacks;
100 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
106 enum heap_action_type{
116 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
117 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
118 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
119 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
120 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
121 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
122 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
128 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
130 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
135 /** @ingroup SURF_interface
136 * @brief SURF action interface class
137 * @details An action is an event generated by a resource (e.g.: a communication for the network)
139 XBT_PUBLIC_CLASS Action {
141 boost::intrusive::list_member_hook<> action_hook;
142 boost::intrusive::list_member_hook<> action_lmm_hook;
143 typedef boost::intrusive::member_hook<
144 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
145 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
148 * @brief Common initializations for the constructors
150 void initialize(simgrid::surf::Model *model, double cost, bool failed,
151 lmm_variable_t var = NULL);
155 * @brief Action constructor
157 * @param model The Model associated to this Action
158 * @param cost The cost of the Action
159 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
161 Action(simgrid::surf::Model *model, double cost, bool failed);
164 * @brief Action constructor
166 * @param model The Model associated to this Action
167 * @param cost The cost of the Action
168 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
169 * @param var The lmm variable associated to this Action if it is part of a LMM component
171 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
173 /** @brief Destructor */
176 /** @brief Mark that the action is now finished */
179 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
180 e_surf_action_state_t getState(); /**< get the state*/
181 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
182 virtual void setState(e_surf_action_state_t state);
184 /** @brief Get the bound of the current Action */
186 /** @brief Set the bound of the current Action */
187 void setBound(double bound);
189 /** @brief Get the start time of the current action */
190 double getStartTime();
191 /** @brief Get the finish time of the current action */
192 double getFinishTime();
194 /** @brief Get the user data associated to the current action */
195 void *getData() {return p_data;}
196 /** @brief Set the user data associated to the current action */
197 void setData(void* data);
199 /** @brief Get the cost of the current action */
200 double getCost() {return m_cost;}
201 /** @brief Set the cost of the current action */
202 void setCost(double cost) {m_cost = cost;}
204 /** @brief Update the maximum duration of the current action
205 * @param delta Amount to remove from the MaxDuration */
206 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
208 /** @brief Update the remaining time of the current action
209 * @param delta Amount to remove from the remaining time */
210 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
212 /** @brief Set the remaining time of the current action */
213 void setRemains(double value) {m_remains = value;}
214 /** @brief Get the remaining time of the current action after updating the resource */
215 virtual double getRemains();
216 /** @brief Get the remaining time of the current action without updating the resource */
217 double getRemainsNoUpdate();
219 /** @brief Set the finish time of the current action */
220 void setFinishTime(double value) {m_finish = value;}
222 /**@brief Add a reference to the current action (refcounting) */
224 /** @brief Unref that action (and destroy it if refcount reaches 0)
225 * @return true if the action was destroyed and false if someone still has references on it
229 /** @brief Cancel the current Action if running */
230 virtual void cancel();
232 /** @brief Suspend the current Action */
233 virtual void suspend();
235 /** @brief Resume the current Action */
236 virtual void resume();
238 /** @brief Returns true if the current action is running */
239 virtual bool isSuspended();
241 /** @brief Get the maximum duration of the current action */
242 double getMaxDuration() {return m_maxDuration;}
243 /** @brief Set the maximum duration of the current Action */
244 virtual void setMaxDuration(double duration);
246 /** @brief Get the tracing category associated to the current action */
247 char *getCategory() {return p_category;}
248 /** @brief Set the tracing category of the current Action */
249 void setCategory(const char *category);
251 /** @brief Get the priority of the current Action */
252 double getPriority() {return m_priority;};
253 /** @brief Set the priority of the current Action */
254 virtual void setPriority(double priority);
256 /** @brief Get the state set in which the action is */
257 ActionList* getStateSet() {return p_stateSet;};
259 s_xbt_swag_hookup_t p_stateHookup;
261 simgrid::surf::Model *getModel() {return p_model;}
264 ActionList* p_stateSet;
265 double m_priority; /**< priority (1.0 by default) */
267 double m_remains; /**< How much of that cost remains to be done in the currently running task */
268 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
269 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
272 double m_start; /**< start time */
273 char *p_category; /**< tracing category for categorized resource utilization monitoring */
275 #ifdef HAVE_LATENCY_BOUND_TRACKING
276 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
279 simgrid::surf::Model *p_model;
280 void *p_data; /**< for your convenience */
284 virtual void updateRemainingLazy(double now);
285 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
286 void heapRemove(xbt_heap_t heap);
287 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
288 void updateIndexHeap(int i);
289 lmm_variable_t getVariable() {return p_variable;}
290 double getLastUpdate() {return m_lastUpdate;}
291 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
292 enum heap_action_type getHat() {return m_hat;}
293 bool is_linked() {return action_lmm_hook.is_linked();}
297 lmm_variable_t p_variable;
302 enum heap_action_type m_hat;
305 typedef Action::ActionList ActionList;
307 typedef boost::intrusive::member_hook<
308 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
309 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
310 typedef ActionLmmList* ActionLmmListPtr;
316 /** @ingroup SURF_interface
317 * @brief SURF model interface class
318 * @details A model is an object which handle the interactions between its Resources and its Actions
320 XBT_PUBLIC_CLASS Model {
325 virtual void addTraces() =0;
327 /** @brief Get the set of [actions](@ref Action) in *ready* state */
328 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
330 /** @brief Get the set of [actions](@ref Action) in *running* state */
331 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
333 /** @brief Get the set of [actions](@ref Action) in *failed* state */
334 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
336 /** @brief Get the set of [actions](@ref Action) in *done* state */
337 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
339 /** @brief Get the set of modified [actions](@ref Action) */
340 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
342 /** @brief Get the maxmin system of the current Model */
343 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
346 * @brief Get the update mechanism of the current Model
349 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
351 /** @brief Get Action heap */
352 xbt_heap_t getActionHeap() {return p_actionHeap;}
355 * @brief Share the resources between the actions
357 * @param now The current time of the simulation
358 * @return The delta of time till the next action will finish
360 virtual double shareResources(double now);
361 virtual double shareResourcesLazy(double now);
362 virtual double shareResourcesFull(double now);
363 double shareResourcesMaxMin(ActionList* running_actions,
365 void (*solve) (lmm_system_t));
368 * @brief Update action to the current time
370 * @param now The current time of the simulation
371 * @param delta The delta of time since the last update
373 virtual void updateActionsState(double now, double delta);
374 virtual void updateActionsStateLazy(double now, double delta);
375 virtual void updateActionsStateFull(double now, double delta);
377 /** @brief Returns whether this model have an idempotent shareResource()
379 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
380 * so we need to call it only when the next timestamp of other sources is computed.
382 virtual bool shareResourcesIsIdempotent()=0;
385 ActionLmmListPtr p_modifiedSet;
386 lmm_system_t p_maxminSystem;
387 e_UM_t p_updateMechanism = UM_UNDEFINED;
388 int m_selectiveUpdate;
389 xbt_heap_t p_actionHeap;
392 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
393 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
394 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
395 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
405 /** @ingroup SURF_interface
406 * @brief Resource which have a metric handled by a maxmin system
409 double peak; /**< The peak of the metric, ie its max value */
410 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
411 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
417 /** @ingroup SURF_interface
418 * @brief SURF resource interface class
419 * @details A resource represent an element of a component (e.g.: a link for the network)
421 XBT_PUBLIC_CLASS Resource {
426 * @brief Constructor of non-LMM Resources
428 * @param model Model associated to this Resource
429 * @param name The name of the Resource
431 Resource(Model *model, const char *name);
434 * @brief Constructor of LMM Resources
436 * @param model Model associated to this Resource
437 * @param name The name of the Resource
438 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
440 Resource(Model *model, const char *name, lmm_constraint_t constraint);
442 Resource(Model *model, const char *name, lmm_constraint_t constraint, e_surf_resource_state_t stateInit);
445 * @brief Resource constructor
447 * @param model Model associated to this Resource
448 * @param name The name of the Resource
449 * @param stateInit the initial state of the Resource
451 Resource(Model *model, const char *name, e_surf_resource_state_t stateInit);
455 /** @brief Get the Model of the current Resource */
458 /** @brief Get the name of the current Resource */
459 const char *getName();
462 * @brief Update the state of the current Resource
465 * @param event_type [TODO]
466 * @param value [TODO]
469 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
471 /** @brief Check if the current Resource is used (if it currently serves an action) */
472 virtual bool isUsed()=0;
474 /** @brief Check if the current Resource is active */
476 /** @brief Turn on the current Resource */
478 /** @brief Turn off the current Resource */
481 /** @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource */
482 virtual e_surf_resource_state_t getState();
483 /** @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource */
484 virtual void setState(e_surf_resource_state_t state);
488 virtual void onDie();
495 e_surf_resource_state_t m_stateCurrent;
499 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
500 lmm_constraint_t getConstraint();
502 lmm_constraint_t p_constraint;
508 #endif /* SURF_MODEL_H_ */