Logo AND Algorithmique Numérique Distribuée

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