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 <boost/heap/fibonacci_heap.hpp>
17 #include <boost/smart_ptr.hpp>
18 #include "surf/trace_mgr.h"
20 #include "surf/surf_routing.h"
21 #include "simgrid/platf_interface.h"
22 #include "surf/surf.h"
23 #include "surf/surf_private.h"
24 #include "internal_config.h"
27 #include <sigc++/sigc++.h>
28 #define surf_callback(arg1, ...) sigc::signal<arg1,__VA_ARGS__>
29 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
30 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
32 #include <boost/signals2.hpp>
33 #define surf_callback(arg1, ...) boost::signals2::signal<arg1(__VA_ARGS__)>
34 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
35 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
38 extern tmgr_history_t history;
39 #define NO_MAX_DURATION -1.0
47 /* user-visible parameters */
48 extern double sg_tcp_gamma;
49 extern double sg_sender_gap;
50 extern double sg_latency_factor;
51 extern double sg_bandwidth_factor;
52 extern double sg_weight_S_parameter;
53 extern int sg_network_crosstraffic;
55 extern double sg_gtnets_jitter;
56 extern int sg_gtnets_jitter_seed;
58 extern xbt_dynar_t surf_path;
61 XBT_PUBLIC(double) surf_get_clock(void);
64 extern double sg_sender_gap;
65 XBT_PUBLIC(int) SURF_CPU_LEVEL; //Surf cpu level
67 extern surf_callback(void, void) surfExitCallbacks;
69 int __surf_is_absolute_file_path(const char *file_path);
75 typedef Model* ModelPtr;
78 typedef Resource* ResourcePtr;
81 typedef Action* ActionPtr;
83 typedef boost::intrusive::list<Action> ActionList;
84 typedef ActionList* ActionListPtr;
85 typedef boost::intrusive::list_base_hook<> actionHook;
88 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
89 typedef ActionLmmList* ActionLmmListPtr;
90 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
93 template <typename K, typename V>
96 struct compare_PI : binary_function <boost::shared_ptr<HeapItem>, boost::shared_ptr<HeapItem>, bool> {
97 bool operator() (boost::shared_ptr<HeapItem> x, boost::shared_ptr<HeapItem> y) const {
98 return (x->p_key==y->p_key) ? x->m_i<y->m_i : x->p_key>y->p_key;}
101 typedef typename boost::heap::fibonacci_heap<boost::shared_ptr<HeapItem>, boost::heap::compare<compare_PI > >::handle_type HeapHandle;
102 typedef boost::shared_ptr<HeapHandle> HeapHandleSPtr;
106 HeapItem(K key, V value, int i)
107 : p_key(key), p_value(value), m_i(i) {
112 HeapHandleSPtr sp_handle;
114 boost::heap::fibonacci_heap<boost::shared_ptr<HeapItem>, boost::heap::compare<compare_PI > > m_heap;
118 HeapHandleSPtr push(K key, V value) {
119 boost::shared_ptr<HeapItem> hi(new HeapItem(key, value, m_i++));
120 hi->sp_handle = boost::shared_ptr<HeapHandle>(new HeapHandle(m_heap.push(hi)));
121 return hi->sp_handle;
124 HeapHandleSPtr hl = m_heap.top()->sp_handle;
128 void erase(HeapHandleSPtr handle) {
130 m_heap.erase(*handle);
131 handle->node_ = NULL;
134 K topKey() {return m_heap.top()->p_key;}
135 V topValue() {return m_heap.top()->p_value;}
136 int size() {return m_heap.size();}
137 bool empty() {return m_heap.empty();}
138 bool used(HeapHandleSPtr handle) {return handle && handle->node_!=NULL;}
141 typedef Heap<double, ActionPtr> ActionHeap;
143 enum heap_action_type{
153 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
154 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
155 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
156 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
157 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
158 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
159 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
164 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
166 /** @ingroup SURF_interface
167 * @brief SURF model interface class
168 * @details A model is an object which handle the interactions between its Resources and its Actions
170 XBT_PUBLIC_CLASS Model {
173 * @brief Model constructor
175 * @param name the name of the model
177 Model(const char *name);
180 * @brief Model destructor
184 virtual void addTraces() =0;
187 * @brief Get the name of the current Model
189 * @return The name of the current Model
191 const char *getName() {return p_name;}
194 * @brief Get the set of [actions](@ref Action) in *ready* state
196 * @return The set of [actions](@ref Action) in *ready* state
198 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
201 * @brief Get the set of [actions](@ref Action) in *running* state
203 * @return The set of [actions](@ref Action) in *running* state
205 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
208 * @brief Get the set of [actions](@ref Action) in *failed* state
210 * @return The set of [actions](@ref Action) in *failed* state
212 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
215 * @brief Get the set of [actions](@ref Action) in *done* state
217 * @return The set of [actions](@ref Action) in *done* state
219 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
222 * @brief Get the set of modified [actions](@ref Action)
224 * @return The set of modified [actions](@ref Action)
226 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
229 * @brief Get the maxmin system of the current Model
231 * @return The maxmin system of the current Model
233 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
236 * @brief Get the update mechanism of the current Model
239 * @return [description]
241 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
244 * @brief Get Action heap
247 * @return The Action heap
249 ActionHeap *getActionHeap() {return p_actionHeap;}
252 * @brief share the resources
253 * @details Share the resources between the actions
255 * @param now The current time of the simulation
256 * @return The delta of time till the next action will finish
258 virtual double shareResources(double now);
259 virtual double shareResourcesLazy(double now);
260 virtual double shareResourcesFull(double now);
261 double shareResourcesMaxMin(ActionListPtr running_actions,
263 void (*solve) (lmm_system_t));
266 * @brief Update state of actions
267 * @details Update action to the current time
269 * @param now The current time of the simulation
270 * @param delta The delta of time since the last update
272 virtual void updateActionsState(double now, double delta);
273 virtual void updateActionsStateLazy(double now, double delta);
274 virtual void updateActionsStateFull(double now, double delta);
277 ActionLmmListPtr p_modifiedSet;
278 lmm_system_t p_maxminSystem;
279 e_UM_t p_updateMechanism;
280 int m_selectiveUpdate;
281 ActionHeap *p_actionHeap;
286 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
287 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
288 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
289 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
296 /** @ingroup SURF_interface
297 * @brief Resource which have a metric handled by a maxmin system
300 double scale; /**< The scale of the metric */
301 double peak; /**< The peak of the metric */
302 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
305 /** @ingroup SURF_interface
306 * @brief SURF resource interface class
307 * @details A resource represent an element of a component (e.g.: a link for the network)
309 XBT_PUBLIC_CLASS Resource {
312 * @brief Resource constructor
317 * @brief Resource constructor
319 * @param model Model associated to this Resource
320 * @param name The name of the Resource
321 * @param props Dictionary of properties associated to this Resource
323 Resource(ModelPtr model, const char *name, xbt_dict_t props);
326 * @brief Resource constructor
328 * @param model Model associated to this Resource
329 * @param name The name of the Resource
330 * @param props Dictionary of properties associated to this Resource
331 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
333 Resource(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
336 * @brief Resource constructor
338 * @param model Model associated to this Resource
339 * @param name The name of the Resource
340 * @param props Dictionary of properties associated to this Resource
341 * @param stateInit the initial state of the Resource
343 Resource(ModelPtr model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
346 * @brief Resource destructor
351 * @brief Get the Model of the current Resource
353 * @return The Model of the current Resource
358 * @brief Get the name of the current Resource
360 * @return The name of the current Resource
362 const char *getName();
365 * @brief Get the properties of the current Resource
367 * @return The properties of the current Resource
369 virtual xbt_dict_t getProperties();
372 * @brief Update the state of the current Resource
375 * @param event_type [TODO]
376 * @param value [TODO]
379 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
382 * @brief Check if the current Resource is used
383 * @return true if the current Resource is used, false otherwise
385 virtual bool isUsed()=0;
388 * @brief Check if the current Resource is active
390 * @return true if the current Resource is active, false otherwise
395 * @brief Turn on the current Resource
400 * @brief Turn off the current Resource
405 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
407 * @return The state of the currenrt Resource
409 virtual e_surf_resource_state_t getState();
412 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
414 * @param state The new state of the current Resource
416 virtual void setState(e_surf_resource_state_t state);
420 xbt_dict_t p_properties;
423 e_surf_resource_state_t m_stateCurrent;
428 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
430 * @return The lmm constraint associated to this Resource
432 lmm_constraint_t getConstraint();
434 lmm_constraint_t p_constraint;
441 /** @ingroup SURF_interface
442 * @brief SURF action interface class
443 * @details An action is an event generated by a resource (e.g.: a communication for the network)
445 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
448 * @brief Common initializations for the constructors
450 void initialize(ModelPtr model, double cost, bool failed,
451 lmm_variable_t var = NULL);
455 * @brief Action constructor
457 * @param model The Model associated to this Action
458 * @param cost The cost of the Action
459 * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
461 Action(ModelPtr model, double cost, bool failed);
464 * @brief Action constructor
466 * @param model The Model associated to this Action
467 * @param cost The cost of the Action
468 * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
469 * @param var The lmm variable associated to this Action if it is part of a LMM component
471 Action(ModelPtr model, double cost, bool failed, lmm_variable_t var);
474 * @brief Action destructor
479 * @brief Finish the action
484 * @brief Get the [state](\ref e_surf_action_state_t) of the current Action
486 * @return The state of the current Action
488 e_surf_action_state_t getState(); /**< get the state*/
491 * @brief Set the [state](\ref e_surf_action_state_t) of the current Action
493 * @param state The new state of the current Action
495 virtual void setState(e_surf_action_state_t state);
498 * @brief Get the bound of the current Action
500 * @return The bound of the current Action
505 * @brief Set the bound of the current Action
507 * @param bound The new bound of the current Action
509 void setBound(double bound);
512 * @brief Get the start time of the current action
514 * @return The start time of the current action
516 double getStartTime();
519 * @brief Get the finish time of the current action
521 * @return The finish time of the current action
523 double getFinishTime();
526 * @brief Get the data associated to the current action
528 * @return The data associated to the current action
530 void *getData() {return p_data;}
533 * @brief Set the data associated to the current action
535 * @param data The new data associated to the current action
537 void setData(void* data);
540 * @brief Get the maximum duration of the current action
542 * @return The maximum duration of the current action
544 double getMaxDuration() {return m_maxDuration;}
547 * @brief Get the category associated to the current action
549 * @return The category associated to the current action
551 char *getCategory() {return p_category;}
554 * @brief Get the cost of the current action
556 * @return The cost of the current action
558 double getCost() {return m_cost;}
561 * @brief Set the cost of the current action
563 * @param cost The new cost of the current action
565 void setCost(double cost) {m_cost = cost;}
568 * @brief Update the maximum duration of the current action
570 * @param delta [TODO]
572 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
575 * @brief Update the remaining time of the current action
577 * @param delta [TODO]
579 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
582 * @brief Set the remaining time of the current action
584 * @param value The new remaining time of the current action
586 void setRemains(double value) {m_remains = value;}
589 * @brief Set the finish time of the current action
591 * @param value The new Finush time of the current action
593 void setFinishTime(double value) {m_finish = value;}
596 * @brief Add a reference to the current action
601 * @brief Remove a reference to the current action
602 * @details If the Action has no more reference, we destroy it
604 * @return true if the action was destroyed and false if someone still has references on it
609 * @brief Cancel the current Action if running
611 virtual void cancel();
614 * @brief Recycle an Action
616 virtual void recycle(){};
619 * @brief Suspend the current Action
621 virtual void suspend();
624 * @brief Resume the current Action
626 virtual void resume();
629 * @brief Check if the current action is running
631 * @return true if the current Action is suspended, false otherwise
633 virtual bool isSuspended();
636 * @brief Set the maximum duration of the current Action
638 * @param duration The new maximum duration of the current Action
640 virtual void setMaxDuration(double duration);
643 * @brief Set the priority of the current Action
645 * @param priority The new priority of the current Action
647 virtual void setPriority(double priority);
651 * @brief Set the category of the current Action
653 * @param category The new category of the current Action
655 void setCategory(const char *category);
659 * @brief Get the remaining time of the current action after updating the resource
661 * @return The remaining time
663 virtual double getRemains();
666 * @brief Get the remaining time of the current action without updating the resource
668 * @return The remaining time
670 double getRemainsNoUpdate();
673 * @brief Get the priority of the current Action
675 * @return The priority of the current Action
677 double getPriority() {return m_priority;};
680 * @brief Get the state set in which the action is
683 * @return The state set in which the action is
685 ActionListPtr getStateSet() {return p_stateSet;};
687 s_xbt_swag_hookup_t p_stateHookup;
689 ModelPtr getModel() {return p_model;}
692 ActionListPtr p_stateSet;
693 double m_priority; /**< priority (1.0 by default) */
695 double m_remains; /**< How much of that cost remains to be done in the currently running task */
696 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
697 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
700 int resourceUsed(void *resource_id);
703 * @brief Share the resources to the actions
707 * @return in how much time the next action may terminatedescription]
709 double shareResources(double now);
712 * @brief Update the current action state
716 * @param delta [TODO]
718 void updateActionsState(double now, double delta);
721 * @brief Update the [TODO]
725 * @param event_type [TODO]
726 * @param value [TODO]
729 void updateResourceState(void *id, tmgr_trace_event_t event_type,
730 double value, double time);
732 ActionLmmListPtr p_modifiedSet;
733 int m_selectiveUpdate;
735 double m_start; /**< start time */
736 char *p_category; /**< tracing category for categorized resource utilization monitoring */
738 #ifdef HAVE_LATENCY_BOUND_TRACKING
739 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
743 void *p_data; /**< for your convenience */
747 void heapInsert(double key, enum heap_action_type hat);
749 enum heap_action_type getHeapActionType() {return m_hat;}
750 virtual void updateRemainingLazy(double now);
751 lmm_variable_t getVariable() {return p_variable;}
752 double getLastUpdate() {return m_lastUpdate;}
753 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
754 bool is_linked() {return actionLmmHook::is_linked();}
758 lmm_variable_t p_variable;
762 ActionHeap::HeapHandleSPtr sp_heapHandle;
763 enum heap_action_type m_hat;
766 #endif /* SURF_MODEL_H_ */