Logo AND Algorithmique Numérique Distribuée

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