Logo AND Algorithmique Numérique Distribuée

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