Logo AND Algorithmique Numérique Distribuée

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