Logo AND Algorithmique Numérique Distribuée

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