Logo AND Algorithmique Numérique Distribuée

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