Logo AND Algorithmique Numérique Distribuée

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