Logo AND Algorithmique Numérique Distribuée

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