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/resource/Model.hpp"
12 #include "src/kernel/resource/Resource.hpp"
13 #include "src/surf/PropertyHolder.hpp"
14 #include "src/surf/surf_interface.hpp"
17 #include <unordered_map>
29 /** @ingroup SURF_network_interface
30 * @brief SURF network model interface class
31 * @details A model is an object which handles the interactions between its Resources and its Actions
33 class NetworkModel : public kernel::resource::Model {
35 /** @brief Constructor */
36 NetworkModel() : Model() {}
38 /** @brief Destructor */
39 ~NetworkModel() override;
42 * @brief Create a Link
44 * @param name The name of the Link
45 * @param bandwidth The initial bandwidth of the Link in bytes per second
46 * @param latency The initial latency of the Link in seconds
47 * @param policy The sharing policy of the Link
49 virtual LinkImpl* createLink(const std::string& name, double bandwidth, double latency,
50 e_surf_link_sharing_policy_t policy) = 0;
53 * @brief Create a communication between two hosts.
54 * @details It makes calls to the routing part, and execute the communication
55 * between the two end points.
57 * @param src The source of the communication
58 * @param dst The destination of the communication
59 * @param size The size of the communication in bytes
60 * @param rate Allows to limit the transfer rate. Negative value means
62 * @return The action representing the communication
64 virtual kernel::resource::Action* communicate(s4u::Host* src, s4u::Host* dst, double size, double rate) = 0;
66 /** @brief Function pointer to the function to use to solve the lmm_system_t
68 * @param system The lmm_system_t to solve
70 void (*f_networkSolve)(lmm_system_t) = simgrid::kernel::lmm::lmm_solve;
73 * @brief Get the right multiplicative factor for the latency.
74 * @details Depending on the model, the effective latency when sending
75 * a message might be different from the theoretical latency of the link,
76 * in function of the message size. In order to account for this, this
77 * function gets this factor.
79 * @param size The size of the message.
80 * @return The latency factor.
82 virtual double latencyFactor(double size);
85 * @brief Get the right multiplicative factor for the bandwidth.
86 * @details Depending on the model, the effective bandwidth when sending
87 * a message might be different from the theoretical bandwidth of the link,
88 * in function of the message size. In order to account for this, this
89 * function gets this factor.
91 * @param size The size of the message.
92 * @return The bandwidth factor.
94 virtual double bandwidthFactor(double size);
97 * @brief Get definitive bandwidth.
98 * @details It gives the minimum bandwidth between the one that would
99 * occur if no limitation was enforced, and the one arbitrary limited.
100 * @param rate The desired maximum bandwidth.
101 * @param bound The bandwidth with only the network taken into account.
102 * @param size The size of the message.
103 * @return The new bandwidth.
105 virtual double bandwidthConstraint(double rate, double bound, double size);
106 double nextOccuringEventFull(double now) override;
108 LinkImpl* loopback_ = nullptr;
114 /** @ingroup SURF_network_interface
115 * @brief SURF network link interface class
116 * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
118 class LinkImpl : public simgrid::kernel::resource::Resource, public simgrid::surf::PropertyHolder {
120 LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint);
121 ~LinkImpl() override;
124 void destroy(); // Must be called instead of the destructor
126 bool currentlyDestroying_ = false;
129 /** @brief Public interface */
132 /** @brief Get the bandwidth in bytes per second of current Link */
133 virtual double bandwidth();
135 /** @brief Update the bandwidth in bytes per second of current Link */
136 virtual void setBandwidth(double value) = 0;
138 /** @brief Get the latency in seconds of current Link */
139 virtual double latency();
141 /** @brief Update the latency in seconds of current Link */
142 virtual void setLatency(double value) = 0;
144 /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
146 virtual int sharingPolicy();
148 /** @brief Check if the Link is used */
149 bool isUsed() override;
151 void turnOn() override;
152 void turnOff() override;
154 virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
155 Trace must contain boolean values. */
156 virtual void setBandwidthTrace(
157 tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
158 Trace must contain percentages (value between 0 and 1). */
159 virtual void setLatencyTrace(
160 tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
161 Trace must contain absolute values */
163 tmgr_trace_event_t stateEvent_ = nullptr;
164 Metric latency_ = {1.0, 0, nullptr};
165 Metric bandwidth_ = {1.0, 0, nullptr};
168 void* getData() { return userData; }
169 void setData(void* d) { userData = d; }
171 void* userData = nullptr;
173 /* List of all links. FIXME: should move to the Engine */
174 static std::unordered_map<std::string, LinkImpl*>* links;
177 static LinkImpl* byName(std::string name);
178 static int linksCount();
179 static LinkImpl** linksList();
180 static void linksList(std::vector<s4u::Link*>* linkList);
181 static void linksExit();
187 /** @ingroup SURF_network_interface
188 * @brief SURF network action interface class
189 * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
191 class NetworkAction : public simgrid::kernel::resource::Action {
193 /** @brief Constructor
195 * @param model The NetworkModel associated to this NetworkAction
196 * @param cost The cost of this NetworkAction in [TODO]
197 * @param failed [description]
199 NetworkAction(simgrid::kernel::resource::Model* model, double cost, bool failed)
200 : simgrid::kernel::resource::Action(model, cost, failed)
205 * @brief NetworkAction constructor
207 * @param model The NetworkModel associated to this NetworkAction
208 * @param cost The cost of this NetworkAction in [TODO]
209 * @param failed [description]
210 * @param var The lmm variable associated to this Action if it is part of a LMM component
212 NetworkAction(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var)
213 : simgrid::kernel::resource::Action(model, cost, failed, var){};
215 void setState(simgrid::kernel::resource::Action::State state) override;
216 virtual std::list<LinkImpl*> links();
218 double latency_ = {};
219 double latCurrent_ = {};
226 #endif /* SURF_NETWORK_INTERFACE_HPP_ */