Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6deea66080f939835c81cc8006e036122d5d3fec
[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 extern double sg_sender_gap;
47 XBT_PUBLIC(int)  SURF_CPU_LEVEL;    //Surf cpu level
48
49 /***********
50  * Classes *
51  ***********/
52 class Model;
53 typedef Model* ModelPtr;
54
55 class Resource;
56 typedef Resource* ResourcePtr;
57 typedef boost::function<void (ResourcePtr r)> ResourceCallback;
58                         
59 class Action;
60 typedef Action* ActionPtr;
61 typedef boost::function<void (ActionPtr a)> ActionCallback;
62
63 class ActionLmm;
64 typedef ActionLmm* ActionLmmPtr;
65
66 enum heap_action_type{
67   LATENCY = 100,
68   MAX_DURATION,
69   NORMAL,
70   NOTSET
71 };
72
73 typedef enum {
74   UM_FULL,
75   UM_LAZY,
76   UM_UNDEFINED
77 } e_UM_t;
78
79 /*********
80  * Trace *
81  *********/
82 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
83 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
84 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
85 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
86 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail; 
87 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth; 
88 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
89
90
91 /*********
92  * Model *
93  *********/
94 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
95
96 class Model {
97 public:
98   Model(string name) {
99     m_name = name;
100     m_resOnCB = m_resOffCB= 0;
101     m_actSuspendCB = m_actCancelCB = m_actResumeCB = 0;
102   }
103   virtual ~Model() {
104     xbt_swag_free(p_readyActionSet);
105     xbt_swag_free(p_runningActionSet);
106     xbt_swag_free(p_failedActionSet);
107     xbt_swag_free(p_doneActionSet);
108   }
109   ResourcePtr createResource(string name);
110   ActionPtr createAction(double _cost, bool _failed);
111   double (Model::*shareResources)(double now);
112   double shareResourcesLazy(double now);
113   double shareResourcesFull(xbt_swag_t running_actions,
114                                       size_t offset,
115                                       lmm_system_t sys,
116                                       void (*solve) (lmm_system_t));
117   void (Model::*updateActionsState)(double now, double delta);
118   void updateActionsStateLazy(double now, double delta);
119   void updateActionsStateFull(double now, double delta);
120
121   string getName() {return m_name;};
122   void gapRemove(ActionLmmPtr action);
123
124   void addTurnedOnCallback(ResourceCallback rc);
125   void notifyResourceTurnedOn(ResourcePtr r);
126
127   void addTurnedOffCallback(ResourceCallback rc);  
128   void notifyResourceTurnedOff(ResourcePtr r);
129
130   void addActionCancelCallback(ActionCallback ac);
131   void notifyActionCancel(ActionPtr a);
132   void addActionResumeCallback(ActionCallback ac);
133   void notifyActionResume(ActionPtr a);
134   void addActionSuspendCallback(ActionCallback ac);  
135   void notifyActionSuspend(ActionPtr a);
136
137   lmm_system_t p_maxminSystem;
138   e_UM_t p_updateMechanism;
139   xbt_swag_t p_modifiedSet;
140   xbt_heap_t p_actionHeap;
141   int m_selectiveUpdate;
142
143   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
144   xbt_swag_t p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
145   xbt_swag_t p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
146   xbt_swag_t p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
147
148 protected:
149   std::vector<ActionPtr> m_failedActions, m_runningActions;
150
151 private:
152   string m_name;
153   ResourceCallback m_resOnCB, m_resOffCB;
154   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
155 };
156
157 /************
158  * Resource *
159  ************/
160
161 /**
162  * Resource which have a metric handled by a maxmin system
163  */
164 typedef struct {
165   double scale;
166   double peak;
167   tmgr_trace_event_t event;
168 } s_surf_metric_t;
169
170 class Resource {
171 public:
172   Resource() {};
173   Resource(ModelPtr model, const char *name, xbt_dict_t properties):
174           m_name(name),m_running(true),p_model(model),m_properties(properties) {};
175   virtual ~Resource() {};
176
177   void updateState(tmgr_trace_event_t event_type, double value, double date);
178
179   //private
180   bool isUsed();
181   //TODOupdateActionState();
182   //TODOupdateResourceState();
183   //TODOfinilize();
184
185   bool isOn();
186   void turnOn();
187   void turnOff();
188   void setName(string name);
189   string getName();
190   ModelPtr getModel() {return p_model;};
191   e_surf_resource_state_t getState();
192   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
193   void *p_resource;
194   e_surf_resource_state_t m_stateCurrent;
195   const char *m_name;
196
197 protected:
198   ModelPtr p_model;
199   xbt_dict_t m_properties;
200
201 private:
202   bool m_running;  
203 };
204
205 class ResourceLmm: virtual public Resource {
206 public:
207   ResourceLmm() {};
208   lmm_constraint_t p_constraint;
209   e_surf_resource_state_t p_stateCurrent;
210   tmgr_trace_event_t p_stateEvent;
211   s_surf_metric_t p_power;
212 };
213
214 static inline void *surf_cpu_resource_priv(const void *host) {
215   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_CPU_LEVEL);
216 }
217 /*static inline void *surf_workstation_resource_priv(const void *host){
218   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_WKS_LEVEL); 
219 }        
220 static inline void *surf_storage_resource_priv(const void *host){
221   return xbt_lib_get_level((xbt_dictelm_t)host, SURF_STORAGE_LEVEL);
222 }*/
223
224 static inline void *surf_cpu_resource_by_name(const char *name) {
225   return xbt_lib_get_elm_or_null(host_lib, name);
226 }
227 /*static inline void *surf_workstation_resource_by_name(const char *name){
228   return xbt_lib_get_elm_or_null(host_lib, name);
229 }
230 static inline void *surf_storage_resource_by_name(const char *name){
231   return xbt_lib_get_elm_or_null(storage_lib, name);*/
232
233 /**********
234  * Action *
235  **********/
236
237 /** \ingroup SURF_actions
238  *  \brief Action states
239  *
240  *  Action states.
241  *
242  *  \see surf_action_t, surf_action_state_t
243  */
244 extern const char *surf_action_state_names[6];
245
246 typedef enum {
247   SURF_ACTION_READY = 0,        /**< Ready        */
248   SURF_ACTION_RUNNING,          /**< Running      */
249   SURF_ACTION_FAILED,           /**< Task Failure */
250   SURF_ACTION_DONE,             /**< Completed    */
251   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
252   SURF_ACTION_NOT_IN_THE_SYSTEM
253                                 /**< Not in the system anymore. Why did you ask ? */
254 } e_surf_action_state_t;
255
256 class Action {
257 public:
258   Action() {};
259   Action(ModelPtr model, double cost, bool failed):
260          m_cost(cost), p_model(model), m_failed(failed),
261          m_refcount(1), m_priority(1.0), m_maxDuration(NO_MAX_DURATION),
262          m_start(surf_get_clock()), m_finish(-1.0)
263   {
264     m_priority = m_start = m_finish = m_maxDuration = -1.0;
265     m_start = 10;//surf_get_clock();
266     m_suspended = false;
267   };
268   virtual ~Action() {};
269   
270   s_xbt_swag_hookup_t p_stateHookup;
271   e_surf_action_state_t getState(); /**< get the state*/
272   void setState(e_surf_action_state_t state); /**< Change state*/
273   double getStartTime(); /**< Return the start time of an action */
274   double getFinishTime(); /**< Return the finish time of an action */
275   void setData(void* data);
276
277   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. */
278   virtual void cancel()=0;     /**< Cancel a running action */
279   virtual void recycle()=0;     /**< Recycle an action */
280   
281   void suspend();     /**< Suspend an action */
282   void resume();     /**< Resume a suspended action */
283   bool isSuspended();     /**< Return whether an action is suspended */
284   void setMaxDuration(double duration);     /**< Set the max duration of an action*/
285   void setPriority(double priority);     /**< Set the priority of an action */
286 #ifdef HAVE_TRACING
287   void setCategory(const char *category); /**< Set the category of an action */
288 #endif
289   double getRemains();     /**< Get the remains of an action */
290 #ifdef HAVE_LATENCY_BOUND_TRACKING
291   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
292 #endif
293
294   xbt_swag_t p_stateSet;
295
296   double m_priority; /**< priority (1.0 by default) */
297   bool m_failed;
298   bool m_suspended;  
299   double m_start; /**< start time  */
300   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
301   double m_remains; /**< How much of that cost remains to be done in the currently running task */
302   #ifdef HAVE_LATENCY_BOUND_TRACKING
303   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
304   #endif
305   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
306   char *p_category;               /**< tracing category for categorized resource utilization monitoring */  
307 protected:
308   ModelPtr p_model;  
309   int    m_cost;
310   int    m_refcount;
311   void *p_data; /**< for your convenience */
312 #ifdef HAVE_TRACING
313 #endif
314   e_UM_t p_updateMechanism;
315
316 private:
317   int resourceUsed(void *resource_id);
318   /* Share the resources to the actions and return in how much time
319      the next action may terminate */
320   double shareResources(double now);
321   /* Update the actions' state */
322   void updateActionsState(double now, double delta);
323   void updateResourceState(void *id, tmgr_trace_event_t event_type,
324                                  double value, double time);
325   void finalize(void);
326
327   lmm_system_t p_maxminSystem;
328   xbt_swag_t p_modifiedSet;
329   xbt_heap_t p_actionHeap;
330   int m_selectiveUpdate;
331 };
332
333 void surf_action_lmm_update_index_heap(void *action, int i);
334
335 class ActionLmm: virtual public Action {
336 public:
337   ActionLmm() {};       
338   ActionLmm(ModelPtr model, double cost, bool failed) {};
339
340   virtual void updateRemainingLazy(double now)=0;
341   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
342   void heapRemove(xbt_heap_t heap);
343   double getRemains();     /**< Get the remains of an action */
344   void updateIndexHeap(int i);
345
346
347   lmm_variable_t p_variable;
348   //bool m_suspended;
349   s_xbt_swag_hookup_t p_actionListHookup;
350   int m_indexHeap;
351   double m_lastUpdate;
352   double m_lastValue;
353   enum heap_action_type m_hat;
354 };
355
356 #endif /* SURF_MODEL_H_ */