Logo AND Algorithmique Numérique Distribuée

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