1 /* Copyright (c) 2013-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "network_interface.hpp"
10 #include "simgrid/sg_config.h"
12 #ifndef NETWORK_INTERFACE_CPP_
13 #define NETWORK_INTERFACE_CPP_
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
21 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
23 LinkImpl* LinkImpl::byName(std::string name)
25 auto link = links->find(name);
26 return link == links->end() ? nullptr : link->second;
28 /** @brief Returns the amount of links in the platform */
29 int LinkImpl::linksCount()
33 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
35 for (auto const& kv : *links) {
36 linkList->push_back(&kv.second->piface_);
40 /** @brief Returns a list of all existing links */
41 LinkImpl** LinkImpl::linksList()
43 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
45 for (auto const& kv : *links) {
51 /** @brief destructor of the static data */
52 void LinkImpl::linksExit()
54 for (auto const& kv : *links)
55 (kv.second)->destroy();
65 simgrid::surf::NetworkModel *surf_network_model = nullptr;
70 NetworkModel::~NetworkModel() = default;
72 double NetworkModel::latencyFactor(double /*size*/) {
73 return sg_latency_factor;
76 double NetworkModel::bandwidthFactor(double /*size*/) {
77 return sg_bandwidth_factor;
80 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
84 double NetworkModel::nextOccuringEventFull(double now)
86 double minRes = Model::nextOccuringEventFull(now);
88 for (Action const& action : *getRunningActionSet()) {
89 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
90 if (net_action.latency_ > 0)
91 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
94 XBT_DEBUG("Min of share resources %f", minRes);
103 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, lmm_constraint_t constraint)
104 : Resource(model, name, constraint), piface_(this)
107 if (name != "__loopback__")
108 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
111 bandwidth_.scale = 1;
113 links->insert({name, this});
114 XBT_DEBUG("Create link '%s'", name.c_str());
117 /** @brief use destroy() instead of this destructor */
118 LinkImpl::~LinkImpl()
120 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
122 /** @brief Fire the required callbacks and destroy the object
124 * Don't delete directly a Link, call l->destroy() instead.
126 void LinkImpl::destroy()
128 if (not currentlyDestroying_) {
129 currentlyDestroying_ = true;
130 s4u::Link::onDestruction(this->piface_);
135 bool LinkImpl::isUsed()
137 return model()->getMaxminSystem()->constraint_used(constraint());
140 double LinkImpl::latency()
142 return latency_.peak * latency_.scale;
145 double LinkImpl::bandwidth()
147 return bandwidth_.peak * bandwidth_.scale;
150 int LinkImpl::sharingPolicy()
152 return constraint()->get_sharing_policy();
155 void LinkImpl::turnOn()
159 s4u::Link::onStateChange(this->piface_);
162 void LinkImpl::turnOff()
166 s4u::Link::onStateChange(this->piface_);
169 void LinkImpl::setStateTrace(tmgr_trace_t trace)
171 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getCname());
172 stateEvent_ = future_evt_set->add_trace(trace, this);
174 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
176 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getCname());
177 bandwidth_.event = future_evt_set->add_trace(trace, this);
179 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
181 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getCname());
182 latency_.event = future_evt_set->add_trace(trace, this);
190 void NetworkAction::setState(Action::State state)
192 Action::setState(state);
193 s4u::Link::onCommunicationStateChange(this);
196 /** @brief returns a list of all Links that this action is using */
197 std::list<LinkImpl*> NetworkAction::links()
199 std::list<LinkImpl*> retlist;
200 int llen = getVariable()->get_number_of_constraint();
202 for (int i = 0; i < llen; i++) {
203 /* Beware of composite actions: ptasks put links and cpus together */
204 // extra pb: we cannot dynamic_cast from void*...
205 Resource* resource = static_cast<Resource*>(getVariable()->get_constraint(i)->get_id());
206 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
208 retlist.push_back(link);
216 #endif /* NETWORK_INTERFACE_CPP_ */