Logo AND Algorithmique Numérique Distribuée

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