Logo AND Algorithmique Numérique Distribuée

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