Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sed -i -e 's/\t/ /g' [sources] Please people, stop using tabs
[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::shareResourcesFull(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   XBT_DEBUG("Create link '%s'",name);
200 }
201
202 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props,
203                      lmm_constraint_t constraint,
204                      sg_future_evt_set_t fes,
205                        tmgr_trace_t state_trace)
206 : Resource(model, name, constraint),
207   PropertyHolder(props)
208 {
209   if (state_trace)
210     p_stateEvent = fes->add_trace(state_trace, 0.0, this);
211
212   links->insert({name, this});
213   XBT_DEBUG("Create link '%s'",name);
214
215 }
216
217 /** @brief use destroy() instead of this destructor */
218 Link::~Link() {
219   xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
220 }
221 /** @brief Fire the require callbacks and destroy the object
222  *
223  * Don't delete directly an Link, call l->destroy() instead.
224  */
225 void Link::destroy()
226 {
227   if (!currentlyDestroying_) {
228     currentlyDestroying_ = true;
229     onDestruction(this);
230     delete this;
231   }
232 }
233
234 bool Link::isUsed()
235 {
236   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
237 }
238
239 double Link::getLatency()
240 {
241   return m_latCurrent;
242 }
243
244 double Link::getBandwidth()
245 {
246   return p_speed.peak * p_speed.scale;
247 }
248
249 int Link::sharingPolicy()
250 {
251   return lmm_constraint_sharing_policy(getConstraint());
252 }
253
254 void Link::turnOn(){
255   if (isOff()) {
256     Resource::turnOn();
257     onStateChange(this);
258   }
259 }
260 void Link::turnOff(){
261   if (isOn()) {
262     Resource::turnOff();
263     onStateChange(this);
264   }
265 }
266
267 /**********
268  * Action *
269  **********/
270
271 void NetworkAction::setState(e_surf_action_state_t state){
272   e_surf_action_state_t old = getState();
273   Action::setState(state);
274   networkActionStateChangedCallbacks(this, old, state);
275 }
276
277 }
278 }
279
280 #endif /* NETWORK_INTERFACE_CPP_ */