Logo AND Algorithmique Numérique Distribuée

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