Logo AND Algorithmique Numérique Distribuée

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