Logo AND Algorithmique Numérique Distribuée

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