Logo AND Algorithmique Numérique Distribuée

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