Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 /*********
54  * Model *
55  *********/
56
57 namespace simgrid {
58 namespace surf {
59
60 /** @ingroup SURF_network_interface
61  * @brief SURF network model interface class
62  * @details A model is an object which handles the interactions between its Resources and its Actions
63  */
64 class NetworkModel : public Model {
65 public:
66   /** @brief Constructor */
67   NetworkModel() : Model() { }
68
69   /** @brief Destructor */
70   ~NetworkModel() {
71   if (p_maxminSystem)
72     lmm_system_free(p_maxminSystem);
73   if (p_actionHeap)
74     xbt_heap_free(p_actionHeap);
75   if (p_modifiedSet)
76     delete p_modifiedSet;
77   }
78
79   /**
80    * @brief Create a Link
81    *
82    * @param name The name of the Link
83    * @param bw_initial The initial bandwidth of the Link in bytes per second
84    * @param bw_trace The trace associated to the Link bandwidth
85    * @param lat_initial The initial latency of the Link in seconds
86    * @param lat_trace The trace associated to the Link latency
87    * @param state_trace The trace associated to the Link (state)[e_surf_resource_state_t]
88    * @param policy The sharing policy of the Link
89    * @param properties Dictionary of properties associated to this Resource
90    * @return The created Link
91    */
92   virtual Link* createLink(const char *name,
93                                    double bw_initial,
94                                    tmgr_trace_t bw_trace,
95                                    double lat_initial,
96                                    tmgr_trace_t lat_trace,
97                                    tmgr_trace_t state_trace,
98                                    e_surf_link_sharing_policy_t policy,
99                                    xbt_dict_t properties)=0;
100
101   /**
102    * @brief Create a communication between two hosts.
103    * @details It makes calls to the routing part, and execute the communication
104    * between the two end points.
105    *
106    * @param src The source of the communication
107    * @param dst The destination of the communication
108    * @param size The size of the communication in bytes
109    * @param rate Allows to limit the transfer rate. Negative value means
110    * unlimited.
111    * @return The action representing the communication
112    */
113   virtual Action *communicate(NetCard *src, NetCard *dst,
114                                double size, double rate)=0;
115
116   /** @brief Function pointer to the function to use to solve the lmm_system_t
117    *
118    * @param system The lmm_system_t to solve
119    */
120   void (*f_networkSolve)(lmm_system_t) = lmm_solve;
121
122   /**
123    * @brief Get the right multiplicative factor for the latency.
124    * @details Depending on the model, the effective latency when sending
125    * a message might be different from the theoretical latency of the link,
126    * in function of the message size. In order to account for this, this
127    * function gets this factor.
128    *
129    * @param size The size of the message.
130    * @return The latency factor.
131    */
132   virtual double latencyFactor(double size);
133
134   /**
135    * @brief Get the right multiplicative factor for the bandwidth.
136    * @details Depending on the model, the effective bandwidth when sending
137    * a message might be different from the theoretical bandwidth 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 bandwidth factor.
143    */
144   virtual double bandwidthFactor(double size);
145
146   /**
147    * @brief Get definitive bandwidth.
148    * @details It gives the minimum bandwidth between the one that would
149    * occur if no limitation was enforced, and the one arbitrary limited.
150    * @param rate The desired maximum bandwidth.
151    * @param bound The bandwidth with only the network taken into account.
152    * @param size The size of the message.
153    * @return The new bandwidth.
154    */
155   virtual double bandwidthConstraint(double rate, double bound, double size);
156   double next_occuring_event_full(double now) override;
157 };
158
159 /************
160  * Resource *
161  ************/
162  /** @ingroup SURF_network_interface
163   * @brief SURF network link interface class
164   * @details A Link represents the link between two [hosts](\ref Host)
165   */
166 class Link :
167     public simgrid::surf::Resource,
168     public simgrid::surf::PropertyHolder {
169 public:
170   /**
171    * @brief Link constructor
172    *
173    * @param model The NetworkModel associated to this Link
174    * @param name The name of the Link
175    * @param props Dictionary of properties associated to this Link
176    */
177   Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props);
178
179   /**
180    * @brief Link constructor
181    *
182    * @param model The NetworkModel associated to this Link
183    * @param name The name of the Link
184    * @param props Dictionary of properties associated to this Link
185    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
186    * @param state_trace [TODO]
187    */
188   Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props,
189               lmm_constraint_t constraint,
190               tmgr_trace_t state_trace);
191
192   /* Link destruction logic */
193   /**************************/
194 protected:
195   ~Link();
196 public:
197   void destroy(); // Must be called instead of the destructor
198 private:
199   bool currentlyDestroying_ = false;
200
201 public:
202   /** @brief Callback signal fired when a new Link is created.
203    *  Signature: void(Link*) */
204   static simgrid::xbt::signal<void(simgrid::surf::Link*)> onCreation;
205
206   /** @brief Callback signal fired when a Link is destroyed.
207    *  Signature: void(Link*) */
208   static simgrid::xbt::signal<void(simgrid::surf::Link*)> onDestruction;
209
210   /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off)
211    *  Signature: `void(Link*)` */
212   static simgrid::xbt::signal<void(simgrid::surf::Link*)> onStateChange;
213
214
215   /** @brief Get the bandwidth in bytes per second of current Link */
216   virtual double getBandwidth();
217
218   /** @brief Update the bandwidth in bytes per second of current Link */
219   virtual void updateBandwidth(double value)=0;
220
221   /** @brief Get the latency in seconds of current Link */
222   virtual double getLatency();
223
224   /** @brief Update the latency in seconds of current Link */
225   virtual void updateLatency(double value)=0;
226
227   /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2: FULLDUPLEX) */
228   virtual int sharingPolicy();
229
230   /** @brief Check if the Link is used */
231   bool isUsed() override;
232
233   void turnOn() override;
234   void turnOff() override;
235
236   virtual void set_state_trace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values. */
237   virtual void set_bandwidth_trace(tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load). Trace must contain percentages (value between 0 and 1). */
238   virtual void set_latency_trace(tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load). Trace must contain absolute values */
239
240   tmgr_trace_iterator_t m_stateEvent = NULL;
241   s_surf_metric_t m_latency = {1.0,0,NULL};
242   s_surf_metric_t m_bandwidth = {1.0,0,NULL};
243
244   /* User data */
245 public:
246   void *getData()        { return userData;}
247   void  setData(void *d) { userData=d;}
248 private:
249   void *userData = NULL;
250
251   /* List of all links */
252 private:
253   static boost::unordered_map<std::string, Link *> *links;
254 public:
255   static Link *byName(const char* name);
256   static int linksCount();
257   static Link **linksList();
258   static void linksExit();
259 };
260
261 /**********
262  * Action *
263  **********/
264 /** @ingroup SURF_network_interface
265  * @brief SURF network action interface class
266  * @details A NetworkAction represents a communication between two [hosts](\ref Host)
267  */
268 class NetworkAction : public simgrid::surf::Action {
269 public:
270   /** @brief Constructor
271    *
272    * @param model The NetworkModel associated to this NetworkAction
273    * @param cost The cost of this  NetworkAction in [TODO]
274    * @param failed [description]
275    */
276   NetworkAction(simgrid::surf::Model *model, double cost, bool failed)
277   : simgrid::surf::Action(model, cost, failed) {}
278
279   /**
280    * @brief NetworkAction constructor
281    *
282    * @param model The NetworkModel associated to this NetworkAction
283    * @param cost The cost of this  NetworkAction in [TODO]
284    * @param failed [description]
285    * @param var The lmm variable associated to this Action if it is part of a
286    * LMM component
287    */
288   NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
289   : simgrid::surf::Action(model, cost, failed, var) {};
290
291   void setState(e_surf_action_state_t state);
292
293 #ifdef HAVE_LATENCY_BOUND_TRACKING
294   /**
295    * @brief Check if the action is limited by latency.
296    *
297    * @return 1 if action is limited by latency, 0 otherwise
298    */
299   virtual int getLatencyLimited() {return m_latencyLimited;}
300 #endif
301
302   double m_latency;
303   double m_latCurrent;
304   double m_weight;
305   double m_rate;
306   const char* p_senderLinkName;
307   double m_senderSize;
308   xbt_fifo_item_t p_senderFifoItem;
309 #ifdef HAVE_LATENCY_BOUND_TRACKING
310   int m_latencyLimited;
311 #endif
312
313 };
314
315 }
316 }
317
318 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
319
320