Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:mquinson/simgrid
[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->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_trace, link->policy, link->properties);
126     xbt_free(link_id);
127   } else {
128     surf_network_model->createLink(link->id,
129         link->bandwidth,
130         link->bandwidth_trace,
131         link->latency,
132         link->latency_trace,
133         link->state_trace, link->policy, link->properties);
134   }
135 }
136
137 /*********
138  * Model *
139  *********/
140
141 simgrid::surf::NetworkModel *surf_network_model = NULL;
142
143 namespace simgrid {
144   namespace surf {
145
146     double NetworkModel::latencyFactor(double /*size*/) {
147       return sg_latency_factor;
148     }
149
150     double NetworkModel::bandwidthFactor(double /*size*/) {
151       return sg_bandwidth_factor;
152     }
153
154     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
155       return rate;
156     }
157
158     double NetworkModel::next_occuring_event_full(double now)
159     {
160       NetworkAction *action = NULL;
161       ActionList *runningActions = surf_network_model->getRunningActionSet();
162       double minRes;
163
164       minRes = shareResourcesMaxMin(runningActions, surf_network_model->p_maxminSystem, surf_network_model->f_networkSolve);
165
166       for(ActionList::iterator it(runningActions->begin()), itend(runningActions->end())
167           ; it != itend ; ++it) {
168         action = static_cast<NetworkAction*>(&*it);
169 #ifdef HAVE_LATENCY_BOUND_TRACKING
170         if (lmm_is_variable_limited_by_latency(action->getVariable())) {
171           action->m_latencyLimited = 1;
172         } else {
173           action->m_latencyLimited = 0;
174         }
175 #endif
176         if (action->m_latency > 0) {
177           minRes = (minRes < 0) ? action->m_latency : std::min(minRes, action->m_latency);
178         }
179       }
180
181       XBT_DEBUG("Min of share resources %f", minRes);
182
183       return minRes;
184     }
185
186     /************
187      * Resource *
188      ************/
189
190     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
191     : Resource(model, name),
192       PropertyHolder(props)
193     {
194       links->insert({name, this});
195
196       m_latency.scale = 1;
197       m_bandwidth.scale = 1;
198       XBT_DEBUG("Create link '%s'",name);
199     }
200
201     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props,
202         lmm_constraint_t constraint,
203         tmgr_trace_t state_trace)
204     : Resource(model, name, constraint),
205       PropertyHolder(props)
206     {
207       m_latency.scale = 1;
208       m_bandwidth.scale = 1;
209       if (state_trace)
210         m_stateEvent = future_evt_set->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_latency.peak * m_latency.scale;
242     }
243
244     double Link::getBandwidth()
245     {
246       return m_bandwidth.peak * m_bandwidth.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     void Link::set_state_trace(tmgr_trace_t trace)
267     {
268       xbt_assert(m_stateEvent==NULL,"Cannot set a second state trace to Link %s", getName());
269
270       m_stateEvent = future_evt_set->add_trace(trace, 0.0, this);
271     }
272     void Link::set_bandwidth_trace(tmgr_trace_t trace)
273     {
274       xbt_assert(m_bandwidth.event==NULL,"Cannot set a second bandwidth trace to Link %s", getName());
275
276       m_bandwidth.event = future_evt_set->add_trace(trace, 0.0, this);
277     }
278     void Link::set_latency_trace(tmgr_trace_t trace)
279     {
280       xbt_assert(m_latency.event==NULL,"Cannot set a second latency trace to Link %s", getName());
281
282       m_latency.event = future_evt_set->add_trace(trace, 0.0, this);
283     }
284
285
286     /**********
287      * Action *
288      **********/
289
290     void NetworkAction::setState(e_surf_action_state_t state){
291       e_surf_action_state_t old = getState();
292       Action::setState(state);
293       networkActionStateChangedCallbacks(this, old, state);
294     }
295
296   }
297 }
298
299 #endif /* NETWORK_INTERFACE_CPP_ */