Logo AND Algorithmique Numérique Distribuée

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