Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
efaeacc3fe199210f9665964a47cce99ccfec013
[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, "Logging specific to the SURF network module");
16
17 /*********
18  * C API *
19  *********/
20
21 extern "C" {
22
23   const char* sg_link_name(Link *link) {
24     return link->getName();
25   }
26   Link * sg_link_by_name(const char* name) {
27     return Link::byName(name);
28   }
29
30   int sg_link_is_shared(Link *link){
31     return link->sharingPolicy();
32   }
33   double sg_link_bandwidth(Link *link){
34     return link->bandwidth();
35   }
36   double sg_link_latency(Link *link){
37     return link->latency();
38   }
39   void* sg_link_data(Link *link) {
40     return link->getData();
41   }
42   void sg_link_data_set(Link *link,void *data) {
43     link->setData(data);
44   }
45   int sg_link_count() {
46     return Link::linksCount();
47   }
48   Link** sg_link_list() {
49     return Link::linksList();
50   }
51   void sg_link_exit() {
52     Link::linksExit();
53   }
54
55 }
56
57 /*****************
58  * List of links *
59  *****************/
60
61 namespace simgrid {
62   namespace surf {
63
64   std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
65   LinkImpl* LinkImpl::byName(const char* name)
66   {
67     if (links->find(name) == links->end())
68       return nullptr;
69     return links->at(name);
70     }
71     /** @brief Returns the amount of links in the platform */
72     int LinkImpl::linksCount()
73     {
74       return links->size();
75     }
76     /** @brief Returns a list of all existing links */
77     LinkImpl** LinkImpl::linksList()
78     {
79       LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
80       int i=0;
81       for (auto kv : *links) {
82         res[i++] = kv.second;
83       }
84       return res;
85     }
86     /** @brief destructor of the static data */
87     void LinkImpl::linksExit()
88     {
89       for (auto kv : *links)
90         (kv.second)->destroy();
91       delete links;
92     }
93
94     /*************
95      * Callbacks *
96      *************/
97
98     simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onCreation;
99     simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onDestruction;
100     simgrid::xbt::signal<void(LinkImpl*)> LinkImpl::onStateChange;
101
102     simgrid::xbt::signal<void(NetworkAction*, Action::State, Action::State)> networkActionStateChangedCallbacks;
103     simgrid::xbt::signal<void(NetworkAction*, s4u::Host* src, s4u::Host* dst)> LinkImpl::onCommunicate;
104   }
105 }
106
107 /*********
108  * Model *
109  *********/
110
111 simgrid::surf::NetworkModel *surf_network_model = nullptr;
112
113 namespace simgrid {
114   namespace surf {
115
116     NetworkModel::~NetworkModel()
117     {
118       lmm_system_free(maxminSystem_);
119       xbt_heap_free(actionHeap_);
120       delete modifiedSet_;
121     }
122
123     double NetworkModel::latencyFactor(double /*size*/) {
124       return sg_latency_factor;
125     }
126
127     double NetworkModel::bandwidthFactor(double /*size*/) {
128       return sg_bandwidth_factor;
129     }
130
131     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
132       return rate;
133     }
134
135     double NetworkModel::nextOccuringEventFull(double now)
136     {
137       double minRes = Model::nextOccuringEventFull(now);
138
139       for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
140         NetworkAction *action = static_cast<NetworkAction*>(&*it);
141         if (action->latency_ > 0)
142           minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
143       }
144
145       XBT_DEBUG("Min of share resources %f", minRes);
146
147       return minRes;
148     }
149
150     /************
151      * Resource *
152      ************/
153
154     LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
155         : Resource(model, name, constraint)
156     {
157       if (strcmp(name,"__loopback__"))
158         xbt_assert(!LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name);
159
160       latency_.scale   = 1;
161       bandwidth_.scale = 1;
162
163       links->insert({name, this});
164       XBT_DEBUG("Create link '%s'",name);
165
166     }
167
168     /** @brief use destroy() instead of this destructor */
169     LinkImpl::~LinkImpl()
170     {
171       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
172     }
173     /** @brief Fire the required callbacks and destroy the object
174      *
175      * Don't delete directly a Link, call l->destroy() instead.
176      */
177     void LinkImpl::destroy()
178     {
179       if (!currentlyDestroying_) {
180         currentlyDestroying_ = true;
181         onDestruction(this);
182         delete this;
183       }
184     }
185
186     bool LinkImpl::isUsed()
187     {
188       return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
189     }
190
191     double LinkImpl::latency()
192     {
193       return latency_.peak * latency_.scale;
194     }
195
196     double LinkImpl::bandwidth()
197     {
198       return bandwidth_.peak * bandwidth_.scale;
199     }
200
201     int LinkImpl::sharingPolicy()
202     {
203       return lmm_constraint_sharing_policy(getConstraint());
204     }
205
206     void LinkImpl::turnOn()
207     {
208       if (isOff()) {
209         Resource::turnOn();
210         onStateChange(this);
211       }
212     }
213     void LinkImpl::turnOff()
214     {
215       if (isOn()) {
216         Resource::turnOff();
217         onStateChange(this);
218       }
219     }
220     void LinkImpl::setStateTrace(tmgr_trace_t trace)
221     {
222       xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getName());
223       stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
224     }
225     void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
226     {
227       xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getName());
228       bandwidth_.event = future_evt_set->add_trace(trace, 0.0, this);
229     }
230     void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
231     {
232       xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getName());
233       latency_.event = future_evt_set->add_trace(trace, 0.0, this);
234     }
235
236
237     /**********
238      * Action *
239      **********/
240
241     void NetworkAction::setState(Action::State state){
242       Action::State old = getState();
243       Action::setState(state);
244       networkActionStateChangedCallbacks(this, old, state);
245     }
246
247   }
248 }
249
250 #endif /* NETWORK_INTERFACE_CPP_ */