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->getBandwidth();
36 double sg_link_latency(Link *link){
37 return link->getLatency();
39 void* sg_link_data(Link *link) {
40 return link->getData();
42 void sg_link_data_set(Link *link,void *data) {
45 int sg_link_count(void) {
46 return Link::linksCount();
48 Link** sg_link_list(void) {
49 return Link::linksList();
51 void sg_link_exit(void) {
64 boost::unordered_map<std::string,Link *> *Link::links = new boost::unordered_map<std::string,Link *>();
65 Link *Link::byName(const char* name) {
66 if (links->find(name) == links->end())
68 return links->at(name);
70 /** @brief Returns the amount of links in the platform */
71 int Link::linksCount() {
74 /** @brief Returns a list of all existing links */
75 Link **Link::linksList() {
76 Link **res = xbt_new(Link*, (int)links->size());
78 for (auto kv : *links) {
83 /** @brief destructor of the static data */
84 void Link::linksExit() {
85 for (auto kv : *links)
86 (kv.second)->destroy();
94 simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onCreation;
95 simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onDestruction;
96 simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onStateChange;
98 simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, e_surf_action_state_t, e_surf_action_state_t)> networkActionStateChangedCallbacks;
99 simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst, double size, double rate)> networkCommunicateCallbacks;
104 void netlink_parse_init(sg_platf_link_cbarg_t link){
105 if (link->policy == SURF_LINK_FULLDUPLEX) {
107 link_id = bprintf("%s_UP", link->id);
108 Link *l = surf_network_model->createLink(link_id, link->bandwidth, link->latency,
109 link->policy, link->properties);
110 if (link->latency_trace)
111 l->setLatencyTrace(link->latency_trace);
112 if (link->bandwidth_trace)
113 l->setBandwidthTrace(link->bandwidth_trace);
114 if (link->state_trace)
115 l->setStateTrace(link->state_trace);
118 link_id = bprintf("%s_DOWN", link->id);
119 l = surf_network_model->createLink(link_id, link->bandwidth, link->latency,
120 link->policy, link->properties);
121 if (link->latency_trace)
122 l->setLatencyTrace(link->latency_trace);
123 if (link->bandwidth_trace)
124 l->setBandwidthTrace(link->bandwidth_trace);
125 if (link->state_trace)
126 l->setStateTrace(link->state_trace);
129 Link *l=surf_network_model->createLink(link->id, link->bandwidth, link->latency,
130 link->policy, link->properties);
131 if (link->latency_trace)
132 l->setLatencyTrace(link->latency_trace);
133 if (link->bandwidth_trace)
134 l->setBandwidthTrace(link->bandwidth_trace);
135 if (link->state_trace)
136 l->setStateTrace(link->state_trace);
144 simgrid::surf::NetworkModel *surf_network_model = NULL;
149 double NetworkModel::latencyFactor(double /*size*/) {
150 return sg_latency_factor;
153 double NetworkModel::bandwidthFactor(double /*size*/) {
154 return sg_bandwidth_factor;
157 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
161 double NetworkModel::next_occuring_event_full(double now)
163 NetworkAction *action = NULL;
164 ActionList *runningActions = surf_network_model->getRunningActionSet();
167 minRes = shareResourcesMaxMin(runningActions, surf_network_model->maxminSystem_, surf_network_model->f_networkSolve);
169 for(ActionList::iterator it(runningActions->begin()), itend(runningActions->end())
170 ; it != itend ; ++it) {
171 action = static_cast<NetworkAction*>(&*it);
172 if (action->m_latency > 0) {
173 minRes = (minRes < 0) ? action->m_latency : std::min(minRes, action->m_latency);
177 XBT_DEBUG("Min of share resources %f", minRes);
186 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
187 : Resource(model, name),
188 PropertyHolder(props)
190 links->insert({name, this});
193 m_bandwidth.scale = 1;
194 XBT_DEBUG("Create link '%s'",name);
197 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint)
198 : Resource(model, name, constraint),
199 PropertyHolder(props)
202 m_bandwidth.scale = 1;
204 links->insert({name, this});
205 XBT_DEBUG("Create link '%s'",name);
209 /** @brief use destroy() instead of this destructor */
211 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
213 /** @brief Fire the require callbacks and destroy the object
215 * Don't delete directly an Link, call l->destroy() instead.
219 if (!currentlyDestroying_) {
220 currentlyDestroying_ = true;
228 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
231 double Link::getLatency()
233 return m_latency.peak * m_latency.scale;
236 double Link::getBandwidth()
238 return m_bandwidth.peak * m_bandwidth.scale;
241 int Link::sharingPolicy()
243 return lmm_constraint_sharing_policy(getConstraint());
252 void Link::turnOff(){
258 void Link::setStateTrace(tmgr_trace_t trace) {
259 xbt_assert(m_stateEvent==NULL,"Cannot set a second state trace to Link %s", getName());
260 m_stateEvent = future_evt_set->add_trace(trace, 0.0, this);
262 void Link::setBandwidthTrace(tmgr_trace_t trace)
264 xbt_assert(m_bandwidth.event==NULL,"Cannot set a second bandwidth trace to Link %s", getName());
265 m_bandwidth.event = future_evt_set->add_trace(trace, 0.0, this);
267 void Link::setLatencyTrace(tmgr_trace_t trace)
269 xbt_assert(m_latency.event==NULL,"Cannot set a second latency trace to Link %s", getName());
270 m_latency.event = future_evt_set->add_trace(trace, 0.0, this);
278 void NetworkAction::setState(e_surf_action_state_t state){
279 e_surf_action_state_t old = getState();
280 Action::setState(state);
281 networkActionStateChangedCallbacks(this, old, state);
287 #endif /* NETWORK_INTERFACE_CPP_ */