Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6c1d47d7fafb45813c890549bfd99b501c23e734
[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 <unordered_map>
13
14 #include "xbt/fifo.h"
15 #include "xbt/dict.h"
16 #include "surf_interface.hpp"
17 #include "src/surf/PropertyHolder.hpp"
18
19 #include "simgrid/link.h"
20 #include "simgrid/s4u/Link.hpp"
21
22 /***********
23  * Classes *
24  ***********/
25
26 namespace simgrid {
27   namespace surf {
28
29     class NetworkAction;
30
31     /*********
32      * Model *
33      *********/
34
35     /** @ingroup SURF_network_interface
36      * @brief SURF network model interface class
37      * @details A model is an object which handles the interactions between its Resources and its Actions
38      */
39     class NetworkModel : public Model {
40     public:
41       /** @brief Constructor */
42       NetworkModel() : Model() { }
43
44       /** @brief Destructor */
45       ~NetworkModel() override;
46
47       /**
48        * @brief Create a Link
49        *
50        * @param name The name of the Link
51        * @param bandwidth The initial bandwidth of the Link in bytes per second
52        * @param latency The initial latency of the Link in seconds
53        * @param policy The sharing policy of the Link
54        */
55       virtual LinkImpl* createLink(const char* name, double bandwidth, double latency,
56                                    e_surf_link_sharing_policy_t policy) = 0;
57
58       /**
59        * @brief Create a communication between two hosts.
60        * @details It makes calls to the routing part, and execute the communication
61        *          between the two end points.
62        *
63        * @param src The source of the communication
64        * @param dst The destination of the communication
65        * @param size The size of the communication in bytes
66        * @param rate Allows to limit the transfer rate. Negative value means
67        * unlimited.
68        * @return The action representing the communication
69        */
70       virtual Action* communicate(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double size, double rate) = 0;
71
72       /** @brief Function pointer to the function to use to solve the lmm_system_t
73        *
74        * @param system The lmm_system_t to solve
75        */
76       void (*f_networkSolve)(lmm_system_t) = lmm_solve;
77
78       /**
79        * @brief Get the right multiplicative factor for the latency.
80        * @details Depending on the model, the effective latency when sending
81        * a message might be different from the theoretical latency of the link,
82        * in function of the message size. In order to account for this, this
83        * function gets this factor.
84        *
85        * @param size The size of the message.
86        * @return The latency factor.
87        */
88       virtual double latencyFactor(double size);
89
90       /**
91        * @brief Get the right multiplicative factor for the bandwidth.
92        * @details Depending on the model, the effective bandwidth when sending
93        * a message might be different from the theoretical bandwidth of the link,
94        * in function of the message size. In order to account for this, this
95        * function gets this factor.
96        *
97        * @param size The size of the message.
98        * @return The bandwidth factor.
99        */
100       virtual double bandwidthFactor(double size);
101
102       /**
103        * @brief Get definitive bandwidth.
104        * @details It gives the minimum bandwidth between the one that would
105        * occur if no limitation was enforced, and the one arbitrary limited.
106        * @param rate The desired maximum bandwidth.
107        * @param bound The bandwidth with only the network taken into account.
108        * @param size The size of the message.
109        * @return The new bandwidth.
110        */
111       virtual double bandwidthConstraint(double rate, double bound, double size);
112       double nextOccuringEventFull(double now) override;
113
114       LinkImpl* loopback_ = nullptr;
115     };
116
117     /************
118      * Resource *
119      ************/
120     /** @ingroup SURF_network_interface
121      * @brief SURF network link interface class
122      * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
123      */
124     class LinkImpl : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder {
125     protected:
126       LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint);
127       ~LinkImpl() override;
128     public:
129       void destroy(); // Must be called instead of the destructor
130     private:
131       bool currentlyDestroying_ = false;
132
133     public:
134       /** @brief Public interface */
135       s4u::Link piface_;
136
137       /** @brief Get the bandwidth in bytes per second of current Link */
138       virtual double bandwidth();
139
140       /** @brief Update the bandwidth in bytes per second of current Link */
141       virtual void setBandwidth(double value) = 0;
142
143       /** @brief Get the latency in seconds of current Link */
144       virtual double latency();
145
146       /** @brief Update the latency in seconds of current Link */
147       virtual void setLatency(double value) = 0;
148
149       /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
150        * FULLDUPLEX) */
151       virtual int sharingPolicy();
152
153       /** @brief Check if the Link is used */
154       bool isUsed() override;
155
156       void turnOn() override;
157       void turnOff() override;
158
159       virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
160                                                           Trace must contain boolean values. */
161       virtual void setBandwidthTrace(tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
162                                                               Trace must contain percentages (value between 0 and 1). */
163       virtual void setLatencyTrace(tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
164                                                             Trace must contain absolute values */
165
166       tmgr_trace_iterator_t stateEvent_ = nullptr;
167       s_surf_metric_t latency_          = {1.0, 0, nullptr};
168       s_surf_metric_t bandwidth_        = {1.0, 0, nullptr};
169
170       /* User data */
171     public:
172       void *getData()        { return userData;}
173       void  setData(void *d) { userData=d;}
174     private:
175       void *userData = nullptr;
176
177       /* List of all links. FIXME: should move to the Engine */
178     private:
179       static std::unordered_map<std::string, LinkImpl*>* links;
180
181     public:
182       static LinkImpl* byName(const char* name);
183       static int linksCount();
184       static LinkImpl** linksList();
185       static void linksExit();
186     };
187
188     /**********
189      * Action *
190      **********/
191     /** @ingroup SURF_network_interface
192      * @brief SURF network action interface class
193      * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
194      */
195     class NetworkAction : public simgrid::surf::Action {
196     public:
197       /** @brief Constructor
198        *
199        * @param model The NetworkModel associated to this NetworkAction
200        * @param cost The cost of this  NetworkAction in [TODO]
201        * @param failed [description]
202        */
203       NetworkAction(simgrid::surf::Model *model, double cost, bool failed)
204     : simgrid::surf::Action(model, cost, failed) {}
205
206       /**
207        * @brief NetworkAction constructor
208        *
209        * @param model The NetworkModel associated to this NetworkAction
210        * @param cost The cost of this  NetworkAction in [TODO]
211        * @param failed [description]
212        * @param var The lmm variable associated to this Action if it is part of a
213        * LMM component
214        */
215       NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
216       : simgrid::surf::Action(model, cost, failed, var) {};
217
218       void setState(simgrid::surf::Action::State state) override;
219
220       double latency_;
221       double latCurrent_;
222       double weight_;
223       double rate_;
224       const char* senderLinkName_;
225       double senderSize_;
226       xbt_fifo_item_t senderFifoItem_;
227     };
228   }
229 }
230
231 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
232
233