Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use SG_{BEGIN,END}_DECL() for what it's designed to serve.
[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 public:
99   Model(string name);
100   virtual ~Model();
101
102   //ResourcePtr createResource(string name);
103   ActionPtr createAction(double _cost, bool _failed);
104   virtual double shareResources(double now);
105   virtual double shareResourcesLazy(double now);
106   virtual double shareResourcesFull(double now);
107   double shareResourcesMaxMin(xbt_swag_t running_actions,
108                                       lmm_system_t sys,
109                                       void (*solve) (lmm_system_t));
110   virtual void updateActionsState(double now, double delta);
111   virtual void updateActionsStateLazy(double now, double delta);
112   virtual void updateActionsStateFull(double now, double delta);
113
114   string getName() {return m_name;};
115
116   void addTurnedOnCallback(ResourceCallback rc);
117   void notifyResourceTurnedOn(ResourcePtr r);
118
119   void addTurnedOffCallback(ResourceCallback rc);  
120   void notifyResourceTurnedOff(ResourcePtr r);
121
122   void addActionCancelCallback(ActionCallback ac);
123   void notifyActionCancel(ActionPtr a);
124   void addActionResumeCallback(ActionCallback ac);
125   void notifyActionResume(ActionPtr a);
126   void addActionSuspendCallback(ActionCallback ac);  
127   void notifyActionSuspend(ActionPtr a);
128
129   lmm_system_t p_maxminSystem;
130   e_UM_t p_updateMechanism;
131   xbt_swag_t p_modifiedSet;
132   xbt_heap_t p_actionHeap;
133   int m_selectiveUpdate;
134
135   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
136   xbt_swag_t p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
137   xbt_swag_t p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
138   xbt_swag_t p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
139   string m_name;
140
141 protected:
142   std::vector<ActionPtr> m_failedActions, m_runningActions;
143
144 private:
145   ResourceCallback m_resOnCB, m_resOffCB;
146   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
147 };
148
149 /************
150  * Resource *
151  ************/
152
153 /**
154  * Resource which have a metric handled by a maxmin system
155  */
156 typedef struct {
157   double scale;
158   double peak;
159   tmgr_trace_event_t event;
160 } s_surf_metric_t;
161
162 class Resource {
163 public:
164   Resource();
165   Resource(ModelPtr model, const char *name, xbt_dict_t properties);
166   virtual ~Resource() {
167         xbt_free((void*)m_name);
168     xbt_dict_free(&m_properties);
169   };
170
171   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
172
173   //private
174   virtual bool isUsed()=0;
175   //FIXME:updateActionState();
176   //FIXME:updateResourceState();
177   //FIXME:finilize();
178
179   bool isOn();
180   void turnOn();
181   void turnOff();
182   void setName(string name);
183   const char *getName();
184   virtual xbt_dict_t getProperties();
185
186   ModelPtr getModel() {return p_model;};
187   virtual e_surf_resource_state_t getState();
188   virtual void setState(e_surf_resource_state_t state);
189   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
190   void *p_resource;
191   const char *m_name;
192   xbt_dict_t m_properties;
193   ModelPtr p_model;
194   e_surf_resource_state_t p_stateCurrent;
195
196 protected:
197
198 private:
199   bool m_running;  
200 };
201
202 class ResourceLmm: virtual public Resource {
203 public:
204   ResourceLmm() : p_constraint(NULL) {
205         p_power.event = NULL;
206   };
207   ResourceLmm(lmm_system_t system,
208               double constraint_value,
209               tmgr_history_t history,
210               e_surf_resource_state_t state_init,
211               tmgr_trace_t state_trace,
212               double metric_peak,
213               tmgr_trace_t metric_trace);
214   ~ResourceLmm() {
215   };
216   lmm_constraint_t p_constraint;
217   tmgr_trace_event_t p_stateEvent;
218   s_surf_metric_t p_power;
219 };
220
221 /**********
222  * Action *
223  **********/
224
225 class Action {
226 public:
227   Action();
228   Action(ModelPtr model, double cost, bool failed);
229   virtual ~Action();
230   
231   s_xbt_swag_hookup_t p_stateHookup;
232
233   e_surf_action_state_t getState(); /**< get the state*/
234   virtual void setState(e_surf_action_state_t state); /**< Change state*/
235   double getStartTime(); /**< Return the start time of an action */
236   double getFinishTime(); /**< Return the finish time of an action */
237   void setData(void* data);
238
239   void ref();
240   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. */
241   virtual void cancel();     /**< Cancel a running action */
242   virtual void recycle();     /**< Recycle an action */
243   
244   virtual void suspend()=0;     /**< Suspend an action */
245   virtual void resume()=0;     /**< Resume a suspended action */
246   virtual bool isSuspended()=0;     /**< Return whether an action is suspended */
247   virtual void setMaxDuration(double duration)=0;     /**< Set the max duration of an action*/
248   virtual void setPriority(double priority)=0;     /**< Set the priority of an action */
249 #ifdef HAVE_TRACING
250   void setCategory(const char *category); /**< Set the category of an action */
251 #endif
252   virtual double getRemains();     /**< Get the remains of an action */
253 #ifdef HAVE_LATENCY_BOUND_TRACKING
254   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
255 #endif
256
257   xbt_swag_t p_stateSet;
258
259   double m_priority; /**< priority (1.0 by default) */
260   double m_bound;              /**< the capping of the CPU use  */
261   bool m_failed;
262   double m_start; /**< start time  */
263   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
264   double m_remains; /**< How much of that cost remains to be done in the currently running task */
265   #ifdef HAVE_LATENCY_BOUND_TRACKING
266   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
267   #endif
268   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */  
269   char *p_category;               /**< tracing category for categorized resource utilization monitoring */  
270   int    m_cost;
271   void *p_data; /**< for your convenience */
272 protected:
273   ModelPtr p_model;  
274   int    m_refcount;
275 #ifdef HAVE_TRACING
276 #endif
277
278 private:
279   int resourceUsed(void *resource_id);
280   /* Share the resources to the actions and return in how much time
281      the next action may terminate */
282   double shareResources(double now);
283   /* Update the actions' state */
284   void updateActionsState(double now, double delta);
285   void updateResourceState(void *id, tmgr_trace_event_t event_type,
286                                  double value, double time);
287
288   xbt_swag_t p_modifiedSet;
289   xbt_heap_t p_actionHeap;
290   int m_selectiveUpdate;
291 };
292
293 //FIXME:REMOVE
294 void surf_action_lmm_update_index_heap(void *action, int i);
295
296 class ActionLmm: virtual public Action {
297 public:
298   ActionLmm() : m_suspended(false) {
299         p_actionListHookup.prev = 0;
300         p_actionListHookup.next = 0;
301         m_lastUpdate = 0;
302         m_lastValue = 0;
303   };
304   ActionLmm(ModelPtr /*model*/, double /*cost*/, bool /*failed*/) : m_suspended(false) {
305         p_actionListHookup.prev = 0;
306         p_actionListHookup.next = 0;
307         m_lastUpdate = 0;
308         m_lastValue = 0;
309   };
310
311   virtual void updateRemainingLazy(double now);
312   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
313   void heapRemove(xbt_heap_t heap);
314   double getRemains();     /**< Get the remains of an action */
315   void updateIndexHeap(int i);
316
317   virtual int unref();
318   void cancel();
319   void suspend();
320   void resume();
321   bool isSuspended();
322   void setMaxDuration(double duration);
323   void setPriority(double priority);
324   void gapRemove();
325
326   lmm_variable_t p_variable;
327   s_xbt_swag_hookup_t p_actionListHookup;
328   int m_indexHeap;
329   double m_lastUpdate;
330   double m_lastValue;
331   enum heap_action_type m_hat;
332   int m_suspended;
333 };
334
335 #endif /* SURF_MODEL_H_ */