Logo AND Algorithmique Numérique Distribuée

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