Logo AND Algorithmique Numérique Distribuée

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