1 /* Copyright (c) 2004-2018. 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/s4u/Link.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "src/kernel/model/Resource.hpp"
12 #include "src/surf/PropertyHolder.hpp"
13 #include "src/surf/surf_interface.hpp"
16 #include <unordered_map>
28 /** @ingroup SURF_network_interface
29 * @brief SURF network model interface class
30 * @details A model is an object which handles the interactions between its Resources and its Actions
32 class NetworkModel : public Model {
34 /** @brief Constructor */
35 NetworkModel() : Model() {}
37 /** @brief Destructor */
38 ~NetworkModel() override;
41 * @brief Create a Link
43 * @param name The name of the Link
44 * @param bandwidth The initial bandwidth of the Link in bytes per second
45 * @param latency The initial latency of the Link in seconds
46 * @param policy The sharing policy of the Link
48 virtual LinkImpl* createLink(const std::string& name, double bandwidth, double latency,
49 e_surf_link_sharing_policy_t policy) = 0;
52 * @brief Create a communication between two hosts.
53 * @details It makes calls to the routing part, and execute the communication
54 * between the two end points.
56 * @param src The source of the communication
57 * @param dst The destination of the communication
58 * @param size The size of the communication in bytes
59 * @param rate Allows to limit the transfer rate. Negative value means
61 * @return The action representing the communication
63 virtual Action* communicate(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double size, double rate) = 0;
65 /** @brief Function pointer to the function to use to solve the lmm_system_t
67 * @param system The lmm_system_t to solve
69 void (*f_networkSolve)(lmm_system_t) = simgrid::kernel::lmm::lmm_solve;
72 * @brief Get the right multiplicative factor for the latency.
73 * @details Depending on the model, the effective latency when sending
74 * a message might be different from the theoretical latency of the link,
75 * in function of the message size. In order to account for this, this
76 * function gets this factor.
78 * @param size The size of the message.
79 * @return The latency factor.
81 virtual double latencyFactor(double size);
84 * @brief Get the right multiplicative factor for the bandwidth.
85 * @details Depending on the model, the effective bandwidth when sending
86 * a message might be different from the theoretical bandwidth of the link,
87 * in function of the message size. In order to account for this, this
88 * function gets this factor.
90 * @param size The size of the message.
91 * @return The bandwidth factor.
93 virtual double bandwidthFactor(double size);
96 * @brief Get definitive bandwidth.
97 * @details It gives the minimum bandwidth between the one that would
98 * occur if no limitation was enforced, and the one arbitrary limited.
99 * @param rate The desired maximum bandwidth.
100 * @param bound The bandwidth with only the network taken into account.
101 * @param size The size of the message.
102 * @return The new bandwidth.
104 virtual double bandwidthConstraint(double rate, double bound, double size);
105 double nextOccuringEventFull(double now) override;
107 LinkImpl* loopback_ = nullptr;
113 /** @ingroup SURF_network_interface
114 * @brief SURF network link interface class
115 * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
117 class LinkImpl : public simgrid::kernel::model::Resource, public simgrid::surf::PropertyHolder {
119 LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint);
120 ~LinkImpl() override;
123 void destroy(); // Must be called instead of the destructor
125 bool currentlyDestroying_ = false;
128 /** @brief Public interface */
131 /** @brief Get the bandwidth in bytes per second of current Link */
132 virtual double bandwidth();
134 /** @brief Update the bandwidth in bytes per second of current Link */
135 virtual void setBandwidth(double value) = 0;
137 /** @brief Get the latency in seconds of current Link */
138 virtual double latency();
140 /** @brief Update the latency in seconds of current Link */
141 virtual void setLatency(double value) = 0;
143 /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
145 virtual int sharingPolicy();
147 /** @brief Check if the Link is used */
148 bool isUsed() override;
150 void turnOn() override;
151 void turnOff() override;
153 virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
154 Trace must contain boolean values. */
155 virtual void setBandwidthTrace(
156 tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
157 Trace must contain percentages (value between 0 and 1). */
158 virtual void setLatencyTrace(
159 tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
160 Trace must contain absolute values */
162 tmgr_trace_event_t stateEvent_ = nullptr;
163 Metric latency_ = {1.0, 0, nullptr};
164 Metric bandwidth_ = {1.0, 0, nullptr};
167 void* getData() { return userData; }
168 void setData(void* d) { userData = d; }
170 void* userData = nullptr;
172 /* List of all links. FIXME: should move to the Engine */
173 static std::unordered_map<std::string, LinkImpl*>* links;
176 static LinkImpl* byName(std::string name);
177 static int linksCount();
178 static LinkImpl** linksList();
179 static void linksList(std::vector<s4u::Link*>* linkList);
180 static void linksExit();
186 /** @ingroup SURF_network_interface
187 * @brief SURF network action interface class
188 * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
190 class NetworkAction : public simgrid::surf::Action {
192 /** @brief Constructor
194 * @param model The NetworkModel associated to this NetworkAction
195 * @param cost The cost of this NetworkAction in [TODO]
196 * @param failed [description]
198 NetworkAction(simgrid::surf::Model* model, double cost, bool failed) : simgrid::surf::Action(model, cost, failed) {}
201 * @brief NetworkAction constructor
203 * @param model The NetworkModel associated to this NetworkAction
204 * @param cost The cost of this NetworkAction in [TODO]
205 * @param failed [description]
206 * @param var The lmm variable associated to this Action if it is part of a LMM component
208 NetworkAction(simgrid::surf::Model* model, double cost, bool failed, kernel::lmm::Variable* var)
209 : simgrid::surf::Action(model, cost, failed, var){};
211 void setState(simgrid::surf::Action::State state) override;
212 virtual std::list<LinkImpl*> links();
214 double latency_ = {};
215 double latCurrent_ = {};
222 #endif /* SURF_NETWORK_INTERFACE_HPP_ */