Logo AND Algorithmique Numérique Distribuée

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