Logo AND Algorithmique Numérique Distribuée

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