Logo AND Algorithmique Numérique Distribuée

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