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 <boost/unordered_map.hpp>
16 #include "surf_interface.hpp"
17 #include "surf_routing.hpp"
18 #include "src/surf/PropertyHolder.hpp"
20 #include "simgrid/link.h"
37 /** @brief Callback signal fired when the state of a NetworkAction changes
38 * Signature: `void(NetworkAction *action, simgrid::surf::Action::State old, simgrid::surf::Action::State current)` */
39 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::Action::State, simgrid::surf::Action::State)>) networkActionStateChangedCallbacks;
50 /** @ingroup SURF_network_interface
51 * @brief SURF network model interface class
52 * @details A model is an object which handles the interactions between its Resources and its Actions
54 class NetworkModel : public Model {
56 /** @brief Constructor */
57 NetworkModel() : Model() { }
59 /** @brief Destructor */
61 lmm_system_free(maxminSystem_);
62 xbt_heap_free(actionHeap_);
67 * @brief Create a Link
69 * @param name The name of the Link
70 * @param bandwidth The initial bandwidth of the Link in bytes per second
71 * @param latency The initial latency of the Link in seconds
72 * @param policy The sharing policy of the Link
73 * @param properties Dictionary of properties associated to this Resource
74 * @return The created Link
76 virtual Link* createLink(const char *name, double bandwidth, double latency,
77 e_surf_link_sharing_policy_t policy, xbt_dict_t properties)=0;
80 * @brief Create a communication between two hosts.
81 * @details It makes calls to the routing part, and execute the communication
82 * between the two end points.
84 * @param src The source of the communication
85 * @param dst The destination of the communication
86 * @param size The size of the communication in bytes
87 * @param rate Allows to limit the transfer rate. Negative value means
89 * @return The action representing the communication
91 virtual Action *communicate(NetCard *src, NetCard *dst, double size, double rate)=0;
93 /** @brief Function pointer to the function to use to solve the lmm_system_t
95 * @param system The lmm_system_t to solve
97 void (*f_networkSolve)(lmm_system_t) = lmm_solve;
100 * @brief Get the right multiplicative factor for the latency.
101 * @details Depending on the model, the effective latency when sending
102 * a message might be different from the theoretical latency of the link,
103 * in function of the message size. In order to account for this, this
104 * function gets this factor.
106 * @param size The size of the message.
107 * @return The latency factor.
109 virtual double latencyFactor(double size);
112 * @brief Get the right multiplicative factor for the bandwidth.
113 * @details Depending on the model, the effective bandwidth when sending
114 * a message might be different from the theoretical bandwidth of the link,
115 * in function of the message size. In order to account for this, this
116 * function gets this factor.
118 * @param size The size of the message.
119 * @return The bandwidth factor.
121 virtual double bandwidthFactor(double size);
124 * @brief Get definitive bandwidth.
125 * @details It gives the minimum bandwidth between the one that would
126 * occur if no limitation was enforced, and the one arbitrary limited.
127 * @param rate The desired maximum bandwidth.
128 * @param bound The bandwidth with only the network taken into account.
129 * @param size The size of the message.
130 * @return The new bandwidth.
132 virtual double bandwidthConstraint(double rate, double bound, double size);
133 double next_occuring_event_full(double now) override;
139 /** @ingroup SURF_network_interface
140 * @brief SURF network link interface class
141 * @details A Link represents the link between two [hosts](\ref Host)
144 public simgrid::surf::Resource,
145 public simgrid::surf::PropertyHolder {
148 * @brief Link constructor
150 * @param model The NetworkModel associated to this Link
151 * @param name The name of the Link
152 * @param props Dictionary of properties associated to this Link
154 Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props);
157 * @brief Link constructor
159 * @param model The NetworkModel associated to this Link
160 * @param name The name of the Link
161 * @param props Dictionary of properties associated to this Link
162 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
164 Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
166 /* Link destruction logic */
167 /**************************/
171 void destroy(); // Must be called instead of the destructor
173 bool currentlyDestroying_ = false;
176 /** @brief Callback signal fired when a new Link is created.
177 * Signature: void(Link*) */
178 static simgrid::xbt::signal<void(simgrid::surf::Link*)> onCreation;
180 /** @brief Callback signal fired when a Link is destroyed.
181 * Signature: void(Link*) */
182 static simgrid::xbt::signal<void(simgrid::surf::Link*)> onDestruction;
184 /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off)
185 * Signature: `void(Link*)` */
186 static simgrid::xbt::signal<void(simgrid::surf::Link*)> onStateChange;
188 /** @brief Callback signal fired when a communication starts
189 * Signature: `void(NetworkAction *action, RoutingEdge *src, RoutingEdge *dst)` */
190 static simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst)> onCommunicate;
194 /** @brief Get the bandwidth in bytes per second of current Link */
195 virtual double getBandwidth();
197 /** @brief Update the bandwidth in bytes per second of current Link */
198 virtual void updateBandwidth(double value)=0;
200 /** @brief Get the latency in seconds of current Link */
201 virtual double getLatency();
203 /** @brief Update the latency in seconds of current Link */
204 virtual void updateLatency(double value)=0;
206 /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2: FULLDUPLEX) */
207 virtual int sharingPolicy();
209 /** @brief Check if the Link is used */
210 bool isUsed() override;
212 void turnOn() override;
213 void turnOff() override;
215 virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values. */
216 virtual void setBandwidthTrace(tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load). Trace must contain percentages (value between 0 and 1). */
217 virtual void setLatencyTrace(tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load). Trace must contain absolute values */
219 tmgr_trace_iterator_t m_stateEvent = NULL;
220 s_surf_metric_t m_latency = {1.0,0,NULL};
221 s_surf_metric_t m_bandwidth = {1.0,0,NULL};
225 void *getData() { return userData;}
226 void setData(void *d) { userData=d;}
228 void *userData = NULL;
230 /* List of all links */
232 static boost::unordered_map<std::string, Link *> *links;
234 static Link *byName(const char* name);
235 static int linksCount();
236 static Link **linksList();
237 static void linksExit();
243 /** @ingroup SURF_network_interface
244 * @brief SURF network action interface class
245 * @details A NetworkAction represents a communication between two [hosts](\ref Host)
247 class NetworkAction : public simgrid::surf::Action {
249 /** @brief Constructor
251 * @param model The NetworkModel associated to this NetworkAction
252 * @param cost The cost of this NetworkAction in [TODO]
253 * @param failed [description]
255 NetworkAction(simgrid::surf::Model *model, double cost, bool failed)
256 : simgrid::surf::Action(model, cost, failed) {}
259 * @brief NetworkAction constructor
261 * @param model The NetworkModel associated to this NetworkAction
262 * @param cost The cost of this NetworkAction in [TODO]
263 * @param failed [description]
264 * @param var The lmm variable associated to this Action if it is part of a
267 NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
268 : simgrid::surf::Action(model, cost, failed, var) {};
270 void setState(simgrid::surf::Action::State state) override;
276 const char* senderLinkName_;
278 xbt_fifo_item_t senderFifoItem_;
283 #endif /* SURF_NETWORK_INTERFACE_HPP_ */