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 #define surf_callback(arg1, ...) ::simgrid::surf::signal<arg1(__VA_ARGS__)>
67 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
68 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
71 #pragma warning( disable : 4251)
72 // 4251: needs to have dll-interface to be used by clients of class
75 extern XBT_PRIVATE tmgr_history_t history;
76 #define NO_MAX_DURATION -1.0
82 /* user-visible parameters */
83 extern XBT_PRIVATE double sg_tcp_gamma;
84 extern XBT_PRIVATE double sg_sender_gap;
85 extern XBT_PRIVATE double sg_latency_factor;
86 extern XBT_PRIVATE double sg_bandwidth_factor;
87 extern XBT_PRIVATE double sg_weight_S_parameter;
88 extern XBT_PRIVATE int sg_network_crosstraffic;
89 extern XBT_PRIVATE xbt_dynar_t surf_path;
92 XBT_PUBLIC(double) surf_get_clock(void);
95 extern XBT_PRIVATE double sg_sender_gap;
100 extern XBT_PRIVATE surf_callback(void, void) surfExitCallbacks;
105 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
111 enum heap_action_type{
121 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
122 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
123 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
124 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
125 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
126 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
127 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
133 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
135 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
140 /** @ingroup SURF_interface
141 * @brief SURF action interface class
142 * @details An action is an event generated by a resource (e.g.: a communication for the network)
144 XBT_PUBLIC_CLASS Action {
146 boost::intrusive::list_member_hook<> action_hook;
147 boost::intrusive::list_member_hook<> action_lmm_hook;
148 typedef boost::intrusive::member_hook<
149 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
150 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
153 * @brief Common initializations for the constructors
155 void initialize(simgrid::surf::Model *model, double cost, bool failed,
156 lmm_variable_t var = NULL);
160 * @brief Action constructor
162 * @param model The Model associated to this Action
163 * @param cost The cost of the Action
164 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
166 Action(simgrid::surf::Model *model, double cost, bool failed);
169 * @brief Action constructor
171 * @param model The Model associated to this Action
172 * @param cost The cost of the Action
173 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
174 * @param var The lmm variable associated to this Action if it is part of a LMM component
176 Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
178 /** @brief Destructor */
181 /** @brief Mark that the action is now finished */
184 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
185 e_surf_action_state_t getState(); /**< get the state*/
186 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
187 virtual void setState(e_surf_action_state_t state);
189 /** @brief Get the bound of the current Action */
191 /** @brief Set the bound of the current Action */
192 void setBound(double bound);
194 /** @brief Get the start time of the current action */
195 double getStartTime();
196 /** @brief Get the finish time of the current action */
197 double getFinishTime();
199 /** @brief Get the user data associated to the current action */
200 void *getData() {return p_data;}
201 /** @brief Set the user data associated to the current action */
202 void setData(void* data);
204 /** @brief Get the cost of the current action */
205 double getCost() {return m_cost;}
206 /** @brief Set the cost of the current action */
207 void setCost(double cost) {m_cost = cost;}
209 /** @brief Update the maximum duration of the current action
210 * @param delta Amount to remove from the MaxDuration */
211 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
213 /** @brief Update the remaining time of the current action
214 * @param delta Amount to remove from the remaining time */
215 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
217 /** @brief Set the remaining time of the current action */
218 void setRemains(double value) {m_remains = value;}
219 /** @brief Get the remaining time of the current action after updating the resource */
220 virtual double getRemains();
221 /** @brief Get the remaining time of the current action without updating the resource */
222 double getRemainsNoUpdate();
224 /** @brief Set the finish time of the current action */
225 void setFinishTime(double value) {m_finish = value;}
227 /**@brief Add a reference to the current action (refcounting) */
229 /** @brief Unref that action (and destroy it if refcount reaches 0)
230 * @return true if the action was destroyed and false if someone still has references on it
234 /** @brief Cancel the current Action if running */
235 virtual void cancel();
237 /** @brief Suspend the current Action */
238 virtual void suspend();
240 /** @brief Resume the current Action */
241 virtual void resume();
243 /** @brief Returns true if the current action is running */
244 virtual bool isSuspended();
246 /** @brief Get the maximum duration of the current action */
247 double getMaxDuration() {return m_maxDuration;}
248 /** @brief Set the maximum duration of the current Action */
249 virtual void setMaxDuration(double duration);
251 /** @brief Get the tracing category associated to the current action */
252 char *getCategory() {return p_category;}
253 /** @brief Set the tracing category of the current Action */
254 void setCategory(const char *category);
256 /** @brief Get the priority of the current Action */
257 double getPriority() {return m_priority;};
258 /** @brief Set the priority of the current Action */
259 virtual void setPriority(double priority);
261 /** @brief Get the state set in which the action is */
262 ActionList* getStateSet() {return p_stateSet;};
264 s_xbt_swag_hookup_t p_stateHookup;
266 simgrid::surf::Model *getModel() {return p_model;}
269 ActionList* p_stateSet;
270 double m_priority; /**< priority (1.0 by default) */
272 double m_remains; /**< How much of that cost remains to be done in the currently running task */
273 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
274 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
277 double m_start; /**< start time */
278 char *p_category; /**< tracing category for categorized resource utilization monitoring */
280 #ifdef HAVE_LATENCY_BOUND_TRACKING
281 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
284 simgrid::surf::Model *p_model;
285 void *p_data; /**< for your convenience */
289 virtual void updateRemainingLazy(double now);
290 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
291 void heapRemove(xbt_heap_t heap);
292 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
293 void updateIndexHeap(int i);
294 lmm_variable_t getVariable() {return p_variable;}
295 double getLastUpdate() {return m_lastUpdate;}
296 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
297 enum heap_action_type getHat() {return m_hat;}
298 bool is_linked() {return action_lmm_hook.is_linked();}
302 lmm_variable_t p_variable;
307 enum heap_action_type m_hat;
310 typedef Action::ActionList ActionList;
312 typedef boost::intrusive::member_hook<
313 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
314 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
315 typedef ActionLmmList* ActionLmmListPtr;
321 /** @ingroup SURF_interface
322 * @brief SURF model interface class
323 * @details A model is an object which handle the interactions between its Resources and its Actions
325 XBT_PUBLIC_CLASS Model {
330 virtual void addTraces() =0;
332 /** @brief Get the set of [actions](@ref Action) in *ready* state */
333 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
335 /** @brief Get the set of [actions](@ref Action) in *running* state */
336 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
338 /** @brief Get the set of [actions](@ref Action) in *failed* state */
339 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
341 /** @brief Get the set of [actions](@ref Action) in *done* state */
342 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
344 /** @brief Get the set of modified [actions](@ref Action) */
345 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
347 /** @brief Get the maxmin system of the current Model */
348 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
351 * @brief Get the update mechanism of the current Model
354 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
356 /** @brief Get Action heap */
357 xbt_heap_t getActionHeap() {return p_actionHeap;}
360 * @brief Share the resources between the actions
362 * @param now The current time of the simulation
363 * @return The delta of time till the next action will finish
365 virtual double shareResources(double now);
366 virtual double shareResourcesLazy(double now);
367 virtual double shareResourcesFull(double now);
368 double shareResourcesMaxMin(ActionList* running_actions,
370 void (*solve) (lmm_system_t));
373 * @brief Update action to the current time
375 * @param now The current time of the simulation
376 * @param delta The delta of time since the last update
378 virtual void updateActionsState(double now, double delta);
379 virtual void updateActionsStateLazy(double now, double delta);
380 virtual void updateActionsStateFull(double now, double delta);
382 /** @brief Returns whether this model have an idempotent shareResource()
384 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
385 * so we need to call it only when the next timestamp of other sources is computed.
387 virtual bool shareResourcesIsIdempotent()=0;
390 ActionLmmListPtr p_modifiedSet;
391 lmm_system_t p_maxminSystem;
392 e_UM_t p_updateMechanism = UM_UNDEFINED;
393 int m_selectiveUpdate;
394 xbt_heap_t p_actionHeap;
397 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
398 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
399 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
400 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
410 /** @ingroup SURF_interface
411 * @brief Resource which have a metric handled by a maxmin system
414 double peak; /**< The peak of the metric, ie its max value */
415 double scale; /**< Current availability of the metric according to the traces, in [0,1] */
416 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
422 /** @ingroup SURF_interface
423 * @brief SURF resource interface class
424 * @details A resource represent an element of a component (e.g.: a link for the network)
426 XBT_PUBLIC_CLASS Resource {
431 * @brief Constructor of non-LMM Resources
433 * @param model Model associated to this Resource
434 * @param name The name of the Resource
436 Resource(Model *model, const char *name);
439 * @brief Constructor of LMM Resources
441 * @param model Model associated to this Resource
442 * @param name The name of the Resource
443 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
445 Resource(Model *model, const char *name, lmm_constraint_t constraint);
447 Resource(Model *model, const char *name, lmm_constraint_t constraint, e_surf_resource_state_t stateInit);
450 * @brief Resource constructor
452 * @param model Model associated to this Resource
453 * @param name The name of the Resource
454 * @param stateInit the initial state of the Resource
456 Resource(Model *model, const char *name, e_surf_resource_state_t stateInit);
460 /** @brief Get the Model of the current Resource */
463 /** @brief Get the name of the current Resource */
464 const char *getName();
467 * @brief Update the state of the current Resource
470 * @param event_type [TODO]
471 * @param value [TODO]
474 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
476 /** @brief Check if the current Resource is used (if it currently serves an action) */
477 virtual bool isUsed()=0;
479 /** @brief Check if the current Resource is active */
481 /** @brief Turn on the current Resource */
483 /** @brief Turn off the current Resource */
486 /** @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource */
487 virtual e_surf_resource_state_t getState();
488 /** @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource */
489 virtual void setState(e_surf_resource_state_t state);
493 virtual void onDie();
500 e_surf_resource_state_t m_stateCurrent;
504 /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
505 lmm_constraint_t getConstraint();
507 lmm_constraint_t p_constraint;
513 #endif /* SURF_MODEL_H_ */