Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better use of inherency around Model::next_occuring_event_full()
[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->getBandwidth();
35   }
36   double sg_link_latency(Link *link){
37     return link->getLatency();
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,Link *> *Link::links = new std::unordered_map<std::string,Link *>();
65     Link *Link::byName(const char* name) {
66       if (links->find(name) == links->end())
67         return nullptr;
68       return  links->at(name);
69     }
70     /** @brief Returns the amount of links in the platform */
71     int Link::linksCount() {
72       return links->size();
73     }
74     /** @brief Returns a list of all existing links */
75     Link **Link::linksList() {
76       Link **res = xbt_new(Link*, (int)links->size());
77       int i=0;
78       for (auto kv : *links) {
79         res[i++] = kv.second;
80       }
81       return res;
82     }
83     /** @brief destructor of the static data */
84     void Link::linksExit() {
85       for (auto kv : *links)
86         (kv.second)->destroy();
87       delete links;
88     }
89
90     /*************
91      * Callbacks *
92      *************/
93
94     simgrid::xbt::signal<void(Link*)> Link::onCreation;
95     simgrid::xbt::signal<void(Link*)> Link::onDestruction;
96     simgrid::xbt::signal<void(Link*)> Link::onStateChange;
97
98     simgrid::xbt::signal<void(NetworkAction*, Action::State, Action::State)> networkActionStateChangedCallbacks;
99     simgrid::xbt::signal<void(NetworkAction*, kernel::routing::NetCard *src, kernel::routing::NetCard *dst)> Link::onCommunicate;
100
101   }
102 }
103
104 /*********
105  * Model *
106  *********/
107
108 simgrid::surf::NetworkModel *surf_network_model = nullptr;
109
110 namespace simgrid {
111   namespace surf {
112
113     NetworkModel::~NetworkModel()
114     {
115       lmm_system_free(maxminSystem_);
116       xbt_heap_free(actionHeap_);
117       delete modifiedSet_;
118     }
119
120     double NetworkModel::latencyFactor(double /*size*/) {
121       return sg_latency_factor;
122     }
123
124     double NetworkModel::bandwidthFactor(double /*size*/) {
125       return sg_bandwidth_factor;
126     }
127
128     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
129       return rate;
130     }
131
132     double NetworkModel::next_occuring_event_full(double now)
133     {
134       double minRes = Model::next_occuring_event_full(now);
135
136       for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
137         NetworkAction *action = static_cast<NetworkAction*>(&*it);
138         if (action->latency_ > 0)
139           minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
140       }
141
142       XBT_DEBUG("Min of share resources %f", minRes);
143
144       return minRes;
145     }
146
147     /************
148      * Resource *
149      ************/
150
151     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
152     : Resource(model, name),
153       PropertyHolder(props)
154     {
155       links->insert({name, this});
156
157       m_latency.scale = 1;
158       m_bandwidth.scale = 1;
159       XBT_DEBUG("Create link '%s'",name);
160     }
161
162     Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint)
163     : Resource(model, name, constraint),
164       PropertyHolder(props)
165     {
166       if (strcmp(name,"__loopback__"))
167         xbt_assert(!Link::byName(name), "Link '%s' declared several times in the platform.", name);
168
169       m_latency.scale = 1;
170       m_bandwidth.scale = 1;
171
172       links->insert({name, this});
173       XBT_DEBUG("Create link '%s'",name);
174
175     }
176
177     /** @brief use destroy() instead of this destructor */
178     Link::~Link() {
179       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
180     }
181     /** @brief Fire the require callbacks and destroy the object
182      *
183      * Don't delete directly an Link, call l->destroy() instead.
184      */
185     void Link::destroy()
186     {
187       if (!currentlyDestroying_) {
188         currentlyDestroying_ = true;
189         onDestruction(this);
190         delete this;
191       }
192     }
193
194     bool Link::isUsed()
195     {
196       return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
197     }
198
199     double Link::getLatency()
200     {
201       return m_latency.peak * m_latency.scale;
202     }
203
204     double Link::getBandwidth()
205     {
206       return m_bandwidth.peak * m_bandwidth.scale;
207     }
208
209     int Link::sharingPolicy()
210     {
211       return lmm_constraint_sharing_policy(getConstraint());
212     }
213
214     void Link::turnOn(){
215       if (isOff()) {
216         Resource::turnOn();
217         onStateChange(this);
218       }
219     }
220     void Link::turnOff(){
221       if (isOn()) {
222         Resource::turnOff();
223         onStateChange(this);
224       }
225     }
226     void Link::setStateTrace(tmgr_trace_t trace) {
227       xbt_assert(m_stateEvent==nullptr,"Cannot set a second state trace to Link %s", getName());
228       m_stateEvent = future_evt_set->add_trace(trace, 0.0, this);
229     }
230     void Link::setBandwidthTrace(tmgr_trace_t trace)
231     {
232       xbt_assert(m_bandwidth.event==nullptr,"Cannot set a second bandwidth trace to Link %s", getName());
233       m_bandwidth.event = future_evt_set->add_trace(trace, 0.0, this);
234     }
235     void Link::setLatencyTrace(tmgr_trace_t trace)
236     {
237       xbt_assert(m_latency.event==nullptr,"Cannot set a second latency trace to Link %s", getName());
238       m_latency.event = future_evt_set->add_trace(trace, 0.0, this);
239     }
240
241
242     /**********
243      * Action *
244      **********/
245
246     void NetworkAction::setState(Action::State state){
247       Action::State old = getState();
248       Action::setState(state);
249       networkActionStateChangedCallbacks(this, old, state);
250     }
251
252   }
253 }
254
255 #endif /* NETWORK_INTERFACE_CPP_ */