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)
25 if (links->find(name) == links->end())
27 return links->at(name);
29 /** @brief Returns the amount of links in the platform */
30 int LinkImpl::linksCount()
34 /** @brief Returns a list of all existing links */
35 LinkImpl** LinkImpl::linksList()
37 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
39 for (auto kv : *links) {
44 /** @brief destructor of the static data */
45 void LinkImpl::linksExit()
47 for (auto kv : *links)
48 (kv.second)->destroy();
58 simgrid::surf::NetworkModel *surf_network_model = nullptr;
63 NetworkModel::~NetworkModel()
65 lmm_system_free(maxminSystem_);
66 xbt_heap_free(actionHeap_);
70 double NetworkModel::latencyFactor(double /*size*/) {
71 return sg_latency_factor;
74 double NetworkModel::bandwidthFactor(double /*size*/) {
75 return sg_bandwidth_factor;
78 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
82 double NetworkModel::nextOccuringEventFull(double now)
84 double minRes = Model::nextOccuringEventFull(now);
86 for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
87 NetworkAction *action = static_cast<NetworkAction*>(&*it);
88 if (action->latency_ > 0)
89 minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
92 XBT_DEBUG("Min of share resources %f", minRes);
101 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
102 : Resource(model, name, constraint), piface_(this)
105 if (strcmp(name,"__loopback__"))
106 xbt_assert(!LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name);
109 bandwidth_.scale = 1;
111 links->insert({name, this});
112 XBT_DEBUG("Create link '%s'",name);
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 (!currentlyDestroying_) {
128 currentlyDestroying_ = true;
129 s4u::Link::onDestruction(this->piface_);
134 bool LinkImpl::isUsed()
136 return lmm_constraint_used(model()->getMaxminSystem(), 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 lmm_constraint_sharing_policy(constraint());
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", cname());
171 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
173 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
175 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", cname());
176 bandwidth_.event = future_evt_set->add_trace(trace, 0.0, this);
178 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
180 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", cname());
181 latency_.event = future_evt_set->add_trace(trace, 0.0, 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 lmm_system_t sys = getModel()->getMaxminSystem();
200 int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
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*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
206 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
208 retlist.push_back(link);
216 #endif /* NETWORK_INTERFACE_CPP_ */