Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'split_link_impl' into 'master'
[simgrid.git] / include / simgrid / s4u / Link.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 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::LinkImpl;
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::LinkImplIntf* pimpl) : pimpl_(pimpl) {}
40   virtual ~Link() = default;
41   // The implementation that never changes
42   kernel::resource::LinkImplIntf* const pimpl_;
43
44 public:
45   enum class SharingPolicy { WIFI = 3, SPLITDUPLEX = 2, SHARED = 1, FATPIPE = 0 };
46
47   kernel::resource::LinkImpl* get_impl() const;
48
49   /** @brief Retrieve a link from its name */
50   static Link* by_name(const std::string& name);
51   static Link* by_name_or_null(const std::string& name);
52
53   /** @brief Retrieves the name of that link as a C++ string */
54   const std::string& get_name() const;
55   /** @brief Retrieves the name of that link as a C string */
56   const char* get_cname() const;
57
58   /** Get/Set the bandwidth of the current Link (in bytes per second) */
59   double get_bandwidth() const;
60   Link* set_bandwidth(double value);
61
62   /** Get/Set the latency of the current Link (in seconds) */
63   double get_latency() const;
64   /**
65    * @brief Set link's latency
66    *
67    * @param value New latency value (in s)
68    */
69   Link* set_latency(double value);
70   /**
71    * @brief Set latency (string version)
72    *
73    * @throw std::invalid_argument if latency format is incorrect.
74    */
75   Link* set_latency(const std::string& value);
76
77   /** @brief Describes how the link is shared between flows */
78   Link* set_sharing_policy(SharingPolicy policy);
79   SharingPolicy get_sharing_policy() const;
80
81   /** Setup the profile with states events (ON or OFF). The profile must contain boolean values. */
82   Link* set_state_profile(kernel::profile::Profile* profile);
83   /** Setup the profile with bandwidth events (peak speed changes due to external load).
84    * The profile must contain percentages (value between 0 and 1). */
85   Link* set_bandwidth_profile(kernel::profile::Profile* profile);
86   /** Setup the profile file with latency events (peak latency changes due to external load).
87    * The profile must contain absolute values */
88   Link* set_latency_profile(kernel::profile::Profile* profile);
89
90   const std::unordered_map<std::string, std::string>* get_properties() const;
91   const char* get_property(const std::string& key) const;
92   Link* set_properties(const std::unordered_map<std::string, std::string>& properties);
93   Link* set_property(const std::string& key, const std::string& value);
94
95   /**
96    * @brief Set the number of communications that can shared this link at the same time
97    *
98    * Use this method to serialize communication flows going through this link.
99    * Use -1 to set no limit.
100    *
101    * @param limit  Number of concurrent flows
102    */
103   Link* set_concurrency_limit(int limit);
104
105   /** @brief Set the level of communication speed of the given host on this wifi link.
106    *
107    * The bandwidth of a wifi link for a given host depends on its SNR (signal to noise ratio),
108    * which ultimately depends on the distance between the host and the station and the material between them.
109    *
110    * This is modeled in SimGrid by providing several bandwidths to wifi links, one per SNR level (just provide
111    * comma-separated values in the XML file). By default, the first level in the list is used, but you can use the
112    * current function to specify that a given host uses another level of bandwidth. This can be used to take the
113    * location of hosts into account, or even to model mobility in your SimGrid simulation.
114    *
115    * Note that this function asserts that the link is actually a wifi link */
116   void set_host_wifi_rate(const s4u::Host* host, int level) const;
117
118   /** @brief Returns the current load (in bytes per second) */
119   double get_usage() const;
120
121   /** @brief Check if the Link is used (at least one flow uses the link) */
122   bool is_used() const;
123
124   /** @brief Check if the Link is shared (not a FATPIPE) */
125   bool is_shared() const;
126
127   void turn_on();
128   void turn_off();
129   bool is_on() const;
130
131   Link* seal();
132
133   /* The signals */
134   /** @brief Callback signal fired when a new Link is created */
135   static xbt::signal<void(Link&)> on_creation;
136
137   /** @brief Callback signal fired when a Link is destroyed */
138   static xbt::signal<void(Link const&)> on_destruction;
139
140   /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off) */
141   static xbt::signal<void(Link const&)> on_state_change;
142
143   /** @brief Callback signal fired when the bandwidth of a Link changes */
144   static xbt::signal<void(Link const&)> on_bandwidth_change;
145
146   /** @brief Callback signal fired when a communication starts */
147   static xbt::signal<void(kernel::resource::NetworkAction&)> on_communicate;
148
149   /** @brief Callback signal fired when a communication changes it state (ready/done/cancel) */
150   static xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
151       on_communication_state_change;
152 };
153
154 /**
155  * @beginrst
156  * A SplitDuplexLink encapsulates the :cpp:class:`links <simgrid::s4u::Link>` which
157  * compose a Split-Duplex link. Remember that a Split-Duplex link is nothing more than
158  * a pair of up/down links.
159  * @endrst
160  */
161 class XBT_PUBLIC SplitDuplexLink : public Link {
162 public:
163   explicit SplitDuplexLink(kernel::resource::LinkImplIntf* pimpl) : Link(pimpl) {}
164   /** @brief Get the link direction up*/
165   Link* get_link_up() const;
166   /** @brief Get the link direction down */
167   Link* get_link_down() const;
168
169   /** @brief Retrieve a link from its name */
170   static SplitDuplexLink* by_name(const std::string& name);
171 };
172
173 /**
174  * @beginrst
175  * Another encapsulation for using links in the :cpp:function:: NetZone::add_route
176  *
177  * When adding a route with split-duplex links, you need to specify the direction of the link
178  * so SimGrid can know exactly which physical link to insert in the route.
179  *
180  * For shared/fat-pipe links, use the Direction::NONE since they don't have
181  * the concept of UP/DOWN links.
182  * @endrst
183  */
184 class XBT_PUBLIC LinkInRoute {
185 public:
186   enum class Direction { UP = 2, DOWN = 1, NONE = 0 };
187
188   LinkInRoute(const Link* link) : link_(link) {}
189   LinkInRoute(const Link* link, Direction d) : link_(link), direction_(d) {}
190
191   /** @brief Get direction of this link in the route: UP or DOWN */
192   Direction get_direction() const { return direction_; }
193   /** @brief Get pointer to the link */
194   const Link* get_link() const { return link_; }
195
196 private:
197   const Link* link_;
198   Direction direction_ = Direction::NONE;
199 };
200
201 } // namespace s4u
202 } // namespace simgrid
203
204 #endif /* S4U_LINK_HPP */