Logo AND Algorithmique Numérique Distribuée

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