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);
71 typedef boost::intrusive::list<Action> ActionList;
72 typedef ActionList* ActionListPtr;
73 typedef boost::intrusive::list_base_hook<> actionHook;
76 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
77 typedef ActionLmmList* ActionLmmListPtr;
78 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
81 enum heap_action_type{
91 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
92 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
94 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
95 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
96 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
97 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
102 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
104 /** @ingroup SURF_interface
105 * @brief SURF model interface class
106 * @details A model is an object which handle the interactions between its Resources and its Actions
108 XBT_PUBLIC_CLASS Model {
111 * @brief Model constructor
113 * @param name the name of the model
115 Model(const char *name);
118 * @brief Model destructor
122 virtual void addTraces() =0;
125 * @brief Get the name of the current Model
127 * @return The name of the current Model
129 const char *getName() {return p_name;}
132 * @brief Get the set of [actions](@ref Action) in *ready* state
134 * @return The set of [actions](@ref Action) in *ready* state
136 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
139 * @brief Get the set of [actions](@ref Action) in *running* state
141 * @return The set of [actions](@ref Action) in *running* state
143 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
146 * @brief Get the set of [actions](@ref Action) in *failed* state
148 * @return The set of [actions](@ref Action) in *failed* state
150 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
153 * @brief Get the set of [actions](@ref Action) in *done* state
155 * @return The set of [actions](@ref Action) in *done* state
157 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
160 * @brief Get the set of modified [actions](@ref Action)
162 * @return The set of modified [actions](@ref Action)
164 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
167 * @brief Get the maxmin system of the current Model
169 * @return The maxmin system of the current Model
171 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
174 * @brief Get the update mechanism of the current Model
177 * @return [description]
179 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
182 * @brief Get Action heap
185 * @return The Action heap
187 xbt_heap_t getActionHeap() {return p_actionHeap;}
190 * @brief share the resources
191 * @details Share the resources between the actions
193 * @param now The current time of the simulation
194 * @return The delta of time till the next action will finish
196 virtual double shareResources(double now);
197 virtual double shareResourcesLazy(double now);
198 virtual double shareResourcesFull(double now);
199 double shareResourcesMaxMin(ActionListPtr running_actions,
201 void (*solve) (lmm_system_t));
204 * @brief Update state of actions
205 * @details Update action to the current time
207 * @param now The current time of the simulation
208 * @param delta The delta of time since the last update
210 virtual void updateActionsState(double now, double delta);
211 virtual void updateActionsStateLazy(double now, double delta);
212 virtual void updateActionsStateFull(double now, double delta);
215 ActionLmmListPtr p_modifiedSet;
216 lmm_system_t p_maxminSystem;
217 e_UM_t p_updateMechanism;
218 int m_selectiveUpdate;
219 xbt_heap_t p_actionHeap;
224 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
225 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
226 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
227 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
234 /** @ingroup SURF_interface
235 * @brief Resource which have a metric handled by a maxmin system
238 double scale; /**< The scale of the metric */
239 double peak; /**< The peak of the metric */
240 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
243 /** @ingroup SURF_interface
244 * @brief SURF resource interface class
245 * @details A resource represent an element of a component (e.g.: a link for the network)
247 XBT_PUBLIC_CLASS Resource {
250 * @brief Resource constructor
255 * @brief Resource constructor
257 * @param model Model associated to this Resource
258 * @param name The name of the Resource
259 * @param props Dictionary of properties associated to this Resource
261 Resource(Model *model, const char *name, xbt_dict_t props);
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
269 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
271 Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
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 stateInit the initial state of the Resource
280 Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
283 * @brief Resource destructor
288 * @brief Get the Model of the current Resource
290 * @return The Model of the current Resource
295 * @brief Get the name of the current Resource
297 * @return The name of the current Resource
299 const char *getName();
302 * @brief Get the properties of the current Resource
304 * @return The properties of the current Resource
306 virtual xbt_dict_t getProperties();
309 * @brief Update the state of the current Resource
312 * @param event_type [TODO]
313 * @param value [TODO]
316 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
319 * @brief Check if the current Resource is used
320 * @return true if the current Resource is used, false otherwise
322 virtual bool isUsed()=0;
325 * @brief Check if the current Resource is active
327 * @return true if the current Resource is active, false otherwise
332 * @brief Turn on the current Resource
337 * @brief Turn off the current Resource
342 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
344 * @return The state of the currenrt Resource
346 virtual e_surf_resource_state_t getState();
349 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
351 * @param state The new state of the current Resource
353 virtual void setState(e_surf_resource_state_t state);
357 xbt_dict_t p_properties;
360 e_surf_resource_state_t m_stateCurrent;
365 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
367 * @return The lmm constraint associated to this Resource
369 lmm_constraint_t getConstraint();
371 lmm_constraint_t p_constraint;
377 void surf_action_lmm_update_index_heap(void *action, int i);
379 /** @ingroup SURF_interface
380 * @brief SURF action interface class
381 * @details An action is an event generated by a resource (e.g.: a communication for the network)
383 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
386 * @brief Common initializations for the constructors
388 void initialize(Model *model, double cost, bool failed,
389 lmm_variable_t var = NULL);
393 * @brief Action constructor
395 * @param model The Model associated to this Action
396 * @param cost The cost of the Action
397 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
399 Action(Model *model, double cost, bool failed);
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)
407 * @param var The lmm variable associated to this Action if it is part of a LMM component
409 Action(Model *model, double cost, bool failed, lmm_variable_t var);
412 * @brief Action destructor
417 * @brief Finish the action
422 * @brief Get the [state](\ref e_surf_action_state_t) of the current Action
424 * @return The state of the current Action
426 e_surf_action_state_t getState(); /**< get the state*/
429 * @brief Set the [state](\ref e_surf_action_state_t) of the current Action
431 * @param state The new state of the current Action
433 virtual void setState(e_surf_action_state_t state);
436 * @brief Get the bound of the current Action
438 * @return The bound of the current Action
443 * @brief Set the bound of the current Action
445 * @param bound The new bound of the current Action
447 void setBound(double bound);
450 * @brief Get the start time of the current action
452 * @return The start time of the current action
454 double getStartTime();
457 * @brief Get the finish time of the current action
459 * @return The finish time of the current action
461 double getFinishTime();
464 * @brief Get the data associated to the current action
466 * @return The data associated to the current action
468 void *getData() {return p_data;}
471 * @brief Set the data associated to the current action
473 * @param data The new data associated to the current action
475 void setData(void* data);
478 * @brief Get the maximum duration of the current action
480 * @return The maximum duration of the current action
482 double getMaxDuration() {return m_maxDuration;}
485 * @brief Get the category associated to the current action
487 * @return The category associated to the current action
489 char *getCategory() {return p_category;}
492 * @brief Get the cost of the current action
494 * @return The cost of the current action
496 double getCost() {return m_cost;}
499 * @brief Set the cost of the current action
501 * @param cost The new cost of the current action
503 void setCost(double cost) {m_cost = cost;}
506 * @brief Update the maximum duration of the current action
508 * @param delta [TODO]
510 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
513 * @brief Update the remaining time of the current action
515 * @param delta [TODO]
517 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
520 * @brief Set the remaining time of the current action
522 * @param value The new remaining time of the current action
524 void setRemains(double value) {m_remains = value;}
527 * @brief Set the finish time of the current action
529 * @param value The new Finush time of the current action
531 void setFinishTime(double value) {m_finish = value;}
534 * @brief Add a reference to the current action
539 * @brief Remove a reference to the current action
540 * @details If the Action has no more reference, we destroy it
542 * @return true if the action was destroyed and false if someone still has references on it
547 * @brief Cancel the current Action if running
549 virtual void cancel();
552 * @brief Recycle an Action
554 virtual void recycle(){};
557 * @brief Suspend the current Action
559 virtual void suspend();
562 * @brief Resume the current Action
564 virtual void resume();
567 * @brief Check if the current action is running
569 * @return true if the current Action is suspended, false otherwise
571 virtual bool isSuspended();
574 * @brief Set the maximum duration of the current Action
576 * @param duration The new maximum duration of the current Action
578 virtual void setMaxDuration(double duration);
581 * @brief Set the priority of the current Action
583 * @param priority The new priority of the current Action
585 virtual void setPriority(double priority);
588 * @brief Set the category of the current Action
590 * @param category The new category of the current Action
592 void setCategory(const char *category);
595 * @brief Get the remaining time of the current action after updating the resource
597 * @return The remaining time
599 virtual double getRemains();
602 * @brief Get the remaining time of the current action without updating the resource
604 * @return The remaining time
606 double getRemainsNoUpdate();
609 * @brief Get the priority of the current Action
611 * @return The priority of the current Action
613 double getPriority() {return m_priority;};
616 * @brief Get the state set in which the action is
618 * @return The state set in which the action is
620 ActionListPtr getStateSet() {return p_stateSet;};
622 s_xbt_swag_hookup_t p_stateHookup;
624 Model *getModel() {return p_model;}
627 ActionListPtr p_stateSet;
628 double m_priority; /**< priority (1.0 by default) */
630 double m_remains; /**< How much of that cost remains to be done in the currently running task */
631 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
632 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
636 double m_start; /**< start time */
637 char *p_category; /**< tracing category for categorized resource utilization monitoring */
639 #ifdef HAVE_LATENCY_BOUND_TRACKING
640 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
644 void *p_data; /**< for your convenience */
648 virtual void updateRemainingLazy(double now);
649 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
650 void heapRemove(xbt_heap_t heap);
651 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
652 void updateIndexHeap(int i);
653 lmm_variable_t getVariable() {return p_variable;}
654 double getLastUpdate() {return m_lastUpdate;}
655 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
656 enum heap_action_type getHat() {return m_hat;}
657 bool is_linked() {return actionLmmHook::is_linked();}
661 lmm_variable_t p_variable;
666 enum heap_action_type m_hat;
669 #endif /* SURF_MODEL_H_ */