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/surf/PropertyHolder.hpp"
12 #include "src/surf/surf_interface.hpp"
15 #include <unordered_map>
27 /** @ingroup SURF_network_interface
28 * @brief SURF network model interface class
29 * @details A model is an object which handles the interactions between its Resources and its Actions
31 class NetworkModel : public Model {
33 /** @brief Constructor */
34 NetworkModel() : Model() {}
36 /** @brief Destructor */
37 ~NetworkModel() override;
40 * @brief Create a Link
42 * @param name The name of the Link
43 * @param bandwidth The initial bandwidth of the Link in bytes per second
44 * @param latency The initial latency of the Link in seconds
45 * @param policy The sharing policy of the Link
47 virtual LinkImpl* createLink(const std::string& name, double bandwidth, double latency,
48 e_surf_link_sharing_policy_t policy) = 0;
51 * @brief Create a communication between two hosts.
52 * @details It makes calls to the routing part, and execute the communication
53 * between the two end points.
55 * @param src The source of the communication
56 * @param dst The destination of the communication
57 * @param size The size of the communication in bytes
58 * @param rate Allows to limit the transfer rate. Negative value means
60 * @return The action representing the communication
62 virtual Action* communicate(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double size, double rate) = 0;
64 /** @brief Function pointer to the function to use to solve the lmm_system_t
66 * @param system The lmm_system_t to solve
68 void (*f_networkSolve)(lmm_system_t) = simgrid::kernel::lmm::lmm_solve;
71 * @brief Get the right multiplicative factor for the latency.
72 * @details Depending on the model, the effective latency when sending
73 * a message might be different from the theoretical latency of the link,
74 * in function of the message size. In order to account for this, this
75 * function gets this factor.
77 * @param size The size of the message.
78 * @return The latency factor.
80 virtual double latencyFactor(double size);
83 * @brief Get the right multiplicative factor for the bandwidth.
84 * @details Depending on the model, the effective bandwidth when sending
85 * a message might be different from the theoretical bandwidth of the link,
86 * in function of the message size. In order to account for this, this
87 * function gets this factor.
89 * @param size The size of the message.
90 * @return The bandwidth factor.
92 virtual double bandwidthFactor(double size);
95 * @brief Get definitive bandwidth.
96 * @details It gives the minimum bandwidth between the one that would
97 * occur if no limitation was enforced, and the one arbitrary limited.
98 * @param rate The desired maximum bandwidth.
99 * @param bound The bandwidth with only the network taken into account.
100 * @param size The size of the message.
101 * @return The new bandwidth.
103 virtual double bandwidthConstraint(double rate, double bound, double size);
104 double nextOccuringEventFull(double now) override;
106 LinkImpl* loopback_ = nullptr;
112 /** @ingroup SURF_network_interface
113 * @brief SURF network link interface class
114 * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
116 class LinkImpl : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder {
118 LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, lmm_constraint_t constraint);
119 ~LinkImpl() override;
122 void destroy(); // Must be called instead of the destructor
124 bool currentlyDestroying_ = false;
127 /** @brief Public interface */
130 /** @brief Get the bandwidth in bytes per second of current Link */
131 virtual double bandwidth();
133 /** @brief Update the bandwidth in bytes per second of current Link */
134 virtual void setBandwidth(double value) = 0;
136 /** @brief Get the latency in seconds of current Link */
137 virtual double latency();
139 /** @brief Update the latency in seconds of current Link */
140 virtual void setLatency(double value) = 0;
142 /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
144 virtual int sharingPolicy();
146 /** @brief Check if the Link is used */
147 bool isUsed() override;
149 void turnOn() override;
150 void turnOff() override;
152 virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
153 Trace must contain boolean values. */
154 virtual void setBandwidthTrace(
155 tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
156 Trace must contain percentages (value between 0 and 1). */
157 virtual void setLatencyTrace(
158 tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
159 Trace must contain absolute values */
161 tmgr_trace_event_t stateEvent_ = nullptr;
162 s_surf_metric_t latency_ = {1.0, 0, nullptr};
163 s_surf_metric_t bandwidth_ = {1.0, 0, nullptr};
166 void* getData() { return userData; }
167 void setData(void* d) { userData = d; }
169 void* userData = nullptr;
171 /* List of all links. FIXME: should move to the Engine */
172 static std::unordered_map<std::string, LinkImpl*>* links;
175 static LinkImpl* byName(std::string name);
176 static int linksCount();
177 static LinkImpl** linksList();
178 static void linksList(std::vector<s4u::Link*>* linkList);
179 static void linksExit();
185 /** @ingroup SURF_network_interface
186 * @brief SURF network action interface class
187 * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
189 class NetworkAction : public simgrid::surf::Action {
191 /** @brief Constructor
193 * @param model The NetworkModel associated to this NetworkAction
194 * @param cost The cost of this NetworkAction in [TODO]
195 * @param failed [description]
197 NetworkAction(simgrid::surf::Model* model, double cost, bool failed) : simgrid::surf::Action(model, cost, failed) {}
200 * @brief NetworkAction constructor
202 * @param model The NetworkModel associated to this NetworkAction
203 * @param cost The cost of this NetworkAction in [TODO]
204 * @param failed [description]
205 * @param var The lmm variable associated to this Action if it is part of a LMM component
207 NetworkAction(simgrid::surf::Model* model, double cost, bool failed, lmm_variable_t var)
208 : simgrid::surf::Action(model, cost, failed, var){};
210 void setState(simgrid::surf::Action::State state) override;
211 virtual std::list<LinkImpl*> links();
213 double latency_ = {};
214 double latCurrent_ = {};
221 #endif /* SURF_NETWORK_INTERFACE_HPP_ */