Logo AND Algorithmique Numérique Distribuée

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