Logo AND Algorithmique Numérique Distribuée

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