1 /* Copyright (c) 2004-2017. 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/surf/PropertyHolder.hpp"
11 #include "src/surf/surf_interface.hpp"
14 #include <unordered_map>
26 /** @ingroup SURF_network_interface
27 * @brief SURF network model interface class
28 * @details A model is an object which handles the interactions between its Resources and its Actions
30 class NetworkModel : public Model {
32 /** @brief Constructor */
33 NetworkModel() : Model() {}
35 /** @brief Destructor */
36 ~NetworkModel() override;
39 * @brief Create a Link
41 * @param name The name of the Link
42 * @param bandwidth The initial bandwidth of the Link in bytes per second
43 * @param latency The initial latency of the Link in seconds
44 * @param policy The sharing policy of the Link
46 virtual LinkImpl* createLink(const std::string& name, double bandwidth, double latency,
47 e_surf_link_sharing_policy_t policy) = 0;
50 * @brief Create a communication between two hosts.
51 * @details It makes calls to the routing part, and execute the communication
52 * between the two end points.
54 * @param src The source of the communication
55 * @param dst The destination of the communication
56 * @param size The size of the communication in bytes
57 * @param rate Allows to limit the transfer rate. Negative value means
59 * @return The action representing the communication
61 virtual Action* communicate(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double size, double rate) = 0;
63 /** @brief Function pointer to the function to use to solve the lmm_system_t
65 * @param system The lmm_system_t to solve
67 void (*f_networkSolve)(lmm_system_t) = lmm_solve;
70 * @brief Get the right multiplicative factor for the latency.
71 * @details Depending on the model, the effective latency when sending
72 * a message might be different from the theoretical latency of the link,
73 * in function of the message size. In order to account for this, this
74 * function gets this factor.
76 * @param size The size of the message.
77 * @return The latency factor.
79 virtual double latencyFactor(double size);
82 * @brief Get the right multiplicative factor for the bandwidth.
83 * @details Depending on the model, the effective bandwidth when sending
84 * a message might be different from the theoretical bandwidth of the link,
85 * in function of the message size. In order to account for this, this
86 * function gets this factor.
88 * @param size The size of the message.
89 * @return The bandwidth factor.
91 virtual double bandwidthFactor(double size);
94 * @brief Get definitive bandwidth.
95 * @details It gives the minimum bandwidth between the one that would
96 * occur if no limitation was enforced, and the one arbitrary limited.
97 * @param rate The desired maximum bandwidth.
98 * @param bound The bandwidth with only the network taken into account.
99 * @param size The size of the message.
100 * @return The new bandwidth.
102 virtual double bandwidthConstraint(double rate, double bound, double size);
103 double nextOccuringEventFull(double now) override;
105 LinkImpl* loopback_ = nullptr;
111 /** @ingroup SURF_network_interface
112 * @brief SURF network link interface class
113 * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
115 class LinkImpl : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder {
117 LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, lmm_constraint_t constraint);
118 ~LinkImpl() override;
121 void destroy(); // Must be called instead of the destructor
123 bool currentlyDestroying_ = false;
126 /** @brief Public interface */
129 /** @brief Get the bandwidth in bytes per second of current Link */
130 virtual double bandwidth();
132 /** @brief Update the bandwidth in bytes per second of current Link */
133 virtual void setBandwidth(double value) = 0;
135 /** @brief Get the latency in seconds of current Link */
136 virtual double latency();
138 /** @brief Update the latency in seconds of current Link */
139 virtual void setLatency(double value) = 0;
141 /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
143 virtual int sharingPolicy();
145 /** @brief Check if the Link is used */
146 bool isUsed() override;
148 void turnOn() override;
149 void turnOff() override;
151 virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
152 Trace must contain boolean values. */
153 virtual void setBandwidthTrace(
154 tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
155 Trace must contain percentages (value between 0 and 1). */
156 virtual void setLatencyTrace(
157 tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
158 Trace must contain absolute values */
160 tmgr_trace_event_t stateEvent_ = nullptr;
161 s_surf_metric_t latency_ = {1.0, 0, nullptr};
162 s_surf_metric_t bandwidth_ = {1.0, 0, nullptr};
165 void* getData() { return userData; }
166 void setData(void* d) { userData = d; }
168 void* userData = nullptr;
170 /* List of all links. FIXME: should move to the Engine */
171 static std::unordered_map<std::string, LinkImpl*>* links;
174 static LinkImpl* byName(std::string name);
175 static int linksCount();
176 static LinkImpl** linksList();
177 static void linksExit();
183 /** @ingroup SURF_network_interface
184 * @brief SURF network action interface class
185 * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
187 class NetworkAction : public simgrid::surf::Action {
189 /** @brief Constructor
191 * @param model The NetworkModel associated to this NetworkAction
192 * @param cost The cost of this NetworkAction in [TODO]
193 * @param failed [description]
195 NetworkAction(simgrid::surf::Model* model, double cost, bool failed) : simgrid::surf::Action(model, cost, failed) {}
198 * @brief NetworkAction constructor
200 * @param model The NetworkModel associated to this NetworkAction
201 * @param cost The cost of this NetworkAction in [TODO]
202 * @param failed [description]
203 * @param var The lmm variable associated to this Action if it is part of a LMM component
205 NetworkAction(simgrid::surf::Model* model, double cost, bool failed, lmm_variable_t var)
206 : simgrid::surf::Action(model, cost, failed, var){};
208 void setState(simgrid::surf::Action::State state) override;
209 virtual std::list<LinkImpl*> links();
211 double latency_ = {};
212 double latCurrent_ = {};
219 #endif /* SURF_NETWORK_INTERFACE_HPP_ */