Logo AND Algorithmique Numérique Distribuée

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