1 /* Copyright (c) 2004-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SURF_NETWORK_INTERFACE_HPP_
8 #define SURF_NETWORK_INTERFACE_HPP_
12 #include <unordered_map>
14 #include "src/surf/PropertyHolder.hpp"
15 #include "src/surf/surf_interface.hpp"
18 #include "simgrid/s4u/Link.hpp"
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 /** @brief Constructor */
37 NetworkModel() : Model() { }
39 /** @brief Destructor */
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* createLink(const char* name, double bandwidth, double latency,
51 e_surf_link_sharing_policy_t 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(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double size, double rate) = 0;
67 /** @brief Function pointer to the function to use to solve the lmm_system_t
69 * @param system The lmm_system_t to solve
71 void (*f_networkSolve)(lmm_system_t) = lmm_solve;
74 * @brief Get the right multiplicative factor for the latency.
75 * @details Depending on the model, the effective latency when sending
76 * a message might be different from the theoretical latency of the link,
77 * in function of the message size. In order to account for this, this
78 * function gets this factor.
80 * @param size The size of the message.
81 * @return The latency factor.
83 virtual double latencyFactor(double size);
86 * @brief Get the right multiplicative factor for the bandwidth.
87 * @details Depending on the model, the effective bandwidth when sending
88 * a message might be different from the theoretical bandwidth of the link,
89 * in function of the message size. In order to account for this, this
90 * function gets this factor.
92 * @param size The size of the message.
93 * @return The bandwidth factor.
95 virtual double bandwidthFactor(double size);
98 * @brief Get definitive bandwidth.
99 * @details It gives the minimum bandwidth between the one that would
100 * occur if no limitation was enforced, and the one arbitrary limited.
101 * @param rate The desired maximum bandwidth.
102 * @param bound The bandwidth with only the network taken into account.
103 * @param size The size of the message.
104 * @return The new bandwidth.
106 virtual double bandwidthConstraint(double rate, double bound, double size);
107 double nextOccuringEventFull(double now) override;
109 LinkImpl* loopback_ = nullptr;
115 /** @ingroup SURF_network_interface
116 * @brief SURF network link interface class
117 * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
119 class LinkImpl : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder {
121 LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint);
122 ~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(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(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_iterator_t stateEvent_ = nullptr;
162 s_surf_metric_t latency_ = {1.0, 0, nullptr};
163 s_surf_metric_t 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 */
174 static std::unordered_map<std::string, LinkImpl*>* links;
177 static LinkImpl* byName(const char* name);
178 static int linksCount();
179 static LinkImpl** linksList();
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)
199 : simgrid::surf::Action(model, cost, failed) {}
202 * @brief NetworkAction constructor
204 * @param model The NetworkModel associated to this NetworkAction
205 * @param cost The cost of this NetworkAction in [TODO]
206 * @param failed [description]
207 * @param var The lmm variable associated to this Action if it is part of a
210 NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
211 : simgrid::surf::Action(model, cost, failed, var) {};
213 void setState(simgrid::surf::Action::State state) override;
219 const char* senderLinkName_;
221 xbt_fifo_item_t senderFifoItem_;
226 #endif /* SURF_NETWORK_INTERFACE_HPP_ */