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/surf/PropertyHolder.hpp"
14 #include "src/surf/trace_mgr.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 simgrid::config::Flag<double> cfg_tcp_gamma;
37 static simgrid::config::Flag<bool> cfg_crosstraffic;
39 explicit NetworkModel(Model::UpdateAlgo algo) : Model(algo) {}
40 ~NetworkModel() override;
43 * @brief Create a Link
45 * @param name The name of the Link
46 * @param bandwidth The initial bandwidth of the Link in bytes per second
47 * @param latency The initial latency of the Link in seconds
48 * @param policy The sharing policy of the Link
50 virtual LinkImpl* create_link(const std::string& name, double bandwidth, double latency,
51 s4u::Link::SharingPolicy policy) = 0;
54 * @brief Create a communication between two hosts.
55 * @details It makes calls to the routing part, and execute the communication
56 * between the two end points.
58 * @param src The source of the communication
59 * @param dst The destination of the communication
60 * @param size The size of the communication in bytes
61 * @param rate Allows to limit the transfer rate. Negative value means
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
70 * a message might be different from the theoretical latency of the link,
71 * in function of the message size. In order to account for this, this
72 * function gets this factor.
74 * @param size The size of the message.
75 * @return The latency factor.
77 virtual double get_latency_factor(double size);
80 * @brief Get the right multiplicative factor for the bandwidth.
81 * @details Depending on the model, the effective bandwidth when sending
82 * a message might be different from the theoretical bandwidth of the link,
83 * in function of the message size. In order to account for this, this
84 * function gets this factor.
86 * @param size The size of the message.
87 * @return The bandwidth factor.
89 virtual double get_bandwidth_factor(double size);
92 * @brief Get definitive bandwidth.
93 * @details It gives the minimum bandwidth between the one that would
94 * occur if no limitation was enforced, and the one arbitrary limited.
95 * @param rate The desired maximum bandwidth.
96 * @param bound The bandwidth with only the network taken into account.
97 * @param size The size of the message.
98 * @return The new bandwidth.
100 virtual double get_bandwidth_constraint(double rate, double bound, double size);
101 double next_occuring_event_full(double now) override;
103 LinkImpl* loopback_ = nullptr;
109 /** @ingroup SURF_network_interface
110 * @brief SURF network link interface class
111 * @details A Link represents the link between two [hosts](@ref simgrid::surf::HostImpl)
113 class LinkImpl : public Resource, public simgrid::surf::PropertyHolder {
115 LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint);
116 ~LinkImpl() override;
119 void destroy(); // Must be called instead of the destructor
121 bool currently_destroying_ = false;
124 /** @brief Public interface */
127 /** @brief Get the bandwidth in bytes per second of current Link */
128 virtual double get_bandwidth();
130 /** @brief Update the bandwidth in bytes per second of current Link */
131 virtual void set_bandwidth(double value) = 0;
133 /** @brief Get the latency in seconds of current Link */
134 virtual double get_latency();
136 /** @brief Update the latency in seconds of current Link */
137 virtual void set_latency(double value) = 0;
139 /** @brief The sharing policy */
140 virtual s4u::Link::SharingPolicy get_sharing_policy();
142 /** @brief Check if the Link is used */
143 bool is_used() override;
145 void turn_on() override;
146 void turn_off() override;
148 void on_bandwidth_change();
150 virtual void set_bandwidth_trace(
151 tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
152 Trace must contain percentages (value between 0 and 1). */
153 virtual void set_latency_trace(
154 tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
155 Trace must contain absolute values */
157 Metric latency_ = {1.0, 0, nullptr};
158 Metric bandwidth_ = {1.0, 0, nullptr};
161 void* get_data() { return userdata_; }
162 void set_data(void* d) { userdata_ = d; }
164 void* userdata_ = nullptr;
170 /** @ingroup SURF_network_interface
171 * @brief SURF network action interface class
172 * @details A NetworkAction represents a communication between two [hosts](@ref simgrid::surf::HostImpl)
174 class NetworkAction : public Action {
176 /** @brief Constructor
178 * @param model The NetworkModel associated to this NetworkAction
179 * @param cost The cost of this NetworkAction in [TODO]
180 * @param failed [description]
182 NetworkAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
185 * @brief NetworkAction constructor
187 * @param model The NetworkModel associated to this NetworkAction
188 * @param cost The cost of this NetworkAction in bytes
189 * @param failed Actions can be created in a failed state
190 * @param var The lmm variable associated to this Action if it is part of a LMM component
192 NetworkAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var){};
194 void set_state(Action::State state) override;
195 virtual std::list<LinkImpl*> links();
197 double latency_ = {};
198 double lat_current_ = {};
204 } // 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_ */