Logo AND Algorithmique Numérique Distribuée

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