Logo AND Algorithmique Numérique Distribuée

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