Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the scope of some #include, and cut useless ones
[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/trace_mgr.hpp"
15
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   static simgrid::config::Flag<double> cfg_tcp_gamma;
36   static simgrid::config::Flag<bool> cfg_crosstraffic;
37
38   explicit NetworkModel(kernel::resource::Model::UpdateAlgo algo) : Model(algo) {}
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   /**
67    * @brief Get the right multiplicative factor for the latency.
68    * @details Depending on the model, the effective latency when sending
69    * a message might be different from the theoretical latency of the link,
70    * in function of the message size. In order to account for this, this
71    * function gets this factor.
72    *
73    * @param size The size of the message.
74    * @return The latency factor.
75    */
76   virtual double latencyFactor(double size);
77
78   /**
79    * @brief Get the right multiplicative factor for the bandwidth.
80    * @details Depending on the model, the effective bandwidth when sending
81    * a message might be different from the theoretical bandwidth of the link,
82    * in function of the message size. In order to account for this, this
83    * function gets this factor.
84    *
85    * @param size The size of the message.
86    * @return The bandwidth factor.
87    */
88   virtual double bandwidthFactor(double size);
89
90   /**
91    * @brief Get definitive bandwidth.
92    * @details It gives the minimum bandwidth between the one that would
93    * occur if no limitation was enforced, and the one arbitrary limited.
94    * @param rate The desired maximum bandwidth.
95    * @param bound The bandwidth with only the network taken into account.
96    * @param size The size of the message.
97    * @return The new bandwidth.
98    */
99   virtual double bandwidthConstraint(double rate, double bound, double size);
100   double next_occuring_event_full(double now) override;
101
102   LinkImpl* loopback_ = nullptr;
103 };
104
105 /************
106  * Resource *
107  ************/
108 /** @ingroup SURF_network_interface
109  * @brief SURF network link interface class
110  * @details A Link represents the link between two [hosts](\ref simgrid::surf::HostImpl)
111  */
112 class LinkImpl : public simgrid::kernel::resource::Resource, public simgrid::surf::PropertyHolder {
113 protected:
114   LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint);
115   ~LinkImpl() override;
116
117 public:
118   void destroy(); // Must be called instead of the destructor
119 private:
120   bool currentlyDestroying_ = false;
121
122 public:
123   /** @brief Public interface */
124   s4u::Link piface_;
125
126   /** @brief Get the bandwidth in bytes per second of current Link */
127   virtual double bandwidth();
128
129   /** @brief Update the bandwidth in bytes per second of current Link */
130   virtual void setBandwidth(double value) = 0;
131
132   /** @brief Get the latency in seconds of current Link */
133   virtual double latency();
134
135   /** @brief Update the latency in seconds of current Link */
136   virtual void setLatency(double value) = 0;
137
138   /** @brief The sharing policy is a @{link e_surf_link_sharing_policy_t::EType} (0: FATPIPE, 1: SHARED, 2:
139    * SPLITDUPLEX) */
140   virtual int sharingPolicy();
141
142   /** @brief Check if the Link is used */
143   bool is_used() override;
144
145   void turn_on() override;
146   void turn_off() override;
147
148   virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF).
149                                                           Trace must contain boolean values. */
150   virtual void setBandwidthTrace(
151       tmgr_trace_t trace); /*< setup the trace file with bandwidth events (peak speed changes due to external load).
152                                    Trace must contain percentages (value between 0 and 1). */
153   virtual void setLatencyTrace(
154       tmgr_trace_t trace); /*< setup the trace file with latency events (peak latency changes due to external load).
155                                    Trace must contain absolute values */
156
157   tmgr_trace_event_t stateEvent_    = nullptr;
158   Metric latency_                   = {1.0, 0, nullptr};
159   Metric bandwidth_                 = {1.0, 0, nullptr};
160
161   /* User data */
162   void* getData() { return userData; }
163   void setData(void* d) { userData = d; }
164 private:
165   void* userData = nullptr;
166
167   /* List of all links. FIXME: should move to the Engine */
168   static std::unordered_map<std::string, LinkImpl*>* links;
169
170 public:
171   static LinkImpl* byName(std::string name);
172   static int linksCount();
173   static LinkImpl** linksList();
174   static void linksList(std::vector<s4u::Link*>* linkList);
175   static void linksExit();
176 };
177
178 /**********
179  * Action *
180  **********/
181 /** @ingroup SURF_network_interface
182  * @brief SURF network action interface class
183  * @details A NetworkAction represents a communication between two [hosts](\ref HostImpl)
184  */
185 class NetworkAction : public simgrid::kernel::resource::Action {
186 public:
187   /** @brief Constructor
188    *
189    * @param model The NetworkModel associated to this NetworkAction
190    * @param cost The cost of this  NetworkAction in [TODO]
191    * @param failed [description]
192    */
193   NetworkAction(simgrid::kernel::resource::Model* model, double cost, bool failed)
194       : simgrid::kernel::resource::Action(model, cost, failed)
195   {
196   }
197
198   /**
199    * @brief NetworkAction constructor
200    *
201    * @param model The NetworkModel associated to this NetworkAction
202    * @param cost The cost of this  NetworkAction in [TODO]
203    * @param failed [description]
204    * @param var The lmm variable associated to this Action if it is part of a LMM component
205    */
206   NetworkAction(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var)
207       : simgrid::kernel::resource::Action(model, cost, failed, var){};
208
209   void set_state(simgrid::kernel::resource::Action::State state) override;
210   virtual std::list<LinkImpl*> links();
211
212   double latency_    = {};
213   double lat_current_ = {};
214   double weight_     = {};
215   double rate_       = {};
216 };
217 }
218 }
219
220 #endif /* SURF_NETWORK_INTERFACE_HPP_ */
221
222