Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into S4U
[simgrid.git] / src / surf / network_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_NETWORK_INTERFACE_HPP_
8 #define SURF_NETWORK_INTERFACE_HPP_
9
10 #include <boost/unordered_map.hpp>
11
12 #include "xbt/fifo.h"
13 #include "xbt/dict.h"
14 #include "surf_interface.hpp"
15 #include "surf_routing.hpp"
16
17 #include "simgrid/link.h"
18
19 /***********
20  * Classes *
21  ***********/
22 class NetworkModel;
23 class NetworkAction;
24
25 /*************
26  * Callbacks *
27  *************/
28
29 /** @ingroup SURF_callbacks
30  * @brief Callbacks handler which emits the callbacks after Link creation
31  * @details Callback functions have the following signature: `void(Link*)`
32  */
33 XBT_PUBLIC_DATA( surf_callback(void, Link*)) networkLinkCreatedCallbacks;
34
35 /** @ingroup SURF_callbacks
36  * @brief Callbacks handler which emits the callbacks after Link destruction
37  * @details Callback functions have the following signature: `void(Link*)`
38  */
39 XBT_PUBLIC_DATA( surf_callback(void, Link*)) networkLinkDestructedCallbacks;
40
41 /** @ingroup SURF_callbacks
42  * @brief Callbacks handler which emits the callbacks after Link State changed
43  * @details Callback functions have the following signature: `void(LinkAction *action, e_surf_resource_state_t old, e_surf_resource_state_t current)`
44  */
45 XBT_PUBLIC_DATA( surf_callback(void, Link*, e_surf_resource_state_t, e_surf_resource_state_t)) networkLinkStateChangedCallbacks;
46
47 /** @ingroup SURF_callbacks
48  * @brief Callbacks handler which emits the callbacks after NetworkAction State changed
49  * @details Callback functions have the following signature: `void(NetworkAction *action, e_surf_action_state_t old, e_surf_action_state_t current)`
50  */
51 XBT_PUBLIC_DATA( surf_callback(void, NetworkAction*, e_surf_action_state_t, e_surf_action_state_t)) networkActionStateChangedCallbacks;
52
53 /** @ingroup SURF_callbacks
54  * @brief Callbacks handler which emits the callbacks after communication created
55  * @details Callback functions have the following signature: `void(NetworkAction *action, RoutingEdge *src, RoutingEdge *dst, double size, double rate)`
56  */
57 XBT_PUBLIC_DATA( surf_callback(void, NetworkAction*, RoutingEdge *src, RoutingEdge *dst, double size, double rate)) networkCommunicateCallbacks;
58
59 /*********
60  * Tools *
61  *********/
62 XBT_PUBLIC(void) netlink_parse_init(sg_platf_link_cbarg_t link);
63
64 XBT_PUBLIC(void) net_add_traces();
65
66 /*********
67  * Model *
68  *********/
69 /** @ingroup SURF_network_interface
70  * @brief SURF network model interface class
71  * @details A model is an object which handles the interactions between its Resources and its Actions
72  */
73 class NetworkModel : public Model {
74 public:
75   /** @brief Constructor */
76   NetworkModel() : Model() { }
77
78   /** @brief Destructor */
79   ~NetworkModel() {
80         if (p_maxminSystem)
81           lmm_system_free(p_maxminSystem);
82         if (p_actionHeap)
83           xbt_heap_free(p_actionHeap);
84         if (p_modifiedSet)
85           delete p_modifiedSet;
86   }
87
88   /**
89    * @brief Create a Link
90    *
91    * @param name The name of the Link
92    * @param bw_initial The initial bandwidth of the Link in bytes per second
93    * @param bw_trace The trace associated to the Link bandwidth
94    * @param lat_initial The initial latency of the Link in seconds
95    * @param lat_trace The trace associated to the Link latency
96    * @param state_initial The initial Link (state)[e_surf_resource_state_t]
97    * @param state_trace The trace associated to the Link (state)[e_surf_resource_state_t]
98    * @param policy The sharing policy of the Link
99    * @param properties Dictionary of properties associated to this Resource
100    * @return The created Link
101    */
102   virtual Link* createLink(const char *name,
103                                    double bw_initial,
104                                    tmgr_trace_t bw_trace,
105                                    double lat_initial,
106                                    tmgr_trace_t lat_trace,
107                                    e_surf_resource_state_t state_initial,
108                                    tmgr_trace_t state_trace,
109                                    e_surf_link_sharing_policy_t policy,
110                                    xbt_dict_t properties)=0;
111
112   virtual void gapAppend(double /*size*/, const Link* /*link*/, NetworkAction */*action*/) {};
113
114   /**
115    * @brief Create a communication between two hosts.
116    * @details It makes calls to the routing part, and execute the communication
117    * between the two end points.
118    *
119    * @param src The source of the communication
120    * @param dst The destination of the communication
121    * @param size The size of the communication in bytes
122    * @param rate Allows to limit the transfer rate. Negative value means
123    * unlimited.
124    * @return The action representing the communication
125    */
126   virtual Action *communicate(RoutingEdge *src, RoutingEdge *dst,
127                                            double size, double rate)=0;
128
129   /** @brief Function pointer to the function to use to solve the lmm_system_t
130    *
131    * @param system The lmm_system_t to solve
132    */
133   void (*f_networkSolve)(lmm_system_t) = lmm_solve;
134
135   /**
136    * @brief Get the right multiplicative factor for the latency.
137    * @details Depending on the model, the effective latency when sending
138    * a message might be different from the theoretical latency of the link,
139    * in function of the message size. In order to account for this, this
140    * function gets this factor.
141    *
142    * @param size The size of the message.
143    * @return The latency factor.
144    */
145   virtual double latencyFactor(double size);
146
147   /**
148    * @brief Get the right multiplicative factor for the bandwidth.
149    * @details Depending on the model, the effective bandwidth when sending
150    * a message might be different from the theoretical bandwidth of the link,
151    * in function of the message size. In order to account for this, this
152    * function gets this factor.
153    *
154    * @param size The size of the message.
155    * @return The bandwidth factor.
156    */
157   virtual double bandwidthFactor(double size);
158
159   /**
160    * @brief Get definitive bandwidth.
161    * @details It gives the minimum bandwidth between the one that would
162    * occur if no limitation was enforced, and the one arbitrary limited.
163    * @param rate The desired maximum bandwidth.
164    * @param bound The bandwidth with only the network taken into account.
165    * @param size The size of the message.
166    * @return The new bandwidth.
167    */
168   virtual double bandwidthConstraint(double rate, double bound, double size);
169   double shareResourcesFull(double now);
170   bool m_haveGap = false;
171 };
172
173 /************
174  * Resource *
175  ************/
176  /** @ingroup SURF_network_interface
177   * @brief SURF network link interface class
178   * @details A Link represents the link between two [hosts](\ref Host)
179   */
180 class Link : public Resource {
181 public:
182   /**
183    * @brief Link constructor
184    *
185    * @param model The NetworkModel associated to this Link
186    * @param name The name of the Link
187    * @param props Dictionary of properties associated to this Link
188    */
189   Link(NetworkModel *model, const char *name, xbt_dict_t props);
190
191   /**
192    * @brief Link constructor
193    *
194    * @param model The NetworkModel associated to this Link
195    * @param name The name of the Link
196    * @param props Dictionary of properties associated to this Link
197    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
198    * @param history [TODO]
199    * @param state_trace [TODO]
200    */
201   Link(NetworkModel *model, const char *name, xbt_dict_t props,
202               lmm_constraint_t constraint,
203               tmgr_history_t history,
204               tmgr_trace_t state_trace);
205
206   /** @brief Link destructor */
207   ~Link();
208
209   /** @brief Get the bandwidth in bytes per second of current Link */
210   virtual double getBandwidth();
211
212   /** @brief Update the bandwidth in bytes per second of current Link */
213   virtual void updateBandwidth(double value, double date=surf_get_clock())=0;
214
215   /** @brief Get the latency in seconds of current Link */
216   virtual double getLatency();
217
218   /** @brief Update the latency in seconds of current Link */
219   virtual void updateLatency(double value, double date=surf_get_clock())=0;
220
221   /**
222    * @brief Check if the Link is shared
223    *
224    * @return true if the current NetwokrLink is shared, false otherwise
225    */
226   virtual bool isShared();
227
228   /** @brief Check if the Link is used */
229   bool isUsed();
230
231   void setState(e_surf_resource_state_t state);
232
233   /* Using this object with the public part of
234     model does not make sense */
235   double m_latCurrent;
236   tmgr_trace_event_t p_latEvent;
237
238   /* LMM */
239   tmgr_trace_event_t p_stateEvent = NULL;
240   s_surf_metric_t p_power;
241
242   /* User data */
243 public:
244   void *getData()        { return userData;}
245   void  setData(void *d) { userData=d;}
246 private:
247   void *userData = NULL;
248
249   /* List of all links */
250 private:
251   static boost::unordered_map<std::string, Link *> *links;
252 public:
253   static Link *byName(const char* name);
254   static int linksAmount();
255   static Link **linksList();
256   static void linksExit();
257 };
258
259 /**********
260  * Action *
261  **********/
262 /** @ingroup SURF_network_interface
263  * @brief SURF network action interface class
264  * @details A NetworkAction represents a communication between two [hosts](\ref Host)
265  */
266 class NetworkAction : public Action {
267 public:
268   /** @brief Constructor
269    *
270    * @param model The NetworkModel associated to this NetworkAction
271    * @param cost The cost of this  NetworkAction in [TODO]
272    * @param failed [description]
273    */
274   NetworkAction(Model *model, double cost, bool failed)
275   : Action(model, cost, failed) {}
276
277   /**
278    * @brief NetworkAction constructor
279    *
280    * @param model The NetworkModel associated to this NetworkAction
281    * @param cost The cost of this  NetworkAction in [TODO]
282    * @param failed [description]
283    * @param var The lmm variable associated to this Action if it is part of a
284    * LMM component
285    */
286   NetworkAction(Model *model, double cost, bool failed, lmm_variable_t var)
287   : Action(model, cost, failed, var) {};
288
289   void setState(e_surf_action_state_t state);
290
291 #ifdef HAVE_LATENCY_BOUND_TRACKING
292   /**
293    * @brief Check if the action is limited by latency.
294    *
295    * @return 1 if action is limited by latency, 0 otherwise
296    */
297   virtual int getLatencyLimited() {return m_latencyLimited;}
298 #endif
299
300   double m_latency;
301   double m_latCurrent;
302   double m_weight;
303   double m_rate;
304   const char* p_senderLinkName;
305   double m_senderGap;
306   double m_senderSize;
307   xbt_fifo_item_t p_senderFifoItem;
308 #ifdef HAVE_LATENCY_BOUND_TRACKING
309   int m_latencyLimited;
310 #endif
311
312 };
313
314 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
315
316