Logo AND Algorithmique Numérique Distribuée

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