Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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.h"
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   NetworkModel::~NetworkModel() = default;
70
71     double NetworkModel::latencyFactor(double /*size*/) {
72       return sg_latency_factor;
73     }
74
75     double NetworkModel::bandwidthFactor(double /*size*/) {
76       return sg_bandwidth_factor;
77     }
78
79     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
80       return rate;
81     }
82
83     double NetworkModel::nextOccuringEventFull(double now)
84     {
85       double minRes = Model::nextOccuringEventFull(now);
86
87       for (kernel::resource::Action const& action : *getRunningActionSet()) {
88         const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
89         if (net_action.latency_ > 0)
90           minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
91       }
92
93       XBT_DEBUG("Min of share resources %f", minRes);
94
95       return minRes;
96     }
97
98     /************
99      * Resource *
100      ************/
101
102     LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint)
103         : Resource(model, name, constraint), piface_(this)
104     {
105
106       if (name != "__loopback__")
107         xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
108
109       latency_.scale   = 1;
110       bandwidth_.scale = 1;
111
112       links->insert({name, this});
113       XBT_DEBUG("Create link '%s'", name.c_str());
114     }
115
116     /** @brief use destroy() instead of this destructor */
117     LinkImpl::~LinkImpl()
118     {
119       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
120     }
121     /** @brief Fire the required callbacks and destroy the object
122      *
123      * Don't delete directly a Link, call l->destroy() instead.
124      */
125     void LinkImpl::destroy()
126     {
127       if (not currentlyDestroying_) {
128         currentlyDestroying_ = true;
129         s4u::Link::onDestruction(this->piface_);
130         delete this;
131       }
132     }
133
134     bool LinkImpl::isUsed()
135     {
136       return model()->getMaxminSystem()->constraint_used(constraint());
137     }
138
139     double LinkImpl::latency()
140     {
141       return latency_.peak * latency_.scale;
142     }
143
144     double LinkImpl::bandwidth()
145     {
146       return bandwidth_.peak * bandwidth_.scale;
147     }
148
149     int LinkImpl::sharingPolicy()
150     {
151       return constraint()->get_sharing_policy();
152     }
153
154     void LinkImpl::turnOn()
155     {
156       if (isOff()) {
157         Resource::turnOn();
158         s4u::Link::onStateChange(this->piface_);
159       }
160     }
161     void LinkImpl::turnOff()
162     {
163       if (isOn()) {
164         Resource::turnOff();
165         s4u::Link::onStateChange(this->piface_);
166       }
167     }
168     void LinkImpl::setStateTrace(tmgr_trace_t trace)
169     {
170       xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getCname());
171       stateEvent_ = future_evt_set->add_trace(trace, this);
172     }
173     void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
174     {
175       xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getCname());
176       bandwidth_.event = future_evt_set->add_trace(trace, this);
177     }
178     void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
179     {
180       xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getCname());
181       latency_.event = future_evt_set->add_trace(trace, this);
182     }
183
184
185     /**********
186      * Action *
187      **********/
188
189     void NetworkAction::set_state(Action::State state)
190     {
191       Action::set_state(state);
192       s4u::Link::onCommunicationStateChange(this);
193     }
194
195     /** @brief returns a list of all Links that this action is using */
196     std::list<LinkImpl*> NetworkAction::links()
197     {
198       std::list<LinkImpl*> retlist;
199       int llen = get_variable()->get_number_of_constraint();
200
201       for (int i = 0; i < llen; i++) {
202         /* Beware of composite actions: ptasks put links and cpus together */
203         // extra pb: we cannot dynamic_cast from void*...
204         kernel::resource::Resource* resource =
205             static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
206         LinkImpl* link     = dynamic_cast<LinkImpl*>(resource);
207         if (link != nullptr)
208           retlist.push_back(link);
209       }
210
211       return retlist;
212     }
213   }
214 }
215
216 #endif /* NETWORK_INTERFACE_CPP_ */