Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SURF_MODEL_H_
8 #define SURF_MODEL_H_
9
10 #include <xbt.h>
11 #include <string>
12 #include <vector>
13 #include <memory>
14 #include <boost/function.hpp>
15 #include <boost/intrusive/list.hpp>
16 #include "surf/trace_mgr.h"
17 #include "xbt/lib.h"
18 #include "surf/surf_routing.h"
19 #include "simgrid/platf_interface.h"
20 #include "surf/surf.h"
21 #include "surf/surf_private.h"
22 #include "internal_config.h"
23
24 #ifdef LIBSIGC
25 #include <sigc++/sigc++.h>
26 #define surf_callback(arg1, ...)  sigc::signal<arg1,__VA_ARGS__>
27 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
28 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
29 #else
30 #include <boost/signals2.hpp>
31 #define surf_callback(arg1, ...)  boost::signals2::signal<arg1(__VA_ARGS__)>
32 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
33 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
34 #endif
35
36 extern tmgr_history_t history;
37 #define NO_MAX_DURATION -1.0
38
39 using namespace std;
40
41 /*********
42  * Utils *
43  *********/
44
45 /* user-visible parameters */
46 extern double sg_tcp_gamma;
47 extern double sg_sender_gap;
48 extern double sg_latency_factor;
49 extern double sg_bandwidth_factor;
50 extern double sg_weight_S_parameter;
51 extern int sg_network_crosstraffic;
52 extern xbt_dynar_t surf_path;
53
54 extern "C" {
55 XBT_PUBLIC(double) surf_get_clock(void);
56 }
57
58 extern double sg_sender_gap;
59
60 extern surf_callback(void, void) surfExitCallbacks;
61
62 int __surf_is_absolute_file_path(const char *file_path);
63
64 /***********
65  * Classes *
66  ***********/
67 typedef boost::intrusive::list<Action> ActionList;
68 typedef ActionList* ActionListPtr;
69 typedef boost::intrusive::list_base_hook<> actionHook;
70
71 struct lmmTag;
72 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
73 typedef ActionLmmList* ActionLmmListPtr;
74 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
75
76
77 enum heap_action_type{
78   LATENCY = 100,
79   MAX_DURATION,
80   NORMAL,
81   NOTSET
82 };
83
84 /*********
85  * Trace *
86  *********/
87 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
88 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
89 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
90 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
91 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
92 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
93 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
94
95 /*********
96  * Model *
97  *********/
98 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
99
100 /** @ingroup SURF_interface
101  * @brief SURF model interface class
102  * @details A model is an object which handle the interactions between its Resources and its Actions
103  */
104 XBT_PUBLIC_CLASS Model {
105 public:
106   /** @brief Constructor */
107   Model();
108   /** @brief Destructor */
109   virtual ~Model();
110
111   virtual void addTraces() =0;
112
113   /**
114    * @brief Get the set of [actions](@ref Action) in *ready* state
115    *
116    * @return The set of [actions](@ref Action) in *ready* state
117    */
118   virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
119
120   /**
121    * @brief Get the set of [actions](@ref Action) in *running* state
122    *
123    * @return The set of [actions](@ref Action) in *running* state
124    */
125   virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
126
127   /**
128    * @brief Get the set of [actions](@ref Action) in *failed* state
129    *
130    * @return The set of [actions](@ref Action) in *failed* state
131    */
132   virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
133
134   /**
135    * @brief Get the set of [actions](@ref Action) in *done* state
136    *
137    * @return The set of [actions](@ref Action) in *done* state
138    */
139   virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
140
141   /**
142    * @brief Get the set of modified [actions](@ref Action)
143    *
144    * @return The set of modified [actions](@ref Action)
145    */
146   virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
147
148   /**
149    * @brief Get the maxmin system of the current Model
150    *
151    * @return The maxmin system of the current Model
152    */
153   lmm_system_t getMaxminSystem() {return p_maxminSystem;}
154
155   /**
156    * @brief Get the update mechanism of the current Model
157    * @see e_UM_t
158    *
159    * @return [description]
160    */
161   e_UM_t getUpdateMechanism() {return p_updateMechanism;}
162
163   /**
164    * @brief Get Action heap
165    * @details [TODO]
166    *
167    * @return The Action heap
168    */
169   xbt_heap_t getActionHeap() {return p_actionHeap;}
170
171   /**
172    * @brief share the resources
173    * @details Share the resources between the actions
174    *
175    * @param now The current time of the simulation
176    * @return The delta of time till the next action will finish
177    */
178   virtual double shareResources(double now);
179   virtual double shareResourcesLazy(double now);
180   virtual double shareResourcesFull(double now);
181   double shareResourcesMaxMin(ActionListPtr running_actions,
182                                       lmm_system_t sys,
183                                       void (*solve) (lmm_system_t));
184
185   /**
186    * @brief Update state of actions
187    * @details Update action to the current time
188    *
189    * @param now The current time of the simulation
190    * @param delta The delta of time since the last update
191    */
192   virtual void updateActionsState(double now, double delta);
193   virtual void updateActionsStateLazy(double now, double delta);
194   virtual void updateActionsStateFull(double now, double delta);
195
196   /** @brief Returns whether this model have an idempotent shareResource()
197    *
198    * The only model that is not is NS3: computing the next timestamp moves the model up to that point,
199    * so we need to call it only when the next timestamp of other sources is computed.
200    */
201   virtual bool shareResourcesIsIdempotent()=0;
202
203 protected:
204   ActionLmmListPtr p_modifiedSet;
205   lmm_system_t p_maxminSystem;
206   e_UM_t p_updateMechanism;
207   int m_selectiveUpdate;
208   xbt_heap_t p_actionHeap;
209
210 private:
211   ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
212   ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
213   ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
214   ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
215 };
216
217 /************
218  * Resource *
219  ************/
220
221 /** @ingroup SURF_interface
222  * @brief Resource which have a metric handled by a maxmin system
223  */
224 typedef struct {
225   double scale;             /**< The scale of the metric */
226   double peak;              /**< The peak of the metric */
227   tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
228 } s_surf_metric_t;
229
230 /** @ingroup SURF_interface
231  * @brief SURF resource interface class
232  * @details A resource represent an element of a component (e.g.: a link for the network)
233  */
234 XBT_PUBLIC_CLASS Resource {
235 public:
236   /**
237    * @brief Resource constructor
238    */
239   Resource();
240
241   /**
242    * @brief Resource constructor
243    *
244    * @param model Model associated to this Resource
245    * @param name The name of the Resource
246    * @param props Dictionary of properties associated to this Resource
247    */
248   Resource(Model *model, const char *name, xbt_dict_t props);
249
250   /**
251    * @brief Resource constructor
252    *
253    * @param model Model associated to this Resource
254    * @param name The name of the Resource
255    * @param props Dictionary of properties associated to this Resource
256    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
257    */
258   Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
259   /**
260    * @brief Resource constructor
261    *
262    * @param model Model associated to this Resource
263    * @param name The name of the Resource
264    * @param props Dictionary of properties associated to this Resource
265    * @param stateInit the initial state of the Resource
266    */
267   Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
268
269   /**
270    * @brief Resource destructor
271    */
272   virtual ~Resource();
273
274   /**
275    * @brief Get the Model of the current Resource
276    *
277    * @return The Model of the current Resource
278    */
279   Model *getModel();
280
281   /**
282    * @brief Get the name of the current Resource
283    *
284    * @return The name of the current Resource
285    */
286   const char *getName();
287
288   /**
289    * @brief Get the properties of the current Resource
290    *
291    * @return The properties of the current Resource
292    */
293   virtual xbt_dict_t getProperties();
294
295   /**
296    * @brief Update the state of the current Resource
297    * @details [TODO]
298    *
299    * @param event_type [TODO]
300    * @param value [TODO]
301    * @param date [TODO]
302    */
303   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
304
305   /**
306    * @brief Check if the current Resource is used
307    * @return true if the current Resource is used, false otherwise
308    */
309   virtual bool isUsed()=0;
310
311   /**
312    * @brief Check if the current Resource is active
313    *
314    * @return true if the current Resource is active, false otherwise
315    */
316   bool isOn();
317
318   /**
319    * @brief Turn on the current Resource
320    */
321   void turnOn();
322
323   /**
324    * @brief Turn off the current Resource
325    */
326   void turnOff();
327
328   /**
329    * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
330    *
331    * @return The state of the currenrt Resource
332    */
333   virtual e_surf_resource_state_t getState();
334
335   /**
336    * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
337    *
338    * @param state The new state of the current Resource
339    */
340   virtual void setState(e_surf_resource_state_t state);
341
342 private:
343   const char *p_name;
344   xbt_dict_t p_properties;
345   Model *p_model;
346   bool m_running;
347   e_surf_resource_state_t m_stateCurrent;
348
349   /* LMM */
350 public:
351   /**
352    * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
353    *
354    * @return The lmm constraint associated to this Resource
355    */
356   lmm_constraint_t getConstraint();
357 private:
358   lmm_constraint_t p_constraint;
359 };
360
361 /**********
362  * Action *
363  **********/
364 void surf_action_lmm_update_index_heap(void *action, int i);
365
366 /** @ingroup SURF_interface
367  * @brief SURF action interface class
368  * @details An action is an event generated by a resource (e.g.: a communication for the network)
369  */
370 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
371 private:
372   /**
373    * @brief Common initializations for the constructors
374    */
375   void initialize(Model *model, double cost, bool failed,
376                   lmm_variable_t var = NULL);
377
378 public:
379   /**
380    * @brief Action constructor
381    *
382    * @param model The Model associated to this Action
383    * @param cost The cost of the Action
384    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
385    */
386   Action(Model *model, double cost, bool failed);
387
388   /**
389    * @brief Action constructor
390    *
391    * @param model The Model associated to this Action
392    * @param cost The cost of the Action
393    * @param failed If the action is impossible (e.g.: execute something on a switched off host)
394    * @param var The lmm variable associated to this Action if it is part of a LMM component
395    */
396   Action(Model *model, double cost, bool failed, lmm_variable_t var);
397
398   /** @brief Destructor */
399   virtual ~Action();
400
401   /** @brief Mark that the action is now finished */
402   void finish();
403
404   /** @brief Get the [state](\ref e_surf_action_state_t) of the current Action */
405   e_surf_action_state_t getState(); /**< get the state*/
406   /** @brief Set the [state](\ref e_surf_action_state_t) of the current Action */
407   virtual void setState(e_surf_action_state_t state);
408
409   /** @brief Get the bound of the current Action */
410   double getBound();
411   /** @brief Set the bound of the current Action */
412   void setBound(double bound);
413
414   /** @brief Get the start time of the current action */
415   double getStartTime();
416   /** @brief Get the finish time of the current action */
417   double getFinishTime();
418
419   /** @brief Get the user data associated to the current action */
420   void *getData() {return p_data;}
421   /** @brief Set the user data associated to the current action */
422   void setData(void* data);
423
424   /** @brief Get the cost of the current action */
425   double getCost() {return m_cost;}
426   /** @brief Set the cost of the current action */
427   void setCost(double cost) {m_cost = cost;}
428
429   /** @brief Update the maximum duration of the current action
430    *  @param delta Amount to remove from the MaxDuration */
431   void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
432
433   /** @brief Update the remaining time of the current action
434    *  @param delta Amount to remove from the remaining time */
435   void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
436
437   /** @brief Set the remaining time of the current action */
438   void setRemains(double value) {m_remains = value;}
439   /** @brief Get the remaining time of the current action after updating the resource */
440   virtual double getRemains();
441   /** @brief Get the remaining time of the current action without updating the resource */
442   double getRemainsNoUpdate();
443
444   /** @brief Set the finish time of the current action */
445   void setFinishTime(double value) {m_finish = value;}
446
447   /**@brief Add a reference to the current action (refcounting) */
448   void ref();
449   /** @brief Unref that action (and destroy it if refcount reaches 0)
450    *  @return true if the action was destroyed and false if someone still has references on it
451    */
452   virtual int unref();
453
454   /** @brief Cancel the current Action if running */
455   virtual void cancel();
456
457   /** @brief Suspend the current Action */
458   virtual void suspend();
459
460   /** @brief Resume the current Action */
461   virtual void resume();
462
463   /** @brief Returns true if the current action is running */
464   virtual bool isSuspended();
465
466   /** @brief Get the maximum duration of the current action */
467   double getMaxDuration() {return m_maxDuration;}
468   /** @brief Set the maximum duration of the current Action */
469   virtual void setMaxDuration(double duration);
470
471   /** @brief Get the tracing category associated to the current action */
472   char *getCategory() {return p_category;}
473   /** @brief Set the tracing category of the current Action */
474   void setCategory(const char *category);
475
476   /** @brief Get the priority of the current Action */
477   double getPriority() {return m_priority;};
478   /** @brief Set the priority of the current Action */
479   virtual void setPriority(double priority);
480
481   /** @brief Get the state set in which the action is */
482   ActionListPtr getStateSet() {return p_stateSet;};
483
484   s_xbt_swag_hookup_t p_stateHookup;
485
486   Model *getModel() {return p_model;}
487
488 protected:
489   ActionListPtr p_stateSet;
490   double m_priority; /**< priority (1.0 by default) */
491   int    m_refcount;
492   double m_remains; /**< How much of that cost remains to be done in the currently running task */
493   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
494   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
495
496 private:
497   double m_start; /**< start time  */
498   char *p_category;               /**< tracing category for categorized resource utilization monitoring */
499
500   #ifdef HAVE_LATENCY_BOUND_TRACKING
501   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
502   #endif
503   double    m_cost;
504   Model *p_model;
505   void *p_data; /**< for your convenience */
506
507   /* LMM */
508 public:
509   virtual void updateRemainingLazy(double now);
510   void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
511   void heapRemove(xbt_heap_t heap);
512   void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
513   void updateIndexHeap(int i);
514   lmm_variable_t getVariable() {return p_variable;}
515   double getLastUpdate() {return m_lastUpdate;}
516   void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
517   enum heap_action_type getHat() {return m_hat;}
518   bool is_linked() {return actionLmmHook::is_linked();}
519   void gapRemove();
520
521 protected:
522   lmm_variable_t p_variable;
523   double m_lastValue;
524   double m_lastUpdate;
525   int m_suspended;
526   int m_indexHeap;
527   enum heap_action_type m_hat;
528 };
529
530 #endif /* SURF_MODEL_H_ */