Logo AND Algorithmique Numérique Distribuée

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