Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
consistency between Action::ActionLmmList and Action::ActionList
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2017. 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 #include "xbt/utility.hpp"
11
12 #include "src/surf/surf_private.hpp"
13 #include "surf/surf.hpp"
14 #include "xbt/str.h"
15
16 #include <boost/heap/pairing_heap.hpp>
17 #include <boost/intrusive/list.hpp>
18 #include <boost/optional.hpp>
19 #include <cmath>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23
24 #define NO_MAX_DURATION -1.0
25
26 /*********
27  * Utils *
28  *********/
29
30 /* user-visible parameters */
31 XBT_PUBLIC_DATA(double) sg_maxmin_precision;
32 XBT_PUBLIC_DATA(double) sg_surf_precision;
33 XBT_PUBLIC_DATA(int) sg_concurrency_limit;
34
35 extern XBT_PRIVATE double sg_tcp_gamma;
36 extern XBT_PRIVATE double sg_latency_factor;
37 extern XBT_PRIVATE double sg_bandwidth_factor;
38 extern XBT_PRIVATE double sg_weight_S_parameter;
39 extern XBT_PRIVATE int sg_network_crosstraffic;
40 extern XBT_PRIVATE std::vector<std::string> surf_path;
41 extern XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
42 extern XBT_PRIVATE std::set<std::string> watched_hosts;
43
44 static inline void double_update(double* variable, double value, double precision)
45 {
46   // printf("Updating %g -= %g +- %g\n",*variable,value,precision);
47   // xbt_assert(value==0  || value>precision);
48   // Check that precision is higher than the machine-dependent size of the mantissa. If not, brutal rounding  may
49   // happen, and the precision mechanism is not active...
50   // xbt_assert(*variable< (2<<DBL_MANT_DIG)*precision && FLT_RADIX==2);
51   *variable -= value;
52   if (*variable < precision)
53     *variable = 0.0;
54 }
55
56 static inline int double_positive(double value, double precision)
57 {
58   return (value > precision);
59 }
60
61 static inline int double_equals(double value1, double value2, double precision)
62 {
63   return (fabs(value1 - value2) < precision);
64 }
65
66 extern "C" {
67 XBT_PUBLIC(double) surf_get_clock();
68 }
69 /** \ingroup SURF_simulation
70  *  \brief List of hosts that have just restarted and whose autorestart process should be restarted.
71  */
72 XBT_PUBLIC_DATA(std::vector<sg_host_t>) host_that_restart;
73
74 int XBT_PRIVATE __surf_is_absolute_file_path(const char *file_path);
75
76 /**********
77  * Action *
78  **********/
79
80 /** \ingroup SURF_models
81  *  \brief List of initialized models
82  */
83 XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
84
85 namespace simgrid {
86 namespace surf {
87
88 typedef std::pair<double, simgrid::surf::Action*> heap_element_type;
89 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
90                                   boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>
91     heap_type;
92
93 /** @ingroup SURF_interface
94  * @brief SURF action interface class
95  * @details An action is an event generated by a resource (e.g.: a communication for the network)
96  */
97 XBT_PUBLIC_CLASS Action {
98 public:
99   boost::intrusive::list_member_hook<> modifiedSetHook_; /* Used by the lazy update to list the actions to track */
100   bool isLinkedModifiedSet() const { return modifiedSetHook_.is_linked(); }
101
102   typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modifiedSetHook_>
103       ActionLmmOptions;
104   typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
105
106   boost::intrusive::list_member_hook<> stateSetHook_;
107   typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::stateSetHook_>
108       ActionOptions;
109   typedef boost::intrusive::list<Action, ActionOptions> ActionList;
110
111   enum class State {
112     ready = 0,        /**< Ready        */
113     running,          /**< Running      */
114     failed,           /**< Task Failure */
115     done,             /**< Completed    */
116     to_free,          /**< Action to free in next cleanup */
117     not_in_the_system /**< Not in the system anymore. Why did you ask ? */
118   };
119
120   enum class SuspendStates {
121     not_suspended = 0, /**< Action currently not suspended **/
122     suspended,
123     sleeping
124   };
125
126   enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
127
128   /**
129    * @brief Action constructor
130    *
131    * @param model The Model associated to this Action
132    * @param cost The cost of the Action
133    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
134    */
135   Action(simgrid::surf::Model* model, double cost, bool failed);
136
137   /**
138    * @brief Action constructor
139    *
140    * @param model The Model associated to this Action
141    * @param cost The cost of the Action
142    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
143    * @param var The lmm variable associated to this Action if it is part of a LMM component
144    */
145   Action(simgrid::surf::Model * model, double cost, bool failed, kernel::lmm::Variable* var);
146
147   virtual ~Action();
148
149   /**
150    * @brief Mark that the action is now finished
151    *
152    * @param state the new [state](\ref simgrid::surf::Action::State) of the current Action
153    */
154   void finish(Action::State state);
155
156   /** @brief Get the [state](\ref simgrid::surf::Action::State) of the current Action */
157   Action::State getState() const; /**< get the state*/
158   /** @brief Set the [state](\ref simgrid::surf::Action::State) of the current Action */
159   virtual void setState(Action::State state);
160
161   /** @brief Get the bound of the current Action */
162   double getBound() const;
163   /** @brief Set the bound of the current Action */
164   void setBound(double bound);
165
166   /** @brief Get the start time of the current action */
167   double getStartTime() const { return start_; }
168   /** @brief Get the finish time of the current action */
169   double getFinishTime() const { return finishTime_; }
170
171   /** @brief Get the user data associated to the current action */
172   void* getData() const { return data_; }
173   /** @brief Set the user data associated to the current action */
174   void setData(void* data) { data_ = data; }
175
176   /** @brief Get the cost of the current action */
177   double getCost() const { return cost_; }
178   /** @brief Set the cost of the current action */
179   void setCost(double cost) {cost_ = cost;}
180
181   /** @brief Update the maximum duration of the current action
182    *  @param delta Amount to remove from the MaxDuration */
183   void updateMaxDuration(double delta) {double_update(&maxDuration_, delta,sg_surf_precision);}
184
185   /** @brief Update the remaining time of the current action
186    *  @param delta Amount to remove from the remaining time */
187   void updateRemains(double delta) {double_update(&remains_, delta, sg_maxmin_precision*sg_surf_precision);}
188
189   /** @brief Set the remaining time of the current action */
190   void setRemains(double value) {remains_ = value;}
191   /** @brief Get the remaining time of the current action after updating the resource */
192   virtual double getRemains();
193   /** @brief Get the remaining time of the current action without updating the resource */
194   double getRemainsNoUpdate() const { return remains_; }
195
196   /** @brief Set the finish time of the current action */
197   void setFinishTime(double value) {finishTime_ = value;}
198
199   /**@brief Add a reference to the current action (refcounting) */
200   void ref();
201   /** @brief Unref that action (and destroy it if refcount reaches 0)
202    *  @return true if the action was destroyed and false if someone still has references on it
203    */
204   virtual int unref();
205
206   /** @brief Cancel the current Action if running */
207   virtual void cancel();
208
209   /** @brief Suspend the current Action */
210   virtual void suspend();
211
212   /** @brief Resume the current Action */
213   virtual void resume();
214
215   /** @brief Returns true if the current action is running */
216   virtual bool isSuspended();
217
218   /** @brief Get the maximum duration of the current action */
219   double getMaxDuration() const { return maxDuration_; }
220   /** @brief Set the maximum duration of the current Action */
221   virtual void setMaxDuration(double duration);
222
223   /** @brief Get the tracing category associated to the current action */
224   char* getCategory() const { return category_; }
225   /** @brief Set the tracing category of the current Action */
226   void setCategory(const char *category);
227
228   /** @brief Get the priority of the current Action */
229   double getPriority() const { return sharingWeight_; };
230   /** @brief Set the priority of the current Action */
231   virtual void setSharingWeight(double priority);
232   void setSharingWeightNoUpdate(double weight) { sharingWeight_ = weight; }
233
234   /** @brief Get the state set in which the action is */
235   ActionList* getStateSet() const { return stateSet_; };
236
237   simgrid::surf::Model* getModel() const { return model_; }
238
239 protected:
240   ActionList* stateSet_;
241   int    refcount_ = 1;
242
243 private:
244   double sharingWeight_ = 1.0;             /**< priority (1.0 by default) */
245   double maxDuration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
246   double remains_;                       /**< How much of that cost remains to be done in the currently running task */
247   double start_; /**< start time  */
248   char *category_ = nullptr;            /**< tracing category for categorized resource utilization monitoring */
249   double finishTime_ =
250       -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
251
252   double    cost_;
253   simgrid::surf::Model *model_;
254   void *data_ = nullptr; /**< for your convenience */
255
256   /* LMM */
257   double lastUpdate_                                  = 0;
258   double lastValue_                                   = 0;
259   kernel::lmm::Variable* variable_                    = nullptr;
260   Action::Type type_                                  = Action::Type::NOTSET;
261   boost::optional<heap_type::handle_type> heapHandle_ = boost::none;
262
263 public:
264   virtual void updateRemainingLazy(double now) { THROW_IMPOSSIBLE; };
265   void heapInsert(heap_type & heap, double key, Action::Type hat);
266   void heapRemove(heap_type& heap);
267   void heapUpdate(heap_type & heap, double key, Action::Type hat);
268   void clearHeapHandle() { heapHandle_ = boost::none; }
269   kernel::lmm::Variable* getVariable() const { return variable_; }
270   void setVariable(kernel::lmm::Variable * var) { variable_ = var; }
271   double getLastUpdate() const { return lastUpdate_; }
272   void refreshLastUpdate() {lastUpdate_ = surf_get_clock();}
273   double getLastValue() const { return lastValue_; }
274   void setLastValue(double val) { lastValue_ = val; }
275   Action::Type getType() const { return type_; }
276
277 protected:
278   Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
279 };
280
281 typedef Action::ActionList ActionList;
282 typedef Action::ActionLmmList ActionLmmList;
283 typedef Action::ActionLmmList* ActionLmmListPtr;
284
285 /*********
286  * Model *
287  *********/
288
289 /** @ingroup SURF_interface
290  * @brief SURF model interface class
291  * @details A model is an object which handle the interactions between its Resources and its Actions
292  */
293 XBT_PUBLIC_CLASS Model {
294 public:
295   Model();
296   virtual ~Model();
297
298   /** @brief Get the set of [actions](@ref Action) in *ready* state */
299   virtual ActionList* getReadyActionSet() const { return readyActionSet_; }
300
301   /** @brief Get the set of [actions](@ref Action) in *running* state */
302   virtual ActionList* getRunningActionSet() const { return runningActionSet_; }
303
304   /** @brief Get the set of [actions](@ref Action) in *failed* state */
305   virtual ActionList* getFailedActionSet() const { return failedActionSet_; }
306
307   /** @brief Get the set of [actions](@ref Action) in *done* state */
308   virtual ActionList* getDoneActionSet() const { return doneActionSet_; }
309
310   /** @brief Get the set of modified [actions](@ref Action) */
311   virtual ActionLmmListPtr getModifiedSet() const { return modifiedSet_; }
312
313   /** @brief Get the maxmin system of the current Model */
314   lmm_system_t getMaxminSystem() const { return maxminSystem_; }
315
316   /**
317    * @brief Get the update mechanism of the current Model
318    * @see e_UM_t
319    */
320   e_UM_t getUpdateMechanism() const { return updateMechanism_; }
321   void setUpdateMechanism(e_UM_t mechanism) { updateMechanism_ = mechanism; }
322
323   /** @brief Get Action heap */
324   heap_type& getActionHeap() { return actionHeap_; }
325
326   double actionHeapTopDate() const { return actionHeap_.top().first; }
327   Action* actionHeapPop();
328   bool actionHeapIsEmpty() const { return actionHeap_.empty(); }
329
330   /**
331    * @brief Share the resources between the actions
332    *
333    * @param now The current time of the simulation
334    * @return The delta of time till the next action will finish
335    */
336   virtual double nextOccuringEvent(double now);
337   virtual double nextOccuringEventLazy(double now);
338   virtual double nextOccuringEventFull(double now);
339
340   /**
341    * @brief Update action to the current time
342    *
343    * @param now The current time of the simulation
344    * @param delta The delta of time since the last update
345    */
346   virtual void updateActionsState(double now, double delta);
347   virtual void updateActionsStateLazy(double now, double delta);
348   virtual void updateActionsStateFull(double now, double delta);
349
350   /** @brief Returns whether this model have an idempotent shareResource()
351    *
352    * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
353    * so we need to call it only when the next timestamp of other sources is computed.
354    */
355   virtual bool nextOccuringEventIsIdempotent() { return true;}
356
357 protected:
358   ActionLmmListPtr modifiedSet_;
359   lmm_system_t maxminSystem_ = nullptr;
360   bool selectiveUpdate_;
361
362 private:
363   e_UM_t updateMechanism_ = UM_UNDEFINED;
364   ActionList* readyActionSet_; /**< Actions in state SURF_ACTION_READY */
365   ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
366   ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
367   ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
368   heap_type actionHeap_;
369 };
370
371 }
372 }
373
374 /************
375  * Resource *
376  ************/
377
378
379 #endif /* SURF_MODEL_H_ */