Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename the fields of surf::Model
[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 (maxminSystem_)
72           lmm_system_free(maxminSystem_);
73         if (actionHeap_)
74           xbt_heap_free(actionHeap_);
75         delete modifiedSet_;
76       }
77
78       /**
79        * @brief Create a Link
80        *
81        * @param name The name of the Link
82        * @param bandwidth The initial bandwidth of the Link in bytes per second
83        * @param latency The initial latency of the Link in seconds
84        * @param policy The sharing policy of the Link
85        * @param properties Dictionary of properties associated to this Resource
86        * @return The created Link
87        */
88       virtual Link* createLink(const char *name, double bandwidth, double latency,
89           e_surf_link_sharing_policy_t policy, xbt_dict_t properties)=0;
90
91       /**
92        * @brief Create a communication between two hosts.
93        * @details It makes calls to the routing part, and execute the communication
94        * between the two end points.
95        *
96        * @param src The source of the communication
97        * @param dst The destination of the communication
98        * @param size The size of the communication in bytes
99        * @param rate Allows to limit the transfer rate. Negative value means
100        * unlimited.
101        * @return The action representing the communication
102        */
103       virtual Action *communicate(NetCard *src, NetCard *dst,
104           double size, double rate)=0;
105
106       /** @brief Function pointer to the function to use to solve the lmm_system_t
107        *
108        * @param system The lmm_system_t to solve
109        */
110       void (*f_networkSolve)(lmm_system_t) = lmm_solve;
111
112       /**
113        * @brief Get the right multiplicative factor for the latency.
114        * @details Depending on the model, the effective latency when sending
115        * a message might be different from the theoretical latency of the link,
116        * in function of the message size. In order to account for this, this
117        * function gets this factor.
118        *
119        * @param size The size of the message.
120        * @return The latency factor.
121        */
122       virtual double latencyFactor(double size);
123
124       /**
125        * @brief Get the right multiplicative factor for the bandwidth.
126        * @details Depending on the model, the effective bandwidth when sending
127        * a message might be different from the theoretical bandwidth of the link,
128        * in function of the message size. In order to account for this, this
129        * function gets this factor.
130        *
131        * @param size The size of the message.
132        * @return The bandwidth factor.
133        */
134       virtual double bandwidthFactor(double size);
135
136       /**
137        * @brief Get definitive bandwidth.
138        * @details It gives the minimum bandwidth between the one that would
139        * occur if no limitation was enforced, and the one arbitrary limited.
140        * @param rate The desired maximum bandwidth.
141        * @param bound The bandwidth with only the network taken into account.
142        * @param size The size of the message.
143        * @return The new bandwidth.
144        */
145       virtual double bandwidthConstraint(double rate, double bound, double size);
146       double next_occuring_event_full(double now) override;
147     };
148
149     /************
150      * Resource *
151      ************/
152     /** @ingroup SURF_network_interface
153      * @brief SURF network link interface class
154      * @details A Link represents the link between two [hosts](\ref Host)
155      */
156     class Link :
157         public simgrid::surf::Resource,
158         public simgrid::surf::PropertyHolder {
159         public:
160       /**
161        * @brief Link constructor
162        *
163        * @param model The NetworkModel associated to this Link
164        * @param name The name of the Link
165        * @param props Dictionary of properties associated to this Link
166        */
167       Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props);
168
169       /**
170        * @brief Link constructor
171        *
172        * @param model The NetworkModel associated to this Link
173        * @param name The name of the Link
174        * @param props Dictionary of properties associated to this Link
175        * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
176        */
177       Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
178
179       /* Link destruction logic */
180       /**************************/
181         protected:
182       ~Link();
183         public:
184       void destroy(); // Must be called instead of the destructor
185         private:
186       bool currentlyDestroying_ = false;
187
188         public:
189       /** @brief Callback signal fired when a new Link is created.
190        *  Signature: void(Link*) */
191       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onCreation;
192
193       /** @brief Callback signal fired when a Link is destroyed.
194        *  Signature: void(Link*) */
195       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onDestruction;
196
197       /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off)
198        *  Signature: `void(Link*)` */
199       static simgrid::xbt::signal<void(simgrid::surf::Link*)> onStateChange;
200
201
202       /** @brief Get the bandwidth in bytes per second of current Link */
203       virtual double getBandwidth();
204
205       /** @brief Update the bandwidth in bytes per second of current Link */
206       virtual void updateBandwidth(double value)=0;
207
208       /** @brief Get the latency in seconds of current Link */
209       virtual double getLatency();
210
211       /** @brief Update the latency in seconds of current Link */
212       virtual void updateLatency(double value)=0;
213
214       /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2: FULLDUPLEX) */
215       virtual int sharingPolicy();
216
217       /** @brief Check if the Link is used */
218       bool isUsed() override;
219
220       void turnOn() override;
221       void turnOff() override;
222
223       virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values. */
224       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). */
225       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 */
226
227       tmgr_trace_iterator_t m_stateEvent = NULL;
228       s_surf_metric_t m_latency = {1.0,0,NULL};
229       s_surf_metric_t m_bandwidth = {1.0,0,NULL};
230
231       /* User data */
232         public:
233       void *getData()        { return userData;}
234       void  setData(void *d) { userData=d;}
235         private:
236       void *userData = NULL;
237
238       /* List of all links */
239         private:
240       static boost::unordered_map<std::string, Link *> *links;
241         public:
242       static Link *byName(const char* name);
243       static int linksCount();
244       static Link **linksList();
245       static void linksExit();
246     };
247
248     /**********
249      * Action *
250      **********/
251     /** @ingroup SURF_network_interface
252      * @brief SURF network action interface class
253      * @details A NetworkAction represents a communication between two [hosts](\ref Host)
254      */
255     class NetworkAction : public simgrid::surf::Action {
256     public:
257       /** @brief Constructor
258        *
259        * @param model The NetworkModel associated to this NetworkAction
260        * @param cost The cost of this  NetworkAction in [TODO]
261        * @param failed [description]
262        */
263       NetworkAction(simgrid::surf::Model *model, double cost, bool failed)
264     : simgrid::surf::Action(model, cost, failed) {}
265
266       /**
267        * @brief NetworkAction constructor
268        *
269        * @param model The NetworkModel associated to this NetworkAction
270        * @param cost The cost of this  NetworkAction in [TODO]
271        * @param failed [description]
272        * @param var The lmm variable associated to this Action if it is part of a
273        * LMM component
274        */
275       NetworkAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
276       : simgrid::surf::Action(model, cost, failed, var) {};
277
278       void setState(e_surf_action_state_t state);
279
280       double m_latency;
281       double m_latCurrent;
282       double m_weight;
283       double m_rate;
284       const char* p_senderLinkName;
285       double m_senderSize;
286       xbt_fifo_item_t p_senderFifoItem;
287     };
288   }
289 }
290
291 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
292
293