Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improving the performance of the NS-3 bindings by:
[simgrid.git] / src / surf / network_interface.hpp
1 /* Copyright (c) 2004-2019. 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 "src/surf/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   explicit NetworkModel(Model::UpdateAlgo algo) : Model(algo) {}
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 latency The initial latency of the Link in seconds
49    * @param policy The sharing policy of the Link
50    */
51   virtual LinkImpl* create_link(const std::string& name, const std::vector<double>& bandwidths, double latency,
52                                 s4u::Link::SharingPolicy policy) = 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);
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);
87
88   /**
89    * @brief Get definitive bandwidth.
90    * @details It gives the minimum bandwidth between the one that would occur if no limitation was enforced, and the
91    * one arbitrary limited.
92    * @param rate The desired maximum bandwidth.
93    * @param bound The bandwidth with only the network taken into account.
94    * @param size The size of the message.
95    * @return The new bandwidth.
96    */
97   virtual double get_bandwidth_constraint(double rate, double bound, double size);
98   double next_occuring_event_full(double now) override;
99
100   LinkImpl* loopback_ = nullptr;
101 };
102
103 /************
104  * Resource *
105  ************/
106 /** @ingroup SURF_network_interface
107  * @brief SURF network link interface class
108  * @details A Link represents the link between two [hosts](@ref simgrid::surf::HostImpl)
109  */
110 class LinkImpl : public Resource, public surf::PropertyHolder {
111   bool currently_destroying_ = false;
112   void* userdata_            = nullptr;
113
114 protected:
115   LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint);
116   LinkImpl(const LinkImpl&) = delete;
117   LinkImpl& operator=(const LinkImpl&) = delete;
118   ~LinkImpl() override;
119
120 public:
121   void destroy(); // Must be called instead of the destructor
122   void* get_data() { return userdata_; }
123   void set_data(void* d) { userdata_ = d; }
124
125   /** @brief Public interface */
126   s4u::Link piface_;
127
128   /** @brief Get the bandwidth in bytes per second of current Link */
129   virtual double get_bandwidth();
130
131   /** @brief Update the bandwidth in bytes per second of current Link */
132   virtual void set_bandwidth(double value) = 0;
133
134   /** @brief Get the latency in seconds of current Link */
135   virtual double get_latency();
136
137   /** @brief Update the latency in seconds of current Link */
138   virtual void set_latency(double value) = 0;
139
140   /** @brief The sharing policy */
141   virtual s4u::Link::SharingPolicy get_sharing_policy();
142
143   /** @brief Check if the Link is used */
144   bool is_used() override;
145
146   void turn_on() override;
147   void turn_off() override;
148
149   void on_bandwidth_change();
150
151   virtual void
152   set_bandwidth_profile(kernel::profile::Profile* profile); /*< setup the profile file with bandwidth events
153                                                    (peak speed changes due to external load). Trace must
154                                                    contain percentages (value between 0 and 1). */
155   virtual void
156   set_latency_profile(kernel::profile::Profile* profile); /*< setup the trace file with latency events (peak
157                                                  latency changes due to external load).   Trace must contain
158                                                  absolute values */
159
160   Metric latency_                   = {1.0, 0, nullptr};
161   Metric bandwidth_                 = {1.0, 0, nullptr};
162
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 public:
174   /** @brief Constructor
175    *
176    * @param model The NetworkModel associated to this NetworkAction
177    * @param cost The cost of this  NetworkAction in [TODO]
178    * @param failed [description]
179    */
180   NetworkAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
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, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var){};
191
192   void set_state(Action::State state) override;
193   virtual std::list<LinkImpl*> links() const;
194
195   double latency_    = {};
196   double lat_current_ = {};
197   double sharing_penalty_ = {};
198   double rate_       = {};
199 };
200 } // namespace resource
201 } // namespace kernel
202 } // namespace simgrid
203
204 /** @ingroup SURF_models
205  *  @brief The network model
206  */
207 XBT_PUBLIC_DATA simgrid::kernel::resource::NetworkModel* surf_network_model;
208
209 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
210
211