1 /* Copyright (c) 2004-2021. The SimGrid Team. All rights reserved. */
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. */
9 #include <simgrid/forward.h>
10 #include <simgrid/kernel/resource/Action.hpp>
11 #include <simgrid/link.h>
13 #include <unordered_map>
14 #include <xbt/Extendable.hpp>
16 #include <xbt/signal.hpp>
24 extern template class XBT_PUBLIC xbt::Extendable<s4u::Link>;
29 * A Link represents the network facilities between :cpp:class:`hosts <simgrid::s4u::Host>`.
32 class XBT_PUBLIC Link : public xbt::Extendable<Link> {
33 friend kernel::resource::LinkImpl;
35 // Links are created from the NetZone, and destroyed by their private implementation when the simulation ends
36 explicit Link(kernel::resource::LinkImpl* pimpl) : pimpl_(pimpl) {}
37 virtual ~Link() = default;
38 // The private implementation, that never changes
39 kernel::resource::LinkImpl* const pimpl_;
42 enum class SharingPolicy { WIFI = 3, SPLITDUPLEX = 2, SHARED = 1, FATPIPE = 0 };
44 kernel::resource::LinkImpl* get_impl() const { return pimpl_; }
46 /** @brief Retrieve a link from its name */
47 static Link* by_name(const std::string& name);
48 static Link* by_name_or_null(const std::string& name);
50 /** @brief Retrieves the name of that link as a C++ string */
51 const std::string& get_name() const;
52 /** @brief Retrieves the name of that link as a C string */
53 const char* get_cname() const;
55 /** Get the bandwidth of the current Link (in bytes per second) */
56 double get_bandwidth() const;
57 /** Set the bandwidth of the current Link (in bytes per second) */
58 void set_bandwidth(double value);
60 /** Get the latency of the current Link (in seconds) */
61 double get_latency() const;
62 /** Set the latency of the current Link (in seconds) */
63 void set_latency(double value);
65 /** @brief Describes how the link is shared between flows */
66 SharingPolicy get_sharing_policy() const;
68 /** @brief Set the level of communication speed of the given host on this wifi link.
70 * The bandwidth of a wifi link for a given host depends on its SNR (signal to noise ratio),
71 * which ultimately depends on the distance between the host and the station and the material between them.
73 * This is modeled in SimGrid by providing several bandwidths to wifi links, one per SNR level (just provide
74 * comma-separated values in the XML file). By default, the first level in the list is used, but you can use the
75 * current function to specify that a given host uses another level of bandwidth. This can be used to take the
76 * location of hosts into account, or even to model mobility in your SimGrid simulation.
78 * Note that this function asserts that the link is actually a wifi link */
79 void set_host_wifi_rate(const s4u::Host* host, int level) const;
81 /** @brief Returns the current load (in bytes per second) */
82 double get_usage() const;
84 /** @brief Check if the Link is used (at least one flow uses the link) */
87 /** @brief Check if the Link is shared (not a FATPIPE) */
88 bool is_shared() const;
94 /** Setup the profile with states events (ON or OFF). The profile must contain boolean values. */
95 void set_state_profile(kernel::profile::Profile* profile);
96 /** Setup the profile with bandwidth events (peak speed changes due to external load).
97 * The profile must contain percentages (value between 0 and 1). */
98 void set_bandwidth_profile(kernel::profile::Profile* profile);
99 /** Setup the profile file with latency events (peak latency changes due to external load).
100 * The profile must contain absolute values */
101 void set_latency_profile(kernel::profile::Profile* profile);
103 const char* get_property(const std::string& key) const;
104 void set_property(const std::string& key, const std::string& value);
107 /** @brief Callback signal fired when a new Link is created */
108 static xbt::signal<void(Link&)> on_creation;
110 /** @brief Callback signal fired when a Link is destroyed */
111 static xbt::signal<void(Link const&)> on_destruction;
113 /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off) */
114 static xbt::signal<void(Link const&)> on_state_change;
116 /** @brief Callback signal fired when the bandwidth of a Link changes */
117 static xbt::signal<void(Link const&)> on_bandwidth_change;
119 /** @brief Callback signal fired when a communication starts */
120 static xbt::signal<void(kernel::resource::NetworkAction&)> on_communicate;
122 /** @brief Callback signal fired when a communication changes it state (ready/done/cancel) */
123 static xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
124 on_communication_state_change;
127 } // namespace simgrid
129 #endif /* S4U_LINK_HPP */