Logo AND Algorithmique Numérique Distribuée

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