Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ns3: no need for 2 postparse callbacks
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <algorithm>
8
9 #include "network_interface.hpp"
10 #include "simgrid/sg_config.h"
11
12 #ifndef NETWORK_INTERFACE_CPP_
13 #define NETWORK_INTERFACE_CPP_
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
16
17 /*********
18  * C API *
19  *********/
20
21 extern "C" {
22
23   const char* sg_link_name(Link *link) {
24     return link->getName();
25   }
26   Link * sg_link_by_name(const char* name) {
27     return Link::byName(name);
28   }
29
30   int sg_link_is_shared(Link *link){
31     return link->sharingPolicy();
32   }
33   double sg_link_bandwidth(Link *link){
34     return link->getBandwidth();
35   }
36   double sg_link_latency(Link *link){
37     return link->getLatency();
38   }
39   void* sg_link_data(Link *link) {
40     return link->getData();
41   }
42   void sg_link_data_set(Link *link,void *data) {
43     link->setData(data);
44   }
45   int sg_link_count(void) {
46     return Link::linksCount();
47   }
48   Link** sg_link_list(void) {
49     return Link::linksList();
50   }
51   void sg_link_exit(void) {
52     Link::linksExit();
53   }
54
55 }
56
57 /*****************
58  * List of links *
59  *****************/
60
61 namespace simgrid {
62   namespace surf {
63
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())
67         return NULL;
68       return  links->at(name);
69     }
70     /** @brief Returns the amount of links in the platform */
71     int Link::linksCount() {
72       return links->size();
73     }
74     /** @brief Returns a list of all existing links */
75     Link **Link::linksList() {
76       Link **res = xbt_new(Link*, (int)links->size());
77       int i=0;
78       for (auto kv : *links) {
79         res[i++] = kv.second;
80       }
81       return res;
82     }
83     /** @brief destructor of the static data */
84     void Link::linksExit() {
85       for (auto kv : *links)
86         (kv.second)->destroy();
87       delete links;
88     }
89
90     /*************
91      * Callbacks *
92      *************/
93
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;
97
98     simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::Action::State, simgrid::surf::Action::State)> networkActionStateChangedCallbacks;
99     simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst, double size, double rate)> networkCommunicateCallbacks;
100
101   }
102 }
103
104 void netlink_parse_init(sg_platf_link_cbarg_t link){
105   std::vector<char*> names;
106
107   if (link->policy == SURF_LINK_FULLDUPLEX) {
108     names.push_back(bprintf("%s_UP", link->id));
109     names.push_back(bprintf("%s_DOWN", link->id));
110   } else {
111     names.push_back(xbt_strdup(link->id));
112   }
113   for (auto link_name : names) {
114     Link *l = surf_network_model->createLink(link_name, link->bandwidth, link->latency, link->policy, link->properties);
115
116     if (link->latency_trace)
117       l->setLatencyTrace(link->latency_trace);
118     if (link->bandwidth_trace)
119       l->setBandwidthTrace(link->bandwidth_trace);
120     if (link->state_trace)
121       l->setStateTrace(link->state_trace);
122
123     xbt_free(link_name);
124   }
125 }
126
127 /*********
128  * Model *
129  *********/
130
131 simgrid::surf::NetworkModel *surf_network_model = NULL;
132
133 namespace simgrid {
134   namespace surf {
135
136     double NetworkModel::latencyFactor(double /*size*/) {
137       return sg_latency_factor;
138     }
139
140     double NetworkModel::bandwidthFactor(double /*size*/) {
141       return sg_bandwidth_factor;
142     }
143
144     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
145       return rate;
146     }
147
148     double NetworkModel::next_occuring_event_full(double now)
149     {
150       NetworkAction *action = NULL;
151       ActionList *runningActions = surf_network_model->getRunningActionSet();
152       double minRes;
153
154       minRes = shareResourcesMaxMin(runningActions, surf_network_model->maxminSystem_, surf_network_model->f_networkSolve);
155
156       for(ActionList::iterator it(runningActions->begin()), itend(runningActions->end())
157           ; it != itend ; ++it) {
158         action = static_cast<NetworkAction*>(&*it);
159         if (action->latency_ > 0) {
160           minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
161         }
162       }
163
164       XBT_DEBUG("Min of share resources %f", minRes);
165
166       return minRes;
167     }
168
169     /************
170      * Resource *
171      ************/
172
173     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
174     : Resource(model, name),
175       PropertyHolder(props)
176     {
177       links->insert({name, this});
178
179       m_latency.scale = 1;
180       m_bandwidth.scale = 1;
181       XBT_DEBUG("Create link '%s'",name);
182     }
183
184     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint)
185     : Resource(model, name, constraint),
186       PropertyHolder(props)
187     {
188       if (strcmp(name,"__loopback__"))
189         xbt_assert(!Link::byName(name), "Link '%s' declared several times in the platform.", name);
190
191       m_latency.scale = 1;
192       m_bandwidth.scale = 1;
193
194       links->insert({name, this});
195       XBT_DEBUG("Create link '%s'",name);
196
197     }
198
199     /** @brief use destroy() instead of this destructor */
200     Link::~Link() {
201       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
202     }
203     /** @brief Fire the require callbacks and destroy the object
204      *
205      * Don't delete directly an Link, call l->destroy() instead.
206      */
207     void Link::destroy()
208     {
209       if (!currentlyDestroying_) {
210         currentlyDestroying_ = true;
211         onDestruction(this);
212         delete this;
213       }
214     }
215
216     bool Link::isUsed()
217     {
218       return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
219     }
220
221     double Link::getLatency()
222     {
223       return m_latency.peak * m_latency.scale;
224     }
225
226     double Link::getBandwidth()
227     {
228       return m_bandwidth.peak * m_bandwidth.scale;
229     }
230
231     int Link::sharingPolicy()
232     {
233       return lmm_constraint_sharing_policy(getConstraint());
234     }
235
236     void Link::turnOn(){
237       if (isOff()) {
238         Resource::turnOn();
239         onStateChange(this);
240       }
241     }
242     void Link::turnOff(){
243       if (isOn()) {
244         Resource::turnOff();
245         onStateChange(this);
246       }
247     }
248     void Link::setStateTrace(tmgr_trace_t trace) {
249       xbt_assert(m_stateEvent==NULL,"Cannot set a second state trace to Link %s", getName());
250       m_stateEvent = future_evt_set->add_trace(trace, 0.0, this);
251     }
252     void Link::setBandwidthTrace(tmgr_trace_t trace)
253     {
254       xbt_assert(m_bandwidth.event==NULL,"Cannot set a second bandwidth trace to Link %s", getName());
255       m_bandwidth.event = future_evt_set->add_trace(trace, 0.0, this);
256     }
257     void Link::setLatencyTrace(tmgr_trace_t trace)
258     {
259       xbt_assert(m_latency.event==NULL,"Cannot set a second latency trace to Link %s", getName());
260       m_latency.event = future_evt_set->add_trace(trace, 0.0, this);
261     }
262
263
264     /**********
265      * Action *
266      **********/
267
268     void NetworkAction::setState(Action::State state){
269       Action::State old = getState();
270       Action::setState(state);
271       networkActionStateChangedCallbacks(this, old, state);
272     }
273
274   }
275 }
276
277 #endif /* NETWORK_INTERFACE_CPP_ */