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");
23 const char* sg_link_name(Link *link) {
24 return link->getName();
26 Link * sg_link_by_name(const char* name) {
27 return Link::byName(name);
30 int sg_link_is_shared(Link *link){
31 return link->sharingPolicy();
33 double sg_link_bandwidth(Link *link){
34 return link->bandwidth();
36 double sg_link_latency(Link *link){
37 return link->latency();
39 void* sg_link_data(Link *link) {
40 return link->getData();
42 void sg_link_data_set(Link *link,void *data) {
46 return Link::linksCount();
48 Link** sg_link_list() {
49 return Link::linksList();
64 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
65 LinkImpl* LinkImpl::byName(const char* name)
67 if (links->find(name) == links->end())
69 return links->at(name);
71 /** @brief Returns the amount of links in the platform */
72 int LinkImpl::linksCount()
76 /** @brief Returns a list of all existing links */
77 LinkImpl** LinkImpl::linksList()
79 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
81 for (auto kv : *links) {
86 /** @brief destructor of the static data */
87 void LinkImpl::linksExit()
89 for (auto kv : *links)
90 (kv.second)->destroy();
98 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onCreation;
99 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onDestruction;
100 simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onStateChange;
102 simgrid::xbt::signal<void(NetworkAction*, Action::State, Action::State)> networkActionStateChangedCallbacks;
103 simgrid::xbt::signal<void(NetworkAction*, s4u::Host* src, s4u::Host* dst)> LinkImpl::onCommunicate;
111 simgrid::surf::NetworkModel *surf_network_model = nullptr;
116 NetworkModel::~NetworkModel()
118 lmm_system_free(maxminSystem_);
119 xbt_heap_free(actionHeap_);
123 double NetworkModel::latencyFactor(double /*size*/) {
124 return sg_latency_factor;
127 double NetworkModel::bandwidthFactor(double /*size*/) {
128 return sg_bandwidth_factor;
131 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
135 double NetworkModel::nextOccuringEventFull(double now)
137 double minRes = Model::nextOccuringEventFull(now);
139 for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
140 NetworkAction *action = static_cast<NetworkAction*>(&*it);
141 if (action->latency_ > 0)
142 minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
145 XBT_DEBUG("Min of share resources %f", minRes);
154 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
155 : Resource(model, name, constraint)
157 if (strcmp(name,"__loopback__"))
158 xbt_assert(!LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name);
161 bandwidth_.scale = 1;
163 links->insert({name, this});
164 XBT_DEBUG("Create link '%s'",name);
168 /** @brief use destroy() instead of this destructor */
169 LinkImpl::~LinkImpl()
171 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
173 /** @brief Fire the required callbacks and destroy the object
175 * Don't delete directly a Link, call l->destroy() instead.
177 void LinkImpl::destroy()
179 if (!currentlyDestroying_) {
180 currentlyDestroying_ = true;
186 bool LinkImpl::isUsed()
188 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
191 double LinkImpl::latency()
193 return latency_.peak * latency_.scale;
196 double LinkImpl::bandwidth()
198 return bandwidth_.peak * bandwidth_.scale;
201 int LinkImpl::sharingPolicy()
203 return lmm_constraint_sharing_policy(getConstraint());
206 void LinkImpl::turnOn()
213 void LinkImpl::turnOff()
220 void LinkImpl::setStateTrace(tmgr_trace_t trace)
222 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getName());
223 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
225 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
227 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getName());
228 bandwidth_.event = future_evt_set->add_trace(trace, 0.0, this);
230 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
232 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getName());
233 latency_.event = future_evt_set->add_trace(trace, 0.0, this);
241 void NetworkAction::setState(Action::State state){
242 Action::State old = getState();
243 Action::setState(state);
244 networkActionStateChangedCallbacks(this, old, state);
250 #endif /* NETWORK_INTERFACE_CPP_ */