Logo AND Algorithmique Numérique Distribuée

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