Logo AND Algorithmique Numérique Distribuée

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