Logo AND Algorithmique Numérique Distribuée

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