Logo AND Algorithmique Numérique Distribuée

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