Logo AND Algorithmique Numérique Distribuée

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