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