Logo AND Algorithmique Numérique Distribuée

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