Logo AND Algorithmique Numérique Distribuée

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