1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved. */
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. */
8 #include "network_interface.hpp"
9 #include "simgrid/sg_config.h"
11 #ifndef NETWORK_INTERFACE_CPP_
12 #define NETWORK_INTERFACE_CPP_
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
20 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
22 LinkImpl* LinkImpl::byName(std::string name)
24 auto link = links->find(name);
25 return link == links->end() ? nullptr : link->second;
27 /** @brief Returns the amount of links in the platform */
28 int LinkImpl::linksCount()
32 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
34 for (auto const& kv : *links) {
35 linkList->push_back(&kv.second->piface_);
39 /** @brief Returns a list of all existing links */
40 LinkImpl** LinkImpl::linksList()
42 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
44 for (auto const& kv : *links) {
50 /** @brief destructor of the static data */
51 void LinkImpl::linksExit()
53 for (auto const& kv : *links)
54 (kv.second)->destroy();
64 simgrid::surf::NetworkModel *surf_network_model = nullptr;
69 NetworkModel::~NetworkModel() = default;
71 double NetworkModel::latencyFactor(double /*size*/) {
72 return sg_latency_factor;
75 double NetworkModel::bandwidthFactor(double /*size*/) {
76 return sg_bandwidth_factor;
79 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
83 double NetworkModel::nextOccuringEventFull(double now)
85 double minRes = Model::nextOccuringEventFull(now);
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_);
93 XBT_DEBUG("Min of share resources %f", minRes);
102 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint)
103 : Resource(model, name, constraint), piface_(this)
106 if (name != "__loopback__")
107 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
110 bandwidth_.scale = 1;
112 links->insert({name, this});
113 XBT_DEBUG("Create link '%s'", name.c_str());
116 /** @brief use destroy() instead of this destructor */
117 LinkImpl::~LinkImpl()
119 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
121 /** @brief Fire the required callbacks and destroy the object
123 * Don't delete directly a Link, call l->destroy() instead.
125 void LinkImpl::destroy()
127 if (not currentlyDestroying_) {
128 currentlyDestroying_ = true;
129 s4u::Link::onDestruction(this->piface_);
134 bool LinkImpl::isUsed()
136 return model()->getMaxminSystem()->constraint_used(constraint());
139 double LinkImpl::latency()
141 return latency_.peak * latency_.scale;
144 double LinkImpl::bandwidth()
146 return bandwidth_.peak * bandwidth_.scale;
149 int LinkImpl::sharingPolicy()
151 return constraint()->get_sharing_policy();
154 void LinkImpl::turnOn()
158 s4u::Link::onStateChange(this->piface_);
161 void LinkImpl::turnOff()
165 s4u::Link::onStateChange(this->piface_);
168 void LinkImpl::setStateTrace(tmgr_trace_t trace)
170 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getCname());
171 stateEvent_ = future_evt_set->add_trace(trace, this);
173 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
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);
178 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
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);
189 void NetworkAction::setState(Action::State state)
191 Action::setState(state);
192 s4u::Link::onCommunicationStateChange(this);
195 /** @brief returns a list of all Links that this action is using */
196 std::list<LinkImpl*> NetworkAction::links()
198 std::list<LinkImpl*> retlist;
199 int llen = getVariable()->get_number_of_constraint();
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*>(getVariable()->get_constraint(i)->get_id());
206 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
208 retlist.push_back(link);
216 #endif /* NETWORK_INTERFACE_CPP_ */