Logo AND Algorithmique Numérique Distribuée

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