Logo AND Algorithmique Numérique Distribuée

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