Logo AND Algorithmique Numérique Distribuée

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