1 /* Copyright (c) 2004-2014. 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);
67 typedef boost::intrusive::list<Action> ActionList;
68 typedef ActionList* ActionListPtr;
69 typedef boost::intrusive::list_base_hook<> actionHook;
72 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
73 typedef ActionLmmList* ActionLmmListPtr;
74 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
77 enum heap_action_type{
87 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
88 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
89 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
90 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
91 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
92 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
98 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
100 /** @ingroup SURF_interface
101 * @brief SURF model interface class
102 * @details A model is an object which handle the interactions between its Resources and its Actions
104 XBT_PUBLIC_CLASS Model {
106 /** @brief Constructor */
108 /** @brief Destructor */
111 virtual void addTraces() =0;
114 * @brief Get the set of [actions](@ref Action) in *ready* state
116 * @return The set of [actions](@ref Action) in *ready* state
118 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
121 * @brief Get the set of [actions](@ref Action) in *running* state
123 * @return The set of [actions](@ref Action) in *running* state
125 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
128 * @brief Get the set of [actions](@ref Action) in *failed* state
130 * @return The set of [actions](@ref Action) in *failed* state
132 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
135 * @brief Get the set of [actions](@ref Action) in *done* state
137 * @return The set of [actions](@ref Action) in *done* state
139 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
142 * @brief Get the set of modified [actions](@ref Action)
144 * @return The set of modified [actions](@ref Action)
146 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
149 * @brief Get the maxmin system of the current Model
151 * @return The maxmin system of the current Model
153 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
156 * @brief Get the update mechanism of the current Model
159 * @return [description]
161 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
164 * @brief Get Action heap
167 * @return The Action heap
169 xbt_heap_t getActionHeap() {return p_actionHeap;}
172 * @brief share the resources
173 * @details Share the resources between the actions
175 * @param now The current time of the simulation
176 * @return The delta of time till the next action will finish
178 virtual double shareResources(double now);
179 virtual double shareResourcesLazy(double now);
180 virtual double shareResourcesFull(double now);
181 double shareResourcesMaxMin(ActionListPtr running_actions,
183 void (*solve) (lmm_system_t));
186 * @brief Update state of actions
187 * @details Update action to the current time
189 * @param now The current time of the simulation
190 * @param delta The delta of time since the last update
192 virtual void updateActionsState(double now, double delta);
193 virtual void updateActionsStateLazy(double now, double delta);
194 virtual void updateActionsStateFull(double now, double delta);
196 /** @brief Returns whether this model have an idempotent shareResource()
198 * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
199 * so we need to call it only when the next timestamp of other sources is computed.
201 virtual bool shareResourcesIsIdempotent()=0;
204 ActionLmmListPtr p_modifiedSet;
205 lmm_system_t p_maxminSystem;
206 e_UM_t p_updateMechanism;
207 int m_selectiveUpdate;
208 xbt_heap_t p_actionHeap;
211 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
212 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
213 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
214 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
221 /** @ingroup SURF_interface
222 * @brief Resource which have a metric handled by a maxmin system
225 double scale; /**< The scale of the metric */
226 double peak; /**< The peak of the metric */
227 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
230 /** @ingroup SURF_interface
231 * @brief SURF resource interface class
232 * @details A resource represent an element of a component (e.g.: a link for the network)
234 XBT_PUBLIC_CLASS Resource {
237 * @brief Resource constructor
242 * @brief Resource constructor
244 * @param model Model associated to this Resource
245 * @param name The name of the Resource
246 * @param props Dictionary of properties associated to this Resource
248 Resource(Model *model, const char *name, xbt_dict_t props);
251 * @brief Resource constructor
253 * @param model Model associated to this Resource
254 * @param name The name of the Resource
255 * @param props Dictionary of properties associated to this Resource
256 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
258 Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
260 * @brief Resource constructor
262 * @param model Model associated to this Resource
263 * @param name The name of the Resource
264 * @param props Dictionary of properties associated to this Resource
265 * @param stateInit the initial state of the Resource
267 Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
270 * @brief Resource destructor
275 * @brief Get the Model of the current Resource
277 * @return The Model of the current Resource
282 * @brief Get the name of the current Resource
284 * @return The name of the current Resource
286 const char *getName();
289 * @brief Get the properties of the current Resource
291 * @return The properties of the current Resource
293 virtual xbt_dict_t getProperties();
296 * @brief Update the state of the current Resource
299 * @param event_type [TODO]
300 * @param value [TODO]
303 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
306 * @brief Check if the current Resource is used
307 * @return true if the current Resource is used, false otherwise
309 virtual bool isUsed()=0;
312 * @brief Check if the current Resource is active
314 * @return true if the current Resource is active, false otherwise
319 * @brief Turn on the current Resource
324 * @brief Turn off the current Resource
329 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
331 * @return The state of the currenrt Resource
333 virtual e_surf_resource_state_t getState();
336 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
338 * @param state The new state of the current Resource
340 virtual void setState(e_surf_resource_state_t state);
344 xbt_dict_t p_properties;
347 e_surf_resource_state_t m_stateCurrent;
352 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
354 * @return The lmm constraint associated to this Resource
356 lmm_constraint_t getConstraint();
358 lmm_constraint_t p_constraint;
364 void surf_action_lmm_update_index_heap(void *action, int i);
366 /** @ingroup SURF_interface
367 * @brief SURF action interface class
368 * @details An action is an event generated by a resource (e.g.: a communication for the network)
370 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
373 * @brief Common initializations for the constructors
375 void initialize(Model *model, double cost, bool failed,
376 lmm_variable_t var = NULL);
380 * @brief Action constructor
382 * @param model The Model associated to this Action
383 * @param cost The cost of the Action
384 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
386 Action(Model *model, double cost, bool failed);
389 * @brief Action constructor
391 * @param model The Model associated to this Action
392 * @param cost The cost of the Action
393 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
394 * @param var The lmm variable associated to this Action if it is part of a LMM component
396 Action(Model *model, double cost, bool failed, lmm_variable_t var);
398 /** @brief Destructor */
401 /** @brief Mark that the action is now finished */
404 /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
405 e_surf_action_state_t getState(); /**< get the state*/
406 /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
407 virtual void setState(e_surf_action_state_t state);
409 /** @brief Get the bound of the current Action */
411 /** @brief Set the bound of the current Action */
412 void setBound(double bound);
414 /** @brief Get the start time of the current action */
415 double getStartTime();
416 /** @brief Get the finish time of the current action */
417 double getFinishTime();
419 /** @brief Get the user data associated to the current action */
420 void *getData() {return p_data;}
421 /** @brief Set the user data associated to the current action */
422 void setData(void* data);
424 /** @brief Get the cost of the current action */
425 double getCost() {return m_cost;}
426 /** @brief Set the cost of the current action */
427 void setCost(double cost) {m_cost = cost;}
429 /** @brief Update the maximum duration of the current action
430 * @param delta Amount to remove from the MaxDuration */
431 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
433 /** @brief Update the remaining time of the current action
434 * @param delta Amount to remove from the remaining time */
435 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
437 /** @brief Set the remaining time of the current action */
438 void setRemains(double value) {m_remains = value;}
439 /** @brief Get the remaining time of the current action after updating the resource */
440 virtual double getRemains();
441 /** @brief Get the remaining time of the current action without updating the resource */
442 double getRemainsNoUpdate();
444 /** @brief Set the finish time of the current action */
445 void setFinishTime(double value) {m_finish = value;}
447 /**@brief Add a reference to the current action (refcounting) */
449 /** @brief Unref that action (and destroy it if refcount reaches 0)
450 * @return true if the action was destroyed and false if someone still has references on it
454 /** @brief Cancel the current Action if running */
455 virtual void cancel();
457 /** @brief Suspend the current Action */
458 virtual void suspend();
460 /** @brief Resume the current Action */
461 virtual void resume();
463 /** @brief Returns true if the current action is running */
464 virtual bool isSuspended();
466 /** @brief Get the maximum duration of the current action */
467 double getMaxDuration() {return m_maxDuration;}
468 /** @brief Set the maximum duration of the current Action */
469 virtual void setMaxDuration(double duration);
471 /** @brief Get the tracing category associated to the current action */
472 char *getCategory() {return p_category;}
473 /** @brief Set the tracing category of the current Action */
474 void setCategory(const char *category);
476 /** @brief Get the priority of the current Action */
477 double getPriority() {return m_priority;};
478 /** @brief Set the priority of the current Action */
479 virtual void setPriority(double priority);
481 /** @brief Get the state set in which the action is */
482 ActionListPtr getStateSet() {return p_stateSet;};
484 s_xbt_swag_hookup_t p_stateHookup;
486 Model *getModel() {return p_model;}
489 ActionListPtr p_stateSet;
490 double m_priority; /**< priority (1.0 by default) */
492 double m_remains; /**< How much of that cost remains to be done in the currently running task */
493 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
494 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
497 double m_start; /**< start time */
498 char *p_category; /**< tracing category for categorized resource utilization monitoring */
500 #ifdef HAVE_LATENCY_BOUND_TRACKING
501 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
505 void *p_data; /**< for your convenience */
509 virtual void updateRemainingLazy(double now);
510 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
511 void heapRemove(xbt_heap_t heap);
512 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
513 void updateIndexHeap(int i);
514 lmm_variable_t getVariable() {return p_variable;}
515 double getLastUpdate() {return m_lastUpdate;}
516 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
517 enum heap_action_type getHat() {return m_hat;}
518 bool is_linked() {return actionLmmHook::is_linked();}
522 lmm_variable_t p_variable;
527 enum heap_action_type m_hat;
530 #endif /* SURF_MODEL_H_ */