Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1f2dba54d1207635fe5f438c06136762c47aad22
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SURF_MODEL_H_
7 #define SURF_MODEL_H_
8
9 #include <xbt.h>
10 #include <string>
11 #include <vector>
12 #include <memory>
13 #include <utility>
14
15 #include <boost/function.hpp>
16 #include <boost/intrusive/list.hpp>
17 #include "surf/trace_mgr.h"
18 #include "xbt/lib.h"
19 #include "surf/surf_routing.h"
20 #include "simgrid/platf_interface.h"
21 #include "surf/surf.h"
22 #include "src/surf/surf_private.h"
23 #include "src/internal_config.h"
24
25 #ifdef LIBSIGC
26 #include <sigc++/sigc++.h>
27 namespace simgrid {
28 namespace surf {
29   // Wraps sigc++ signals with the interface of boost::signals2:
30   template<class T> class signal;
31   template<class R, class... P>
32   class signal<R(P...)> {
33   private:
34     sigc::signal<R, P...> sig_;
35   public:
36     template<class U> XBT_ALWAYS_INLINE
37     void connect(U&& slot)
38     {
39       sig_.connect(std::forward<U>(slot));
40     }
41     template<class Res, class... Args> XBT_ALWAYS_INLINE
42     void connect(Res(*slot)(Args...))
43     {
44       sig_.connect(sigc::ptr_fun(slot));
45     }
46     template<class... Args>
47     R operator()(Args&&... args) const
48     {
49       return sig_.emit(std::forward<Args>(args)...);
50     }
51   };
52 }
53 }
54 #else
55 #include <boost/signals2.hpp>
56 namespace simgrid {
57 namespace surf {
58   template<class T>
59   using signal = ::boost::signals2::signal<T>;
60 }
61 }
62 #endif
63
64 extern XBT_PRIVATE tmgr_history_t history;
65 #define NO_MAX_DURATION -1.0
66
67 /*********
68  * Utils *
69  *********/
70
71 /* user-visible parameters */
72 extern XBT_PRIVATE double sg_tcp_gamma;
73 extern XBT_PRIVATE double sg_sender_gap;
74 extern XBT_PRIVATE double sg_latency_factor;
75 extern XBT_PRIVATE double sg_bandwidth_factor;
76 extern XBT_PRIVATE double sg_weight_S_parameter;
77 extern XBT_PRIVATE int sg_network_crosstraffic;
78 extern XBT_PRIVATE xbt_dynar_t surf_path;
79
80 extern "C" {
81 XBT_PUBLIC(double) surf_get_clock(void);
82 }
83
84 extern XBT_PRIVATE double sg_sender_gap;
85
86 namespace simgrid {
87 namespace surf {
88
89 extern XBT_PRIVATE simgrid::surf::signal<void(void)> surfExitCallbacks;
90
91 }
92 }
93
94 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
95
96 /***********
97  * Classes *
98  ***********/
99
100 enum heap_action_type{
101   LATENCY = 100,
102   MAX_DURATION,
103   NORMAL,
104   NOTSET
105 };
106
107 /*********
108  * Trace *
109  *********/
110 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
111 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
112 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
113 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
114 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
115 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
116 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
117
118 /**********
119  * Action *
120  **********/
121
122 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
123
124 XBT_PUBLIC_DATA(xbt_dynar_t) all_existing_models;
125
126 namespace simgrid {
127 namespace surf {
128
129 /** @ingroup SURF_interface
130  * @brief SURF action interface class
131  * @details An action is an event generated by a resource (e.g.: a communication for the network)
132  */
133 XBT_PUBLIC_CLASS Action {
134 public:
135   boost::intrusive::list_member_hook<> action_hook;
136   boost::intrusive::list_member_hook<> action_lmm_hook;
137   typedef boost::intrusive::member_hook<
138     Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
139   typedef boost::intrusive::list<Action, ActionOptions> ActionList;
140 private:
141   /**
142    * @brief Common initializations for the constructors
143    */
144   void initialize(simgrid::surf::Model *model, double cost, bool failed,
145                   lmm_variable_t var = NULL);
146
147 public:
148   /**
149    * @brief Action constructor
150    *
151    * @param model The Model associated to this Action
152    * @param cost The cost of the Action
153    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
154    */
155   Action(simgrid::surf::Model *model, double cost, bool failed);
156
157   /**
158    * @brief Action constructor
159    *
160    * @param model The Model associated to this Action
161    * @param cost The cost of the Action
162    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
163    * @param var The lmm variable associated to this Action if it is part of a LMM component
164    */
165   Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
166
167   /** @brief Destructor */
168   virtual ~Action();
169
170   /** @brief Mark that the action is now finished */
171   void finish();
172
173   /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
174   e_surf_action_state_t getState(); /**< get the state*/
175   /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
176   virtual void setState(e_surf_action_state_t state);
177
178   /** @brief Get the bound of the current Action */
179   double getBound();
180   /** @brief Set the bound of the current Action */
181   void setBound(double bound);
182
183   /** @brief Get the start time of the current action */
184   double getStartTime();
185   /** @brief Get the finish time of the current action */
186   double getFinishTime();
187
188   /** @brief Get the user data associated to the current action */
189   void *getData() {return p_data;}
190   /** @brief Set the user data associated to the current action */
191   void setData(void* data);
192
193   /** @brief Get the cost of the current action */
194   double getCost() {return m_cost;}
195   /** @brief Set the cost of the current action */
196   void setCost(double cost) {m_cost = cost;}
197
198   /** @brief Update the maximum duration of the current action
199    *  @param delta Amount to remove from the MaxDuration */
200   void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
201
202   /** @brief Update the remaining time of the current action
203    *  @param delta Amount to remove from the remaining time */
204   void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
205
206   /** @brief Set the remaining time of the current action */
207   void setRemains(double value) {m_remains = value;}
208   /** @brief Get the remaining time of the current action after updating the resource */
209   virtual double getRemains();
210   /** @brief Get the remaining time of the current action without updating the resource */
211   double getRemainsNoUpdate();
212
213   /** @brief Set the finish time of the current action */
214   void setFinishTime(double value) {m_finish = value;}
215
216   /**@brief Add a reference to the current action (refcounting) */
217   void ref();
218   /** @brief Unref that action (and destroy it if refcount reaches 0)
219    *  @return true if the action was destroyed and false if someone still has references on it
220    */
221   virtual int unref();
222
223   /** @brief Cancel the current Action if running */
224   virtual void cancel();
225
226   /** @brief Suspend the current Action */
227   virtual void suspend();
228
229   /** @brief Resume the current Action */
230   virtual void resume();
231
232   /** @brief Returns true if the current action is running */
233   virtual bool isSuspended();
234
235   /** @brief Get the maximum duration of the current action */
236   double getMaxDuration() {return m_maxDuration;}
237   /** @brief Set the maximum duration of the current Action */
238   virtual void setMaxDuration(double duration);
239
240   /** @brief Get the tracing category associated to the current action */
241   char *getCategory() {return p_category;}
242   /** @brief Set the tracing category of the current Action */
243   void setCategory(const char *category);
244
245   /** @brief Get the priority of the current Action */
246   double getPriority() {return m_priority;};
247   /** @brief Set the priority of the current Action */
248   virtual void setPriority(double priority);
249
250   /** @brief Get the state set in which the action is */
251   ActionList* getStateSet() {return p_stateSet;};
252
253   s_xbt_swag_hookup_t p_stateHookup = {NULL,NULL};
254
255   simgrid::surf::Model *getModel() {return p_model;}
256
257 protected:
258   ActionList* p_stateSet;
259   double m_priority = 1.0; /**< priority (1.0 by default) */
260   int    m_refcount = 1;
261   double m_remains; /**< How much of that cost remains to be done in the currently running task */
262   double m_maxDuration = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
263   double m_finish = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
264
265 private:
266   double m_start; /**< start time  */
267   char *p_category = NULL;            /**< tracing category for categorized resource utilization monitoring */
268
269   #ifdef HAVE_LATENCY_BOUND_TRACKING
270   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
271   #endif
272   double    m_cost;
273   simgrid::surf::Model *p_model;
274   void *p_data = NULL; /**< for your convenience */
275
276   /* LMM */
277 public:
278   virtual void updateRemainingLazy(double now);
279   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
280   void heapRemove(xbt_heap_t heap);
281   void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
282   void updateIndexHeap(int i);
283   lmm_variable_t getVariable() {return p_variable;}
284   double getLastUpdate() {return m_lastUpdate;}
285   void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
286   enum heap_action_type getHat() {return m_hat;}
287   bool is_linked() {return action_lmm_hook.is_linked();}
288   void gapRemove();
289
290 protected:
291   lmm_variable_t p_variable;
292   double m_lastValue = 0;
293   double m_lastUpdate = 0;
294   int m_suspended = 0;
295   int m_indexHeap;
296   enum heap_action_type m_hat = NOTSET;
297 };
298
299 typedef Action::ActionList ActionList;
300
301 typedef boost::intrusive::member_hook<
302   Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
303 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
304 typedef ActionLmmList* ActionLmmListPtr;
305
306 /*********
307  * Model *
308  *********/
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 = nullptr;
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 }
394
395 /************
396  * Resource *
397  ************/
398
399 /** @ingroup SURF_interface
400  * @brief Resource which have a metric handled by a maxmin system
401  */
402 typedef struct {
403   double peak;              /**< The peak of the metric, ie its max value */
404   double scale;             /**< Current availability of the metric according to the traces, in [0,1] */
405   tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
406 } s_surf_metric_t;
407
408 namespace simgrid {
409 namespace surf {
410
411 /** @ingroup SURF_interface
412  * @brief SURF resource interface class
413  * @details A resource represent an element of a component (e.g.: a link for the network)
414  */
415 XBT_PUBLIC_CLASS Resource {
416 public:
417   /**
418    * @brief Constructor of non-LMM Resources
419    *
420    * @param model Model associated to this Resource
421    * @param name The name of the Resource
422    */
423   Resource(Model *model, const char *name);
424
425   /**
426    * @brief Constructor of LMM Resources
427    *
428    * @param model Model associated to this Resource
429    * @param name The name of the Resource
430    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
431    */
432   Resource(Model *model, const char *name, lmm_constraint_t constraint);
433
434   Resource(Model *model, const char *name, lmm_constraint_t constraint, int initiallyOn);
435
436   /**
437    * @brief Resource constructor
438    *
439    * @param model Model associated to this Resource
440    * @param name The name of the Resource
441    * @param initiallyOn the initial state of the Resource
442    */
443   Resource(Model *model, const char *name, int initiallyOn);
444
445   virtual ~Resource();
446
447   /** @brief Get the Model of the current Resource */
448   Model *getModel();
449
450   /** @brief Get the name of the current Resource */
451   const char *getName();
452
453   /**
454    * @brief Update the state of the current Resource
455    * @details [TODO]
456    *
457    * @param event_type [TODO]
458    * @param value [TODO]
459    * @param date [TODO]
460    */
461   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
462
463   /** @brief Check if the current Resource is used (if it currently serves an action) */
464   virtual bool isUsed()=0;
465
466   /** @brief Check if the current Resource is active */
467   virtual bool isOn();
468   /** @brief Check if the current Resource is shut down */
469   virtual bool isOff();
470   /** @brief Turn on the current Resource */
471   virtual void turnOn();
472   /** @brief Turn off the current Resource */
473   virtual void turnOff();
474
475 private:
476   const char *p_name;
477   Model *p_model;
478   bool m_isOn;
479
480 public: /* LMM */
481   /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component */
482   lmm_constraint_t getConstraint();
483 protected:
484   lmm_constraint_t p_constraint = nullptr;
485 };
486
487 }
488 }
489
490 #endif /* SURF_MODEL_H_ */