Logo AND Algorithmique Numérique Distribuée

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