Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
CpuTi in c++
[simgrid.git] / src / surf / surf.hpp
1 //using namespace generic;
2
3 #ifndef SURF_MODEL_H_
4 #define SURF_MODEL_H_
5
6 #include <xbt.h>
7 #include <string>
8 #include <vector>
9 #include <memory>
10 #include <boost/smart_ptr.hpp>
11 #include <boost/function.hpp>
12 #include <boost/functional/factory.hpp>
13 #include <boost/bind.hpp>
14 #include "surf/trace_mgr.h"
15 #include "xbt/lib.h"
16 #include "surf/surf_routing.h"
17 #include "simgrid/platf_interface.h"
18
19 extern tmgr_history_t history;
20 #define NO_MAX_DURATION -1.0
21
22 using namespace std;
23
24 // TODO: put in surf_private.hpp
25 extern xbt_dict_t watched_hosts_lib;
26
27 /** \ingroup SURF_simulation
28  *  \brief Return the current time
29  *
30  *  Return the current time in millisecond.
31  */
32
33 /*********
34  * Utils *
35  *********/
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 XBT_PUBLIC(double) surf_get_clock(void);
41 XBT_PUBLIC(void) surf_watched_hosts(void);
42 #ifdef __cplusplus
43 }
44 #endif
45
46 XBT_PUBLIC(int)  SURF_CPU_LEVEL;    //Surf cpu level
47
48 /***********
49  * Classes *
50  ***********/
51 class Model;
52 typedef Model* ModelPtr;
53
54 class Resource;
55 typedef Resource* ResourcePtr;
56 typedef boost::function<void (ResourcePtr r)> ResourceCallback;
57                         
58 class Action;
59 typedef Action* ActionPtr;
60 typedef boost::function<void (ActionPtr a)> ActionCallback;
61
62 /*********
63  * Trace *
64  *********/
65 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
66 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
67 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
68 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
69 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail; 
70 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth; 
71 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
72
73
74 /*********
75  * Model *
76  *********/
77 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
78
79 class Model {
80 public:
81   Model(string name) {
82     m_name = name;
83     m_resOnCB = m_resOffCB= 0;
84     m_actSuspendCB = m_actCancelCB = m_actResumeCB = 0;
85   }
86   virtual ~Model() {
87     xbt_swag_free(p_readyActionSet);
88     xbt_swag_free(p_runningActionSet);
89     xbt_swag_free(p_failedActionSet);
90     xbt_swag_free(p_doneActionSet);
91   }
92   ResourcePtr createResource(string name);
93   ActionPtr createAction(double _cost, bool _failed);
94   double shareResources(double now);
95   void updateActionsState(double now, double delta);
96
97   string getName() {return m_name;};
98
99   void addTurnedOnCallback(ResourceCallback rc);
100   void notifyResourceTurnedOn(ResourcePtr r);
101
102   void addTurnedOffCallback(ResourceCallback rc);  
103   void notifyResourceTurnedOff(ResourcePtr r);
104
105   void addActionCancelCallback(ActionCallback ac);
106   void notifyActionCancel(ActionPtr a);
107   void addActionResumeCallback(ActionCallback ac);
108   void notifyActionResume(ActionPtr a);
109   void addActionSuspendCallback(ActionCallback ac);  
110   void notifyActionSuspend(ActionPtr a);
111
112   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
113   xbt_swag_t p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
114   xbt_swag_t p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
115   xbt_swag_t p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
116
117 protected:
118   std::vector<ActionPtr> m_failedActions, m_runningActions;
119
120 private:
121   string m_name;
122   ResourceCallback m_resOnCB, m_resOffCB;
123   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
124 };
125
126 /************
127  * Resource *
128  ************/
129 class Resource {
130 public:
131   Resource(ModelPtr model, const char *name, xbt_dict_t properties):
132           m_name(name),m_running(true),p_model(model),m_properties(properties) {};
133   virtual ~Resource() {};
134
135   void updateState(tmgr_trace_event_t event_type, double value, double date);
136
137   //private
138   bool isUsed();
139   //TODOupdateActionState();
140   //TODOupdateResourceState();
141   //TODOfinilize();
142
143   bool isOn();
144   void turnOn();
145   void turnOff();
146   void setName(string name);
147   string getName();
148   ModelPtr getModel() {return p_model;};
149   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
150   void *p_resource;
151   e_surf_resource_state_t m_stateCurrent;
152
153 protected:
154   ModelPtr p_model;
155   const char *m_name;
156   xbt_dict_t m_properties;
157
158 private:
159   bool m_running;  
160 };
161
162 static inline void *surf_cpu_resource_priv(const void *host) {
163   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_CPU_LEVEL);
164 }
165 /*static inline void *surf_workstation_resource_priv(const void *host){
166   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_WKS_LEVEL); 
167 }        
168 static inline void *surf_storage_resource_priv(const void *host){
169   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_STORAGE_LEVEL);
170 }*/
171
172 static inline void *surf_cpu_resource_by_name(const char *name) {
173   return xbt_lib_get_elm_or_null(host_lib, name);
174 }
175 /*static inline void *surf_workstation_resource_by_name(const char *name){
176   return xbt_lib_get_elm_or_null(host_lib, name);
177 }
178 static inline void *surf_storage_resource_by_name(const char *name){
179   return xbt_lib_get_elm_or_null(storage_lib, name);*/
180
181 /**********
182  * Action *
183  **********/
184
185 /** \ingroup SURF_actions
186  *  \brief Action states
187  *
188  *  Action states.
189  *
190  *  \see surf_action_t, surf_action_state_t
191  */
192 extern const char *surf_action_state_names[6];
193
194 typedef enum {
195   SURF_ACTION_READY = 0,        /**< Ready        */
196   SURF_ACTION_RUNNING,          /**< Running      */
197   SURF_ACTION_FAILED,           /**< Task Failure */
198   SURF_ACTION_DONE,             /**< Completed    */
199   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
200   SURF_ACTION_NOT_IN_THE_SYSTEM
201                                 /**< Not in the system anymore. Why did you ask ? */
202 } e_surf_action_state_t;
203
204 typedef enum {
205   UM_FULL,
206   UM_LAZY,
207   UM_UNDEFINED
208 } e_UM_t;
209
210 class Action {
211 public:
212   Action(ModelPtr model, double cost, bool failed):
213          m_cost(cost), p_model(model), m_failed(failed),
214          m_refcount(1), m_priority(1.0), m_maxDuration(NO_MAX_DURATION),
215          m_start(surf_get_clock()), m_finish(-1.0)
216   {
217     m_priority = m_start = m_finish = m_maxDuration = -1.0;
218     m_start = 10;//surf_get_clock();
219     m_suspended = false;
220   };
221   virtual ~Action() {};
222   
223   e_surf_action_state_t getState(); /**< get the state*/
224   void setState(e_surf_action_state_t state); /**< Change state*/
225   double getStartTime(); /**< Return the start time of an action */
226   double getFinishTime(); /**< Return the finish time of an action */
227   void setData(void* data);
228
229   virtual int unref()=0;     /**< Specify that we don't use that action anymore. Returns true if the action was destroyed and false if someone still has references on it. */
230   virtual void cancel()=0;     /**< Cancel a running action */
231   virtual void recycle()=0;     /**< Recycle an action */
232   
233   void suspend();     /**< Suspend an action */
234   void resume();     /**< Resume a suspended action */
235   bool isSuspended();     /**< Return whether an action is suspended */
236   void setMaxDuration(double duration);     /**< Set the max duration of an action*/
237   void setPriority(double priority);     /**< Set the priority of an action */
238 #ifdef HAVE_TRACING
239   void setCategory(const char *category); /**< Set the category of an action */
240 #endif
241   double getRemains();     /**< Get the remains of an action */
242 #ifdef HAVE_LATENCY_BOUND_TRACKING
243   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
244 #endif
245   xbt_swag_t p_stateSet;
246
247   double m_priority; /**< priority (1.0 by default) */
248   bool m_failed;
249   bool m_suspended;  
250   double m_start; /**< start time  */
251   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
252   double m_remains; /**< How much of that cost remains to be done in the currently running task */
253   #ifdef HAVE_LATENCY_BOUND_TRACKING
254   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
255   #endif
256   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
257 protected:
258   ModelPtr p_model;  
259   int    m_cost;
260   int    m_refcount;
261   void *p_data; /**< for your convenience */
262 #ifdef HAVE_TRACING
263   char *p_category;               /**< tracing category for categorized resource utilization monitoring */
264 #endif
265
266 private:
267   int resourceUsed(void *resource_id);
268   /* Share the resources to the actions and return in how much time
269      the next action may terminate */
270   double shareResources(double now);
271   /* Update the actions' state */
272   void updateActionsState(double now, double delta);
273   void updateResourceState(void *id, tmgr_trace_event_t event_type,
274                                  double value, double time);
275   void finalize(void);
276
277   lmm_system_t p_maxminSystem;
278   e_UM_t p_updateMechanism;
279   xbt_swag_t p_modifiedSet;
280   xbt_heap_t p_actionHeap;
281   int m_selectiveUpdate;
282 };
283
284 #endif /* SURF_MODEL_H_ */