Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid into s_type_cleanup
[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.h"
12 #include "surf/surf.h"
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   /** @brief Mark that the action is now finished */
122   void finish();
123
124   /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
125   Action::State getState(); /**< get the state*/
126   /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
127   virtual void setState(Action::State state);
128
129   /** @brief Get the bound of the current Action */
130   double getBound();
131   /** @brief Set the bound of the current Action */
132   void setBound(double bound);
133
134   /** @brief Get the start time of the current action */
135   double getStartTime();
136   /** @brief Get the finish time of the current action */
137   double getFinishTime();
138
139   /** @brief Get the user data associated to the current action */
140   void *getData() {return data_;}
141   /** @brief Set the user data associated to the current action */
142   void setData(void* data);
143
144   /** @brief Get the cost of the current action */
145   double getCost() {return cost_;}
146   /** @brief Set the cost of the current action */
147   void setCost(double cost) {cost_ = cost;}
148
149   /** @brief Update the maximum duration of the current action
150    *  @param delta Amount to remove from the MaxDuration */
151   void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
152
153   /** @brief Update the remaining time of the current action
154    *  @param delta Amount to remove from the remaining time */
155   void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
156
157   /** @brief Set the remaining time of the current action */
158   void setRemains(double value) {remains_ = value;}
159   /** @brief Get the remaining time of the current action after updating the resource */
160   virtual double getRemains();
161   /** @brief Get the remaining time of the current action without updating the resource */
162   double getRemainsNoUpdate();
163
164   /** @brief Set the finish time of the current action */
165   void setFinishTime(double value) {finishTime_ = value;}
166
167   /**@brief Add a reference to the current action (refcounting) */
168   void ref();
169   /** @brief Unref that action (and destroy it if refcount reaches 0)
170    *  @return true if the action was destroyed and false if someone still has references on it
171    */
172   virtual int unref();
173
174   /** @brief Cancel the current Action if running */
175   virtual void cancel();
176
177   /** @brief Suspend the current Action */
178   virtual void suspend();
179
180   /** @brief Resume the current Action */
181   virtual void resume();
182
183   /** @brief Returns true if the current action is running */
184   virtual bool isSuspended();
185
186   /** @brief Get the maximum duration of the current action */
187   double getMaxDuration() {return maxDuration_;}
188   /** @brief Set the maximum duration of the current Action */
189   virtual void setMaxDuration(double duration);
190
191   /** @brief Get the tracing category associated to the current action */
192   char *getCategory() {return category_;}
193   /** @brief Set the tracing category of the current Action */
194   void setCategory(const char *category);
195
196   /** @brief Get the priority of the current Action */
197   double getPriority() { return sharingWeight_; };
198   /** @brief Set the priority of the current Action */
199   virtual void setSharingWeight(double priority);
200
201   /** @brief Get the state set in which the action is */
202   ActionList* getStateSet() {return stateSet_;};
203
204   s_xbt_swag_hookup_t stateHookup_ = {nullptr,nullptr};
205
206   simgrid::surf::Model* getModel() { return model_; }
207
208 protected:
209   ActionList* stateSet_;
210   double sharingWeight_ = 1.0; /**< priority (1.0 by default) */
211   int    refcount_ = 1;
212   double remains_; /**< How much of that cost remains to be done in the currently running task */
213   double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
214   double finishTime_ = -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
215
216 private:
217   double start_; /**< start time  */
218   char *category_ = nullptr;            /**< tracing category for categorized resource utilization monitoring */
219
220   double    cost_;
221   simgrid::surf::Model *model_;
222   void *data_ = nullptr; /**< for your convenience */
223
224   /* LMM */
225 public:
226   virtual void updateRemainingLazy(double now);
227   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
228   void heapRemove(xbt_heap_t heap);
229   void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
230   virtual void updateIndexHeap(int i);
231   lmm_variable_t getVariable() {return variable_;}
232   double getLastUpdate() {return lastUpdate_;}
233   void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
234   enum heap_action_type getHat() {return hat_;}
235   bool is_linked() {return action_lmm_hook.is_linked();}
236
237 protected:
238   lmm_variable_t variable_ = nullptr;
239   double lastValue_ = 0;
240   double lastUpdate_ = 0;
241   int suspended_ = 0;
242   int indexHeap_;
243   enum heap_action_type hat_ = NOTSET;
244 };
245
246 typedef Action::ActionList ActionList;
247
248 typedef boost::intrusive::member_hook<
249   Action, boost::intrusive::list_member_hook<>, &Action::action_lmm_hook> ActionLmmOptions;
250 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
251 typedef ActionLmmList* ActionLmmListPtr;
252
253 /*********
254  * Model *
255  *********/
256
257 /** @ingroup SURF_interface
258  * @brief SURF model interface class
259  * @details A model is an object which handle the interactions between its Resources and its Actions
260  */
261 XBT_PUBLIC_CLASS Model {
262 public:
263   Model();
264   virtual ~Model();
265
266   /** @brief Get the set of [actions](@ref Action) in *ready* state */
267   virtual ActionList* getReadyActionSet() {return readyActionSet_;}
268
269   /** @brief Get the set of [actions](@ref Action) in *running* state */
270   virtual ActionList* getRunningActionSet() {return runningActionSet_;}
271
272   /** @brief Get the set of [actions](@ref Action) in *failed* state */
273   virtual ActionList* getFailedActionSet() {return failedActionSet_;}
274
275   /** @brief Get the set of [actions](@ref Action) in *done* state */
276   virtual ActionList* getDoneActionSet() {return doneActionSet_;}
277
278   /** @brief Get the set of modified [actions](@ref Action) */
279   virtual ActionLmmListPtr getModifiedSet() {return modifiedSet_;}
280
281   /** @brief Get the maxmin system of the current Model */
282   lmm_system_t getMaxminSystem() {return maxminSystem_;}
283
284   /**
285    * @brief Get the update mechanism of the current Model
286    * @see e_UM_t
287    */
288   e_UM_t getUpdateMechanism() {return updateMechanism_;}
289
290   /** @brief Get Action heap */
291   xbt_heap_t getActionHeap() {return actionHeap_;}
292
293   /**
294    * @brief Share the resources between the actions
295    *
296    * @param now The current time of the simulation
297    * @return The delta of time till the next action will finish
298    */
299   virtual double nextOccuringEvent(double now);
300   virtual double nextOccuringEventLazy(double now);
301   virtual double nextOccuringEventFull(double now);
302
303   /**
304    * @brief Update action to the current time
305    *
306    * @param now The current time of the simulation
307    * @param delta The delta of time since the last update
308    */
309   virtual void updateActionsState(double now, double delta);
310   virtual void updateActionsStateLazy(double now, double delta);
311   virtual void updateActionsStateFull(double now, double delta);
312
313   /** @brief Returns whether this model have an idempotent shareResource()
314    *
315    * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
316    * so we need to call it only when the next timestamp of other sources is computed.
317    */
318   virtual bool nextOccuringEventIsIdempotent() { return true;}
319
320 protected:
321   ActionLmmListPtr modifiedSet_;
322   lmm_system_t maxminSystem_ = nullptr;
323   e_UM_t updateMechanism_ = UM_UNDEFINED;
324   bool selectiveUpdate_;
325   xbt_heap_t actionHeap_;
326
327 private:
328   ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
329   ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
330   ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
331   ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
332 };
333
334 }
335 }
336
337 /************
338  * Resource *
339  ************/
340
341 /** @ingroup SURF_interface
342  * @brief Resource which have a metric handled by a maxmin system
343  */
344 typedef struct {
345   double peak;              /**< The peak of the metric, ie its max value */
346   double scale;             /**< Current availability of the metric according to the traces, in [0,1] */
347   tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
348 } s_surf_metric_t;
349
350 namespace simgrid {
351 namespace surf {
352
353 /** @ingroup SURF_interface
354  * @brief SURF resource interface class
355  * @details This is the ancestor class of every resources in SimGrid, such as links, CPU or storage
356  */
357 XBT_PUBLIC_CLASS Resource {
358 public:
359   /**
360    * @brief Constructor of LMM Resources
361    *
362    * @param model Model associated to this Resource
363    * @param name The name of the Resource
364    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
365    */
366   Resource(Model *model, const char *name, lmm_constraint_t constraint);
367
368   virtual ~Resource();
369
370   /** @brief Get the Model of the current Resource */
371   Model* model() const;
372
373   /** @brief Get the name of the current Resource */
374   const char* cname() const;
375
376   bool operator==(const Resource &other) const;
377
378   /**
379    * @brief Apply an event of external load event to that resource
380    *
381    * @param event What happened
382    * @param value [TODO]
383    */
384   virtual void apply_event(tmgr_trace_event_t event, double value) = 0;
385
386   /** @brief Check if the current Resource is used (if it currently serves an action) */
387   virtual bool isUsed()=0;
388
389   /** @brief Check if the current Resource is active */
390   virtual bool isOn() const;
391   /** @brief Check if the current Resource is shut down */
392   virtual bool isOff() const;
393   /** @brief Turn on the current Resource */
394   virtual void turnOn();
395   /** @brief Turn off the current Resource */
396   virtual void turnOff();
397
398 private:
399   std::string name_;
400   Model *model_;
401   bool isOn_ = true;
402
403 public: /* LMM */
404   /** @brief Get the lmm constraint associated to this Resource if it is part of a LMM component (or null if none) */
405   lmm_constraint_t constraint() const;
406
407 protected:
408   const lmm_constraint_t constraint_ = nullptr;
409 };
410
411 }
412 }
413
414 namespace std {
415   template <>
416   struct hash<simgrid::surf::Resource>
417   {
418     std::size_t operator()(const simgrid::surf::Resource& r) const
419     {
420       return (std::size_t) xbt_str_hash(r.cname());
421     }
422   };
423 }
424
425 #endif /* SURF_MODEL_H_ */