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. */
15 #include <boost/function.hpp>
16 #include <boost/intrusive/list.hpp>
17 #include "surf/trace_mgr.h"
19 #include "surf/surf_routing.h"
20 #include "simgrid/platf_interface.h"
21 #include "surf/surf.h"
22 #include "surf/surf_private.h"
23 #include "internal_config.h"
26 #include <sigc++/sigc++.h>
27 #define surf_callback(arg1, ...) sigc::signal<arg1,__VA_ARGS__>
28 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
29 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
31 #include <boost/signals2.hpp>
32 #define surf_callback(arg1, ...) boost::signals2::signal<arg1(__VA_ARGS__)>
33 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
34 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
37 extern tmgr_history_t history;
38 #define NO_MAX_DURATION -1.0
46 /* user-visible parameters */
47 extern double sg_tcp_gamma;
48 extern double sg_sender_gap;
49 extern double sg_latency_factor;
50 extern double sg_bandwidth_factor;
51 extern double sg_weight_S_parameter;
52 extern int sg_network_crosstraffic;
54 extern double sg_gtnets_jitter;
55 extern int sg_gtnets_jitter_seed;
57 extern xbt_dynar_t surf_path;
60 XBT_PUBLIC(double) surf_get_clock(void);
63 extern double sg_sender_gap;
64 XBT_PUBLIC(int) SURF_CPU_LEVEL; //Surf cpu level
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
120 * @brief Model constructor
122 * @param name the name of the model
124 Model(const char *name);
127 * @brief Model destructor
132 * @brief Get the name of the current Model
134 * @return The name of the current Model
136 const char *getName() {return p_name;}
139 * @brief Get the set of [actions](@ref Action) in *ready* state
141 * @return The set of [actions](@ref Action) in *ready* state
143 virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
146 * @brief Get the set of [actions](@ref Action) in *running* state
148 * @return The set of [actions](@ref Action) in *running* state
150 virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
153 * @brief Get the set of [actions](@ref Action) in *failed* state
155 * @return The set of [actions](@ref Action) in *failed* state
157 virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
160 * @brief Get the set of [actions](@ref Action) in *done* state
162 * @return The set of [actions](@ref Action) in *done* state
164 virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
167 * @brief Get the set of modified [actions](@ref Action)
169 * @return The set of modified [actions](@ref Action)
171 virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
174 * @brief Get the maxmin system of the current Model
176 * @return The maxmin system of the current Model
178 lmm_system_t getMaxminSystem() {return p_maxminSystem;}
181 * @brief Get the update mechanism of the current Model
184 * @return [description]
186 e_UM_t getUpdateMechanism() {return p_updateMechanism;}
189 * @brief Get Action heap
192 * @return The Action heap
194 xbt_heap_t getActionHeap() {return p_actionHeap;}
197 * @brief share the resources
198 * @details Share the resources between the actions
201 * @return the date of the next action will finish
203 virtual double shareResources(double now);
204 virtual double shareResourcesLazy(double now);
205 virtual double shareResourcesFull(double now);
206 double shareResourcesMaxMin(ActionListPtr running_actions,
208 void (*solve) (lmm_system_t));
211 * @brief Update state of actions
215 * @param delta [TODO]
217 virtual void updateActionsState(double now, double delta);
218 virtual void updateActionsStateLazy(double now, double delta);
219 virtual void updateActionsStateFull(double now, double delta);
222 ActionLmmListPtr p_modifiedSet;
223 lmm_system_t p_maxminSystem;
224 e_UM_t p_updateMechanism;
225 int m_selectiveUpdate;
226 xbt_heap_t p_actionHeap;
231 ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
232 ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
233 ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
234 ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
241 /** @ingroup SURF_interface
242 * @brief Resource which have a metric handled by a maxmin system
245 double scale; /**< The scale of the metric */
246 double peak; /**< The peak of the metric */
247 tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
250 /** @ingroup SURF_interface
251 * @brief SURF resource interface class
252 * @details A resource represent an element of a component (e.g.: a link for the network)
257 * @brief Resource constructor
262 * @brief Resource constructor
264 * @param model Model associated to this Resource
265 * @param name The name of the Resource
266 * @param props Dictionary of properties associated to this Resource
268 Resource(ModelPtr model, const char *name, xbt_dict_t props);
271 * @brief Resource constructor
273 * @param model Model associated to this Resource
274 * @param name The name of the Resource
275 * @param props Dictionary of properties associated to this Resource
276 * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
278 Resource(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
281 * @brief Resource constructor
283 * @param model Model associated to this Resource
284 * @param name The name of the Resource
285 * @param props Dictionary of properties associated to this Resource
286 * @param stateInit the initial state of the Resource
288 Resource(ModelPtr model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
291 * @brief Resource destructor
296 * @brief Get the Model of the current Resource
298 * @return The Model of the current Resource
303 * @brief Get the name of the current Resource
305 * @return The name of the current Resource
307 const char *getName();
310 * @brief Get the properties of the current Resource
312 * @return The properties of the current Resource
314 virtual xbt_dict_t getProperties();
317 * @brief Update the state of the current Resource
320 * @param event_type [TODO]
321 * @param value [TODO]
324 virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
327 * @brief Check if the current Resource is used
328 * @return true if the current Resource is used, false otherwise
330 virtual bool isUsed()=0;
333 * @brief Check if the current Resource is active
335 * @return true if the current Resource is active, false otherwise
340 * @brief Turn on the current Resource
345 * @brief Turn off the current Resource
350 * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
352 * @return The state of the currenrt Resource
354 virtual e_surf_resource_state_t getState();
357 * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
359 * @param state The new state of the current Resource
361 virtual void setState(e_surf_resource_state_t state);
364 e_surf_resource_state_t m_stateCurrent;
368 xbt_dict_t p_properties;
376 * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
378 * @return The lmm constraint associated to this Resource
380 lmm_constraint_t getConstraint();
382 lmm_constraint_t p_constraint;
388 void surf_action_lmm_update_index_heap(void *action, int i);
390 /** @ingroup SURF_interface
391 * @brief SURF action interface class
392 * @details An action is an event generated by a resource (e.g.: a communication for the network)
394 class Action : public actionHook, public actionLmmHook {
397 * @brief Action constructor
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 workstation)
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 workstation)
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); /**< Change state*/
445 * @brief Get the bound of the current Action
447 * @return The bound of the current Action
449 double getBound() {return m_bound;}
452 * @brief Get the start time of the current action
454 * @return The start time of the current action
456 double getStartTime();
459 * @brief Get the finish time of the current action
461 * @return The finish time of the current action
463 double getFinishTime();
466 * @brief Get the data associated to the current action
468 * @return The data associated to the current action
470 void *getData() {return p_data;}
473 * @brief Set the data associated to the current action
475 * @param data The new data associated to the current action
477 void setData(void* data);
480 * @brief Get the maximum duration of the current action
482 * @return The maximum duration of the current action
484 double getMaxDuration() {return m_maxDuration;}
487 * @brief Get the category associated to the current action
489 * @return The category associated to the current action
491 char *getCategory() {return p_category;}
494 * @brief Get the cost of the current action
496 * @return The cost of the current action
498 double getCost() {return m_cost;}
501 * @brief Set the cost of the current action
503 * @param cost The new cost of the current action
505 void setCost(double cost) {m_cost = cost;}
508 * @brief Update the maximum duration of the current action
510 * @param delta [TODO]
512 void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta);}
515 * @brief Update the remaining time of the current action
517 * @param delta [TODO]
519 void updateRemains(double delta) {double_update(&m_remains, delta);}
522 * @brief Set the remaining time of the current action
524 * @param value The new remaining time of the current action
526 void setRemains(double value) {m_remains = value;}
529 * @brief Set the finish time of the current action
531 * @param value The new Finush time of the current action
533 void setFinishTime(double value) {m_finish = value;}
536 * @brief Add a reference to the current action
541 * @brief Remove a reference to the current action
542 * @details If the Action has no more reference, we destroy it
544 * @return true if the action was destroyed and false if someone still has references on it
549 * @brief Cancel the current Action if running
551 virtual void cancel();
554 * @brief Recycle an Action
556 virtual void recycle(){};
559 * @brief Suspend the current Action
561 virtual void suspend();
564 * @brief Resume the current Action
566 virtual void resume();
569 * @brief Check if the current action is running
571 * @return true if the current Action is suspended, false otherwise
573 virtual bool isSuspended();
576 * @brief Set the maximum duration of the current Action
578 * @param duration The new maximum duration of the current Action
580 virtual void setMaxDuration(double duration);
583 * @brief Set the priority of the current Action
585 * @param priority The new priority of the current Action
587 virtual void setPriority(double priority);
591 * @brief Set the category of the current Action
593 * @param category The new category of the current Action
595 void setCategory(const char *category);
599 * @brief Get the remaining time of the current action after updating the resource
601 * @return The remaining time
603 virtual double getRemains();
606 * @brief Get the remaining time of the current action without updating the resource
608 * @return The remaining time
610 double getRemainsNoUpdate();
612 #ifdef HAVE_LATENCY_BOUND_TRACKING
614 * @brief Check if the action is limited by latency.
616 * @return 1 if action is limited by latency, 0 otherwise
618 int getLatencyLimited();
622 * @brief Get the priority of the current Action
624 * @return The priority of the current Action
626 double getPriority() {return m_priority;};
629 * @brief Get the state set in which the action is
632 * @return The state set in which the action is
634 ActionListPtr getStateSet() {return p_stateSet;};
636 s_xbt_swag_hookup_t p_stateHookup;
639 ActionListPtr p_stateSet;
640 double m_priority; /**< priority (1.0 by default) */
642 double m_remains; /**< How much of that cost remains to be done in the currently running task */
643 double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
644 double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
646 ModelPtr getModel() {return p_model;}
649 int resourceUsed(void *resource_id);
652 * @brief Share the resources to the actions
656 * @return in how much time the next action may terminatedescription]
658 double shareResources(double now);
661 * @brief Update the current action state
665 * @param delta [TODO]
667 void updateActionsState(double now, double delta);
670 * @brief Update the [TODO]
674 * @param event_type [TODO]
675 * @param value [TODO]
678 void updateResourceState(void *id, tmgr_trace_event_t event_type,
679 double value, double time);
681 ActionLmmListPtr p_modifiedSet;
682 xbt_heap_t p_actionHeap;
683 int m_selectiveUpdate;
684 double m_bound; /**< the capping of the CPU use */
686 double m_start; /**< start time */
687 char *p_category; /**< tracing category for categorized resource utilization monitoring */
689 #ifdef HAVE_LATENCY_BOUND_TRACKING
690 int m_latencyLimited; /**< Set to 1 if is limited by latency, 0 otherwise */
694 void *p_data; /**< for your convenience */
698 virtual void updateRemainingLazy(double now);
699 void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
700 void heapRemove(xbt_heap_t heap);
701 void updateIndexHeap(int i);
702 lmm_variable_t getVariable() {return p_variable;}
703 double getLastUpdate() {return m_lastUpdate;}
704 void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
705 enum heap_action_type getHat() {return m_hat;}
706 bool is_linked() {return actionLmmHook::is_linked();}
710 lmm_variable_t p_variable;
715 enum heap_action_type m_hat;
718 #endif /* SURF_MODEL_H_ */