Logo AND Algorithmique Numérique Distribuée

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