1 /* Copyright (c) 2013-2015. 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(const char* name)
26 return links->at(name);
27 } catch (std::out_of_range& unfound) {
31 /** @brief Returns the amount of links in the platform */
32 int LinkImpl::linksCount()
36 /** @brief Returns a list of all existing links */
37 LinkImpl** LinkImpl::linksList()
39 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
41 for (auto kv : *links) {
47 /** @brief destructor of the static data */
48 void LinkImpl::linksExit()
50 for (auto kv : *links)
51 (kv.second)->destroy();
61 simgrid::surf::NetworkModel *surf_network_model = nullptr;
66 NetworkModel::~NetworkModel()
68 lmm_system_free(maxminSystem_);
69 xbt_heap_free(actionHeap_);
73 double NetworkModel::latencyFactor(double /*size*/) {
74 return sg_latency_factor;
77 double NetworkModel::bandwidthFactor(double /*size*/) {
78 return sg_bandwidth_factor;
81 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
85 double NetworkModel::nextOccuringEventFull(double now)
87 double minRes = Model::nextOccuringEventFull(now);
89 for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
90 NetworkAction *action = static_cast<NetworkAction*>(&*it);
91 if (action->latency_ > 0)
92 minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
95 XBT_DEBUG("Min of share resources %f", minRes);
104 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
105 : Resource(model, name, constraint), piface_(this)
108 if (strcmp(name,"__loopback__"))
109 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name);
112 bandwidth_.scale = 1;
114 links->insert({name, this});
115 XBT_DEBUG("Create link '%s'",name);
119 /** @brief use destroy() instead of this destructor */
120 LinkImpl::~LinkImpl()
122 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
124 /** @brief Fire the required callbacks and destroy the object
126 * Don't delete directly a Link, call l->destroy() instead.
128 void LinkImpl::destroy()
130 if (not currentlyDestroying_) {
131 currentlyDestroying_ = true;
132 s4u::Link::onDestruction(this->piface_);
137 bool LinkImpl::isUsed()
139 return lmm_constraint_used(model()->getMaxminSystem(), constraint());
142 double LinkImpl::latency()
144 return latency_.peak * latency_.scale;
147 double LinkImpl::bandwidth()
149 return bandwidth_.peak * bandwidth_.scale;
152 int LinkImpl::sharingPolicy()
154 return lmm_constraint_sharing_policy(constraint());
157 void LinkImpl::turnOn()
161 s4u::Link::onStateChange(this->piface_);
164 void LinkImpl::turnOff()
168 s4u::Link::onStateChange(this->piface_);
171 void LinkImpl::setStateTrace(tmgr_trace_t trace)
173 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", cname());
174 stateEvent_ = future_evt_set->add_trace(trace, this);
176 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
178 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", cname());
179 bandwidth_.event = future_evt_set->add_trace(trace, this);
181 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
183 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", cname());
184 latency_.event = future_evt_set->add_trace(trace, this);
192 void NetworkAction::setState(Action::State state)
194 Action::setState(state);
195 s4u::Link::onCommunicationStateChange(this);
198 /** @brief returns a list of all Links that this action is using */
199 std::list<LinkImpl*> NetworkAction::links()
201 std::list<LinkImpl*> retlist;
202 lmm_system_t sys = getModel()->getMaxminSystem();
203 int llen = lmm_get_number_of_cnst_from_var(sys, variable_);
205 for (int i = 0; i < llen; i++) {
206 /* Beware of composite actions: ptasks put links and cpus together */
207 // extra pb: we cannot dynamic_cast from void*...
208 Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
209 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
211 retlist.push_back(link);
219 #endif /* NETWORK_INTERFACE_CPP_ */