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 "src/surf/surf_private.h"
22 #include "src/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) all_existing_models;
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 {
287 virtual void addTraces() =0;
289 /** @brief Get the set of [actions](@ref Action) in *ready* state */
290 virtual ActionList* getReadyActionSet() {return p_readyActionSet;}
292 /** @brief Get the set of [actions](@ref Action) in *running* state */
293 virtual ActionList* getRunningActionSet() {return p_runningActionSet;}
295 /** @brief Get the set of [actions](@ref Action) in *failed* state */
296 virtual ActionList* getFailedActionSet() {return p_failedActionSet;}
298 /** @brief Get the set of [actions](@ref Action) in *done* state */
299 virtual ActionList* getDoneActionSet() {return p_doneActionSet;}
301 /** @brief Get the set of modified [actions](@ref Action) */
302 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
304 /** @brief Get the maxmin system of the current Model */
305 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
308 * @brief Get the update mechanism of the current Model
311 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
313 /** @brief Get Action heap */
314 xbt_heap_t getActionHeap() {return p_actionHeap;}
317 * @brief Share the resources between the actions
319 * @param now The current time of the simulation
320 * @return The delta of time till the next action will finish
322 virtual double shareResources(double now);
323 virtual double shareResourcesLazy(double now);
324 virtual double shareResourcesFull(double now);
325 double shareResourcesMaxMin(ActionList* running_actions,
327 void (*solve) (lmm_system_t));
330 * @brief Update action to the current time
332 * @param now The current time of the simulation
333 * @param delta The delta of time since the last update
335 virtual void updateActionsState(double now, double delta);
336 virtual void updateActionsStateLazy(double now, double delta);
337 virtual void updateActionsStateFull(double now, double delta);
339 /** @brief Returns whether this model have an idempotent shareResource()
341 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
342 * so we need to call it only when the next timestamp of other sources is computed.
344 virtual bool shareResourcesIsIdempotent()=0;
347 ActionLmmListPtr p_modifiedSet;
348 lmm_system_t p_maxminSystem;
349 e_UM_t p_updateMechanism = UM_UNDEFINED;
350 int m_selectiveUpdate;
351 xbt_heap_t p_actionHeap;
354 ActionList* p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
355 ActionList* p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
356 ActionList* p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
357 ActionList* p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
364 /** @ingroup SURF_interface
365 * @brief Resource which have a metric handled by a maxmin system
368 double scale; /**< The scale of the metric */
369 double peak; /**< The peak of the metric */
370 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
373 /** @ingroup SURF_interface
374 * @brief SURF resource interface class
375 * @details A resource represent an element of a component (e.g.: a link for the network)
377 XBT_PUBLIC_CLASS Resource {
380 * @brief Resource constructor
385 * @brief Resource constructor
387 * @param model Model associated to this Resource
388 * @param name The name of the Resource
389 * @param props Dictionary of properties associated to this Resource
391 Resource(Model *model, const char *name, xbt_dict_t props);
394 * @brief Resource constructor
396 * @param model Model associated to this Resource
397 * @param name The name of the Resource
398 * @param props Dictionary of properties associated to this Resource
399 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
401 Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
403 * @brief Resource constructor
405 * @param model Model associated to this Resource
406 * @param name The name of the Resource
407 * @param props Dictionary of properties associated to this Resource
408 * @param stateInit the initial state of the Resource
410 Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
413 * @brief Resource destructor
418 * @brief Get the Model of the current Resource
420 * @return The Model of the current Resource
425 * @brief Get the name of the current Resource
427 * @return The name of the current Resource
429 const char *getName();
432 * @brief Get the properties of the current Resource
434 * @return The properties of the current Resource
436 virtual xbt_dict_t getProperties();
439 * @brief Update the state of the current Resource
442 * @param event_type [TODO]
443 * @param value [TODO]
446 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
449 * @brief Check if the current Resource is used
450 * @return true if the current Resource is used, false otherwise
452 virtual bool isUsed()=0;
455 * @brief Check if the current Resource is active
457 * @return true if the current Resource is active, false otherwise
462 * @brief Turn on the current Resource
467 * @brief Turn off the current Resource
472 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
474 * @return The state of the currenrt Resource
476 virtual e_surf_resource_state_t getState();
479 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
481 * @param state The new state of the current Resource
483 virtual void setState(e_surf_resource_state_t state);
487 xbt_dict_t p_properties;
490 e_surf_resource_state_t m_stateCurrent;
495 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
497 * @return The lmm constraint associated to this Resource
499 lmm_constraint_t getConstraint();
501 lmm_constraint_t p_constraint;
504 #endif /* SURF_MODEL_H_ */