Logo AND Algorithmique Numérique Distribuée

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