Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix network constant issues
[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 <iostream>
10 #include <memory>
11 #include <boost/function.hpp>
12 #include "surf/trace_mgr.h"
13 #include "xbt/lib.h"
14 #include "surf/surf_routing.h"
15 #include "simgrid/platf_interface.h"
16 #include "surf/surf.h"
17 #include "surf/surf_private.h"
18
19 extern tmgr_history_t history;
20 #define NO_MAX_DURATION -1.0
21
22 using namespace std;
23
24 /** \ingroup SURF_simulation
25  *  \brief Return the current time
26  *
27  *  Return the current time in millisecond.
28  */
29
30 /*********
31  * Utils *
32  *********/
33
34 /* user-visible parameters */
35 extern double sg_tcp_gamma;
36 extern double sg_sender_gap;
37 extern double sg_latency_factor;
38 extern double sg_bandwidth_factor;
39 extern double sg_weight_S_parameter;
40 extern int sg_network_crosstraffic;
41 #ifdef HAVE_GTNETS
42 extern double sg_gtnets_jitter;
43 extern int sg_gtnets_jitter_seed;
44 #endif
45 extern xbt_dynar_t surf_path;
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 XBT_PUBLIC(double) surf_get_clock(void);
51 #ifdef __cplusplus
52 }
53 #endif
54
55 extern double sg_sender_gap;
56 XBT_PUBLIC(int)  SURF_CPU_LEVEL;    //Surf cpu level
57
58 int __surf_is_absolute_file_path(const char *file_path);
59
60 /***********
61  * Classes *
62  ***********/
63 //class Model;
64 typedef Model* ModelPtr;
65
66 //class Resource;
67 typedef Resource* ResourcePtr;
68 typedef boost::function<void (ResourcePtr r)> ResourceCallback;
69                         
70 //class Action;
71 typedef Action* ActionPtr;
72 typedef boost::function<void (ActionPtr a)> ActionCallback;
73
74 //class ActionLmm;
75 typedef ActionLmm* ActionLmmPtr;
76
77 enum heap_action_type{
78   LATENCY = 100,
79   MAX_DURATION,
80   NORMAL,
81   NOTSET
82 };
83
84 /*********
85  * Trace *
86  *********/
87 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
88 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
89 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
90 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
91 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail; 
92 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth; 
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
94
95
96 /*********
97  * Model *
98  *********/
99 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
100
101 class Model {
102 public:
103   Model(string name);
104   virtual ~Model();
105
106   ResourcePtr createResource(string name);
107   ActionPtr createAction(double _cost, bool _failed);
108   virtual double shareResources(double now);
109   virtual double shareResourcesLazy(double now);
110   virtual double shareResourcesFull(double now);
111   double shareResourcesMaxMin(xbt_swag_t running_actions,
112                                       lmm_system_t sys,
113                                       void (*solve) (lmm_system_t));
114   virtual void updateActionsState(double now, double delta);
115   virtual void updateActionsStateLazy(double now, double delta);
116   virtual void updateActionsStateFull(double now, double delta);
117
118   string getName() {return m_name;};
119
120   void addTurnedOnCallback(ResourceCallback rc);
121   void notifyResourceTurnedOn(ResourcePtr r);
122
123   void addTurnedOffCallback(ResourceCallback rc);  
124   void notifyResourceTurnedOff(ResourcePtr r);
125
126   void addActionCancelCallback(ActionCallback ac);
127   void notifyActionCancel(ActionPtr a);
128   void addActionResumeCallback(ActionCallback ac);
129   void notifyActionResume(ActionPtr a);
130   void addActionSuspendCallback(ActionCallback ac);  
131   void notifyActionSuspend(ActionPtr a);
132
133   lmm_system_t p_maxminSystem;
134   e_UM_t p_updateMechanism;
135   xbt_swag_t p_modifiedSet;
136   xbt_heap_t p_actionHeap;
137   int m_selectiveUpdate;
138
139   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
140   xbt_swag_t p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
141   xbt_swag_t p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
142   xbt_swag_t p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
143   string m_name;
144
145 protected:
146   std::vector<ActionPtr> m_failedActions, m_runningActions;
147
148 private:
149   ResourceCallback m_resOnCB, m_resOffCB;
150   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
151 };
152
153 /************
154  * Resource *
155  ************/
156
157 /**
158  * Resource which have a metric handled by a maxmin system
159  */
160 typedef struct {
161   double scale;
162   double peak;
163   tmgr_trace_event_t event;
164 } s_surf_metric_t;
165
166 class Resource {
167 public:
168   Resource();
169   Resource(ModelPtr model, const char *name, xbt_dict_t properties);
170   virtual ~Resource() {
171         xbt_free((void*)m_name);
172     xbt_dict_free(&m_properties);
173   };
174
175   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
176
177   //private
178   virtual bool isUsed()=0;
179   //FIXME:updateActionState();
180   //FIXME:updateResourceState();
181   //FIXME:finilize();
182
183   bool isOn();
184   void turnOn();
185   void turnOff();
186   void setName(string name);
187   const char *getName();
188   virtual xbt_dict_t getProperties();
189
190   ModelPtr getModel() {return p_model;};
191   virtual e_surf_resource_state_t getState();
192   virtual void setState(e_surf_resource_state_t state);
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() : p_constraint(NULL) {
209         p_power.event = NULL;
210   };
211   ResourceLmm(surf_model_t model, const char *name, xbt_dict_t props,
212                            lmm_system_t system,
213                            double constraint_value,
214                            tmgr_history_t history,
215                            e_surf_resource_state_t state_init,
216                            tmgr_trace_t state_trace,
217                            double metric_peak,
218                            tmgr_trace_t metric_trace);
219   ~ResourceLmm() {
220   };
221   lmm_constraint_t p_constraint;
222   tmgr_trace_event_t p_stateEvent;
223   s_surf_metric_t p_power;
224 };
225
226 /**********
227  * Action *
228  **********/
229
230 class Action {
231 public:
232   Action();
233   Action(ModelPtr model, double cost, bool failed);
234   virtual ~Action();
235   
236   s_xbt_swag_hookup_t p_stateHookup;
237
238   e_surf_action_state_t getState(); /**< get the state*/
239   virtual void setState(e_surf_action_state_t state); /**< Change state*/
240   double getStartTime(); /**< Return the start time of an action */
241   double getFinishTime(); /**< Return the finish time of an action */
242   void setData(void* data);
243
244   void ref();
245   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. */
246   virtual void cancel();     /**< Cancel a running action */
247   virtual void recycle();     /**< Recycle an action */
248   
249   virtual void suspend()=0;     /**< Suspend an action */
250   virtual void resume()=0;     /**< Resume a suspended action */
251   virtual bool isSuspended()=0;     /**< Return whether an action is suspended */
252   virtual void setMaxDuration(double duration)=0;     /**< Set the max duration of an action*/
253   virtual void setPriority(double priority)=0;     /**< Set the priority of an action */
254 #ifdef HAVE_TRACING
255   void setCategory(const char *category); /**< Set the category of an action */
256 #endif
257   virtual double getRemains();     /**< Get the remains of an action */
258 #ifdef HAVE_LATENCY_BOUND_TRACKING
259   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
260 #endif
261
262   xbt_swag_t p_stateSet;
263
264   double m_priority; /**< priority (1.0 by default) */
265   double m_bound;              /**< the capping of the CPU use  */
266   bool m_failed;
267   double m_start; /**< start time  */
268   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
269   double m_remains; /**< How much of that cost remains to be done in the currently running task */
270   #ifdef HAVE_LATENCY_BOUND_TRACKING
271   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
272   #endif
273   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
274   char *p_category;               /**< tracing category for categorized resource utilization monitoring */  
275   int    m_cost;
276   void *p_data; /**< for your convenience */
277 protected:
278   ModelPtr p_model;  
279   int    m_refcount;
280 #ifdef HAVE_TRACING
281 #endif
282   //FIXME:removee_UM_t p_updateMechanism;
283
284 private:
285   int resourceUsed(void *resource_id);
286   /* Share the resources to the actions and return in how much time
287      the next action may terminate */
288   double shareResources(double now);
289   /* Update the actions' state */
290   void updateActionsState(double now, double delta);
291   void updateResourceState(void *id, tmgr_trace_event_t event_type,
292                                  double value, double time);
293
294   xbt_swag_t p_modifiedSet;
295   xbt_heap_t p_actionHeap;
296   int m_selectiveUpdate;
297 };
298
299 //FIXME:REMOVE
300 void surf_action_lmm_update_index_heap(void *action, int i);
301
302 class ActionLmm: virtual public Action {
303 public:
304   ActionLmm() : m_suspended(false) {
305         p_actionListHookup.prev = 0;
306         p_actionListHookup.next = 0;
307         m_lastUpdate = 0;
308         m_lastValue = 0;
309   };
310   ActionLmm(ModelPtr /*model*/, double /*cost*/, bool /*failed*/) : m_suspended(false) {
311         p_actionListHookup.prev = 0;
312         p_actionListHookup.next = 0;
313         m_lastUpdate = 0;
314         m_lastValue = 0;
315   };
316
317   virtual void updateRemainingLazy(double now);
318   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
319   void heapRemove(xbt_heap_t heap);
320   double getRemains();     /**< Get the remains of an action */
321   void updateIndexHeap(int i);
322
323   virtual int unref();
324   void cancel();
325   void suspend();
326   void resume();
327   bool isSuspended();
328   void setMaxDuration(double duration);
329   void setPriority(double priority);
330   void gapRemove();
331
332   lmm_variable_t p_variable;
333   s_xbt_swag_hookup_t p_actionListHookup;
334   int m_indexHeap;
335   double m_lastUpdate;
336   double m_lastValue;
337   enum heap_action_type m_hat;
338   int m_suspended;
339 };
340
341 #endif /* SURF_MODEL_H_ */