Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into tomerge
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SURF_MODEL_H_
8 #define SURF_MODEL_H_
9
10 #include <xbt.h>
11 #include <string>
12 #include <vector>
13 #include <memory>
14 #include <boost/function.hpp>
15 #include <boost/intrusive/list.hpp>
16 #include "surf/trace_mgr.h"
17 #include "xbt/lib.h"
18 #include "surf/surf_routing.h"
19 #include "simgrid/platf_interface.h"
20 #include "surf/surf.h"
21 #include "surf/surf_private.h"
22 #include "internal_config.h"
23
24 #ifdef LIBSIGC
25 #include <sigc++/sigc++.h>
26 #define surf_callback(arg1, ...)  sigc::signal<arg1,__VA_ARGS__>
27 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
28 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
29 #else
30 #include <boost/signals2.hpp>
31 #define surf_callback(arg1, ...)  boost::signals2::signal<arg1(__VA_ARGS__)>
32 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
33 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
34 #endif
35
36 extern tmgr_history_t history;
37 #define NO_MAX_DURATION -1.0
38
39 using namespace std;
40
41 /*********
42  * Utils *
43  *********/
44
45 /* user-visible parameters */
46 extern double sg_tcp_gamma;
47 extern double sg_sender_gap;
48 extern double sg_latency_factor;
49 extern double sg_bandwidth_factor;
50 extern double sg_weight_S_parameter;
51 extern int sg_network_crosstraffic;
52 #ifdef HAVE_GTNETS
53 extern double sg_gtnets_jitter;
54 extern int sg_gtnets_jitter_seed;
55 #endif
56 extern xbt_dynar_t surf_path;
57
58 extern "C" {
59 XBT_PUBLIC(double) surf_get_clock(void);
60 }
61
62 extern double sg_sender_gap;
63
64 extern surf_callback(void, void) surfExitCallbacks;
65
66 int __surf_is_absolute_file_path(const char *file_path);
67
68 /***********
69  * Classes *
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
81 enum heap_action_type{
82   LATENCY = 100,
83   MAX_DURATION,
84   NORMAL,
85   NOTSET
86 };
87
88 /*********
89  * Trace *
90  *********/
91 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
92 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
94 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
95 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
96 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
97 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
98
99 /*********
100  * Model *
101  *********/
102 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
103
104 /** @ingroup SURF_interface
105  * @brief SURF model interface class
106  * @details A model is an object which handle the interactions between its Resources and its Actions
107  */
108 XBT_PUBLIC_CLASS Model {
109 public:
110   /** @brief Constructor */
111   Model();
112   /** @brief Destructor */
113   virtual ~Model();
114
115   virtual void addTraces() =0;
116
117   /**
118    * @brief Get the set of [actions](@ref Action) in *ready* state
119    *
120    * @return The set of [actions](@ref Action) in *ready* state
121    */
122   virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
123
124   /**
125    * @brief Get the set of [actions](@ref Action) in *running* state
126    *
127    * @return The set of [actions](@ref Action) in *running* state
128    */
129   virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
130
131   /**
132    * @brief Get the set of [actions](@ref Action) in *failed* state
133    *
134    * @return The set of [actions](@ref Action) in *failed* state
135    */
136   virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
137
138   /**
139    * @brief Get the set of [actions](@ref Action) in *done* state
140    *
141    * @return The set of [actions](@ref Action) in *done* state
142    */
143   virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
144
145   /**
146    * @brief Get the set of modified [actions](@ref Action)
147    *
148    * @return The set of modified [actions](@ref Action)
149    */
150   virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
151
152   /**
153    * @brief Get the maxmin system of the current Model
154    *
155    * @return The maxmin system of the current Model
156    */
157   lmm_system_t getMaxminSystem() {return p_maxminSystem;}
158
159   /**
160    * @brief Get the update mechanism of the current Model
161    * @see e_UM_t
162    *
163    * @return [description]
164    */
165   e_UM_t getUpdateMechanism() {return p_updateMechanism;}
166
167   /**
168    * @brief Get Action heap
169    * @details [TODO]
170    *
171    * @return The Action heap
172    */
173   xbt_heap_t getActionHeap() {return p_actionHeap;}
174
175   /**
176    * @brief share the resources
177    * @details Share the resources between the actions
178    *
179    * @param now The current time of the simulation
180    * @return The delta of time till the next action will finish
181    */
182   virtual double shareResources(double now);
183   virtual double shareResourcesLazy(double now);
184   virtual double shareResourcesFull(double now);
185   double shareResourcesMaxMin(ActionListPtr running_actions,
186                                       lmm_system_t sys,
187                                       void (*solve) (lmm_system_t));
188
189   /**
190    * @brief Update state of actions
191    * @details Update action to the current time
192    *
193    * @param now The current time of the simulation
194    * @param delta The delta of time since the last update
195    */
196   virtual void updateActionsState(double now, double delta);
197   virtual void updateActionsStateLazy(double now, double delta);
198   virtual void updateActionsStateFull(double now, double delta);
199
200   /** @brief Returns whether this model have an idempotent shareResource()
201    *
202    * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
203    * so we need to call it only when the next timestamp of other sources is computed.
204    */
205   virtual bool shareResourcesIsIdempotent()=0;
206
207 protected:
208   ActionLmmListPtr p_modifiedSet;
209   lmm_system_t p_maxminSystem;
210   e_UM_t p_updateMechanism;
211   int m_selectiveUpdate;
212   xbt_heap_t p_actionHeap;
213
214 private:
215   ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
216   ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
217   ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
218   ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
219 };
220
221 /************
222  * Resource *
223  ************/
224
225 /** @ingroup SURF_interface
226  * @brief Resource which have a metric handled by a maxmin system
227  */
228 typedef struct {
229   double scale;             /**< The scale of the metric */
230   double peak;              /**< The peak of the metric */
231   tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
232 } s_surf_metric_t;
233
234 /** @ingroup SURF_interface
235  * @brief SURF resource interface class
236  * @details A resource represent an element of a component (e.g.: a link for the network)
237  */
238 XBT_PUBLIC_CLASS Resource {
239 public:
240   /**
241    * @brief Resource constructor
242    */
243   Resource();
244
245   /**
246    * @brief Resource constructor
247    *
248    * @param model Model associated to this Resource
249    * @param name The name of the Resource
250    * @param props Dictionary of properties associated to this Resource
251    */
252   Resource(Model *model, const char *name, xbt_dict_t props);
253
254   /**
255    * @brief Resource constructor
256    *
257    * @param model Model associated to this Resource
258    * @param name The name of the Resource
259    * @param props Dictionary of properties associated to this Resource
260    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
261    */
262   Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
263   /**
264    * @brief Resource constructor
265    *
266    * @param model Model associated to this Resource
267    * @param name The name of the Resource
268    * @param props Dictionary of properties associated to this Resource
269    * @param stateInit the initial state of the Resource
270    */
271   Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
272
273   /**
274    * @brief Resource destructor
275    */
276   virtual ~Resource();
277
278   /**
279    * @brief Get the Model of the current Resource
280    *
281    * @return The Model of the current Resource
282    */
283   Model *getModel();
284
285   /**
286    * @brief Get the name of the current Resource
287    *
288    * @return The name of the current Resource
289    */
290   const char *getName();
291
292   /**
293    * @brief Get the properties of the current Resource
294    *
295    * @return The properties of the current Resource
296    */
297   virtual xbt_dict_t getProperties();
298
299   /**
300    * @brief Update the state of the current Resource
301    * @details [TODO]
302    *
303    * @param event_type [TODO]
304    * @param value [TODO]
305    * @param date [TODO]
306    */
307   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
308
309   /**
310    * @brief Check if the current Resource is used
311    * @return true if the current Resource is used, false otherwise
312    */
313   virtual bool isUsed()=0;
314
315   /**
316    * @brief Check if the current Resource is active
317    *
318    * @return true if the current Resource is active, false otherwise
319    */
320   bool isOn();
321
322   /**
323    * @brief Turn on the current Resource
324    */
325   void turnOn();
326
327   /**
328    * @brief Turn off the current Resource
329    */
330   void turnOff();
331
332   /**
333    * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
334    *
335    * @return The state of the currenrt Resource
336    */
337   virtual e_surf_resource_state_t getState();
338
339   /**
340    * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
341    *
342    * @param state The new state of the current Resource
343    */
344   virtual void setState(e_surf_resource_state_t state);
345
346 private:
347   const char *p_name;
348   xbt_dict_t p_properties;
349   Model *p_model;
350   bool m_running;
351   e_surf_resource_state_t m_stateCurrent;
352
353   /* LMM */
354 public:
355   /**
356    * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
357    *
358    * @return The lmm constraint associated to this Resource
359    */
360   lmm_constraint_t getConstraint();
361 private:
362   lmm_constraint_t p_constraint;
363 };
364
365 /**********
366  * Action *
367  **********/
368 void surf_action_lmm_update_index_heap(void *action, int i);
369
370 /** @ingroup SURF_interface
371  * @brief SURF action interface class
372  * @details An action is an event generated by a resource (e.g.: a communication for the network)
373  */
374 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
375 private:
376   /**
377    * @brief Common initializations for the constructors
378    */
379   void initialize(Model *model, double cost, bool failed,
380                   lmm_variable_t var = NULL);
381
382 public:
383   /**
384    * @brief Action constructor
385    *
386    * @param model The Model associated to this Action
387    * @param cost The cost of the Action
388    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
389    */
390   Action(Model *model, double cost, bool failed);
391
392   /**
393    * @brief Action constructor
394    *
395    * @param model The Model associated to this Action
396    * @param cost The cost of the Action
397    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
398    * @param var The lmm variable associated to this Action if it is part of a LMM component
399    */
400   Action(Model *model, double cost, bool failed, lmm_variable_t var);
401
402   /** @brief Destructor */
403   virtual ~Action();
404
405   /** @brief Mark that the action is now finished */
406   void finish();
407
408   /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
409   e_surf_action_state_t getState(); /**< get the state*/
410   /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
411   virtual void setState(e_surf_action_state_t state);
412
413   /** @brief Get the bound of the current Action */
414   double getBound();
415   /** @brief Set the bound of the current Action */
416   void setBound(double bound);
417
418   /** @brief Get the start time of the current action */
419   double getStartTime();
420   /** @brief Get the finish time of the current action */
421   double getFinishTime();
422
423   /** @brief Get the user data associated to the current action */
424   void *getData() {return p_data;}
425   /** @brief Set the user data associated to the current action */
426   void setData(void* data);
427
428   /** @brief Get the cost of the current action */
429   double getCost() {return m_cost;}
430   /** @brief Set the cost of the current action */
431   void setCost(double cost) {m_cost = cost;}
432
433   /** @brief Update the maximum duration of the current action
434    *  @param delta Amount to remove from the MaxDuration */
435   void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
436
437   /** @brief Update the remaining time of the current action
438    *  @param delta Amount to remove from the remaining time */
439   void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
440
441   /** @brief Set the remaining time of the current action */
442   void setRemains(double value) {m_remains = value;}
443   /** @brief Get the remaining time of the current action after updating the resource */
444   virtual double getRemains();
445   /** @brief Get the remaining time of the current action without updating the resource */
446   double getRemainsNoUpdate();
447
448   /** @brief Set the finish time of the current action */
449   void setFinishTime(double value) {m_finish = value;}
450
451   /**@brief Add a reference to the current action (refcounting) */
452   void ref();
453   /** @brief Unref that action (and destroy it if refcount reaches 0)
454    *  @return true if the action was destroyed and false if someone still has references on it
455    */
456   virtual int unref();
457
458   /** @brief Cancel the current Action if running */
459   virtual void cancel();
460
461   /** @brief Suspend the current Action */
462   virtual void suspend();
463
464   /** @brief Resume the current Action */
465   virtual void resume();
466
467   /** @brief Returns true if the current action is running */
468   virtual bool isSuspended();
469
470   /** @brief Get the maximum duration of the current action */
471   double getMaxDuration() {return m_maxDuration;}
472   /** @brief Set the maximum duration of the current Action */
473   virtual void setMaxDuration(double duration);
474
475   /** @brief Get the tracing category associated to the current action */
476   char *getCategory() {return p_category;}
477   /** @brief Set the tracing category of the current Action */
478   void setCategory(const char *category);
479
480   /** @brief Get the priority of the current Action */
481   double getPriority() {return m_priority;};
482   /** @brief Set the priority of the current Action */
483   virtual void setPriority(double priority);
484
485   /** @brief Get the state set in which the action is */
486   ActionListPtr getStateSet() {return p_stateSet;};
487
488   s_xbt_swag_hookup_t p_stateHookup;
489
490   Model *getModel() {return p_model;}
491
492 protected:
493   ActionListPtr p_stateSet;
494   double m_priority; /**< priority (1.0 by default) */
495   int    m_refcount;
496   double m_remains; /**< How much of that cost remains to be done in the currently running task */
497   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
498   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
499
500 private:
501   double m_start; /**< start time  */
502   char *p_category;               /**< tracing category for categorized resource utilization monitoring */
503
504   #ifdef HAVE_LATENCY_BOUND_TRACKING
505   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
506   #endif
507   double    m_cost;
508   Model *p_model;
509   void *p_data; /**< for your convenience */
510
511   /* LMM */
512 public:
513   virtual void updateRemainingLazy(double now);
514   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
515   void heapRemove(xbt_heap_t heap);
516   void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
517   void updateIndexHeap(int i);
518   lmm_variable_t getVariable() {return p_variable;}
519   double getLastUpdate() {return m_lastUpdate;}
520   void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
521   enum heap_action_type getHat() {return m_hat;}
522   bool is_linked() {return actionLmmHook::is_linked();}
523   void gapRemove();
524
525 protected:
526   lmm_variable_t p_variable;
527   double m_lastValue;
528   double m_lastUpdate;
529   int m_suspended;
530   int m_indexHeap;
531   enum heap_action_type m_hat;
532 };
533
534 #endif /* SURF_MODEL_H_ */