Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a6b79372c6b01dd3013e91dc264ff2d235a876b1
[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_amount(void) {
47         return Link::linksAmount();
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::linksAmount() {
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                 delete (kv.second);
91         delete links;
92 }
93
94 /*************
95  * Callbacks *
96  *************/
97
98 surf_callback(void, simgrid::surf::Link*) networkLinkCreatedCallbacks;
99 surf_callback(void, simgrid::surf::Link*) networkLinkDestructedCallbacks;
100 surf_callback(void, simgrid::surf::Link*, e_surf_resource_state_t, e_surf_resource_state_t) networkLinkStateChangedCallbacks;
101 surf_callback(void, simgrid::surf::NetworkAction*, e_surf_action_state_t, e_surf_action_state_t) networkActionStateChangedCallbacks;
102 surf_callback(void, simgrid::surf::NetworkAction*, simgrid::surf::RoutingEdge *src, simgrid::surf::RoutingEdge *dst, double size, double rate) networkCommunicateCallbacks;
103
104 }
105 }
106
107 void netlink_parse_init(sg_platf_link_cbarg_t link){
108   if (link->policy == SURF_LINK_FULLDUPLEX) {
109     char *link_id;
110     link_id = bprintf("%s_UP", link->id);
111     surf_network_model->createLink(link_id,
112                       link->bandwidth,
113                       link->bandwidth_trace,
114                       link->latency,
115                       link->latency_trace,
116                       link->state,
117                       link->state_trace, link->policy, link->properties);
118     xbt_free(link_id);
119     link_id = bprintf("%s_DOWN", link->id);
120     surf_network_model->createLink(link_id,
121                       link->bandwidth,
122                       link->bandwidth_trace,
123                       link->latency,
124                       link->latency_trace,
125                       link->state,
126                       link->state_trace, link->policy, link->properties);
127     xbt_free(link_id);
128   } else {
129           surf_network_model->createLink(link->id,
130                           link->bandwidth,
131                           link->bandwidth_trace,
132                           link->latency,
133                           link->latency_trace,
134                           link->state,
135                           link->state_trace, link->policy, link->properties);
136   }
137 }
138
139 void net_add_traces(){
140   surf_network_model->addTraces();
141 }
142
143 /*********
144  * Model *
145  *********/
146
147 simgrid::surf::NetworkModel *surf_network_model = NULL;
148
149 namespace simgrid {
150 namespace surf {
151
152 double NetworkModel::latencyFactor(double /*size*/) {
153   return sg_latency_factor;
154 }
155
156 double NetworkModel::bandwidthFactor(double /*size*/) {
157   return sg_bandwidth_factor;
158 }
159
160 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
161   return rate;
162 }
163
164 double NetworkModel::shareResourcesFull(double now)
165 {
166   NetworkAction *action = NULL;
167   ActionList *runningActions = surf_network_model->getRunningActionSet();
168   double minRes;
169
170   minRes = shareResourcesMaxMin(runningActions, surf_network_model->p_maxminSystem, surf_network_model->f_networkSolve);
171
172   for(ActionList::iterator it(runningActions->begin()), itend(runningActions->end())
173        ; it != itend ; ++it) {
174       action = static_cast<NetworkAction*>(&*it);
175 #ifdef HAVE_LATENCY_BOUND_TRACKING
176     if (lmm_is_variable_limited_by_latency(action->getVariable())) {
177       action->m_latencyLimited = 1;
178     } else {
179       action->m_latencyLimited = 0;
180     }
181 #endif
182     if (action->m_latency > 0) {
183       minRes = (minRes < 0) ? action->m_latency : std::min(minRes, action->m_latency);
184     }
185   }
186
187   XBT_DEBUG("Min of share resources %f", minRes);
188
189   return minRes;
190 }
191
192 /************
193  * Resource *
194  ************/
195
196 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
197 : Resource(model, name),
198   p_properties(props)
199 {
200   links->insert({name, this});
201
202   XBT_DEBUG("Create link '%s'",name);
203 }
204
205 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props,
206                                  lmm_constraint_t constraint,
207                              tmgr_history_t history,
208                              tmgr_trace_t state_trace)
209 : Resource(model, name, constraint),
210   p_properties(props)
211 {
212   if (state_trace)
213     p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
214
215   links->insert({name, this});
216   XBT_DEBUG("Create link '%s'",name);
217
218 }
219
220 Link::~Link()
221 {
222   surf_callback_emit(networkLinkDestructedCallbacks, this);
223   xbt_dict_free(&p_properties);
224 }
225
226 bool Link::isUsed()
227 {
228   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
229 }
230
231 double Link::getLatency()
232 {
233   return m_latCurrent;
234 }
235
236 double Link::getBandwidth()
237 {
238   return p_speed.peak * p_speed.scale;
239 }
240
241 int Link::sharingPolicy()
242 {
243   return lmm_constraint_sharing_policy(getConstraint());
244 }
245
246 void Link::setState(e_surf_resource_state_t state){
247   e_surf_resource_state_t old = Resource::getState();
248   Resource::setState(state);
249   surf_callback_emit(networkLinkStateChangedCallbacks, this, old, state);
250 }
251
252 xbt_dict_t Link::getProperties()
253 {
254         if (p_properties==NULL)
255                 p_properties = xbt_dict_new();
256         return p_properties;
257 }
258
259 /**********
260  * Action *
261  **********/
262
263 void NetworkAction::setState(e_surf_action_state_t state){
264   e_surf_action_state_t old = getState();
265   Action::setState(state);
266   surf_callback_emit(networkActionStateChangedCallbacks, this, old, state);
267 }
268
269 }
270 }
271
272 #endif /* NETWORK_INTERFACE_CPP_ */