Logo AND Algorithmique Numérique Distribuée

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