Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c2e64533ae5343dcb87c86c5f2fd6da30cb02872
[simgrid.git] / include / simgrid / s4u / Link.hpp
1 /* Copyright (c) 2004-2020. 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 /** @brief A Link represents the network facilities between [hosts](@ref simgrid::s4u::Host) */
28 class XBT_PUBLIC Link : public xbt::Extendable<Link> {
29   friend kernel::resource::LinkImpl;
30
31   // Links are created from the NetZone, and destroyed by their private implementation when the simulation ends
32   explicit Link(kernel::resource::LinkImpl* pimpl) : pimpl_(pimpl) {}
33   virtual ~Link() = default;
34   // The private implementation, that never changes
35   kernel::resource::LinkImpl* const pimpl_;
36
37 public:
38   enum class SharingPolicy { WIFI = 3, SPLITDUPLEX = 2, SHARED = 1, FATPIPE = 0 };
39
40   kernel::resource::LinkImpl* get_impl() const { return pimpl_; }
41
42   /** @brief Retrieve a link from its name */
43   static Link* by_name(const std::string& name);
44   static Link* by_name_or_null(const std::string& name);
45
46   /** @brief Retrieves the name of that link as a C++ string */
47   const std::string& get_name() const;
48   /** @brief Retrieves the name of that link as a C string */
49   const char* get_cname() const;
50
51   /** @brief Get the bandwidth in bytes per second of current Link */
52   double get_bandwidth() const;
53
54   /** @brief Get the latency in seconds of current Link */
55   double get_latency() const;
56
57   /** @brief Describes how the link is shared between flows */
58   SharingPolicy get_sharing_policy() const;
59
60   /** @brief Returns the current load (in flops per second) */
61   double get_usage() const;
62
63   /** @brief Check if the Link is used (at least one flow uses the link) */
64   bool is_used() const;
65
66   void turn_on();
67   bool is_on() const;
68   void turn_off();
69
70   /** Setup the profile with states events (ON or OFF). The profile must contain boolean values. */
71   void set_state_profile(kernel::profile::Profile* profile);
72   /** Setup the profile with bandwidth events (peak speed changes due to external load).
73    * The profile must contain percentages (value between 0 and 1). */
74   void set_bandwidth_profile(kernel::profile::Profile* profile);
75   /** Setup the profile file with latency events (peak latency changes due to external load).
76    * The profile must contain absolute values */
77   void set_latency_profile(kernel::profile::Profile* profile);
78
79   const char* get_property(const std::string& key) const;
80   void set_property(const std::string& key, const std::string& value);
81
82   /* The signals */
83   /** @brief Callback signal fired when a new Link is created */
84   static xbt::signal<void(Link&)> on_creation;
85
86   /** @brief Callback signal fired when a Link is destroyed */
87   static xbt::signal<void(Link const&)> on_destruction;
88
89   /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off) */
90   static xbt::signal<void(Link const&)> on_state_change;
91
92   /** @brief Callback signal fired when the bandwidth of a Link changes */
93   static xbt::signal<void(Link const&)> on_bandwidth_change;
94
95   /** @brief Callback signal fired when a communication starts */
96   static xbt::signal<void(kernel::resource::NetworkAction&, Host* src, Host* dst)> on_communicate;
97
98   /** @brief Callback signal fired when a communication changes it state (ready/done/cancel) */
99   static xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
100       on_communication_state_change;
101 };
102 } // namespace s4u
103 } // namespace simgrid
104
105 #endif /* SURF_NETWORK_INTERFACE_HPP_ */