1 /* Copyright (c) 2004-2014. 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 #include <boost/unordered_map.hpp>
10 #include "surf_interface.hpp"
11 #include "surf_routing.hpp"
13 #include "simgrid/link.h"
15 #ifndef SURF_NETWORK_INTERFACE_HPP_
16 #define SURF_NETWORK_INTERFACE_HPP_
22 typedef NetworkModel *NetworkModelPtr;
25 typedef NetworkAction *NetworkActionPtr;
31 /** @ingroup SURF_callbacks
32 * @brief Callbacks handler which emits the callbacks after Link creation
33 * @details Callback functions have the following signature: `void(Link*)`
35 XBT_PUBLIC_DATA( surf_callback(void, Link*)) networkLinkCreatedCallbacks;
37 /** @ingroup SURF_callbacks
38 * @brief Callbacks handler which emits the callbacks after Link destruction
39 * @details Callback functions have the following signature: `void(Link*)`
41 XBT_PUBLIC_DATA( surf_callback(void, Link*)) networkLinkDestructedCallbacks;
43 /** @ingroup SURF_callbacks
44 * @brief Callbacks handler which emits the callbacks after Link State changed
45 * @details Callback functions have the following signature: `void(LinkActionPtr action, e_surf_resource_state_t old, e_surf_resource_state_t current)`
47 XBT_PUBLIC_DATA( surf_callback(void, Link*, e_surf_resource_state_t, e_surf_resource_state_t)) networkLinkStateChangedCallbacks;
49 /** @ingroup SURF_callbacks
50 * @brief Callbacks handler which emits the callbacks after NetworkAction State changed
51 * @details Callback functions have the following signature: `void(NetworkActionPtr action, e_surf_action_state_t old, e_surf_action_state_t current)`
53 XBT_PUBLIC_DATA( surf_callback(void, NetworkActionPtr, e_surf_action_state_t, e_surf_action_state_t)) networkActionStateChangedCallbacks;
55 /** @ingroup SURF_callbacks
56 * @brief Callbacks handler which emits the callbacks after communication created
57 * @details Callback functions have the following signature: `void(NetworkActionPtr action, RoutingEdgePtr src, RoutingEdgePtr dst, double size, double rate)`
59 XBT_PUBLIC_DATA( surf_callback(void, NetworkActionPtr, RoutingEdgePtr src, RoutingEdgePtr dst, double size, double rate)) networkCommunicateCallbacks;
64 XBT_PUBLIC(void) netlink_parse_init(sg_platf_link_cbarg_t link);
66 XBT_PUBLIC(void) net_add_traces();
71 /** @ingroup SURF_network_interface
72 * @brief SURF network model interface class
73 * @details A model is an object which handles the interactions between its Resources and its Actions
75 class NetworkModel : public Model {
77 /** @brief NetworkModel constructor */
78 NetworkModel() : Model("network") {
79 f_networkSolve = lmm_solve;
82 /** @brief NetworkModel constructor */
83 NetworkModel(const char *name) : Model(name) {
84 f_networkSolve = lmm_solve;
87 /** @brief The destructor of the NetworkModel */
90 lmm_system_free(p_maxminSystem);
92 xbt_heap_free(p_actionHeap);
98 * @brief Create a Link
100 * @param name The name of the Link
101 * @param bw_initial The initial bandwidth of the Link in bytes per second
102 * @param bw_trace The trace associated to the Link bandwidth
103 * @param lat_initial The initial latency of the Link in seconds
104 * @param lat_trace The trace associated to the Link latency
105 * @param state_initial The initial Link (state)[e_surf_resource_state_t]
106 * @param state_trace The trace associated to the Link (state)[e_surf_resource_state_t]
107 * @param policy The sharing policy of the Link
108 * @param properties Dictionary of properties associated to this Resource
109 * @return The created Link
111 virtual Link* createLink(const char *name,
113 tmgr_trace_t bw_trace,
115 tmgr_trace_t lat_trace,
116 e_surf_resource_state_t state_initial,
117 tmgr_trace_t state_trace,
118 e_surf_link_sharing_policy_t policy,
119 xbt_dict_t properties)=0;
121 virtual void gapAppend(double /*size*/, const Link* /*link*/, NetworkActionPtr /*action*/) {};
124 * @brief Create a communication between two hosts.
125 * @details It makes calls to the routing part, and execute the communication
126 * between the two end points.
128 * @param src The source of the communication
129 * @param dst The destination of the communication
130 * @param size The size of the communication in bytes
131 * @param rate Allows to limit the transfer rate. Negative value means
133 * @return The action representing the communication
135 virtual ActionPtr communicate(RoutingEdgePtr src, RoutingEdgePtr dst,
136 double size, double rate)=0;
139 * @brief Function pointer to the function to use to solve the lmm_system_t
141 * @param system The lmm_system_t to solve
143 void (*f_networkSolve)(lmm_system_t);
146 * @brief Get the right multiplicative factor for the latency.
147 * @details Depending on the model, the effective latency when sending
148 * a message might be different from the theoretical latency of the link,
149 * in function of the message size. In order to account for this, this
150 * function gets this factor.
152 * @param size The size of the message.
153 * @return The latency factor.
155 virtual double latencyFactor(double size);
158 * @brief Get the right multiplicative factor for the bandwidth.
159 * @details Depending on the model, the effective bandwidth when sending
160 * a message might be different from the theoretical bandwidth of the link,
161 * in function of the message size. In order to account for this, this
162 * function gets this factor.
164 * @param size The size of the message.
165 * @return The bandwidth factor.
167 virtual double bandwidthFactor(double size);
170 * @brief Get definitive bandwidth.
171 * @details It gives the minimum bandwidth between the one that would
172 * occur if no limitation was enforced, and the one arbitrary limited.
173 * @param rate The desired maximum bandwidth.
174 * @param bound The bandwidth with only the network taken into account.
175 * @param size The size of the message.
176 * @return The new bandwidth.
178 virtual double bandwidthConstraint(double rate, double bound, double size);
179 double shareResourcesFull(double now);
180 bool m_haveGap = false;
186 /** @ingroup SURF_network_interface
187 * @brief SURF network link interface class
188 * @details A Link represents the link between two [hosts](\ref Host)
190 class Link : public Resource {
193 * @brief Link constructor
195 * @param model The NetworkModel associated to this Link
196 * @param name The name of the Link
197 * @param props Dictionary of properties associated to this Link
199 Link(NetworkModelPtr model, const char *name, xbt_dict_t props);
202 * @brief Link constructor
204 * @param model The NetworkModel associated to this Link
205 * @param name The name of the Link
206 * @param props Dictionary of properties associated to this Link
207 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
208 * @param history [TODO]
209 * @param state_trace [TODO]
211 Link(NetworkModelPtr model, const char *name, xbt_dict_t props,
212 lmm_constraint_t constraint,
213 tmgr_history_t history,
214 tmgr_trace_t state_trace);
216 /** @brief Link destructor */
219 /** @brief Get the bandwidth in bytes per second of current Link */
220 virtual double getBandwidth();
222 /** @brief Update the bandwidth in bytes per second of current Link */
223 virtual void updateBandwidth(double value, double date=surf_get_clock())=0;
225 /** @brief Get the latency in seconds of current Link */
226 virtual double getLatency();
228 /** @brief Update the latency in seconds of current Link */
229 virtual void updateLatency(double value, double date=surf_get_clock())=0;
232 * @brief Check if the Link is shared
234 * @return true if the current NetwokrLink is shared, false otherwise
236 virtual bool isShared();
238 /** @brief Check if the Link is used */
241 void setState(e_surf_resource_state_t state);
243 /* Using this object with the public part of
244 model does not make sense */
246 tmgr_trace_event_t p_latEvent;
249 tmgr_trace_event_t p_stateEvent = NULL;
250 s_surf_metric_t p_power;
254 void *getData() { return userData;}
255 void setData(void *d) { userData=d;}
257 void *userData = NULL;
259 /* List of all links */
261 static boost::unordered_map<std::string, Link *> *links;
263 static Link *byName(const char* name);
264 static int linksAmount();
265 static Link **linksList();
266 static void linksExit();
272 /** @ingroup SURF_network_interface
273 * @brief SURF network action interface class
274 * @details A NetworkAction represents a communication between two [hosts](\ref Host)
276 class NetworkAction : public Action {
279 * @brief NetworkAction constructor
281 * @param model The NetworkModel associated to this NetworkAction
282 * @param cost The cost of this NetworkAction in [TODO]
283 * @param failed [description]
285 NetworkAction(ModelPtr model, double cost, bool failed)
286 : Action(model, cost, failed) {}
289 * @brief NetworkAction constructor
291 * @param model The NetworkModel associated to this NetworkAction
292 * @param cost The cost of this NetworkAction in [TODO]
293 * @param failed [description]
294 * @param var The lmm variable associated to this Action if it is part of a
297 NetworkAction(ModelPtr model, double cost, bool failed, lmm_variable_t var)
298 : Action(model, cost, failed, var) {};
300 void setState(e_surf_action_state_t state);
302 #ifdef HAVE_LATENCY_BOUND_TRACKING
304 * @brief Check if the action is limited by latency.
306 * @return 1 if action is limited by latency, 0 otherwise
308 virtual int getLatencyLimited() {return m_latencyLimited;}
315 const char* p_senderLinkName;
318 xbt_fifo_item_t p_senderFifoItem;
319 #ifdef HAVE_LATENCY_BOUND_TRACKING
320 int m_latencyLimited;
325 #endif /* SURF_NETWORK_INTERFACE_HPP_ */