Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 <cstddef>
10 #include <string>
11
12 #include <xbt.h>
13 #include <memory>
14 #include <utility>
15
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 /** \ingroup SURF_simulation
45  *  \brief List of hosts that have just restarted and whose autorestart process should be restarted.
46  */
47 XBT_PUBLIC_DATA(std::vector<sg_host_t>) host_that_restart;
48
49
50 extern XBT_PRIVATE double sg_sender_gap;
51
52 namespace simgrid {
53 namespace surf {
54
55 extern XBT_PRIVATE simgrid::xbt::signal<void(void)> surfExitCallbacks;
56
57 }
58 }
59
60 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
61
62 static inline char* sg_storage_name(sg_storage_t storage) {
63   return storage->key;
64 }
65
66 /***********
67  * Classes *
68  ***********/
69
70 enum heap_action_type{
71   LATENCY = 100,
72   MAX_DURATION,
73   NORMAL,
74   NOTSET
75 };
76
77 /*********
78  * Trace *
79  *********/
80 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
81 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
82
83 /**********
84  * Action *
85  **********/
86
87 XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
88
89 /** \ingroup SURF_models
90  *  \brief List of initialized models
91  */
92 XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
93
94 namespace simgrid {
95 namespace surf {
96
97 /** @ingroup SURF_interface
98  * @brief SURF action interface class
99  * @details An action is an event generated by a resource (e.g.: a communication for the network)
100  */
101 XBT_PUBLIC_CLASS Action {
102 public:
103   boost::intrusive::list_member_hook<> action_hook;
104   boost::intrusive::list_member_hook<> action_lmm_hook;
105   typedef boost::intrusive::member_hook<
106     Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
107   typedef boost::intrusive::list<Action, ActionOptions> ActionList;
108
109   enum class State {
110     ready = 0,        /**< Ready        */
111     running,          /**< Running      */
112     failed,           /**< Task Failure */
113     done,             /**< Completed    */
114     to_free,          /**< Action to free in next cleanup */
115     not_in_the_system /**< Not in the system anymore. Why did you ask ? */
116   };
117
118 public:
119   /**
120    * @brief Action constructor
121    *
122    * @param model The Model associated to this Action
123    * @param cost The cost of the Action
124    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
125    */
126   Action(simgrid::surf::Model *model, double cost, bool failed);
127
128   /**
129    * @brief Action constructor
130    *
131    * @param model The Model associated to this Action
132    * @param cost The cost of the Action
133    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
134    * @param var The lmm variable associated to this Action if it is part of a LMM component
135    */
136   Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
137
138   /** @brief Destructor */
139   virtual ~Action();
140
141   /** @brief Mark that the action is now finished */
142   void finish();
143
144   /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
145   Action::State getState(); /**< get the state*/
146   /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
147   virtual void setState(Action::State state);
148
149   /** @brief Get the bound of the current Action */
150   double getBound();
151   /** @brief Set the bound of the current Action */
152   void setBound(double bound);
153
154   /** @brief Get the start time of the current action */
155   double getStartTime();
156   /** @brief Get the finish time of the current action */
157   double getFinishTime();
158
159   /** @brief Get the user data associated to the current action */
160   void *getData() {return data_;}
161   /** @brief Set the user data associated to the current action */
162   void setData(void* data);
163
164   /** @brief Get the cost of the current action */
165   double getCost() {return cost_;}
166   /** @brief Set the cost of the current action */
167   void setCost(double cost) {cost_ = cost;}
168
169   /** @brief Update the maximum duration of the current action
170    *  @param delta Amount to remove from the MaxDuration */
171   void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
172
173   /** @brief Update the remaining time of the current action
174    *  @param delta Amount to remove from the remaining time */
175   void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
176
177   /** @brief Set the remaining time of the current action */
178   void setRemains(double value) {remains_ = value;}
179   /** @brief Get the remaining time of the current action after updating the resource */
180   virtual double getRemains();
181   /** @brief Get the remaining time of the current action without updating the resource */
182   double getRemainsNoUpdate();
183
184   /** @brief Set the finish time of the current action */
185   void setFinishTime(double value) {finishTime_ = value;}
186
187   /**@brief Add a reference to the current action (refcounting) */
188   void ref();
189   /** @brief Unref that action (and destroy it if refcount reaches 0)
190    *  @return true if the action was destroyed and false if someone still has references on it
191    */
192   virtual int unref();
193
194   /** @brief Cancel the current Action if running */
195   virtual void cancel();
196
197   /** @brief Suspend the current Action */
198   virtual void suspend();
199
200   /** @brief Resume the current Action */
201   virtual void resume();
202
203   /** @brief Returns true if the current action is running */
204   virtual bool isSuspended();
205
206   /** @brief Get the maximum duration of the current action */
207   double getMaxDuration() {return maxDuration_;}
208   /** @brief Set the maximum duration of the current Action */
209   virtual void setMaxDuration(double duration);
210
211   /** @brief Get the tracing category associated to the current action */
212   char *getCategory() {return category_;}
213   /** @brief Set the tracing category of the current Action */
214   void setCategory(const char *category);
215
216   /** @brief Get the priority of the current Action */
217   double getPriority() {return priority_;};
218   /** @brief Set the priority of the current Action */
219   virtual void setPriority(double priority);
220
221   /** @brief Get the state set in which the action is */
222   ActionList* getStateSet() {return stateSet_;};
223
224   s_xbt_swag_hookup_t stateHookup_ = {nullptr,nullptr};
225
226   simgrid::surf::Model *getModel() {return model_;}
227
228 protected:
229   ActionList* stateSet_;
230   double priority_ = 1.0; /**< priority (1.0 by default) */
231   int    refcount_ = 1;
232   double remains_; /**< How much of that cost remains to be done in the currently running task */
233   double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
234   double finishTime_ = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
235
236 private:
237   double start_; /**< start time  */
238   char *category_ = nullptr;            /**< tracing category for categorized resource utilization monitoring */
239
240   double    cost_;
241   simgrid::surf::Model *model_;
242   void *data_ = nullptr; /**< for your convenience */
243
244   /* LMM */
245 public:
246   virtual void updateRemainingLazy(double now);
247   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
248   void heapRemove(xbt_heap_t heap);
249   void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
250   void updateIndexHeap(int i);
251   lmm_variable_t getVariable() {return variable_;}
252   double getLastUpdate() {return lastUpdate_;}
253   void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
254   enum heap_action_type getHat() {return hat_;}
255   bool is_linked() {return action_lmm_hook.is_linked();}
256   void gapRemove();
257
258 protected:
259   lmm_variable_t variable_ = nullptr;
260   double lastValue_ = 0;
261   double lastUpdate_ = 0;
262   int suspended_ = 0;
263   int indexHeap_;
264   enum heap_action_type hat_ = NOTSET;
265 };
266
267 typedef Action::ActionList ActionList;
268
269 typedef boost::intrusive::member_hook<
270   Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
271 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
272 typedef ActionLmmList* ActionLmmListPtr;
273
274 /*********
275  * Model *
276  *********/
277
278 /** @ingroup SURF_interface
279  * @brief SURF model interface class
280  * @details A model is an object which handle the interactions between its Resources and its Actions
281  */
282 XBT_PUBLIC_CLASS Model {
283 public:
284   Model();
285   virtual ~Model();
286
287   /** @brief Get the set of [actions](@ref Action) in *ready* state */
288   virtual ActionList* getReadyActionSet() {return readyActionSet_;}
289
290   /** @brief Get the set of [actions](@ref Action) in *running* state */
291   virtual ActionList* getRunningActionSet() {return runningActionSet_;}
292
293   /** @brief Get the set of [actions](@ref Action) in *failed* state */
294   virtual ActionList* getFailedActionSet() {return failedActionSet_;}
295
296   /** @brief Get the set of [actions](@ref Action) in *done* state */
297   virtual ActionList* getDoneActionSet() {return doneActionSet_;}
298
299   /** @brief Get the set of modified [actions](@ref Action) */
300   virtual ActionLmmListPtr getModifiedSet() {return modifiedSet_;}
301
302   /** @brief Get the maxmin system of the current Model */
303   lmm_system_t getMaxminSystem() {return maxminSystem_;}
304
305   /**
306    * @brief Get the update mechanism of the current Model
307    * @see e_UM_t
308    */
309   e_UM_t getUpdateMechanism() {return updateMechanism_;}
310
311   /** @brief Get Action heap */
312   xbt_heap_t getActionHeap() {return actionHeap_;}
313
314   /**
315    * @brief Share the resources between the actions
316    *
317    * @param now The current time of the simulation
318    * @return The delta of time till the next action will finish
319    */
320   virtual double nextOccuringEvent(double now);
321   virtual double nextOccuringEventLazy(double now);
322   virtual double nextOccuringEventFull(double now);
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 nextOccuringEventIsIdempotent() { return true;}
340
341 protected:
342   ActionLmmListPtr modifiedSet_;
343   lmm_system_t maxminSystem_ = nullptr;
344   e_UM_t updateMechanism_ = UM_UNDEFINED;
345   bool selectiveUpdate_;
346   xbt_heap_t actionHeap_;
347
348 private:
349   ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
350   ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
351   ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
352   ActionList* 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 This is the ancestor class of every resources in SimGrid, such as links, CPU or storage
377  */
378 XBT_PUBLIC_CLASS Resource {
379 public:
380   /**
381    * @brief Constructor of LMM Resources
382    *
383    * @param model Model associated to this Resource
384    * @param name The name of the Resource
385    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
386    */
387   Resource(Model *model, const char *name, lmm_constraint_t constraint);
388
389   virtual ~Resource();
390
391   /** @brief Get the Model of the current Resource */
392   Model *getModel() const;
393
394   /** @brief Get the name of the current Resource */
395   const char *getName() const;
396
397   bool operator==(const Resource &other) const;
398
399   /**
400    * @brief Apply an event of external load event to that resource
401    *
402    * @param event What happened
403    * @param value [TODO]
404    */
405   virtual void apply_event(tmgr_trace_iterator_t event, double value)=0;
406
407   /** @brief Check if the current Resource is used (if it currently serves an action) */
408   virtual bool isUsed()=0;
409
410   /** @brief Check if the current Resource is active */
411   virtual bool isOn() const;
412   /** @brief Check if the current Resource is shut down */
413   virtual bool isOff() const;
414   /** @brief Turn on the current Resource */
415   virtual void turnOn();
416   /** @brief Turn off the current Resource */
417   virtual void turnOff();
418
419 private:
420   std::string name_;
421   Model *model_;
422   bool isOn_ = true;
423
424 public: /* LMM */
425   /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component (or null if none) */
426   lmm_constraint_t getConstraint() const;
427 protected:
428   const lmm_constraint_t constraint_ = nullptr;
429 };
430
431 }
432 }
433
434 namespace std {
435   template <>
436   struct hash<simgrid::surf::Resource>
437   {
438     std::size_t operator()(const simgrid::surf::Resource& r) const
439     {
440       return (std::size_t) xbt_str_hash(r.getName());
441     }
442   };
443 }
444
445 #endif /* SURF_MODEL_H_ */