Logo AND Algorithmique Numérique Distribuée

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