Logo AND Algorithmique Numérique Distribuée

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