Logo AND Algorithmique Numérique Distribuée

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