Logo AND Algorithmique Numérique Distribuée

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