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;
53 extern double sg_gtnets_jitter;
54 extern int sg_gtnets_jitter_seed;
56 extern xbt_dynar_t surf_path;
59 XBT_PUBLIC(double) surf_get_clock(void);
62 extern double sg_sender_gap;
64 extern surf_callback(void, void) surfExitCallbacks;
66 int __surf_is_absolute_file_path(const char *file_path);
72 typedef Model* ModelPtr;
75 typedef Resource* ResourcePtr;
78 typedef Action* ActionPtr;
80 typedef boost::intrusive::list<Action> ActionList;
81 typedef ActionList* ActionListPtr;
82 typedef boost::intrusive::list_base_hook<> actionHook;
85 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
86 typedef ActionLmmList* ActionLmmListPtr;
87 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
90 enum heap_action_type{
100 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
101 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
102 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
103 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
104 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
105 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
106 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
111 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
113 /** @ingroup SURF_interface
114 * @brief SURF model interface class
115 * @details A model is an object which handle the interactions between its Resources and its Actions
117 XBT_PUBLIC_CLASS Model {
120 * @brief Model constructor
122 * @param name the name of the model
124 Model(const char *name);
127 * @brief Model destructor
131 virtual void addTraces() =0;
134 * @brief Get the name of the current Model
136 * @return The name of the current Model
138 const char *getName() {return p_name;}
141 * @brief Get the set of [actions](@ref Action) in *ready* state
143 * @return The set of [actions](@ref Action) in *ready* state
145 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
148 * @brief Get the set of [actions](@ref Action) in *running* state
150 * @return The set of [actions](@ref Action) in *running* state
152 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
155 * @brief Get the set of [actions](@ref Action) in *failed* state
157 * @return The set of [actions](@ref Action) in *failed* state
159 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
162 * @brief Get the set of [actions](@ref Action) in *done* state
164 * @return The set of [actions](@ref Action) in *done* state
166 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
169 * @brief Get the set of modified [actions](@ref Action)
171 * @return The set of modified [actions](@ref Action)
173 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
176 * @brief Get the maxmin system of the current Model
178 * @return The maxmin system of the current Model
180 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
183 * @brief Get the update mechanism of the current Model
186 * @return [description]
188 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
191 * @brief Get Action heap
194 * @return The Action heap
196 xbt_heap_t getActionHeap() {return p_actionHeap;}
199 * @brief share the resources
200 * @details Share the resources between the actions
202 * @param now The current time of the simulation
203 * @return The delta of time till the next action will finish
205 virtual double shareResources(double now);
206 virtual double shareResourcesLazy(double now);
207 virtual double shareResourcesFull(double now);
208 double shareResourcesMaxMin(ActionListPtr running_actions,
210 void (*solve) (lmm_system_t));
213 * @brief Update state of actions
214 * @details Update action to the current time
216 * @param now The current time of the simulation
217 * @param delta The delta of time since the last update
219 virtual void updateActionsState(double now, double delta);
220 virtual void updateActionsStateLazy(double now, double delta);
221 virtual void updateActionsStateFull(double now, double delta);
224 ActionLmmListPtr p_modifiedSet;
225 lmm_system_t p_maxminSystem;
226 e_UM_t p_updateMechanism;
227 int m_selectiveUpdate;
228 xbt_heap_t p_actionHeap;
233 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
234 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
235 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
236 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
243 /** @ingroup SURF_interface
244 * @brief Resource which have a metric handled by a maxmin system
247 double scale; /**< The scale of the metric */
248 double peak; /**< The peak of the metric */
249 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
252 /** @ingroup SURF_interface
253 * @brief SURF resource interface class
254 * @details A resource represent an element of a component (e.g.: a link for the network)
256 XBT_PUBLIC_CLASS Resource {
259 * @brief Resource constructor
264 * @brief Resource constructor
266 * @param model Model associated to this Resource
267 * @param name The name of the Resource
268 * @param props Dictionary of properties associated to this Resource
270 Resource(ModelPtr model, const char *name, xbt_dict_t props);
273 * @brief Resource constructor
275 * @param model Model associated to this Resource
276 * @param name The name of the Resource
277 * @param props Dictionary of properties associated to this Resource
278 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
280 Resource(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
282 * @brief Resource constructor
284 * @param model Model associated to this Resource
285 * @param name The name of the Resource
286 * @param props Dictionary of properties associated to this Resource
287 * @param stateInit the initial state of the Resource
289 Resource(ModelPtr model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
292 * @brief Resource destructor
297 * @brief Get the Model of the current Resource
299 * @return The Model of the current Resource
304 * @brief Get the name of the current Resource
306 * @return The name of the current Resource
308 const char *getName();
311 * @brief Get the properties of the current Resource
313 * @return The properties of the current Resource
315 virtual xbt_dict_t getProperties();
318 * @brief Update the state of the current Resource
321 * @param event_type [TODO]
322 * @param value [TODO]
325 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
328 * @brief Check if the current Resource is used
329 * @return true if the current Resource is used, false otherwise
331 virtual bool isUsed()=0;
334 * @brief Check if the current Resource is active
336 * @return true if the current Resource is active, false otherwise
341 * @brief Turn on the current Resource
346 * @brief Turn off the current Resource
351 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
353 * @return The state of the currenrt Resource
355 virtual e_surf_resource_state_t getState();
358 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
360 * @param state The new state of the current Resource
362 virtual void setState(e_surf_resource_state_t state);
366 xbt_dict_t p_properties;
369 e_surf_resource_state_t m_stateCurrent;
374 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
376 * @return The lmm constraint associated to this Resource
378 lmm_constraint_t getConstraint();
380 lmm_constraint_t p_constraint;
386 void surf_action_lmm_update_index_heap(void *action, int i);
388 /** @ingroup SURF_interface
389 * @brief SURF action interface class
390 * @details An action is an event generated by a resource (e.g.: a communication for the network)
392 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
395 * @brief Common initializations for the constructors
397 void initialize(ModelPtr model, double cost, bool failed,
398 lmm_variable_t var = NULL);
402 * @brief Action constructor
404 * @param model The Model associated to this Action
405 * @param cost The cost of the Action
406 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
408 Action(ModelPtr model, double cost, bool failed);
411 * @brief Action constructor
413 * @param model The Model associated to this Action
414 * @param cost The cost of the Action
415 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
416 * @param var The lmm variable associated to this Action if it is part of a LMM component
418 Action(ModelPtr model, double cost, bool failed, lmm_variable_t var);
421 * @brief Action destructor
426 * @brief Finish the action
431 * @brief Get the [state](\ref e_surf_action_state_t) of the current Action
433 * @return The state of the current Action
435 e_surf_action_state_t getState(); /**< get the state*/
438 * @brief Set the [state](\ref e_surf_action_state_t) of the current Action
440 * @param state The new state of the current Action
442 virtual void setState(e_surf_action_state_t state);
445 * @brief Get the bound of the current Action
447 * @return The bound of the current Action
452 * @brief Set the bound of the current Action
454 * @param bound The new bound of the current Action
456 void setBound(double bound);
459 * @brief Get the start time of the current action
461 * @return The start time of the current action
463 double getStartTime();
466 * @brief Get the finish time of the current action
468 * @return The finish time of the current action
470 double getFinishTime();
473 * @brief Get the data associated to the current action
475 * @return The data associated to the current action
477 void *getData() {return p_data;}
480 * @brief Set the data associated to the current action
482 * @param data The new data associated to the current action
484 void setData(void* data);
487 * @brief Get the maximum duration of the current action
489 * @return The maximum duration of the current action
491 double getMaxDuration() {return m_maxDuration;}
494 * @brief Get the category associated to the current action
496 * @return The category associated to the current action
498 char *getCategory() {return p_category;}
501 * @brief Get the cost of the current action
503 * @return The cost of the current action
505 double getCost() {return m_cost;}
508 * @brief Set the cost of the current action
510 * @param cost The new cost of the current action
512 void setCost(double cost) {m_cost = cost;}
515 * @brief Update the maximum duration of the current action
517 * @param delta [TODO]
519 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
522 * @brief Update the remaining time of the current action
524 * @param delta [TODO]
526 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
529 * @brief Set the remaining time of the current action
531 * @param value The new remaining time of the current action
533 void setRemains(double value) {m_remains = value;}
536 * @brief Set the finish time of the current action
538 * @param value The new Finush time of the current action
540 void setFinishTime(double value) {m_finish = value;}
543 * @brief Add a reference to the current action
548 * @brief Remove a reference to the current action
549 * @details If the Action has no more reference, we destroy it
551 * @return true if the action was destroyed and false if someone still has references on it
556 * @brief Cancel the current Action if running
558 virtual void cancel();
561 * @brief Recycle an Action
563 virtual void recycle(){};
566 * @brief Suspend the current Action
568 virtual void suspend();
571 * @brief Resume the current Action
573 virtual void resume();
576 * @brief Check if the current action is running
578 * @return true if the current Action is suspended, false otherwise
580 virtual bool isSuspended();
583 * @brief Set the maximum duration of the current Action
585 * @param duration The new maximum duration of the current Action
587 virtual void setMaxDuration(double duration);
590 * @brief Set the priority of the current Action
592 * @param priority The new priority of the current Action
594 virtual void setPriority(double priority);
597 * @brief Set the category of the current Action
599 * @param category The new category of the current Action
601 void setCategory(const char *category);
604 * @brief Get the remaining time of the current action after updating the resource
606 * @return The remaining time
608 virtual double getRemains();
611 * @brief Get the remaining time of the current action without updating the resource
613 * @return The remaining time
615 double getRemainsNoUpdate();
618 * @brief Get the priority of the current Action
620 * @return The priority of the current Action
622 double getPriority() {return m_priority;};
625 * @brief Get the state set in which the action is
627 * @return The state set in which the action is
629 ActionListPtr getStateSet() {return p_stateSet;};
631 s_xbt_swag_hookup_t p_stateHookup;
633 ModelPtr getModel() {return p_model;}
636 ActionListPtr p_stateSet;
637 double m_priority; /**< priority (1.0 by default) */
639 double m_remains; /**< How much of that cost remains to be done in the currently running task */
640 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
641 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
645 double m_start; /**< start time */
646 char *p_category; /**< tracing category for categorized resource utilization monitoring */
648 #ifdef HAVE_LATENCY_BOUND_TRACKING
649 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
653 void *p_data; /**< for your convenience */
657 virtual void updateRemainingLazy(double now);
658 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
659 void heapRemove(xbt_heap_t heap);
660 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
661 void updateIndexHeap(int i);
662 lmm_variable_t getVariable() {return p_variable;}
663 double getLastUpdate() {return m_lastUpdate;}
664 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
665 enum heap_action_type getHat() {return m_hat;}
666 bool is_linked() {return actionLmmHook::is_linked();}
670 lmm_variable_t p_variable;
675 enum heap_action_type m_hat;
678 #endif /* SURF_MODEL_H_ */