Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / simgrid / s4u / Link.hpp
1 /* Copyright (c) 2004-2023. 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 S4U_LINK_HPP
7 #define S4U_LINK_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/kernel/resource/Action.hpp>
11 #include <simgrid/link.h>
12 #include <string>
13 #include <unordered_map>
14 #include <xbt/Extendable.hpp>
15 #include <xbt/base.h>
16 #include <xbt/signal.hpp>
17
18 /***********
19  * Classes *
20  ***********/
21
22 namespace simgrid {
23
24 extern template class XBT_PUBLIC xbt::Extendable<s4u::Link>;
25
26 namespace s4u {
27 /**
28  * @beginrst
29  * A Link represents the network facilities between :cpp:class:`hosts <simgrid::s4u::Host>`.
30  * @endrst
31  */
32 class XBT_PUBLIC Link : public xbt::Extendable<Link> {
33 #ifndef DOXYGEN
34   friend kernel::resource::StandardLinkImpl;
35 #endif
36
37 protected:
38   // Links are created from the NetZone, and destroyed by their private implementation when the simulation ends
39   explicit Link(kernel::resource::LinkImpl* pimpl) : pimpl_(pimpl) {}
40   virtual ~Link() = default;
41   // The implementation that never changes
42   kernel::resource::LinkImpl* const pimpl_;
43 #ifndef DOXYGEN
44   friend kernel::resource::NetworkAction; // signal comm_state_changed
45 #endif
46
47 public:
48   /** Specifies how a given link is shared between concurrent communications */
49   enum class SharingPolicy {
50     /// This policy takes a callback that specifies the maximal capacity as a function of the number of usage. See the
51     /// examples with 'degradation' in their name.
52     NONLINEAR = 4,
53     /// Pseudo-sharing policy requesting wifi-specific sharing.
54     WIFI = 3,
55     /// Each link is split in 2, UP and DOWN, one per direction. These links are SHARED.
56     SPLITDUPLEX = 2,
57     /// The bandwidth is shared between all comms using that link, regardless of their direction.
58     SHARED = 1,
59     /// Each comm can use the link fully, with no sharing (only a maximum). This is intended to represent the backbone
60     /// links that cannot be saturated by concurrent links, but have a maximal bandwidth.
61     FATPIPE = 0
62   };
63
64   kernel::resource::StandardLinkImpl* get_impl() const;
65
66   /** \static @brief Retrieve a link from its name */
67   static Link* by_name(const std::string& name);
68   static Link* by_name_or_null(const std::string& name);
69
70   /** @brief Retrieves the name of that link as a C++ string */
71   const std::string& get_name() const;
72   /** @brief Retrieves the name of that link as a C string */
73   const char* get_cname() const;
74
75   /** Get/Set the bandwidth of the current Link (in bytes per second) */
76   double get_bandwidth() const;
77   Link* set_bandwidth(double value);
78
79   /** Get/Set the latency of the current Link (in seconds) */
80   double get_latency() const;
81   /**
82    * @brief Set link's latency
83    *
84    * @param value New latency value (in s)
85    */
86   Link* set_latency(double value);
87   /**
88    * @brief Set latency (string version)
89    *
90    * Accepts values with units, such as '1s' or '7ms'.
91    *
92    * Full list of accepted units: w (week), d (day), h, s, ms, us, ns, ps.
93    *
94    * @throw std::invalid_argument if latency format is incorrect.
95    */
96   Link* set_latency(const std::string& value);
97
98   /** @brief Describes how the link is shared between flows
99    *
100    *  Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
101    */
102   Link* set_sharing_policy(SharingPolicy policy, const NonLinearResourceCb& cb = {});
103   SharingPolicy get_sharing_policy() const;
104
105   /** Setup the profile with states events (ON or OFF). The profile must contain boolean values. */
106   Link* set_state_profile(kernel::profile::Profile* profile);
107   /** Setup the profile with bandwidth events (peak speed changes due to external load).
108    * The profile must contain percentages (value between 0 and 1). */
109   Link* set_bandwidth_profile(kernel::profile::Profile* profile);
110   /** Setup the profile file with latency events (peak latency changes due to external load).
111    * The profile must contain absolute values */
112   Link* set_latency_profile(kernel::profile::Profile* profile);
113
114   const std::unordered_map<std::string, std::string>* get_properties() const;
115   const char* get_property(const std::string& key) const;
116   Link* set_properties(const std::unordered_map<std::string, std::string>& properties);
117   Link* set_property(const std::string& key, const std::string& value);
118
119   /**
120    * @brief Set the number of communications that can shared this link at the same time
121    *
122    * Use this method to serialize communication flows going through this link.
123    * Use -1 to set no limit.
124    *
125    * @param limit  Number of concurrent flows
126    */
127   Link* set_concurrency_limit(int limit);
128   int get_concurrency_limit() const;
129
130   /** @brief Set the level of communication speed of the given host on this wifi link.
131    *
132    * The bandwidth of a wifi link for a given host depends on its SNR (signal to noise ratio),
133    * which ultimately depends on the distance between the host and the station and the material between them.
134    *
135    * This is modeled in SimGrid by providing several bandwidths to wifi links, one per SNR level (just provide
136    * comma-separated values in the XML file). By default, the first level in the list is used, but you can use the
137    * current function to specify that a given host uses another level of bandwidth. This can be used to take the
138    * location of hosts into account, or even to model mobility in your SimGrid simulation.
139    *
140    * Note that this function asserts that the link is actually a wifi link
141    *
142    * warning: in the case where a 0Mbps data rate should be used, set that rate only once during the
143    * experiment, and don't modify the bandwidth of that host later */
144   void set_host_wifi_rate(const s4u::Host* host, int level) const;
145
146   /** @brief Returns the current load (in bytes per second) */
147   double get_load() const;
148
149 #ifndef DOXYGEN
150   XBT_ATTRIB_DEPRECATED_v338("Please use get_load() instead") double get_usage() const { return get_load(); }
151 #endif
152
153   /** @brief Check if the Link is used (at least one flow uses the link) */
154   bool is_used() const;
155
156   /** @brief Check if the Link is shared (not a FATPIPE) */
157   bool is_shared() const;
158
159   /** Turns the link on. */
160   void turn_on();
161   /** Turns the link off. */
162   void turn_off();
163   /** Checks whether the link is on. */
164   bool is_on() const;
165
166   Link* seal();
167
168 private:
169 #ifndef DOXYGEN
170   static xbt::signal<void(Link&)> on_creation;
171   static xbt::signal<void(Link const&)> on_onoff;
172   xbt::signal<void(Link const&)> on_this_onoff;
173   static xbt::signal<void(Link const&)> on_bandwidth_change;
174   xbt::signal<void(Link const&)> on_this_bandwidth_change;
175   static xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
176       on_communication_state_change;
177   static xbt::signal<void(Link const&)> on_destruction;
178   xbt::signal<void(Link const&)> on_this_destruction;
179 #endif
180
181 public:
182   /* The signals */
183   /** \static @brief Add a callback fired when a new Link is created */
184   static void on_creation_cb(const std::function<void(Link&)>& cb) { on_creation.connect(cb); }
185   /** \static @brief Add a callback fired when any Link is turned on or off */
186   static void on_onoff_cb(const std::function<void(Link const&)>& cb)
187   {
188     on_onoff.connect(cb);
189   }
190   /** @brief Add a callback fired when this specific Link is turned on or off */
191   void on_this_onoff_cb(const std::function<void(Link const&)>& cb)
192   {
193     on_this_onoff.connect(cb);
194   }
195   /** \static @brief Add a callback fired when the bandwidth of any Link changes */
196   static void on_bandwidth_change_cb(const std::function<void(Link const&)>& cb) { on_bandwidth_change.connect(cb); }
197   /** @brief Add a callback fired when the bandwidth of this specific Link changes */
198   void on_this_bandwidth_change_cb(const std::function<void(Link const&)>& cb)
199   {
200     on_this_bandwidth_change.connect(cb);
201   }
202   /** \static @brief Add a callback fired when a communication changes it state (ready/done/cancel) */
203   static void on_communication_state_change_cb(
204       const std::function<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>& cb)
205   {
206     on_communication_state_change.connect(cb);
207   }
208   /** \static @brief Add a callback fired when any Link is destroyed */
209   static void on_destruction_cb(const std::function<void(Link const&)>& cb) { on_destruction.connect(cb); }
210   /** @brief Add a callback fired when this specific Link is destroyed */
211   void on_this_destruction_cb(const std::function<void(Link const&)>& cb)
212   {
213     on_this_destruction.connect(cb);
214   }
215
216   XBT_ATTRIB_DEPRECATED_v338("Please use on_onoff_cb() instead") static void on_state_change_cb(
217       const std::function<void(Link const&)>& cb)
218   {
219     on_onoff.connect(cb);
220   }
221 };
222
223 /**
224  * @beginrst
225  * A SplitDuplexLink encapsulates the :cpp:class:`links <simgrid::s4u::Link>` which
226  * compose a Split-Duplex link. Remember that a Split-Duplex link is nothing more than
227  * a pair of up/down links.
228  * @endrst
229  */
230 class XBT_PUBLIC SplitDuplexLink : public Link {
231 public:
232   explicit SplitDuplexLink(kernel::resource::LinkImpl* pimpl) : Link(pimpl) {}
233   /** @brief Get the link direction up*/
234   Link* get_link_up() const;
235   /** @brief Get the link direction down */
236   Link* get_link_down() const;
237
238   /** \static @brief Retrieve a link from its name */
239   static SplitDuplexLink* by_name(const std::string& name);
240 };
241
242 /**
243  * @beginrst
244  * Another encapsulation for using links in the :cpp:func:`NetZone::add_route`
245  *
246  * When adding a route with split-duplex links, you need to specify the direction of the link
247  * so SimGrid can know exactly which physical link to insert in the route.
248  *
249  * For shared/fat-pipe links, use the :cpp:enumerator:`Direction::NONE` since they don't have
250  * the concept of UP/DOWN links.
251  * @endrst
252  */
253 class XBT_PUBLIC LinkInRoute {
254 public:
255   enum class Direction { UP = 2, DOWN = 1, NONE = 0 };
256
257   explicit LinkInRoute(const Link* link) : link_(link) {}
258   LinkInRoute(const Link* link, Direction d) : link_(link), direction_(d) {}
259
260   /** @brief Get direction of this link in the route: UP or DOWN */
261   Direction get_direction() const { return direction_; }
262   /** @brief Get pointer to the link */
263   const Link* get_link() const { return link_; }
264
265 private:
266   const Link* link_;
267   Direction direction_ = Direction::NONE;
268 };
269
270 } // namespace s4u
271 } // namespace simgrid
272
273 #endif /* S4U_LINK_HPP */