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();
56 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onCreation;
57 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onDestruction;
58 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onStateChange;
60 simgrid::xbt::signal<void(NetworkAction*, Action::State, Action::State)> networkActionStateChangedCallbacks;
61 simgrid::xbt::signal<void(NetworkAction*, s4u::Host* src, s4u::Host* dst)> LinkImpl::onCommunicate;
69 simgrid::surf::NetworkModel *surf_network_model = nullptr;
74 NetworkModel::~NetworkModel()
76 lmm_system_free(maxminSystem_);
77 xbt_heap_free(actionHeap_);
81 double NetworkModel::latencyFactor(double /*size*/) {
82 return sg_latency_factor;
85 double NetworkModel::bandwidthFactor(double /*size*/) {
86 return sg_bandwidth_factor;
89 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
93 double NetworkModel::nextOccuringEventFull(double now)
95 double minRes = Model::nextOccuringEventFull(now);
97 for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
98 NetworkAction *action = static_cast<NetworkAction*>(&*it);
99 if (action->latency_ > 0)
100 minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
103 XBT_DEBUG("Min of share resources %f", minRes);
112 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
113 : Resource(model, name, constraint), piface_(Link(this))
116 if (strcmp(name,"__loopback__"))
117 xbt_assert(!LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name);
120 bandwidth_.scale = 1;
122 links->insert({name, this});
123 XBT_DEBUG("Create link '%s'",name);
127 /** @brief use destroy() instead of this destructor */
128 LinkImpl::~LinkImpl()
130 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
132 /** @brief Fire the required callbacks and destroy the object
134 * Don't delete directly a Link, call l->destroy() instead.
136 void LinkImpl::destroy()
138 if (!currentlyDestroying_) {
139 currentlyDestroying_ = true;
145 bool LinkImpl::isUsed()
147 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
150 double LinkImpl::latency()
152 return latency_.peak * latency_.scale;
155 double LinkImpl::bandwidth()
157 return bandwidth_.peak * bandwidth_.scale;
160 int LinkImpl::sharingPolicy()
162 return lmm_constraint_sharing_policy(getConstraint());
165 void LinkImpl::turnOn()
172 void LinkImpl::turnOff()
179 void LinkImpl::setStateTrace(tmgr_trace_t trace)
181 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getName());
182 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
184 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
186 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getName());
187 bandwidth_.event = future_evt_set->add_trace(trace, 0.0, this);
189 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
191 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getName());
192 latency_.event = future_evt_set->add_trace(trace, 0.0, this);
200 void NetworkAction::setState(Action::State state){
201 Action::State old = getState();
202 Action::setState(state);
203 networkActionStateChangedCallbacks(this, old, state);
209 #endif /* NETWORK_INTERFACE_CPP_ */