Logo AND Algorithmique Numérique Distribuée

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