Logo AND Algorithmique Numérique Distribuée

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