Logo AND Algorithmique Numérique Distribuée

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