Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3917de1168d95ed222646006e7fc4cee362e0e24
[simgrid.git] / src / surf / network_interface.hpp
1 /* Copyright (c) 2004-2021. 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/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/NetworkModelIntf.hpp"
11 #include "simgrid/kernel/resource/Resource.hpp"
12 #include "simgrid/s4u/Link.hpp"
13 #include "src/kernel/lmm/maxmin.hpp"
14 #include <xbt/PropertyHolder.hpp>
15
16 #include <list>
17 #include <unordered_map>
18
19 /***********
20  * Classes *
21  ***********/
22
23 namespace simgrid {
24 namespace kernel {
25 namespace resource {
26 /*********
27  * Model *
28  *********/
29
30 /** @ingroup SURF_network_interface
31  * @brief SURF network model interface class
32  * @details A model is an object which handles the interactions between its Resources and its Actions
33  */
34 class NetworkModel : public Model, public NetworkModelIntf {
35 public:
36   static config::Flag<double> cfg_tcp_gamma;
37   static config::Flag<bool> cfg_crosstraffic;
38
39   using Model::Model;
40   NetworkModel(const NetworkModel&) = delete;
41   NetworkModel& operator=(const NetworkModel&) = delete;
42   ~NetworkModel() override;
43
44   /**
45    * @brief Create a [WiFi]Link
46    *
47    * @param name The name of the Link
48    * @param bandwidths The vector of bandwidths of the Link in bytes per second
49    */
50   virtual LinkImpl* create_link(const std::string& name, const std::vector<double>& bandwidths) = 0;
51
52   virtual LinkImpl* create_wifi_link(const std::string& name, const std::vector<double>& bandwidths) = 0;
53
54   /**
55    * @brief Create a communication between two hosts.
56    * @details It makes calls to the routing part, and execute the communication between the two end points.
57    *
58    * @param src The source of the communication
59    * @param dst The destination of the communication
60    * @param size The size of the communication in bytes
61    * @param rate Allows to limit the transfer rate. Negative value means unlimited.
62    * @return The action representing the communication
63    */
64   virtual Action* communicate(s4u::Host* src, s4u::Host* dst, double size, double rate) = 0;
65
66   /**
67    * @brief Get the right multiplicative factor for the latency.
68    * @details Depending on the model, the effective latency when sending a message might be different from the
69    * theoretical latency of the link, in function of the message size. In order to account for this, this function gets
70    * this factor.
71    *
72    * @param size The size of the message.
73    * @return The latency factor.
74    */
75   virtual double get_latency_factor(double /* size */) { return sg_latency_factor; }
76
77   /**
78    * @brief Get the right multiplicative factor for the bandwidth.
79    * @details Depending on the model, the effective bandwidth when sending a message might be different from the
80    * theoretical bandwidth of the link, in function of the message size. In order to account for this, this function
81    * gets this factor.
82    *
83    * @param size The size of the message.
84    * @return The bandwidth factor.
85    */
86   virtual double get_bandwidth_factor(double /* size*/) { return sg_bandwidth_factor; }
87
88   double next_occurring_event_full(double now) override;
89
90   void set_lat_factor_cb(const std::function<NetworkFactorCb>& cb) override { THROW_UNIMPLEMENTED; }
91   void set_bw_factor_cb(const std::function<NetworkFactorCb>& cb) override { THROW_UNIMPLEMENTED; }
92
93   LinkImpl* loopback_ = nullptr;
94 };
95
96 /************
97  * Resource *
98  ************/
99 /** @ingroup SURF_network_interface
100  * @brief SURF network link interface class
101  * @details A Link represents the link between two [hosts](@ref simgrid::surf::HostImpl)
102  */
103 class LinkImpl : public Resource_T<LinkImpl>, public xbt::PropertyHolder {
104   s4u::Link piface_;
105   s4u::Link::SharingPolicy sharing_policy_ = s4u::Link::SharingPolicy::SHARED;
106
107 protected:
108   explicit LinkImpl(const std::string& name);
109   LinkImpl(const LinkImpl&) = delete;
110   LinkImpl& operator=(const LinkImpl&) = delete;
111   ~LinkImpl() override                 = default; // Use destroy() instead of this destructor.
112
113 public:
114   void destroy(); // Must be called instead of the destructor
115
116   void latency_check(double latency) const;
117
118   /** @brief Public interface */
119   const s4u::Link* get_iface() const { return &piface_; }
120   s4u::Link* get_iface() { return &piface_; }
121
122   /** @brief Get the bandwidth in bytes per second of current Link */
123   double get_bandwidth() const { return bandwidth_.peak * bandwidth_.scale; }
124   /** @brief Update the bandwidth in bytes per second of current Link */
125   virtual void set_bandwidth(double value) = 0;
126
127   /** @brief Get the latency in seconds of current Link */
128   double get_latency() const { return latency_.peak * latency_.scale; }
129   /** @brief Update the latency in seconds of current Link */
130   virtual LinkImpl* set_latency(double value) = 0;
131
132   /** @brief The sharing policy */
133   virtual LinkImpl* set_sharing_policy(s4u::Link::SharingPolicy policy);
134   virtual s4u::Link::SharingPolicy get_sharing_policy() const;
135
136   /** @brief Check if the Link is used */
137   bool is_used() const override;
138
139   void turn_on() override;
140   void turn_off() override;
141
142   void seal() override;
143
144   void on_bandwidth_change() const;
145
146   /* setup the profile file with bandwidth events (peak speed changes due to external load).
147    * Profile must contain percentages (value between 0 and 1). */
148   virtual LinkImpl* set_bandwidth_profile(kernel::profile::Profile* profile);
149   /* setup the profile file with latency events (peak latency changes due to external load).
150    * Profile must contain absolute values */
151   virtual LinkImpl* set_latency_profile(kernel::profile::Profile* profile);
152
153   void set_concurrency_limit(int limit) const;
154
155   Metric latency_   = {0.0, 1, nullptr};
156   Metric bandwidth_ = {1.0, 1, nullptr};
157 };
158
159 /**********
160  * Action *
161  **********/
162 /** @ingroup SURF_network_interface
163  * @brief SURF network action interface class
164  * @details A NetworkAction represents a communication between two [hosts](@ref simgrid::surf::HostImpl)
165  */
166 class NetworkAction : public Action {
167   s4u::Host& src_;
168   s4u::Host& dst_;
169
170 public:
171   /** @brief Constructor
172    *
173    * @param model The NetworkModel associated to this NetworkAction
174    * @param cost The cost of this  NetworkAction in [TODO]
175    * @param failed [description]
176    */
177   NetworkAction(Model* model, s4u::Host& src, s4u::Host& dst, double cost, bool failed)
178       : Action(model, cost, failed), src_(src), dst_(dst)
179   {
180   }
181
182   /**
183    * @brief NetworkAction constructor
184    *
185    * @param model The NetworkModel associated to this NetworkAction
186    * @param cost The cost of this  NetworkAction in bytes
187    * @param failed Actions can be created in a failed state
188    * @param var The lmm variable associated to this Action if it is part of a LMM component
189    */
190   NetworkAction(Model* model, s4u::Host& src, s4u::Host& dst, double cost, bool failed, lmm::Variable* var)
191       : Action(model, cost, failed, var), src_(src), dst_(dst){};
192
193   void set_state(Action::State state) override;
194   virtual std::list<LinkImpl*> get_links() const;
195
196   double latency_         = 0.; // Delay before the action starts
197   double lat_current_     = 0.; // Used to compute the communication RTT, and accordingly limit the communication rate
198   double sharing_penalty_ = {};
199
200   s4u::Host& get_src() const { return src_; }
201   s4u::Host& get_dst() const { return dst_; }
202 };
203
204 /* Insert link(s) at the end of vector `result' (at the beginning, and reversed, for insert_link_latency()), and add
205  * link->get_latency() to *latency when latency is not null
206  */
207 void add_link_latency(std::vector<LinkImpl*>& result, LinkImpl* link, double* latency);
208 void add_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency);
209 void insert_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency);
210
211 } // namespace resource
212 } // namespace kernel
213 } // namespace simgrid
214
215 #endif /* SURF_NETWORK_INTERFACE_HPP_ */