Logo AND Algorithmique Numérique Distribuée

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