Logo AND Algorithmique Numérique Distribuée

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