Logo AND Algorithmique Numérique Distribuée

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