Logo AND Algorithmique Numérique Distribuée

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