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;
63 XBT_PUBLIC(int) SURF_CPU_LEVEL; //Surf cpu level
65 extern surf_callback(void, void) surfExitCallbacks;
67 int __surf_is_absolute_file_path(const char *file_path);
73 typedef Model* ModelPtr;
76 typedef Resource* ResourcePtr;
79 typedef Action* ActionPtr;
81 typedef boost::intrusive::list<Action> ActionList;
82 typedef ActionList* ActionListPtr;
83 typedef boost::intrusive::list_base_hook<> actionHook;
86 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
87 typedef ActionLmmList* ActionLmmListPtr;
88 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
91 enum heap_action_type{
101 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
102 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
103 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
104 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
105 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
106 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
107 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
112 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
114 /** @ingroup SURF_interface
115 * @brief SURF model interface class
116 * @details A model is an object which handle the interactions between its Resources and its Actions
118 XBT_PUBLIC_CLASS Model {
121 * @brief Model constructor
123 * @param name the name of the model
125 Model(const char *name);
128 * @brief Model destructor
132 virtual void addTraces() =0;
135 * @brief Get the name of the current Model
137 * @return The name of the current Model
139 const char *getName() {return p_name;}
142 * @brief Get the set of [actions](@ref Action) in *ready* state
144 * @return The set of [actions](@ref Action) in *ready* state
146 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
149 * @brief Get the set of [actions](@ref Action) in *running* state
151 * @return The set of [actions](@ref Action) in *running* state
153 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
156 * @brief Get the set of [actions](@ref Action) in *failed* state
158 * @return The set of [actions](@ref Action) in *failed* state
160 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
163 * @brief Get the set of [actions](@ref Action) in *done* state
165 * @return The set of [actions](@ref Action) in *done* state
167 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
170 * @brief Get the set of modified [actions](@ref Action)
172 * @return The set of modified [actions](@ref Action)
174 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
177 * @brief Get the maxmin system of the current Model
179 * @return The maxmin system of the current Model
181 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
184 * @brief Get the update mechanism of the current Model
187 * @return [description]
189 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
192 * @brief Get Action heap
195 * @return The Action heap
197 xbt_heap_t getActionHeap() {return p_actionHeap;}
200 * @brief share the resources
201 * @details Share the resources between the actions
203 * @param now The current time of the simulation
204 * @return The delta of time till the next action will finish
206 virtual double shareResources(double now);
207 virtual double shareResourcesLazy(double now);
208 virtual double shareResourcesFull(double now);
209 double shareResourcesMaxMin(ActionListPtr running_actions,
211 void (*solve) (lmm_system_t));
214 * @brief Update state of actions
215 * @details Update action to the current time
217 * @param now The current time of the simulation
218 * @param delta The delta of time since the last update
220 virtual void updateActionsState(double now, double delta);
221 virtual void updateActionsStateLazy(double now, double delta);
222 virtual void updateActionsStateFull(double now, double delta);
225 ActionLmmListPtr p_modifiedSet;
226 lmm_system_t p_maxminSystem;
227 e_UM_t p_updateMechanism;
228 int m_selectiveUpdate;
229 xbt_heap_t p_actionHeap;
234 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
235 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
236 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
237 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
244 /** @ingroup SURF_interface
245 * @brief Resource which have a metric handled by a maxmin system
248 double scale; /**< The scale of the metric */
249 double peak; /**< The peak of the metric */
250 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
253 /** @ingroup SURF_interface
254 * @brief SURF resource interface class
255 * @details A resource represent an element of a component (e.g.: a link for the network)
257 XBT_PUBLIC_CLASS Resource {
260 * @brief Resource constructor
265 * @brief Resource constructor
267 * @param model Model associated to this Resource
268 * @param name The name of the Resource
269 * @param props Dictionary of properties associated to this Resource
271 Resource(ModelPtr model, const char *name, xbt_dict_t props);
274 * @brief Resource constructor
276 * @param model Model associated to this Resource
277 * @param name The name of the Resource
278 * @param props Dictionary of properties associated to this Resource
279 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
281 Resource(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
283 * @brief Resource constructor
285 * @param model Model associated to this Resource
286 * @param name The name of the Resource
287 * @param props Dictionary of properties associated to this Resource
288 * @param stateInit the initial state of the Resource
290 Resource(ModelPtr model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
293 * @brief Resource destructor
298 * @brief Get the Model of the current Resource
300 * @return The Model of the current Resource
305 * @brief Get the name of the current Resource
307 * @return The name of the current Resource
309 const char *getName();
312 * @brief Get the properties of the current Resource
314 * @return The properties of the current Resource
316 virtual xbt_dict_t getProperties();
319 * @brief Update the state of the current Resource
322 * @param event_type [TODO]
323 * @param value [TODO]
326 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
329 * @brief Check if the current Resource is used
330 * @return true if the current Resource is used, false otherwise
332 virtual bool isUsed()=0;
335 * @brief Check if the current Resource is active
337 * @return true if the current Resource is active, false otherwise
342 * @brief Turn on the current Resource
347 * @brief Turn off the current Resource
352 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
354 * @return The state of the currenrt Resource
356 virtual e_surf_resource_state_t getState();
359 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
361 * @param state The new state of the current Resource
363 virtual void setState(e_surf_resource_state_t state);
367 xbt_dict_t p_properties;
370 e_surf_resource_state_t m_stateCurrent;
375 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
377 * @return The lmm constraint associated to this Resource
379 lmm_constraint_t getConstraint();
381 lmm_constraint_t p_constraint;
387 void surf_action_lmm_update_index_heap(void *action, int i);
389 /** @ingroup SURF_interface
390 * @brief SURF action interface class
391 * @details An action is an event generated by a resource (e.g.: a communication for the network)
393 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
396 * @brief Common initializations for the constructors
398 void initialize(ModelPtr model, double cost, bool failed,
399 lmm_variable_t var = NULL);
403 * @brief Action constructor
405 * @param model The Model associated to this Action
406 * @param cost The cost of the Action
407 * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
409 Action(ModelPtr model, double cost, bool failed);
412 * @brief Action constructor
414 * @param model The Model associated to this Action
415 * @param cost The cost of the Action
416 * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
417 * @param var The lmm variable associated to this Action if it is part of a LMM component
419 Action(ModelPtr model, double cost, bool failed, lmm_variable_t var);
422 * @brief Action destructor
427 * @brief Finish the action
432 * @brief Get the [state](\ref e_surf_action_state_t) of the current Action
434 * @return The state of the current Action
436 e_surf_action_state_t getState(); /**< get the state*/
439 * @brief Set the [state](\ref e_surf_action_state_t) of the current Action
441 * @param state The new state of the current Action
443 virtual void setState(e_surf_action_state_t state);
446 * @brief Get the bound of the current Action
448 * @return The bound of the current Action
453 * @brief Set the bound of the current Action
455 * @param bound The new bound of the current Action
457 void setBound(double bound);
460 * @brief Get the start time of the current action
462 * @return The start time of the current action
464 double getStartTime();
467 * @brief Get the finish time of the current action
469 * @return The finish time of the current action
471 double getFinishTime();
474 * @brief Get the data associated to the current action
476 * @return The data associated to the current action
478 void *getData() {return p_data;}
481 * @brief Set the data associated to the current action
483 * @param data The new data associated to the current action
485 void setData(void* data);
488 * @brief Get the maximum duration of the current action
490 * @return The maximum duration of the current action
492 double getMaxDuration() {return m_maxDuration;}
495 * @brief Get the category associated to the current action
497 * @return The category associated to the current action
499 char *getCategory() {return p_category;}
502 * @brief Get the cost of the current action
504 * @return The cost of the current action
506 double getCost() {return m_cost;}
509 * @brief Set the cost of the current action
511 * @param cost The new cost of the current action
513 void setCost(double cost) {m_cost = cost;}
516 * @brief Update the maximum duration of the current action
518 * @param delta [TODO]
520 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
523 * @brief Update the remaining time of the current action
525 * @param delta [TODO]
527 void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
530 * @brief Set the remaining time of the current action
532 * @param value The new remaining time of the current action
534 void setRemains(double value) {m_remains = value;}
537 * @brief Set the finish time of the current action
539 * @param value The new Finush time of the current action
541 void setFinishTime(double value) {m_finish = value;}
544 * @brief Add a reference to the current action
549 * @brief Remove a reference to the current action
550 * @details If the Action has no more reference, we destroy it
552 * @return true if the action was destroyed and false if someone still has references on it
557 * @brief Cancel the current Action if running
559 virtual void cancel();
562 * @brief Recycle an Action
564 virtual void recycle(){};
567 * @brief Suspend the current Action
569 virtual void suspend();
572 * @brief Resume the current Action
574 virtual void resume();
577 * @brief Check if the current action is running
579 * @return true if the current Action is suspended, false otherwise
581 virtual bool isSuspended();
584 * @brief Set the maximum duration of the current Action
586 * @param duration The new maximum duration of the current Action
588 virtual void setMaxDuration(double duration);
591 * @brief Set the priority of the current Action
593 * @param priority The new priority of the current Action
595 virtual void setPriority(double priority);
598 * @brief Set the category of the current Action
600 * @param category The new category of the current Action
602 void setCategory(const char *category);
605 * @brief Get the remaining time of the current action after updating the resource
607 * @return The remaining time
609 virtual double getRemains();
612 * @brief Get the remaining time of the current action without updating the resource
614 * @return The remaining time
616 double getRemainsNoUpdate();
619 * @brief Get the priority of the current Action
621 * @return The priority of the current Action
623 double getPriority() {return m_priority;};
626 * @brief Get the state set in which the action is
628 * @return The state set in which the action is
630 ActionListPtr getStateSet() {return p_stateSet;};
632 s_xbt_swag_hookup_t p_stateHookup;
634 ModelPtr getModel() {return p_model;}
637 ActionListPtr p_stateSet;
638 double m_priority; /**< priority (1.0 by default) */
640 double m_remains; /**< How much of that cost remains to be done in the currently running task */
641 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
642 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
646 double m_start; /**< start time */
647 char *p_category; /**< tracing category for categorized resource utilization monitoring */
649 #ifdef HAVE_LATENCY_BOUND_TRACKING
650 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
654 void *p_data; /**< for your convenience */
658 virtual void updateRemainingLazy(double now);
659 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
660 void heapRemove(xbt_heap_t heap);
661 void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
662 void updateIndexHeap(int i);
663 lmm_variable_t getVariable() {return p_variable;}
664 double getLastUpdate() {return m_lastUpdate;}
665 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
666 enum heap_action_type getHat() {return m_hat;}
667 bool is_linked() {return actionLmmHook::is_linked();}
671 lmm_variable_t p_variable;
676 enum heap_action_type m_hat;
679 #endif /* SURF_MODEL_H_ */