Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reimplement energy plugin using Host callbacks (not Cpu ones)
[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 "network_interface.hpp"
8 #include "simgrid/sg_config.h"
9
10 #ifndef NETWORK_INTERFACE_CPP_
11 #define NETWORK_INTERFACE_CPP_
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
14                                 "Logging specific to the SURF network module");
15
16 /*********
17  * C API *
18  *********/
19 SG_BEGIN_DECL()
20 const char* sg_link_name(Link *link) {
21   return link->getName();
22 }
23 Link * sg_link_by_name(const char* name) {
24   return Link::byName(name);
25 }
26
27 int sg_link_is_shared(Link *link){
28   return link->isShared();
29 }
30 double sg_link_bandwidth(Link *link){
31   return link->getBandwidth();
32 }
33 double sg_link_latency(Link *link){
34   return link->getLatency();
35 }
36 void* sg_link_data(Link *link) {
37         return link->getData();
38 }
39 void sg_link_data_set(Link *link,void *data) {
40         link->setData(data);
41 }
42 int sg_link_amount(void) {
43         return Link::linksAmount();
44 }
45 Link** sg_link_list(void) {
46         return Link::linksList();
47 }
48 void sg_link_exit(void) {
49         Link::linksExit();
50 }
51 SG_END_DECL()
52 /*****************
53  * List of links *
54  *****************/
55
56 boost::unordered_map<std::string,Link *> *Link::links = new boost::unordered_map<std::string,Link *>();
57 Link *Link::byName(const char* name) {
58           Link * res = NULL;
59           try {
60                   res = links->at(name);
61           } catch (std::out_of_range& e) {}
62
63           return res;
64 }
65 /** @brief Returns the amount of links in the platform */
66 int Link::linksAmount() {
67           return links->size();
68 }
69 /** @brief Returns a list of all existing links */
70 Link **Link::linksList() {
71           Link **res = xbt_new(Link*, (int)links->size());
72           int i=0;
73           for (auto kv : *links) {
74                   res[i++] = kv.second;
75           }
76           return res;
77 }
78 /** @brief destructor of the static data */
79 void Link::linksExit() {
80         for (auto kv : *links)
81                 delete (kv.second);
82         delete links;
83 }
84 /*************
85  * Callbacks *
86  *************/
87
88 surf_callback(void, Link*) networkLinkCreatedCallbacks;
89 surf_callback(void, Link*) networkLinkDestructedCallbacks;
90 surf_callback(void, Link*, e_surf_resource_state_t, e_surf_resource_state_t) networkLinkStateChangedCallbacks;
91 surf_callback(void, NetworkAction*, e_surf_action_state_t, e_surf_action_state_t) networkActionStateChangedCallbacks;
92 surf_callback(void, NetworkAction*, RoutingEdge *src, RoutingEdge *dst, double size, double rate) networkCommunicateCallbacks;
93
94 void netlink_parse_init(sg_platf_link_cbarg_t link){
95   if (link->policy == SURF_LINK_FULLDUPLEX) {
96     char *link_id;
97     link_id = bprintf("%s_UP", link->id);
98     surf_network_model->createLink(link_id,
99                       link->bandwidth,
100                       link->bandwidth_trace,
101                       link->latency,
102                       link->latency_trace,
103                       link->state,
104                       link->state_trace, link->policy, link->properties);
105     xbt_free(link_id);
106     link_id = bprintf("%s_DOWN", link->id);
107     surf_network_model->createLink(link_id,
108                       link->bandwidth,
109                       link->bandwidth_trace,
110                       link->latency,
111                       link->latency_trace,
112                       link->state,
113                       link->state_trace, link->policy, link->properties);
114     xbt_free(link_id);
115   } else {
116           surf_network_model->createLink(link->id,
117                           link->bandwidth,
118                           link->bandwidth_trace,
119                           link->latency,
120                           link->latency_trace,
121                           link->state,
122                           link->state_trace, link->policy, link->properties);
123   }
124 }
125
126 void net_add_traces(){
127   surf_network_model->addTraces();
128 }
129
130 /*********
131  * Model *
132  *********/
133
134 NetworkModel *surf_network_model = NULL;
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::shareResourcesFull(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->p_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 #ifdef HAVE_LATENCY_BOUND_TRACKING
160     if (lmm_is_variable_limited_by_latency(action->getVariable())) {
161       action->m_latencyLimited = 1;
162     } else {
163       action->m_latencyLimited = 0;
164     }
165 #endif
166     if (action->m_latency > 0) {
167       minRes = (minRes < 0) ? action->m_latency : min(minRes, action->m_latency);
168     }
169   }
170
171   XBT_DEBUG("Min of share resources %f", minRes);
172
173   return minRes;
174 }
175
176 /************
177  * Resource *
178  ************/
179
180 Link::Link(NetworkModel *model, const char *name, xbt_dict_t props)
181 : Resource(model, name, props)
182 , p_latEvent(NULL)
183 {
184   links->insert({name, this});
185
186   XBT_DEBUG("Create link '%s'",name);
187 }
188
189 Link::Link(NetworkModel *model, const char *name, xbt_dict_t props,
190                                  lmm_constraint_t constraint,
191                              tmgr_history_t history,
192                              tmgr_trace_t state_trace)
193 : Resource(model, name, props, constraint),
194   p_latEvent(NULL)
195 {
196   if (state_trace)
197     p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
198
199   links->insert({name, this});
200   XBT_DEBUG("Create link '%s'",name);
201
202 }
203
204 Link::~Link()
205 {
206   surf_callback_emit(networkLinkDestructedCallbacks, this);
207 }
208
209 bool Link::isUsed()
210 {
211   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
212 }
213
214 double Link::getLatency()
215 {
216   return m_latCurrent;
217 }
218
219 double Link::getBandwidth()
220 {
221   return p_power.peak * p_power.scale;
222 }
223
224 bool Link::isShared()
225 {
226   return lmm_constraint_is_shared(getConstraint());
227 }
228
229 void Link::setState(e_surf_resource_state_t state){
230   e_surf_resource_state_t old = Resource::getState();
231   Resource::setState(state);
232   surf_callback_emit(networkLinkStateChangedCallbacks, this, old, state);
233 }
234
235
236
237 /**********
238  * Action *
239  **********/
240
241 void NetworkAction::setState(e_surf_action_state_t state){
242   e_surf_action_state_t old = getState();
243   Action::setState(state);
244   surf_callback_emit(networkActionStateChangedCallbacks, this, old, state);
245 }
246
247 #endif /* NETWORK_INTERFACE_CPP_ */