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__)
37 #pragma warning( disable : 4251)
38 // 4251: needs to have dll-interface to be used by clients of class
41 extern XBT_PRIVATE tmgr_history_t history;
42 #define NO_MAX_DURATION -1.0
50 /* user-visible parameters */
51 extern XBT_PRIVATE double sg_tcp_gamma;
52 extern XBT_PRIVATE double sg_sender_gap;
53 extern XBT_PRIVATE double sg_latency_factor;
54 extern XBT_PRIVATE double sg_bandwidth_factor;
55 extern XBT_PRIVATE double sg_weight_S_parameter;
56 extern XBT_PRIVATE int sg_network_crosstraffic;
57 extern XBT_PRIVATE xbt_dynar_t surf_path;
60 XBT_PUBLIC(double) surf_get_clock(void);
63 extern XBT_PRIVATE double sg_sender_gap;
65 extern XBT_PRIVATE surf_callback(void, void) surfExitCallbacks;
67 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
73 enum heap_action_type{
83 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
84 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
85 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
86 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
87 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
88 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
89 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
94 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
96 /** @ingroup SURF_interface
97 * @brief SURF action interface class
98 * @details An action is an event generated by a resource (e.g.: a communication for the network)
100 XBT_PUBLIC_CLASS Action {
102 boost::intrusive::list_member_hook<> action_hook;
103 boost::intrusive::list_member_hook<> action_lmm_hook;
104 typedef boost::intrusive::member_hook<
105 Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
106 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
109 * @brief Common initializations for the constructors
111 void initialize(Model *model, double cost, bool failed,
112 lmm_variable_t var = NULL);
116 * @brief Action constructor
118 * @param model The Model associated to this Action
119 * @param cost The cost of the Action
120 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
122 Action(Model *model, double cost, bool failed);
125 * @brief Action constructor
127 * @param model The Model associated to this Action
128 * @param cost The cost of the Action
129 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
130 * @param var The lmm variable associated to this Action if it is part of a LMM component
132 Action(Model *model, double cost, bool failed, lmm_variable_t var);
134 /** @brief Destructor */
137 /** @brief Mark that the action is now finished */
140 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
141 e_surf_action_state_t getState(); /**< get the state*/
142 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
143 virtual void setState(e_surf_action_state_t state);
145 /** @brief Get the bound of the current Action */
147 /** @brief Set the bound of the current Action */
148 void setBound(double bound);
150 /** @brief Get the start time of the current action */
151 double getStartTime();
152 /** @brief Get the finish time of the current action */
153 double getFinishTime();
155 /** @brief Get the user data associated to the current action */
156 void *getData() {return p_data;}
157 /** @brief Set the user data associated to the current action */
158 void setData(void* data);
160 /** @brief Get the cost of the current action */
161 double getCost() {return m_cost;}
162 /** @brief Set the cost of the current action */
163 void setCost(double cost) {m_cost = cost;}
165 /** @brief Update the maximum duration of the current action
166 * @param delta Amount to remove from the MaxDuration */
167 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
169 /** @brief Update the remaining time of the current action
170 * @param delta Amount to remove from the remaining time */
171 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
173 /** @brief Set the remaining time of the current action */
174 void setRemains(double value) {m_remains = value;}
175 /** @brief Get the remaining time of the current action after updating the resource */
176 virtual double getRemains();
177 /** @brief Get the remaining time of the current action without updating the resource */
178 double getRemainsNoUpdate();
180 /** @brief Set the finish time of the current action */
181 void setFinishTime(double value) {m_finish = value;}
183 /**@brief Add a reference to the current action (refcounting) */
185 /** @brief Unref that action (and destroy it if refcount reaches 0)
186 * @return true if the action was destroyed and false if someone still has references on it
190 /** @brief Cancel the current Action if running */
191 virtual void cancel();
193 /** @brief Suspend the current Action */
194 virtual void suspend();
196 /** @brief Resume the current Action */
197 virtual void resume();
199 /** @brief Returns true if the current action is running */
200 virtual bool isSuspended();
202 /** @brief Get the maximum duration of the current action */
203 double getMaxDuration() {return m_maxDuration;}
204 /** @brief Set the maximum duration of the current Action */
205 virtual void setMaxDuration(double duration);
207 /** @brief Get the tracing category associated to the current action */
208 char *getCategory() {return p_category;}
209 /** @brief Set the tracing category of the current Action */
210 void setCategory(const char *category);
212 /** @brief Get the priority of the current Action */
213 double getPriority() {return m_priority;};
214 /** @brief Set the priority of the current Action */
215 virtual void setPriority(double priority);
217 /** @brief Get the state set in which the action is */
218 ActionList* getStateSet() {return p_stateSet;};
220 s_xbt_swag_hookup_t p_stateHookup;
222 Model *getModel() {return p_model;}
225 ActionList* p_stateSet;
226 double m_priority; /**< priority (1.0 by default) */
228 double m_remains; /**< How much of that cost remains to be done in the currently running task */
229 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
230 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
233 double m_start; /**< start time */
234 char *p_category; /**< tracing category for categorized resource utilization monitoring */
236 #ifdef HAVE_LATENCY_BOUND_TRACKING
237 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
241 void *p_data; /**< for your convenience */
245 virtual void updateRemainingLazy(double now);
246 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
247 void heapRemove(xbt_heap_t heap);
248 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
249 void updateIndexHeap(int i);
250 lmm_variable_t getVariable() {return p_variable;}
251 double getLastUpdate() {return m_lastUpdate;}
252 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
253 enum heap_action_type getHat() {return m_hat;}
254 bool is_linked() {return action_lmm_hook.is_linked();}
258 lmm_variable_t p_variable;
263 enum heap_action_type m_hat;
266 typedef Action::ActionList ActionList;
268 typedef boost::intrusive::member_hook<
269 Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
270 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
271 typedef ActionLmmList* ActionLmmListPtr;
276 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
278 /** @ingroup SURF_interface
279 * @brief SURF model interface class
280 * @details A model is an object which handle the interactions between its Resources and its Actions
282 XBT_PUBLIC_CLASS Model {
284 /** @brief Constructor */
286 /** @brief Destructor */
289 virtual void addTraces() =0;
292 * @brief Get the set of [actions](@ref Action) in *ready* state
294 * @return The set of [actions](@ref Action) in *ready* state
296 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
299 * @brief Get the set of [actions](@ref Action) in *running* state
301 * @return The set of [actions](@ref Action) in *running* state
303 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
306 * @brief Get the set of [actions](@ref Action) in *failed* state
308 * @return The set of [actions](@ref Action) in *failed* state
310 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
313 * @brief Get the set of [actions](@ref Action) in *done* state
315 * @return The set of [actions](@ref Action) in *done* state
317 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
320 * @brief Get the set of modified [actions](@ref Action)
322 * @return The set of modified [actions](@ref Action)
324 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
327 * @brief Get the maxmin system of the current Model
329 * @return The maxmin system of the current Model
331 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
334 * @brief Get the update mechanism of the current Model
337 * @return [description]
339 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
342 * @brief Get Action heap
345 * @return The Action heap
347 xbt_heap_t getActionHeap() {return p_actionHeap;}
350 * @brief share the resources
351 * @details Share the resources between the actions
353 * @param now The current time of the simulation
354 * @return The delta of time till the next action will finish
356 virtual double shareResources(double now);
357 virtual double shareResourcesLazy(double now);
358 virtual double shareResourcesFull(double now);
359 double shareResourcesMaxMin(ActionList* running_actions,
361 void (*solve) (lmm_system_t));
364 * @brief Update state of actions
365 * @details Update action to the current time
367 * @param now The current time of the simulation
368 * @param delta The delta of time since the last update
370 virtual void updateActionsState(double now, double delta);
371 virtual void updateActionsStateLazy(double now, double delta);
372 virtual void updateActionsStateFull(double now, double delta);
374 /** @brief Returns whether this model have an idempotent shareResource()
376 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
377 * so we need to call it only when the next timestamp of other sources is computed.
379 virtual bool shareResourcesIsIdempotent()=0;
382 ActionLmmListPtr p_modifiedSet;
383 lmm_system_t p_maxminSystem;
384 e_UM_t p_updateMechanism = UM_UNDEFINED;
385 int m_selectiveUpdate;
386 xbt_heap_t p_actionHeap;
389 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
390 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
391 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
392 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
399 /** @ingroup SURF_interface
400 * @brief Resource which have a metric handled by a maxmin system
403 double scale; /**< The scale of the metric */
404 double peak; /**< The peak of the metric */
405 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
408 /** @ingroup SURF_interface
409 * @brief SURF resource interface class
410 * @details A resource represent an element of a component (e.g.: a link for the network)
412 XBT_PUBLIC_CLASS Resource {
415 * @brief Resource constructor
420 * @brief Resource constructor
422 * @param model Model associated to this Resource
423 * @param name The name of the Resource
424 * @param props Dictionary of properties associated to this Resource
426 Resource(Model *model, const char *name, xbt_dict_t props);
429 * @brief Resource constructor
431 * @param model Model associated to this Resource
432 * @param name The name of the Resource
433 * @param props Dictionary of properties associated to this Resource
434 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
436 Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
438 * @brief Resource constructor
440 * @param model Model associated to this Resource
441 * @param name The name of the Resource
442 * @param props Dictionary of properties associated to this Resource
443 * @param stateInit the initial state of the Resource
445 Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
448 * @brief Resource destructor
453 * @brief Get the Model of the current Resource
455 * @return The Model of the current Resource
460 * @brief Get the name of the current Resource
462 * @return The name of the current Resource
464 const char *getName();
467 * @brief Get the properties of the current Resource
469 * @return The properties of the current Resource
471 virtual xbt_dict_t getProperties();
474 * @brief Update the state of the current Resource
477 * @param event_type [TODO]
478 * @param value [TODO]
481 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
484 * @brief Check if the current Resource is used
485 * @return true if the current Resource is used, false otherwise
487 virtual bool isUsed()=0;
490 * @brief Check if the current Resource is active
492 * @return true if the current Resource is active, false otherwise
497 * @brief Turn on the current Resource
502 * @brief Turn off the current Resource
507 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
509 * @return The state of the currenrt Resource
511 virtual e_surf_resource_state_t getState();
514 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
516 * @param state The new state of the current Resource
518 virtual void setState(e_surf_resource_state_t state);
522 xbt_dict_t p_properties;
525 e_surf_resource_state_t m_stateCurrent;
530 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
532 * @return The lmm constraint associated to this Resource
534 lmm_constraint_t getConstraint();
536 lmm_constraint_t p_constraint;
539 #endif /* SURF_MODEL_H_ */