Logo AND Algorithmique Numérique Distribuée

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