Logo AND Algorithmique Numérique Distribuée

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