Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix Network Constant
[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                                       lmm_system_t sys,
116                                       void (*solve) (lmm_system_t));
117   virtual void updateActionsState(double now, double delta);
118   virtual void updateActionsStateLazy(double now, double delta);
119   virtual void updateActionsStateFull(double now, double delta);
120
121   string getName() {return m_name;};
122
123   void addTurnedOnCallback(ResourceCallback rc);
124   void notifyResourceTurnedOn(ResourcePtr r);
125
126   void addTurnedOffCallback(ResourceCallback rc);  
127   void notifyResourceTurnedOff(ResourcePtr r);
128
129   void addActionCancelCallback(ActionCallback ac);
130   void notifyActionCancel(ActionPtr a);
131   void addActionResumeCallback(ActionCallback ac);
132   void notifyActionResume(ActionPtr a);
133   void addActionSuspendCallback(ActionCallback ac);  
134   void notifyActionSuspend(ActionPtr a);
135
136   lmm_system_t p_maxminSystem;
137   e_UM_t p_updateMechanism;
138   xbt_swag_t p_modifiedSet;
139   xbt_heap_t p_actionHeap;
140   int m_selectiveUpdate;
141
142   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
143   xbt_swag_t p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
144   xbt_swag_t p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
145   xbt_swag_t p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
146   string m_name;
147
148 protected:
149   std::vector<ActionPtr> m_failedActions, m_runningActions;
150
151 private:
152   ResourceCallback m_resOnCB, m_resOffCB;
153   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
154 };
155
156 /************
157  * Resource *
158  ************/
159
160 /**
161  * Resource which have a metric handled by a maxmin system
162  */
163 typedef struct {
164   double scale;
165   double peak;
166   tmgr_trace_event_t event;
167 } s_surf_metric_t;
168
169 class Resource {
170 public:
171   Resource();
172   Resource(ModelPtr model, const char *name, xbt_dict_t properties);
173   virtual ~Resource() {};
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   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
193   void *p_resource;
194   const char *m_name;
195   xbt_dict_t m_properties;
196   ModelPtr p_model;
197   e_surf_resource_state_t p_stateCurrent;
198
199 protected:
200
201 private:
202   bool m_running;  
203 };
204
205 class ResourceLmm: virtual public Resource {
206 public:
207   ResourceLmm() {};
208   ResourceLmm(surf_model_t model, const char *name, xbt_dict_t props,
209                            lmm_system_t system,
210                            double constraint_value,
211                            tmgr_history_t history,
212                            e_surf_resource_state_t state_init,
213                            tmgr_trace_t state_trace,
214                            double metric_peak,
215                            tmgr_trace_t metric_trace);
216   lmm_constraint_t p_constraint;
217   tmgr_trace_event_t p_stateEvent;
218   s_surf_metric_t p_power;
219 };
220
221 /**********
222  * Action *
223  **********/
224
225 class Action {
226 public:
227   Action();
228   Action(ModelPtr model, double cost, bool failed);
229   virtual ~Action();
230   
231   s_xbt_swag_hookup_t p_stateHookup;
232
233   e_surf_action_state_t getState(); /**< get the state*/
234   virtual void setState(e_surf_action_state_t state); /**< Change state*/
235   double getStartTime(); /**< Return the start time of an action */
236   double getFinishTime(); /**< Return the finish time of an action */
237   void setData(void* data);
238
239   void ref();
240   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. */
241   virtual void cancel();     /**< Cancel a running action */
242   virtual void recycle();     /**< Recycle an action */
243   
244   virtual void suspend()=0;     /**< Suspend an action */
245   virtual void resume()=0;     /**< Resume a suspended action */
246   virtual bool isSuspended()=0;     /**< Return whether an action is suspended */
247   virtual void setMaxDuration(double duration)=0;     /**< Set the max duration of an action*/
248   virtual void setPriority(double priority)=0;     /**< Set the priority of an action */
249 #ifdef HAVE_TRACING
250   void setCategory(const char *category); /**< Set the category of an action */
251 #endif
252   virtual double getRemains();     /**< Get the remains of an action */
253 #ifdef HAVE_LATENCY_BOUND_TRACKING
254   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
255 #endif
256
257   xbt_swag_t p_stateSet;
258
259   double m_priority; /**< priority (1.0 by default) */
260   bool m_failed;
261   double m_start; /**< start time  */
262   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
263   double m_remains; /**< How much of that cost remains to be done in the currently running task */
264   #ifdef HAVE_LATENCY_BOUND_TRACKING
265   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
266   #endif
267   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
268   char *p_category;               /**< tracing category for categorized resource utilization monitoring */  
269   int    m_cost;
270   void *p_data; /**< for your convenience */
271 protected:
272   ModelPtr p_model;  
273   int    m_refcount;
274 #ifdef HAVE_TRACING
275 #endif
276   e_UM_t p_updateMechanism;
277
278 private:
279   int resourceUsed(void *resource_id);
280   /* Share the resources to the actions and return in how much time
281      the next action may terminate */
282   double shareResources(double now);
283   /* Update the actions' state */
284   void updateActionsState(double now, double delta);
285   void updateResourceState(void *id, tmgr_trace_event_t event_type,
286                                  double value, double time);
287
288   lmm_system_t p_maxminSystem;
289   xbt_swag_t p_modifiedSet;
290   xbt_heap_t p_actionHeap;
291   int m_selectiveUpdate;
292 };
293
294 //FIXME:REMOVE
295 void surf_action_lmm_update_index_heap(void *action, int i);
296
297 class ActionLmm: virtual public Action {
298 public:
299   ActionLmm() : m_suspended(false) {
300         p_actionListHookup.prev = 0;
301         p_actionListHookup.next = 0;
302   };
303   ActionLmm(ModelPtr model, double cost, bool failed) : m_suspended(false) {
304         p_actionListHookup.prev = 0;
305         p_actionListHookup.next = 0;
306   };
307
308   virtual void updateRemainingLazy(double now);
309   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
310   void heapRemove(xbt_heap_t heap);
311   double getRemains();     /**< Get the remains of an action */
312   void updateIndexHeap(int i);
313
314   virtual int unref();
315   void cancel();
316   void suspend();
317   void resume();
318   bool isSuspended();
319   void setMaxDuration(double duration);
320   void setPriority(double priority);
321   void gapRemove();
322
323   lmm_variable_t p_variable;
324   s_xbt_swag_hookup_t p_actionListHookup;
325   int m_indexHeap;
326   double m_lastUpdate;
327   double m_lastValue;
328   enum heap_action_type m_hat;
329   int m_suspended;
330 };
331
332 #endif /* SURF_MODEL_H_ */