Logo AND Algorithmique Numérique Distribuée

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