Logo AND Algorithmique Numérique Distribuée

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