Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Give a p_netcard to simgrid::Host instead of relying on extensions for that
[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::surf::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::surf::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                                    e_surf_resource_state_t state_initial,
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::surf::signal<void(simgrid::surf::Link*)> onCreation;
211
212   /** @brief Callback signal fired when a Link is destroyed.
213    *  Signature: void(Link*) */
214   static simgrid::surf::signal<void(simgrid::surf::Link*)> onDestruction;
215
216   /** @brief Callback signal fired when the state of a Link changes
217    *  Signature: `void(LinkAction *action, e_surf_resource_state_t oldState, e_surf_resource_state_t currentState)` */
218   static simgrid::surf::signal<void(simgrid::surf::Link*, e_surf_resource_state_t, e_surf_resource_state_t)> 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 setState(e_surf_resource_state_t state);
240
241   /* Using this object with the public part of
242     model does not make sense */
243   double m_latCurrent = 0;
244   tmgr_trace_event_t p_latEvent = NULL;
245
246   /* LMM */
247   tmgr_trace_event_t p_stateEvent = NULL;
248   s_surf_metric_t p_speed;
249
250   /* User data */
251 public:
252   void *getData()        { return userData;}
253   void  setData(void *d) { userData=d;}
254 private:
255   void *userData = NULL;
256
257   /* List of all links */
258 private:
259   static boost::unordered_map<std::string, Link *> *links;
260 public:
261   static Link *byName(const char* name);
262   static int linksAmount();
263   static Link **linksList();
264   static void linksExit();
265 };
266
267 /**********
268  * Action *
269  **********/
270 /** @ingroup SURF_network_interface
271  * @brief SURF network action interface class
272  * @details A NetworkAction represents a communication between two [hosts](\ref Host)
273  */
274 class NetworkAction : public simgrid::surf::Action {
275 public:
276   /** @brief 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(simgrid::surf::Model *model, double cost, bool failed)
283   : simgrid::surf::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(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
295   : simgrid::surf::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_senderSize;
314   xbt_fifo_item_t p_senderFifoItem;
315 #ifdef HAVE_LATENCY_BOUND_TRACKING
316   int m_latencyLimited;
317 #endif
318
319 };
320
321 }
322 }
323
324 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
325
326