Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_heap with boost::heap for surf actions.
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2017. 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/signal.hpp"
10
11 #include "src/surf/surf_private.hpp"
12 #include "surf/surf.hpp"
13 #include "xbt/str.h"
14
15 #include <boost/heap/pairing_heap.hpp>
16 #include <boost/intrusive/list.hpp>
17 #include <boost/optional.hpp>
18 #include <set>
19 #include <string>
20 #include <unordered_map>
21
22 #define NO_MAX_DURATION -1.0
23
24 /*********
25  * Utils *
26  *********/
27
28 /* user-visible parameters */
29 extern XBT_PRIVATE double sg_tcp_gamma;
30 extern XBT_PRIVATE double sg_latency_factor;
31 extern XBT_PRIVATE double sg_bandwidth_factor;
32 extern XBT_PRIVATE double sg_weight_S_parameter;
33 extern XBT_PRIVATE int sg_network_crosstraffic;
34 extern XBT_PRIVATE std::vector<std::string> surf_path;
35 extern XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
36 extern XBT_PRIVATE std::set<std::string> watched_hosts;
37
38 extern "C" {
39 XBT_PUBLIC(double) surf_get_clock();
40 }
41 /** \ingroup SURF_simulation
42  *  \brief List of hosts that have just restarted and whose autorestart process should be restarted.
43  */
44 XBT_PUBLIC_DATA(std::vector<sg_host_t>) host_that_restart;
45
46 namespace simgrid {
47 namespace surf {
48
49 extern XBT_PRIVATE simgrid::xbt::signal<void()> surfExitCallbacks;
50 }
51 }
52
53 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
54
55 /***********
56  * Classes *
57  ***********/
58
59 enum heap_action_type{
60   LATENCY = 100,
61   MAX_DURATION,
62   NORMAL,
63   NOTSET
64 };
65
66 /**********
67  * Action *
68  **********/
69
70 /** \ingroup SURF_models
71  *  \brief List of initialized models
72  */
73 XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
74
75 namespace simgrid {
76 namespace surf {
77
78 typedef std::pair<double, simgrid::surf::Action*> heap_element_type;
79 struct heap_element_compare {
80   bool operator()(const heap_element_type& a, const heap_element_type& b) const { return a.first > b.first; }
81 };
82 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
83                                   boost::heap::compare<heap_element_compare>>
84     heap_type;
85
86 /** @ingroup SURF_interface
87  * @brief SURF action interface class
88  * @details An action is an event generated by a resource (e.g.: a communication for the network)
89  */
90 XBT_PUBLIC_CLASS Action {
91 public:
92   boost::intrusive::list_member_hook<> action_hook;
93   boost::intrusive::list_member_hook<> action_lmm_hook;
94   typedef boost::intrusive::member_hook<
95     Action, boost::intrusive::list_member_hook<>, &Action::action_hook> ActionOptions;
96   typedef boost::intrusive::list<Action, ActionOptions> ActionList;
97
98   enum class State {
99     ready = 0,        /**< Ready        */
100     running,          /**< Running      */
101     failed,           /**< Task Failure */
102     done,             /**< Completed    */
103     to_free,          /**< Action to free in next cleanup */
104     not_in_the_system /**< Not in the system anymore. Why did you ask ? */
105   };
106
107   /**
108    * @brief Action constructor
109    *
110    * @param model The Model associated to this Action
111    * @param cost The cost of the Action
112    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
113    */
114   Action(simgrid::surf::Model *model, double cost, bool failed);
115
116   /**
117    * @brief Action constructor
118    *
119    * @param model The Model associated to this Action
120    * @param cost The cost of the Action
121    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
122    * @param var The lmm variable associated to this Action if it is part of a LMM component
123    */
124   Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var);
125
126   /** @brief Destructor */
127   virtual ~Action();
128
129   /**
130    * @brief Mark that the action is now finished
131    *
132    * @param state the new [state](\ref simgrid::surf::Action::State) of the current Action
133    */
134   void finish(Action::State state);
135
136   /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
137   Action::State getState(); /**< get the state*/
138   /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
139   virtual void setState(Action::State 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 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 cost_;}
158   /** @brief Set the cost of the current action */
159   void setCost(double cost) {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(&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(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
168
169   /** @brief Set the remaining time of the current action */
170   void setRemains(double value) {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) {finishTime_ = 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 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 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 sharingWeight_; };
210   /** @brief Set the priority of the current Action */
211   virtual void setSharingWeight(double priority);
212   void setSharingWeightNoUpdate(double weight) { sharingWeight_ = weight; }
213
214   /** @brief Get the state set in which the action is */
215   ActionList* getStateSet() {return stateSet_;};
216
217   s_xbt_swag_hookup_t stateHookup_ = {nullptr,nullptr};
218
219   simgrid::surf::Model* getModel() { return model_; }
220
221 protected:
222   ActionList* stateSet_;
223   int    refcount_ = 1;
224
225 private:
226   double sharingWeight_ = 1.0;             /**< priority (1.0 by default) */
227   double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
228   double remains_;                       /**< How much of that cost remains to be done in the currently running task */
229   double start_; /**< start time  */
230   char *category_ = nullptr;            /**< tracing category for categorized resource utilization monitoring */
231   double finishTime_ =
232       -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
233
234   double    cost_;
235   simgrid::surf::Model *model_;
236   void *data_ = nullptr; /**< for your convenience */
237
238   /* LMM */
239   double lastUpdate_         = 0;
240   double lastValue_          = 0;
241   lmm_variable_t variable_   = nullptr;
242   enum heap_action_type hat_ = NOTSET;
243   boost::optional<heap_type::handle_type> heapHandle_ = boost::none;
244
245 public:
246   virtual void updateRemainingLazy(double now) { THROW_IMPOSSIBLE; };
247   void heapInsert(heap_type& heap, double key, enum heap_action_type hat);
248   void heapRemove(heap_type& heap);
249   void heapUpdate(heap_type& heap, double key, enum heap_action_type hat);
250   void clearHeapHandle() { heapHandle_ = boost::none; }
251   lmm_variable_t getVariable() {return variable_;}
252   void setVariable(lmm_variable_t var) { variable_ = var; }
253   double getLastUpdate() {return lastUpdate_;}
254   void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
255   double getLastValue() { return lastValue_; }
256   void setLastValue(double val) { lastValue_ = val; }
257   enum heap_action_type getHat() { return hat_; }
258   bool is_linked() {return action_lmm_hook.is_linked();}
259 protected:
260   int suspended_ = 0;
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   /** @brief Get the set of [actions](@ref Action) in *ready* state */
284   virtual ActionList* getReadyActionSet() {return readyActionSet_;}
285
286   /** @brief Get the set of [actions](@ref Action) in *running* state */
287   virtual ActionList* getRunningActionSet() {return runningActionSet_;}
288
289   /** @brief Get the set of [actions](@ref Action) in *failed* state */
290   virtual ActionList* getFailedActionSet() {return failedActionSet_;}
291
292   /** @brief Get the set of [actions](@ref Action) in *done* state */
293   virtual ActionList* getDoneActionSet() {return doneActionSet_;}
294
295   /** @brief Get the set of modified [actions](@ref Action) */
296   virtual ActionLmmListPtr getModifiedSet() {return modifiedSet_;}
297
298   /** @brief Get the maxmin system of the current Model */
299   lmm_system_t getMaxminSystem() {return maxminSystem_;}
300
301   /**
302    * @brief Get the update mechanism of the current Model
303    * @see e_UM_t
304    */
305   e_UM_t getUpdateMechanism() {return updateMechanism_;}
306   void setUpdateMechanism(e_UM_t mechanism) { updateMechanism_ = mechanism; }
307
308   /** @brief Get Action heap */
309   heap_type& getActionHeap() { return actionHeap_; }
310
311   double actionHeapTopDate() const { return actionHeap_.top().first; }
312   Action* actionHeapPop();
313   bool actionHeapIsEmpty() const { return actionHeap_.empty(); }
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 nextOccuringEvent(double now);
322   virtual double nextOccuringEventLazy(double now);
323   virtual double nextOccuringEventFull(double now);
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 nextOccuringEventIsIdempotent() { return true;}
341
342 protected:
343   ActionLmmListPtr modifiedSet_;
344   lmm_system_t maxminSystem_ = nullptr;
345   bool selectiveUpdate_;
346
347 private:
348   e_UM_t updateMechanism_ = UM_UNDEFINED;
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   heap_type actionHeap_;
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 struct s_surf_metric_t {
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_event_t event; /**< The associated trace event associated to the metric */
370 };
371
372 namespace simgrid {
373 namespace surf {
374
375 /** @ingroup SURF_interface
376  * @brief SURF resource interface class
377  * @details This is the ancestor class of every resources in SimGrid, such as links, CPU or storage
378  */
379 XBT_PUBLIC_CLASS Resource {
380 public:
381   /**
382    * @brief Constructor of LMM Resources
383    *
384    * @param model Model associated to this Resource
385    * @param name The name of the Resource
386    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
387    */
388   Resource(Model * model, const std::string& name, lmm_constraint_t constraint);
389
390   virtual ~Resource();
391
392   /** @brief Get the Model of the current Resource */
393   Model* model() const;
394
395   /** @brief Get the name of the current Resource */
396   const std::string& getName() const;
397   /** @brief Get the name of the current Resource */
398   const char* getCname() const;
399
400   bool operator==(const Resource &other) const;
401
402   /**
403    * @brief Apply an event of external load event to that resource
404    *
405    * @param event What happened
406    * @param value [TODO]
407    */
408   virtual void apply_event(tmgr_trace_event_t event, double value) = 0;
409
410   /** @brief Check if the current Resource is used (if it currently serves an action) */
411   virtual bool isUsed()=0;
412
413   /** @brief Check if the current Resource is active */
414   virtual bool isOn() const;
415   /** @brief Check if the current Resource is shut down */
416   virtual bool isOff() const;
417   /** @brief Turn on the current Resource */
418   virtual void turnOn();
419   /** @brief Turn off the current Resource */
420   virtual void turnOff();
421
422 private:
423   std::string name_;
424   Model *model_;
425   bool isOn_ = true;
426
427 public: /* LMM */
428   /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component (or null if none) */
429   lmm_constraint_t constraint() const;
430
431 protected:
432   const lmm_constraint_t constraint_ = nullptr;
433 };
434
435 }
436 }
437
438 namespace std {
439 template <> class hash<simgrid::surf::Resource> {
440 public:
441   std::size_t operator()(const simgrid::surf::Resource& r) const { return (std::size_t)xbt_str_hash(r.getCname()); }
442 };
443 }
444
445 #endif /* SURF_MODEL_H_ */