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. */
14 #include <boost/function.hpp>
15 #include <boost/intrusive/list.hpp>
16 #include "surf/trace_mgr.h"
18 #include "surf/surf_routing.h"
19 #include "simgrid/platf_interface.h"
20 #include "surf/surf.h"
21 #include "surf/surf_private.h"
22 #include "internal_config.h"
25 #include <sigc++/sigc++.h>
26 #define surf_callback(arg1, ...) sigc::signal<arg1,__VA_ARGS__>
27 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
28 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
30 #include <boost/signals2.hpp>
31 #define surf_callback(arg1, ...) boost::signals2::signal<arg1(__VA_ARGS__)>
32 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
33 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
36 extern tmgr_history_t history;
37 #define NO_MAX_DURATION -1.0
45 /* user-visible parameters */
46 extern double sg_tcp_gamma;
47 extern double sg_sender_gap;
48 extern double sg_latency_factor;
49 extern double sg_bandwidth_factor;
50 extern double sg_weight_S_parameter;
51 extern int sg_network_crosstraffic;
52 extern xbt_dynar_t surf_path;
55 XBT_PUBLIC(double) surf_get_clock(void);
58 extern double sg_sender_gap;
60 extern surf_callback(void, void) surfExitCallbacks;
62 int __surf_is_absolute_file_path(const char *file_path);
68 enum heap_action_type{
78 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
79 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
80 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
81 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
82 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
83 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
84 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
89 void surf_action_lmm_update_index_heap(void *action, int i);
91 /** @ingroup SURF_interface
92 * @brief SURF action interface class
93 * @details An action is an event generated by a resource (e.g.: a communication for the network)
95 XBT_PUBLIC_CLASS Action {
97 boost::intrusive::list_member_hook<> action_hook;
98 boost::intrusive::list_member_hook<> action_lmm_hook;
99 typedef boost::intrusive::member_hook<
100 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
101 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
104 * @brief Common initializations for the constructors
106 void initialize(Model *model, double cost, bool failed,
107 lmm_variable_t var = NULL);
111 * @brief Action constructor
113 * @param model The Model associated to this Action
114 * @param cost The cost of the Action
115 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
117 Action(Model *model, double cost, bool failed);
120 * @brief Action constructor
122 * @param model The Model associated to this Action
123 * @param cost The cost of the Action
124 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
125 * @param var The lmm variable associated to this Action if it is part of a LMM component
127 Action(Model *model, double cost, bool failed, lmm_variable_t var);
129 /** @brief Destructor */
132 /** @brief Mark that the action is now finished */
135 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
136 e_surf_action_state_t getState(); /**< get the state*/
137 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
138 virtual void setState(e_surf_action_state_t state);
140 /** @brief Get the bound of the current Action */
142 /** @brief Set the bound of the current Action */
143 void setBound(double bound);
145 /** @brief Get the start time of the current action */
146 double getStartTime();
147 /** @brief Get the finish time of the current action */
148 double getFinishTime();
150 /** @brief Get the user data associated to the current action */
151 void *getData() {return p_data;}
152 /** @brief Set the user data associated to the current action */
153 void setData(void* data);
155 /** @brief Get the cost of the current action */
156 double getCost() {return m_cost;}
157 /** @brief Set the cost of the current action */
158 void setCost(double cost) {m_cost = cost;}
160 /** @brief Update the maximum duration of the current action
161 * @param delta Amount to remove from the MaxDuration */
162 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
164 /** @brief Update the remaining time of the current action
165 * @param delta Amount to remove from the remaining time */
166 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
168 /** @brief Set the remaining time of the current action */
169 void setRemains(double value) {m_remains = value;}
170 /** @brief Get the remaining time of the current action after updating the resource */
171 virtual double getRemains();
172 /** @brief Get the remaining time of the current action without updating the resource */
173 double getRemainsNoUpdate();
175 /** @brief Set the finish time of the current action */
176 void setFinishTime(double value) {m_finish = value;}
178 /**@brief Add a reference to the current action (refcounting) */
180 /** @brief Unref that action (and destroy it if refcount reaches 0)
181 * @return true if the action was destroyed and false if someone still has references on it
185 /** @brief Cancel the current Action if running */
186 virtual void cancel();
188 /** @brief Suspend the current Action */
189 virtual void suspend();
191 /** @brief Resume the current Action */
192 virtual void resume();
194 /** @brief Returns true if the current action is running */
195 virtual bool isSuspended();
197 /** @brief Get the maximum duration of the current action */
198 double getMaxDuration() {return m_maxDuration;}
199 /** @brief Set the maximum duration of the current Action */
200 virtual void setMaxDuration(double duration);
202 /** @brief Get the tracing category associated to the current action */
203 char *getCategory() {return p_category;}
204 /** @brief Set the tracing category of the current Action */
205 void setCategory(const char *category);
207 /** @brief Get the priority of the current Action */
208 double getPriority() {return m_priority;};
209 /** @brief Set the priority of the current Action */
210 virtual void setPriority(double priority);
212 /** @brief Get the state set in which the action is */
213 ActionList* getStateSet() {return p_stateSet;};
215 s_xbt_swag_hookup_t p_stateHookup;
217 Model *getModel() {return p_model;}
220 ActionList* p_stateSet;
221 double m_priority; /**< priority (1.0 by default) */
223 double m_remains; /**< How much of that cost remains to be done in the currently running task */
224 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
225 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
228 double m_start; /**< start time */
229 char *p_category; /**< tracing category for categorized resource utilization monitoring */
231 #ifdef HAVE_LATENCY_BOUND_TRACKING
232 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
236 void *p_data; /**< for your convenience */
240 virtual void updateRemainingLazy(double now);
241 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
242 void heapRemove(xbt_heap_t heap);
243 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
244 void updateIndexHeap(int i);
245 lmm_variable_t getVariable() {return p_variable;}
246 double getLastUpdate() {return m_lastUpdate;}
247 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
248 enum heap_action_type getHat() {return m_hat;}
249 bool is_linked() {return action_lmm_hook.is_linked();}
253 lmm_variable_t p_variable;
258 enum heap_action_type m_hat;
261 typedef Action::ActionList ActionList;
263 typedef boost::intrusive::member_hook<
264 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
265 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
266 typedef ActionLmmList* ActionLmmListPtr;
271 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
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 {
279 /** @brief Constructor */
281 /** @brief Destructor */
284 virtual void addTraces() =0;
287 * @brief Get the set of [actions](@ref Action) in *ready* state
289 * @return The set of [actions](@ref Action) in *ready* state
291 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
294 * @brief Get the set of [actions](@ref Action) in *running* state
296 * @return The set of [actions](@ref Action) in *running* state
298 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
301 * @brief Get the set of [actions](@ref Action) in *failed* state
303 * @return The set of [actions](@ref Action) in *failed* state
305 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
308 * @brief Get the set of [actions](@ref Action) in *done* state
310 * @return The set of [actions](@ref Action) in *done* state
312 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
315 * @brief Get the set of modified [actions](@ref Action)
317 * @return The set of modified [actions](@ref Action)
319 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
322 * @brief Get the maxmin system of the current Model
324 * @return The maxmin system of the current Model
326 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
329 * @brief Get the update mechanism of the current Model
332 * @return [description]
334 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
337 * @brief Get Action heap
340 * @return The Action heap
342 xbt_heap_t getActionHeap() {return p_actionHeap;}
345 * @brief share the resources
346 * @details Share the resources between the actions
348 * @param now The current time of the simulation
349 * @return The delta of time till the next action will finish
351 virtual double shareResources(double now);
352 virtual double shareResourcesLazy(double now);
353 virtual double shareResourcesFull(double now);
354 double shareResourcesMaxMin(ActionList* running_actions,
356 void (*solve) (lmm_system_t));
359 * @brief Update state of actions
360 * @details Update action to the current time
362 * @param now The current time of the simulation
363 * @param delta The delta of time since the last update
365 virtual void updateActionsState(double now, double delta);
366 virtual void updateActionsStateLazy(double now, double delta);
367 virtual void updateActionsStateFull(double now, double delta);
369 /** @brief Returns whether this model have an idempotent shareResource()
371 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
372 * so we need to call it only when the next timestamp of other sources is computed.
374 virtual bool shareResourcesIsIdempotent()=0;
377 ActionLmmListPtr p_modifiedSet;
378 lmm_system_t p_maxminSystem;
379 e_UM_t p_updateMechanism = UM_UNDEFINED;
380 int m_selectiveUpdate;
381 xbt_heap_t p_actionHeap;
384 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
385 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
386 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
387 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
394 /** @ingroup SURF_interface
395 * @brief Resource which have a metric handled by a maxmin system
398 double scale; /**< The scale of the metric */
399 double peak; /**< The peak of the metric */
400 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
403 /** @ingroup SURF_interface
404 * @brief SURF resource interface class
405 * @details A resource represent an element of a component (e.g.: a link for the network)
407 XBT_PUBLIC_CLASS Resource {
410 * @brief Resource constructor
415 * @brief Resource constructor
417 * @param model Model associated to this Resource
418 * @param name The name of the Resource
419 * @param props Dictionary of properties associated to this Resource
421 Resource(Model *model, const char *name, xbt_dict_t props);
424 * @brief Resource constructor
426 * @param model Model associated to this Resource
427 * @param name The name of the Resource
428 * @param props Dictionary of properties associated to this Resource
429 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
431 Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
433 * @brief Resource constructor
435 * @param model Model associated to this Resource
436 * @param name The name of the Resource
437 * @param props Dictionary of properties associated to this Resource
438 * @param stateInit the initial state of the Resource
440 Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
443 * @brief Resource destructor
448 * @brief Get the Model of the current Resource
450 * @return The Model of the current Resource
455 * @brief Get the name of the current Resource
457 * @return The name of the current Resource
459 const char *getName();
462 * @brief Get the properties of the current Resource
464 * @return The properties of the current Resource
466 virtual xbt_dict_t getProperties();
469 * @brief Update the state of the current Resource
472 * @param event_type [TODO]
473 * @param value [TODO]
476 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
479 * @brief Check if the current Resource is used
480 * @return true if the current Resource is used, false otherwise
482 virtual bool isUsed()=0;
485 * @brief Check if the current Resource is active
487 * @return true if the current Resource is active, false otherwise
492 * @brief Turn on the current Resource
497 * @brief Turn off the current Resource
502 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
504 * @return The state of the currenrt Resource
506 virtual e_surf_resource_state_t getState();
509 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
511 * @param state The new state of the current Resource
513 virtual void setState(e_surf_resource_state_t state);
517 xbt_dict_t p_properties;
520 e_surf_resource_state_t m_stateCurrent;
525 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
527 * @return The lmm constraint associated to this Resource
529 lmm_constraint_t getConstraint();
531 lmm_constraint_t p_constraint;
534 #endif /* SURF_MODEL_H_ */