1 /* Copyright (c) 2004-2019. 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. */
6 #ifndef SURF_NETWORK_INTERFACE_HPP_
7 #define SURF_NETWORK_INTERFACE_HPP_
9 #include "simgrid/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/Resource.hpp"
11 #include "simgrid/s4u/Link.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/kernel/resource/profile/trace_mgr.hpp"
14 #include "src/surf/PropertyHolder.hpp"
17 #include <unordered_map>
30 /** @ingroup SURF_network_interface
31 * @brief SURF network model interface class
32 * @details A model is an object which handles the interactions between its Resources and its Actions
34 class NetworkModel : public Model {
36 static config::Flag<double> cfg_tcp_gamma;
37 static config::Flag<bool> cfg_crosstraffic;
39 explicit NetworkModel(Model::UpdateAlgo algo) : Model(algo) {}
40 NetworkModel(const NetworkModel&) = delete;
41 NetworkModel& operator=(const NetworkModel&) = delete;
42 ~NetworkModel() override;
45 * @brief Create a Link
47 * @param name The name of the Link
48 * @param bandwidth The initial bandwidth of the Link in bytes per second
49 * @param latency The initial latency of the Link in seconds
50 * @param policy The sharing policy of the Link
52 virtual LinkImpl* create_link(const std::string& name, double bandwidth, double latency,
53 s4u::Link::SharingPolicy policy) = 0;
56 * @brief Create a communication between two hosts.
57 * @details It makes calls to the routing part, and execute the communication between the two end points.
59 * @param src The source of the communication
60 * @param dst The destination of the communication
61 * @param size The size of the communication in bytes
62 * @param rate Allows to limit the transfer rate. Negative value means unlimited.
63 * @return The action representing the communication
65 virtual Action* communicate(s4u::Host* src, s4u::Host* dst, double size, double rate) = 0;
68 * @brief Get the right multiplicative factor for the latency.
69 * @details Depending on the model, the effective latency when sending a message might be different from the
70 * theoretical latency of the link, in function of the message size. In order to account for this, this function gets
73 * @param size The size of the message.
74 * @return The latency factor.
76 virtual double get_latency_factor(double size);
79 * @brief Get the right multiplicative factor for the bandwidth.
80 * @details Depending on the model, the effective bandwidth when sending a message might be different from the
81 * theoretical bandwidth of the link, in function of the message size. In order to account for this, this function
84 * @param size The size of the message.
85 * @return The bandwidth factor.
87 virtual double get_bandwidth_factor(double size);
90 * @brief Get definitive bandwidth.
91 * @details It gives the minimum bandwidth between the one that would occur if no limitation was enforced, and the
92 * one arbitrary limited.
93 * @param rate The desired maximum bandwidth.
94 * @param bound The bandwidth with only the network taken into account.
95 * @param size The size of the message.
96 * @return The new bandwidth.
98 virtual double get_bandwidth_constraint(double rate, double bound, double size);
99 double next_occuring_event_full(double now) override;
101 LinkImpl* loopback_ = nullptr;
107 /** @ingroup SURF_network_interface
108 * @brief SURF network link interface class
109 * @details A Link represents the link between two [hosts](@ref simgrid::surf::HostImpl)
111 class LinkImpl : public Resource, public surf::PropertyHolder {
112 bool currently_destroying_ = false;
113 void* userdata_ = nullptr;
116 LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint);
117 LinkImpl(const LinkImpl&) = delete;
118 LinkImpl& operator=(const LinkImpl&) = delete;
119 ~LinkImpl() override;
122 void destroy(); // Must be called instead of the destructor
123 void* get_data() { return userdata_; }
124 void set_data(void* d) { userdata_ = d; }
126 /** @brief Public interface */
129 /** @brief Get the bandwidth in bytes per second of current Link */
130 virtual double get_bandwidth();
132 /** @brief Update the bandwidth in bytes per second of current Link */
133 virtual void set_bandwidth(double value) = 0;
135 /** @brief Get the latency in seconds of current Link */
136 virtual double get_latency();
138 /** @brief Update the latency in seconds of current Link */
139 virtual void set_latency(double value) = 0;
141 /** @brief The sharing policy */
142 virtual s4u::Link::SharingPolicy get_sharing_policy();
144 /** @brief Check if the Link is used */
145 bool is_used() override;
147 void turn_on() override;
148 void turn_off() override;
150 void on_bandwidth_change();
153 set_bandwidth_profile(kernel::profile::Profile* profile); /*< setup the profile file with bandwidth events
154 (peak speed changes due to external load). Trace must
155 contain percentages (value between 0 and 1). */
157 set_latency_profile(kernel::profile::Profile* profile); /*< setup the trace file with latency events (peak
158 latency changes due to external load). Trace must contain
161 Metric latency_ = {1.0, 0, nullptr};
162 Metric bandwidth_ = {1.0, 0, nullptr};
169 /** @ingroup SURF_network_interface
170 * @brief SURF network action interface class
171 * @details A NetworkAction represents a communication between two [hosts](@ref simgrid::surf::HostImpl)
173 class NetworkAction : public Action {
175 /** @brief Constructor
177 * @param model The NetworkModel associated to this NetworkAction
178 * @param cost The cost of this NetworkAction in [TODO]
179 * @param failed [description]
181 NetworkAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
184 * @brief NetworkAction constructor
186 * @param model The NetworkModel associated to this NetworkAction
187 * @param cost The cost of this NetworkAction in bytes
188 * @param failed Actions can be created in a failed state
189 * @param var The lmm variable associated to this Action if it is part of a LMM component
191 NetworkAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var){};
193 void set_state(Action::State state) override;
194 virtual std::list<LinkImpl*> links() const;
196 double latency_ = {};
197 double lat_current_ = {};
201 } // namespace resource
202 } // namespace kernel
203 } // namespace simgrid
205 /** @ingroup SURF_models
206 * @brief The network model
208 XBT_PUBLIC_DATA simgrid::kernel::resource::NetworkModel* surf_network_model;
210 #endif /* SURF_NETWORK_INTERFACE_HPP_ */