Logo AND Algorithmique Numérique Distribuée

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