Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove the state trace from the Link constructor
[simgrid.git] / src / surf / network_interface.hpp
1 /* Copyright (c) 2004-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SURF_NETWORK_INTERFACE_HPP_
8 #define SURF_NETWORK_INTERFACE_HPP_
9
10 #include <xbt/base.h>
11
12 #include <boost/unordered_map.hpp>
13
14 #include "xbt/fifo.h"
15 #include "xbt/dict.h"
16 #include "surf_interface.hpp"
17 #include "surf_routing.hpp"
18 #include "src/surf/PropertyHolder.hpp"
19
20 #include "simgrid/link.h"
21
22 /***********
23  * Classes *
24  ***********/
25
26 namespace simgrid {
27   namespace surf {
28
29     class NetworkModel;
30     class NetworkAction;
31
32     /*************
33      * Callbacks *
34      *************/
35
36
37     /** @brief Callback signal fired when the state of a NetworkAction changes
38      *  Signature: `void(NetworkAction *action, e_surf_action_state_t old, e_surf_action_state_t current)` */
39     XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, e_surf_action_state_t, e_surf_action_state_t)>) networkActionStateChangedCallbacks;
40
41     /** @brief Callback signal fired when a NetworkAction is created (when a communication starts)
42      *  Signature: `void(NetworkAction *action, RoutingEdge *src, RoutingEdge *dst, double size, double rate)` */
43     XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst, double size, double rate)>) networkCommunicateCallbacks;
44
45   }
46 }
47
48 /*********
49  * Tools *
50  *********/
51 XBT_PUBLIC(void) netlink_parse_init(sg_platf_link_cbarg_t link);
52
53 /*********
54  * Model *
55  *********/
56
57 namespace simgrid {
58   namespace surf {
59
60     /** @ingroup SURF_network_interface
61      * @brief SURF network model interface class
62      * @details A model is an object which handles the interactions between its Resources and its Actions
63      */
64     class NetworkModel : public Model {
65     public:
66       /** @brief Constructor */
67       NetworkModel() : Model() { }
68
69       /** @brief Destructor */
70       ~NetworkModel() {
71         if (p_maxminSystem)
72           lmm_system_free(p_maxminSystem);
73         if (p_actionHeap)
74           xbt_heap_free(p_actionHeap);
75         if (p_modifiedSet)
76           delete p_modifiedSet;
77       }
78
79       /**
80        * @brief Create a Link
81        *
82        * @param name The name of the Link
83        * @param bw_initial The initial bandwidth of the Link in bytes per second
84        * @param bw_trace The trace associated to the Link bandwidth
85        * @param lat_initial The initial latency of the Link in seconds
86        * @param lat_trace The trace associated to the Link latency
87        * @param policy The sharing policy of the Link
88        * @param properties Dictionary of properties associated to this Resource
89        * @return The created Link
90        */
91       virtual Link* createLink(const char *name,
92           double bw_initial,
93           tmgr_trace_t bw_trace,
94           double lat_initial,
95           tmgr_trace_t lat_trace,
96           e_surf_link_sharing_policy_t policy,
97           xbt_dict_t properties)=0;
98
99       /**
100        * @brief Create a communication between two hosts.
101        * @details It makes calls to the routing part, and execute the communication
102        * between the two end points.
103        *
104        * @param src The source of the communication
105        * @param dst The destination of the communication
106        * @param size The size of the communication in bytes
107        * @param rate Allows to limit the transfer rate. Negative value means
108        * unlimited.
109        * @return The action representing the communication
110        */
111       virtual Action *communicate(NetCard *src, NetCard *dst,
112           double size, double rate)=0;
113
114       /** @brief Function pointer to the function to use to solve the lmm_system_t
115        *
116        * @param system The lmm_system_t to solve
117        */
118       void (*f_networkSolve)(lmm_system_t) = lmm_solve;
119
120       /**
121        * @brief Get the right multiplicative factor for the latency.
122        * @details Depending on the model, the effective latency when sending
123        * a message might be different from the theoretical latency of the link,
124        * in function of the message size. In order to account for this, this
125        * function gets this factor.
126        *
127        * @param size The size of the message.
128        * @return The latency factor.
129        */
130       virtual double latencyFactor(double size);
131
132       /**
133        * @brief Get the right multiplicative factor for the bandwidth.
134        * @details Depending on the model, the effective bandwidth when sending
135        * a message might be different from the theoretical bandwidth of the link,
136        * in function of the message size. In order to account for this, this
137        * function gets this factor.
138        *
139        * @param size The size of the message.
140        * @return The bandwidth factor.
141        */
142       virtual double bandwidthFactor(double size);
143
144       /**
145        * @brief Get definitive bandwidth.
146        * @details It gives the minimum bandwidth between the one that would
147        * occur if no limitation was enforced, and the one arbitrary limited.
148        * @param rate The desired maximum bandwidth.
149        * @param bound The bandwidth with only the network taken into account.
150        * @param size The size of the message.
151        * @return The new bandwidth.
152        */
153       virtual double bandwidthConstraint(double rate, double bound, double size);
154       double next_occuring_event_full(double now) override;
155     };
156
157     /************
158      * Resource *
159      ************/
160     /** @ingroup SURF_network_interface
161      * @brief SURF network link interface class
162      * @details A Link represents the link between two [hosts](\ref Host)
163      */
164     class Link :
165         public simgrid::surf::Resource,
166         public simgrid::surf::PropertyHolder {
167         public:
168       /**
169        * @brief Link constructor
170        *
171        * @param model The NetworkModel associated to this Link
172        * @param name The name of the Link
173        * @param props Dictionary of properties associated to this Link
174        */
175       Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props);
176
177       /**
178        * @brief Link constructor
179        *
180        * @param model The NetworkModel associated to this Link
181        * @param name The name of the Link
182        * @param props Dictionary of properties associated to this Link
183        * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
184        */
185       Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
186
187       /** @brief State traces are used to model the on/off state of the link */
188       void setStateTrace(tmgr_trace_t trace);
189
190       /* Link destruction logic */
191       /**************************/
192         protected:
193       ~Link();
194         public:
195       void destroy(); // Must be called instead of the destructor
196         private:
197       bool currentlyDestroying_ = false;
198
199         public:
200       /** @brief Callback signal fired when a new Link is created.
201        *  Signature: void(Link*) */
202       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onCreation;
203
204       /** @brief Callback signal fired when a Link is destroyed.
205        *  Signature: void(Link*) */
206       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onDestruction;
207
208       /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off)
209        *  Signature: `void(Link*)` */
210       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onStateChange;
211
212
213       /** @brief Get the bandwidth in bytes per second of current Link */
214       virtual double getBandwidth();
215
216       /** @brief Update the bandwidth in bytes per second of current Link */
217       virtual void updateBandwidth(double value)=0;
218
219       /** @brief Get the latency in seconds of current Link */
220       virtual double getLatency();
221
222       /** @brief Update the latency in seconds of current Link */
223       virtual void updateLatency(double value)=0;
224
225       /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2: FULLDUPLEX) */
226       virtual int sharingPolicy();
227
228       /** @brief Check if the Link is used */
229       bool isUsed() override;
230
231       void turnOn() override;
232       void turnOff() override;
233
234       virtual void set_state_trace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values. */
235       virtual void set_bandwidth_trace(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). */
236       virtual void set_latency_trace(tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load). Trace must contain absolute values */
237
238       tmgr_trace_iterator_t m_stateEvent = NULL;
239       s_surf_metric_t m_latency = {1.0,0,NULL};
240       s_surf_metric_t m_bandwidth = {1.0,0,NULL};
241
242       /* User data */
243         public:
244       void *getData()        { return userData;}
245       void  setData(void *d) { userData=d;}
246         private:
247       void *userData = NULL;
248
249       /* List of all links */
250         private:
251       static boost::unordered_map<std::string, Link *> *links;
252         public:
253       static Link *byName(const char* name);
254       static int linksCount();
255       static Link **linksList();
256       static void linksExit();
257     };
258
259     /**********
260      * Action *
261      **********/
262     /** @ingroup SURF_network_interface
263      * @brief SURF network action interface class
264      * @details A NetworkAction represents a communication between two [hosts](\ref Host)
265      */
266     class NetworkAction : public simgrid::surf::Action {
267     public:
268       /** @brief Constructor
269        *
270        * @param model The NetworkModel associated to this NetworkAction
271        * @param cost The cost of this  NetworkAction in [TODO]
272        * @param failed [description]
273        */
274       NetworkAction(simgrid::surf::Model *model, double cost, bool failed)
275     : simgrid::surf::Action(model, cost, failed) {}
276
277       /**
278        * @brief NetworkAction constructor
279        *
280        * @param model The NetworkModel associated to this NetworkAction
281        * @param cost The cost of this  NetworkAction in [TODO]
282        * @param failed [description]
283        * @param var The lmm variable associated to this Action if it is part of a
284        * LMM component
285        */
286       NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
287       : simgrid::surf::Action(model, cost, failed, var) {};
288
289       void setState(e_surf_action_state_t state);
290
291       double m_latency;
292       double m_latCurrent;
293       double m_weight;
294       double m_rate;
295       const char* p_senderLinkName;
296       double m_senderSize;
297       xbt_fifo_item_t p_senderFifoItem;
298     };
299   }
300 }
301
302 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
303
304