Logo AND Algorithmique Numérique Distribuée

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