Logo AND Algorithmique Numérique Distribuée

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