Logo AND Algorithmique Numérique Distribuée

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