Logo AND Algorithmique Numérique Distribuée

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