Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
89ed8efe55a14b1b44648fab1c29f93da66b9731
[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 #include "surf/surf.h"
19 #include "surf/surf_private.h"
20
21 extern tmgr_history_t history;
22 #define NO_MAX_DURATION -1.0
23
24 using namespace std;
25
26 /** \ingroup SURF_simulation
27  *  \brief Return the current time
28  *
29  *  Return the current time in millisecond.
30  */
31
32 /*********
33  * Utils *
34  *********/
35
36 /* user-visible parameters */
37 extern double sg_tcp_gamma;
38 extern double sg_sender_gap;
39 extern double sg_latency_factor;
40 extern double sg_bandwidth_factor;
41 extern double sg_weight_S_parameter;
42 extern int sg_network_crosstraffic;
43 #ifdef HAVE_GTNETS
44 extern double sg_gtnets_jitter;
45 extern int sg_gtnets_jitter_seed;
46 #endif
47 extern xbt_dynar_t surf_path;
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 XBT_PUBLIC(double) surf_get_clock(void);
53 XBT_PUBLIC(void) surf_watched_hosts(void);
54 #ifdef __cplusplus
55 }
56 #endif
57
58 extern double sg_sender_gap;
59 XBT_PUBLIC(int)  SURF_CPU_LEVEL;    //Surf cpu level
60
61 int __surf_is_absolute_file_path(const char *file_path);
62
63 /***********
64  * Classes *
65  ***********/
66 //class Model;
67 typedef Model* ModelPtr;
68
69 //class Resource;
70 typedef Resource* ResourcePtr;
71 typedef boost::function<void (ResourcePtr r)> ResourceCallback;
72                         
73 //class Action;
74 typedef Action* ActionPtr;
75 typedef boost::function<void (ActionPtr a)> ActionCallback;
76
77 //class ActionLmm;
78 typedef ActionLmm* ActionLmmPtr;
79
80 enum heap_action_type{
81   LATENCY = 100,
82   MAX_DURATION,
83   NORMAL,
84   NOTSET
85 };
86
87 /*********
88  * Trace *
89  *********/
90 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
91 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
92 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
94 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail; 
95 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth; 
96 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
97
98
99 /*********
100  * Model *
101  *********/
102 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
103
104 class Model {
105 public:
106   Model(string name);
107   virtual ~Model();
108
109   ResourcePtr createResource(string name);
110   ActionPtr createAction(double _cost, bool _failed);
111   virtual double shareResources(double now);
112   virtual double shareResourcesLazy(double now);
113   virtual double shareResourcesFull(double now);
114   double shareResourcesMaxMin(xbt_swag_t running_actions,
115                                       size_t offset,
116                                       lmm_system_t sys,
117                                       void (*solve) (lmm_system_t));
118   virtual void updateActionsState(double now, double delta);
119   virtual void updateActionsStateLazy(double now, double delta);
120   virtual void updateActionsStateFull(double now, double delta);
121
122   string getName() {return m_name;};
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   string m_name;
148
149 protected:
150   std::vector<ActionPtr> m_failedActions, m_runningActions;
151
152 private:
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   virtual ~Resource() {};
175
176   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
177
178   //private
179   virtual bool isUsed()=0;
180   //FIXME:updateActionState();
181   //FIXME:updateResourceState();
182   //FIXME:finilize();
183
184   bool isOn();
185   void turnOn();
186   void turnOff();
187   void setName(string name);
188   const char *getName();
189   virtual xbt_dict_t getProperties();
190
191   ModelPtr getModel() {return p_model;};
192   virtual e_surf_resource_state_t getState();
193   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
194   void *p_resource;
195   const char *m_name;
196   xbt_dict_t m_properties;
197   ModelPtr p_model;
198   e_surf_resource_state_t p_stateCurrent;
199
200 protected:
201
202 private:
203   bool m_running;  
204 };
205
206 class ResourceLmm: virtual public Resource {
207 public:
208   ResourceLmm() {};
209   ResourceLmm(surf_model_t model, const char *name, xbt_dict_t props,
210                            lmm_system_t system,
211                            double constraint_value,
212                            tmgr_history_t history,
213                            e_surf_resource_state_t state_init,
214                            tmgr_trace_t state_trace,
215                            double metric_peak,
216                            tmgr_trace_t metric_trace);
217   lmm_constraint_t p_constraint;
218   tmgr_trace_event_t p_stateEvent;
219   s_surf_metric_t p_power;
220 };
221
222 /**********
223  * Action *
224  **********/
225
226 class Action {
227 public:
228   Action();
229   Action(ModelPtr model, double cost, bool failed);
230   virtual ~Action();
231   
232   s_xbt_swag_hookup_t p_stateHookup;
233
234   e_surf_action_state_t getState(); /**< get the state*/
235   void setState(e_surf_action_state_t state); /**< Change state*/
236   double getStartTime(); /**< Return the start time of an action */
237   double getFinishTime(); /**< Return the finish time of an action */
238   void setData(void* data);
239
240   void ref();
241   virtual int unref();     /**< 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. */
242   virtual void cancel();     /**< Cancel a running action */
243   virtual void recycle();     /**< Recycle an action */
244   
245   virtual void suspend()=0;     /**< Suspend an action */
246   virtual void resume()=0;     /**< Resume a suspended action */
247   virtual bool isSuspended()=0;     /**< Return whether an action is suspended */
248   virtual void setMaxDuration(double duration)=0;     /**< Set the max duration of an action*/
249   virtual void setPriority(double priority)=0;     /**< Set the priority of an action */
250 #ifdef HAVE_TRACING
251   void setCategory(const char *category); /**< Set the category of an action */
252 #endif
253   virtual double getRemains();     /**< Get the remains of an action */
254 #ifdef HAVE_LATENCY_BOUND_TRACKING
255   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
256 #endif
257
258   xbt_swag_t p_stateSet;
259
260   double m_priority; /**< priority (1.0 by default) */
261   bool m_failed;
262   double m_start; /**< start time  */
263   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
264   double m_remains; /**< How much of that cost remains to be done in the currently running task */
265   #ifdef HAVE_LATENCY_BOUND_TRACKING
266   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
267   #endif
268   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
269   char *p_category;               /**< tracing category for categorized resource utilization monitoring */  
270   int    m_cost;
271   void *p_data; /**< for your convenience */
272 protected:
273   ModelPtr p_model;  
274   int    m_refcount;
275 #ifdef HAVE_TRACING
276 #endif
277   e_UM_t p_updateMechanism;
278
279 private:
280   int resourceUsed(void *resource_id);
281   /* Share the resources to the actions and return in how much time
282      the next action may terminate */
283   double shareResources(double now);
284   /* Update the actions' state */
285   void updateActionsState(double now, double delta);
286   void updateResourceState(void *id, tmgr_trace_event_t event_type,
287                                  double value, double time);
288
289   lmm_system_t p_maxminSystem;
290   xbt_swag_t p_modifiedSet;
291   xbt_heap_t p_actionHeap;
292   int m_selectiveUpdate;
293 };
294
295 //FIXME:REMOVE
296 void surf_action_lmm_update_index_heap(void *action, int i);
297
298 class ActionLmm: virtual public Action {
299 public:
300   ActionLmm() : m_suspended(false) {
301         p_actionListHookup.prev = 0;
302         p_actionListHookup.next = 0;
303   };
304   ActionLmm(ModelPtr model, double cost, bool failed) : m_suspended(false) {
305         p_actionListHookup.prev = 0;
306         p_actionListHookup.next = 0;
307   };
308
309   virtual void updateRemainingLazy(double now);
310   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
311   void heapRemove(xbt_heap_t heap);
312   double getRemains();     /**< Get the remains of an action */
313   void updateIndexHeap(int i);
314
315   virtual int unref();
316   void cancel();
317   void suspend();
318   void resume();
319   bool isSuspended();
320   void setMaxDuration(double duration);
321   void setPriority(double priority);
322   void gapRemove();
323
324   lmm_variable_t p_variable;
325   s_xbt_swag_hookup_t p_actionListHookup;
326   int m_indexHeap;
327   double m_lastUpdate;
328   double m_lastValue;
329   enum heap_action_type m_hat;
330   int m_suspended;
331 };
332
333 #endif /* SURF_MODEL_H_ */