Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
the property holder does not take a dict in its constructor
[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,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*, s4u::Host* src, s4u::Host* dst)> Link::onCommunicate;
100   }
101 }
102
103 /*********
104  * Model *
105  *********/
106
107 simgrid::surf::NetworkModel *surf_network_model = nullptr;
108
109 namespace simgrid {
110   namespace surf {
111
112     NetworkModel::~NetworkModel()
113     {
114       lmm_system_free(maxminSystem_);
115       xbt_heap_free(actionHeap_);
116       delete modifiedSet_;
117     }
118
119     double NetworkModel::latencyFactor(double /*size*/) {
120       return sg_latency_factor;
121     }
122
123     double NetworkModel::bandwidthFactor(double /*size*/) {
124       return sg_bandwidth_factor;
125     }
126
127     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
128       return rate;
129     }
130
131     double NetworkModel::nextOccuringEventFull(double now)
132     {
133       double minRes = Model::nextOccuringEventFull(now);
134
135       for(auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; it++) {
136         NetworkAction *action = static_cast<NetworkAction*>(&*it);
137         if (action->latency_ > 0)
138           minRes = (minRes < 0) ? action->latency_ : std::min(minRes, action->latency_);
139       }
140
141       XBT_DEBUG("Min of share resources %f", minRes);
142
143       return minRes;
144     }
145
146     /************
147      * Resource *
148      ************/
149
150     Link::Link(simgrid::surf::NetworkModel* model, const char* name, lmm_constraint_t constraint)
151         : Resource(model, name, constraint)
152     {
153       if (strcmp(name,"__loopback__"))
154         xbt_assert(!Link::byName(name), "Link '%s' declared several times in the platform.", name);
155
156       latency_.scale   = 1;
157       bandwidth_.scale = 1;
158
159       links->insert({name, this});
160       XBT_DEBUG("Create link '%s'",name);
161
162     }
163
164     /** @brief use destroy() instead of this destructor */
165     Link::~Link() {
166       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
167     }
168     /** @brief Fire the required callbacks and destroy the object
169      *
170      * Don't delete directly a Link, call l->destroy() instead.
171      */
172     void Link::destroy()
173     {
174       if (!currentlyDestroying_) {
175         currentlyDestroying_ = true;
176         onDestruction(this);
177         delete this;
178       }
179     }
180
181     bool Link::isUsed()
182     {
183       return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
184     }
185
186     double Link::latency()
187     {
188       return latency_.peak * latency_.scale;
189     }
190
191     double Link::bandwidth()
192     {
193       return bandwidth_.peak * bandwidth_.scale;
194     }
195
196     int Link::sharingPolicy()
197     {
198       return lmm_constraint_sharing_policy(getConstraint());
199     }
200
201     void Link::turnOn(){
202       if (isOff()) {
203         Resource::turnOn();
204         onStateChange(this);
205       }
206     }
207     void Link::turnOff(){
208       if (isOn()) {
209         Resource::turnOff();
210         onStateChange(this);
211       }
212     }
213     void Link::setStateTrace(tmgr_trace_t trace) {
214       xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getName());
215       stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
216     }
217     void Link::setBandwidthTrace(tmgr_trace_t trace)
218     {
219       xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getName());
220       bandwidth_.event = future_evt_set->add_trace(trace, 0.0, this);
221     }
222     void Link::setLatencyTrace(tmgr_trace_t trace)
223     {
224       xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getName());
225       latency_.event = future_evt_set->add_trace(trace, 0.0, this);
226     }
227
228
229     /**********
230      * Action *
231      **********/
232
233     void NetworkAction::setState(Action::State state){
234       Action::State old = getState();
235       Action::setState(state);
236       networkActionStateChangedCallbacks(this, old, state);
237     }
238
239   }
240 }
241
242 #endif /* NETWORK_INTERFACE_CPP_ */