Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2019. 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 #include "network_interface.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include "surf/surf.hpp"
11
12 #ifndef NETWORK_INTERFACE_CPP_
13 #define NETWORK_INTERFACE_CPP_
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
16
17 /*********
18  * Model *
19  *********/
20
21 simgrid::kernel::resource::NetworkModel* surf_network_model = nullptr;
22
23 namespace simgrid {
24 namespace kernel {
25 namespace resource {
26
27 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
28 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
29     "network/TCP-gamma", {"network/TCP_gamma"},
30     "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
31     "Use the last given value, which is the max window size)",
32     4194304.0);
33
34 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
35 simgrid::config::Flag<bool> NetworkModel::cfg_crosstraffic(
36     "network/crosstraffic",
37     "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
38
39 NetworkModel::~NetworkModel() = default;
40
41 double NetworkModel::get_latency_factor(double /*size*/)
42 {
43   return sg_latency_factor;
44 }
45
46 double NetworkModel::get_bandwidth_factor(double /*size*/)
47 {
48   return sg_bandwidth_factor;
49 }
50
51 double NetworkModel::get_bandwidth_constraint(double rate, double /*bound*/, double /*size*/)
52 {
53   return rate;
54 }
55
56 double NetworkModel::next_occuring_event_full(double now)
57 {
58   double minRes = Model::next_occuring_event_full(now);
59
60   for (Action const& action : *get_started_action_set()) {
61     const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
62     if (net_action.latency_ > 0)
63       minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
64   }
65
66   XBT_DEBUG("Min of share resources %f", minRes);
67
68   return minRes;
69 }
70
71 /************
72  * Resource *
73  ************/
74
75 LinkImpl::LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint)
76     : Resource(model, name, constraint), piface_(this)
77 {
78
79   if (name != "__loopback__")
80     xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
81
82   latency_.scale   = 1;
83   bandwidth_.scale = 1;
84
85   s4u::Engine::get_instance()->link_register(name, &piface_);
86   XBT_DEBUG("Create link '%s'", name.c_str());
87 }
88
89 /** @brief use destroy() instead of this destructor */
90 LinkImpl::~LinkImpl()
91 {
92   xbt_assert(currently_destroying_, "Don't delete Links directly. Call destroy() instead.");
93 }
94 /** @brief Fire the required callbacks and destroy the object
95  *
96  * Don't delete directly a Link, call l->destroy() instead.
97  */
98 void LinkImpl::destroy()
99 {
100   if (not currently_destroying_) {
101     currently_destroying_ = true;
102     s4u::Link::on_destruction(this->piface_);
103     delete this;
104   }
105 }
106
107 bool LinkImpl::is_used()
108 {
109   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
110 }
111
112 double LinkImpl::get_latency()
113 {
114   return latency_.peak * latency_.scale;
115 }
116
117 double LinkImpl::get_bandwidth()
118 {
119   return bandwidth_.peak * bandwidth_.scale;
120 }
121
122 s4u::Link::SharingPolicy LinkImpl::get_sharing_policy()
123 {
124   return get_constraint()->get_sharing_policy();
125 }
126
127 void LinkImpl::turn_on()
128 {
129   if (is_off()) {
130     Resource::turn_on();
131     s4u::Link::on_state_change(this->piface_);
132   }
133 }
134
135 void LinkImpl::turn_off()
136 {
137   if (is_on()) {
138     Resource::turn_off();
139     s4u::Link::on_state_change(this->piface_);
140   }
141 }
142
143 void LinkImpl::on_bandwidth_change()
144 {
145   s4u::Link::on_bandwidth_change(this->piface_);
146 }
147
148 void LinkImpl::set_bandwidth_trace(tmgr_trace_t trace)
149 {
150   xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", get_cname());
151   bandwidth_.event = future_evt_set.add_trace(trace, this);
152 }
153
154 void LinkImpl::set_latency_trace(tmgr_trace_t trace)
155 {
156   xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", get_cname());
157   latency_.event = future_evt_set.add_trace(trace, this);
158 }
159
160 /**********
161  * Action *
162  **********/
163
164 void NetworkAction::set_state(Action::State state)
165 {
166   Action::State previous = get_state();
167   Action::set_state(state);
168   if (previous != state) // Trigger only if the state changed
169     s4u::Link::on_communication_state_change(this, previous);
170 }
171
172 /** @brief returns a list of all Links that this action is using */
173 std::list<LinkImpl*> NetworkAction::links()
174 {
175   std::list<LinkImpl*> retlist;
176   int llen = get_variable()->get_number_of_constraint();
177
178   for (int i = 0; i < llen; i++) {
179     /* Beware of composite actions: ptasks put links and cpus together */
180     // extra pb: we cannot dynamic_cast from void*...
181     Resource* resource = static_cast<Resource*>(get_variable()->get_constraint(i)->get_id());
182     LinkImpl* link     = dynamic_cast<LinkImpl*>(resource);
183     if (link != nullptr)
184       retlist.push_back(link);
185   }
186
187   return retlist;
188 }
189 }
190 } // namespace kernel
191 }
192
193 #endif /* NETWORK_INTERFACE_CPP_ */