Logo AND Algorithmique Numérique Distribuée

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