Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use references, and avoid disturbing expressions like &*it.
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <algorithm>
8
9 #include "network_interface.hpp"
10 #include "simgrid/sg_config.h"
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 namespace simgrid {
18   namespace surf {
19
20   /* List of links */
21   std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
22
23   LinkImpl* LinkImpl::byName(std::string name)
24   {
25     auto link = links->find(name);
26     return link == links->end() ? nullptr : link->second;
27   }
28   /** @brief Returns the amount of links in the platform */
29   int LinkImpl::linksCount()
30   {
31     return links->size();
32   }
33   void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
34   {
35     for (auto const& kv : *links) {
36       linkList->push_back(&kv.second->piface_);
37     }
38   }
39
40   /** @brief Returns a list of all existing links */
41   LinkImpl** LinkImpl::linksList()
42   {
43     LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
44     int i          = 0;
45     for (auto const& kv : *links) {
46       res[i] = kv.second;
47       i++;
48     }
49     return res;
50   }
51   /** @brief destructor of the static data */
52   void LinkImpl::linksExit()
53   {
54     for (auto const& kv : *links)
55       (kv.second)->destroy();
56     delete links;
57   }
58   }
59 }
60
61 /*********
62  * Model *
63  *********/
64
65 simgrid::surf::NetworkModel *surf_network_model = nullptr;
66
67 namespace simgrid {
68   namespace surf {
69
70     NetworkModel::~NetworkModel()
71     {
72       lmm_system_free(maxminSystem_);
73       delete modifiedSet_;
74     }
75
76     double NetworkModel::latencyFactor(double /*size*/) {
77       return sg_latency_factor;
78     }
79
80     double NetworkModel::bandwidthFactor(double /*size*/) {
81       return sg_bandwidth_factor;
82     }
83
84     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
85       return rate;
86     }
87
88     double NetworkModel::nextOccuringEventFull(double now)
89     {
90       double minRes = Model::nextOccuringEventFull(now);
91
92       for (Action const& action : *getRunningActionSet()) {
93         const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
94         if (net_action.latency_ > 0)
95           minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
96       }
97
98       XBT_DEBUG("Min of share resources %f", minRes);
99
100       return minRes;
101     }
102
103     /************
104      * Resource *
105      ************/
106
107     LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, lmm_constraint_t constraint)
108         : Resource(model, name, constraint), piface_(this)
109     {
110
111       if (name != "__loopback__")
112         xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
113
114       latency_.scale   = 1;
115       bandwidth_.scale = 1;
116
117       links->insert({name, this});
118       XBT_DEBUG("Create link '%s'", name.c_str());
119     }
120
121     /** @brief use destroy() instead of this destructor */
122     LinkImpl::~LinkImpl()
123     {
124       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
125     }
126     /** @brief Fire the required callbacks and destroy the object
127      *
128      * Don't delete directly a Link, call l->destroy() instead.
129      */
130     void LinkImpl::destroy()
131     {
132       if (not currentlyDestroying_) {
133         currentlyDestroying_ = true;
134         s4u::Link::onDestruction(this->piface_);
135         delete this;
136       }
137     }
138
139     bool LinkImpl::isUsed()
140     {
141       return lmm_constraint_used(model()->getMaxminSystem(), constraint());
142     }
143
144     double LinkImpl::latency()
145     {
146       return latency_.peak * latency_.scale;
147     }
148
149     double LinkImpl::bandwidth()
150     {
151       return bandwidth_.peak * bandwidth_.scale;
152     }
153
154     int LinkImpl::sharingPolicy()
155     {
156       return lmm_constraint_sharing_policy(constraint());
157     }
158
159     void LinkImpl::turnOn()
160     {
161       if (isOff()) {
162         Resource::turnOn();
163         s4u::Link::onStateChange(this->piface_);
164       }
165     }
166     void LinkImpl::turnOff()
167     {
168       if (isOn()) {
169         Resource::turnOff();
170         s4u::Link::onStateChange(this->piface_);
171       }
172     }
173     void LinkImpl::setStateTrace(tmgr_trace_t trace)
174     {
175       xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getCname());
176       stateEvent_ = future_evt_set->add_trace(trace, this);
177     }
178     void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
179     {
180       xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getCname());
181       bandwidth_.event = future_evt_set->add_trace(trace, this);
182     }
183     void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
184     {
185       xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getCname());
186       latency_.event = future_evt_set->add_trace(trace, this);
187     }
188
189
190     /**********
191      * Action *
192      **********/
193
194     void NetworkAction::setState(Action::State state)
195     {
196       Action::setState(state);
197       s4u::Link::onCommunicationStateChange(this);
198     }
199
200     /** @brief returns a list of all Links that this action is using */
201     std::list<LinkImpl*> NetworkAction::links()
202     {
203       std::list<LinkImpl*> retlist;
204       lmm_system_t sys = getModel()->getMaxminSystem();
205       int llen         = lmm_get_number_of_cnst_from_var(sys, getVariable());
206
207       for (int i = 0; i < llen; i++) {
208         /* Beware of composite actions: ptasks put links and cpus together */
209         // extra pb: we cannot dynamic_cast from void*...
210         Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
211         LinkImpl* link     = dynamic_cast<LinkImpl*>(resource);
212         if (link != nullptr)
213           retlist.push_back(link);
214       }
215
216       return retlist;
217     }
218   }
219 }
220
221 #endif /* NETWORK_INTERFACE_CPP_ */