Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove one of the many pimple: HostModel::p_cpuModel
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2014. 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 }
83 /*************
84  * Callbacks *
85  *************/
86
87 surf_callback(void, Link*) networkLinkCreatedCallbacks;
88 surf_callback(void, Link*) networkLinkDestructedCallbacks;
89 surf_callback(void, Link*, e_surf_resource_state_t, e_surf_resource_state_t) networkLinkStateChangedCallbacks;
90 surf_callback(void, NetworkAction*, e_surf_action_state_t, e_surf_action_state_t) networkActionStateChangedCallbacks;
91 surf_callback(void, NetworkAction*, RoutingEdge *src, RoutingEdge *dst, double size, double rate) networkCommunicateCallbacks;
92
93 void netlink_parse_init(sg_platf_link_cbarg_t link){
94   if (link->policy == SURF_LINK_FULLDUPLEX) {
95     char *link_id;
96     link_id = bprintf("%s_UP", link->id);
97     surf_network_model->createLink(link_id,
98                       link->bandwidth,
99                       link->bandwidth_trace,
100                       link->latency,
101                       link->latency_trace,
102                       link->state,
103                       link->state_trace, link->policy, link->properties);
104     xbt_free(link_id);
105     link_id = bprintf("%s_DOWN", link->id);
106     surf_network_model->createLink(link_id,
107                       link->bandwidth,
108                       link->bandwidth_trace,
109                       link->latency,
110                       link->latency_trace,
111                       link->state,
112                       link->state_trace, link->policy, link->properties);
113     xbt_free(link_id);
114   } else {
115           surf_network_model->createLink(link->id,
116                           link->bandwidth,
117                           link->bandwidth_trace,
118                           link->latency,
119                           link->latency_trace,
120                           link->state,
121                           link->state_trace, link->policy, link->properties);
122   }
123 }
124
125 void net_add_traces(){
126   surf_network_model->addTraces();
127 }
128
129 /*********
130  * Model *
131  *********/
132
133 NetworkModel *surf_network_model = NULL;
134
135 double NetworkModel::latencyFactor(double /*size*/) {
136   return sg_latency_factor;
137 }
138
139 double NetworkModel::bandwidthFactor(double /*size*/) {
140   return sg_bandwidth_factor;
141 }
142
143 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
144   return rate;
145 }
146
147 double NetworkModel::shareResourcesFull(double now)
148 {
149   NetworkAction *action = NULL;
150   ActionList *runningActions = surf_network_model->getRunningActionSet();
151   double minRes;
152
153   minRes = shareResourcesMaxMin(runningActions, surf_network_model->p_maxminSystem, surf_network_model->f_networkSolve);
154
155   for(ActionList::iterator it(runningActions->begin()), itend(runningActions->end())
156        ; it != itend ; ++it) {
157       action = static_cast<NetworkAction*>(&*it);
158 #ifdef HAVE_LATENCY_BOUND_TRACKING
159     if (lmm_is_variable_limited_by_latency(action->getVariable())) {
160       action->m_latencyLimited = 1;
161     } else {
162       action->m_latencyLimited = 0;
163     }
164 #endif
165     if (action->m_latency > 0) {
166       minRes = (minRes < 0) ? action->m_latency : min(minRes, action->m_latency);
167     }
168   }
169
170   XBT_DEBUG("Min of share resources %f", minRes);
171
172   return minRes;
173 }
174
175 /************
176  * Resource *
177  ************/
178
179 Link::Link(NetworkModel *model, const char *name, xbt_dict_t props)
180 : Resource(model, name, props)
181 , p_latEvent(NULL)
182 {
183   surf_callback_emit(networkLinkCreatedCallbacks, this);
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   surf_callback_emit(networkLinkCreatedCallbacks, this);
197   if (state_trace)
198     p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
199
200   links->insert({name, this});
201   XBT_DEBUG("Create link '%s'",name);
202
203 }
204
205 Link::~Link()
206 {
207   surf_callback_emit(networkLinkDestructedCallbacks, this);
208 }
209
210 bool Link::isUsed()
211 {
212   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
213 }
214
215 double Link::getLatency()
216 {
217   return m_latCurrent;
218 }
219
220 double Link::getBandwidth()
221 {
222   return p_power.peak * p_power.scale;
223 }
224
225 bool Link::isShared()
226 {
227   return lmm_constraint_is_shared(getConstraint());
228 }
229
230 void Link::setState(e_surf_resource_state_t state){
231   e_surf_resource_state_t old = Resource::getState();
232   Resource::setState(state);
233   surf_callback_emit(networkLinkStateChangedCallbacks, this, old, state);
234 }
235
236
237
238 /**********
239  * Action *
240  **********/
241
242 void NetworkAction::setState(e_surf_action_state_t state){
243   e_surf_action_state_t old = getState();
244   Action::setState(state);
245   surf_callback_emit(networkActionStateChangedCallbacks, this, old, state);
246 }
247
248 #endif /* NETWORK_INTERFACE_CPP_ */